Flux.jl/docs/src/gpu.md

88 lines
2.5 KiB
Markdown
Raw Normal View History

2017-09-28 10:08:37 +00:00
# GPU Support
## Installation
To get GPU support for NVIDIA graphics cards, you need to install `CuArrays.jl`
**Steps needed**
2019-01-11 04:57:54 +00:00
1. Install [NVIDIA Driver](http://www.nvidia.com/Download/index.aspx?lang=en-us)
2. Install [NVIDIA toolkit](https://developer.nvidia.com/cuda-downloads)
3. Install [NVIDIA cuDNN library](https://developer.nvidia.com/cudnn)
4. In Julia's terminal run `]add CuArrays`
5. In Julia's terminal run `]build CuArrays`
6. In Julia's terminal run `]build Flux`
## GPU Usage
2017-09-28 10:08:37 +00:00
2018-03-05 19:23:13 +00:00
Support for array operations on other hardware backends, like GPUs, is provided by external packages like [CuArrays](https://github.com/JuliaGPU/CuArrays.jl). Flux is agnostic to array types, so we simply need to move model weights and data to the GPU and Flux will handle it.
2017-09-28 10:08:37 +00:00
2017-09-28 10:11:11 +00:00
For example, we can use `CuArrays` (with the `cu` converter) to run our [basic example](models/basics.md) on an NVIDIA GPU.
2017-09-28 10:08:37 +00:00
2019-01-03 00:25:25 +00:00
(Note that you need to have CUDA available to use CuArrays please see the [CuArrays.jl](https://github.com/JuliaGPU/CuArrays.jl) instructions for more details.)
2018-06-26 13:25:24 +00:00
2017-09-28 10:08:37 +00:00
```julia
using CuArrays
2017-09-28 10:11:11 +00:00
W = cu(rand(2, 5)) # a 2×5 CuArray
2017-09-28 10:08:37 +00:00
b = cu(rand(2))
predict(x) = W*x .+ b
loss(x, y) = sum((predict(x) .- y).^2)
x, y = cu(rand(5)), cu(rand(2)) # Dummy data
loss(x, y) # ~ 3
```
Note that we convert both the parameters (`W`, `b`) and the data set (`x`, `y`) to cuda arrays. Taking derivatives and training works exactly as before.
2017-10-17 00:08:15 +00:00
If you define a structured model, like a `Dense` layer or `Chain`, you just need to convert the internal parameters. Flux provides `mapleaves`, which allows you to alter all parameters of a model at once.
2017-09-28 10:08:37 +00:00
```julia
d = Dense(10, 5, σ)
2017-10-17 00:08:15 +00:00
d = mapleaves(cu, d)
2017-09-28 10:08:37 +00:00
d.W # Tracked CuArray
d(cu(rand(10))) # CuArray output
m = Chain(Dense(10, 5, σ), Dense(5, 2), softmax)
2017-10-17 00:08:15 +00:00
m = mapleaves(cu, m)
2017-09-28 10:08:37 +00:00
d(cu(rand(10)))
```
2017-09-28 10:11:11 +00:00
2018-03-05 19:23:13 +00:00
As a convenience, Flux provides the `gpu` function to convert models and data to the GPU if one is available. By default, it'll do nothing, but loading `CuArrays` will cause it to move data to the GPU instead.
```julia
julia> using Flux, CuArrays
julia> m = Dense(10,5) |> gpu
Dense(10, 5)
julia> x = rand(10) |> gpu
10-element CuArray{Float32,1}:
0.800225
0.511655
julia> m(x)
Tracked 5-element CuArray{Float32,1}:
-0.30535
-0.618002
```
2018-03-05 19:25:43 +00:00
The analogue `cpu` is also available for moving models and data back off of the GPU.
2018-03-19 12:03:31 +00:00
```julia
2018-03-05 19:25:43 +00:00
julia> x = rand(10) |> gpu
10-element CuArray{Float32,1}:
0.235164
0.192538
julia> x |> cpu
10-element Array{Float32,1}:
0.235164
0.192538
2018-03-19 12:03:31 +00:00
```