This document is relevant for: Inf2, Trn1, Trn2, Trn3
Neuron Rounding Modes#
Round Nearest, ties to Even (RNE)#
When the exact result of a floating point operation cannot be exactly represented as a floating point value, it must be rounded. The IEEE 754-2008 standard defines the default rounding mode to be ‘Round Nearest, ties to Even’ (RNE for short). Under this scheme, numbers are rounded to the nearest representable value, and in case of a ‘tie’ (i.e. the number is exactly between the two nearest representable values) numbers will be rounded to the nearest even number.
All NeuronCore generations support the RNE rounding scheme, which is the most commonly used rounding scheme for Machine Learning workloads. Below is an illustration of the RNE rounding scheme:
Stochastic Rounding (SR)#
One downside of the RNE rounding scheme (and other rounding schemes described in the IEEE 754-2008 standard), is that when adding floating point values of significantly different magnitudes, rounding can squash small values and prevent them from accumulating over time.
To improve this, NeuronCore-v2 and later (NeuronCore-v2, NeuronCore-v3, and NeuronCore-v4, on Trn1, Inf2, Trn2, and Trn3) let you choose between the RNE rounding scheme described above, and a second rounding scheme called ‘Stochastic Rounding’ (SR for short). Stochastic rounding prevents the computation precision-loss described above, by performing the rounding operations in a probabilistic manner, according to the relative distance from the two nearest representable values, as illustrated below:
By performing the rounding in a probabilistic manner, this scheme allows for small increments to accumulate over time, even when added to numbers of significantly higher magnitude, which leads to more precise results when performing large floating point computations (as done for machine learning).
Select the rounding mode#
The Neuron Runtime selects the rounding mode from two environment variables:
Variable |
Description |
|---|---|
|
|
|
The seed of the stochastic rounding random number generator. Set it to make a stochastic rounding run reproducible. |
TorchNeuron does not set NEURON_RT_STOCHASTIC_ROUNDING_EN. Set the variable explicitly before
you start your program, so that you know which rounding mode a run uses. For the defaults of both
variables, see Reference: Neuron Runtime environment variables.
To make a run with stochastic rounding reproducible, pin both random number generators: set
NEURON_RT_STOCHASTIC_ROUNDING_SEED for the rounding, and call torch.manual_seed() for the
random operations in your PyTorch code.
export NEURON_RT_STOCHASTIC_ROUNDING_EN=1
export NEURON_RT_STOCHASTIC_ROUNDING_SEED=42
python3 train.py # train.py calls torch.manual_seed(42)
Quick test#
This test adds 0.5 to an FP16 value of 1024 for 2,048 steps on a NeuronCore, in TorchNeuron
eager mode. FP16 can represent 1024 and 1025, but no value between them. So each exact result,
such as 1024.5, is exactly halfway between two FP16 values: a tie. Save the code as rounding_mode_test.py:
import torch
device = torch.device("neuron")
a = torch.tensor(1024.0, dtype=torch.float16, device=device)
for i in range(2048):
a = a + 0.5
print(a.cpu())
Run the test with each rounding mode:
$ NEURON_RT_STOCHASTIC_ROUNDING_EN=0 python3 rounding_mode_test.py
$ NEURON_RT_STOCHASTIC_ROUNDING_EN=1 python3 rounding_mode_test.py
With RNE rounding, the result stays at 1024: each tie rounds to the even value, 1024. With
stochastic rounding, the result is close to the exact sum, 2048. The exact value changes from
run to run unless you set NEURON_RT_STOCHASTIC_ROUNDING_SEED (see Select the rounding mode).