kups.core.utils.segment
¶
Native single-segment sums, wider scatters, and their gather adjoint.
Provides segment_sum, a drop-in
jax.ops.segment_sum whose scatter accumulates in a wider float, and
segment_take, the gather that is its
exact adjoint.
bincount(x, *, length)
¶
Count flattened integer indices into a fixed number of bins.
As in jnp.bincount, negative indices count in bin zero and indices at
or above length are dropped. Counts use JAX's default integer dtype.
Reuses segment_sum's reduction for a single bin.
Source code in src/kups/core/utils/segment.py
segment_sum(data, segment_ids, num_segments, *, mode=None)
¶
Sum into bins, using a native reduction for one bin and wider scatters otherwise.
Drop-in replacement for jax.ops.segment_sum: the same value in exact
arithmetic and the same mode for segment ids outside [0, num_segments),
dropped by default, negatives included.
A single segment uses a masked jnp.sum with native accumulation precision
and retains data's dtype; float32 is not promoted to float64. Otherwise,
the scatter accumulates one float wider than data and converts back once,
f32 in f64 and f16 or bf16 in f32, so a bin taking k contributions rounds
once rather than k times. With multiple segments, integer data and empty
inputs use the plain scatter. segment_ids may additionally
cover several leading axes of data rather than only the first, which
jax.ops.segment_sum rejects.
Linear in data and non-differentiable in the integral segment_ids, so it
transforms under jit, vmap over either argument, forward mode,
reverse mode, and repeated differentiation. The reverse pass is
segment_take under the same mode,
its exact adjoint.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Array
|
Contributions of shape |
required |
segment_ids
|
Array
|
Bin index per contribution, of any shape that is a prefix of
|
required |
num_segments
|
int
|
Number of output bins. |
required |
mode
|
Mode | str | None
|
How to treat ids outside |
None
|
Returns:
| Type | Description |
|---|---|
Array
|
Bin sums of shape |
Array
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Example
Source code in src/kups/core/utils/segment.py
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 | |
segment_take(data, segment_ids, *, mode=None, fill_value=None)
¶
Gather rows of data by segment id, summing the cotangents stably.
The forward pass moves rows and is exact; the accuracy is in the reverse
pass, which sums the cotangents landing in each row through
segment_sum rather than the serial
jnp.zeros(...).at[segment_ids].add(...) that differentiating
data[segment_ids] gives. The two are exact adjoints under the same mode
and a zero fill_value, so (segment_take(data, ids) * cotangent).sum()
equals
(data * segment_sum(cotangent, ids, len(data))).sum(), and each is the
other's transpose, so differentiating either repeatedly keeps using the wide
accumulator instead of falling back to the plain scatter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Array
|
Rows to gather from, shape |
required |
segment_ids
|
Array
|
Row index per output, of any shape. |
required |
mode
|
Mode | str | None
|
How to treat ids outside |
None
|
fill_value
|
ArrayLike | None
|
Value the out-of-range outputs take under the default |
None
|
Returns:
| Type | Description |
|---|---|
Array
|
Gathered rows of shape |
Array
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Example
Source code in src/kups/core/utils/segment.py
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 | |