This document is relevant for: Trn2, Trn3

nki.language.abs_max#

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

Maximum 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 larger absolute value. For example, abs_max(-5, 3) returns -5 because |-5| > |3|.

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_max -- returns the input with the larger absolute value
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_max(a, b)  # |−5| > |3| → returns -5.0 (original signed value)

expected = nl.full((128, 512), -5.0, dtype=nl.float32, buffer=nl.sbuf)
assert nl.equal(c, expected)
# nki.language.abs_max -- 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_max(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