This document is relevant for: Inf2, Trn1, Trn2, Trn3
nrtpy tensor cast and compute API#
This page documents tensor_cast() and
tensor_compute(), which run dtype conversion and element-wise
reductions on device tensors directly on Neuron hardware, without compiling or
loading a NEFF. They wrap the nrt_tensor_cast and nrt_tensor_compute
APIs in libnrt.
Both functions are synchronous. They block until the operation completes, and they release the Python global interpreter lock (GIL) while they run, so other Python threads can continue.
Requirements and limitations#
Supported only on Trn2 and later instances.
Requires Neuron Driver 2.31 or later.
All source and destination tensors must be allocated on the same logical NeuronCore (LNC).
Every tensor passed to these functions must have a device dtype attached. See Device dtypes.
Operations always start at element 0 of each tensor.
Functions#
- nrtpy.tensor_cast(src, dst, num_elements)#
Convert
num_elementselements ofsrcfrom its device dtype to the device dtype ofdstand write the result todst.- Parameters:
src (nrtpy.NrtpyTensor) – Source tensor. Must have a device dtype attached.
dst (nrtpy.NrtpyTensor) – Destination tensor. Must have a device dtype attached and be large enough to hold
num_elementselements of that dtype.num_elements (int) – Number of elements to convert.
- Raises:
TypeError – If
srcordstis not anNrtpyTensor.nrtpy.NrtpyError – If a tensor has been freed or has no dtype.
nrtpy.NrtError – If
libnrtrejects or fails the cast.
- nrtpy.tensor_compute(op, srcs, dst, num_elements)#
Apply the element-wise reduction
opacross all tensors insrcsand writenum_elementsresult elements todst. For example, withNrtComputeOp.ADDand sources[a, b, c],dst[i] = a[i] + b[i] + c[i].- Parameters:
op (nrtpy.NrtComputeOp) – Reduction to apply.
srcs (Sequence[nrtpy.NrtpyTensor]) – One or more source tensors. Each must have a device dtype attached. Source dtypes can differ from each other and from
dst.dst (nrtpy.NrtpyTensor) – Destination tensor. Must have a device dtype attached.
num_elements (int) – Number of elements to process in each tensor.
- Raises:
TypeError – If
dstor any element ofsrcsis not anNrtpyTensor.nrtpy.NrtpyError – If
srcsis empty, or if a tensor has been freed or has no dtype.nrtpy.NrtError – If
libnrtrejects or fails the operation.
Both functions take NrtpyTensor objects and raise
TypeError for any other argument type.
Enumerations#
- class nrtpy.NrtComputeOp#
Element-wise reduction applied by
tensor_compute().- ADD#
Element-wise sum of all sources.
- MIN#
Element-wise minimum of all sources.
- MAX#
Element-wise maximum of all sources.
- class nrtpy.NrtDtype#
Element data type of a device tensor, matching
nrt_dtype_tinlibnrt.Value
Description
FLOAT3232-bit IEEE float
FP32R32-bit float (
fp32rhardware format)FLOAT1616-bit IEEE float
BFLOAT1616-bit brain float
FP8_E3,FP8_E4,FP8_E58-bit floats with 3, 4, or 5 exponent bits
INT8,INT16,INT32,INT64Signed integers
UINT8,UINT16,UINT32,UINT64Unsigned integers
Device dtypes#
Cast and compute use each tensor’s device dtype, not its NumPy dtype.
NrtpyTensor.from_numpy sets the
device dtype automatically for float16, float32, and the signed and
unsigned 8- to 64-bit integer types. Tensors created from other NumPy dtypes
have no device dtype and are rejected with NrtpyError. See
Device dtypes for the full mapping.
Output tensors that NrtpyModel allocates automatically also
have no device dtype. To cast or reduce model outputs, pass your own output
tensors created with from_numpy.
Numerical behavior#
The hardware block behind these APIs computes only in FP32. For every operation it:
Converts each source element from its dtype to FP32.
Applies the operation in FP32 (
ADD,MIN,MAX, or a multiply by 1.0 for a cast).Converts the FP32 result to the destination dtype.
Results can therefore differ from the equivalent host computation:
Casts that don’t involve FP32 round twice. For example,
FLOAT16toBFLOAT16goes fp16 → fp32 (exact) → bf16 (rounded), which may not match a single-step host conversion of the original data. Casts where one side is FP32 round only once and match the host result.Reductions accumulate in FP32. Summing BF16 sources keeps FP32 precision across the reduction and rounds once, on the final store. This is more accurate than accumulating in BF16, but FP32 addition is not associative, so results are not bit-identical to a different summation order.
Wide integers lose low bits. FP32 has a 24-bit significand, so
INT32,UINT32,INT64, andUINT64values with magnitude above 224 are rounded. Don’t use these APIs to move wide integer data byte-for-byte; use a plain tensor copy when no dtype change is needed.
Examples#
Cast FP32 to FP16#
import numpy as np
import nrtpy
from nrtpy import NrtpyTensor
src_data = np.array([1.5, -2.0, 3.25, 4.0], dtype=np.float32)
src = NrtpyTensor.from_numpy(src_data, name="cast_src")
dst = NrtpyTensor.from_numpy(np.zeros(src_data.size, dtype=np.float16),
name="cast_dst")
nrtpy.tensor_cast(src, dst, src_data.size)
print(dst.numpy()) # [ 1.5 -2. 3.25 4. ]
Element-wise ADD of two tensors#
import numpy as np
import nrtpy
from nrtpy import NrtComputeOp, NrtpyTensor
lhs = NrtpyTensor.from_numpy(np.array([1.0, 2.0, -3.0, 4.0], dtype=np.float32))
rhs = NrtpyTensor.from_numpy(np.array([5.0, -2.0, 7.0, 8.0], dtype=np.float32))
out = NrtpyTensor.from_numpy(np.zeros(4, dtype=np.float32))
nrtpy.tensor_compute(
NrtComputeOp.ADD,
[lhs, rhs],
out,
4,
)
print(out.numpy()) # [ 6. 0. 4. 12.]