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

Perform constant propagation when loading models #241

Merged
merged 5 commits into from
Jun 18, 2024

Conversation

robertknight
Copy link
Owner

@robertknight robertknight commented Jun 17, 2024

Add initial infrastructure to perform graph optimizations as part of loading a model, and implement constant propagation (pre-evaluating the parts of the graph that don't depend on dynamic input values) as the first graph-level optimization. This is an optimization that was already implemented downstream in rten-cli and rten-generate after the model was loaded. Those downstream implementations are also removed in this change.

Optimization is performed by default when a model is loaded, but can be disabled using ModelOptions. This mirrors ONNX Runtime's approach of enabling optimizations unless the consumer opts out.

TODO:

  • Tests
  • Consider making the graph immutable once optimized. This avoids hazards where eg. cached execution plans could become invalidated by model changes (Edit: Will revisit this later)

@robertknight robertknight force-pushed the const-prop-optimize branch 2 times, most recently from cf31d1a to 4a7cfe5 Compare June 18, 2024 06:12
This causes parts of the graph that depend only on constant inputs (ie.
weights, biases) to be replaced when the model is loaded.
This optimization is now performed as part of graph optimizations inside
`Model::load`, so does not need to be done by the CLI.
Since constant propagation is now performed as a graph optimization when the
model is loaded, it only needs to be re-done if additional constants are added.

This change assumes that graph optimizations were enabled when the model was
loaded. If they were disabled, and no other constants were added, this could
lead to expensive re-evaluation of unchanging parts of the graph on each run.
@robertknight robertknight force-pushed the const-prop-optimize branch from 4a7cfe5 to 75570a9 Compare June 18, 2024 08:02
@robertknight robertknight marked this pull request as ready for review June 18, 2024 08:18
@robertknight robertknight merged commit 7750899 into main Jun 18, 2024
2 checks passed
@robertknight robertknight deleted the const-prop-optimize branch June 18, 2024 08:18
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.

1 participant