diff --git a/crates/cuda-driver/src/moe_fixed.ptx b/crates/cuda-driver/src/moe_fixed.ptx new file mode 100644 index 00000000..5bcc1aed --- /dev/null +++ b/crates/cuda-driver/src/moe_fixed.ptx @@ -0,0 +1,816 @@ +// Fixed STRA-167 default-profile MoE assets. Offline regeneration only. +// Pinned upstream d9ab8435f654c368c586340d490915f6addf56a3. +// shared_expert.cu SHA256 bcb63f8b6f2dd92d5035ceae33f9b6b513fe34e3e2c5c54a4c9bbf867dbad4ca +// Native SwiGLU62-68; default scalar109-125; scale128-130; legacy combine143-156. +// bf16_bits.hpp supplies exact ties-even integer conversion. +// Regenerate: nvcc --ptx -std=c++17 -O3 -arch=sm_120 --fmad=true --ftz=false +// No global --use_fast_math: original shared_expert.cu uses ordinary flags; +// native SwiGLU explicitly invokes __expf/__fdividef. +// This candidate source is not the independent original-source oracle. +// Exact Context-owned whole buffers; grid/block fixed in the private registry. +// Source-defined positiveInf exponential saturation remains admissible. +// Invalid input/NaN/invalid denominator/nonfinite output signals canonical qNaN. +// Cargo neither builds nor links CUDA; no Toolkit/Strata serving dependency. +// +// MIT License +// +// Copyright (c) 2026 Niko1221 and the Strata contributors +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +// +// Generated by NVIDIA NVVM Compiler +// +// Compiler Build ID: CL-35583870 +// Cuda compilation tools, release 12.8, V12.8.93 +// Based on NVVM 20.0.0 +// + +.version 8.7 +.target sm_120 +.address_size 64 + + // .globl leleka_moe_swiglu_f32 +// _ZZ31leleka_moe_shared_gate_f32_bf16E7scratch has been demoted +// _ZZ31leleka_moe_shared_gate_f32_bf16E15invalid_scratch has been demoted + +.visible .entry leleka_moe_swiglu_f32( + .param .u64 leleka_moe_swiglu_f32_param_0, + .param .u64 leleka_moe_swiglu_f32_param_1, + .param .u64 leleka_moe_swiglu_f32_param_2 +) +{ + .reg .pred %p<9>; + .reg .b32 %r<7>; + .reg .f32 %f<15>; + .reg .b64 %rd<17>; + + ld.param.u64 %rd3, [leleka_moe_swiglu_f32_param_0]; + ld.param.u64 %rd4, [leleka_moe_swiglu_f32_param_1]; + ld.param.u64 %rd5, [leleka_moe_swiglu_f32_param_2]; + cvta.to.global.u64 %rd1, %rd5; + mov.u32 %r2, %ctaid.x; + mov.u32 %r3, %ntid.x; + mov.u32 %r4, %tid.x; + mad.lo.s32 %r1, %r2, %r3, %r4; + setp.gt.u32 %p1, %r1, 639; + @%p1 bra $L__BB0_6; + cvta.to.global.u64 %rd6, %rd3; + cvta.to.global.u64 %rd7, %rd4; + cvt.u64.u32 %rd2, %r1; + mul.wide.u32 %rd8, %r1, 4; + add.s64 %rd9, %rd6, %rd8; + ld.global.f32 %f1, [%rd9]; + add.s64 %rd10, %rd7, %rd8; + ld.global.f32 %f2, [%rd10]; + abs.f32 %f4, %f1; + setp.ne.f32 %p2, %f4, 0f7F800000; + abs.f32 %f5, %f2; + setp.ne.f32 %p3, %f5, 0f7F800000; + and.pred %p4, %p2, %p3; + @%p4 bra $L__BB0_3; + shl.b64 %rd15, %rd2, 2; + add.s64 %rd16, %rd1, %rd15; + mov.b32 %r6, 2143289344; + st.global.u32 [%rd16], %r6; + bra.uni $L__BB0_6; +$L__BB0_3: + mul.f32 %f7, %f1, 0fBFB8AA3B; + ex2.approx.f32 %f8, %f7; + add.f32 %f9, %f8, 0f3F800000; + abs.f32 %f10, %f8; + setp.num.f32 %p5, %f10, %f10; + setp.gt.f32 %p6, %f9, 0f00000000; + and.pred %p7, %p5, %p6; + @%p7 bra $L__BB0_5; + shl.b64 %rd13, %rd2, 2; + add.s64 %rd14, %rd1, %rd13; + mov.b32 %r5, 2143289344; + st.global.u32 [%rd14], %r5; + bra.uni $L__BB0_6; +$L__BB0_5: + div.approx.f32 %f11, %f1, %f9; + mul.f32 %f12, %f2, %f11; + abs.f32 %f13, %f12; + setp.equ.f32 %p8, %f13, 0f7F800000; + selp.f32 %f14, 0f7FC00000, %f12, %p8; + shl.b64 %rd11, %rd2, 2; + add.s64 %rd12, %rd1, %rd11; + st.global.f32 [%rd12], %f14; +$L__BB0_6: + ret; + +} + // .globl leleka_moe_shared_gate_f32_bf16 +.visible .entry leleka_moe_shared_gate_f32_bf16( + .param .u64 leleka_moe_shared_gate_f32_bf16_param_0, + .param .u64 leleka_moe_shared_gate_f32_bf16_param_1, + .param .u64 leleka_moe_shared_gate_f32_bf16_param_2 +) +{ + .reg .pred %p<91>; + .reg .b16 %rs<6>; + .reg .b32 %r<210>; + .reg .f32 %f<44>; + .reg .b64 %rd<39>; + .reg .f64 %fd<88>; + // demoted variable + .shared .align 8 .b8 _ZZ31leleka_moe_shared_gate_f32_bf16E7scratch[64]; + // demoted variable + .shared .align 4 .b8 _ZZ31leleka_moe_shared_gate_f32_bf16E15invalid_scratch[32]; + ld.param.u64 %rd16, [leleka_moe_shared_gate_f32_bf16_param_0]; + ld.param.u64 %rd17, [leleka_moe_shared_gate_f32_bf16_param_1]; + ld.param.u64 %rd15, [leleka_moe_shared_gate_f32_bf16_param_2]; + cvta.to.global.u64 %rd1, %rd17; + cvta.to.global.u64 %rd2, %rd16; + mov.u32 %r1, %tid.x; + mov.u32 %r2, %ntid.x; + add.s32 %r194, %r1, %r2; + cvt.u16.u32 %rs1, %r194; + mov.u16 %rs2, 2559; + sub.s16 %rs3, %rs2, %rs1; + cvt.u16.u32 %rs4, %r2; + div.u16 %rs5, %rs3, %rs4; + cvt.u32.u16 %r62, %rs5; + add.s32 %r4, %r62, 2; + and.b32 %r5, %r4, 3; + setp.lt.u16 %p17, %rs5, 2; + mov.f64 %fd85, 0d0000000000000000; + mov.b32 %r209, 0; + mov.u32 %r204, %r1; + @%p17 bra $L__BB1_23; + mad.lo.s32 %r196, %r2, 3, %r1; + shl.b32 %r7, %r2, 2; + add.s32 %r64, %r2, %r2; + add.s32 %r195, %r1, %r64; + and.b32 %r65, %r4, 131068; + neg.s32 %r193, %r65; + mov.f64 %fd85, 0d0000000000000000; + mov.b32 %r209, 0; + mov.u32 %r204, %r1; +$L__BB1_2: + cvt.u64.u32 %rd3, %r204; + mul.wide.u32 %rd18, %r204, 4; + add.s64 %rd19, %rd2, %rd18; + ld.global.f32 %f1, [%rd19]; + mov.b32 %r16, %f1; + and.b32 %r66, %r16, 2147483647; + setp.lt.u32 %p18, %r66, 2139095041; + @%p18 bra $L__BB1_4; + and.b32 %r71, %r16, -4259840; + or.b32 %r199, %r71, 4194304; + bra.uni $L__BB1_5; +$L__BB1_4: + shr.u32 %r67, %r16, 16; + and.b32 %r68, %r67, 1; + add.s32 %r69, %r16, %r68; + add.s32 %r70, %r69, 32767; + and.b32 %r199, %r70, -65536; +$L__BB1_5: + mov.b32 %f2, %r199; + add.s64 %rd20, %rd3, %rd3; + add.s64 %rd21, %rd1, %rd20; + ld.global.u16 %r72, [%rd21]; + shl.b32 %r73, %r72, 16; + mov.b32 %f3, %r73; + abs.f32 %f17, %f1; + setp.equ.f32 %p20, %f17, 0f7F800000; + abs.f32 %f18, %f2; + setp.equ.f32 %p21, %f18, 0f7F800000; + or.pred %p22, %p20, %p21; + mov.pred %p84, -1; + @%p22 bra $L__BB1_7; + abs.f32 %f20, %f3; + setp.equ.f32 %p84, %f20, 0f7F800000; +$L__BB1_7: + cvt.u32.u64 %r20, %rd3; + selp.u32 %r74, 1, 0, %p84; + or.b32 %r21, %r209, %r74; + cvt.f64.f32 %fd22, %f2; + cvt.f64.f32 %fd23, %f3; + fma.rn.f64 %fd2, %fd22, %fd23, %fd85; + cvt.u64.u32 %rd4, %r194; + mul.wide.u32 %rd22, %r194, 4; + add.s64 %rd23, %rd2, %rd22; + ld.global.f32 %f4, [%rd23]; + mov.b32 %r22, %f4; + and.b32 %r75, %r22, 2147483647; + setp.gt.u32 %p23, %r75, 2139095040; + @%p23 bra $L__BB1_9; + shr.u32 %r76, %r22, 16; + and.b32 %r77, %r76, 1; + add.s32 %r78, %r22, %r77; + add.s32 %r79, %r78, 32767; + and.b32 %r200, %r79, -65536; + bra.uni $L__BB1_10; +$L__BB1_9: + and.b32 %r80, %r22, -4259840; + or.b32 %r200, %r80, 4194304; +$L__BB1_10: + mov.b32 %f5, %r200; + add.s64 %rd24, %rd4, %rd4; + add.s64 %rd25, %rd1, %rd24; + ld.global.u16 %r81, [%rd25]; + shl.b32 %r82, %r81, 16; + mov.b32 %f6, %r82; + abs.f32 %f21, %f4; + setp.equ.f32 %p25, %f21, 0f7F800000; + abs.f32 %f22, %f5; + setp.equ.f32 %p26, %f22, 0f7F800000; + or.pred %p27, %p25, %p26; + mov.pred %p85, -1; + @%p27 bra $L__BB1_12; + abs.f32 %f24, %f6; + setp.equ.f32 %p85, %f24, 0f7F800000; +$L__BB1_12: + selp.u32 %r83, 1, 0, %p85; + or.b32 %r26, %r21, %r83; + cvt.f64.f32 %fd24, %f5; + cvt.f64.f32 %fd25, %f6; + fma.rn.f64 %fd3, %fd24, %fd25, %fd2; + add.s32 %r27, %r20, %r2; + cvt.u64.u32 %rd5, %r195; + mul.wide.u32 %rd26, %r195, 4; + add.s64 %rd27, %rd2, %rd26; + ld.global.f32 %f7, [%rd27]; + mov.b32 %r28, %f7; + and.b32 %r84, %r28, 2147483647; + setp.gt.u32 %p28, %r84, 2139095040; + @%p28 bra $L__BB1_14; + shr.u32 %r85, %r28, 16; + and.b32 %r86, %r85, 1; + add.s32 %r87, %r28, %r86; + add.s32 %r88, %r87, 32767; + and.b32 %r201, %r88, -65536; + bra.uni $L__BB1_15; +$L__BB1_14: + and.b32 %r89, %r28, -4259840; + or.b32 %r201, %r89, 4194304; +$L__BB1_15: + mov.b32 %f8, %r201; + add.s64 %rd28, %rd5, %rd5; + add.s64 %rd29, %rd1, %rd28; + ld.global.u16 %r90, [%rd29]; + shl.b32 %r91, %r90, 16; + mov.b32 %f9, %r91; + abs.f32 %f25, %f7; + setp.equ.f32 %p30, %f25, 0f7F800000; + abs.f32 %f26, %f8; + setp.equ.f32 %p31, %f26, 0f7F800000; + or.pred %p32, %p30, %p31; + mov.pred %p86, -1; + @%p32 bra $L__BB1_17; + abs.f32 %f28, %f9; + setp.equ.f32 %p86, %f28, 0f7F800000; +$L__BB1_17: + selp.u32 %r92, 1, 0, %p86; + or.b32 %r32, %r26, %r92; + cvt.f64.f32 %fd26, %f8; + cvt.f64.f32 %fd27, %f9; + fma.rn.f64 %fd4, %fd26, %fd27, %fd3; + add.s32 %r33, %r27, %r2; + cvt.u64.u32 %rd6, %r196; + mul.wide.u32 %rd30, %r196, 4; + add.s64 %rd31, %rd2, %rd30; + ld.global.f32 %f10, [%rd31]; + mov.b32 %r34, %f10; + and.b32 %r93, %r34, 2147483647; + setp.gt.u32 %p33, %r93, 2139095040; + @%p33 bra $L__BB1_19; + shr.u32 %r94, %r34, 16; + and.b32 %r95, %r94, 1; + add.s32 %r96, %r34, %r95; + add.s32 %r97, %r96, 32767; + and.b32 %r202, %r97, -65536; + bra.uni $L__BB1_20; +$L__BB1_19: + and.b32 %r98, %r34, -4259840; + or.b32 %r202, %r98, 4194304; +$L__BB1_20: + mov.b32 %f11, %r202; + add.s64 %rd32, %rd6, %rd6; + add.s64 %rd33, %rd1, %rd32; + ld.global.u16 %r99, [%rd33]; + shl.b32 %r100, %r99, 16; + mov.b32 %f12, %r100; + abs.f32 %f29, %f10; + setp.equ.f32 %p35, %f29, 0f7F800000; + abs.f32 %f30, %f11; + setp.equ.f32 %p36, %f30, 0f7F800000; + or.pred %p37, %p35, %p36; + mov.pred %p87, -1; + @%p37 bra $L__BB1_22; + abs.f32 %f32, %f12; + setp.equ.f32 %p87, %f32, 0f7F800000; +$L__BB1_22: + selp.u32 %r101, 1, 0, %p87; + or.b32 %r209, %r32, %r101; + cvt.f64.f32 %fd28, %f11; + cvt.f64.f32 %fd29, %f12; + fma.rn.f64 %fd85, %fd28, %fd29, %fd4; + add.s32 %r102, %r33, %r2; + add.s32 %r204, %r102, %r2; + add.s32 %r196, %r196, %r7; + add.s32 %r195, %r195, %r7; + add.s32 %r194, %r194, %r7; + add.s32 %r193, %r193, 4; + setp.ne.s32 %p38, %r193, 0; + @%p38 bra $L__BB1_2; +$L__BB1_23: + setp.eq.s32 %p39, %r5, 0; + @%p39 bra $L__BB1_31; + mul.wide.u32 %rd34, %r204, 2; + add.s64 %rd38, %rd1, %rd34; + mul.wide.u32 %rd8, %r2, 2; + mul.wide.u32 %rd35, %r204, 4; + add.s64 %rd37, %rd2, %rd35; + mul.wide.u32 %rd10, %r2, 4; + neg.s32 %r206, %r5; +$L__BB1_25: + .pragma "nounroll"; + ld.global.f32 %f13, [%rd37]; + mov.b32 %r50, %f13; + and.b32 %r103, %r50, 2147483647; + setp.gt.u32 %p40, %r103, 2139095040; + @%p40 bra $L__BB1_27; + shr.u32 %r104, %r50, 16; + and.b32 %r105, %r104, 1; + add.s32 %r106, %r50, %r105; + add.s32 %r107, %r106, 32767; + and.b32 %r208, %r107, -65536; + bra.uni $L__BB1_28; +$L__BB1_27: + and.b32 %r108, %r50, -4259840; + or.b32 %r208, %r108, 4194304; +$L__BB1_28: + mov.b32 %f14, %r208; + ld.global.u16 %r109, [%rd38]; + shl.b32 %r110, %r109, 16; + mov.b32 %f15, %r110; + abs.f32 %f33, %f13; + setp.equ.f32 %p42, %f33, 0f7F800000; + abs.f32 %f34, %f14; + setp.equ.f32 %p43, %f34, 0f7F800000; + or.pred %p44, %p42, %p43; + mov.pred %p88, -1; + @%p44 bra $L__BB1_30; + abs.f32 %f36, %f15; + setp.equ.f32 %p88, %f36, 0f7F800000; +$L__BB1_30: + selp.u32 %r111, 1, 0, %p88; + or.b32 %r209, %r209, %r111; + cvt.f64.f32 %fd30, %f14; + cvt.f64.f32 %fd31, %f15; + fma.rn.f64 %fd85, %fd30, %fd31, %fd85; + add.s64 %rd38, %rd38, %rd8; + add.s64 %rd37, %rd37, %rd10; + add.s32 %r206, %r206, 1; + setp.ne.s32 %p45, %r206, 0; + @%p45 bra $L__BB1_25; +$L__BB1_31: + bar.sync 0; + and.b32 %r136, %r1, 31; + // begin inline asm + mov.b64 {%r112,%r113}, %fd85; + // end inline asm + shfl.sync.down.b32 %r115|%p46, %r113, 16, 31, -1; + shfl.sync.down.b32 %r114|%p47, %r112, 16, 31, -1; + // begin inline asm + mov.b64 %fd33, {%r114,%r115}; + // end inline asm + add.f64 %fd34, %fd85, %fd33; + // begin inline asm + mov.b64 {%r116,%r117}, %fd34; + // end inline asm + shfl.sync.down.b32 %r119|%p48, %r117, 8, 31, -1; + shfl.sync.down.b32 %r118|%p49, %r116, 8, 31, -1; + // begin inline asm + mov.b64 %fd35, {%r118,%r119}; + // end inline asm + add.f64 %fd36, %fd34, %fd35; + // begin inline asm + mov.b64 {%r120,%r121}, %fd36; + // end inline asm + shfl.sync.down.b32 %r123|%p50, %r121, 4, 31, -1; + shfl.sync.down.b32 %r122|%p51, %r120, 4, 31, -1; + // begin inline asm + mov.b64 %fd37, {%r122,%r123}; + // end inline asm + add.f64 %fd38, %fd36, %fd37; + // begin inline asm + mov.b64 {%r124,%r125}, %fd38; + // end inline asm + shfl.sync.down.b32 %r127|%p52, %r125, 2, 31, -1; + shfl.sync.down.b32 %r126|%p53, %r124, 2, 31, -1; + // begin inline asm + mov.b64 %fd39, {%r126,%r127}; + // end inline asm + add.f64 %fd40, %fd38, %fd39; + // begin inline asm + mov.b64 {%r128,%r129}, %fd40; + // end inline asm + shfl.sync.down.b32 %r131|%p54, %r129, 1, 31, -1; + shfl.sync.down.b32 %r130|%p55, %r128, 1, 31, -1; + // begin inline asm + mov.b64 %fd41, {%r130,%r131}; + // end inline asm + add.f64 %fd42, %fd40, %fd41; + // begin inline asm + mov.b64 {%r132,%r133}, %fd42; + // end inline asm + shfl.sync.idx.b32 %r135|%p56, %r133, 0, 31, -1; + shfl.sync.idx.b32 %r134|%p57, %r132, 0, 31, -1; + // begin inline asm + mov.b64 %fd43, {%r134,%r135}; + // end inline asm + setp.ne.s32 %p58, %r209, 0; + mov.b32 %r137, -1; + vote.sync.any.pred %p11, %p58, %r137; + setp.ne.s32 %p59, %r136, 0; + @%p59 bra $L__BB1_33; + selp.u32 %r139, 1, 0, %p11; + shr.u32 %r140, %r1, 2; + mov.u32 %r141, _ZZ31leleka_moe_shared_gate_f32_bf16E7scratch; + add.s32 %r142, %r141, %r140; + st.shared.f64 [%r142], %fd43; + shr.u32 %r143, %r1, 3; + mov.u32 %r144, _ZZ31leleka_moe_shared_gate_f32_bf16E15invalid_scratch; + add.s32 %r145, %r144, %r143; + st.shared.u32 [%r145], %r139; +$L__BB1_33: + bar.sync 0; + setp.gt.u32 %p60, %r1, 31; + @%p60 bra $L__BB1_43; + setp.gt.u32 %p62, %r1, 7; + mov.pred %p89, 0; + mov.f64 %fd86, 0d0000000000000000; + @%p62 bra $L__BB1_36; + shl.b32 %r146, %r1, 3; + mov.u32 %r147, _ZZ31leleka_moe_shared_gate_f32_bf16E7scratch; + add.s32 %r148, %r147, %r146; + ld.shared.f64 %fd86, [%r148]; + shl.b32 %r149, %r1, 2; + mov.u32 %r150, _ZZ31leleka_moe_shared_gate_f32_bf16E15invalid_scratch; + add.s32 %r151, %r150, %r149; + ld.shared.u32 %r152, [%r151]; + setp.ne.s32 %p89, %r152, 0; +$L__BB1_36: + // begin inline asm + mov.b64 {%r153,%r154}, %fd86; + // end inline asm + shfl.sync.down.b32 %r156|%p63, %r154, 16, 31, -1; + shfl.sync.down.b32 %r155|%p64, %r153, 16, 31, -1; + // begin inline asm + mov.b64 %fd46, {%r155,%r156}; + // end inline asm + add.f64 %fd47, %fd86, %fd46; + // begin inline asm + mov.b64 {%r157,%r158}, %fd47; + // end inline asm + shfl.sync.down.b32 %r160|%p65, %r158, 8, 31, -1; + shfl.sync.down.b32 %r159|%p66, %r157, 8, 31, -1; + // begin inline asm + mov.b64 %fd48, {%r159,%r160}; + // end inline asm + add.f64 %fd49, %fd47, %fd48; + // begin inline asm + mov.b64 {%r161,%r162}, %fd49; + // end inline asm + shfl.sync.down.b32 %r164|%p67, %r162, 4, 31, -1; + shfl.sync.down.b32 %r163|%p68, %r161, 4, 31, -1; + // begin inline asm + mov.b64 %fd50, {%r163,%r164}; + // end inline asm + add.f64 %fd51, %fd49, %fd50; + // begin inline asm + mov.b64 {%r165,%r166}, %fd51; + // end inline asm + shfl.sync.down.b32 %r168|%p69, %r166, 2, 31, -1; + shfl.sync.down.b32 %r167|%p70, %r165, 2, 31, -1; + // begin inline asm + mov.b64 %fd52, {%r167,%r168}; + // end inline asm + add.f64 %fd53, %fd51, %fd52; + // begin inline asm + mov.b64 {%r169,%r170}, %fd53; + // end inline asm + shfl.sync.down.b32 %r172|%p71, %r170, 1, 31, -1; + shfl.sync.down.b32 %r171|%p72, %r169, 1, 31, -1; + // begin inline asm + mov.b64 %fd54, {%r171,%r172}; + // end inline asm + add.f64 %fd55, %fd53, %fd54; + // begin inline asm + mov.b64 {%r173,%r174}, %fd55; + // end inline asm + shfl.sync.idx.b32 %r176|%p73, %r174, 0, 31, -1; + shfl.sync.idx.b32 %r175|%p74, %r173, 0, 31, -1; + // begin inline asm + mov.b64 %fd56, {%r175,%r176}; + // end inline asm + mov.b32 %r177, -1; + vote.sync.any.pred %p14, %p89, %r177; + setp.ne.s32 %p75, %r1, 0; + @%p75 bra $L__BB1_43; + neg.f64 %fd57, %fd56; + fma.rn.f64 %fd58, %fd56, 0dBFF71547652B82FE, 0d4338000000000000; + { + .reg .b32 %temp; + mov.b64 {%r57, %temp}, %fd58; + } + mov.f64 %fd59, 0dC338000000000000; + add.rn.f64 %fd60, %fd58, %fd59; + fma.rn.f64 %fd61, %fd60, 0dBFE62E42FEFA39EF, %fd57; + fma.rn.f64 %fd62, %fd60, 0dBC7ABC9E3B39803F, %fd61; + fma.rn.f64 %fd63, %fd62, 0d3E5ADE1569CE2BDF, 0d3E928AF3FCA213EA; + fma.rn.f64 %fd64, %fd63, %fd62, 0d3EC71DEE62401315; + fma.rn.f64 %fd65, %fd64, %fd62, 0d3EFA01997C89EB71; + fma.rn.f64 %fd66, %fd65, %fd62, 0d3F2A01A014761F65; + fma.rn.f64 %fd67, %fd66, %fd62, 0d3F56C16C1852B7AF; + fma.rn.f64 %fd68, %fd67, %fd62, 0d3F81111111122322; + fma.rn.f64 %fd69, %fd68, %fd62, 0d3FA55555555502A1; + fma.rn.f64 %fd70, %fd69, %fd62, 0d3FC5555555555511; + fma.rn.f64 %fd71, %fd70, %fd62, 0d3FE000000000000B; + fma.rn.f64 %fd72, %fd71, %fd62, 0d3FF0000000000000; + fma.rn.f64 %fd73, %fd72, %fd62, 0d3FF0000000000000; + { + .reg .b32 %temp; + mov.b64 {%r58, %temp}, %fd73; + } + { + .reg .b32 %temp; + mov.b64 {%temp, %r59}, %fd73; + } + shl.b32 %r179, %r57, 20; + add.s32 %r180, %r179, %r59; + mov.b64 %fd87, {%r58, %r180}; + { + .reg .b32 %temp; + mov.b64 {%temp, %r181}, %fd57; + } + mov.b32 %f37, %r181; + abs.f32 %f16, %f37; + setp.lt.f32 %p76, %f16, 0f4086232B; + @%p76 bra $L__BB1_40; + setp.gt.f64 %p77, %fd56, 0d0000000000000000; + mov.f64 %fd74, 0d7FF0000000000000; + sub.f64 %fd75, %fd74, %fd56; + selp.f64 %fd87, 0d0000000000000000, %fd75, %p77; + setp.geu.f32 %p78, %f16, 0f40874800; + @%p78 bra $L__BB1_40; + shr.u32 %r182, %r57, 31; + add.s32 %r183, %r57, %r182; + shr.s32 %r184, %r183, 1; + shl.b32 %r185, %r184, 20; + add.s32 %r186, %r59, %r185; + mov.b64 %fd76, {%r58, %r186}; + sub.s32 %r187, %r57, %r184; + shl.b32 %r188, %r187, 20; + add.s32 %r189, %r188, 1072693248; + mov.b32 %r190, 0; + mov.b64 %fd77, {%r190, %r189}; + mul.f64 %fd87, %fd77, %fd76; +$L__BB1_40: + mov.pred %p90, -1; + @%p14 bra $L__BB1_42; + { + .reg .b32 %temp; + mov.b64 {%temp, %r191}, %fd56; + } + and.b32 %r192, %r191, 2146435072; + setp.eq.s32 %p80, %r192, 2146435072; + setp.nan.f64 %p81, %fd87, %fd87; + or.pred %p90, %p80, %p81; +$L__BB1_42: + add.f64 %fd78, %fd87, 0d3FF0000000000000; + rcp.rn.f64 %fd80, %fd78; + cvt.rn.f32.f64 %f38, %fd80; + setp.leu.f64 %p82, %fd78, 0d0000000000000000; + abs.f32 %f39, %f38; + setp.equ.f32 %p83, %f39, 0f7F800000; + selp.f32 %f41, 0f7FC00000, %f38, %p83; + selp.f32 %f42, 0f7FC00000, %f41, %p82; + selp.f32 %f43, 0f7FC00000, %f42, %p90; + cvta.to.global.u64 %rd36, %rd15; + st.global.f32 [%rd36], %f43; +$L__BB1_43: + ret; + +} + // .globl leleka_moe_scale_shared_f32 +.visible .entry leleka_moe_scale_shared_f32( + .param .u64 leleka_moe_scale_shared_f32_param_0, + .param .u64 leleka_moe_scale_shared_f32_param_1, + .param .u64 leleka_moe_scale_shared_f32_param_2 +) +{ + .reg .pred %p<5>; + .reg .b32 %r<5>; + .reg .f32 %f<12>; + .reg .b64 %rd<10>; + + ld.param.u64 %rd1, [leleka_moe_scale_shared_f32_param_0]; + ld.param.u64 %rd2, [leleka_moe_scale_shared_f32_param_1]; + ld.param.u64 %rd3, [leleka_moe_scale_shared_f32_param_2]; + mov.u32 %r2, %ctaid.x; + mov.u32 %r3, %ntid.x; + mov.u32 %r4, %tid.x; + mad.lo.s32 %r1, %r2, %r3, %r4; + setp.gt.u32 %p1, %r1, 2559; + @%p1 bra $L__BB2_2; + cvta.to.global.u64 %rd4, %rd3; + cvta.to.global.u64 %rd5, %rd1; + cvta.to.global.u64 %rd6, %rd2; + mul.wide.u32 %rd7, %r1, 4; + add.s64 %rd8, %rd5, %rd7; + ld.global.f32 %f1, [%rd8]; + ld.global.f32 %f2, [%rd6]; + mul.f32 %f3, %f1, %f2; + abs.f32 %f4, %f1; + setp.equ.f32 %p2, %f4, 0f7F800000; + abs.f32 %f5, %f2; + setp.equ.f32 %p3, %f5, 0f7F800000; + abs.f32 %f7, %f3; + setp.equ.f32 %p4, %f7, 0f7F800000; + selp.f32 %f9, 0f7FC00000, %f3, %p4; + selp.f32 %f10, 0f7FC00000, %f9, %p3; + selp.f32 %f11, 0f7FC00000, %f10, %p2; + add.s64 %rd9, %rd4, %rd7; + st.global.f32 [%rd9], %f11; +$L__BB2_2: + ret; + +} + // .globl leleka_moe_combine_f32 +.visible .entry leleka_moe_combine_f32( + .param .u64 leleka_moe_combine_f32_param_0, + .param .u64 leleka_moe_combine_f32_param_1, + .param .u64 leleka_moe_combine_f32_param_2, + .param .u64 leleka_moe_combine_f32_param_3 +) +{ + .reg .pred %p<24>; + .reg .b32 %r<5>; + .reg .f32 %f<79>; + .reg .b64 %rd<13>; + .reg .f64 %fd<33>; + + ld.param.u64 %rd1, [leleka_moe_combine_f32_param_0]; + ld.param.u64 %rd2, [leleka_moe_combine_f32_param_1]; + ld.param.u64 %rd3, [leleka_moe_combine_f32_param_2]; + ld.param.u64 %rd4, [leleka_moe_combine_f32_param_3]; + mov.u32 %r2, %ctaid.x; + mov.u32 %r3, %ntid.x; + mov.u32 %r4, %tid.x; + mad.lo.s32 %r1, %r2, %r3, %r4; + setp.gt.u32 %p1, %r1, 2559; + @%p1 bra $L__BB3_2; + cvta.to.global.u64 %rd5, %rd3; + cvta.to.global.u64 %rd6, %rd4; + cvta.to.global.u64 %rd7, %rd2; + cvta.to.global.u64 %rd8, %rd1; + ld.global.f32 %f1, [%rd7]; + mul.wide.u32 %rd9, %r1, 4; + add.s64 %rd10, %rd8, %rd9; + ld.global.f32 %f2, [%rd10]; + abs.f32 %f3, %f1; + setp.equ.f32 %p2, %f3, 0f7F800000; + abs.f32 %f4, %f2; + setp.equ.f32 %p3, %f4, 0f7F800000; + cvt.f64.f32 %fd1, %f1; + cvt.f64.f32 %fd2, %f2; + fma.rn.f64 %fd3, %fd1, %fd2, 0d0000000000000000; + ld.global.f32 %f6, [%rd7+4]; + ld.global.f32 %f7, [%rd10+10240]; + abs.f32 %f8, %f6; + setp.equ.f32 %p4, %f8, 0f7F800000; + abs.f32 %f9, %f7; + setp.equ.f32 %p5, %f9, 0f7F800000; + cvt.f64.f32 %fd4, %f6; + cvt.f64.f32 %fd5, %f7; + fma.rn.f64 %fd6, %fd4, %fd5, %fd3; + ld.global.f32 %f11, [%rd7+8]; + ld.global.f32 %f12, [%rd10+20480]; + abs.f32 %f13, %f11; + setp.equ.f32 %p6, %f13, 0f7F800000; + abs.f32 %f14, %f12; + setp.equ.f32 %p7, %f14, 0f7F800000; + cvt.f64.f32 %fd7, %f11; + cvt.f64.f32 %fd8, %f12; + fma.rn.f64 %fd9, %fd7, %fd8, %fd6; + ld.global.f32 %f16, [%rd7+12]; + ld.global.f32 %f17, [%rd10+30720]; + abs.f32 %f18, %f16; + setp.equ.f32 %p8, %f18, 0f7F800000; + abs.f32 %f19, %f17; + setp.equ.f32 %p9, %f19, 0f7F800000; + cvt.f64.f32 %fd10, %f16; + cvt.f64.f32 %fd11, %f17; + fma.rn.f64 %fd12, %fd10, %fd11, %fd9; + ld.global.f32 %f21, [%rd7+16]; + ld.global.f32 %f22, [%rd10+40960]; + abs.f32 %f23, %f21; + setp.equ.f32 %p10, %f23, 0f7F800000; + abs.f32 %f24, %f22; + setp.equ.f32 %p11, %f24, 0f7F800000; + cvt.f64.f32 %fd13, %f21; + cvt.f64.f32 %fd14, %f22; + fma.rn.f64 %fd15, %fd13, %fd14, %fd12; + ld.global.f32 %f26, [%rd7+20]; + ld.global.f32 %f27, [%rd10+51200]; + abs.f32 %f28, %f26; + setp.equ.f32 %p12, %f28, 0f7F800000; + abs.f32 %f29, %f27; + setp.equ.f32 %p13, %f29, 0f7F800000; + cvt.f64.f32 %fd16, %f26; + cvt.f64.f32 %fd17, %f27; + fma.rn.f64 %fd18, %fd16, %fd17, %fd15; + ld.global.f32 %f31, [%rd7+24]; + ld.global.f32 %f32, [%rd10+61440]; + abs.f32 %f33, %f31; + setp.equ.f32 %p14, %f33, 0f7F800000; + abs.f32 %f34, %f32; + setp.equ.f32 %p15, %f34, 0f7F800000; + cvt.f64.f32 %fd19, %f31; + cvt.f64.f32 %fd20, %f32; + fma.rn.f64 %fd21, %fd19, %fd20, %fd18; + ld.global.f32 %f36, [%rd7+28]; + ld.global.f32 %f37, [%rd10+71680]; + abs.f32 %f38, %f36; + setp.equ.f32 %p16, %f38, 0f7F800000; + abs.f32 %f39, %f37; + setp.equ.f32 %p17, %f39, 0f7F800000; + cvt.f64.f32 %fd22, %f36; + cvt.f64.f32 %fd23, %f37; + fma.rn.f64 %fd24, %fd22, %fd23, %fd21; + ld.global.f32 %f41, [%rd7+32]; + ld.global.f32 %f42, [%rd10+81920]; + abs.f32 %f43, %f41; + setp.equ.f32 %p18, %f43, 0f7F800000; + abs.f32 %f44, %f42; + setp.equ.f32 %p19, %f44, 0f7F800000; + cvt.f64.f32 %fd25, %f41; + cvt.f64.f32 %fd26, %f42; + fma.rn.f64 %fd27, %fd25, %fd26, %fd24; + ld.global.f32 %f46, [%rd7+36]; + ld.global.f32 %f47, [%rd10+92160]; + abs.f32 %f48, %f46; + setp.equ.f32 %p20, %f48, 0f7F800000; + abs.f32 %f49, %f47; + setp.equ.f32 %p21, %f49, 0f7F800000; + cvt.f64.f32 %fd28, %f46; + cvt.f64.f32 %fd29, %f47; + fma.rn.f64 %fd30, %fd28, %fd29, %fd27; + add.s64 %rd11, %rd5, %rd9; + ld.global.f32 %f51, [%rd11]; + cvt.f64.f32 %fd31, %f51; + add.f64 %fd32, %fd30, %fd31; + cvt.rn.f32.f64 %f52, %fd32; + abs.f32 %f53, %f51; + setp.equ.f32 %p22, %f53, 0f7F800000; + abs.f32 %f55, %f52; + setp.equ.f32 %p23, %f55, 0f7F800000; + selp.f32 %f57, 0f7FC00000, %f52, %p23; + selp.f32 %f58, 0f7FC00000, %f57, %p22; + selp.f32 %f59, 0f7FC00000, %f58, %p21; + selp.f32 %f60, 0f7FC00000, %f59, %p20; + selp.f32 %f61, 0f7FC00000, %f60, %p19; + selp.f32 %f62, 0f7FC00000, %f61, %p18; + selp.f32 %f63, 0f7FC00000, %f62, %p17; + selp.f32 %f64, 0f7FC00000, %f63, %p16; + selp.f32 %f65, 0f7FC00000, %f64, %p15; + selp.f32 %f66, 0f7FC00000, %f65, %p14; + selp.f32 %f67, 0f7FC00000, %f66, %p13; + selp.f32 %f68, 0f7FC00000, %f67, %p12; + selp.f32 %f69, 0f7FC00000, %f68, %p11; + selp.f32 %f70, 0f7FC00000, %f69, %p10; + selp.f32 %f71, 0f7FC00000, %f70, %p9; + selp.f32 %f72, 0f7FC00000, %f71, %p8; + selp.f32 %f73, 0f7FC00000, %f72, %p7; + selp.f32 %f74, 0f7FC00000, %f73, %p6; + selp.f32 %f75, 0f7FC00000, %f74, %p5; + selp.f32 %f76, 0f7FC00000, %f75, %p4; + selp.f32 %f77, 0f7FC00000, %f76, %p3; + selp.f32 %f78, 0f7FC00000, %f77, %p2; + add.s64 %rd12, %rd6, %rd9; + st.global.f32 [%rd12], %f78; +$L__BB3_2: + ret; + +} diff --git a/crates/cuda-driver/src/offline_moe_kernel.cu b/crates/cuda-driver/src/offline_moe_kernel.cu new file mode 100644 index 00000000..ce45f136 --- /dev/null +++ b/crates/cuda-driver/src/offline_moe_kernel.cu @@ -0,0 +1,117 @@ +// Fixed STRA-167 default-profile MoE assets. Offline regeneration only. +// Pinned upstream d9ab8435f654c368c586340d490915f6addf56a3. +// shared_expert.cu SHA256 bcb63f8b6f2dd92d5035ceae33f9b6b513fe34e3e2c5c54a4c9bbf867dbad4ca +// Native SwiGLU62-68; default scalar109-125; scale128-130; legacy combine143-156. +// bf16_bits.hpp supplies exact ties-even integer conversion. +// Regenerate: nvcc --ptx -std=c++17 -O3 -arch=sm_120 --fmad=true --ftz=false +// No global --use_fast_math: original shared_expert.cu uses ordinary flags; +// native SwiGLU explicitly invokes __expf/__fdividef. +// This candidate source is not the independent original-source oracle. +// Exact Context-owned whole buffers; grid/block fixed in the private registry. +// Source-defined positiveInf exponential saturation remains admissible. +// Invalid input/NaN/invalid denominator/nonfinite output signals canonical qNaN. +// Cargo neither builds nor links CUDA; no Toolkit/Strata serving dependency. +// +// MIT License +// +// Copyright (c) 2026 Niko1221 and the Strata contributors +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. +#include +#include +#include + +__device__ __forceinline__ float refusal() { return __uint_as_float(0x7fc00000u); } +__device__ __forceinline__ uint16_t original_bf16(float value) { + uint32_t word = __float_as_uint(value); + if ((word & 0x7fffffffu) > 0x7f800000u) return uint16_t((word >> 16) | 64u); + word = (word + ((word >> 16) & 1u) + 0x7fffu) & 0xffff0000u; + return uint16_t(word >> 16); +} +__device__ __forceinline__ float original_f32(uint16_t value) { return __uint_as_float(uint32_t(value) << 16); } +__device__ __forceinline__ double original_warp_sum(double value) { + for (int offset = 16; offset > 0; offset >>= 1) value += __shfl_down_sync(0xffffffffu, value, offset); + return __shfl_sync(0xffffffffu, value, 0); +} +extern "C" __global__ void leleka_moe_swiglu_f32(const float* gate, const float* up, float* output) { + const int i = int(blockIdx.x) * int(blockDim.x) + int(threadIdx.x); + if (i >= 640) return; + const float g = gate[i], u = up[i]; + if (!isfinite(g) || !isfinite(u)) { output[i] = refusal(); return; } + const float exponential = __expf(-g); + const float denominator = 1.0f + exponential; + if (isnan(exponential) || !(denominator > 0.0f)) { output[i] = refusal(); return; } + // Preserve source operation order, including signedzero from +Inf exp. + const float result = __fdividef(g, denominator) * u; + output[i] = isfinite(result) ? result : refusal(); +} +extern "C" __global__ void leleka_moe_shared_gate_f32_bf16(const float* input, const uint16_t* weights, float* output) { + __shared__ double scratch[8]; + __shared__ int invalid_scratch[8]; + double acc = 0.0; + int invalid = 0; + for (int i = int(threadIdx.x); i < 2560; i += int(blockDim.x)) { + const float source = input[i]; + const float x = original_f32(original_bf16(source)); + const float w = original_f32(weights[i]); + invalid |= !isfinite(source) || !isfinite(x) || !isfinite(w); + acc += double(x) * double(w); + } + __syncthreads(); + const int lane = int(threadIdx.x) & 31, warp = int(threadIdx.x) >> 5; + acc = original_warp_sum(acc); + invalid = __any_sync(0xffffffffu, invalid); + if (lane == 0) { scratch[warp] = acc; invalid_scratch[warp] = invalid; } + __syncthreads(); + if (warp == 0) { + acc = threadIdx.x < 8 ? scratch[threadIdx.x] : 0.0; + invalid = threadIdx.x < 8 ? invalid_scratch[threadIdx.x] : 0; + acc = original_warp_sum(acc); + invalid = __any_sync(0xffffffffu, invalid); + if (threadIdx.x == 0) { + // Original DOUBLE exp can overflow to +Inf and legitimately give0. + const double exponential = exp(-acc); + const double denominator = 1.0 + exponential; + const float result = float(1.0 / denominator); + output[0] = !invalid && isfinite(acc) && !isnan(exponential) && denominator > 0.0 && isfinite(result) ? result : refusal(); + } + } +} +extern "C" __global__ void leleka_moe_scale_shared_f32(const float* input, const float* scalar, float* output) { + const int i = int(blockIdx.x) * int(blockDim.x) + int(threadIdx.x); + if (i >= 2560) return; + const float x = input[i], g = scalar[0]; + const float result = x * g; + output[i] = isfinite(x) && isfinite(g) && isfinite(result) ? result : refusal(); +} +extern "C" __global__ void leleka_moe_combine_f32(const float* parts, const float* weights, const float* shared, float* output) { + const int j = int(blockIdx.x) * int(blockDim.x) + int(threadIdx.x); + if (j >= 2560) return; + double acc = 0.0; + int invalid = 0; + for (int expert = 0; expert < 10; ++expert) { + const float weight = weights[expert], part = parts[expert * 2560 + j]; + invalid |= !isfinite(weight) || !isfinite(part); + acc += double(weight) * double(part); + } + const float plain_shared = shared[j]; + acc += double(plain_shared); + const float result = float(acc); + output[j] = !invalid && isfinite(plain_shared) && isfinite(result) ? result : refusal(); +} diff --git a/crates/cuda-driver/src/qwen_dispatch.rs b/crates/cuda-driver/src/qwen_dispatch.rs index 533f5054..fc832ddd 100644 --- a/crates/cuda-driver/src/qwen_dispatch.rs +++ b/crates/cuda-driver/src/qwen_dispatch.rs @@ -310,6 +310,63 @@ fn admission<'asset>( Ok((asset, layout, handles)) } impl Context<'_> { + /// Native quantized-expert SwiGLU on two fixed 640-value F32 vectors. + pub fn moe_swiglu_f32( + &self, + gate: &DeviceBuffer<'_>, + up: &DeviceBuffer<'_>, + output: &mut DeviceBuffer<'_>, + ) -> Result<()> { + self.qwen_call(Operation::MoeSwiglu, &[gate, up, output], 0.0, false) + } + + /// Original BF16-rounded activation and DOUBLE shared scalar reduction. + pub fn moe_shared_scalar_f32_bf16( + &self, + input: &DeviceBuffer<'_>, + weights: &DeviceBuffer<'_>, + output: &mut DeviceBuffer<'_>, + ) -> Result<()> { + self.qwen_call( + Operation::MoeSharedScalar, + &[input, weights, output], + 0.0, + false, + ) + } + + /// Scale one shared 2560-value vector by its completed scalar gate. + pub fn moe_shared_scale_f32( + &self, + input: &DeviceBuffer<'_>, + scalar: &DeviceBuffer<'_>, + output: &mut DeviceBuffer<'_>, + ) -> Result<()> { + self.qwen_call( + Operation::MoeSharedScale, + &[input, scalar, output], + 0.0, + false, + ) + } + + /// Original ordered DOUBLE reduction of ten routed outputs plus shared. + /// All buffers store F32; only the fixed kernel's accumulator is F64. + pub fn moe_combine_f64( + &self, + parts: &DeviceBuffer<'_>, + weights: &DeviceBuffer<'_>, + shared: &DeviceBuffer<'_>, + output: &mut DeviceBuffer<'_>, + ) -> Result<()> { + self.qwen_call( + Operation::MoeCombine, + &[parts, weights, shared, output], + 0.0, + false, + ) + } + /// Fixed PLE value projection: BF16-rounded activation, source double sum. pub fn ple_value_bf16_f32( &self, @@ -657,6 +714,179 @@ mod tests { use super::*; use std::cell::RefCell; + const MOE_OPERATIONS: [Operation; 4] = [ + Operation::MoeSwiglu, + Operation::MoeSharedScalar, + Operation::MoeSharedScale, + Operation::MoeCombine, + ]; + + #[cfg(not(all( + feature = "cuda-driver", + target_os = "linux", + target_pointer_width = "64" + )))] + #[test] + fn moe_qualified_methods_refuse_unsupported_platform_without_mutation() { + use crate::{Driver, FixedPairKernels, ProjectionKernels}; + use std::marker::PhantomData; + let driver = Driver { + api: Rc::new(ffi::Api), + version: 0, + }; + let context = Context { + driver: &driver, + handle: Some(ffi::ContextHandle::synthetic(1)), + budget: 1_000_000, + allocated: Cell::new(99), + poisoned: Cell::new(false), + vector_add_kernel: Cell::new(None), + matvec_kernel: Cell::new(None), + q5_k_kernel: Cell::>::new(None), + q6_k_kernel: Cell::new(None), + common_quantizer: Cell::new(None), + projection_kernels: std::array::from_fn(|_| { + Cell::>::new(None) + }), + qwen_kernels: Cache::new(), + }; + for operation in MOE_OPERATIONS { + let layout = operation.layout(); + let mut buffers: Vec<_> = (0..layout.count) + .map(|i| DeviceBuffer { + context: &context, + handle: Some(ffi::BufferHandle::synthetic(0x1000000 * (i as u64 + 1))), + bytes: layout.bytes[i], + _thread: PhantomData, + }) + .collect(); + let (inputs, outputs) = buffers.split_at_mut(layout.count - 1); + let result = match operation { + Operation::MoeSwiglu => { + context.moe_swiglu_f32(&inputs[0], &inputs[1], &mut outputs[0]) + } + Operation::MoeSharedScalar => { + context.moe_shared_scalar_f32_bf16(&inputs[0], &inputs[1], &mut outputs[0]) + } + Operation::MoeSharedScale => { + context.moe_shared_scale_f32(&inputs[0], &inputs[1], &mut outputs[0]) + } + Operation::MoeCombine => { + context.moe_combine_f64(&inputs[0], &inputs[1], &inputs[2], &mut outputs[0]) + } + _ => unreachable!("four frozen MoE methods"), + }; + assert_eq!(result, Err(Error::UnsupportedPlatform)); + assert_eq!(context.allocated.get(), 99); + assert_eq!(ready(&context.qwen_kernels), 0); + assert!(!context.poisoned.get()); + for (i, buffer) in buffers.iter_mut().enumerate() { + assert!( + buffer.handle == Some(ffi::BufferHandle::synthetic(0x1000000 * (i as u64 + 1))) + ); + assert_eq!(buffer.bytes, layout.bytes[i]); + // Synthetic owners have no CUDA allocation to free. + buffer.handle = None; + } + } + } + + #[test] + fn moe_new_preflight_requires_exact_disjoint_owner_spans() { + let handles = + std::array::from_fn(|i| ffi::BufferHandle::synthetic(0x1000000 * (i as u64 + 1))); + for operation in MOE_OPERATIONS { + let layout = operation.layout(); + assert_eq!(ranges(operation, handles, layout.bytes, false), Ok(())); + for index in 0..layout.count { + let mut short = layout.bytes; + short[index] -= 1; + assert_eq!( + ranges(operation, handles, short, false), + Err(Error::InvalidQwenShape) + ); + for address in [handles[index].qwen_address() + 1, u64::MAX - 1] { + let mut wrong = handles; + wrong[index] = ffi::BufferHandle::synthetic(address); + assert_eq!( + ranges(operation, wrong, layout.bytes, false), + Err(Error::InvalidQwenShape) + ); + } + } + let output = layout.count - 1; + for input in 0..output { + let mut aliased = handles; + aliased[output] = handles[input]; + assert_eq!( + ranges(operation, aliased, layout.bytes, false), + Err(Error::OutputAliasesInput) + ); + } + assert_eq!( + ranges(operation, handles, layout.bytes, true), + Err(Error::OutputAliasesInput) + ); + } + } + + #[test] + fn moe_new_slots_cache_atomically_and_keep_launch_sync_restore_order() { + for operation in MOE_OPERATIONS { + let backend = Mock::default(); + let cache = Cache::new(); + let asset = qwen_ffi::mock_asset(operation); + execute(&backend, &cache, &asset, arguments(operation)).unwrap(); + execute(&backend, &cache, &asset, arguments(operation)).unwrap(); + assert_eq!( + *backend.trace.borrow(), + [ + "push", "load", "lookup", "launch", "sync", "restore", "push", "launch", + "sync", "restore" + ] + ); + assert_eq!(ready(&cache), 1); + + let backend = Mock::fault(&["lookup", "unload", "restore"]); + let cache = Cache::new(); + assert_eq!( + execute(&backend, &cache, &asset, arguments(operation)), + Err(nested( + nested(error("lookup"), error("unload")), + error("restore") + )) + ); + assert_eq!(ready(&cache), 0); + assert_eq!( + *backend.trace.borrow(), + ["push", "load", "lookup", "unload", "restore"] + ); + assert!(backend.poisoned()); + + let backend = Mock::fault(&["launch", "sync", "restore"]); + let cache = Cache::new(); + assert_eq!( + execute(&backend, &cache, &asset, arguments(operation)), + Err(nested( + nested(error("launch"), error("sync")), + error("restore") + )) + ); + assert_eq!( + *backend.trace.borrow(), + ["push", "load", "lookup", "launch", "sync", "restore"] + ); + assert!(backend.poisoned()); + assert!(backend.retained.get() > 0); + let trace = backend.trace.borrow().clone(); + assert_eq!( + execute(&backend, &cache, &asset, arguments(operation)), + Err(Error::ContextPoisoned) + ); + assert_eq!(*backend.trace.borrow(), trace); + } + } + #[test] fn gdn_fixed_abi_keeps_recurrent_order_and_refuses_prior_output_alias() { let operation = Operation::GdnStep; diff --git a/crates/cuda-driver/src/qwen_ffi.rs b/crates/cuda-driver/src/qwen_ffi.rs index 323f47e7..9955afc8 100644 --- a/crates/cuda-driver/src/qwen_ffi.rs +++ b/crates/cuda-driver/src/qwen_ffi.rs @@ -34,8 +34,12 @@ pub(super) enum Operation { PleBroadcast, PleConv, PleResidual, + MoeSwiglu, + MoeSharedScalar, + MoeSharedScale, + MoeCombine, } -pub(super) const OPERATIONS: [Operation; 22] = [ +pub(super) const OPERATIONS: [Operation; 26] = [ Operation::Down, Operation::Up, Operation::InjectProjection, @@ -58,6 +62,10 @@ pub(super) const OPERATIONS: [Operation; 22] = [ Operation::PleBroadcast, Operation::PleConv, Operation::PleResidual, + Operation::MoeSwiglu, + Operation::MoeSharedScalar, + Operation::MoeSharedScale, + Operation::MoeCombine, ]; impl Operation { @@ -82,9 +90,16 @@ impl Operation { Self::PleBroadcast => c"leleka_ple_broadcast_f32", Self::PleConv => c"leleka_ple_conv_history_f32", Self::PleResidual => c"leleka_ple_residual_f32", + Self::MoeSwiglu => c"leleka_moe_swiglu_f32", + Self::MoeSharedScalar => c"leleka_moe_shared_gate_f32_bf16", + Self::MoeSharedScale => c"leleka_moe_scale_shared_f32", + Self::MoeCombine => c"leleka_moe_combine_f32", } } pub(super) fn layout(self) -> Layout { + if let Some(layout) = moe_layout(self) { + return layout; + } if let Some(layout) = ple_layout(self) { return layout; } @@ -169,6 +184,9 @@ impl Operation { | Self::PleBroadcast | Self::PleConv | Self::PleResidual => unreachable!("fixed PLE geometry above"), + Self::MoeSwiglu | Self::MoeSharedScalar | Self::MoeSharedScale | Self::MoeCombine => { + unreachable!("fixed MoE geometry above") + } }; let projection = matches!(self, Self::Down | Self::Up | Self::InjectProjection); let grid = if projection || self == Self::Norm { @@ -227,6 +245,33 @@ fn ple_layout(operation: Operation) -> Option { }) } +fn moe_layout(operation: Operation) -> Option { + let (bytes, count, outputs, grid) = match operation { + Operation::MoeSwiglu => ([2560, 2560, 2560, 0, 0, 0, 0, 0], 3, 0b100, 3), + Operation::MoeSharedScalar => ([10240, 5120, 4, 0, 0, 0, 0, 0], 3, 0b100, 1), + Operation::MoeSharedScale => ([10240, 4, 10240, 0, 0, 0, 0, 0], 3, 0b100, 10), + Operation::MoeCombine => ([102400, 40, 10240, 10240, 0, 0, 0, 0], 4, 0b1000, 10), + _ => return None, + }; + let mut align = [4; 8]; + if operation == Operation::MoeSharedScalar { + align[1] = 2; + } + Some(Layout { + bytes, + align, + count, + outputs, + rows: 10, + columns: 2560, + grid, + grid_z: 1, + block: 256, + block_y: 1, + projection: false, + }) +} + fn gdn_layout(operation: Operation) -> Option { let (bytes, count, outputs, grid, grid_z, block, block_y) = match operation { Operation::GdnConv => ( @@ -595,6 +640,45 @@ const GDN_ASSETS: [Asset; 6] = [ gdn_asset(Operation::GdnStep), gdn_asset(Operation::GdnOutNorm), ]; +// Exact STRA-167 fixed-operator corpus admission; full-model quality remains open. +const MOE_RECEIPT: Receipt = Receipt { + version: 1, + source_sha256: [ + 0xbd, 0xd9, 0xd3, 0x2a, 0xf7, 0xcf, 0x5b, 0x11, 0x7f, 0xa9, 0xb1, 0xff, 0xae, 0x08, 0x83, + 0xbb, 0xc9, 0xc1, 0xbc, 0x33, 0x2d, 0x8e, 0xe8, 0x7c, 0x88, 0x72, 0x34, 0xa4, 0x00, 0x02, + 0x07, 0xee, + ], + ptx_sha256: [ + 0xeb, 0x25, 0xf4, 0x5f, 0x2b, 0xf1, 0xcd, 0x3b, 0xe5, 0x7e, 0x0b, 0x5f, 0x32, 0x6a, 0x63, + 0xf6, 0x66, 0x05, 0x9a, 0x58, 0x70, 0x6d, 0x7a, 0x2b, 0xd4, 0x70, 0x52, 0x6c, 0x22, 0xc0, + 0xf6, 0x7a, + ], + oracle_sha256: [ + 0xfb, 0x0a, 0xd5, 0x84, 0x81, 0xbc, 0xb8, 0x4b, 0x18, 0xa4, 0x37, 0xe6, 0x6b, 0x14, 0x56, + 0xd0, 0x26, 0x66, 0xf3, 0x97, 0x02, 0x17, 0xb1, 0x72, 0x9c, 0xa9, 0x7b, 0x2d, 0x84, 0xb2, + 0x99, 0x48, + ], + compiler: "NVCC12.8.93 CUDA12.8.1; installed Driver595.91.07; SM120", + compiler_flags: "-ptx -O3 -std=c++17 -arch=sm_120 --fmad=true --ftz=false; explicit native SwiGLU intrinsics; legacy shared/combine ordinary flags", + qualification_receipt: "STRA-167 1999cfbd-15c8-4edd-86fe-e51378eeb1ff; rawmanifest63742810", + comparison: "Frozen10case corpus11524 F32words exact/signzero;3 malformed refusals; defined exponential saturation preserved; component profile only, no allfinite/fullmodel proof", + abi_version: 1, + topology: [N as u32, HC as u32, H as u32], +}; +const fn moe_asset(operation: Operation) -> Asset { + Asset { + operation, + ptx: concat!(include_str!("moe_fixed.ptx"), "\0").as_bytes(), + receipt: MOE_RECEIPT, + pin: MOE_RECEIPT, + } +} +const MOE_ASSETS: [Asset; 4] = [ + moe_asset(Operation::MoeSwiglu), + moe_asset(Operation::MoeSharedScalar), + moe_asset(Operation::MoeSharedScale), + moe_asset(Operation::MoeCombine), +]; pub(super) fn asset(operation: Operation) -> Option<&'static Asset> { match operation { Operation::Down => Some(&BF16_ASSETS[0]), @@ -619,6 +703,10 @@ pub(super) fn asset(operation: Operation) -> Option<&'static Asset> { Operation::GdnStep => Some(&GDN_ASSETS[4]), Operation::GdnOutNorm => Some(&GDN_ASSETS[5]), Operation::RouteTop10 => Some(&ROUTE_ASSET), + Operation::MoeSwiglu => Some(&MOE_ASSETS[0]), + Operation::MoeSharedScalar => Some(&MOE_ASSETS[1]), + Operation::MoeSharedScale => Some(&MOE_ASSETS[2]), + Operation::MoeCombine => Some(&MOE_ASSETS[3]), } } @@ -815,6 +903,76 @@ pub(super) fn mock_asset(operation: Operation) -> Asset { #[cfg(test)] mod tests { use super::*; + #[test] + fn moe_fixed_pointer_abi_extents_and_qualified_receipts() { + // Literal expectations come from STRA-167 AD v1 / STRA-172 AD v8. + for (operation, entry, bytes, align, count, outputs, grid) in [ + ( + Operation::MoeSwiglu, + c"leleka_moe_swiglu_f32", + [2560, 2560, 2560, 0], + [4, 4, 4, 4], + 3, + 4, + 3, + ), + ( + Operation::MoeSharedScalar, + c"leleka_moe_shared_gate_f32_bf16", + [10240, 5120, 4, 0], + [4, 2, 4, 4], + 3, + 4, + 1, + ), + ( + Operation::MoeSharedScale, + c"leleka_moe_scale_shared_f32", + [10240, 4, 10240, 0], + [4, 4, 4, 4], + 3, + 4, + 10, + ), + ( + Operation::MoeCombine, + c"leleka_moe_combine_f32", + [102400, 40, 10240, 10240], + [4, 4, 4, 4], + 4, + 8, + 10, + ), + ] { + let layout = operation.layout(); + assert_eq!(operation.entry(), entry); + assert_eq!(layout.bytes[..4], bytes); + assert_eq!(layout.align[..4], align); + assert_eq!((layout.count, layout.outputs), (count, outputs)); + assert_eq!( + (layout.grid, layout.grid_z, layout.block, layout.block_y), + (grid, 1, 256, 1) + ); + assert!(!layout.projection); + assert!(!operation.has_epsilon()); + let qualified = asset(operation).expect("frozen167 qualified component"); + qualified.validate(operation).unwrap(); + assert_eq!(qualified.receipt, MOE_RECEIPT); + let handles = + [101, 202, 303, 404, 505, 606, 707, 808].map(ffi::BufferHandle::synthetic); + let mut arguments = Arguments::new(operation, handles, 0.0); + let parameters = arguments.parameters(); + // SAFETY: inspect only live initialized pointer-table fields. + unsafe { + for (index, expected) in [101u64, 202, 303, 404].iter().take(count).enumerate() { + assert_eq!(*(*parameters.add(index)).cast::(), *expected); + } + assert!((*parameters.add(count)).is_null()); + } + } + assert_eq!(OPERATIONS.len(), 26); + } + #[test] fn qwen_hash_known_vectors_and_padding_boundaries() { let hex = |bytes: &[u8]| {