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

doc reference for gpu_backend! #2292

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/src/gpu.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Flux relies on [Preferences.jl](https://github.com/JuliaPackaging/Preferences.jl

There are two ways you can specify it:

- From the REPL/code in your project, call `Flux.gpu_backend!("AMD")` and restart (if needed) Julia session for the changes to take effect.
- From the REPL/code in your project, call [`Flux.gpu_backend!`](ref)`("AMD")` and restart (if needed) Julia session for the changes to take effect.
- In `LocalPreferences.toml` file in you project directory specify:
```toml
[Flux]
Expand Down
1 change: 1 addition & 0 deletions docs/src/models/functors.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Functors.fmapstructure
Flux provides some convenience functions based on `fmap`. Some ([`f16`](@ref Flux.f16), [`f32`](@ref Flux.f32), [`f64`](@ref Flux.f64)) change the precision of all arrays in a model. Others are used for moving a model to of from GPU memory:

```@docs
Flux.gpu_backend!
cpu
gpu(::Any)
gpu(::Flux.DataLoader)
Expand Down
16 changes: 14 additions & 2 deletions src/functor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,14 @@ _isleaf(x) = _isbitsarray(x) || Functors.isleaf(x)
const GPU_BACKENDS = ("CUDA", "AMD", "Metal")
const GPU_BACKEND = @load_preference("gpu_backend", "CUDA")

"""
gpu_backend!(backend::String)

Sets the gpu backend in `LocalPreferences.toml`.
Possible `backend` values are "CUDA", "AMD", and "Metal".
The selected backend affects the data movement in [`Flux.gpu`](@ref).
Current backend is stored in `Flux.GPU_BACKEND`.
"""
function gpu_backend!(backend::String)
if backend == GPU_BACKEND
@info """
Expand Down Expand Up @@ -217,8 +225,10 @@ end
Copies `m` to the current GPU device (using current GPU backend), if one is available.
If no GPU is available, it does nothing (but prints a warning the first time).

On arrays, this calls CUDA's `cu`, which also changes arrays
with Float64 elements to Float32 while copying them to the device (same for AMDGPU).
When the backed is set to "CUDA", when called on arrays it calls `CUDA.cu`,
which also changes arrays with Float64 elements to Float32 while copying them to the device.
Similar conversions happen for "AMDGPU" and "Metal" backends.

To act on arrays within a struct, the struct type must be marked with [`@functor`](@ref).

Use [`cpu`](@ref) to copy back to ordinary `Array`s.
Expand All @@ -227,6 +237,8 @@ See also [`f32`](@ref) and [`f16`](@ref) to change element type only.
See the [CUDA.jl docs](https://juliagpu.github.io/CUDA.jl/stable/usage/multigpu/)
to help identify the current device.

See [`Flux.gpu_backend!`](@ref) for setting the backend.

# Example
```julia-repl
julia> m = Dense(rand(2, 3)) # constructed with Float64 weight matrix
Expand Down