Skip to content

Improve keep_intermediate to enable keeping IR after each pass #1791

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

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

Conversation

mehrdad2m
Copy link
Contributor

Context:
keep_intermediate is a boolean flag that if enabled, keeps the intermediate IR files after each pipeline. However, it is quite common that a developer needs the IR after a specific pass, which is not accessible in the current format via keep_intermediate.

Description of the Change:
Added a third level to the keep_intermediate that enables accessing the IR in a more fine grained format i.e. after each pass.
Note that this is not a breaking change, meaning that the current behaviour of keep_intermediate is kept intact.
With this PR, the user can provide three different levels of printing:

  • False or 0 or "none" (default): No intermediate files are kept.
  • True or 1 or "basic": Standard intermediate files are kept (IR after each pipeline).
  • 2 or "debug": Standard intermediate files are kept, and IR is saved after each pass.

Benefits:
easier development and debugging

Possible Drawbacks:

Related GitHub Issues:

@mehrdad2m mehrdad2m requested a review from a team June 6, 2025 13:47
Copy link
Member

@paul0403 paul0403 left a comment

Choose a reason for hiding this comment

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

🤑 If I had a dollar every time I wanted this I would've been a millionaire. Thank you!!

Copy link

codecov bot commented Jun 6, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.61%. Comparing base (bf9b922) to head (2b5a5af).
Report is 3 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1791   +/-   ##
=======================================
  Coverage   96.60%   96.61%           
=======================================
  Files          82       82           
  Lines        9221     9241   +20     
  Branches      872      877    +5     
=======================================
+ Hits         8908     8928   +20     
  Misses        254      254           
  Partials       59       59           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@@ -95,6 +95,14 @@
performance by eliminating indirect conversion.
[(#1738)](https://github.com/PennyLaneAI/catalyst/pull/1738)

* The `keep_intermediate` argument in the `qjit` decorator now accepts a new value that allows for
saving intermediate files after each pass. The updated possible options for this argument are:
* `False` or `0` or `"none"`: No intermediate files are kept.
Copy link
Member

Choose a reason for hiding this comment

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

Wait, is it the string "none" or the python None?

Comment on lines +354 to +356
if options.keep_intermediate >= KeepIntermediateLevel.PASS:
extra_args += ["--save-ir-after-each=pass"]

Copy link
Member

Choose a reason for hiding this comment

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

I think it makes more sense to put the two if options.keep_intermediate blocks together

:attr:`~.QJIT.mlir`, :attr:`~.QJIT.mlir_opt`, :attr:`~.QJIT.jaxpr`,
and :attr:`~.QJIT.qir`, representing different stages in the optimization process.
keep_intermediate (Union[str, int, bool]): Level controlling intermediate file generation.
- ``False`` or ``0`` or ``"none"`` (default): No intermediate files are kept.
Copy link
Member

Choose a reason for hiding this comment

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

Same here, is this the string or the python None?

Comment on lines +62 to +66
case _:
raise ValueError(
f"Invalid value for keep_intermediate: {level}. "
"Valid values are True, False, 0, 1, 2, 'none', 'pipeline', 'pass'."
)
Copy link
Member

Choose a reason for hiding this comment

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

Hmm, does the python None come to this error case? Since the above is matching for the string

Copy link
Member

Choose a reason for hiding this comment

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

Also are booleans parsed here? 🤔

Comment on lines +132 to +133
assert "--save-ir-after-each=pass" not in flags
assert flags.count("--save-ir-after-each=pass") <= 1
Copy link
Member

Choose a reason for hiding this comment

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

Why check this twice? 😅

Comment on lines +140 to +141
assert "--save-ir-after-each=pass" in flags
assert flags.count("--save-ir-after-each=pass") == 1
Copy link
Member

Choose a reason for hiding this comment

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

Also here

Copy link
Member

@paul0403 paul0403 left a comment

Choose a reason for hiding this comment

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

Pre-approving as long as these concerns are addressed 🥇

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