View Source Torchx (Torchx v0.9.2)
Bindings and Nx integration for PyTorch.
Torchx provides an Nx backend through Torchx.Backend
, which
allows for integration with both the CPU and GPU functionality
that PyTorch provides. To enable Torchx as the default backend
you can add the following line to your desired config environment (config/config.exs
,
config/test.exs
, etc):
import Config
config :nx, :default_backend, Torchx.Backend
This will ensure that by default all tensors are created PyTorch tensors.
It's important to keep in mind that the default device is the CPU. If you
wish to allocated tensors to the GPU by default, you can pass the :device
option to the config line, as follows:
import Config
config :nx, :default_backend, {Torchx.Backend, device: :cuda}
The device_available?/1
function can be used to determine whether
:cuda
is available. If you have CUDA installed but it doesn't show
as available, check out the Installation README section.
Types
Torchx implements specific names for PyTorch types, which have Nx counterparts as in the following table:
Nx Type | Torchx Type | Description |
---|---|---|
{:u, 8} | :byte | Unsigned 8-bit integer |
{:s, 8} | :char | Signed 8-bit integer |
{:s, 16} | :short | Signed 16-bit integer |
{:s, 32} | :int | Signed 32-bit integer |
{:s, 64} | :long | Signed 64-bit integer |
{:bf, 16} | :brain | 16-bit brain floating-point number |
{:f, 8} | :float8_e5m2 | 8-bit floating-point number |
{:f, 16} | :half | 16-bit floating-point number |
{:f, 32} | :float | 32-bit floating-point number |
{:f, 64} | :double | 64-bit floating-point number |
{:c, 64} | :complex | 64-bit complex number, with two 32-bit float components |
{:c, 128} | :complex_double | 128-bit complex number, with two 64-bit float components |
Devices
PyTorch implements a variety of devices, which can be seen below.
:cpu
:cuda
:mkldnn
:opengl
:opencl
:ideep
:hip
:fpga
:msnpu
:xla
:vulkan
:metal
:xpu
:mps
Summary
Functions
Returns the default device.
Check if device of the given type is available for Torchx.
Return devices quantity for the given device type.
Gets a Torchx tensor from a Nx tensor.
Converts a Torchx tensor to a Nx tensor.
Functions
conv(tensor_input, tensor_kernel, strides, padding, dilation, transposed, groups)
View SourceReturns the default device.
Here is the priority in the order of availability:
:cuda
:cpu
The default can also be set (albeit not recommended)
via the application environment by setting the
:default_device
option under the :torchx
application.
Check if device of the given type is available for Torchx.
You can currently check the availability of:
:cuda
:mps
:cpu
Return devices quantity for the given device type.
You can check the device count of :cuda
for now.
Gets a Torchx tensor from a Nx tensor.
index_put(tensor_input, tensor_indices, tensor_updates, accumulate)
View Sourcemax_pool_3d(tensor_input, kernel_size, strides, padding, dilation)
View SourceConverts a Torchx tensor to a Nx tensor.