Skip to content

Fix MethodError in ifelse when both branches are constants - #325

Merged
adrhill merged 1 commit into
JuliaDiff:mainfrom
SAY-5:fix-ifelse-constant-branches
Jul 20, 2026
Merged

adrhill merged 1 commit into
JuliaDiff:mainfrom
SAY-5:fix-ifelse-constant-branches

Conversation

@SAY-5

@SAY-5 SAY-5 commented Jul 18, 2026

Copy link
Copy Markdown
Contributor

Fixes #314.

ifelse(tracer_cond, c1, c2) where both branches are plain constants (not tracers) hit output_union with no matching method and threw MethodError: no method matching output_union(::Float64, ::Float64). The existing scalar fallbacks only cover the cases where at least one branch is an AbstractTracer.

Added a fallback for when neither branch is a tracer. In that case the result carries no dependency on the inputs, so returning either constant gives the correct (empty) sparsity pattern. Also added a gradient test for the both-constant case next to the existing single-constant ones.

Signed-off-by: Sai Asish Y <say.apm35@gmail.com>
@codecov-commenter

codecov-commenter commented Jul 18, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.82%. Comparing base (3dc3b2f) to head (fdcc6e4).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #325   +/-   ##
=======================================
  Coverage   93.81%   93.82%           
=======================================
  Files          28       28           
  Lines        1100     1101    +1     
=======================================
+ Hits         1032     1033    +1     
  Misses         68       68           

☔ View full report in Codecov by Harness.
📢 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.

@adrhill

adrhill commented Jul 19, 2026

Copy link
Copy Markdown
Collaborator

Maybe we should add a size assertion on both inputs while we are at it.

@SAY-5

SAY-5 commented Jul 20, 2026

Copy link
Copy Markdown
Contributor Author

The scalar Base.ifelse(::AbstractTracer, x, y) entry already asserts size(x) != size(y) before dispatching to output_union, so mismatched branch sizes are caught there. The gap is the array output_union overloads (the ones under the # TODO: add test marker), which assume matching sizes without checking. Happy to add a matching size(tx) == size(ty) assertion to those two-array methods and drop the TODO by covering them with a test. Want me to go that route, or did you have a different pair of inputs in mind?

@adrhill

adrhill commented Jul 20, 2026

Copy link
Copy Markdown
Collaborator

Thanks! :)

@adrhill
adrhill merged commit bee8149 into JuliaDiff:main Jul 20, 2026
10 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.

Bug: ifelse with constant branches fails with MethodError

3 participants