View Source Torchx.Backend (Torchx v0.9.0)

An opaque backend Nx backend with bindings to libtorch/Pytorch.

Torchx behaviour that is different from BinaryBackend:

  1. Torchx emulates the u16/u32/u64 unsigned integers using signed integers. In practice this means u64 actually overflows at u32. When accessing the underlying tensor, you will get signed integers back.

    iex> t = Nx.Constants.max_finite({:u, 32}) #Nx.Tensor< u32 4294967295

    iex> t |> Torchx.from_nx() |> Torchx.to_nx() #Nx.Tensor< s64 4294967295

  2. Torchx rounds half-to-even, while Elixir rounds half-away-from-zero. So in Elixir round(0.5) == 1.0 while in Torchx round(0.5) == 0.0.

  3. Nx.as_type/2 converts non-finite values such as infinity becomes the maximum value for a type, negative infinity becomes the minimum value, and nan becomes zero. Torchx behaviour is type dependent with no clear rule across types.

Options

  • :device - Defaults to Torchx.default_device/0. An atom representing the device for the allocation of a given tensor. Valid values can be seen at the main Torchx docs.