Numba: fuse AdvancedSubtensor->Elemwise->AdvancedIncSubtensor#2015
Merged
Conversation
6d875d8 to
0ad6e2e
Compare
41869a4 to
a07997b
Compare
2b65554 to
f939f9c
Compare
4cc068c to
e5e5ea0
Compare
2758911 to
cb1e83d
Compare
Member
Author
|
Here is a benchmark experiment with a subset of models where the rewrite fires: https://ricardov94.github.io/pymc-model-catalogue/experiments.html#base=index_elemwise_fusion_curated_base&compare=index_elemwise_fusion_curated Nice gains in eval time for quite some models |
…ariables When a shared variable's update is deleted but the variable is still destroyed (mutated inplace) by a node in the copied graph, the shared variable storage will still be mutated. Emit a UserWarning in this case.
Extend FusionOptimizer to merge independent subgraphs that share inputs but have no producer-consumer edge (siblings like f(x) and g(x)). The eager expansion only walks producer-consumer edges, missing these. Also extract InplaceGraphOptimizer.try_inplace_on_node helper and _insert_sorted_subgraph to deduplicate insertion-point logic.
Extract _decode_literal, _compute_vectorized_types, and _codegen_return_outputs from the monolithic _vectorized intrinsic. Add NO_SIZE sentinel. Rename variables in make_loop_call for clarity (input→inp, output→out, idxs→loop_idxs, ptr→read_ptr/write_ptr, val→read_val/write_val, core_arry_type→read_array_type/write_array_type). Move _vectorized below make_loop_call. Fixes input_type.layout bug in inplace type computation (was using leaked variable from prior loop).
cb1e83d to
9592fbe
Compare
ricardoV94
commented
Jun 1, 2026
| from pytensor.link.numba.dispatch import basic as numba_basic | ||
|
|
||
|
|
||
| # Numba is missing getitem(array, Ellipsis), so o[...] += val fails. |
Member
Author
There was a problem hiding this comment.
TODO: This is not equivalent, only in +=, but most of the times is what we want. Anyway better to use a custom fn in the store_core_outputs and only overload that
Member
Author
There was a problem hiding this comment.
store_inplace, inc_inplace.
ricardoV94
commented
Jun 1, 2026
|
|
||
| Examples:: | ||
|
|
||
| tgt[idx] += exp(x) → indexed_outputs=[((0,), 0, "inc")] |
Member
Author
There was a problem hiding this comment.
todo: check didn't we get rid of inc/set
jessegrabowski
approved these changes
Jun 1, 2026
jessegrabowski
left a comment
Member
There was a problem hiding this comment.
we went over this in a call, Ricardo explained it to me and it seems good.
Add IndexedElemwise(OpFromGraph) and FuseIndexedElemwise graph rewriter that detects AdvancedSubtensor1/AdvancedSubtensor on Elemwise inputs and AdvancedIncSubtensor1/AdvancedIncSubtensor on outputs, fusing them into a single vectorized loop with indirect indexing. Supports arbitrary-axis indexing, ND index arrays, multi-index groups, broadcast indices, repeated accumulation (inc/set modes), and multi-client outputs via Composite identity duplication.
9592fbe to
7d870ea
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Introduce
IndexedElemwise, anOpFromGraphthat wrapsAdvancedSubtensor+Elemwise+AdvancedIncSubtensorsubgraphs so the Numba backend can generate a single loop with indirect indexing, avoiding materializing AdvancedSubtensor input arrays, and writing directly on the output buffer, doing the job of AdvancedIncSubtensor in the same loop, without having to loop again through the intermediate elemwise output.Supports arbitrary-axis indexing, ND index arrays, multi-index groups, broadcast indices, repeated accumulation (inc/set modes), and multi-client outputs via Composite identity duplication.
Also included:
FusionOptimizerto merge independent sibling subgraphs that share inputs but have no producer-consumer edge (e.g.f(x)andg(x))Motivation
In hierarchical models with mu = beta[group_idx] * x + ..., the logp+gradient graph combines indexed reads and indexed updates in the same Elemwise (the forward expands group-level parameters via advanced subtensor, and the gradient accumulates back into the source via advanced inc subtensor).
A simple example
Next step would be to also fuse the sum directly on the elemwise, so we end up with a single loop over the data.