This document is relevant for: Trn2, Trn3

nki.language.reciprocal#

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

Reciprocal of the input, element-wise.

((Similar to numpy.reciprocal))

Warning

This API is experimental and may change in future releases.

reciprocal(x) = 1 / x

This runs the Scalar Engine’s reciprocal activation (see Supported Activation Functions by NKI ISA), a table that is valid for 2^-42 <= |x| < 2^42 and accurate to 400 ULP there. Outside that range the table returns fixed values instead of 1 / x:

  • ±1e7, with the sign of x, for 0 < |x| < 2^-42;

  • ±0, with the sign of x, for finite |x| >= 2^42;

  • +3.3895314e38, the largest bfloat16 value, for ±0;

  • +0 for ±inf.

For exact 1 / x over the full range, use nki.isa.reciprocal() on the Vector Engine, at about 8x lower throughput for large tiles (see NKI Engine Selection for Operators Supported on Multiple Engines).

Parameters:
  • x – a tile.

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

a tile that has reciprocal values of x.

Examples:

import nki.language as nl

# nki.language.reciprocal -- reciprocal(4.0) = 0.25
a = nl.full((128, 512), 4.0, dtype=nl.float32,
            buffer=nl.sbuf)
b = nl.reciprocal(a)
expected = nl.full((128, 512), 0.25, dtype=nl.float32,
                   buffer=nl.sbuf)
assert nl.equal(b, expected)

This document is relevant for: Trn2, Trn3