HeadlinesBriefing favicon HeadlinesBriefing.com

Hijax Types for Custom JAX Data Structures

Hacker News •
×

JAX’s tracing machinery works on arrays, but sometimes you need a distinct type that carries invariants and appears as a single value in jaxprs. Hijax types let you define such types so that users can only produce and consume them through specific primitives, preserving coupling and enabling custom behavior for autodiff, batching, and sharding.

To define a hijax type you subclass Hi Type and implement `lo_ty`, `lower_val`, and `raise_val` to map the type to low‑level array representations; you then register the value class with `register_hitype`. The documentation walks through a QArray example that pairs int8 values with a float32 scale, using a frozen dataclass for hashability and equality.

Primitives like quantize and dequantize are written as `VJPHi Primitive` subclasses, declaring input and output avals that mention the new type. Autodiff relies on `to_tangent_aval` on the type and VJP/JVP rules; vmap requires `dec_rank`/`inc_rank` and a custom Mapping Spec; sharding data can be stored in the type and consumed in `lo_ty` for explicit sharding mode.

This approach keeps coupled data together, provides meaningful tangents (e.g., float32 for quantized arrays), and lets the type participate in JAX’s explicit sharding. It yields safer, more expressive APIs for domains such as low‑precision numerics while remaining fully compatible with JAX’s transformation stack.