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

Zygote ignores ChainRulesCore.rrule for Base.getindex on AbstractArray types #811

Open
oschulz opened this issue Oct 21, 2020 · 0 comments
Labels
ChainRules adjoint -> rrule, and further integration

Comments

@oschulz
Copy link

oschulz commented Oct 21, 2020

With

using ChainRulesCore, Zygote

struct Foo <: AbstractArray{Int,0} end

running

myfunc(x::Foo) = throw(ErrorException("This should not be called"))
ChainRulesCore.rrule(::typeof(myfunc), x::Foo) = 99, _ -> (NO_FIELDS, 42)
Zygote.gradient(myfunc, Foo())

yields (42,), as expected. myfunc is bypassed and never called. However, the same with Base.getindex

Base.getindex(x::Foo) = throw(ErrorException("This should not be called"))
ChainRulesCore.rrule(::typeof(Base.getindex), x::Foo) = 99, _ -> (NO_FIELDS, 42)
Zygote.gradient(Base.getindex, Foo())

throws

ERROR: This should not be called

so Zygote seems to ignore that rrule and call Base.getindex.

This only happens with

struct Foo <: AbstractArray{Int,0} end

When using

struct Foo end

Zygote does not call Base.getindex.

Also, using Foo <: AbstractArray{Int,0}, but ZygoteRules.@adjoint instead of ChainRulesCore.rrule

using Zygote, ZygoteRules

struct Foo <: AbstractArray{Int,0} end

Base.getindex(x::Foo) = throw(ErrorException("This should not be called"))
ZygoteRules.@adjoint Base.getindex(x::Foo) = 99, _ -> (42,)
Zygote.gradient(Base.getindex, Foo())

works as expected.

@oschulz oschulz changed the title ChainRulesCore.rrule for Base.getindex seems to be ignored with Zygote Zygote ignores ChainRulesCore.rrule for Base.getindex on AbstractArray types Oct 21, 2020
@mcabbott mcabbott added the ChainRules adjoint -> rrule, and further integration label Jul 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ChainRules adjoint -> rrule, and further integration
Projects
None yet
Development

No branches or pull requests

2 participants