Repository navigation
Conversation
|
Sorry @aaronanish-t for potentially sniping you, but my models ran into a slightly modified version of original #2691 (inc and sum rewrites) and I figured it makes sense to cover the original proposal as well in a slightly more general rewrite PR |
785c441 to
387577f
Compare
|
The Benchmarks job failed on |
x[1:3].set(v)[2] -> v[1], and the no-coverage and partial cases, per axis. The basic-index counterpart of local_advanced_read_of_write_constant_indices. Closes pymc-devs#2691 Co-Authored-By: Claude Fable 5.2 <[email protected]>
x[:, 1:3].inc(v).sum(axis=0) -> x.sum(axis=0)[1:3].inc(v.sum(axis=0)) Co-Authored-By: Claude Fable 5.2 <[email protected]>
387577f to
9781045
Compare
|
I pushed some simplifications/comments on the first rewrite that I'm on board with (just extending what we already were trying to do for advanced cases). I'm not sure how to think about multiple clients. In the read of write rewrite you gate on other clients, except if they have ops that may or may not be simplified by other rewrites (we never know unless you reimplemented the rewrite checks in this rewrite - obviously not a solution). I don't have good answers here, but note the adavced version did not gate on other clients. Then on the sum rewrite, you do not check for other clients (otherwise you'd have deadlocked anyway in your motivating example I suppose). But it's not a principled way of going about this. I DO NOT HAVE the magical solution either. Anyway a couple of concerns on the new sum rewrite (which doesn't have an advanced counterpart, should it?):
|
bbd8fa0 to
7f69921
Compare
The store truncates x + v, not v. Co-Authored-By: Claude Fable 5.2 <[email protected]>
|
Thanks for the simplifications, they read better. Multiple clients. I agree it isn't principled, but each gate is there because removing it measurably hurt (Numba, logp + dlogp, paired runs against main):
You're right that a Sum rewrite.
|
This is very loaded. "Narrow" in one example you're looking at. And it's not the read that's the concern it's the duplicated write(s)
Again "small" qualifiers don't make a good argument. I can build a counterexample where you nearly double the cost of whatever gigantic array this applies in Although sum isn't necessarily very expensive vs copy |
|
I didn't argue for single client rule btw, I said the logic was unprincipled and made one rewrite the "reference" coordinator/aware of the others. Those others have to then be unconditional for the "coordination to work". |
The issue with that is you protected against a regression but you have no guarantees the cases you let pass through aren't still gonna regress in other cases. |
A read that computes something and a Sum are only rewritten when every client of the write can be, so the write is never built next to its replacements. Reads that cost nothing stay in local_basic_read_of_write_constant_indices. Co-Authored-By: Claude Fable 5.2 <[email protected]>
|
You're right on all four, and "narrow" and "small" were my qualifiers, not bounds. I've pushed a restructure (9a0f883) that removes the coordination:
So there is one rewrite, no knowledge of other rewrites, and the write has no clients afterwards: it is never built next to its replacements. That covers the duplicated-write concern. Numbers (Numba, logp + dlogp, one paired run each against main):
What it does not fix:
I updated the PR description to match. |
|
For the sum of set how crazy to sum only the part that's not shadowed and then combine with two sets? |
sum(x) would also sum what the set overwrites. Co-Authored-By: Claude Fable 5.2 <[email protected]>
Not crazy for a write sliced on one kept axis: it is a join of up to three sums, What I pushed instead (fbd432b): the sum of a set is only rewritten when the base is a constant or an It costs nothing on the benchmarks (one paired run each against main): voting 33.8 -> 25.0 ms (24.1 ms before this commit, same run, within noise: minima 22.3 and 22.8), Happy to add the join for the one-axis case if you'd rather have that than skip it. |
|
No I'm happy with that direction |
Co-Authored-By: Claude Fable 5.2 <[email protected]>
Co-Authored-By: Claude Fable 5.2 <[email protected]>
Summary
Closes #2691.
The gradient of a slice is a full-size array of zeros with the small gradient block written into it. When that array is then only read through other slices or sums, almost all the work is filling and reading zeros. Two rewrites skip it:
The first is the basic-index counterpart of
local_advanced_read_of_write_constant_indices, with the same three outcomes and the same registration. Coverage is decided per axis, for constant scalars and unit-step slices (or the very same index on both sides); negative entries need a static length.Fusion: the result reads
vthrough basic indices only, which lift into whatever producesv, and the coveredinccase is an add of two views. No advanced index is introduced.Pessimisation: the first rewrite only makes views. The second computes something new for each client (an add, a patched copy of the read region, a sum of
v), so it replaces all clients of the write together or none: the write is never built next to its replacements. It skips a write that covers all ofx, whichlocal_useless_inc_subtensorturns into an add. The sum of a set is only rewritten for a constant orAllocbase, whose sum folds; otherwise it would also sum what the set overwrites. What remains: one wide operation can become several narrow ones.Not done here: steps other than 1, an advanced write read by a scalar (
x[[1, 2]].set(v)[2], mentioned in #2691), sums of advanced writes, other reductions.Known limitation: under JAX, with unknown static shapes, the read of a gradient's zeros becomes
zeros(w.shape[0], k), which JAX'sAllocrejects ("shapes must be concrete").pt.zeros((w.shape[0], w.shape[1]))[:, 5:8]fails the same way on main. No test intests/link/jaxhits it.Evidence
pt.zeros(4)[1:3].set(v)[2]compiles toAlloc, SetSubtensor{start:stop}, Subtensor{i}After:
Subtensor{i}(v[1])logp + dlogp, Numba, median eval per call, paired runs against main.
votingis a large private hierarchical model whose gradients are zero-padded column blocks (1303 x 313 buffers); the other two are in pymc-devs/pymc-model-catalogue#27:survey_split_ballot_scoped_multinomialsurvey_split_ballot_multinomialAn earlier revision, which rewrote reads one at a time, was 2.2x slower on
survey_split_ballot_multinomial; that is why all clients are replaced together. The earlier revision was also measured on a second large model (34.9 -> 21.2 ms) and on 199 pymc-examples models (eval geomean 1.000); neither has been re-run on this one.Merge Danger
Door: two-way
Blast Radius: default-mode graphs
Both rewrites are
shape_unsafe: a write whose value has the wrong runtime shape may no longer raise once the write is rewritten away.🤖 Generated with Claude Code