Skip to content

Commit

Permalink
Read default backend from application environment
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Sep 25, 2021
1 parent 6fda0ce commit 31b7c92
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
27 changes: 17 additions & 10 deletions nx/lib/nx.ex
Original file line number Diff line number Diff line change
Expand Up @@ -277,17 +277,24 @@ defmodule Nx do
Nx.exp(t) / Nx.sum(Nx.exp(t))
end
The `keep_on_device: true` run option will keep the tensor on
the backend. You can transfer it back to a binary tensor by
calling `backend_transfer/2` or `backend_copy/2`. If you don't
intend to use the data for some reason, you can explicitly call
`backend_deallocate/1` to deallocate it.
The `keep_on_device: true` run option will keep the tensor
allocated elsewhere and not as an Elixir binary. You can transfer
it back to a binary tensor by calling `backend_transfer/2` or
`backend_copy/2`. If you don't intend to use the data for some
reason, you can explicitly call `backend_deallocate/1` to
deallocate it.
However, most often backends are used to provide a completely
different implementation of tensor operations, often accelerated
to the GPU. In such cases, you want to guarantee all tensors
are allocated in the new backend. This can be done by calling
`Nx.default_backend/1`:
are allocated in the new backend. This can be done by configuring
your runtime:
# config/runtime.exs
import Config
config :nx, default_backend: Lib.CustomBackend
Or by calling `Nx.default_backend/1`:
Nx.default_backend({Lib.CustomBackend, device: :cuda})
Expand Down Expand Up @@ -2444,7 +2451,6 @@ defmodule Nx do
## Backend API

@backend_key {Nx, :default_backend}
@backend_default {Nx.BinaryBackend, []}

@doc """
Sets the current process default backend to `backend` with the given `opts`.
Expand All @@ -2462,14 +2468,15 @@ defmodule Nx do
"""
def default_backend(backend) do
Process.put(@backend_key, backend!(backend)) || @backend_default
Process.put(@backend_key, backend!(backend)) ||
backend!(Application.fetch_env!(:nx, :default_backend))
end

@doc """
Gets the default backend for the current process.
"""
def default_backend() do
Process.get(@backend_key) || @backend_default
Process.get(@backend_key) || backend!(Application.fetch_env!(:nx, :default_backend))
end

@doc """
Expand Down
5 changes: 4 additions & 1 deletion nx/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ defmodule Nx.MixProject do
end

def application do
[extra_applications: [:logger]]
[
extra_applications: [:logger],
env: [default_backend: {Nx.BinaryBackend, []}]
]
end

defp deps do
Expand Down

0 comments on commit 31b7c92

Please sign in to comment.