Import ChainRules for broadcast(f, As...) for Array{<:IEEEFloat}
          #740
        
          
      
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Mooncake's default
broadcast(f, As...)rules are generally fast, but can be slow whenfis computationally lightweight. This PR imports ChainRules rules for broadcast operations onArray{<:IEEEFloat}to improve performance in these cases.Implementation
Since
broadcast(f, args...) = copy(broadcasted(f, args...)), this PR adds two@from_rruledeclarations tosrc/rrules/performance_patches.jl:@from_rrule DefaultCtx Tuple{typeof(copy),Base.Broadcast.Broadcasted}- imports ChainRules rule for copying broadcasted objects@from_rrule DefaultCtx Tuple{typeof(Base.Broadcast.broadcasted),Function,Array{P}} where {P<:IEEEFloat}- imports ChainRules rules for creating broadcasted objects over IEEE floating point arraysPerformance Impact
This change ensures that broadcast operations like
sin.(cos.(exp.(x)))now use ChainRules' optimized implementations instead of Mooncake's default derived rules when operating on IEEE floating point arrays. All component broadcast operations (broadcasted(exp, x),broadcasted(cos, y),broadcasted(sin, z)) are now recognized as primitives and will use the more efficient ChainRules implementations.Testing
Added comprehensive test cases covering:
copy(::Base.Broadcast.Broadcasted)for all IEEE float precisions (Float16, Float32, Float64)Base.Broadcast.broadcasted(f, array)for all IEEE float precisionsVerified that benchmark functions like
_broadcast_sin_cos_exp(x) = sum(sin.(cos.(exp.(x))))now use the imported ChainRules rules for all broadcast operations.Fixes #737.
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.