This document is relevant for: Trn2, Trn3

nki.language.abs_min#

nki.language.abs_min(x, y, dtype=None)[source]#

Minimum of the inputs compared by magnitude, element-wise.

Compares abs(x) and abs(y) and returns the original (signed) value of whichever input has the smaller absolute value. For example, abs_min(-5, 3) returns 3 because |3| < |-5|.

Note

Available only on NeuronCore-v4 (trn3) and newer.

Warning

This API is experimental and may change in future releases.

Parameters:
  • x – a tile or a scalar value.

  • y – a tile or a scalar value.

  • dtype – (optional) data type to cast the output type to (see Supported Data Types for more information);

Returns:

a tile where each element is x if |x| < |y|, else y.

Examples:

import nki.language as nl

# nki.language.abs_min -- element-wise absolute minimum of two tiles
a = nl.full((128, 512), 5.0, dtype=nl.float32, buffer=nl.sbuf)
b = nl.full((128, 1), -3.0, dtype=nl.float32, buffer=nl.sbuf)
c = nl.abs_min(a, b)  # |-3| < |5|, so returns -3.0 (original signed value)

expected = nl.full((128, 512), -3.0, dtype=nl.float32, buffer=nl.sbuf)
assert nl.equal(c, expected)
# nki.language.abs_min -- tie-breaking: returns y when |x| == |y|
a = nl.full((128, 512), 1.0, dtype=nl.float32, buffer=nl.sbuf)
b = nl.full((128, 1), -1.0, dtype=nl.float32, buffer=nl.sbuf)
c = nl.abs_min(a, b)  # |1| == |-1| → tie, returns y = -1.0

expected = nl.full((128, 512), -1.0, dtype=nl.float32, buffer=nl.sbuf)
assert nl.equal(c, expected)

This document is relevant for: Trn2, Trn3