kups.core.utils.segment
¶
Segment sums accumulated in a wider float, 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.
segment_sum(data, segment_ids, num_segments, *, mode=None)
¶
Sum data into num_segments bins, accumulating in a wider float.
Drop-in replacement for jax.ops.segment_sum: the same value in exact
arithmetic, closer to it in floating point, and the same mode for segment
ids outside [0, num_segments), dropped by default, negatives included.
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. Integer data and empty inputs fall through to
the plain scatter, which is already exact. 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
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
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 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 | |