@@ -104,29 +104,45 @@ inline uint ink2_word(device const uchar* p) {
104104 return as_type<uint>(uchar4 (*(device const packed_uchar4*)p));
105105}
106106
107- // E2M1 times two is 0, 1, 2, 3, 4, 6, 8 or 12: one nibble of this word a code.
108- inline float ink2_e2m1_twice (uint code) {
109- const float v = float ((0xC8643210u >> (4u * (code & 7u ))) & 15u );
110- return (code & 8u ) ? -v : v;
107+ constant float ink2_e2m1_twice_lut[16 ] = {0 , 1 , 2 , 3 , 4 , 6 , 8 , 12 , -0 .f , -1 , -2 , -3 , -4 , -6 , -8 , -12 };
108+
109+ // Exact 2-product guard: safe normal exponent [2^-116, 2^118) and no 3-multiple BF16 tie rounding diff.
110+ inline bool ink2_nv_two_prod_safe (float phi) {
111+ if (phi == 0 .0f ) return true ;
112+ const float ap = fabs (phi);
113+ if (!(ap >= 0x1 .0p-116f && ap < 0x1 .0p118f)) return false ;
114+ const float np = as_type<float >((as_type<uint>(phi) & 0x7fffffu ) | 0x3f800000u );
115+ const float hp = 0 .5f * np;
116+ for (uint m = 1u ; m < 8u ; ++m)
117+ if (bfloat (3 .0f * ((1 .0f + float (m) * 0 .125f ) * hp)) != bfloat ((1 .5f + float (m) * 0 .1875f ) * np)) return false ;
118+ return true ;
111119}
112120
113- // One simdgroup a dot: lane l sums blocks l, l + 32, ... in column order, then the 32 lanes are summed.
114- inline float ink2_dot_nv (device const uchar* w, device const uchar* scales,
115- device const bfloat* x, float phi, uint k, uint lane) {
121+ inline float ink2_e2m1_twice (uint code) { return ink2_e2m1_twice_lut[code & 15u ]; }
122+
123+ template <bool TWO_PROD >
124+ inline float ink2_dot_nv_impl (device const uchar* w, device const uchar* scales,
125+ device const bfloat* x, float arg, uint k, uint lane) {
116126 float sum = 0 .0f ;
117127 for (uint b = lane; b < k / 16u ; b += 32u ) {
118- // ink2_nv_weight's inner product, exact in fp32 either way: (2 * e2m1) * (e4m3 / 2).
119- const float half_scale = 0 .5f * ink2_e4m3 (scales[b]);
128+ const float bs = TWO_PROD ? (ink2_e4m3 (scales[b]) * arg) : (0 .5f * ink2_e4m3 (scales[b]));
120129 const uint lo = ink2_word (w + 8u * b), hi = ink2_word (w + 8u * b + 4u );
121130 for (uint j = 0 ; j < 16u ; ++j) {
122131 const uint code = ((j < 8u ? lo : hi) >> (4u * (j & 7u ))) & 15u ;
123- const float weight = float ( bfloat ( ink2_e2m1_twice ( code) * half_scale * phi) );
124- sum += weight * float (x[16u * b + j]);
132+ const float prod = TWO_PROD ? (ink2_e2m1_twice_lut[ code] * bs) : (ink2_e2m1_twice_lut[code] * bs * arg );
133+ sum += float ( bfloat (prod)) * float (x[16u * b + j]);
125134 }
126135 }
127136 return simd_sum (sum);
128137}
129138
139+ // One simdgroup a dot: lane l sums blocks l, l + 32, ... in column order, then the 32 lanes are summed.
140+ inline float ink2_dot_nv (device const uchar* w, device const uchar* scales,
141+ device const bfloat* x, float phi, uint k, uint lane) {
142+ return ink2_nv_two_prod_safe (phi) ? ink2_dot_nv_impl<true >(w, scales, x, 0 .5f * phi, k, lane)
143+ : ink2_dot_nv_impl<false >(w, scales, x, phi, k, lane);
144+ }
145+
130146// As ink2_dot_nv over 32-column MXFP8 blocks.
131147inline float ink2_dot_mx (device const uchar* w, device const uchar* scales,
132148 device const bfloat* x, uint k, uint lane) {
@@ -319,13 +335,14 @@ inline uint ink2_simd_match(uint raw, bool valid, uint nbits) {
319335 }
320336}
321337
322- // Block b of a packed NVFP4 row as its 16 BF16 weights, each as ink2_dot_nv decodes it.
323- inline void ink2_nv_block (device const uchar* w, device const uchar* scales, float phi, uint b, thread float * out) {
324- const float half_scale = 0 .5f * ink2_e4m3 (scales[b]);
338+ // Block b of a packed NVFP4 row as its 16 BF16 weights, matching ink2_dot_nv.
339+ template <bool TWO_PROD = false >
340+ inline void ink2_nv_block (device const uchar* w, device const uchar* scales, float arg, uint b, thread float * out) {
341+ const float bs = TWO_PROD ? (ink2_e4m3 (scales[b]) * arg) : (0 .5f * ink2_e4m3 (scales[b]));
325342 const uint lo = ink2_word (w + 8u * b), hi = ink2_word (w + 8u * b + 4u );
326343 for (uint j = 0 ; j < 16u ; ++j) {
327344 const uint code = ((j < 8u ? lo : hi) >> (4u * (j & 7u ))) & 15u ;
328- out[j] = float (bfloat (ink2_e2m1_twice ( code) * half_scale * phi ));
345+ out[j] = float (bfloat (TWO_PROD ? (ink2_e2m1_twice_lut[ code] * bs) : (ink2_e2m1_twice_lut[code] * bs * arg) ));
329346 }
330347}
331348
@@ -340,54 +357,77 @@ inline void ink2_mx_block(device const uchar* w, device const uchar* scales, uin
340357 }
341358}
342359
343- // Threadgroup (x, e): outputs 8x to 8x + 7 of expert e for each of its route pairs, four pairs a pass.
344- [[kernel, max_total_threads_per_threadgroup(256)]] void tf_ink2_nvfp4_gu_grouped (
345- device const bfloat* X [[buffer(0)]] , device const uchar* W [[buffer(1)]] ,
346- device const uchar* S [[buffer(2)]] , device const float * amax [[buffer(3)]] ,
347- device const float * denominator [[buffer(4)]] , device const uint* counts [[buffer(5)]] ,
348- device const uint* members [[buffer(6)]] , device bfloat* Y [[buffer(7)]] ,
349- constant Ink2GroupedArgs& a [[buffer(8)]] ,
350- uint2 tg [[threadgroup_position_in_grid]] , uint sg [[simdgroup_index_in_threadgroup]] ,
351- uint lane [[thread_index_in_simdgroup]] ) {
352- const uint n = tg.x * 8u + sg, expert = tg.y ;
353- if (n >= a.n ) return ;
354- const uint count = counts[expert];
355- if (count == 0u ) return ;
356- const ulong expert_w = ulong (expert) * 2u * a.n * (a.k / 2u );
357- const ulong expert_s = ulong (expert) * 2u * a.n * (a.k / 16u );
358- const float phi = precise::divide (float (bfloat (amax[expert])), float (bfloat (denominator[0 ])));
359- device const uchar* gate_w = W + expert_w + ulong (2u * n) * (a.k / 2u );
360- device const uchar* gate_s = S + expert_s + ulong (2u * n) * (a.k / 16u );
361- device const uchar* up_w = gate_w + (a.k / 2u );
362- device const uchar* up_s = gate_s + (a.k / 16u );
360+ template <bool GU , bool TWO_PROD >
361+ inline void ink2_nv_grouped_pass (device const bfloat* X, device const uchar* row_w, device const uchar* row_s,
362+ device const uint* members, device bfloat* Y, constant Ink2GroupedArgs& a,
363+ uint n, uint expert, uint count, float arg, uint lane) {
363364 for (uint first = 0u ; first < count; first += INK2_PASS ) {
364365 uint pair[INK2_PASS ];
365366 device const bfloat* x[INK2_PASS ];
366367 float g[INK2_PASS ], u[INK2_PASS ];
367368 for (uint j = 0u ; j < INK2_PASS ; ++j) {
368369 pair[j] = members[expert * a.cap + min (first + j, count - 1u )];
369- x[j] = X + ulong (pair[j] / a.topk ) * a.k ;
370- g[j] = 0 .0f ;
371- u[j] = 0 .0f ;
370+ x[j] = X + ulong (GU ? pair[j] / a.topk : pair[j]) * a.k ;
371+ g[j] = u[j] = 0 .0f ;
372372 }
373373 for (uint b = lane; b < a.k / 16u ; b += 32u ) {
374374 float gate[16 ], up[16 ];
375- ink2_nv_block (gate_w, gate_s, phi , b, gate);
376- ink2_nv_block (up_w, up_s, phi , b, up);
375+ ink2_nv_block< TWO_PROD >(row_w, row_s, arg , b, gate);
376+ if ( GU ) ink2_nv_block< TWO_PROD >(row_w + a. k / 2u , row_s + a. k / 16u , arg , b, up);
377377 for (uint j = 0u ; j < INK2_PASS ; ++j)
378378 for (uint i = 0u ; i < 16u ; ++i) {
379379 const float v = float (x[j][16u * b + i]);
380380 g[j] += gate[i] * v;
381- u[j] += up[i] * v;
381+ if ( GU ) u[j] += up[i] * v;
382382 }
383383 }
384384 for (uint j = 0u ; j < INK2_PASS ; ++j) {
385- const float gs = simd_sum (g[j]), us = simd_sum (u[j]);
386- if (lane == 0u && first + j < count) Y[ulong (pair[j]) * a.n + n] = ink2_act (gs, us);
385+ const float gs = simd_sum (g[j]), us = GU ? simd_sum (u[j]) : 0 . 0f ;
386+ if (lane == 0u && first + j < count) Y[ulong (pair[j]) * a.n + n] = GU ? ink2_act (gs, us) : bfloat (gs );
387387 }
388388 }
389389}
390390
391+ template <bool GU >
392+ __attribute__ ((noinline)) void ink2_nv_grouped_exact(
393+ device const bfloat* X, device const uchar* row_w, device const uchar* row_s,
394+ device const uint* members, device bfloat* Y, constant Ink2GroupedArgs& a,
395+ uint n, uint expert, uint count, float phi, uint lane) {
396+ ink2_nv_grouped_pass<GU , false >(X, row_w, row_s, members, Y, a, n, expert, count, phi, lane);
397+ }
398+
399+ template <bool GU >
400+ inline void ink2_nv_grouped (device const bfloat* X, device const uchar* W, device const uchar* S,
401+ device const float * amax, device const float * denominator,
402+ device const uint* counts, device const uint* members,
403+ device bfloat* Y, constant Ink2GroupedArgs& a, uint2 tg, uint sg, uint lane) {
404+ const uint n = tg.x * 8u + sg, expert = tg.y ;
405+ if (n >= a.n ) return ;
406+ const uint count = counts[expert];
407+ if (count == 0u ) return ;
408+ const uint rows_w = GU ? 2u * a.n : a.n , r = GU ? 2u * n : n;
409+ const float phi = precise::divide (float (bfloat (amax[expert])), float (bfloat (denominator[0 ])));
410+ device const uchar* row_w = W + ulong (expert) * rows_w * (a.k / 2u ) + ulong (r) * (a.k / 2u );
411+ device const uchar* row_s = S + ulong (expert) * rows_w * (a.k / 16u ) + ulong (r) * (a.k / 16u );
412+ if (!ink2_nv_two_prod_safe (phi)) {
413+ ink2_nv_grouped_exact<GU >(X, row_w, row_s, members, Y, a, n, expert, count, phi, lane);
414+ return ;
415+ }
416+ ink2_nv_grouped_pass<GU , true >(X, row_w, row_s, members, Y, a, n, expert, count, 0 .5f * phi, lane);
417+ }
418+
419+ // Threadgroup (x, e): outputs 8x to 8x + 7 of expert e for each of its route pairs, four pairs a pass.
420+ [[kernel, max_total_threads_per_threadgroup(256)]] void tf_ink2_nvfp4_gu_grouped (
421+ device const bfloat* X [[buffer(0)]] , device const uchar* W [[buffer(1)]] ,
422+ device const uchar* S [[buffer(2)]] , device const float * amax [[buffer(3)]] ,
423+ device const float * denominator [[buffer(4)]] , device const uint* counts [[buffer(5)]] ,
424+ device const uint* members [[buffer(6)]] , device bfloat* Y [[buffer(7)]] ,
425+ constant Ink2GroupedArgs& a [[buffer(8)]] ,
426+ uint2 tg [[threadgroup_position_in_grid]] , uint sg [[simdgroup_index_in_threadgroup]] ,
427+ uint lane [[thread_index_in_simdgroup]] ) {
428+ ink2_nv_grouped<true >(X, W, S, amax, denominator, counts, members, Y, a, tg, sg, lane);
429+ }
430+
391431[[kernel, max_total_threads_per_threadgroup(256)]] void tf_ink2_mxfp8_gu_grouped (
392432 device const bfloat* X [[buffer(0)]] , device const uchar* W [[buffer(1)]] ,
393433 device const uchar* S [[buffer(2)]] , device const uint* counts [[buffer(3)]] ,
@@ -441,33 +481,7 @@ inline void ink2_mx_block(device const uchar* w, device const uchar* scales, uin
441481 constant Ink2GroupedArgs& a [[buffer(8)]] ,
442482 uint2 tg [[threadgroup_position_in_grid]] , uint sg [[simdgroup_index_in_threadgroup]] ,
443483 uint lane [[thread_index_in_simdgroup]] ) {
444- const uint n = tg.x * 8u + sg, expert = tg.y ;
445- if (n >= a.n ) return ;
446- const uint count = counts[expert];
447- if (count == 0u ) return ;
448- const float phi = precise::divide (float (bfloat (amax[expert])), float (bfloat (denominator[0 ])));
449- device const uchar* row_w = W + ulong (expert) * a.n * (a.k / 2u ) + ulong (n) * (a.k / 2u );
450- device const uchar* row_s = S + ulong (expert) * a.n * (a.k / 16u ) + ulong (n) * (a.k / 16u );
451- for (uint first = 0u ; first < count; first += INK2_PASS ) {
452- uint pair[INK2_PASS ];
453- device const bfloat* x[INK2_PASS ];
454- float d[INK2_PASS ];
455- for (uint j = 0u ; j < INK2_PASS ; ++j) {
456- pair[j] = members[expert * a.cap + min (first + j, count - 1u )];
457- x[j] = X + ulong (pair[j]) * a.k ;
458- d[j] = 0 .0f ;
459- }
460- for (uint b = lane; b < a.k / 16u ; b += 32u ) {
461- float weight[16 ];
462- ink2_nv_block (row_w, row_s, phi, b, weight);
463- for (uint j = 0u ; j < INK2_PASS ; ++j)
464- for (uint i = 0u ; i < 16u ; ++i) d[j] += weight[i] * float (x[j][16u * b + i]);
465- }
466- for (uint j = 0u ; j < INK2_PASS ; ++j) {
467- const float ds = simd_sum (d[j]);
468- if (lane == 0u && first + j < count) Y[ulong (pair[j]) * a.n + n] = bfloat (ds);
469- }
470- }
484+ ink2_nv_grouped<false >(X, W, S, amax, denominator, counts, members, Y, a, tg, sg, lane);
471485}
472486
473487[[kernel, max_total_threads_per_threadgroup(256)]] void tf_ink2_mxfp8_down_grouped (
0 commit comments