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

Flux.cpu and Flux.gpu no longer move data on views #2553

Closed
piever opened this issue Dec 13, 2024 · 3 comments
Closed

Flux.cpu and Flux.gpu no longer move data on views #2553

piever opened this issue Dec 13, 2024 · 3 comments

Comments

@piever
Copy link
Contributor

piever commented Dec 13, 2024

Currently (as of Flux 0.15), Flux.cpu and Flux.gpu do not error when fed a view, but they simply return an identical value, without moving data to / from the GPU.

MWE

julia> using Flux, CUDA, cuDNN

julia> cpu_view = view(rand(2, 2), 1, :);

julia> gpu_view = view(CUDA.rand(2, 2), 1, :);

julia> Flux.gpu(cpu_view)
2-element view(::Matrix{Float64}, 1, :) with eltype Float64:
 0.10101004098832933
 0.7891135815339817

julia> Flux.cpu(gpu_view)
2-element view(::CuArray{Float32, 2, CUDA.DeviceMemory}, 1, :) with eltype Float32:
 0.5095508
 0.93324655

Dependencies:

  [052768ef] CUDA v5.5.2
  [587475ba] Flux v0.15.2
  [02a925ec] cuDNN v1.4.0

on Julia 1.11.2

On Flux 0.14, Flux.cpu and Flux.gpu would transform views by applying the transformation to the parent array.

@CarloLucibello
Copy link
Member

We now completely rely on MLDataDevices.jl for the data movement. gpu and cpu are simply defined as

gpu(x) = gpu_device()(x)
cpu(x) = cpu_device()(x)

Could you report the issue to the Lux repo?
Unfortunately the transition has not been entirely smooth, and some of the v0.14 behavior is not preserved, e.g. LuxDL/Lux.jl#1129

@CarloLucibello
Copy link
Member

The issue is not specific to the CUDA backend:

julia> using Flux, Metal

julia> x = rand(2 , 2)
2×2 Matrix{Float64}:
 0.99688   0.613988
 0.363596  0.985924

julia> cpu_view = view(x, 1, :)
2-element view(::Matrix{Float64}, 1, :) with eltype Float64:
 0.9968804026540483
 0.6139877405888565

julia> gpu_view = view(gpu(x), 1, :)
2-element view(::MtlMatrix{Float32, Metal.PrivateStorage}, 1, :) with eltype Float32:
 0.9968804
 0.61398774

julia> gpu(cpu_view)
2-element view(::Matrix{Float64}, 1, :) with eltype Float64:
 0.9968804026540483
 0.6139877405888565

julia> cpu(gpu_view)
2-element view(::MtlMatrix{Float32, Metal.PrivateStorage}, 1, :) with eltype Float32:
 0.9968804
 0.61398774

@piever
Copy link
Contributor Author

piever commented Dec 16, 2024

That was fast! Closing here as this was fixed in the latest MLDataDevices release before I even reported it...

Even if there are some hiccups, happy to hear that more infrastructure is being shared.

@piever piever closed this as completed Dec 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants