Skip to content

Gemma-4-E4B (7.4B FP8)

Full source in examples/gemma4_e4b.rs. Run a CPU-friendly smoke test with JACOBI_BACKEND=cpu cargo run --release --example gemma4_e4b -- --small --fp8 --adamw.

What This Shows

  • Training the 42-layer, 262,144-vocabulary Gemma-4-E4B dense architecture (7.43B total parameters, 4.61B effective) in native FP8 (F8E4M3FN weights and dynamic F8E5M2 backward GEMMs)
  • Live next-token language modeling on Hugging Face PleIAs/SYNTH via hub::load_dataset (or --tokens-file for up to 1B+ pre-tokenized tokens)
  • Per-Layer Embeddings (PLE) with fused QKV projections, TN-native nn::quantized_linear, and nn::fused_geglu_linear
  • D=256 grouped-query attention (8 query heads, 2 KV heads) via nn::flash_attention with RoPE and QK RMSNorm
  • Constant-HBM 262k-vocab next-token cross-entropy using nn::chunked_cross_entropy_tn with logit softcapping (30.0)
  • Selective activation checkpointing (for_loop_selective), zero-HBM AOT compilation (compile_for), configurable Fp8UpdateMode (--fp8-mode master-f32|master-bf16|sr), and batched Rollout (--rollout) with SageAttention + sliding-window FP8 KV cache

Architecture and Core Layer

Each layer stores weights stacked along axis 0 ([L, out, in]) in TN-native layout and executes inside a single compiled for_loop_selective without unrolling checkpointed layers.

let qkv = nn::quantized_linear(&h_in, &wqkv);
let q = rope.apply(&nn::rms_norm(&q, &qn_g, cfg.rms_norm_eps), 0);
let k = rope.apply(&nn::rms_norm(&k, &kn_g, cfg.rms_norm_eps), 0);
let ctx = nn::flash_attention(&q, &k, &v, true, Some(scale))
    .permute(&[0, 2, 1, 3])
    .reshape(&[b * t, cfg.num_attention_heads * cfg.head_dim]);
let attn = nn::quantized_linear(&ctx, &wo).reshape(&[b, t, h]);
let x = x + &nn::rms_norm(&attn, &post_a_g, cfg.rms_norm_eps);

let gu = nn::quantized_linear(&f_in, &w_gu);
let mlp = nn::fused_geglu_linear(&gu, &wd).reshape(&[b, t, h]);
let x = &x + &nn::rms_norm(&mlp, &post_f_g, cfg.rms_norm_eps);

At the output head, chunked_cross_entropy_tn projects directly against the [vocab_size, hidden_size] embedding table without transposing or materializing full [B * T, 262144] logits.

let h_norm = self.forward_hidden(cfg, token_ids);
let (h_pred, targets) = (
    h_norm.slice_axis(1, 0, t - 1),
    token_ids.slice_axis(1, 1, t),
);
let cap = (cfg.final_logit_softcapping > 0.0).then_some(cfg.final_logit_softcapping);
chunked_cross_entropy_tn(&h_pred, &self.embed_w, &targets, lm_chunk, cap)

Running and Tuning

# Fast local CPU smoke test (4 layers, 4096 vocab, FP8 + full AdamW)
JACOBI_BACKEND=cpu cargo run --release --example gemma4_e4b -- --small --fp8 --adamw

# Full 7.43B FP8 training on 1x B200 180GB (4K context, PleIAs/SYNTH cross-entropy)
JACOBI_CKPT_LAYERS=10 JACOBI_FMHA_Q_TILE=1024 JACOBI_LM_CHUNK=8192 \
  cargo run --release --example gemma4_e4b -- --fp8 --skip-fwd --adamw --fp8-mode sr --batch 8 --seq 4096 --runs 15 --lr 3e-4 --warmup 3

# 1B-token training run from a pre-tokenized PleIAs/SYNTH shard
JACOBI_CKPT_LAYERS=10 JACOBI_FMHA_Q_TILE=1024 JACOBI_LM_CHUNK=8192 \
  cargo run --release --example gemma4_e4b -- --fp8 --skip-fwd --adamw --fp8-mode sr --batch 8 --seq 4096 --tokens-file synth_1b.bin --max-tokens 1000000000

# Batched RL Rollout (32 4K prompts x 16 GRPO rollouts = 512 x 1,024 tokens, SageAttention + window=512 + FP8 KV)
cargo run --release --example gemma4_e4b -- --fp8 --skip-fwd --rollout --batch 32 --group 16 --prompt-len 4096 --rollout-len 1024 --window 512 --runs 3

1x B200 180GB Benchmark Results (4K Context, FP8, PleIAs/SYNTH)

Measured on random-weight architecture replicas (no HuggingFace weight-parity check).

Mode Batch Checkpointed Layers Peak HBM Throughput 15-Step CE Loss Cost / 1T
Forward (4k) 16 N/A 21.59 GB 154,127.0 tok/s N/A N/A
Train (Full AdamW, SR, 4k) 8 10 of 42 155.07 GB 35,020.9 tok/s 12.83 -> 8.85 $39.7k
Train (Full AdamW, SR, 4k) 16 42 of 42 84.31 GB 33,666.2 tok/s 12.84 -> 9.01 $41.3k
Train (rank-32 EfficientAdamW, SR, 4k) 8 0 of 42 154.46 GB 32,915.7 tok/s 12.83 -> 10.48 $42.2k
Rollout (Sage + SW-512, 4k->1k, 32x16) 512 N/A 20.17 GB 30,998.7 tok/s N/A N/A
Rollout (Sage + SW-512, 4k->1k, 64x1) 64 N/A 31.88 GB 8,111.7 tok/s N/A N/A