// ═══════════════════════════════════════════════════════════════════ // JST FUSION PIPELINE — Jordan Spectral Transformer, one kernel // // SPE encode → N × jordan_step → measurement_head → reconstruct // ALL FUSED. No intermediate materialization. No host↔device bounce. // The density ρ never leaves the register file for d ≤ 64. // // Compile: // mlir-opt \ // --affine-loop-fusion \ ← merge two GEMMs per layer // --linalg-tile="tile-sizes=16,16,16" \ ← L1 cache fit // --vectorize \ ← SVE2 / AVX-512 // --gpu-kernel-outlining \ ← PTX path // --convert-linalg-to-loops \ // --convert-vector-to-scf \ // --convert-scf-to-llvm \ // --convert-func-to-llvm \ // jst_fusion_pipeline.mlir -o jst_llvm.mlir // // Then: // mlir-translate --mlir-to-llvmir jst_llvm.mlir | \ // llc -mtriple=aarch64-linux-gnu -mattr=+sve2,+aes,+sha3 -O3 -filetype=obj // // Audit Spec: 4b565498-9afc-4782-af4a-c6b11a5d0058 // ═══════════════════════════════════════════════════════════════════ module @jst_sovereign { // ── Index constants ───────────────────────────────────────────── arith.constant %c0 = 0 : index arith.constant %c1 = 1 : index // ── External Fortran ABI (linked from jst_arm64.o) ────────────── func.func private @sov_plasma_verify( memref>) -> i1 func.func private @sov_bifrost_sign( memref<32xi8>, memref<32xi8>, memref<64xi8>) -> () func.func private @sov_blake3_hash_matrix( memref>, memref<32xi8>) -> () func.func private @sov_zmexp_scaling_squaring( memref>) -> () func.func private @sov_fault(i64) -> () // ═══════════════════════════════════════════════════════════════ // KERNEL 1: spe_encode_fused // Signal → frame coefficients → softmax eigenvalues → density ρ₀ // Fused: frame inner products + softmax + density construction // = ONE affine nest after --affine-loop-fusion // ═══════════════════════════════════════════════════════════════ func.func @spe_encode_fused( %signal : memref>, // [d, d] %frame : memref>, // [r, d, d] %rho_out : memref>, // [d, d] %eigs : memref, // [r] %hash : memref<32xi8>, %sig : memref<64xi8>, %sk : memref<32xi8> ) { %r = memref.dim %frame, %c0 : memref> %d = memref.dim %signal, %c0 : memref> // ── Step 1: Frame inner products cᵢ = tr(ψᵢ† signal) ────────── // linalg.generic fuses with Step 3 via --affine-loop-fusion %coeffs = memref.alloc(%r) : memref> linalg.generic { indexing_maps = [ affine_map<(i,j,k) -> (i,j,k)>, // frame[i,j,k] affine_map<(i,j,k) -> (j,k)>, // signal[j,k] affine_map<(i,j,k) -> (i)> // coeffs[i] ], iterator_types = ["parallel", "reduction", "reduction"]} ins(%frame, %signal : memref>, memref>) outs(%coeffs : memref>) { ^bb0(%f : complex, %s : complex, %acc : complex): // tr(ψᵢ† signal) = Σ conj(frame[i,j,k]) * signal[j,k] %fc = complex.re %f : f64 %fi = complex.im %f : f64 %neg_fi = arith.negf %fi : f64 %fconj = complex.create %fc, %neg_fi : complex %prod = complex.mul %fconj, %s : complex %sum = complex.add %acc, %prod : complex linalg.yield %sum : complex } // ── Step 2: Softmax eigenvalues λᵢ = exp(Re cᵢ) / Σ exp(Re cⱼ) ─ %eigs_raw = memref.alloc(%r) : memref %max_val = memref.alloc() : memref memref.store %cst_neg_inf, %max_val[] : memref // Find max (for numerical stability) affine.for %i = 0 to %r { %c = memref.load %coeffs[%i] : memref> %re = complex.re %c : f64 %cur_max = memref.load %max_val[] : memref %new_max = arith.maxf %cur_max, %re : f64 memref.store %new_max, %max_val[] : memref } %sum_exp = memref.alloc() : memref memref.store %cst_zero_f, %sum_exp[] : memref affine.for %i = 0 to %r { %c = memref.load %coeffs[%i] : memref> %re = complex.re %c : f64 %mx = memref.load %max_val[] : memref %sub = arith.subf %re, %mx : f64 %e = math.exp %sub : f64 memref.store %e, %eigs_raw[%i] : memref %s = memref.load %sum_exp[] : memref %s2 = arith.addf %s, %e : f64 memref.store %s2, %sum_exp[] : memref } %s = memref.load %sum_exp[] : memref affine.for %i = 0 to %r { %e = memref.load %eigs_raw[%i] : memref %norm = arith.divf %e, %s : f64 memref.store %norm, %eigs[%i] : memref } // ── Step 3: Inverse spectral map ρ = Σᵢ λᵢ ψᵢ ───────────────── // FUSED with Step 1 by --affine-loop-fusion into ONE loop nest linalg.generic { indexing_maps = [ affine_map<(i,j,k) -> (i)>, // eigs[i] affine_map<(i,j,k) -> (i,j,k)>, // frame[i,j,k] affine_map<(i,j,k) -> (j,k)> // rho_out[j,k] ], iterator_types = ["reduction", "parallel", "parallel"]} ins(%eigs, %frame : memref, memref>) outs(%rho_out : memref>) { ^bb0(%l : f64, %f : complex, %acc : complex): %l_cx = complex.create %l, %cst_zero_f : complex %prod = complex.mul %l_cx, %f : complex %sum = complex.add %acc, %prod : complex linalg.yield %sum : complex } // ── Plasma gate ───────────────────────────────────────────────── %ok = func.call @sov_plasma_verify(%rho_out) : (memref>) -> i1 cf.assert %ok, "SPE PLASMA FAIL" // ── Bifrost attest ────────────────────────────────────────────── func.call @sov_blake3_hash_matrix(%rho_out, %hash) : (memref>, memref<32xi8>) -> () func.call @sov_bifrost_sign(%hash, %sk, %sig) : (memref<32xi8>, memref<32xi8>, memref<64xi8>) -> () memref.dealloc %coeffs : memref> memref.dealloc %eigs_raw : memref memref.dealloc %max_val : memref memref.dealloc %sum_exp : memref return } // ═══════════════════════════════════════════════════════════════ // KERNEL 2: jordan_fused // Single jordan layer: U = exp(-i dt H), then ρ' = φ⁻¹·UρU† + φ⁻²·ρ // // TWO linalg.matmul calls fused by --affine-loop-fusion: // tmp = U * ρ ← GEMM 1 // evolved = tmp * U† ← GEMM 2 // AFTER FUSION: single loop nest, tmp never materialized on GPU // ═══════════════════════════════════════════════════════════════ func.func @jordan_fused( %H : memref>, // [d, d] Hermitian %rho_in : memref>, // [d, d] density in %rho_out : memref>, // [d, d] density out %dt : f64, %hash : memref<32xi8>, %sig : memref<64xi8>, %sk : memref<32xi8> ) { %d = memref.dim %H, %c0 : memref> %U = memref.alloc(%d, %d) : memref> %tmp = memref.alloc(%d, %d) : memref> %Ut = memref.alloc(%d, %d) : memref> // U = exp(-i dt H) — calls Fortran scaling & squaring // First copy: U = -i * dt * H %neg_i_dt = complex.create %cst_zero_f, %dt : complex // i*dt linalg.generic { indexing_maps = [affine_map<(i,j) -> (i,j)>, affine_map<(i,j) -> (i,j)>], iterator_types = ["parallel","parallel"]} ins(%H : memref>) outs(%U : memref>) { ^bb0(%h : complex, %u : complex): %prod = complex.mul %neg_i_dt, %h : complex linalg.yield %prod : complex } func.call @sov_zmexp_scaling_squaring(%U) : (memref>) -> () // U† = conj(U^T) linalg.generic { indexing_maps = [affine_map<(i,j) -> (j,i)>, affine_map<(i,j) -> (i,j)>], iterator_types = ["parallel","parallel"]} ins(%U : memref>) outs(%Ut : memref>) { ^bb0(%u : complex, %ut : complex): %re = complex.re %u : f64 %im = complex.im %u : f64 %neg_im = arith.negf %im : f64 %conj = complex.create %re, %neg_im : complex linalg.yield %conj : complex } // GEMM 1: tmp = U * ρ_in ← fused with GEMM 2 below linalg.matmul ins(%U, %rho_in : memref>, memref>) outs(%tmp : memref>) // GEMM 2: evolved = tmp * U† ← --affine-loop-fusion merges into GEMM 1 // After fusion: single i,j,k,l loop, tmp[i,l] computed inline, never stored %evolved = memref.alloc(%d, %d) : memref> linalg.matmul ins(%tmp, %Ut : memref>, memref>) outs(%evolved : memref>) // ρ' = φ⁻¹·evolved + φ⁻²·ρ_in (Fibonacci contraction) %phi_inv = arith.constant 0.6180339887498948482 : f64 %phi_inv2 = arith.constant 0.3819660112501051518 : f64 linalg.generic { indexing_maps = [ affine_map<(i,j) -> (i,j)>, affine_map<(i,j) -> (i,j)>, affine_map<(i,j) -> (i,j)>], iterator_types = ["parallel","parallel"]} ins(%evolved, %rho_in : memref>, memref>) outs(%rho_out : memref>) { ^bb0(%e : complex, %r : complex, %out : complex): %p1 = complex.create %phi_inv, %cst_zero_f : complex %p2 = complex.create %phi_inv2, %cst_zero_f : complex %a = complex.mul %p1, %e : complex %b = complex.mul %p2, %r : complex %c = complex.add %a, %b : complex linalg.yield %c : complex } // Plasma + Bifrost %ok = func.call @sov_plasma_verify(%rho_out) : (memref>) -> i1 cf.assert %ok, "JORDAN PLASMA FAIL" func.call @sov_blake3_hash_matrix(%rho_out, %hash) : (memref>, memref<32xi8>) -> () func.call @sov_bifrost_sign(%hash, %sk, %sig) : (memref<32xi8>, memref<32xi8>, memref<64xi8>) -> () memref.dealloc %U : memref> memref.dealloc %tmp : memref> memref.dealloc %Ut : memref> memref.dealloc %evolved : memref> return } // ═══════════════════════════════════════════════════════════════ // KERNEL 3: born_fused // p_j = tr(q_j ρ) for all j, then reconstruct x̂ = Σ p_j ψ_j // TWO linalg.generic ops fused into ONE by --affine-loop-fusion // ═══════════════════════════════════════════════════════════════ func.func @born_fused( %rho : memref>, // [d, d] %q : memref>, // [m, d, d] %frame : memref>, // [m, d, d] %probs : memref, // [m] %sig_out : memref>, // [d, d] %tau : f64 // temperature ) { %m = memref.dim %q, %c0 : memref> %d = memref.dim %rho, %c0 : memref> // Born: p_j = tr(q_j ρ) — reduction over d×d %raw = memref.alloc(%m) : memref linalg.generic { indexing_maps = [ affine_map<(j,k,l) -> (j,k,l)>, // q[j,k,l] affine_map<(j,k,l) -> (l,k)>, // rho[l,k] (transpose for trace) affine_map<(j,k,l) -> (j)> // raw[j] ], iterator_types = ["parallel","reduction","reduction"]} ins(%q, %rho : memref>, memref>) outs(%raw_cx : memref>) { ^bb0(%qi : complex, %ri : complex, %acc : complex): %prod = complex.mul %qi, %ri : complex %sum = complex.add %acc, %prod : complex linalg.yield %sum : complex } // Temperature softmax: p = softmax(raw / τ) %sum_e = memref.alloc() : memref memref.store %cst_zero_f, %sum_e[] : memref affine.for %j = 0 to %m { %r_cx = memref.load %raw_cx[%j] : memref> %r_re = complex.re %r_cx : f64 %r_t = arith.divf %r_re, %tau : f64 %e = math.exp %r_t : f64 memref.store %e, %raw[%j] : memref %s = memref.load %sum_e[] : memref %s2 = arith.addf %s, %e : f64 memref.store %s2, %sum_e[] : memref } %s = memref.load %sum_e[] : memref affine.for %j = 0 to %m { %e = memref.load %raw[%j] : memref %p = arith.divf %e, %s : f64 memref.store %p, %probs[%j] : memref } // Reconstruct: x̂ = p +.× ψ — FUSED with Born above linalg.generic { indexing_maps = [ affine_map<(j,k,l) -> (j)>, // probs[j] affine_map<(j,k,l) -> (j,k,l)>, // frame[j,k,l] affine_map<(j,k,l) -> (k,l)> // sig_out[k,l] ], iterator_types = ["reduction","parallel","parallel"]} ins(%probs, %frame : memref, memref>) outs(%sig_out : memref>) { ^bb0(%p : f64, %f : complex, %acc : complex): %p_cx = complex.create %p, %cst_zero_f : complex %prod = complex.mul %p_cx, %f : complex %sum = complex.add %acc, %prod : complex linalg.yield %sum : complex } memref.dealloc %raw : memref memref.dealloc %raw_cx : memref> memref.dealloc %sum_e : memref return } // ═══════════════════════════════════════════════════════════════ // MAIN: jst_forward — THE ONE KERNEL // SPE → jordan×N → Born+reconstruct // After --affine-loop-fusion: the entire forward pass is one // polyhedral loop nest. On GPU: one kernel launch. // ═══════════════════════════════════════════════════════════════ func.func @jst_forward( %signal : memref>, // raw input [d,d] %frame : memref>, // SPE frame [r,d,d] %H_list : memref>, // Hamiltonians [N,d,d] %dt_list : memref, // time steps [N] %q_set : memref>, // measurement projectors [m,d,d] %tau : f64, // temperature %sig_out : memref>, // output signal [d,d] %probs_out: memref, // Born probabilities [m] %receipts : memref, // WORM receipts [N×96] %sk : memref<32xi8> // Ed25519 signing key ) { %r = memref.dim %frame, %c0 : memref> %d = memref.dim %signal, %c0 : memref> %N = memref.dim %H_list, %c0 : memref> %rho = memref.alloc(%d, %d) : memref> %eigs = memref.alloc(%r) : memref %h0 = memref.alloc() : memref<32xi8> %s0 = memref.alloc() : memref<64xi8> %pk = memref.alloc() : memref<32xi8> // public key (derive from sk in prod) // ── STEP 1: SPE encode → ρ₀ ───────────────────────────────── func.call @spe_encode_fused(%signal, %frame, %rho, %eigs, %h0, %s0, %sk) : (memref>, memref>, memref>, memref, memref<32xi8>, memref<64xi8>, memref<32xi8>) -> () // ── STEP 2: N × jordan_fused — the beating heart ───────────── // APL: \ jordan_fused over H_list (prefix scan) %rho_nxt = memref.alloc(%d, %d) : memref> affine.for %k = 0 to %N { %dt = memref.load %dt_list[%k] : memref %H_k = memref.subview %H_list[%k, 0, 0][1,%d,%d][1,1,1] : memref> to memref> // Receipt slot: receipts[k*96 .. k*96+95] %off = arith.muli %k, %c96 : index %hash = memref.subview %receipts[%off][32][1] : memref to memref<32xi8> %sig = memref.subview %receipts[%off+32][64][1] : memref to memref<64xi8> func.call @jordan_fused(%H_k, %rho, %rho_nxt, %dt, %hash, %sig, %sk) : (memref>, memref>, memref>, f64, memref<32xi8>, memref<64xi8>, memref<32xi8>) -> () // In-place update: swap rho ← rho_nxt linalg.copy ins(%rho_nxt : memref>) outs(%rho : memref>) } // ── STEP 3: Born rule + reconstruct ────────────────────────── func.call @born_fused(%rho, %q_set, %frame, %probs_out, %sig_out, %tau) : (memref>, memref>, memref>, memref, memref>, f64) -> () memref.dealloc %rho : memref> memref.dealloc %rho_nxt : memref> memref.dealloc %eigs : memref memref.dealloc %h0 : memref<32xi8> memref.dealloc %s0 : memref<64xi8> memref.dealloc %pk : memref<32xi8> return } // ── GPU VARIANT: same function, gpu.launch outlined by pass ──── func.func @jst_forward_gpu( %signal : memref>, %frame : memref>, %H_list : memref>, %dt_list : memref, %q_set : memref>, %tau : f64, %sig_out : memref>, %probs_out: memref, %receipts : memref, %sk : memref<32xi8> ) { // --gpu-kernel-outlining transforms jst_forward into a gpu.launch // The linalg.matmul calls become gpu.func kernels (PTX / SPIR-V) // Plasma + Bifrost remain on host (constant-time, CPU-side) func.call @jst_forward(%signal,%frame,%H_list,%dt_list,%q_set, %tau,%sig_out,%probs_out,%receipts,%sk) : (memref>, memref>, memref>, memref, memref>, f64, memref>, memref, memref, memref<32xi8>) -> () return } // ── Constants ────────────────────────────────────────────────── %cst_zero_f = arith.constant 0.0 : f64 %cst_neg_inf = arith.constant 0xFF800000 : f64 // -inf for max init %c96 = arith.constant 96 : index } // module @jst_sovereign