hawk.math#
hawk.math — the free-function namespace a kernel body writes math through.
A namespace, not a layer: every name here either is one of
hawk.trace’s free functions, re-exported unchanged, or a
two-line composition of them, so hawk.math and hawk.ir.ops
declare the same vocabulary by construction
(tests/test_math_namespace.py traces and emits every name in
__all__).
It exists because exp, log, tanh, vec and max want
spelling without importing forty names one at a time, and because
three spellings have no free function elsewhere in HAWK yet:
sample_index()— the lane’s own index as a value;split_index()— the(major, minor)decomposition of a flattened two-dimensional launch domain;take()— the free-function spelling of a plane’sat()read.
The names match the previous code generator’s vmath where it has
one, so a re-authored body moves across with its diffs readable;
semantics do not move (hawk.math.max is HAWK’s maximum).
Importing this module imports the tracer, so import hawk never
reaches it: it is asked for by name, from hawk import math as m.
Element-wise math, on host and device alike, with numpy’s names:
exp / log:
expexp2expm1loglog2log10log1ppower(pow,**)sqrtrsqrtcbrthypottrigonometric:
sincostanasinacosatanatan2hyperbolic:
sinhcoshtanhasinhacoshatanhrounding:
floorceiltruncroundrintmisc:
absolute(abs)signcopysignfmodremainderfdimfmaclipminimum(min)maximum(max)isnanisinfisfinitespecial:
erferfc
Semantics differing from numpy, by design: round is C’s (halves away
from zero; rint is numpy’s half-to-even np.round), and
minimum/maximum ignore a NaN operand (np.fmin/np.fmax).
remainder is numpy’s floor-mod (sign of the divisor), fmod C’s
(sign of the dividend). The class tests return bool.
Shapes: every element-wise function takes vector and matrix operands as
the arithmetic operators do — equal shapes, or one operand rank-0
broadcast — and refuses any other combination in the operators’ words.
exp log sqrt rsqrt, the trigonometric functions but
atan2, tanh, abs, power, minimum and maximum lower to
one aether expression at any rank; the rest, with the comparisons and
land/lor/lnot, are mapped entry by entry at trace time and
re-assembled — aether spells them at rank 0 only — so a class test
or a comparison on a matrix is a bool matrix, the mask
where()/select() takes per entry (a rank-0 branch broadcasts).
Derivatives: the rounding family, sign and the class tests carry a
zero derivative. abs takes +1 at zero (as JAX); copysign is
|a| times b’s sign, constant in b; fmod/remainder
differentiate as a - trunc(a/b)*b / a - floor(a/b)*b; fdim is
zero at a == b; hypot is zero at the origin; clip passes the
gradient to x on the closed interval [lo, hi] (torch’s clamp;
JAX halves it at a tie), to hi above it or whenever lo > hi, and
to lo below it.
Not yet: lgamma/tgamma/digamma, Bessel functions, and
integer-specific operations beyond the existing ones.
Module Attributes
Functions
|
|
|
|
|
Elementwise |
|
Inverse hyperbolic cosine, |
|
The index (rank-0 |
|
Elementwise |
|
Elementwise |
|
Elementwise |
|
Inverse hyperbolic sine ( |
|
Elementwise |
|
The kinds aether spells at rank 0 only, mapped entry by entry on a vector or matrix like the functions below. |
|
Inverse hyperbolic tangent, |
|
Real cube root, odd in |
|
Smallest integer >= |
|
|
|
|
|
Elementwise |
|
Hyperbolic cosine ( |
|
|
|
|
|
|
|
The error function ( |
|
|
|
Elementwise |
|
|
|
|
|
|
|
Piecewise-constant (lever 2): the largest integer <= |
|
|
|
C remainder of |
|
|
|
|
|
|
|
|
|
Logical and of two bool values, component-wise. |
|
Logical not of a bool value, component-wise. |
|
Elementwise |
|
Base-10 logarithm ( |
|
|
|
Base-2 logarithm ( |
|
Logical or of two bool values, component-wise. |
|
|
|
|
|
|
|
|
The readable sample count: the TRUE |
|
|
Elementwise |
|
The outer product |
|
|
|
|
|
Elementwise |
|
|
|
Elementwise |
|
|
A Bernoulli draw as a real |
|
An exponential draw with |
|
|
|
A correlated normal vector |
|
A normal draw |
|
A Poisson(1)-distributed count per lane, as a real value: Knuth's product method, multiplying uniform draws until the running product falls below |
|
A uniform draw on |
|
An integer draw uniform over the closed range |
|
|
Floor-mod |
|
Nearest integer, halves to even ( |
|
Nearest integer, halves AWAY from zero (C |
|
Elementwise |
This lane's GLOBAL sample index, as a value (the |
|
|
|
|
-1, 0 or 1 by the sign of |
|
Elementwise |
|
Hyperbolic sine ( |
|
Split this lane's index into its |
|
Elementwise |
|
Read |
|
Elementwise |
|
Elementwise |
|
Elementwise |
|
Integer part, toward zero ( |
|
Build a rank-1 value from scalar components: both |
|
Elementwise |
|
Branchless |