GPT-OSS 120B
Hugging Face checkpoints
Download the configuration, tokenizer or processor assets, and all weight shards with their index files. Some publishers require accepting their terms on Hugging Face.
Use the names in Model(...) to bind the checkpoints when you compile the RDL.
from redwood.rdl import *
W = Model("checkpoint")
processing = {"pre": "text", "post": "text", "assets": {"tokenizer": W.root}}
# openai/gpt-oss-120b
LAYERS = 36
HEADS, KV_HEADS, HEAD_DIM = 64, 8, 64
MAX_PROMPT, MAX_NEW = 4096, 256
CAPACITY = MAX_PROMPT + MAX_NEW
VOCAB, EOS_IDS = 201088, (200002,)
def norm(x: Tensor, name: str) -> Tensor:
return RMSNorm(x, weight=W[f"{name}.weight"], eps=1e-5, dtype="bf16")
def attention(x: Tensor, position: Tensor, name: str, key: Cache, value: Cache, window: int | None) -> Tensor:
q = SplitHeads(Cast(Linear(x, W[f"{name}.q_proj.weight"], bias=W[f"{name}.q_proj.bias"]), "bf16"), heads=HEADS)
k = SplitHeads(Cast(Linear(x, W[f"{name}.k_proj.weight"], bias=W[f"{name}.k_proj.bias"]), "bf16"), heads=KV_HEADS)
v = SplitHeads(Cast(Linear(x, W[f"{name}.v_proj.weight"], bias=W[f"{name}.v_proj.bias"]), "bf16"), heads=KV_HEADS)
q = YaRN(q, position, dim=HEAD_DIM, theta=150_000, factor=32, original_context=4096, truncate=False, dtype="bf16")
k = YaRN(k, position, dim=HEAD_DIM, theta=150_000, factor=32, original_context=4096, truncate=False, dtype="bf16")
key.append(k)
value.append(v)
context = Attention(q, key.read(), value.read(), window=window, sinks=W[f"{name}.sinks"], dtype="bf16")
return Cast(Linear(MergeHeads(context), W[f"{name}.o_proj.weight"], bias=W[f"{name}.o_proj.bias"]), "bf16")
def mlp(x: Tensor, name: str) -> Tensor:
return Cast(
GptOssMoE(
x,
router=W[f"{name}.router.weight"],
router_bias=W[f"{name}.router.bias"],
gate_up=W[f"{name}.experts.gate_up_proj"],
down=W[f"{name}.experts.down_proj"],
gate_up_bias=W[f"{name}.experts.gate_up_proj_bias"],
down_bias=W[f"{name}.experts.down_proj_bias"],
top_k=4,
),
"bf16",
)
with Graph() as graph:
tokens = Input(name="tokens", shape=(None,), dtype="int32", maximum_shape=(MAX_PROMPT,))
reset = Input(name="reset", shape=(), dtype="bool")
history = Cache(name="history", shape=(CAPACITY,), dtype="int32", mode="append", axis=0)
key_shape = (CAPACITY, KV_HEADS, HEAD_DIM)
keys = [Cache(name=f"{i}.key", shape=key_shape, dtype="bf16", mode="append", axis=0) for i in range(LAYERS)]
values = [Cache(name=f"{i}.value", shape=key_shape, dtype="bf16", mode="append", axis=0) for i in range(LAYERS)]
start = ReusePrefix(tokens, history, caches=(*keys, *values), reset=reset)
def decode(state: tuple[Tensor, ...]) -> tuple[Tensor, ...]:
index, previous, output, _ = state
prefill = Compare(index, 0, predicate="eq")
position = Select(prefill, start, Add(Dim(tokens, 0), Sub(index, 1)))
token_ids = If(
prefill,
then_branch=lambda: (Slice(tokens, axis=0, start=start, rank=1),),
else_branch=lambda: (Reshape(previous, (1,)),),
)[0]
token_positions = Add(Iota(Dim(token_ids, 0)), position)
history.append(token_ids)
x = Cast(Embedding(token_ids, W["model.embed_tokens.weight"]), "bf16")
for i in range(LAYERS):
name = f"model.layers.{i}"
update = attention(
norm(x, f"{name}.input_layernorm"),
token_positions,
f"{name}.self_attn",
keys[i],
values[i],
128 if i % 2 == 0 else None,
)
x = Add(x, update)
x = Add(x, mlp(norm(x, f"{name}.post_attention_layernorm"), f"{name}.mlp"))
x = Slice(x, axis=0, start=-1, rank=2)
logits = Reshape(Linear(Cast(norm(x, "model.norm"), "fp32"), Cast(Constant(W["lm_head.weight"]), "fp32")), (VOCAB,))
token = Greedy(logits)
output = Scatter(output, indices=Reshape(index, (1,)), values=token)
done = Constant(False)
for eos in EOS_IDS:
done = Select(Compare(token, eos, predicate="eq"), True, done)
return Add(index, 1), token, output, done
output = Broadcast(Constant(0, dtype="int32"), (MAX_NEW,))
result = While(
(Constant(0), Constant(0, dtype="int32"), output, Constant(False)),
condition=lambda s: Select(
Compare(s[0], MAX_NEW, predicate="lt"), Compare(s[3], False, predicate="eq"), False
),
body=decode,
)
Output(Slice(result[2], axis=0, stop=result[0], rank=1), name="tokens")