Skip to content

Comments

GEM: Simplify Indexed tensors#131

Merged
pbrubeck merged 28 commits intomainfrom
pbrubeck/simplify-indexed
Feb 4, 2026
Merged

GEM: Simplify Indexed tensors#131
pbrubeck merged 28 commits intomainfrom
pbrubeck/simplify-indexed

Conversation

@pbrubeck
Copy link

@pbrubeck pbrubeck commented Jan 21, 2025

Introduces simplifications that destroy indexed structures

ListTensor([Indexed(A, i) for i in numpy.ndindex(A.shape)]) -> A

Indexed(ListTensor(array), FixedIndex) -> array[FixedIndex]

As a benchmark, we generate code for assembling

from firedrake import *
from firedrake.tsfc_interface import compile_form
V = FunctionSpace(UnitTetrahedronMesh(), "JM", 1)
v = TestFunction(V)
u = TrialFunction(V)
form = inner(u, v) * dx + inner(div(u), div(v)) * dx(degree=0)
kernels = compile_form(form, "stokes", parameters={"mode": "spectral"})

With this PR it takes 9.41 seconds. Using current main, this takes around 20 minutes, the majority of which is spent in sum_factorise.

image

@pbrubeck pbrubeck force-pushed the pbrubeck/simplify-indexed branch from f445d5b to de501c1 Compare August 8, 2025 09:30
@pbrubeck pbrubeck requested a review from ksagiyam August 8, 2025 10:35
@pbrubeck pbrubeck force-pushed the pbrubeck/simplify-indexed branch from d2e362b to 592061e Compare August 11, 2025 12:33
This was referenced Aug 12, 2025
@pbrubeck pbrubeck force-pushed the pbrubeck/simplify-indexed branch from 1982373 to 4ab1c0d Compare August 12, 2025 13:06
@pbrubeck pbrubeck force-pushed the pbrubeck/simplify-indexed branch from 4ab1c0d to b49eb5c Compare August 12, 2025 15:01
@ksagiyam ksagiyam marked this pull request as draft August 14, 2025 18:39
@ksagiyam
Copy link

I marked this PR as Draft as you still seem to be debugging. Please mark it as Ready for review when you become confident.

@pbrubeck pbrubeck marked this pull request as ready for review August 26, 2025 17:20
@pbrubeck pbrubeck force-pushed the pbrubeck/simplify-indexed branch from 996f2b4 to 5dfd17d Compare August 29, 2025 13:19
@pbrubeck
Copy link
Author

Tests are passing (modulo known doc/linkcheck failures) https://github.com/firedrakeproject/firedrake/actions/runs/17322729298/job/49186947155

@ksagiyam I feel quite happy with this, could you review?

gem/gem.py Outdated
slices = tuple(i if isinstance(i, int) else slice(None) for i in multiindex)
sub = aggregate.array[slices]
sub = Literal(sub, dtype=aggregate.dtype) if isinstance(aggregate, Constant) else ListTensor(sub)
return Indexed(sub, tuple(i for i in multiindex if not isinstance(i, int)))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this is safe. This is a recursion, and, unlike when we use DAGTraverser, the result is not cached.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the issue? Are we potentially creating new objects and not freeing memory for the old ones?

Copy link
Author

@pbrubeck pbrubeck Feb 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved this case to the remove_componenttensors DAG traverser.

@pbrubeck pbrubeck force-pushed the pbrubeck/simplify-indexed branch from d60b997 to 8d0dc41 Compare February 2, 2026 12:28
Copy link

@connorjward connorjward left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that this needs some more comments explaining what is going on. To someone not quite up to speed with the intricacies of GEM this is very hard to understand.

Otherwise I think I'm quite happy with this.

@pbrubeck pbrubeck force-pushed the pbrubeck/simplify-indexed branch from 77d6102 to 04f0cf3 Compare February 4, 2026 13:33
@pbrubeck pbrubeck merged commit 6a7c91d into main Feb 4, 2026
8 checks passed
@pbrubeck pbrubeck deleted the pbrubeck/simplify-indexed branch February 4, 2026 17:35
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.

3 participants