JuliaDiff / JuliaDiff/ChainRules.jl

rrule causes scalar indexing for `repeat`

Open
#645 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug GPU
Dominant language
Julia
Stars
475
Forks
98
PR merge metrics
No merged PRs in 30d

Description

julia> func(x) = sum(repeat(x, inner = (1, 3)))
func (generic function with 1 method)

julia> func(CUDA.rand(2,3))
7.5995846f0

julia> Zygote.gradient(func,CUDA.rand(2,3))
ERROR: Scalar indexing is disallowed.
Invocation of getindex resulted in scalar indexing of a GPU array.
This is typically caused by calling an iterating implementation of a method.
Such implementations *do not* execute on the GPU, but very slowly on the CPU,
and therefore are only permitted from the REPL for prototyping purposes.
If you did intend to index this array, annotate the caller with @allowscalar.
Stacktrace:
  [1] error(s::String)
    @ Base .\error.jl:33
  [2] assertscalar(op::String)
    @ GPUArraysCore C:\Users\Luffy\.julia\packages\GPUArraysCore\rSIl2\src\GPUArraysCore.jl:78
  [3] getindex(::CuArray{Float32, 2, CUDA.Mem.DeviceBuffer}, ::Int64, ::Int64)
    @ GPUArrays C:\Users\Luffy\.julia\packages\GPUArrays\gok9K\src\host\indexing.jl:9
  [4] getindex
    @ C:\Users\Luffy\.julia\packages\GPUArrays\gok9K\src\host\indexing.jl:30 [inlined]
  [5] iterate
    @ .\iterators.jl:245 [inlined]
  [6] (::Zygote.var"#519#525"{Tuple{Int64, Int64}, CuArray{Float32, 2, CUDA.Mem.DeviceBuffer}})(Δ::CuArray{Float32, 2, CUDA.Mem.DeviceBuffer})
    @ Zygote C:\Users\Luffy\.julia\packages\Zygote\IoW2g\src\lib\array.jl:136
  [7] #2693#back
    @ C:\Users\Luffy\.julia\packages\ZygoteRules\AIbCs\src\adjoint.jl:73 [inlined]
  [8] Pullback
    @ .\REPL[15]:1 [inlined]
  [9] (::Zygote.var"#60#61"{typeof(∂(func))})(Δ::Float32)
    @ Zygote C:\Users\Luffy\.julia\packages\Zygote\IoW2g\src\compiler\interface.jl:41
 [10] gradient(f::Function, args::CuArray{Float32, 2, CUDA.Mem.DeviceBuffer})
    @ Zygote C:\Users\Luffy\.julia\packages\Zygote\IoW2g\src\compiler\interface.jl:76
 [11] top-level scope
    @ REPL[17]:1
 [12] top-level scope
    @ C:\Users\Luffy\.julia\packages\CUDA\tTK8Y\src\initialization.jl:52

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the Julia example with CUDA.rand, repeat, and Zygote.gradient, then follow the stack trace into Zygote's src/lib/array.jl and the related ChainRules behavior. Done means the gradient path no longer triggers scalar indexing for this GPU-array repeat case, with a regression test covering the example.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.