moe

A mixture-of-experts layer for Tenstorrent Tensix devices that routes on the device and reads only the chosen experts' weights from DRAM, from the moe package of tensix-kernels.

Requirements

  • Linux x86_64, Python 3.10 or 3.12, and torch.
  • A Tenstorrent Blackhole card, or the Blackhole library (libttsim_bh.so) of ttsim v1.16.0.
  • SFPI 7.89.0 in ttnn's runtime directory, installed as RUNNING.md describes.
  • H, I and E (the router's logit columns) multiples of 32; k at most 16.

Usage

pip install kernels ttnn==0.81.1
from kernels import get_kernel

moe = get_kernel("Xylolabs/moe", version=1, backend="cpu")
device = moe.open_device(0)
experts = moe.pack(device, gate_up, down)      # gate_up [E, H, 2I] as [up | gate], down [E, I, H]
y = moe.moe(x, experts, router_logits, k=4)    # x [R, H], router_logits [R, E] -> y [R, H] bf16

API

Function Inputs Output Purpose
open_device(device_id=0) int ttnn device Open a Tensix device or ttsim
close_device(device) ttnn device None Close a device from open_device
pack(device, gate_up, down, *, gu_bias=None) gate_up: float [E, H, 2I] as [up | gate]; down: float [E, I, H]; gu_bias: float [E, 2I] experts (dict) Pack one layer's experts on the device as BFP4_b
moe(x, experts, logits, k, *, act="silu", fidelity="hifi2", kind="softmax", bias=None, scale=1.0, norm=True, groups=None) x: float [R, H], or ttnn bf16 TILE [1, 1, R, H]; logits: float [R, E], or ttnn float32 TILE [1, 1, R, E]; act "silu", "gelu_tanh" or "swiglu_oai" y: bf16 [R, H] Route each row and run its top k experts
route(logits, k, *, device=None, kind="softmax", bias=None, scale=1.0, norm=True, groups=None) logits: float [R, E]; kind "softmax" or "sigmoid"; bias: float [E]; groups: (n_group, topk_group) ids int64 [R, k], weights bf16 [R, k] The router's top k on the device
reference(x, gate_up, down, ids, weights, *, act="silu", gu_bias=None) x: float [R, H]; gate_up, down as pack; ids, weights [R, k] float64 [R, H] The float64 reference of moe for given routing
route_reference(logits, k, *, kind="softmax", bias=None, scale=1.0, norm=True, groups=None) as route ids int64 [R, k], weights float64 [R, k] The float64 reference of route
bfp4_round(w) w: float [..., K, N], K and N multiples of 32 float32 [..., K, N] w's values on the BFP4_b grid
deallocate(experts) experts (dict) None Free the packed experts' device tensors

Supported dtypes

  • Experts: BFP4_b.
  • x and y: bf16; accumulation in fp32.
  • Router logits: float32.

License

Apache License 2.0, with tt-metal's attribution in NOTICE.txt.

Downloads last month
-
kernels
tenstorrent
tensix
apache-2.0
Kernel Builder
95535fd