Skip to content

Add rewrite with Theorem 1 - #20

Open
ppraneth wants to merge 2 commits into
mainfrom
theorem
Open

ppraneth wants to merge 2 commits into
mainfrom
theorem

Conversation

@ppraneth

@ppraneth ppraneth commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator
  • Adds rewrite(tensor), which turns a tensor into an equivalent one that is cheaper to evaluate, or gives it back unchanged as Err when no identity applies.
  • For now it uses Theorem 1, f ∘ Σ ∘ Ū ∘ ψ = Σ ∘ (f ∘ U)‾ ∘ ψ, so a zero-preserving function like square or relu is only applied to the stored values. It checks that f(0) = 0 and that the tensor is sparse.
  • Adds a Sparsity trait so that index functions (COO, Diagonal, EmptyMapping, InvProjection, InvAffine) can tell whether they send each index to at most one index.

@ppraneth
ppraneth requested a review from ValerianRey November 9, 2026 04:04
@ValerianRey

Copy link
Copy Markdown
Member

I'll read in details later but I just have a quick comment so far. I don't think we should return an error when the rewrite is impossible. The idea is that rewrite will be identity when nothing can be improved, so that users (or even us) can call rewrite without a priori knowledge of the rewritability of the tensor, and let cten check that for them.

@ppraneth

ppraneth commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator Author

Cool, I checked what Rust allows:

  1. When the theorem's checks fail (f(0) != 0 or the tensor isn't sparse), rewrite can give the tensor back instead of an error. I tried it: rewrite returns a small type that is either the original or the rewritten tensor, and it evaluates, prints and composes like any tensor. One catch: you can't rewrite that result again (e.g. scale it and rewrite), because its type isn't a plain sum tensor anymore. With the current Result you can.
  2. Calling rewrite on any tensor at all (a Physical, a row sum...) is harder. Right now rewrite only exists for tensors shaped like f ∘ Σ ∘ Ū ∘ ψ, because the type of the result depends on the shape. Adding "identity for every other shape" next to it doesn't compile (conflicting implementations), and the feature that would allow it (specialization) isn't in stable Rust. The way around it I see is to have rewrite return an Rc<dyn Tensor> and look at the composition at runtime, but that's a bigger change and I'd have to prototype it first.

Should I proceed as a whole or just do 1?

@ValerianRey

ValerianRey commented Nov 11, 2026 •

Copy link
Copy Markdown
Member
  1. isn't really satisfying IMO because of the new type and because of not being able to call rewrite again on the result.

The way around it I see is to have rewrite return an Rc and look at the composition at runtime

Is there any issue with that? Seems like the proper way of implementing this

Maybe we can switch to specialization once it becomes more stable.

@ppraneth

Copy link
Copy Markdown
Collaborator Author
  1. isn't really satisfying IMO because of the new type and because of not being able to call rewrite again on the result.

The way around it I see is to have rewrite return an Rc and look at the composition at runtime

Is there any issue with that? Seems like the proper way of implementing this

Maybe we can switch to specialization once it becomes more stable.

Shall we wait for it to be stable ?

@ValerianRey

Copy link
Copy Markdown
Member

Could take a long time. What's the issue with the runtime-based compilation?

@ppraneth

Copy link
Copy Markdown
Collaborator Author

There's no blocker, it's mostly more work. The main points:

  • rewrite would return an Rc<dyn Tensor>, so the result loses its concrete type. It can still be evaluated and rewritten again, and printing it just needs a small Display impl (I checked that part).
  • Rust can't look inside a generic type at runtime on its own, so each function would need to describe itself through the Function trait (e.g. "I'm a composition", "I'm a sum tensor with this ψ"), with a default saying there's nothing to rewrite. That's the bigger part of the change.
  • A small cost from dynamic dispatch when evaluating, which shouldn't matter much here.

I can try and do it if it's okay with u?

@ValerianRey

ValerianRey commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

There's no blocker, it's mostly more work. The main points:

* rewrite would return an `Rc<dyn Tensor>`, so the result loses its concrete type. It can still be evaluated and rewritten again, and printing it just needs a small Display impl (I checked that part).

* Rust can't look inside a generic type at runtime on its own, so each function would need to describe itself through the Function trait (e.g. "I'm a composition", "I'm a sum tensor with this ψ"), with a default saying there's nothing to rewrite. That's the bigger part of the change.

* A small cost from dynamic dispatch when evaluating, which shouldn't matter much here.

I can try and do it if it's okay with u?

Sorry for the late reply.
I think that's a good direction. Could you try that?

We're basically looking into intermediate representation simplification. Maybe a graph structure would help us, but for now I don't think it's needed.
What I mean is, let's say that mathematically we have f ⚬ g, currently in the code we represent that as Composition(f, g), but we could translate that into a graph with nodes [f, g] and edges [(1, 0)], i.e. node 1 (g) connected to node 0 (f), which is the directed acyclic graph: (f)<-(g)

Maybe such a representation could be helpful internally, but honestly it's quite independent from this PR and from the problem we currently have. It's just an idea.

@ppraneth

Copy link
Copy Markdown
Collaborator Author

Done, pushed it. rewrite now takes any tensor and returns an Rc<dyn Tensor>, unchanged where nothing applies. Each function describes itself through a few new methods on Function (with defaults that find nothing), and rewrite looks at the composition at runtime. The result can be rewritten again, and it also rewrites inside a tensor, e.g. a square under a transpose.

Since f̄ ∘ Ū = (f ∘ U)‾, it rewrites f ∘ Ω ∘ Ū ∘ ψ as Ω ∘ f̄ ∘ Ū ∘ ψ, and that works for any reduction as long as f keeps its neutral element (Lemma 6), Theorem 1 being the sum case. In the tests, squaring a sparse 2x4 matrix with 3 stored values calls the function 3 times after rewrite instead of 8.

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.

2 participants