Skip to content

Rules for copysign are missing? #498

@RainerHeintzmann

Description

@RainerHeintzmann

copysign is used in ChainRules.jl in various places, which may have to do with adherence to the IEEE float standard.
Yet, copysign throws a, probably intended error:

julia> Zygote._pullback(copysign, 3.33, 4.4)[2](1)
ERROR: Non-differentiable function Core.Intrinsics.copysign_float
Stacktrace:
 [1] error(s::String)
   @ Base .\error.jl:33
 [2] macro expansion
   @ ~\.julia\packages\Zygote\TaBlo\src\compiler\interface2.jl:0 [inlined]
 [3] (::typeof((λ)))(Δ::Int64)
   @ Zygote ~\.julia\packages\Zygote\TaBlo\src\compiler\interface2.jl:43
 [4] Pullback
   @ .\floatfuncs.jl:5 [inlined]
 [5] (::typeof((copysign)))(Δ::Int64)
   @ Zygote ~\.julia\packages\Zygote\TaBlo\src\compiler\interface2.jl:0
 [6] top-level scope
   @ REPL[56]:1

, but the question is whether this is really required for the pullback rule. E.g. a function like abs works as expected but is also, strictly speaking, not differentiable at the origin. Maybe this could work:

@scalar_rule copysign(y, x) (sign(x)*y, 0)

or is there a fundamental reason that copysign should not have an allowed gradient, but abs should?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions