Skip to content

Add cached evaluation of tensor networks#224

Merged
neiljdo merged 18 commits intoQuantinuum:mainfrom
DNA386:cached-tn-eval
Apr 11, 2025
Merged

Add cached evaluation of tensor networks#224
neiljdo merged 18 commits intoQuantinuum:mainfrom
DNA386:cached-tn-eval

Conversation

@DNA386
Copy link
Copy Markdown
Contributor

@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
Copy Markdown
Contributor

@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
Copy Markdown
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
Copy Markdown
Contributor

dimkart commented Apr 3, 2025

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

Copy link
Copy Markdown
Contributor

@dimkart dimkart left a comment

Choose a reason for hiding this comment

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

This is great work, the only change at this point (@neiljdo can do this) would be to make the old behaviour the default, so users can opt-in for the caching if they want.

We ultimately stuck with the caching path optimizer as the default.

neiljdo added 3 commits April 11, 2025 14:45
* main:
  Fix no-, multiple-root prediction for parent predictions (Quantinuum#239)
  Add Oncilla to CLI (Quantinuum#238)
  Make SplitTensorAnsatz to deal with boxes with domains (Quantinuum#228)
  Fix bug with untokenised input sentences for `OncillaParser` (Quantinuum#235)
  Make `DisCoCircReader` compatible with `OncillaParser` (Quantinuum#234)
@neiljdo neiljdo merged commit ea6ed26 into Quantinuum:main Apr 11, 2025
9 checks passed
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