Skip to content

Commit 9e1aa8c

Browse files
committed
test unsafe_getindex
1 parent c1ebf3f commit 9e1aa8c

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "ChainRules"
22
uuid = "082447d4-558c-5d27-93f4-14fc19e9eca2"
3-
version = "1.43.2"
3+
version = "1.44.0"
44

55
[deps]
66
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"

src/rulesets/Base/indexing.jl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,14 @@ end
196196

197197
# This is called by e.g. `iterate(1:0.1:2)`,
198198
# and fixes https://github.com/FluxML/Zygote.jl/issues/1247
199+
# Only needs to accept AbstractRange, but AbstractVector makes testing easier.
199200

200-
function rrule(cfg::RuleConfig{>:HasReverseMode}, ::typeof(Base.unsafe_getindex), x::AbstractRange, i::Integer)
201-
return rrule_via_ad(cfg, getindex, x, i)
201+
function frule((_, ẋ), ::typeof(Base.unsafe_getindex), x::AbstractVector, i::Integer)
202+
return Base.unsafe_getindex(x, i), getindex(ẋ, i)
203+
end
204+
205+
function rrule(cfg::RuleConfig{>:HasReverseMode}, ::typeof(Base.unsafe_getindex), x::AbstractVector, i::Integer)
206+
return rrule_via_ad(cfg, getindex, x, i)
202207
end
203208

204209
#####

test/rulesets/Base/indexing.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,11 @@ end
177177
test_frule(setindex!, rand(3, 4), [1,10,100.0], :, 3)
178178
end
179179

180+
@testset "unsafe_getindex" begin
181+
test_frule(Base.unsafe_getindex, collect(1:0.1:2), 3)
182+
test_rrule(Base.unsafe_getindex, collect(1:0.1:2), 3)
183+
end
184+
180185
@testset "eachslice" begin
181186
# Testing eachrow not collect∘eachrow leads to errors, e.g.
182187
# test_rrule: eachrow on Vector{Float64}: Error During Test at /Users/me/.julia/packages/ChainRulesTestUtils/8dFTY/src/testers.jl:195

0 commit comments

Comments
 (0)