<htmllang="en"><head><metacharset="UTF-8"/><metaname="viewport"content="width=device-width, initial-scale=1.0"/><title>GPU Support · Flux</title><script>(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
</script><linkhref="https://cdnjs.cloudflare.com/ajax/libs/normalize/4.2.0/normalize.min.css"rel="stylesheet"type="text/css"/><linkhref="https://fonts.googleapis.com/css?family=Lato|Roboto+Mono"rel="stylesheet"type="text/css"/><linkhref="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.min.css"rel="stylesheet"type="text/css"/><linkhref="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/default.min.css"rel="stylesheet"type="text/css"/><script>documenterBaseURL="."</script><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.2.0/require.min.js"data-main="assets/documenter.js"></script><scriptsrc="siteinfo.js"></script><scriptsrc="../versions.js"></script><linkhref="assets/documenter.css"rel="stylesheet"type="text/css"/><linkhref="../flux.css"rel="stylesheet"type="text/css"/></head><body><navclass="toc"><h1>Flux</h1><selectid="version-selector"onChange="window.location.href=this.value"style="visibility: hidden"></select><formclass="search"id="search-form"action="search.html"><inputid="search-query"name="q"type="text"placeholder="Search docs"/></form><ul><li><aclass="toctext"href="index.html">Home</a></li><li><spanclass="toctext">Building Models</span><ul><li><aclass="toctext"href="models/basics.html">Basics</a></li><li><aclass="toctext"href="models/recurrence.html">Recurrence</a></li><li><aclass="toctext"href="models/layers.html">Model Reference</a></li></ul></li><li><spanclass="toctext">Training Models</span><ul><li><aclass="toctext"href="training/optimisers.html">Optimisers</a></li><li><aclass="toctext"href="training/training.html">Training</a></li></ul></li><li><aclass="toctext"href="data/onehot.html">One-Hot Encoding</a></li><liclass="current"><aclass="toctext"href="gpu.html">GPU Support</a><ulclass="internal"></ul></li><li><aclass="toctext"href="community.html">Community</a></li></ul></nav><articleid="docs"><header><nav><ul><li><ahref="gpu.html">GPU Support</a></li></ul><aclass="edit-page"href="https://github.com/FluxML/Flux.jl/blob/master/docs/src/gpu.md"><spanclass="fa"></span> Edit on GitHub</a></nav><hr/><divid="topbar"><span>GPU Support</span><aclass="fa fa-bars"href="#"></a></div></header><h1><aclass="nav-anchor"id="GPU-Support-1"href="#GPU-Support-1">GPU Support</a></h1><p>Support for array operations on other hardware backends, like GPUs, is provided by external packages like <ahref="https://github.com/JuliaGPU/CuArrays.jl">CuArrays</a> and <ahref="https://github.com/JuliaGPU/CLArrays.jl">CLArrays</a>. Flux doesn't care what array type you use, so we can just plug these in without any other changes.</p><p>For example, we can use <code>CuArrays</code> (with the <code>cu</code> converter) to run our <ahref="models/basics.html">basic example</a> on an NVIDIA GPU.</p><pre><codeclass="language-julia">using CuArrays
loss(x, y) # ~ 3</code></pre><p>Note that we convert both the parameters (<code>W</code>, <code>b</code>) and the data set (<code>x</code>, <code>y</code>) to cuda arrays. Taking derivatives and training works exactly as before.</p><p>If you define a structured model, like a <code>Dense</code> layer or <code>Chain</code>, you just need to convert the internal parameters. Flux provides <code>mapleaves</code>, which allows you to alter all parameters of a model at once.</p><pre><codeclass="language-julia">d = Dense(10, 5, σ)
d(cu(rand(10)))</code></pre><p>The <ahref="https://github.com/FluxML/model-zoo/blob/master/mnist/mnist.jl">mnist example</a> contains the code needed to run the model on the GPU; just uncomment the lines after <code>using CuArrays</code>.</p><footer><hr/><aclass="previous"href="data/onehot.html"><spanclass="direction">Previous</span><spanclass="title">One-Hot Encoding</span></a><aclass="next"href="community.html"><spanclass="direction">Next</span><spanclass="title">Community</span></a></footer></article></body></html>