.. meta::
   :description: Rounding modes on AWS Neuron: round nearest ties to even (RNE) and stochastic rounding (SR) on NeuronCore-v2 and later (Trn2, Trn3, Trn1, Inf2), with a TorchNeuron example.
   :keywords: rounding modes, stochastic rounding, RNE, NEURON_RT_STOCHASTIC_ROUNDING_EN, TorchNeuron, NeuronCore-v3, NeuronCore-v4, Trn2, Trn3, AWS Neuron
   :date-modified: 2026-11-16

.. _neuron-rounding-modes:

Neuron Rounding Modes
=====================

.. contents:: Table of contents
	:local:
	:depth: 1


.. _neuron-rounding-mode-rne:

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: 

.. image:: /images/rne1.png
    :width: 700

.. image:: /images/rne2.png
    :width: 700

.. image:: /images/rne3.png
    :width: 700

.. _neuron-rounding-mode-sr:


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: 

.. image:: /images/sr.png
    :width: 700


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).


.. _neuron-rounding-mode-select:

Select the rounding mode
------------------------

The Neuron Runtime selects the rounding mode from two environment variables:

.. list-table::
   :header-rows: 1
   :widths: 40 60

   * - Variable
     - Description
   * - ``NEURON_RT_STOCHASTIC_ROUNDING_EN``
     - ``1`` turns on stochastic rounding. ``0`` uses RNE rounding.
   * - ``NEURON_RT_STOCHASTIC_ROUNDING_SEED``
     - 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 :ref:`nrt-env-var-reference`.

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.

.. code-block:: bash

   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``:

.. code-block:: python

   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:

.. code-block:: bash

   $ 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 :ref:`neuron-rounding-mode-select`).

Related information
-------------------

- :ref:`Neuron data types <neuron-data-types>`
- :ref:`Neuron Runtime environment variables <nrt-env-var-reference>`
- :ref:`TorchNeuron <about-pytorch-neuron>`
