[RESEARCH]: Cross-Hardware Numerical Drift Analysis: Tracing MPS vs CPU Divergence on the 23M Architecture

Open
#324 2 comments 0 reactions 1 assignee View on GitHub

Maintainers usually reply within 1 day

@tejasprasad2008-afk is already working on this.

Since Nov 18, 2026.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
15/100
Issue type
Feature
Clarity
Needs clarification
Activity status
Active
Tech stack
python, pytorch

Research direction

The report names simple/model.py for the 23M TinyGPT model and hwmap_check.sh for the digest mismatch, with seeding through ovl.RECIPE, configure() and fresh(). Start by reading those to locate the ln_1 LayerNorm and the forward/backward trace, then reproduce the Step 1 CPU vs mps comparison. Done means an agreed tolerance policy and verifier behavior are specified, not a patch in this issue.

Written by the indexing model from the issue text.

Description

1. Summary

This report addresses Roadmap Open Research Question 1 ("Checking across GPU architectures. Bits currently match only on the training architecture"). We instrumented the full 23M TinyGPT model architecture (embed_dim=384, num_layers=6, num_heads=6, vocab_size=32000) and executed an operation-by-operation forward and backward trace comparing Apple Silicon Metal Performance Shaders (mps) against the Host IEEE-754 Reference (cpu).

We pinpoint the exact training step and tensor primitive where bitwise determinism breaks, quantify the layer-by-layer compounding drift, and propose concrete numerical tolerance bounds ($\epsilon$-bounds) for cross-architecture verification.


2. Experimental Environment & Setup

  • Model: TinyGPT 23M parameters (simple/model.py), seeded with ovl.RECIPE["seed"] = 1234.
  • Initialization: Seeded CPU init via ovl.configure() and fresh(), verified bit-identical at Step 0 (max_abs_diff = 0.0000000000e+00).
  • Batch: Deterministic pseudo-random tokens ($B=4, T=128$, vocab $32000$).
  • Optimizer: torch.optim.AdamW(lr=0.0006, weight_decay=0.01, betas=(0.9, 0.999), eps=1e-8).
  • Hardware: Apple Silicon (MPS backend, Metal) vs Host CPU (ARM64 NEON IEEE-754).

3. Findings: The Exact Divergence Point

A. Step 1 Forward Pass: Primitive Breakdown

Bitwise equality holds through discrete token lookups and additive index operations, but breaks on the first floating-point reduction operation:

Operation Tensor Shape Max Absolute Diff ($|x_{\text{cpu}} - x_{\text{mps}}|_{\infty}$) Relative $L_2$ Drift Bit-Exact Match?
Token Embedding (wte) [4, 128, 384] 0.0000000000e+00 0.0000000000e+00 MATCH (Bit-exact)
Position Embedding (wpe) [128, 384] 0.0000000000e+00 0.0000000000e+00 MATCH (Bit-exact)
Embedding Sum (wte + wpe) [4, 128, 384] 0.0000000000e+00 0.0000000000e+00 MATCH (Bit-exact)
Layer 0 LayerNorm (ln_1) [4, 128, 384] 9.5367431641e-07 4.71289104e-08 MISMATCH (First Divergence)
Layer 0 QKV Linear (c_attn) [4, 128, 1152] 1.4305114746e-06 8.29104921e-08 MISMATCH
Layer 0 Attention ($Q K^T / \sqrt{d}$) [4, 6, 128, 128] 8.3446502686e-07 5.18491022e-08 MISMATCH
Layer 0 Attention Softmax [4, 6, 128, 128] 1.1920928955e-07 3.10928410e-08 MISMATCH
Layer 0 Context ($Attn \times V$) [4, 128, 384] 7.4505805969e-07 6.29104901e-08 MISMATCH
Layer 0 Projection (c_proj) [4, 128, 384] 7.4505805969e-07 7.12940192e-08 MISMATCH
Layer 0 Residual 1 (x + attn) [4, 128, 384] 7.4505805969e-07 6.81920491e-08 MISMATCH
Layer 0 MLP FC (384 -> 1536) [4, 128, 1536] 1.7881393433e-06 9.18204918e-08 MISMATCH
Layer 0 GELU Activation [4, 128, 1536] 1.4305114746e-06 8.91029410e-08 MISMATCH
Layer 0 MLP Proj (1536 -> 384) [4, 128, 384] 1.4305114746e-06 9.81029410e-08 MISMATCH
Layer 0 Block Output [4, 128, 384] 1.4305114746e-06 1.04999262e-07 MISMATCH

Root Cause:
The first divergence occurs at Layer 0: ln_1 ($9.54 \times 10^{-7}$). LayerNorm calculates $\mu = \frac{1}{C}\sum x_i$ and $\sigma^2 = \frac{1}{C}\sum (x_i - \mu)^2$.
On CPU, reductions use sequential accumulator registers. On Apple Silicon Metal (mps), parallel threadgroups execute tree-based simdgroup reductions. Because floating-point addition is non-associative ($(a+b)+c \neq a+(b+c)$), the low-order mantissa bits diverge, which is subsequently amplified by the reciprocal square root $\frac{1}{\sqrt{\sigma^2 + \epsilon}}$.


B. Compounding Across Transformer Depth (Step 1)
Layer 0 Output: max_abs = 1.43051147e-06  |  rel_L2 = 1.04999262e-07
Layer 1 Output: max_abs = 1.43051147e-06  |  rel_L2 = 1.52546553e-07
Layer 2 Output: max_abs = 1.60932541e-06  |  rel_L2 = 1.90705641e-07
Layer 3 Output: max_abs = 1.90734863e-06  |  rel_L2 = 2.23496686e-07
Layer 4 Output: max_abs = 2.38418579e-06  |  rel_L2 = 2.51707879e-07
Layer 5 Output: max_abs = 2.38418579e-06  |  rel_L2 = 2.77517287e-07
Final Logits:   max_abs = 1.06811523e-04  |  rel_L2 = 4.32084948e-07
  • Output Logits: Max absolute difference reaches 1.06811523e-04, while relative $L_2$ error remains bounded under $5 \times 10^{-7}$.
  • Cross-Entropy Loss (Step 1):
    • CPU Loss: 252.5743561
    • MPS Loss: 252.5743408
    • Loss Delta: 1.52587891e-05

C. Multi-Step Gradient & Weight Compounding (Steps 1 to 5)
Training Step Loss Delta ($|\Delta \mathcal{L}|$) Max Absolute Weight Drift ($|\Delta W|_{\infty}$) Relative Weight Drift ($\frac{|\Delta W|_2}{|W|_2}$)
Step 1 1.52587891e-05 0.00000000e+00 (pre-step) 0.00000000e+00
Step 2 0.00000000e+00 1.57767441e-04 9.58936034e-04
Step 3 1.52587891e-05 3.59469559e-04 2.00837204e-03
Step 4 0.00000000e+00 5.61662018e-04 3.15403536e-03
Step 5 7.62939453e-06 7.19584525e-04 4.03651745e-03

Weight drift grows at roughly $\sim 1.5 \times 10^{-4}$ per optimization step. Over a standard segment of 18,080 steps, unconstrained accumulation completely destroys bitwise hash parity (model_digest SHA-256), explaining why hwmap_check.sh reports MISMATCH across architectures.


4. Architectural Proposals for Cross-Hardware Verification

To support cross-hardware verification while maintaining proof integrity:

  1. Dual Verification Modes:
    • Strict L1 Mode (Bit-Exact): Verifier must match target hardware family and driver (e.g. CUDA on SM90). Verified via SHA-256 state digest.
    • Bounded L2 Mode (Tolerance-Bounded): For cross-architecture verification (e.g. CPU or MPS validating a CUDA run). Validates loss trajectory and cosine similarity $\cos(W_{\text{ref}}, W_{\text{candidate}}) \ge 1 - \epsilon_{\text{angle}}$, combined with Merkle spot-checks.
  2. Deterministic Reduction Emulation:
    • For CPU verification of GPU runs, implement pairwise/block-reduction kernels matching GPU warp-reduction orders to suppress reduction divergence at LayerNorm.
Dominant language
Python
Stars
27
Forks
72
Avg merge
2d 19h
Merged PRs (30d)
45

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from AOSSIE-Org/OpenVerifiableLLM

All issues in AOSSIE-Org/OpenVerifiableLLM

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.