Skip to content

Make pyop3 caches work better with object versioning - #5750

Open
achanbour wants to merge 7 commits into
mainfrom
achanbour/version-pyop3-caches
Open

achanbour wants to merge 7 commits into
mainfrom
achanbour/version-pyop3-caches

Conversation

@achanbour

Copy link
Copy Markdown
Contributor

Following the introduction of VoM mutation mechanism, we need to think more carefully about how we manage caches.

This PR fixes two things:

  1. Removes the possibility of stored caches on multiple objects (see changes in cached_on)
  2. Removes the concept of heavy caches and replaces it with LRU caches instead.

This way, we don't need to worry about 1) having persistent/stale cache copies lying around in memory and 2) the lifetime of caches on heavy objects like mesh topologies, pyop3 AxisTrees etc. and the need to invalidate those.

@achanbour

Copy link
Copy Markdown
Contributor Author

Previously, cached_on stored results on two objects, namely in:

  • MixedFunctionSpaces; on the topology of every mesh in the sequence -> now stores on the MeshSequenceTopology directly (which has a well-defined topology version)
  • pyop3's packing operation; on the topology of every mesh of the FunctionSpace and on the mesh topology of the loop index -> now stores on the iteration topology only and makes the version of the space's topology part of the cache key.

@achanbour

Copy link
Copy Markdown
Contributor Author

WIP: undoing heavy-mesh caches.

@connorjward

Copy link
Copy Markdown
Contributor

@sghelichkhani this might have a performance on G-ADOPT. It would be good to know if that's the case.

@connorjward connorjward left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Largely looks fine but there are some merge artefacts that need removing.

Comment thread firedrake/functionspaceimpl.py Resolved
Comment thread firedrake/interpolation.py Resolved
Comment thread firedrake/mesh.py Resolved
Comment thread pyop3/cache.py Resolved
Comment thread pyop3/cache.py Resolved
@achanbour

Copy link
Copy Markdown
Contributor Author

Removed conflicts, should be good now.

@achanbour
achanbour requested a review from connorjward December 1, 2026 17:01
connorjward
connorjward previously approved these changes Dec 1, 2026

@connorjward connorjward left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks! I want to make sure that this doesn't affect performance before merging though. We can look at:

  • CI runtime
  • Any experiments you have done
  • Feedback from @sghelichkhani ?

@sghelichkhani

Copy link
Copy Markdown
Contributor

@connorjward I tried this on G-ADOPT but the current head (dae70f0) does not import, and the Firedrake CI fails the same way. There are two issues in pyop3/axis_tree/visitors/layout.py, which this PR does not touch:

  1. Line 43 uses @pyop3.cache.with_self_heavy_cache on compute_layouts, which the PR removes from pyop3/cache.py. This raises AttributeError: module 'pyop3.cache' has no attribute 'with_self_heavy_cache' when the first function space is built.
  2. Line 424 uses @memory_cache(heavy=True) on _accumulate_step_sizes. The PR removes the heavy argument, so this will raise a TypeError once the first one is fixed.

Once these are fixed I am happy to test it on G-ADOPT.

@sghelichkhani sghelichkhani left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

See issues in the comment, once fixed let me time gadopt's CI against this.

@achanbour

Copy link
Copy Markdown
Contributor Author

@sghelichkhani I have now fixed these issues. Please feel free to run your tests and let us know about performance.

@sghelichkhani

Copy link
Copy Markdown
Contributor

@achanbour thanks. I ran the G-ADOPT regression suite on this branch. The total time is the same as main, but the iterative fieldsplit Stokes solves change: the velocity block needs about a third more CG iterations and some Schur solves several times more. The cause is that the null space and near null space given by the user no longer reach the fieldsplit blocks. field_ises is now cached on the MeshSequenceTopology, and MeshSequenceGeometry.topology builds a new one for every new geometry. dmhooks.create_field_decomposition rebuilds the space through get_function_space(dm), so PCFieldSplit gets new IS objects, while the solver attached the null spaces to the ones in Z.field_ises. This is also why test_nullspace_mixed and test_mixed_fs_hits_cache fail in CI. Keeping one MeshSequenceTopology for each tuple of component topologies fixes it for me, and the results are then bitwise equal to main.

Two of the new LRU caches also drop entries that G-ADOPT uses again: _compile (100, about 200 needed) and _materialize_composite_dat_cached (10, about 40 needed).

I also have a question on _compile. On main its entries lived on the instruction and were freed with it. Now an entry keeps the buffers and maps of its first call alive until it is evicted, so up to 100 sets of matrices, arrays and meshes can outlive the objects they belong to. Are you not worried that this holds on to a lot of memory in long runs that create many matrices or meshes?

@connorjward

Copy link
Copy Markdown
Contributor

Thanks Sia.

MeshSequenceGeometry.topology builds a new one for every new geometry

Oh yuck. That's definitely not the expected behaviour! We should reuse the topology for different geometries.

Now an entry keeps the buffers and maps of its first call alive until it is evicted, so up to 100 sets of matrices, arrays and meshes can outlive the objects they belong to. Are you not worried that this holds on to a lot of memory in long runs that create many matrices or meshes?

This is a good point. I think there are two possible solutions to this:

  1. Make the cache sizes user-configurable
  2. Get clever with weakrefs - we could for instance make an executor die when some or all of its arguments die

This stuff is hard. The reason for this PR is we realised that the existing 'heavy cache' code didn't really work because Firedrake and pyop3 didn't know enough about each other to cache intelligently.

@achanbour

Copy link
Copy Markdown
Contributor Author

@connorjward looking through CI failures we had:

  1. Solver null space issues, now fixed with Pass topology to MeshSequenceGeometries #5760
  2. The last assertion of the following test failing:

def test_mixed_fs_hits_cache():
m = UnitSquareMesh(1, 1)
V1 = FunctionSpace(m, 'DG', 1)
Q1 = FunctionSpace(m, 'RT', 2)
W1 = V1*Q1
V2 = FunctionSpace(m, 'DG', 1)
Q2 = FunctionSpace(m, 'RT', 2)
W2 = V2*Q2
assert W1 == W2
assert W1.topological == W2.topological
assert W1.axes is W2.axes

In other words, we are no longer sharing axes between mixed spaces defined on "identical" components. This is due to the refactor of the caching, which now stores the entries on MeshSequenceTopology rather than the component mesh topologies:

def _mesh_cached(hashkey=lambda *a: a):
    def wrapper(func):
        return cached_on(
            lambda self: self.mesh().topology,
            get_key=hashkey,
            get_version=lambda topology: topology._topology_version
        )(func)
    return wrapper

Looking at the changes in #5760, every MixedFunctionSpace instantiates a new MeshSequenceTopology so each of W1 and W2 has its own topology and subsequently its own separate cached axes.

Question: do we want to have MeshSequenceGeometry with identical mesh sequences return the same MeshSequenceTopology object?

@sghelichkhani

Copy link
Copy Markdown
Contributor

I think yes. Keeping one MeshSequenceTopology per tuple of component topologies in a WeakValueDictionary fixed both the null spaces and the repeated recomputation for me, with G-ADOPT results bitwise equal to main. It should also let mixed spaces on the same meshes share their cached axes again.

@achanbour

Copy link
Copy Markdown
Contributor Author

I think yes. Keeping one MeshSequenceTopology per tuple of component topologies in a WeakValueDictionary fixed both the null spaces and the repeated recomputation for me, with G-ADOPT results bitwise equal to main. It should also let mixed spaces on the same meshes share their cached axes again.

Thanks @sghelichkhani. I've now implemented this fix in #5760.

@achanbour

Copy link
Copy Markdown
Contributor Author

@connorjward all the issues highlighted above should now be resolved with the fixes in #5760. Only thing left is to figure out how we want to set/optimise the cache sizes...

@connorjward

Copy link
Copy Markdown
Contributor

@connorjward all the issues highlighted above should now be resolved with the fixes in #5760. Only thing left is to figure out how we want to set/optimise the cache sizes...

I want to try and write my own solution for heavy caches. Just replacing with LRU caches may not be optimal.

This branch has not been deployed

No deployments
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