Komorebi

Move the data, move the thread, or move the code.
A programming model for a network of independently programmable processors, in which a
thread reads what it needs or goes to where it is. It comes from the Tensix
architecture of Tenstorrent's Wormhole and Blackhole chips: a grid of cores, each with
its own memory and processors, on a network where any core reaches any other. Today it
runs on Blackhole, on its Tensix grid and its X280 cores. On main it runs on Wormhole
too: one chip, and the chips of a Galaxy as islands
(what Wormhole supports).
Runs on Tenstorrent Blackhole P150A cards: one, or two joined by their Ethernet links.
0.5.3 is on PyPI (as komorebi-tt), with the packer's formats at a handler's entry
named at compile time, and the Dst clear at a handler's entry that builds for Wormhole
(release notes); 0.5.2 brought the make that no longer
refuses a class's buffers the load takes, a failed load that gives back the L1 it took,
and two fields whose header names meet refused at the make
(0.5.2); 0.5.1 brought handlers that share their stream and
table buffers when a class's do not fit, and messages across islands that compile again
(0.5.1); 0.5.0 brought a handler that is a
hand-written kernel (km.kernel), the chips of a Wormhole Galaxy as islands, and
collectives that refuse after a give-up until the host clears them
(0.5.0).
| Program |
On the card |
Beside it, on the same card |
| LLM decode, Qwen3-1.7B in bfloat16, whole tokens |
105.5 tokens a second |
the official stack in bfloat16: 88.3 |
| The same with bfp8 and bfp4 weights, as the official stack's fastest setting |
163.4 tokens a second |
the official stack: 103.0 |
| Matrix product, 1024 cubed |
43.2 µs |
TT-NN: 92.4 µs |
| Matrix product, 8192 cubed |
4.48 ms |
TT-NN: 7.77 ms |
| SwiGLU, 256 x 256 x 512 |
32.2 µs |
TT-NN: 39.4 µs |
| Attention, 2 x 8 x 1024 x 128 |
147.6 µs |
TT-NN: 429.2 µs |
| Linear layer, 32 x 5120 x 17408, two cards |
223.1 µs |
one card: 409.4 µs |
| Rasterizer, 10^6 triangles, 1024 x 1024 |
6.20 ms a frame |
CUDA on a V100: 4.42 ms |
| Ray tracer, Sponza, 1024 x 1024 |
297.8 ms a frame |
CUDA on a V100: 102.0 ms |
The decode rows are whole tokens: the embedding, the 28 layers, the head and the choice
of the token all run on the card, in a loop on one of its X280 cores. The official
stack is tt-metal's tt_transformers demo, and the kernel rows are 32 calls beside
TT-NN, each side at its fastest setting that passes the same check. The first token of
an 88-token prompt comes after 69 ms, against the official stack's 27 ms.
More under Results.
Install
Linux x86-64, Python 3.13, a Blackhole P150A.
pip install "komorebi-tt[runtime]" # the framework and the pinned tt-metal build
kmr setup # prepares the card, once
kmr doctor
The package is imported as komorebi; the command is kmr. No tt-metal tree, ttnn
or torch is needed (the two wheels).
From a clone, for what is on main after 0.5.3 and for Wormhole (it needs a tt-metal source tree at
the version in packaging/tt-metal.pin, built with its X280
runtime, and TT_METAL_HOME naming it; install):
git clone <this repository> komorebi && cd komorebi
uv venv -p 3.13 && source .venv/bin/activate && uv pip install -e ".[dev]"
kmr doctor
Quick start
A program's kernels are loaded once and stay on the cores. After that a kernel is a
call: the host submits commands and the card runs them. Three handlers of one class
(scale, add, sum; this block is
quick.py of 29_chain):
import numpy as np
import komorebi as km
from steps_class import Steps # examples/29_chain/as_class: the kernels
with km.Session() as session:
x = session.tensor(np.ones((16, 1024), np.float32), "float32") # 16 tiles in DRAM
y, z, s = (
session.tensor(np.zeros((16, 1024), np.float32), "float32") for _ in "yzs"
)
t = session.tensor(np.zeros((1, 1024), np.float32), "float32")
run = session.load(Steps(a=x, b=x, out=y, s=s, t=t)) # compiled and loaded once
cores = (0, 8) # a command goes to a core group; each core takes its share
def scale(a, out, factor): # a kernel call, as a function
return run.submit(Steps.scale, cores, args=(a, out, factor, 2), exclusive=True)
# Commands queue on the card: submit as many as you like, wait once.
run.wait([scale(x, y, 2.0), scale(y, z, 3.0), scale(z, s, 0.5)])
# A fixed sequence as ONE command: every core goes from each kernel to the next by
# itself, with no host between them. Stated once, submitted as often as needed.
layer = run.chain(
[
Steps.scale(x, y, 2.0, 2),
Steps.scale(y, z, 3.0, 2),
Steps.add(z, km.past(y), s, 2),
Steps.sum(s, t, 16),
],
cores=cores,
)
for _ in range(100):
run.wait(run.submit(layer))
run.stop() # a tensor is read back once the run is stopped
print(t.read()[0, :4]) # [128. 128. 128. 128.]
The card can also decide what runs next by itself: a C thread on an X280 cluster
submits the same commands (below), and the dispatcher runs streams
and graphs of them (streams, graphs).
Every example runs on its own, checks its output against numpy and prints one JSON
line with ok (examples); each program's page lists its
commands (projects):
cd examples/02_merge_tree
python merge.py --jobs 20 --count 8 --form both
cd ../..
python -m projects.raytrace.frame --scene tiny --reps 16
Writing a kernel
A kernel is loaded once on its core and stays there; every command the core receives
runs one of its handlers. A command goes to a core group, and each core takes its
share of the work.
As a Python class
In 0.3.0, experimental. A method marked as a handler is compiled, when
the class is made, into the C++ kernels of the next section: its streams become the
data-movement code and the buffers, its parameters the command's words. The
arithmetic is tile functions, which the tile compiler turns into the matrix and vector
units' instructions.
A SwiGLU layer, y = (silu(x W1) * (x W3)) W2: two handlers and the chain that runs
them as one command. The class is 31_swiglu's, which
runs it on a card against float64.
import komorebi as km
@km.tile_function
def gate(h, g): # the vector unit
return km.silu(h) * g
class SwiGLU(km.Program):
"""x is [32, D], W1 and W3 are [D, H], W2 is [H, D]; every size in tiles of 32."""
D = km.constant(32) # x's and y's width: 1,024
H = km.constant(96) # the hidden width: 3,072
WIDTH = km.constant(8) # the columns one core makes
CHUNK = km.constant(2) # K tiles a block
W_BLOCK = CHUNK * WIDTH
h: km.L1[km.fp32, 1, WIDTH, 32, 32] # x W1, this core's columns: a field in L1
g: km.L1[km.fp32, 1, WIDTH, 32, 32] # x W3
out: km.L1[km.fp32, 1, WIDTH, 32, 32] # hidden W2
@km.tile_function(fidelity="HiFi2")
def project(x, w): # the matrix unit
return x @ w
@km.handler
def up(self, x: km.Tensor, w1: km.Tensor, w3: km.Tensor, hidden: km.Tensor):
first = self.rank * self.WIDTH # this core's columns of the hidden
for xb, w1b, w3b in zip(
km.Stream(x.tiles[0:1, 0 : self.D], block=self.CHUNK),
km.Stream(
w1.tiles[0 : self.D, first : first + self.WIDTH], block=self.W_BLOCK
),
km.Stream(
w3.tiles[0 : self.D, first : first + self.WIDTH], block=self.W_BLOCK
),
):
self.h += self.project(xb, w1b)
self.g += self.project(xb, w3b)
hidden.tiles[0:1, first : first + self.WIDTH].write(gate(self.h, self.g))
@km.handler
def down(self, hidden: km.Tensor, w2: km.Tensor, y: km.Tensor):
first = self.rank * self.WIDTH # this core's columns of y
for hb, wb in zip(
km.Stream(hidden.tiles[0:1, 0 : self.H], block=self.CHUNK),
km.Stream(
w2.tiles[0 : self.H, first : first + self.WIDTH], block=self.W_BLOCK
),
):
self.out += self.project(hb, wb)
y.tiles[0:1, first : first + self.WIDTH].write(self.out)
@km.chain
def forward(self, x, w1, w3, w2, hidden, y):
self.up(x, w1, w3, hidden, cores=(0, 12)) # 96 columns of the hidden, 8 a core
self.down(hidden, w2, y, cores=(0, 4)) # 32 columns of y
program = SwiGLU(x=x, w1=w1, w3=w3, w2=w2, hidden=hidden, y=y) # compiled here
run = session.load(program)
layer = run.chain(program.forward(x, w1, w3, w2, hidden, y)) # stated once
run.wait(run.submit(layer)) # the whole layer is one command
What a handler's lines may be is in classes, what a tile
function may contain in tile functions, a chain in
the chain. More classes: a group of cores each taking a
part of K and adding up a tree (dense_class),
attention with a broadcast of K and V down a column and inline C++ for the arithmetic
(attention_class), the same class on
two cards (01_mapcollect). Nine of the
examples have a class beside the hand program (as_class/ in each).
As C++ kernels
A program by hand is a data-movement kernel and a compute kernel. This one is the gate
of a SwiGLU layer, out = silu(a) * b, over two tensors in DRAM. The core's two
data-movement CPUs, the movers, bring its share of a and b from DRAM into L1 and
take out back, sharing every stream by a pattern (AAAB: mover A three of every
four blocks, mover B the fourth). The compute kernel does the math in the vector unit,
a block of tiles at a time. Reduced from 22_gate:
// ---- kernels/movement.cpp: one file, built for both data-movement CPUs of a core ----
void kernel_main() {
constexpr auto tensors = TensorAccessorArgs<0>(); // a, b and out share one layout
km::Movement movement; // stays loaded; runs once per command
movement.run([&](const km_command_t& command) {
const uint32_t tiles = command.a3; // tiles a core
const uint32_t first = (movement.me() - km::first(command)) * tiles;
const km::Pattern pattern = km::Pattern::of(command.a4);
const auto stream = [&](uint32_t direction, uint32_t address, km::BufferPair pair) {
const TensorAccessor tensor(tensors, address);
return km::Stream(direction, tensor, first, tiles, kBlock, 1, pair, pattern);
};
return movement.move(
stream(km::kMoveRead, command.a0, kA),
stream(km::kMoveRead, command.a1, kB),
stream(km::kMoveWrite, command.a2, kOut)
);
});
}
// ---- kernels/compute.cpp: out = silu(a) * b in the vector unit, a block at a time ----
void kernel_main() {
km::Compute compute;
compute_kernel_hw_startup(kAMoverA, kOutMoverA);
compute.run([&](const km_command_t& command) {
const km::Pattern pattern = km::Pattern::of(command.a4);
for (uint32_t block = 0; block < command.a3 / kBlock; block++) {
// each block from the half of the buffer pair its mover filled
const uint32_t a = km::block_half(kA, pattern, block);
const uint32_t b = km::block_half(kB, pattern, block);
const uint32_t out = km::block_half(kOut, pattern, block);
compute.wait_front(a, kBlock);
compute.wait_front(b, kBlock);
tile_regs_acquire();
copy_init(a);
for (uint32_t tile = 0; tile < kBlock; tile++) copy_tile(a, tile, tile);
silu_tile_init();
for (uint32_t tile = 0; tile < kBlock; tile++) silu_tile(tile);
mul_reuse_dest_init<kDestToSourceA>(b);
for (uint32_t tile = 0; tile < kBlock; tile++) {
mul_reuse_dest_tiles<kDestToSourceA>(b, tile, tile);
}
tile_regs_commit();
tile_regs_wait();
compute.reserve_back(out, kBlock);
for (uint32_t tile = 0; tile < kBlock; tile++) pack_tile(tile, out);
tile_regs_release();
cb_push_back(out, kBlock);
cb_pop_front(a, kBlock);
cb_pop_front(b, kBlock);
}
});
}
The host declares the program, loads it and submits the command to a core group:
program = km.Program(
"gate",
kernels="kernels/", # movement.cpp for both movers, compute.cpp
buffers=[
km.BufferPair(0, 1, "bfloat16", 2048, pages=32), # a: mover A's, mover B's
km.BufferPair(2, 3, "bfloat16", 2048, pages=32), # b
km.BufferPair(16, 17, "bfloat16", 2048, pages=32), # out
],
compile_args={"movement": a.accessor_args()}, # how the kernel finds a, b and out
handlers="kernels/gate.h", # kHandlerGate = 1
)
run = session.load(program)
pattern = km.Pattern("AAAB")
words = [a.address, b.address, out.address, tiles, pattern.word]
run.wait(run.submit("gate", (0, 120), words))
An X280 thread
The part of a program that decides is C on an X280 work cluster. A thread that splits
the grid between two programs every round, after one command from the host (reduced
from 19_thread_programs):
const uint32_t spin_cores = split(cores, spin_units, spin_rate, tiles, scale_rate);
km_command_t spin, scale;
kmx_command(&spin, kProgramSpin, kProgramSpinHandlerSpin, 0, spin_cores);
kmx_command(&scale, kProgramScale, kProgramScaleHandlerScale, spin_cores, cores - spin_cores);
kmx_submit(&spin, &spin_handle);
kmx_submit(&scale, &scale_handle);
kmx_wait(spin_handle, deadline, &spin_event);
kmx_wait(scale_handle, deadline, &scale_event);
Code that is not in the kernels is an overlay, loaded
into a slot of a core's L1 while the run lives. Every example is a page under
examples, with what it measured.
The programming model
Komorebi explores how to program a network of independently programmable processors,
where computation, communication, code placement and execution control are all the
program's own concern. It sees a program as threads, and a thread meets its data in one
of two ways:
- The data comes to the thread. A processor reads what it needs from shared memory,
or receives it from other processors.
- The thread goes to the data. A message carries a thread's state to the processor
that holds what it needs next, and that processor is chosen while the program runs,
from the data.
A thread needs its code where it arrives: placed there in advance, or loaded when it is
needed.
This model was not brought to the card from somewhere else. It is the Tensix
architecture read as a programming model: a Tensix core has its own memory and its own
RISC-V processors, and the NoC lets any core reach any other core's memory. Wormhole and
Blackhole are both built this way, so the model belongs to both, and to other grids of
this kind and to several cards joined together. Two Blackhole cards joined together run
today.