Skip to content

hk.math

Stability: stable, except where an entry says otherwise (stability).

Elementwise functions of a register tile or vector: hk.math.exp(t) is t.exp(); into= writes an existing value (hk.math.exp(t, into=t) is t.exp_()). This is the whole list -- there is no tanh, sigmoid, sin or erf; compose them (sigmoid(x) = silu(x) / x is not safe at 0, so 1 / (1 + exp(-x)) with exp, add and div).

The transcendental functions are specialised for fp32 tiles: load bf16 or fp16 data into an fp32 tile (x.at[r, c].load(hk.RT(hk.fp32, 16, 64)) converts on the way) and convert back with .to(hk.bf16) before the store. A function the library lacks for a dtype raises while tracing.

fma, rcp, popcount, ctz, ceil and floor have no tile op: of strips each is a one-op hk.map (gfx950), and every function here also takes hk.map elements, inside the fn of hk.map(fn, ...).

function torch equivalent also spelled
hk.math.exp torch.exp t.exp(), t.exp_()
hk.math.exp2 torch.exp2 t.exp2(), t.exp2_()
hk.math.log torch.log t.log()
hk.math.log2 torch.log2 t.log2()
hk.math.abs torch.abs t.abs()
hk.math.relu torch.relu t.relu()
hk.math.gelu F.gelu(x, approximate="tanh") t.gelu()
hk.math.dgelu the derivative of F.gelu(x, approximate="tanh") t.dgelu()
hk.math.sqrt torch.sqrt t.sqrt()
hk.math.rsqrt torch.rsqrt t.rsqrt(), t.rsqrt_()
hk.math.silu F.silu t.silu()
hk.math.add a + b t.add(), a + b
hk.math.sub a - b t.sub(), a - b
hk.math.mul a * b t.mul(), a * b
hk.math.div a / b t.div(), a / b
hk.math.maximum torch.maximum t.maximum()
hk.math.minimum torch.minimum t.minimum()
hk.math.fma a * b + c, one rounding hk.map(lambda x: ...)
hk.math.rcp 1 / x, within 1 ulp hk.map(lambda x: ...)
hk.math.popcount the set bits of the 32 hk.map(lambda x: ...)
hk.math.ctz trailing zero bits, 32 at 0 hk.map(lambda x: ...)
hk.math.ceil torch.ceil hk.map(lambda x: ...)
hk.math.floor torch.floor hk.map(lambda x: ...)

hk.math.exp

hk.math.exp(x, *, into=None)

Elementwise e**x. bf16, fp16 and fp32.; kittens::exp. Also x.exp(). into= writes an existing value instead of making one.

hk.math.exp2

hk.math.exp2(x, *, into=None)

Elementwise 2**x (the hardware's v_exp_f32; exp(x) = exp2(x * log2(e))). bf16, fp16 and fp32.; kittens::exp2. Also x.exp2(). into= writes an existing value instead of making one. Of an hk.map element, math.exp2: C's exp2f, which handles the arguments the bare instruction does not (denormals).

hk.math.log

hk.math.log(x, *, into=None)

Elementwise natural logarithm. bf16, fp16 and fp32.; kittens::log. Also x.log(). into= writes an existing value instead of making one.

hk.math.log2

hk.math.log2(x, *, into=None)

Elementwise base-2 logarithm. bf16, fp16 and fp32.; kittens::log2. Also x.log2(). into= writes an existing value instead of making one. Of an hk.map element, math.log2: C's log2f, which handles the arguments the bare instruction does not (denormals).

hk.math.abs

hk.math.abs(x, *, into=None)

Elementwise |x|. bf16, fp16 and fp32.; kittens::abs. Also x.abs(). into= writes an existing value instead of making one. Of an hk.map element, math.abs: C's fabsf.

hk.math.relu

hk.math.relu(x, *, into=None)

Elementwise max(x, 0). bf16, fp16 and fp32.; kittens::relu. Also x.relu(). into= writes an existing value instead of making one.

hk.math.gelu

hk.math.gelu(x, *, into=None)

Elementwise GELU, the tanh approximation: x * 0.5 * (1 + tanh(sqrt(2/pi) * (x + 0.044715 x**3))), which is F.gelu(x, approximate="tanh"), not the erf form torch defaults to (the two differ by up to 4.7e-4). fp32 tiles and vectors only: load bf16/fp16 data into an fp32 tile (the load converts) and .to(hk.bf16) the result.; kittens::gelu. Also x.gelu(). into= writes an existing value instead of making one.

hk.math.dgelu

hk.math.dgelu(x, *, into=None)

Elementwise derivative of gelu (the tanh form), for a backward pass. fp32 tiles and vectors only: load bf16/fp16 data into an fp32 tile (the load converts) and .to(hk.bf16) the result.; kittens::dgelu. Also x.dgelu(). into= writes an existing value instead of making one.

hk.math.sqrt

hk.math.sqrt(x, *, into=None)

Elementwise square root (one v_sqrt_f32). fp32 tiles and vectors only: load bf16/fp16 data into an fp32 tile (the load converts) and .to(hk.bf16) the result.; kittens::sqrt. Also x.sqrt(). into= writes an existing value instead of making one. Of an hk.map element, math.sqrt: C's sqrtf.

hk.math.rsqrt

hk.math.rsqrt(x, *, into=None)

Elementwise 1/sqrt(x) (one v_rsq_f32, ~1 ulp). fp32 tiles and vectors only: load bf16/fp16 data into an fp32 tile (the load converts) and .to(hk.bf16) the result.; kittens::rsqrt. Also x.rsqrt(). into= writes an existing value instead of making one.

hk.math.silu

hk.math.silu(x, *, into=None)

Elementwise SiLU, x * sigmoid(x) = x / (1 + exp(-x)), through the hardware exp2 and reciprocal: F.silu. fp32 tiles and vectors only: load bf16/fp16 data into an fp32 tile (the load converts) and .to(hk.bf16) the result.; kittens::silu. Also x.silu(). into= writes an existing value instead of making one.

hk.math.add

hk.math.add(a, b, *, into=None)

Elementwise a + b: what the operator does, with into= naming the value it writes (which may be an operand). b is a value of a's type, a Python number, a device Int or, for a tile a, a vector off a tile of the same shape (a col_vec applies along rows, a row_vec along columns).

hk.math.sub

hk.math.sub(a, b, *, into=None)

Elementwise a - b: what the operator does, with into= naming the value it writes (which may be an operand). b is a value of a's type, a Python number, a device Int or, for a tile a, a vector off a tile of the same shape (a col_vec applies along rows, a row_vec along columns).

hk.math.mul

hk.math.mul(a, b, *, into=None)

Elementwise a * b: what the operator does, with into= naming the value it writes (which may be an operand). b is a value of a's type, a Python number, a device Int or, for a tile a, a vector off a tile of the same shape (a col_vec applies along rows, a row_vec along columns).

hk.math.div

hk.math.div(a, b, *, into=None)

Elementwise a / b: what the operator does, with into= naming the value it writes (which may be an operand). b is a value of a's type, a Python number, a device Int or, for a tile a, a vector off a tile of the same shape (a col_vec applies along rows, a row_vec along columns).

hk.math.maximum

hk.math.maximum(a, b, *, into=None)

Elementwise max(a, b) (hk.max(a, b), a.maximum(b)): what the operator does, with into= naming the value it writes (which may be an operand). b is a value of a's type, a Python number, a device Int.

hk.math.minimum

hk.math.minimum(a, b, *, into=None)

Elementwise min(a, b) (hk.min(a, b), a.minimum(b)): what the operator does, with into= naming the value it writes (which may be an operand). b is a value of a's type, a Python number, a device Int.

hk.math.fma

hk.math.fma(*xs)

Elementwise a * b + c with one rounding (__builtin_fmaf, v_fma_f32): fp32. Of strips (one hk.map) or of hk.map elements; gfx950.

hk.math.rcp

hk.math.rcp(*xs)

Elementwise 1 / x by the hardware's reciprocal (v_rcp_f32, __builtin_amdgcn_rcpf: within 1 ulp, not the division): fp32. Of strips (one hk.map) or of hk.map elements; gfx950.

hk.math.popcount

hk.math.popcount(*xs)

Elementwise the number of set bits of the 32 (__builtin_popcount): i32. Of strips (one hk.map) or of hk.map elements; gfx950.

hk.math.ctz

hk.math.ctz(*xs)

Elementwise the number of trailing zero bits, 32 for 0 (__builtin_ctzg(x, 32)): i32. Of strips (one hk.map) or of hk.map elements; gfx950.

hk.math.ceil

hk.math.ceil(*xs)

Elementwise the least integer >= x, as a float (ceilf): fp32. Of strips (one hk.map) or of hk.map elements; gfx950.

hk.math.floor

hk.math.floor(*xs)

Elementwise the greatest integer <= x, as a float (floorf): fp32. Of strips (one hk.map) or of hk.map elements; gfx950.