Skip to content

Fix rrule for broadcasted over empty Tuple{}#834

Merged
ChrisRackauckas merged 4 commits intoJuliaDiff:mainfrom
ChrisRackauckas-Claude:fix-broadcast-empty-tuple
Feb 11, 2026
Merged

Fix rrule for broadcasted over empty Tuple{}#834
ChrisRackauckas merged 4 commits intoJuliaDiff:mainfrom
ChrisRackauckas-Claude:fix-broadcast-empty-tuple

Conversation

@ChrisRackauckas-Claude
Copy link
Contributor

Summary

  • Fixes rrule for Broadcast.broadcasted crashing when broadcasting over an empty Tuple{} (e.g. isone.(()))
  • When args contains an empty tuple, Broadcast.combine_eltypes(f, args) returns Union{}. Since Union{} <: Number is true, the code entered may_bc_derivatives which tried to construct Tuple{Union{}, ...} and errored with "Tuple field type cannot be Union{}"
  • Fix treats T === Union{} as a trivial non-differentiable case alongside T === Bool, since empty containers produce empty results with zero gradients
  • Adds regression tests with isone and sin broadcasted over ()

Fixes #830

Test plan

  • Run broadcast test suite (Pkg.test(; test_args=["broadcast"])) - all 2679 tests pass
  • Verified fix with both MWE functions (isone, sin) over empty tuple

🤖 Generated with Claude Code

When broadcasting over an empty tuple, `Broadcast.combine_eltypes` returns
`Union{}`. Since `Union{} <: Number` is true, the code entered
`may_bc_derivatives` which tried to construct `Tuple{Union{}, ...}` and
errored. Fix by treating `Union{}` as a trivial non-differentiable case
alongside `Bool`.

Fixes JuliaDiff#830

Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copy link
Member

@devmotion devmotion left a comment

Choose a reason for hiding this comment

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

Is it possible to also come up with an example with more arguments?

ChrisRackauckas and others added 3 commits February 11, 2026 11:52
Co-authored-by: David Müller-Widmann <devmotion@users.noreply.github.com>
Co-authored-by: David Müller-Widmann <devmotion@users.noreply.github.com>
Test atan.((), ()) to cover the multi-arg path through the trivial
broadcast rrule when eltype is Union{}.

Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@ChrisRackauckas
Copy link
Member

How about that?

@ChrisRackauckas ChrisRackauckas merged commit 0513e89 into JuliaDiff:main Feb 11, 2026
5 of 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.

rrule for Broadcast.broadcasted fails when broadcasted over Tuple{}

3 participants

Comments