This document is relevant for: Inf2, Trn1, Trn2
nki.isa.bn_stats#
- nki.isa.bn_stats(data, *, mask=None, dtype=None, **kwargs)[source]#
Compute mean- and variance-related statistics for each partition of an input tile
datain parallel using Vector Engine.The output tile of the instruction has 6 elements per partition:
the
countof the even elements (of the input tile elements from the same partition)the
meanof the even elementsvariance * countof the even elementsthe
countof the odd elementsthe
meanof the odd elementsvariance * countof the odd elements
To get the final mean and variance of the input tile, we need to pass the above
bn_statsinstruction output into the bn_aggr instruction, which will output two elements per partition:mean (of the original input tile elements from the same partition)
variance
Due to hardware limitation, the number of elements per partition (i.e., free dimension size) of the input
datamust not exceed 512 (nl.tile_size.bn_stats_fmax). To calculate per-partition mean/variance of a tensor with more than 512 elements in free dimension, we can invokebn_statsinstructions on each 512-element tile and use a singlebn_aggrinstruction to aggregatebn_statsoutputs from all the tiles. Refer to Example 2 for an example implementation.Vector Engine performs the above statistics calculation in float32 precision. Therefore, the engine automatically casts the input
datatile to float32 before performing float32 computation and is capable of casting the float32 computation results into another data type specified by thedtypefield, at no additional performance cost. Ifdtypefield is not specified, the instruction will cast the float32 results back to the same data type as the inputdatatile.Estimated instruction cost:
max(MIN_II, N)Vector Engine cycles, whereNis the number of elements per partition indataandMIN_IIis the minimum instruction initiation interval for small input tiles.MIN_IIis roughly 64 engine cycles.- Parameters:
data – the input tile (up to 512 elements per partition)
mask – (optional) a compile-time constant predicate that controls whether/how this instruction is executed (see NKI API Masking for details)
dtype – (optional) data type to cast the output type to (see Supported Data Types for more information); if not specified, it will default to be the same as the data type of the input tile.
- Returns:
an output tile with 6-element statistics per partition
Example:
import neuronxcc.nki.isa as nisa import neuronxcc.nki.language as nl from neuronxcc.nki.typing import tensor ################################################################## # Example 1: Calculate the mean and variance for each partition # of tile a with shape (128, 128) ################################################################## a: tensor[128, 128] = nl.load(a_tensor) stats_a: tensor[128, 6] = nisa.bn_stats(a) mean_var_a: tensor[128, 2] = nisa.bn_aggr(stats_a) # Extract mean and variance mean_a = mean_var_a[:, 0] var_a = mean_var_a[:, 1] nl.store(mean_a_tensor, mean_a) nl.store(var_a_tensor, var_a) # ################################################################## # # Example 2: Calculate the mean and variance for each partition of # # tile b with shape [128, 1024] # ################################################################## b: tensor[128, 1024] = nl.load(b_tensor) # Run bn_stats in two tiles because b has 1024 elements per partition, # but bn_stats has a limitation of nl.tile_size.bn_stats_fmax # Initialize a bn_stats output tile with shape of [128, 6*2] to # hold outputs of two bn_stats instructions stats_b = nl.ndarray((128, 6 * 2), dtype=nl.float32) bn_tile = nl.tile_size.bn_stats_fmax ix, iy = nl.mgrid[0:128, 0:bn_tile] iz, iw = nl.mgrid[0:128, 0:6] for i in range(1024 // bn_tile): stats_b[iz, i * 6 + iw] = nisa.bn_stats(b[ix, i * bn_tile + iy], dtype=nl.float32) mean_var_b = nisa.bn_aggr(stats_b) # Extract mean and variance mean_b = mean_var_b[:, 0] var_b = mean_var_b[:, 1] nl.store(mean_b_tensor, mean_b) nl.store(var_b_tensor, var_b)
This document is relevant for: Inf2, Trn1, Trn2