Skip to content

Zygote+Views+CuArrays=Bad News Bears (silently gives incorrect results) #600

@clintonTE

Description

@clintonTE

Found this in the context of trying to adapt a Flux model for gpu usage.

using Revise, Zygote, BenchmarkTools, CuArrays, CUDAnative, Flux
function badnewsbears(N)

  A = reshape(collect(1:N^2), (N,N))
  Acu = A |> gpu

  x = collect(1:N)
  xcu = x|>gpu

  f(A,x)=sum(view(A, :, (i->(i-1)%N+1).(1:N^2)) .* x)

  @info "cpu value: $(f(A,x)); gpu value: $(f(Acu,xcu))"

  gs = gradient((x,A)->f(A,x), x, A)
  @info "check cpu gradient: $(gs)"

  gscu = gradient((xcu,Acu)->f(Acu, xcu), xcu, Acu)
  @info "check gpu gradient: $(gscu)"
end

badnewsbears(3)

Output:

[ Info: cpu value: 288; gpu value: 288
[ Info: check cpu gradient: ([36, 45, 54], [3 3 3; 6 6 6; 9 9 9])
[ Info: check gpu gradient: ([36, 45, 54], [1 1 1; 2 2 2; 3 3 3])

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