Skip to content

Add rewriting with Theorem 1 - #21

Draft
ValerianRey wants to merge 1 commit into
mainfrom
add-theorem-1-rewriting
Draft

ValerianRey wants to merge 1 commit into
mainfrom
add-theorem-1-rewriting

Conversation

@ValerianRey

Copy link
Copy Markdown
Member
  • Adds rewrite(tensor) and rewrite_with(tensor, rules). They read any tensor built with the crate's functions as an expression tree (Expr), and rewrite it into an equal one by rules, applied bottom-up until none applies. The result is itself a tensor.
  • The first rule is Theorem 1, f ∘ Σ ∘ Ū ∘ ψ = Σ ∘ (f ∘ U)‾ ∘ ψ. It holds for any reduction whose neutral element f keeps, which is checked by evaluating f once. So a zero-preserving function like relu is only applied to the stored values.
  • Two more rules move f through index maps (f ∘ (T ∘ ρ) = (f ∘ T) ∘ ρ) and fuse functions on values. This lets Theorem 1 apply to transposed sparse tensors, and to pairs like x + 1 then x - 1.
  • Adds Function::parts, whose default is None. A composition or a lifted function returns its parts through it, with their types erased down to their domain and codomain. The tree is read from the existing types, so there is no second way of building tensors, and shared or Rc<dyn Tensor> parts are looked into too. Anything else is a leaf.
  • Multisets can bound the size of its multisets (Multisets::at_most). COO, Diagonal, EmptyMapping, InvProjection and InvAffine set that bound in their codomain, and that is how the rule knows a tensor is sparse. A COO with a repeated coordinate or a sum over a dimension is left unchanged.
  • Composition and MultiFunction now hold their parts in an Rc, so they can hand them out. Their public API is unchanged.
let a = sparse_coo_tensor(vec![vec![0, 2, 4], vec![1, 0, 3]], Physical::new(vec![1.5, -2.0, 3.0]), vec![5, 4]);
let fast = rewrite(compose(ValueFunction::new(|x: f32| x.max(0.0)), transpose(a)));
println!("{fast:?}"); // Reindex(Reduce(Map(Tensor))), i.e. Σ ∘ (relu ∘ U)‾ ∘ COO ∘ ρ

Limits: functions that change the value type are leaves. InvAffine only claims at most one element when its basis has full column rank. Off the support, negate gives +0.0 after the rewrite where it gave −0.0 before.

🤖 Generated with Claude Code

- Adds `rewrite(tensor)` and `rewrite_with(tensor, rules)`, which read any tensor built with the crate's functions as an expression tree and rewrite it into an equal one by rules, applied bottom-up until none applies
- The first rule is Theorem 1, `f ∘ Σ ∘ Ū ∘ ψ = Σ ∘ (f ∘ U)‾ ∘ ψ`, for any reduction whose neutral element f keeps, so a zero-preserving function like relu is only applied to the stored values; two more rules move f through index maps and fuse functions on values
- Adds `Function::parts`, by which a composition or a lifted function gives its parts with their types erased down to their domain and codomain, so the tree is read from the existing types and there is no second way of building tensors
- `Multisets` can bound the size of its multisets, and COO, Diagonal, EmptyMapping, InvProjection and InvAffine set that bound in their codomain, which is how the rule knows that a tensor is sparse
- `Composition` and `MultiFunction` hold their parts in an `Rc`

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant