Skip to content

Chaining gpu and cpu models #69

Closed
Closed
@lungd

Description

@lungd

Is the following a bug or not supposed to work?

using Flux
using CUDA
m = Chain(Dense(1,1)|>gpu, cpu, Dense(1,1))
p, re = Flux.destructure(m)
p
# 4-element CUDA.CuArray{Float32, 1, CUDA.Mem.DeviceBuffer}:    ##  <-- params of 2nd Dense not on GPU
#  -0.3475855
#   0.0
#  1.0547627
#  0.0

re(p)([1]|>gpu)
ERROR: ArgumentError: cannot take the CPU address of a CUDA.CuArray{Float32, 2, CUDA.Mem.DeviceBuffer}
Stacktrace:
  [1] unsafe_convert(#unused#::Type{Ptr{Float32}}, x::CUDA.CuArray{Float32, 2, CUDA.Mem.DeviceBuffer})
    @ CUDA ~/.julia/packages/CUDA/Uurn4/src/array.jl:315
  [2] gemv!(trans::Char, alpha::Float32, A::CUDA.CuArray{Float32, 2, CUDA.Mem.DeviceBuffer}, X::Vector{Float32}, beta::Float32, Y::Vector{Float32})
    @ LinearAlgebra.BLAS ~/julia-1.7.2/share/julia/stdlib/v1.7/LinearAlgebra/src/blas.jl:667
  [3] gemv!(y::Vector{Float32}, tA::Char, A::CUDA.CuArray{Float32, 2, CUDA.Mem.DeviceBuffer}, x::Vector{Float32}, α::Bool, β::Bool)
    @ LinearAlgebra ~/julia-1.7.2/share/julia/stdlib/v1.7/LinearAlgebra/src/matmul.jl:544
  [4] mul!
    @ ~/julia-1.7.2/share/julia/stdlib/v1.7/LinearAlgebra/src/matmul.jl:66 [inlined]
  [5] mul!
    @ ~/julia-1.7.2/share/julia/stdlib/v1.7/LinearAlgebra/src/matmul.jl:275 [inlined]
  [6] *
    @ ~/julia-1.7.2/share/julia/stdlib/v1.7/LinearAlgebra/src/matmul.jl:47 [inlined]
  [7] (::Dense{typeof(identity), CUDA.CuArray{Float32, 2, CUDA.Mem.DeviceBuffer}, CUDA.CuArray{Float32, 1, CUDA.Mem.DeviceBuffer}})(x::Vector{Float32})
    @ Flux ~/.julia/packages/Flux/18YZE/src/layers/basic.jl:160
  [8] macro expansion
    @ ~/.julia/packages/Flux/18YZE/src/layers/basic.jl:53 [inlined]
  [9] applychain(layers::Tuple{Dense{typeof(identity), CUDA.CuArray{Float32, 2, CUDA.Mem.DeviceBuffer}, CUDA.CuArray{Float32, 1, CUDA.Mem.DeviceBuffer}}, typeof(cpu), Dense{typeof(identity), CUDA.CuArray{Float32, 2, CUDA.Mem.DeviceBuffer}, CUDA.CuArray{Float32, 1, CUDA.Mem.DeviceBuffer}}}, x::CUDA.CuArray{Int64, 1, CUDA.Mem.DeviceBuffer})
    @ Flux ~/.julia/packages/Flux/18YZE/src/layers/basic.jl:53
 [10] (::Chain{Tuple{Dense{typeof(identity), CUDA.CuArray{Float32, 2, CUDA.Mem.DeviceBuffer}, CUDA.CuArray{Float32, 1, CUDA.Mem.DeviceBuffer}}, typeof(cpu), Dense{typeof(identity), CUDA.CuArray{Float32, 2, CUDA.Mem.DeviceBuffer}, CUDA.CuArray{Float32, 1, CUDA.Mem.DeviceBuffer}}}})(x::CUDA.CuArray{Int64, 1, CUDA.Mem.DeviceBuffer})
    @ Flux ~/.julia/packages/Flux/18YZE/src/layers/basic.jl:51
 [11] top-level scope
    @ REPL[11]:1
 [12] top-level scope
    @ ~/.julia/packages/CUDA/Uurn4/src/initialization.jl:52

I think the parameter vector will always get typed with the type of the first layer, due to map ?

In my example I have a chain with a nested ODE layer and I think it would be better to keep it on the CPU (not sure though).

Tested with Flux 0.12.10 and 0.13

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