@@ -734,6 +734,7 @@ __global__ void ink_clip_kernel(float* x, const float* __restrict__ scale, float
734734}
735735
736736// ink_clip() and ink_head_rms() of InkClips' four jobs in one launch, a block a token's head as each of them
737+ template <bool KV8 >
737738__global__ void ink_clips_kernel (InkClips c) {
738739 __shared__ float part[128 / WARP ];
739740 int b = blockIdx .x , j = 0 ;
@@ -747,7 +748,7 @@ __global__ void ink_clips_kernel(InkClips c) {
747748 if (!c.cache [j]) return ; // each thread its own values, as clip_head() and rms_head() left them
748749 long long cell = (c.at ? c.at [0 ] : c.pos ) + row;
749750 if (c.ring > 0 ) cell %= c.ring ;
750- if (c. kv8 ) {
751+ if (KV8 ) {
751752 kv8_store (v, (uint8_t *) c.cache [j], cell, c.heads [j], c.dim [j], head, part);
752753 return ;
753754 }
@@ -804,6 +805,7 @@ struct QkvArgs {
804805};
805806
806807// ink_qkv(): blocks [0, n_head) Q's heads, then K's n_kv, V's n_kv, and the relative positions' n_head; 128 threads.
808+ template <bool KV8 >
807809__global__ void ink_qkv_kernel (QkvArgs g) {
808810 pdl_trigger (); // PDL (pdl.hpp): the launch after this one may start
809811 pdl_wait ();
@@ -833,7 +835,7 @@ __global__ void ink_qkv_kernel(QkvArgs g) {
833835 if (is_v) rms_head (head, a.dim , a.eps , part);
834836 else clip_head (head, nullptr , a.qk_thr , a.dim , 0 .0f , 1 .0f , p, part);
835837 const long long row = a.ring > 0 ? p % a.ring : p;
836- if (a. kv8 ) kv8_store (head, (uint8_t *) (is_v ? a.vals : a.keys ), row, a.n_kv , a.dim , h, part);
838+ if (KV8 ) kv8_store (head, (uint8_t *) (is_v ? a.vals : a.keys ), row, a.n_kv , a.dim , h, part);
837839 else if (i < a.dim )
838840 ((__half*) (is_v ? a.vals : a.keys ))[size_t (row) * channels + size_t (h) * a.dim + i] = __float2half (head[i]);
839841 return ;
@@ -898,6 +900,7 @@ __device__ __forceinline__ void lane_halves(const __half* head, int lane, int n,
898900// kScoreKeys keys, one at a time: its lanes read the key side by side, once for all of the group's heads, and
899901// their products with the queries are summed across the warp. The bias's products are the first n_rel lanes'.
900902// t0 is the launch's first token among the call's, nk_max the scores a head has room for.
903+ template <bool KV8 >
901904__global__ void ink_score_kernel (InkAttn a, int t0, int nk_max) {
902905 pdl_trigger (); // PDL (pdl.hpp): the launch after this one may start
903906 pdl_wait ();
@@ -933,7 +936,7 @@ __global__ void ink_score_kernel(InkAttn a, int t0, int nk_max) {
933936#pragma unroll
934937 for (int u = 0 ; u < kScoreKeys ; ++u) {
935938 if (j0 + u < j1) {
936- if (a. kv8 ) lane_fp8 (a.keys , row, a.n_kv , a.dim , g, lane, lh, k[u]);
939+ if (KV8 ) lane_fp8 (a.keys , row, a.n_kv , a.dim , g, lane, lh, k[u]);
937940 else lane_halves ((const __half*) a.keys + (size_t (row) * a.n_kv + g) * a.dim , lane, lh, k[u]);
938941 const long long dist = p - (lo + j0 + u);
939942 rb[u] = lane < a.n_rel && dist < a.extent ? a.rel_bias [size_t (dist) * a.n_rel + lane] : 0 .0f ;
@@ -985,6 +988,7 @@ __global__ void ink_softmax_kernel(InkAttn a, int t0, int nk_max, float* norm) {
985988// as the score kernel reads a key; a lane holds one of the warp's weights, a head and a key each. The warps'
986989// sums are added in their order. value_warps() launches up to 32 warps, so the registers must fit 1,024 threads on
987990// every card: sm_86 and sm_89 gave this kernel 66 a thread without the bound, and the launch failed there.
991+ template <bool KV8 >
988992__global__ void __launch_bounds__ (1024 ) ink_value_kernel(InkAttn a, int t0, int nk_max, float * parts, int n_parts) {
989993 pdl_trigger (); // PDL (pdl.hpp): the launch after this one may start
990994 pdl_wait ();
@@ -1012,7 +1016,7 @@ __global__ void __launch_bounds__(1024) ink_value_kernel(InkAttn a, int t0, int
10121016#pragma unroll
10131017 for (int u = 0 ; u < kValueKeys ; ++u) {
10141018 if (j0 + u < j1) {
1015- if (a. kv8 ) lane_fp8 (a.vals , row, a.n_kv , a.dim , g, lane, lh, v[u]);
1019+ if (KV8 ) lane_fp8 (a.vals , row, a.n_kv , a.dim , g, lane, lh, v[u]);
10161020 else lane_halves ((const __half*) a.vals + (size_t (row) * a.n_kv + g) * a.dim , lane, lh, v[u]);
10171021 if (++row == a.ring ) row = 0 ;
10181022 }
@@ -1632,7 +1636,8 @@ void ink_qkv(const InkQkv& a, void* stream) {
16321636 throw std::invalid_argument (" ink_qkv: heads of up to 128 values, 2 to 8 taps" );
16331637 QkvArgs g{a, 1 .0f / std::sqrt ((float ) ((a.kdyn ? 2 : 1 ) * a.taps + 1 )),
16341638 1 .0f / std::sqrt ((float ) ((a.vdyn ? 2 : 1 ) * a.taps + 1 ))};
1635- launch_pdl (ink_qkv_kernel, dim3 (2 * a.n_head + 2 * a.n_kv ), dim3 (128 ), 0 , cs (stream), g);
1639+ launch_pdl (a.kv8 ? ink_qkv_kernel<true > : ink_qkv_kernel<false >, dim3 (2 * a.n_head + 2 * a.n_kv ), dim3 (128 ), 0 ,
1640+ cs (stream), g);
16361641 launched (" ink_qkv" );
16371642}
16381643
@@ -1646,7 +1651,8 @@ void ink_clip(float* x, const float* scale, float thr, int dim, int heads, int r
16461651
16471652void ink_clips (const InkClips& c, void * stream) {
16481653 if (c.rows <= 0 ) return ;
1649- ink_clips_kernel<<<c.rows * (c.heads[0 ] + c.heads[1 ] + c.heads[2 ] + c.heads[3 ]), 128 , 0 , cs(stream)>>> (c);
1654+ auto * k = c.kv8 ? ink_clips_kernel<true > : ink_clips_kernel<false >;
1655+ k<<<c.rows * (c.heads[0 ] + c.heads[1 ] + c.heads[2 ] + c.heads[3 ]), 128 , 0 , cs(stream)>>> (c);
16501656 launched (" ink_clips" );
16511657}
16521658
@@ -1706,13 +1712,14 @@ void ink_attend(const InkAttn& a, void* stream) {
17061712 float * parts = norm + (size_t ) chunk * a.n_head ;
17071713 for (int t0 = 0 ; t0 < a.rows ; t0 += chunk) {
17081714 const int m = std::min (chunk, a.rows - t0);
1709- launch_pdl (ink_score_kernel, dim3 (m * a.n_kv , score_blocks), dim3 ( kScoreWarps * 32 ), 0 , cs (stream), a, t0 ,
1710- nk_max);
1715+ launch_pdl (a. kv8 ? ink_score_kernel< true > : ink_score_kernel< false > , dim3 (m * a.n_kv , score_blocks),
1716+ dim3 ( kScoreWarps * 32 ), 0 , cs (stream), a, t0, nk_max);
17111717 launched (" ink_score" );
17121718 launch_pdl (ink_softmax_kernel, dim3 (m * a.n_head ), dim3 (kAttnThreads ), 0 , cs (stream), a, t0, nk_max, norm);
17131719 launched (" ink_softmax" );
1714- launch_pdl (ink_value_kernel, dim3 (m * a.n_kv , n_parts), dim3 (warps * 32 ),
1715- (size_t ) warps * group * a.dim * sizeof (float ), cs (stream), a, t0, nk_max, parts, n_parts);
1720+ launch_pdl (a.kv8 ? ink_value_kernel<true > : ink_value_kernel<false >, dim3 (m * a.n_kv , n_parts),
1721+ dim3 (warps * 32 ), (size_t ) warps * group * a.dim * sizeof (float ), cs (stream), a, t0, nk_max, parts,
1722+ n_parts);
17161723 launched (" ink_value" );
17171724 launch_pdl (ink_join_kernel, dim3 (m * a.n_head ), dim3 (128 ), 0 , cs (stream), a, t0, parts, n_parts, norm);
17181725 launched (" ink_join" );
0 commit comments