xnn.common.cpu_kernels#

Triton kernels on the CPU.

launch() runs a @triton.jit kernel on CPU tensors as kernel[grid](*args, **meta) runs it on a GPU. The kernel’s source is translated into C++ once per specialization (the constexpr values and the argument dtypes, as Triton specializes), compiled with the system C++ compiler and cached on disk. Every lane of every program becomes one iteration of a loop that OpenMP threads share; the helpers a kernel calls are inlined.

Lane semantics:

  • a masked load reads only where its mask holds;

  • an atomic add goes to a buffer of the calling thread and the buffers are summed into the tensor after all lanes ran (each thread sums a share of the touched blocks, so a run needs no atomics and its sums do not depend on the scheduling);

  • a block reduction tl.sum is allowed only as the value of an atomic add, where each lane adds its own term;

  • a value that is the same in every lane of a program (a program id, a scalar argument, a load at such an address) stays uniform, and an atomic add or a store of a uniform value at a uniform address runs once per program.

Literals follow Triton’s typing: a Python float with a float32 value is float32, with an integer value float32, two literals stay a literal. A kernel outside these rules (another block reduction, a second grid axis) raises Unsupported; its helpers can be given lane-wise twins with override().

Functions

available()

Whether CPU kernels can be compiled here (Linux, a C++ compiler on the path, Triton importable); checked once per process (every launch asks).

launch(kernel, grid, *args, **meta)

Run kernel[grid](*args, **meta) on CPU tensors (see prepare()).

override(fn, source)

Translate the jit helper fn from source (a function of the same name and arguments in Triton syntax, resolved in fn's module) instead of its own source: the lane-wise twin of a helper with a block reduction.

prepare(kernel, grid, *args[, balance])

The launch kernel[grid](*args, **meta) on CPU tensors, compiled and packed, to be called once or many times (grid an int or a 1-tuple; num_warps and num_stages are ignored).

translate(kernel, args, meta[, balanced])

The C++ source of a kernel for these arguments and constexpr values, and the names of its tensor arguments that receive atomic adds; balanced the programs split by a work prefix passed after the tensor arguments (prepare()).

Classes

Deferred(launches[, after])

Prepared launches whose atomic adds go to per-thread buffers shared by the launches with the same target tensor (float64), in one parallel region with no barrier between them; each target is summed once after them, then the after launches (which may read the sums) run.

Launch(lib, tensors, fs, ints, n[, targets])

A kernel launch with its arguments packed once: calling it runs the kernel on the tensors' current contents (they must stay allocated and in place).

Sequence(launches[, independent])

Prepared launches run in order in one parallel region: no thread start-up per kernel.

V(code, ty, uni[, sumof, where0, conj])

A translated value: C++ code, a type (a scalar type or ("ptr", element type, base argument)), whether it is uniform across the lanes of a program, and the lane terms of a tl.sum or the condition and value of a tl.where(c, v, 0).

Exceptions

Unsupported

A construct the lane translation does not cover.