Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pointer(CuArray) is not defined #131

Closed
una-dinosauria opened this issue Sep 11, 2019 · 2 comments
Closed

pointer(CuArray) is not defined #131

una-dinosauria opened this issue Sep 11, 2019 · 2 comments

Comments

@una-dinosauria
Copy link

The demos from Metalhead.jl are broken if one tries to run them on the GPU (xref FluxML/Metalhead.jl#42), eg

julia> using CuArrays, Metalhead, Flux
julia> vgg = VGG19() |> gpu
julia> x = cu(rand(Float32, 224, 224, 3, 1));
julia> vgg(x)

The trace points to a an exception in NNlib:

ERROR: conversion to pointer not defined for CuArray{Float32,2}
Stacktrace:
 [1] error(::String) at ./error.jl:33
 [2] unsafe_convert(::Type{Ptr{Float32}}, ::CuArray{Float32,2}) at ./pointer.jl:67
 [3] pointer(::CuArray{Float32,2}) at ./abstractarray.jl:934
 [4] macro expansion at /home/julieta/.julia/packages/NNlib/mxWRT/src/impl/conv_im2col.jl:54 [inlined]
 [5] macro expansion at ./gcutils.jl:87 [inlined]
 [6] macro expansion at /home/julieta/.julia/packages/NNlib/mxWRT/src/impl/conv_im2col.jl:53 [inlined]
 [7] #conv_im2col!#231(::CuArray{Float32,2}, ::Float32, ::Float32, ::typeof(NNlib.conv_im2col!), ::CuArray{Float32,5}, ::CuArray{Float32,5}, ::Array{Float32,5}, ::DenseConvDims{3,(3, 3, 1),3,64,(1, 1, 1),(1, 1, 1, 1, 0, 0),(1, 1, 1),false}) at /home/julieta/.julia/packages/TimerOutputs/7zSea/src/TimerOutput.jl:190
 [8] conv!(::CuArray{Float32,5}, ::CuArray{Float32,5}, ::Array{Float32,5}, ::DenseConvDims{3,(3, 3, 1),3,64,(1, 1, 1),(1, 1, 1, 1, 0, 0),(1, 1, 1),false}) at /home/julieta/.julia/packages/TimerOutputs/7zSea/src/TimerOutput.jl:198
 [9] #conv!#56(::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}, ::typeof(conv!), ::CuArray{Float32,4}, ::CuArray{Float32,4}, ::Array{Float32,4}, ::DenseConvDims{2,(3, 3),3,64,(1, 1),(1, 1, 1, 1),(1, 1),false}) at /home/julieta/.julia/packages/NNlib/mxWRT/src/conv.jl:68
 [10] conv!(::CuArray{Float32,4}, ::CuArray{Float32,4}, ::Array{Float32,4}, ::DenseConvDims{2,(3, 3),3,64,(1, 1),(1, 1, 1, 1),(1, 1),false}) at /home/julieta/.julia/packages/NNlib/mxWRT/src/conv.jl:68
 [11] macro expansion at /home/julieta/.julia/packages/NNlib/mxWRT/src/conv.jl:114 [inlined]
 [12] #conv#97(::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}, ::typeof(conv), ::CuArray{Float32,4}, ::Array{Float32,4}, ::DenseConvDims{2,(3, 3),3,64,(1, 1),(1, 1, 1, 1),(1, 1),false}) at /home/julieta/.julia/packages/TimerOutputs/7zSea/src/TimerOutput.jl:190
 [13] conv(::CuArray{Float32,4}, ::Array{Float32,4}, ::DenseConvDims{2,(3, 3),3,64,(1, 1),(1, 1, 1, 1),(1, 1),false}) at /home/julieta/.julia/packages/TimerOutputs/7zSea/src/TimerOutput.jl:198
 [14] (::Conv{2,2,typeof(relu),Array{Float32,4},Array{Float32,1}})(::CuArray{Float32,4}) at /home/julieta/.julia/packages/Flux/dkJUV/src/layers/conv.jl:55
 [15] applychain(::Tuple{Conv{2,2,typeof(relu),Array{Float32,4},Array{Float32,1}},Conv{2,2,typeof(relu),Array{Float32,4},Array{Float32,1}},MaxPool{2,4},Conv{2,2,typeof(relu),Array{Float32,4},Array{Float32,1}},Conv{2,2,typeof(relu),Array{Float32,4},Array{Float32,1}},MaxPool{2,4},Conv{2,2,typeof(relu),Array{Float32,4},Array{Float32,1}},Conv{2,2,typeof(relu),Array{Float32,4},Array{Float32,1}},Conv{2,2,typeof(relu),Array{Float32,4},Array{Float32,1}},Conv{2,2,typeof(relu),Array{Float32,4},Array{Float32,1}},MaxPool{2,4},Conv{2,2,typeof(relu),Array{Float32,4},Array{Float32,1}},Conv{2,2,typeof(relu),Array{Float32,4},Array{Float32,1}},Conv{2,2,typeof(relu),Array{Float32,4},Array{Float32,1}},Conv{2,2,typeof(relu),Array{Float32,4},Array{Float32,1}},MaxPool{2,4},Conv{2,2,typeof(relu),Array{Float32,4},Array{Float32,1}},Conv{2,2,typeof(relu),Array{Float32,4},Array{Float32,1}},Conv{2,2,typeof(relu),Array{Float32,4},Array{Float32,1}},Conv{2,2,typeof(relu),Array{Float32,4},Array{Float32,1}},MaxPool{2,4},getfield(Metalhead, Symbol("##44#45")),Dense{typeof(relu),LinearAlgebra.Adjoint{Float32,Array{Float32,2}},Array{Float32,1}},Dropout{Float32},Dense{typeof(relu),LinearAlgebra.Adjoint{Float32,Array{Float32,2}},Array{Float32,1}},Dropout{Float32},Dense{typeof(identity),LinearAlgebra.Adjoint{Float32,Array{Float32,2}},Array{Float32,1}},typeof(softmax)}, ::CuArray{Float32,4}) at /home/julieta/.julia/packages/Flux/dkJUV/src/layers/basic.jl:31
 [16] (::Chain{Tuple{Conv{2,2,typeof(relu),Array{Float32,4},Array{Float32,1}},Conv{2,2,typeof(relu),Array{Float32,4},Array{Float32,1}},MaxPool{2,4},Conv{2,2,typeof(relu),Array{Float32,4},Array{Float32,1}},Conv{2,2,typeof(relu),Array{Float32,4},Array{Float32,1}},MaxPool{2,4},Conv{2,2,typeof(relu),Array{Float32,4},Array{Float32,1}},Conv{2,2,typeof(relu),Array{Float32,4},Array{Float32,1}},Conv{2,2,typeof(relu),Array{Float32,4},Array{Float32,1}},Conv{2,2,typeof(relu),Array{Float32,4},Array{Float32,1}},MaxPool{2,4},Conv{2,2,typeof(relu),Array{Float32,4},Array{Float32,1}},Conv{2,2,typeof(relu),Array{Float32,4},Array{Float32,1}},Conv{2,2,typeof(relu),Array{Float32,4},Array{Float32,1}},Conv{2,2,typeof(relu),Array{Float32,4},Array{Float32,1}},MaxPool{2,4},Conv{2,2,typeof(relu),Array{Float32,4},Array{Float32,1}},Conv{2,2,typeof(relu),Array{Float32,4},Array{Float32,1}},Conv{2,2,typeof(relu),Array{Float32,4},Array{Float32,1}},Conv{2,2,typeof(relu),Array{Float32,4},Array{Float32,1}},MaxPool{2,4},getfield(Metalhead, Symbol("##44#45")),Dense{typeof(relu),LinearAlgebra.Adjoint{Float32,Array{Float32,2}},Array{Float32,1}},Dropout{Float32},Dense{typeof(relu),LinearAlgebra.Adjoint{Float32,Array{Float32,2}},Array{Float32,1}},Dropout{Float32},Dense{typeof(identity),LinearAlgebra.Adjoint{Float32,Array{Float32,2}},Array{Float32,1}},typeof(softmax)}})(::CuArray{Float32,4}) at /home/julieta/.julia/packages/Flux/dkJUV/src/layers/basic.jl:33
 [17] (::VGG19)(::CuArray{Float32,4}) at /home/julieta/.julia/packages/Metalhead/1dQOk/src/vgg19.jl:46
 [18] top-level scope at REPL[40]:1

Which in this example comes from the NNlib kernel trying to obtain a pointer from a CuArray

col_ptr = pointer(col)

I believe to obtain the pointer of a CuArray one now does

julia> pointer(x.buf)
CUDAdrv.CuPtr{Nothing}(0x00007f09ffc40000)

But I have no idea how to rewrite the code to make this compatible between CPU and GPU arrays.

@ljluestc
Copy link

To make the code compatible between CPU and GPU arrays, you can modify the line in the Metalhead.jl package where the pointer() function is used. Instead of directly calling pointer(col), you can create a helper function that handles both CPU and GPU arrays.

@ToucheSir
Copy link
Member

This is an ancient issue (MWE still uses CuArrays) and we explicitly test Metalhead models on GPU now, so closing as long past stale.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants