JuliaDiff / JuliaDiff/DifferentiationInterface.jl

GPU scalar indexing when `transpose(::CuArray)`

Open
#970 12 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

gpu
Dominant language
Julia
Stars
313
Forks
35
PR merge metrics
No merged PRs in 30d

Description

It's weird because x = CUDA.zeros(1,4) works fine but x = transpose(CUDA.zeros(4)) gives the scalar indexing problem.

MWE:

using KernelAbstractions, CUDA
import DifferentiationInterface as DI

@kernel function foo!(y, x)
    i = @index(Global)
    a = 2*i - 1
    b = 2*i
    offset = (i-1)*4
    y[a] = (offset+1)*x[a] + (offset+2)*x[b]
    y[b] = (offset+3)*x[a] + (offset+4)*x[b]
end

kernel! = foo!(CUDA.CUDABackend())
f!(y,x) = kernel!(y, x, ndrange=2)

# This works fine:
x = CUDA.zeros(1,4)
y = CUDA.rand(1,4)
prep = DI.prepare_jacobian(f!, y, DI.AutoForwardFromPrimitive(DI.AutoForwardDiff()), x);
DI.value_and_jacobian!(fun!, y, jac, prep, DI.AutoForwardFromPrimitive(DI.AutoForwardDiff()), x)
#= Output:
4×4 CuArray{Float32, 2, CUDA.DeviceMemory}:
 1.0  2.0  0.0  0.0
 3.0  4.0  0.0  0.0
 0.0  0.0  5.0  6.0
 0.0  0.0  7.0  8.0
=#

# This causes scalar indexing:
x = transpose(CUDA.zeros(4))
y = transpose(CUDA.rand(4))
prep = DI.prepare_jacobian(f!, y, DI.AutoForwardFromPrimitive(DI.AutoForwardDiff()), x);
DI.value_and_jacobian!(fun!, y, jac, prep, DI.AutoForwardFromPrimitive(DI.AutoForwardDiff()), x)

Contributor guide

Open the contributing guide

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

Start by running the supplied MWE with foo!, f!, DI.prepare_jacobian, and DI.value_and_jacobian!, comparing ordinary CuArrays with transposed ones. Trace where the transposed input reaches the differentiation and kernel calls. Done means the transposed case completes without scalar-indexing warnings and produces the expected Jacobian.

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.