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

gpu function does nothing, but only on first run #1119

Open
clintonTE opened this issue Apr 10, 2020 · 1 comment
Open

gpu function does nothing, but only on first run #1119

clintonTE opened this issue Apr 10, 2020 · 1 comment

Comments

@clintonTE
Copy link

This isn't that big a deal, as the workarounds are fairly obvious, but it is rather strange. Note this seems very related (same root issue?) as #1114.

This is on the latest Flux Master. IDE is Windows-Juno with Julia 1.4.

using Revise, CUDAapi, Flux, BenchmarkTools
import CuArrays

struct MWEStruct{Ta<:AbstractVector, Tb<:AbstractVector}
  a::Ta
  b::Tb
end

MWEStruct(N) = MWEStruct(rand(N), rand(N))

(m::MWEStruct)(x) = x

function mwe(N)
  Flux.@functor MWEStruct

  cpustruct = MWEStruct(N)
  println("type of cpu struct: ", typeof(cpustruct))
  gpustruct = cpustruct |> gpu
  println("type of gpu struct: ", typeof(gpustruct))
end

println("First run:")
mwe(10)
println("\nSecond run:")
mwe(10)

Output:

First run:
type of cpu struct: MWEStruct{Array{Float64,1},Array{Float64,1}}
type of gpu struct: MWEStruct{Array{Float64,1},Array{Float64,1}}

Second run:
type of cpu struct: MWEStruct{Array{Float64,1},Array{Float64,1}}
type of gpu struct: MWEStruct{CuArrays.CuArray{Float32,1,Nothing},CuArrays.CuArray{Float32,1,Nothing}}
@DrChainsaw
Copy link
Contributor

I believe this is because you call the functor macro inside a function. Put it somewhere outside, e.g right after the declaration of MWEStruct.

This explanation might not be 100%, but here's my understanding of it: The function it creates for you will not be available until you hit the global scope so that the world age advances which in this case is after mwe returns. Search for "world age problem julia language" for more info.

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

2 participants