Skip to content
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

keyword args in broadcasted funcs not supported #124

Open
denizyuret opened this issue Feb 12, 2021 · 0 comments
Open

keyword args in broadcasted funcs not supported #124

denizyuret opened this issue Feb 12, 2021 · 0 comments

Comments

@denizyuret
Copy link
Owner

This does not work:

function relu(x::T; max_value=Inf, negative_slope=0, threshold=0) where T                                                                                             
    (x >= max_value ? oftype(x, max_value) :                                                                                                                          
     x >= threshold ? x :                                                                                                                                             
     negative_slope == 0 ? zero(T) :                                                                                                                                  
     negative_slope * (x - oftype(x, threshold)))                                                                                                                     
end                                                                                                                                                                   
                                                                                                                                                                      
function reluback(x::T,dy::T; max_value=Inf, negative_slope=0, threshold=0) where T                                                                                   
    dy * (x >= max_value ? zero(T) :                                                                                                                                  
          x >= threshold ? one(T) :                                                                                                                                   
          oftype(x, negative_slope))                                                                                                                                  
end                                                                                                                                                                   
                                                                                                                                                                      
@primitive relu(x; o...),dy,y  reluback.(x,dy; o...)                                                                                                                  
@primitive reluback(x,dy; o...),ddx,dx  zero(dy)                                                                                                                      

Gives the error:
MethodError: no method matching broadcasted(::typeof(relu), ::Array{Float64, 4}; threshold=1)

Because it turns out these are lowered differently:

julia> Meta.@lower relu.(a; threshold=1)                                                                                                                              
:($(Expr(:thunk, CodeInfo(                                                                                                                                            
    @ none within `top-level scope'                                                                                                                                   
1 ─ %1 = Base.broadcasted_kwsyntax                                                                                                                                    
│   %2 = Core.tuple(:threshold)                                                                                                                                       
│   %3 = Core.apply_type(Core.NamedTuple, %2)                                                                                                                         
│   %4 = Core.tuple(1)                                                                                                                                                
│   %5 = (%3)(%4)                                                                                                                                                     
│   %6 = Core.kwfunc(%1)                                                                                                                                              
│   %7 = (%6)(%5, %1, relu, a)                                                                                                                                        
│   %8 = Base.materialize(%7)                                                                                                                                         
└──      return %8                                                                                                                                                    
))))                                                                                                                                                                  
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

No branches or pull requests

1 participant