Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cached evaluation of tensor networks #224

Open
wants to merge 13 commits into
base: main
Choose a base branch
from

Conversation

DNA386
Copy link

@DNA386 DNA386 commented Mar 7, 2025

Add caching option by default for models that evaluate tensor networks, as per #185
Also bonus include the Sim9 and Sim9Cx ansatze from the inTask repo.

A non-cached option is also included in case the circuits are small enough that caching introduces unnecessary overhead, but must be actively selected by the user. By default this will reproduce the previous behaviour.
The user can select this as a kwarg when initialising the model. Note: the pathfinder must also be provided as a kwarg if resuming from a checkpoint - the checkpoint currently will contain the saved paths, but not the information required to re-build the same fallback path finding algorithm.

By default, the paths will only be cached in memory. The user can chose to either save them to the checkpoint, or to a separate file by supplying a configured CachedTnPathOptimizer instance.

@dimkart dimkart changed the title Cached tn eval Add cached evaluation of tensor networks Mar 11, 2025
Copy link
Collaborator

@neiljdo neiljdo left a comment

Choose a reason for hiding this comment

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

Hi @DNA386 thank you for your work! 🙌🏼 I just came back so I can only do a surface-level review at the moment - kindly take a look at my initial notes. I'll have a closer look soon.

One thing we would like to have are tests specific to the classes/functions implemented in the `lambeq/training/saved_tn_optimizer.py'.

@dimkart
Copy link
Contributor

dimkart commented Mar 13, 2025

We can make this configurable (i.e. enable/disable storing the paths in the checkpoints somehow).

This initial proposal currently saves cached paths to the model checkpoint, but this is not desirable if the datasets are very large as this will result in a lot of duplicated information (assuming checkpoints are saved per epoch). The paths can typically also be reused when training a different iteration of a model on the same dataset (with the same anstaz), which this system doesn't capture. A more natural way to implement is to have an independent checkpointing system for the paths, that overwrite the previous checkpoint each time a new path is added, however this means the user needs to specify and track the checkpoint filepath separately from the model. In this case, it would not be possible to have caching by default; the user would need to explicitly opt-in by providing a configured TnOptimizer.

@DNA386 DNA386 force-pushed the cached-tn-eval branch 2 times, most recently from d6e6035 to e45d4b4 Compare March 31, 2025 16:08
@DNA386 DNA386 marked this pull request as ready for review March 31, 2025 16:09
@dimkart
Copy link
Contributor

dimkart commented Apr 3, 2025

@neiljdo Let's run our benchmarks on this PR and compare speeds with current branch.

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.

DisCoCirc: Request for option to save and then reload previously computed tensornetwork contraction paths
3 participants