Skip to content

Conservative array casting in conditional#2892

Open
dime10 wants to merge 4 commits into
mainfrom
dime-patch-2
Open

Conservative array casting in conditional#2892
dime10 wants to merge 4 commits into
mainfrom
dime-patch-2

Conversation

@dime10
Copy link
Copy Markdown
Contributor

@dime10 dime10 commented May 28, 2026

In the context of JAX tracing, casting values to arrays automatically converts them to tracers, which means this value, and everything it interacts with, is now considered a dynamic program value. Being more conservative in this casting process, thus ensuring that static values remain static for longer, can be beneficial, e.g. for trace-time optimizations. This is especially true given that this is generally a one-way process, i.e. it is much easier to make a static value dynamic, than a dynamic value static.

@dime10 dime10 requested a review from a team May 28, 2026 21:04
@dime10 dime10 added the frontend Pull requests that update the frontend label May 28, 2026
@dime10
Copy link
Copy Markdown
Contributor Author

dime10 commented May 29, 2026

Ah, this change has more impact than I thought. Whereas before the following:

        @qjit(autograph=True)
        def workflow(x):
            n = "fail"

            if n:
                y = x**2
            else:
                y = 0

would raise an error, because we were trying to cast a string to an array, it now succeeds because bool("fail") as a constant input to the catalyst if statement is totally fine.

Not sure if there are other opinions on this, but I'm leaning towards this change being fine?

@paul0403
Copy link
Copy Markdown
Member

Ah, this change has more impact than I thought. Whereas before the following:

        @qjit(autograph=True)
        def workflow(x):
            n = "fail"

            if n:
                y = x**2
            else:
                y = 0

would raise an error, because we were trying to cast a string to an array, it now succeeds because bool("fail") as a constant input to the catalyst if statement is totally fine.

Not sure if there are other opinions on this, but I'm leaning towards this change being fine?

I think it's fine since this is just how regular python would work too? a.k. strings being used as some sort of condition

@dime10
Copy link
Copy Markdown
Contributor Author

dime10 commented May 29, 2026

would raise an error, because we were trying to cast a string to an array, it now succeeds because bool("fail") as a constant input to the catalyst if statement is totally fine.
Not sure if there are other opinions on this, but I'm leaning towards this change being fine?

I think it's fine since this is just how regular python would work too? a.k. strings being used as some sort of condition

Turns out program capture already behaves this way too, so this is actually unifying the behaviour 👍

@codecov
Copy link
Copy Markdown

codecov Bot commented May 29, 2026

Codecov Report

❌ Patch coverage is 75.00000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 97.00%. Comparing base (bea94d8) to head (74bb900).
⚠️ Report is 5 commits behind head on main.

Files with missing lines Patch % Lines
frontend/catalyst/api_extensions/control_flow.py 75.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2892      +/-   ##
==========================================
- Coverage   97.01%   97.00%   -0.02%     
==========================================
  Files         167      167              
  Lines       18830    18872      +42     
  Branches     1770     1773       +3     
==========================================
+ Hits        18268    18306      +38     
- Misses        417      420       +3     
- Partials      145      146       +1     

☔ 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.

Copy link
Copy Markdown
Member

@rniczh rniczh left a comment

Choose a reason for hiding this comment

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

👍

Comment thread frontend/catalyst/api_extensions/control_flow.py
Comment thread frontend/catalyst/api_extensions/control_flow.py Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

frontend Pull requests that update the frontend

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants