okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-037-arc (5eaee81a4)86bed1c92

main
52.9 KiB945 linesraw
1#define DPCT_PROFILING_ENABLED
2#include <sycl/sycl.hpp>
3#include "strata/kprof.hpp"
4#include <dpct/dpct.hpp>
5#include "strata/sycl_queue.hpp"
6#include "strata/kernels/bf16_gemv.hpp"
7#include "s26_tsum.dp.hpp"
8#include "strata/kernels/bf16_bits.hpp"
9
10#include <cstdlib>
11#include <limits>
12#include <stdexcept>
13#include <string>
14#include <cmath>
15
16namespace strata::kernels {
17namespace {
18
19// The FP32-activation MMVF implementation below is adapted from llama.cpp
20// 3cf03257f219afbe7334045ff7c6a06ac68c627d, ggml/src/ggml-cuda/{mmvf.cu,common.cuh}.
21// Scope: ordinary contiguous BF16 matrix, one FP32 activation vector, no fusion/ids/channels.
22//
23// MIT License
24// Copyright (c) 2023-2026 The ggml authors
25//
26// Permission is hereby granted, free of charge, to any person obtaining a copy
27// of this software and associated documentation files (the "Software"), to deal
28// in the Software without restriction, including without limitation the rights
29// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
30// copies of the Software, and to permit persons to whom the Software is
31// furnished to do so, subject to the following conditions:
32//
33// The above copyright notice and this permission notice shall be included in all
34// copies or substantial portions of the Software.
35//
36// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
37// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
38// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
39// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
40// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
41// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
42// SOFTWARE.
43__dpct_inline__ float mmvf_warp_sum(float value) {
44#pragma unroll
45    for (int offset = 16; offset > 0; offset >>= 1)
46        /*
47        DPCT1108: '__shfl_xor_sync' was migrated with the experimental
48        feature masked sub_group function which may not be supported by all
49        compilers or runtimes. You may need to adjust the code.
50        */
51        value += dpct::experimental::permute_sub_group_by_xor(
52            0xffffffffu, sycl::ext::oneapi::this_work_item::get_sub_group(),
53            value, offset);
54    return value;
55}
56
57template <int BLOCK_SIZE>
58__dpct_inline__ void bf16_f32_mmvf_kernel(const float *__restrict__ x,
59                                          const uint16_t *__restrict__ w,
60                                          float *__restrict__ y, int n_in) {
61    auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item<3>();
62    const int t = item_ct1.get_local_id(2);
63    const uint16_t *row = w + (size_t)item_ct1.get_group(2) * n_in;
64    const uint32_t* weights2 = reinterpret_cast<const uint32_t*>(row);
65    const sycl::float2 *inputs2 = reinterpret_cast<const sycl::float2 *>(x);
66    auto &partials =
67        *sycl::ext::oneapi::group_local_memory_for_overwrite<float[32]>(
68            sycl::ext::oneapi::this_work_item::get_work_group<3>());
69    if (t < 32) partials[t] = 0.0f;
70    item_ct1.barrier(sycl::access::fence_space::local_space);
71    float acc = 0.0f;
72    for (int pair = t; pair < n_in / 2; pair += BLOCK_SIZE) {
73        const uint32_t weight = weights2[pair];
74        const sycl::float2 input = inputs2[pair];
75        // Match the two ordered multiply-adds in ggml_cuda_mad, not a pair sum followed by one add.
76        /*
77        DPCT1013: The rounding mode could not be specified and the
78        generated code may have different accuracy than the original code.
79        Verify the correctness. SYCL math built-in function rounding mode is
80        aligned with OpenCL C 1.2 standard.
81        */
82        acc = sycl::fma(f32_from_bf16((uint16_t)weight), input.x(), acc);
83        /*
84        DPCT1013: The rounding mode could not be specified and the
85        generated code may have different accuracy than the original code.
86        Verify the correctness. SYCL math built-in function rounding mode is
87        aligned with OpenCL C 1.2 standard.
88        */
89        acc =
90            sycl::fma(f32_from_bf16((uint16_t)(weight >> 16)), input.y(), acc);
91    }
92    acc = mmvf_warp_sum(acc);
93    if constexpr (BLOCK_SIZE > 32) {
94        // All lanes have the same reduced value; one store avoids a same-value shared-memory race.
95        if ((t & 31) == 0) partials[t / 32] = acc;
96        item_ct1.barrier(sycl::access::fence_space::local_space);
97        if (t < 32) acc = mmvf_warp_sum(partials[t]);
98    }
99    if (t == 0) y[item_ct1.get_group(2)] = acc;
100}
101
102// the same kernel for up to 8 activation rows - the weight row is read ONCE and every
103// token keeps its own accumulator with exactly the single-row kernel's order (pairs, two ordered FMAs, the same warp
104// and block reductions), so each output is bit-identical to a bf16_f32_mmvf_kernel launch of its own.
105template <int BLOCK_SIZE, int NT, bool EXACT_T = true>
106__dpct_inline__ void bf16_f32_mmvf_multi_kernel(
107    const float *__restrict__ x, int64_t ldx, const uint16_t *__restrict__ w,
108    float *__restrict__ y, int64_t ldy, int n_in, int n_tok) {
109    auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item<3>();
110    const int t = item_ct1.get_local_id(2);
111    const uint16_t *row = w + (size_t)item_ct1.get_group(2) * n_in;
112    const uint32_t* weights2 = reinterpret_cast<const uint32_t*>(row);
113    auto &partials =
114        *sycl::ext::oneapi::group_local_memory_for_overwrite<float[NT][32]>(
115            sycl::ext::oneapi::this_work_item::get_work_group<3>());
116    if (t < 32) {
117#pragma unroll
118        for (int k = 0; k < NT; ++k) partials[k][t] = 0.0f;
119    }
120    item_ct1.barrier(sycl::access::fence_space::local_space);
121    float acc[NT];
122#pragma unroll
123    for (int k = 0; k < NT; ++k) acc[k] = 0.0f;
124    for (int pair = t; pair < n_in / 2; pair += BLOCK_SIZE) {
125        /*
126        DPCT1098: The '*' expression is used instead of the __ldg call.
127        These two expressions do not provide the exact same functionality. Check
128        the generated code for potential precision and/or performance issues.
129        */
130        const uint32_t weight = *(weights2 + pair);
131        const float w0 = f32_from_bf16((uint16_t) weight), w1 = f32_from_bf16((uint16_t) (weight >> 16));
132#pragma unroll
133        for (int k = 0; k < NT; ++k) {
134            if (EXACT_T || k < n_tok) {
135                /*
136                DPCT1098: The '*' expression is used instead of the __ldg
137                call. These two expressions do not provide the exact same
138                functionality. Check the generated code for potential precision
139                and/or performance issues.
140                */
141                const sycl::float2 input =
142                    *(reinterpret_cast<const sycl::float2 *>(x +
143                                                             (size_t)k * ldx) +
144                      pair);
145                /*
146                DPCT1013: The rounding mode could not be specified and the
147                generated code may have different accuracy than the original
148                code. Verify the correctness. SYCL math built-in function
149                rounding mode is aligned with OpenCL C 1.2 standard.
150                */
151                acc[k] = sycl::fma(w0, input.x(), acc[k]);
152                /*
153                DPCT1013: The rounding mode could not be specified and the
154                generated code may have different accuracy than the original
155                code. Verify the correctness. SYCL math built-in function
156                rounding mode is aligned with OpenCL C 1.2 standard.
157                */
158                acc[k] = sycl::fma(w1, input.y(), acc[k]);
159            }
160        }
161    }
162#pragma unroll
163    for (int k = 0; k < NT; ++k)
164        if (EXACT_T || k < n_tok) acc[k] = mmvf_warp_sum(acc[k]);
165    if constexpr (BLOCK_SIZE > 32) {
166        if ((t & 31) == 0)
167#pragma unroll
168            for (int k = 0; k < NT; ++k)
169                if (EXACT_T || k < n_tok) partials[k][t / 32] = acc[k];
170        item_ct1.barrier(sycl::access::fence_space::local_space);
171        if (t < 32)
172#pragma unroll
173            for (int k = 0; k < NT; ++k)
174                if (EXACT_T || k < n_tok) acc[k] = mmvf_warp_sum(partials[k][t]);
175    }
176    if (t == 0)
177#pragma unroll
178        for (int k = 0; k < NT; ++k)
179            if (EXACT_T || k < n_tok)
180                y[(size_t)k * ldy + item_ct1.get_group(2)] = acc[k];
181}
182
183// S25 (STRATA_MMVF_ROWS=1): RPB output rows per block. Each block read its row's weights once but all T activation
184// rows again (n_out blocks x T x n_in floats through L2 - the part that grew with T); here a block reads the
185// activations once for RPB rows. Per output, thread t still walks pairs t, t + BLOCK_SIZE, ... with the same two
186// ordered FMAs and the same warp and block reductions: bit-identical to bf16_f32_mmvf_multi_kernel.
187// S26 STRATA_LFUSE=1 (AUX): one more block computes row 0 of w_aux into y_aux[k * ldy_aux] - the same per-output
188// code, so each aux output is bitwise what its own bf16_f32_mmvf_multi_kernel launch gave (the shared expert's gate)
189template <int BLOCK_SIZE, int NT, int RPB, bool TS = false, bool AUX = false>
190/*
191DPCT1110: The total declared local variable size in device function
192bf16_f32_mmvf_rows_kernel exceeds 128 bytes and may cause high register
193pressure. Consult with your hardware vendor to find the total register size
194available and adjust the code, or use smaller sub-group size to avoid high
195register pressure.
196*/
197__dpct_inline__ void bf16_f32_mmvf_rows_kernel(
198    const float *__restrict__ x, int64_t ldx, const uint16_t *__restrict__ w,
199    float *__restrict__ y, int64_t ldy, int n_in, int n_out, int n_tok,
200    const uint16_t *__restrict__ w_aux = nullptr,
201    float *__restrict__ y_aux = nullptr, int64_t ldy_aux = 0) {
202    auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item<3>();
203    const int t = item_ct1.get_local_id(2);
204    int o0 = item_ct1.get_group(2) * RPB;
205    if constexpr (AUX) {
206        if (item_ct1.get_group(2) == item_ct1.get_group_range(2) - 1) {
207            w = w_aux; y = y_aux; ldy = ldy_aux; n_out = 1; o0 = 0;
208        }
209    }
210    auto &partials = *sycl::ext::oneapi::group_local_memory_for_overwrite<
211        float[RPB][NT][32]>(
212        sycl::ext::oneapi::this_work_item::get_work_group<3>());
213    if constexpr (BLOCK_SIZE > 32) {
214        if (t < 32)
215#pragma unroll
216            for (int r = 0; r < RPB; ++r)
217#pragma unroll
218                for (int k = 0; k < NT; ++k) partials[r][k][t] = 0.0f;
219        item_ct1.barrier(sycl::access::fence_space::local_space);
220    }
221    float acc[RPB][NT];
222#pragma unroll
223    for (int r = 0; r < RPB; ++r)
224#pragma unroll
225        for (int k = 0; k < NT; ++k) acc[r][k] = 0.0f;
226    for (int pair = t; pair < n_in / 2; pair += BLOCK_SIZE) {
227        sycl::float2 in[NT];
228#pragma unroll
229        for (int k = 0; k < NT; ++k)
230            if (k < n_tok) in[k] = reinterpret_cast<const sycl::float2 *>(
231                x + (size_t)k * ldx)[pair];
232#pragma unroll
233        for (int r = 0; r < RPB; ++r) {
234            if (o0 + r >= n_out) break;
235            const uint32_t weight = reinterpret_cast<const uint32_t*>(w + (size_t) (o0 + r) * n_in)[pair];
236            const float w0 = f32_from_bf16((uint16_t) weight), w1 = f32_from_bf16((uint16_t) (weight >> 16));
237#pragma unroll
238            for (int k = 0; k < NT; ++k) {
239                if (k < n_tok) {
240                    /*
241                    DPCT1013: The rounding mode could not be specified and
242                    the generated code may have different accuracy than the
243                    original code. Verify the correctness. SYCL math built-in
244                    function rounding mode is aligned with OpenCL C 1.2
245                    standard.
246                    */
247                    acc[r][k] = sycl::fma(w0, in[k].x(), acc[r][k]);
248                    /*
249                    DPCT1013: The rounding mode could not be specified and
250                    the generated code may have different accuracy than the
251                    original code. Verify the correctness. SYCL math built-in
252                    function rounding mode is aligned with OpenCL C 1.2
253                    standard.
254                    */
255                    acc[r][k] = sycl::fma(w1, in[k].y(), acc[r][k]);
256                }
257            }
258        }
259    }
260    if constexpr (TS) {   // S26 STRATA_TSUM=1: the RPB x NT warp sums (both stages) as transposed butterflies, bitwise the same
261        constexpr int V = RPB * NT, P = s26ts::pow2_ceil(V);
262        const int lane = t & 31, j = s26ts::tsum_token<P>(lane);
263        const bool own = lane == s26ts::tsum_lane<P>(j) && j < V;
264        float v[P];
265#pragma unroll
266        for (int q = 0; q < P; ++q) v[q] = q < V ? acc[q / NT][q % NT] : 0.0f;
267        float sum = s26ts::tsum<P>(v, lane);
268        if constexpr (BLOCK_SIZE > 32) {
269            if (own) partials[j / NT][j % NT][t / 32] = sum;
270            item_ct1.barrier(sycl::access::fence_space::local_space);
271            if (t >= 32) return;
272#pragma unroll
273            for (int q = 0; q < P; ++q) v[q] = q < V ? partials[q / NT][q % NT][t] : 0.0f;
274            sum = s26ts::tsum<P>(v, lane);
275        }
276        const int r = j / NT, k = j % NT;
277        if (own && o0 + r < n_out && k < n_tok) y[(size_t) k * ldy + o0 + r] = sum;
278        return;
279    }
280#pragma unroll
281    for (int r = 0; r < RPB; ++r)
282#pragma unroll
283        for (int k = 0; k < NT; ++k) acc[r][k] = mmvf_warp_sum(acc[r][k]);
284    if constexpr (BLOCK_SIZE > 32) {
285        if ((t & 31) == 0)
286#pragma unroll
287            for (int r = 0; r < RPB; ++r)
288#pragma unroll
289                for (int k = 0; k < NT; ++k) partials[r][k][t / 32] = acc[r][k];
290        /*
291        DPCT1064: Migrated __syncthreads call is used in a macro/template
292        definition and may not be valid for all macro/template uses. Adjust the
293        code.
294        */
295        /*
296        DPCT1065: Consider replacing sycl::nd_item::barrier() with
297        sycl::nd_item::barrier(sycl::access::fence_space::local_space) for
298        better performance if there is no access to global memory.
299        */
300        item_ct1.barrier();
301        if (t < 32)
302#pragma unroll
303            for (int r = 0; r < RPB; ++r)
304#pragma unroll
305                for (int k = 0; k < NT; ++k) acc[r][k] = mmvf_warp_sum(partials[r][k][t]);
306    }
307    if (t == 0)
308#pragma unroll
309        for (int r = 0; r < RPB; ++r)
310            if (o0 + r < n_out)
311#pragma unroll
312                for (int k = 0; k < NT; ++k)
313                    if (k < n_tok) y[(size_t) k * ldy + o0 + r] = acc[r][k];
314}
315
316template <int B>
317void launch_rows(const float *x, int64_t ldx, const uint16_t *w, float *y,
318                 int64_t ldy, int n_in, int n_out, int n_tok,
319                 dpct::queue_ptr st, const uint16_t *w_aux = nullptr,
320                 float *y_aux = nullptr, int64_t ldy_aux = 0) {
321    constexpr int RPB = 4;
322    const unsigned nb = (unsigned) ((n_out + RPB - 1) / RPB);
323    static const bool ts = [] { const char* v = std::getenv("STRATA_TSUM"); return v && v[0] == '1'; }();
324    if (w_aux != nullptr) {   // S26 STRATA_LFUSE: + one aux row block
325/*
326DPCT1049: The work-group size passed to the SYCL kernel may exceed the
327limit. To get the device limit, query info::device::max_work_group_size. Adjust
328the work-group size if needed.
329*/
330/*
331DPCT1049: The work-group size passed to the SYCL kernel may exceed the
332limit. To get the device limit, query info::device::max_work_group_size. Adjust
333the work-group size if needed.
334*/
335#define S26_AUX(TSV)                                                           \
336    if (n_tok <= 4) {                                                          \
337        auto exp_props = sycl::ext::oneapi::experimental::properties{          \
338            sycl::ext::oneapi::experimental::use_root_sync};                   \
339                                                                               \
340        strata::kprof::kp(st, __FILE__, __func__, __LINE__)->submit([&](sycl::handler &cgh) {                                   \
341            auto x_ct0 = x;                                                    \
342            auto ldx_ct1 = ldx;                                                \
343            auto w_ct2 = w;                                                    \
344            auto y_ct3 = y;                                                    \
345            auto ldy_ct4 = ldy;                                                \
346            auto n_in_ct5 = n_in;                                              \
347            auto n_out_ct6 = n_out;                                            \
348            auto n_tok_ct7 = n_tok;                                            \
349            auto w_aux_ct8 = w_aux;                                            \
350            auto y_aux_ct9 = y_aux;                                            \
351            auto ldy_aux_ct10 = ldy_aux;                                       \
352                                                                               \
353            cgh.parallel_for<dpct_kernel_name<                                 \
354                class bf16_f32_mmvf_rows_kernel_7c05e5, dpct_kernel_scalar<B>, \
355                dpct_kernel_scalar<4>, dpct_kernel_scalar<RPB>,                \
356                dpct_kernel_scalar<TSV>, dpct_kernel_scalar<true>>>(           \
357                sycl::nd_range<3>(sycl::range(1, 1, nb + 1) *                  \
358                                      sycl::range(1, 1, B),                    \
359                                  sycl::range(1, 1, B)),                       \
360                exp_props,                                                     \
361                [=](sycl::nd_item<3> item_ct1)                                 \
362                    [[sycl::reqd_sub_group_size(32)]] {                        \
363                        bf16_f32_mmvf_rows_kernel<B, 4, RPB, TSV, true>(       \
364                            x_ct0, ldx_ct1, w_ct2, y_ct3, ldy_ct4, n_in_ct5,   \
365                            n_out_ct6, n_tok_ct7, w_aux_ct8, y_aux_ct9,        \
366                            ldy_aux_ct10);                                     \
367                    });                                                        \
368        });                                                                    \
369    } else {                                                                   \
370        auto exp_props = sycl::ext::oneapi::experimental::properties{          \
371            sycl::ext::oneapi::experimental::use_root_sync};                   \
372                                                                               \
373        strata::kprof::kp(st, __FILE__, __func__, __LINE__)->submit([&](sycl::handler &cgh) {                                   \
374            auto x_ct0 = x;                                                    \
375            auto ldx_ct1 = ldx;                                                \
376            auto w_ct2 = w;                                                    \
377            auto y_ct3 = y;                                                    \
378            auto ldy_ct4 = ldy;                                                \
379            auto n_in_ct5 = n_in;                                              \
380            auto n_out_ct6 = n_out;                                            \
381            auto n_tok_ct7 = n_tok;                                            \
382            auto w_aux_ct8 = w_aux;                                            \
383            auto y_aux_ct9 = y_aux;                                            \
384            auto ldy_aux_ct10 = ldy_aux;                                       \
385                                                                               \
386            cgh.parallel_for<dpct_kernel_name<                                 \
387                class bf16_f32_mmvf_rows_kernel_681929, dpct_kernel_scalar<B>, \
388                dpct_kernel_scalar<8>, dpct_kernel_scalar<RPB>,                \
389                dpct_kernel_scalar<TSV>, dpct_kernel_scalar<true>>>(           \
390                sycl::nd_range<3>(sycl::range(1, 1, nb + 1) *                  \
391                                      sycl::range(1, 1, B),                    \
392                                  sycl::range(1, 1, B)),                       \
393                exp_props,                                                     \
394                [=](sycl::nd_item<3> item_ct1)                                 \
395                    [[sycl::reqd_sub_group_size(32)]] {                        \
396                        bf16_f32_mmvf_rows_kernel<B, 8, RPB, TSV, true>(       \
397                            x_ct0, ldx_ct1, w_ct2, y_ct3, ldy_ct4, n_in_ct5,   \
398                            n_out_ct6, n_tok_ct7, w_aux_ct8, y_aux_ct9,        \
399                            ldy_aux_ct10);                                     \
400                    });                                                        \
401        });                                                                    \
402    }
403        if (ts) { S26_AUX(true) } else { S26_AUX(false) }
404#undef S26_AUX
405        return;
406    }
407    if (ts) {
408        /*
409        DPCT1049: The work-group size passed to the SYCL kernel may exceed
410        the limit. To get the device limit, query
411        info::device::max_work_group_size. Adjust the work-group size if needed.
412        */
413        if (n_tok <= 4) {
414            auto exp_props = sycl::ext::oneapi::experimental::properties{
415                sycl::ext::oneapi::experimental::use_root_sync};
416
417            st->/*
418  DPCT1050: The template argument of the dpct_kernel_name could not be
419  deduced. You need to update this code.
420  */
421                parallel_for<dpct_kernel_name<
422                    class bf16_f32_mmvf_rows_kernel_ff3610,
423                    dpct_kernel_scalar<B>, dpct_kernel_scalar<4>,
424                    dpct_kernel_scalar<RPB>, dpct_kernel_scalar<true>,
425                    dpct_kernel_scalar<false>>>(
426                    sycl::nd_range<3>(sycl::range(1, 1, nb) *
427                                          sycl::range(1, 1, B),
428                                      sycl::range(1, 1, B)),
429                    exp_props,
430                    [=](sycl::nd_item<3> item_ct1)
431                        [[sycl::reqd_sub_group_size(32)]] {
432                            bf16_f32_mmvf_rows_kernel<B, 4, RPB, true>(
433                                x, ldx, w, y, ldy, n_in, n_out, n_tok);
434                        });
435        }
436        /*
437        DPCT1049: The work-group size passed to the SYCL kernel may exceed
438        the limit. To get the device limit, query
439        info::device::max_work_group_size. Adjust the work-group size if needed.
440        */
441        else {
442            auto exp_props = sycl::ext::oneapi::experimental::properties{
443                sycl::ext::oneapi::experimental::use_root_sync};
444
445            st->/*
446  DPCT1050: The template argument of the dpct_kernel_name could not be
447  deduced. You need to update this code.
448  */
449                parallel_for<dpct_kernel_name<
450                    class bf16_f32_mmvf_rows_kernel_1b1bb5,
451                    dpct_kernel_scalar<B>, dpct_kernel_scalar<8>,
452                    dpct_kernel_scalar<RPB>, dpct_kernel_scalar<true>,
453                    dpct_kernel_scalar<false>>>(
454                    sycl::nd_range<3>(sycl::range(1, 1, nb) *
455                                          sycl::range(1, 1, B),
456                                      sycl::range(1, 1, B)),
457                    exp_props,
458                    [=](sycl::nd_item<3> item_ct1)
459                        [[sycl::reqd_sub_group_size(32)]] {
460                            bf16_f32_mmvf_rows_kernel<B, 8, RPB, true>(
461                                x, ldx, w, y, ldy, n_in, n_out, n_tok);
462                        });
463        }
464        return;
465    }
466    /*
467    DPCT1049: The work-group size passed to the SYCL kernel may exceed the
468    limit. To get the device limit, query info::device::max_work_group_size.
469    Adjust the work-group size if needed.
470    */
471    if (n_tok <= 4) {
472        auto exp_props = sycl::ext::oneapi::experimental::properties{
473            sycl::ext::oneapi::experimental::use_root_sync};
474
475        st->/*
476  DPCT1050: The template argument of the dpct_kernel_name could not be
477  deduced. You need to update this code.
478  */
479            parallel_for<dpct_kernel_name<
480                class bf16_f32_mmvf_rows_kernel_7ff80d, dpct_kernel_scalar<B>,
481                dpct_kernel_scalar<4>, dpct_kernel_scalar<RPB>,
482                dpct_kernel_scalar<false>,
483                dpct_kernel_scalar<false>>>(
484                sycl::nd_range<3>(sycl::range(1, 1, nb) * sycl::range(1, 1, B),
485                                  sycl::range(1, 1, B)),
486                exp_props,
487                [=](sycl::nd_item<3> item_ct1)
488                    [[sycl::reqd_sub_group_size(32)]] {
489                        bf16_f32_mmvf_rows_kernel<B, 4, RPB>(
490                            x, ldx, w, y, ldy, n_in, n_out, n_tok);
491                    });
492    }
493    /*
494    DPCT1049: The work-group size passed to the SYCL kernel may exceed the
495    limit. To get the device limit, query info::device::max_work_group_size.
496    Adjust the work-group size if needed.
497    */
498    else {
499        auto exp_props = sycl::ext::oneapi::experimental::properties{
500            sycl::ext::oneapi::experimental::use_root_sync};
501
502        st->/*
503  DPCT1050: The template argument of the dpct_kernel_name could not be
504  deduced. You need to update this code.
505  */
506            parallel_for<dpct_kernel_name<
507                class bf16_f32_mmvf_rows_kernel_13db68, dpct_kernel_scalar<B>,
508                dpct_kernel_scalar<8>, dpct_kernel_scalar<RPB>,
509                dpct_kernel_scalar<false>,
510                dpct_kernel_scalar<false>>>(
511                sycl::nd_range<3>(sycl::range(1, 1, nb) * sycl::range(1, 1, B),
512                                  sycl::range(1, 1, B)),
513                exp_props,
514                [=](sycl::nd_item<3> item_ct1)
515                    [[sycl::reqd_sub_group_size(32)]] {
516                        bf16_f32_mmvf_rows_kernel<B, 8, RPB>(
517                            x, ldx, w, y, ldy, n_in, n_out, n_tok);
518                    });
519    }
520}
521
522int mmvf_block_size(int64_t n_in) {
523    int best = 32;
524    int64_t best_iterations = (n_in + 63) / 64;
525    for (int candidate = 64; candidate <= 256; candidate += 32) {
526        const int64_t iterations = (n_in + 2 * candidate - 1) / (2 * candidate);
527        if (iterations < best_iterations) {
528            best_iterations = iterations;
529            best = candidate;
530        }
531    }
532    return best;
533}
534
535}  // namespace
536
537bool bf16_gemv_fp32_mmvf_multi_aux(const float* x, int64_t ldx, const uint16_t* w, float* y, int64_t ldy,
538                                   int64_t n_in, int64_t n_out, int n_tok, const uint16_t* w_aux, float* y_aux,
539                                   int64_t ldy_aux, void* stream) {
540    static const bool rows = [] { const char* v = std::getenv("STRATA_MMVF_ROWS"); return v && v[0] == '1'; }();
541    // exactly the conditions under which both calls would take the rows / multi kernels at the same block size
542    if (!rows || n_out < 64 || n_tok < 2 || n_tok > 8 || n_in <= 0 || (n_in & 1) != 0 || (ldx & 1) != 0 || x == nullptr ||
543        w == nullptr || y == nullptr || w_aux == nullptr || y_aux == nullptr || (reinterpret_cast<uintptr_t>(x) & 7u) != 0)
544        return false;
545    const dpct::queue_ptr st = strata::q_of(stream);
546    const int ni = (int) n_in, no = (int) n_out;
547    switch (mmvf_block_size(n_in)) {
548        case 32: launch_rows<32>(x, ldx, w, y, ldy, ni, no, n_tok, st, w_aux, y_aux, ldy_aux); break;
549        case 64: launch_rows<64>(x, ldx, w, y, ldy, ni, no, n_tok, st, w_aux, y_aux, ldy_aux); break;
550        case 96: launch_rows<96>(x, ldx, w, y, ldy, ni, no, n_tok, st, w_aux, y_aux, ldy_aux); break;
551        case 128: launch_rows<128>(x, ldx, w, y, ldy, ni, no, n_tok, st, w_aux, y_aux, ldy_aux); break;
552        case 160: launch_rows<160>(x, ldx, w, y, ldy, ni, no, n_tok, st, w_aux, y_aux, ldy_aux); break;
553        case 192: launch_rows<192>(x, ldx, w, y, ldy, ni, no, n_tok, st, w_aux, y_aux, ldy_aux); break;
554        case 224: launch_rows<224>(x, ldx, w, y, ldy, ni, no, n_tok, st, w_aux, y_aux, ldy_aux); break;
555        default: launch_rows<256>(x, ldx, w, y, ldy, ni, no, n_tok, st, w_aux, y_aux, ldy_aux); break;
556    }
557    /*
558    DPCT1010: SYCL uses exceptions to report errors and does not use the
559    error codes. The cudaGetLastError function call was replaced with 0. You
560    need to rewrite this code.
561    */
562    const dpct::err0 result = 0;
563    /*
564    DPCT1000: Error handling if-stmt was detected but could not be
565    rewritten.
566    */
567    if (result != 0)
568        /*
569        DPCT1009: SYCL reports errors using exceptions and does not use
570        error codes. Please replace the "get_error_string_dummy(...)" with a
571        real error-handling function.
572        */
573        /*
574        DPCT1001: The statement could not be removed.
575        */
576        throw std::runtime_error(
577            std::string("bf16_gemv_fp32_mmvf_multi_aux launch: ") +
578            dpct::get_error_string_dummy(result));
579    return true;
580}
581
582void bf16_gemv_fp32_mmvf_multi(const float* x, int64_t ldx, const uint16_t* w, float* y, int64_t ldy,
583                               int64_t n_in, int64_t n_out, int n_tok, void* stream) {
584    if (n_tok == 1 && ldy >= n_out) { bf16_gemv_fp32_mmvf(x, w, y, n_in, n_out, stream); return; }
585    if (n_tok < 1 || n_tok > 8 || n_in <= 0 || (n_in & 1) != 0 || n_out <= 0 || (ldx & 1) != 0 || x == nullptr ||
586        w == nullptr || y == nullptr || (reinterpret_cast<uintptr_t>(x) & 7u) != 0)
587        throw std::invalid_argument("bf16_gemv_fp32_mmvf_multi: 1..8 rows, even n_in/ldx, aligned pointers");
588    const dpct::queue_ptr st = strata::q_of(stream);
589    static const bool rows = [] { const char* v = std::getenv("STRATA_MMVF_ROWS"); return v && v[0] == '1'; }();
590    if (rows && n_out >= 64) {
591        const int ni = (int) n_in, no = (int) n_out;
592        switch (mmvf_block_size(n_in)) {
593            case 32: launch_rows<32>(x, ldx, w, y, ldy, ni, no, n_tok, st); break;
594            case 64: launch_rows<64>(x, ldx, w, y, ldy, ni, no, n_tok, st); break;
595            case 96: launch_rows<96>(x, ldx, w, y, ldy, ni, no, n_tok, st); break;
596            case 128: launch_rows<128>(x, ldx, w, y, ldy, ni, no, n_tok, st); break;
597            case 160: launch_rows<160>(x, ldx, w, y, ldy, ni, no, n_tok, st); break;
598            case 192: launch_rows<192>(x, ldx, w, y, ldy, ni, no, n_tok, st); break;
599            case 224: launch_rows<224>(x, ldx, w, y, ldy, ni, no, n_tok, st); break;
600            default: launch_rows<256>(x, ldx, w, y, ldy, ni, no, n_tok, st); break;
601        }
602        /*
603        DPCT1010: SYCL uses exceptions to report errors and does not use
604        the error codes. The cudaGetLastError function call was replaced with 0.
605        You need to rewrite this code.
606        */
607        const dpct::err0 result = 0;
608        /*
609        DPCT1000: Error handling if-stmt was detected but could not be
610        rewritten.
611        */
612        if (result != 0)
613            /*
614            DPCT1009: SYCL reports errors using exceptions and does not use
615            error codes. Please replace the "get_error_string_dummy(...)" with a
616            real error-handling function.
617            */
618            /*
619            DPCT1001: The statement could not be removed.
620            */
621            throw std::runtime_error(
622                std::string("bf16_gemv_fp32_mmvf_multi launch: ") +
623                dpct::get_error_string_dummy(result));
624        return;
625    }
626#define STRATA_MMVF_M(N)                                                       \
627    case N:                                                                    \
628        switch (n_tok) {                                                       \
629        case 1: {                                                              \
630            auto exp_props = sycl::ext::oneapi::experimental::properties{      \
631                sycl::ext::oneapi::experimental::use_root_sync};               \
632                                                                               \
633            strata::kprof::kp(st, __FILE__, __func__, __LINE__)->submit([&](sycl::handler &cgh) {                               \
634                auto x_ct0 = x;                                                \
635                auto ldx_ct1 = ldx;                                            \
636                auto w_ct2 = w;                                                \
637                auto y_ct3 = y;                                                \
638                auto ldy_ct4 = ldy;                                            \
639                auto n_in_ct5 = (int)n_in;                                     \
640                auto n_tok_ct6 = n_tok;                                        \
641                                                                               \
642                cgh.parallel_for<dpct_kernel_name<                             \
643                    class bf16_f32_mmvf_multi_kernel_27194f,                   \
644                    dpct_kernel_scalar<N>, dpct_kernel_scalar<1>,              \
645                    dpct_kernel_scalar<true>>>(                                \
646                    sycl::nd_range<3>(sycl::range(1, 1, (unsigned)n_out) *     \
647                                          sycl::range(1, 1, N),                \
648                                      sycl::range(1, 1, N)),                   \
649                    exp_props,                                                 \
650                    [=](sycl::nd_item<3> item_ct1)                             \
651                        [[sycl::reqd_sub_group_size(32)]] {                    \
652                            bf16_f32_mmvf_multi_kernel<N, 1, true>(            \
653                                x_ct0, ldx_ct1, w_ct2, y_ct3, ldy_ct4,         \
654                                n_in_ct5, n_tok_ct6);                          \
655                        });                                                    \
656            });                                                                \
657        } break;                                                               \
658        case 2: {                                                              \
659            auto exp_props = sycl::ext::oneapi::experimental::properties{      \
660                sycl::ext::oneapi::experimental::use_root_sync};               \
661                                                                               \
662            strata::kprof::kp(st, __FILE__, __func__, __LINE__)->submit([&](sycl::handler &cgh) {                               \
663                auto x_ct0 = x;                                                \
664                auto ldx_ct1 = ldx;                                            \
665                auto w_ct2 = w;                                                \
666                auto y_ct3 = y;                                                \
667                auto ldy_ct4 = ldy;                                            \
668                auto n_in_ct5 = (int)n_in;                                     \
669                auto n_tok_ct6 = n_tok;                                        \
670                                                                               \
671                cgh.parallel_for<dpct_kernel_name<                             \
672                    class bf16_f32_mmvf_multi_kernel_ded48c,                   \
673                    dpct_kernel_scalar<N>, dpct_kernel_scalar<2>,              \
674                    dpct_kernel_scalar<true>>>(                                \
675                    sycl::nd_range<3>(sycl::range(1, 1, (unsigned)n_out) *     \
676                                          sycl::range(1, 1, N),                \
677                                      sycl::range(1, 1, N)),                   \
678                    exp_props,                                                 \
679                    [=](sycl::nd_item<3> item_ct1)                             \
680                        [[sycl::reqd_sub_group_size(32)]] {                    \
681                            bf16_f32_mmvf_multi_kernel<N, 2, true>(            \
682                                x_ct0, ldx_ct1, w_ct2, y_ct3, ldy_ct4,         \
683                                n_in_ct5, n_tok_ct6);                          \
684                        });                                                    \
685            });                                                                \
686        } break;                                                               \
687        case 3: {                                                              \
688            auto exp_props = sycl::ext::oneapi::experimental::properties{      \
689                sycl::ext::oneapi::experimental::use_root_sync};               \
690                                                                               \
691            strata::kprof::kp(st, __FILE__, __func__, __LINE__)->submit([&](sycl::handler &cgh) {                               \
692                auto x_ct0 = x;                                                \
693                auto ldx_ct1 = ldx;                                            \
694                auto w_ct2 = w;                                                \
695                auto y_ct3 = y;                                                \
696                auto ldy_ct4 = ldy;                                            \
697                auto n_in_ct5 = (int)n_in;                                     \
698                auto n_tok_ct6 = n_tok;                                        \
699                                                                               \
700                cgh.parallel_for<dpct_kernel_name<                             \
701                    class bf16_f32_mmvf_multi_kernel_a6c94f,                   \
702                    dpct_kernel_scalar<N>, dpct_kernel_scalar<3>,              \
703                    dpct_kernel_scalar<true>>>(                                \
704                    sycl::nd_range<3>(sycl::range(1, 1, (unsigned)n_out) *     \
705                                          sycl::range(1, 1, N),                \
706                                      sycl::range(1, 1, N)),                   \
707                    exp_props,                                                 \
708                    [=](sycl::nd_item<3> item_ct1)                             \
709                        [[sycl::reqd_sub_group_size(32)]] {                    \
710                            bf16_f32_mmvf_multi_kernel<N, 3, true>(            \
711                                x_ct0, ldx_ct1, w_ct2, y_ct3, ldy_ct4,         \
712                                n_in_ct5, n_tok_ct6);                          \
713                        });                                                    \
714            });                                                                \
715        } break;                                                               \
716        case 4: {                                                              \
717            auto exp_props = sycl::ext::oneapi::experimental::properties{      \
718                sycl::ext::oneapi::experimental::use_root_sync};               \
719                                                                               \
720            strata::kprof::kp(st, __FILE__, __func__, __LINE__)->submit([&](sycl::handler &cgh) {                               \
721                auto x_ct0 = x;                                                \
722                auto ldx_ct1 = ldx;                                            \
723                auto w_ct2 = w;                                                \
724                auto y_ct3 = y;                                                \
725                auto ldy_ct4 = ldy;                                            \
726                auto n_in_ct5 = (int)n_in;                                     \
727                auto n_tok_ct6 = n_tok;                                        \
728                                                                               \
729                cgh.parallel_for<dpct_kernel_name<                             \
730                    class bf16_f32_mmvf_multi_kernel_eba2ed,                   \
731                    dpct_kernel_scalar<N>, dpct_kernel_scalar<4>,              \
732                    dpct_kernel_scalar<true>>>(                                \
733                    sycl::nd_range<3>(sycl::range(1, 1, (unsigned)n_out) *     \
734                                          sycl::range(1, 1, N),                \
735                                      sycl::range(1, 1, N)),                   \
736                    exp_props,                                                 \
737                    [=](sycl::nd_item<3> item_ct1)                             \
738                        [[sycl::reqd_sub_group_size(32)]] {                    \
739                            bf16_f32_mmvf_multi_kernel<N, 4, true>(            \
740                                x_ct0, ldx_ct1, w_ct2, y_ct3, ldy_ct4,         \
741                                n_in_ct5, n_tok_ct6);                          \
742                        });                                                    \
743            });                                                                \
744        } break;                                                               \
745        case 5: {                                                              \
746            auto exp_props = sycl::ext::oneapi::experimental::properties{      \
747                sycl::ext::oneapi::experimental::use_root_sync};               \
748                                                                               \
749            strata::kprof::kp(st, __FILE__, __func__, __LINE__)->submit([&](sycl::handler &cgh) {                               \
750                auto x_ct0 = x;                                                \
751                auto ldx_ct1 = ldx;                                            \
752                auto w_ct2 = w;                                                \
753                auto y_ct3 = y;                                                \
754                auto ldy_ct4 = ldy;                                            \
755                auto n_in_ct5 = (int)n_in;                                     \
756                auto n_tok_ct6 = n_tok;                                        \
757                                                                               \
758                cgh.parallel_for<dpct_kernel_name<                             \
759                    class bf16_f32_mmvf_multi_kernel_3318a1,                   \
760                    dpct_kernel_scalar<N>, dpct_kernel_scalar<5>,              \
761                    dpct_kernel_scalar<true>>>(                                \
762                    sycl::nd_range<3>(sycl::range(1, 1, (unsigned)n_out) *     \
763                                          sycl::range(1, 1, N),                \
764                                      sycl::range(1, 1, N)),                   \
765                    exp_props,                                                 \
766                    [=](sycl::nd_item<3> item_ct1)                             \
767                        [[sycl::reqd_sub_group_size(32)]] {                    \
768                            bf16_f32_mmvf_multi_kernel<N, 5, true>(            \
769                                x_ct0, ldx_ct1, w_ct2, y_ct3, ldy_ct4,         \
770                                n_in_ct5, n_tok_ct6);                          \
771                        });                                                    \
772            });                                                                \
773        } break;                                                               \
774        case 6: {                                                              \
775            auto exp_props = sycl::ext::oneapi::experimental::properties{      \
776                sycl::ext::oneapi::experimental::use_root_sync};               \
777                                                                               \
778            strata::kprof::kp(st, __FILE__, __func__, __LINE__)->submit([&](sycl::handler &cgh) {                               \
779                auto x_ct0 = x;                                                \
780                auto ldx_ct1 = ldx;                                            \
781                auto w_ct2 = w;                                                \
782                auto y_ct3 = y;                                                \
783                auto ldy_ct4 = ldy;                                            \
784                auto n_in_ct5 = (int)n_in;                                     \
785                auto n_tok_ct6 = n_tok;                                        \
786                                                                               \
787                cgh.parallel_for<dpct_kernel_name<                             \
788                    class bf16_f32_mmvf_multi_kernel_eddebc,                   \
789                    dpct_kernel_scalar<N>, dpct_kernel_scalar<6>,              \
790                    dpct_kernel_scalar<true>>>(                                \
791                    sycl::nd_range<3>(sycl::range(1, 1, (unsigned)n_out) *     \
792                                          sycl::range(1, 1, N),                \
793                                      sycl::range(1, 1, N)),                   \
794                    exp_props,                                                 \
795                    [=](sycl::nd_item<3> item_ct1)                             \
796                        [[sycl::reqd_sub_group_size(32)]] {                    \
797                            bf16_f32_mmvf_multi_kernel<N, 6, true>(            \
798                                x_ct0, ldx_ct1, w_ct2, y_ct3, ldy_ct4,         \
799                                n_in_ct5, n_tok_ct6);                          \
800                        });                                                    \
801            });                                                                \
802        } break;                                                               \
803        default: {                                                             \
804            auto exp_props = sycl::ext::oneapi::experimental::properties{      \
805                sycl::ext::oneapi::experimental::use_root_sync};               \
806                                                                               \
807            strata::kprof::kp(st, __FILE__, __func__, __LINE__)->submit([&](sycl::handler &cgh) {                               \
808                auto x_ct0 = x;                                                \
809                auto ldx_ct1 = ldx;                                            \
810                auto w_ct2 = w;                                                \
811                auto y_ct3 = y;                                                \
812                auto ldy_ct4 = ldy;                                            \
813                auto n_in_ct5 = (int)n_in;                                     \
814                auto n_tok_ct6 = n_tok;                                        \
815                                                                               \
816                cgh.parallel_for<dpct_kernel_name<                             \
817                    class bf16_f32_mmvf_multi_kernel_b73f0b,                   \
818                    dpct_kernel_scalar<N>, dpct_kernel_scalar<8>,              \
819                    dpct_kernel_scalar<false>>>(                               \
820                    sycl::nd_range<3>(sycl::range(1, 1, (unsigned)n_out) *     \
821                                          sycl::range(1, 1, N),                \
822                                      sycl::range(1, 1, N)),                   \
823                    exp_props,                                                 \
824                    [=](sycl::nd_item<3> item_ct1)                             \
825                        [[sycl::reqd_sub_group_size(32)]] {                    \
826                            bf16_f32_mmvf_multi_kernel<N, 8, false>(           \
827                                x_ct0, ldx_ct1, w_ct2, y_ct3, ldy_ct4,         \
828                                n_in_ct5, n_tok_ct6);                          \
829                        });                                                    \
830            });                                                                \
831        } break;                                                               \
832        } break
833    switch (mmvf_block_size(n_in)) {
834        STRATA_MMVF_M(32); STRATA_MMVF_M(64); STRATA_MMVF_M(96); STRATA_MMVF_M(128);
835        STRATA_MMVF_M(160); STRATA_MMVF_M(192); STRATA_MMVF_M(224); STRATA_MMVF_M(256);
836    }
837#undef STRATA_MMVF_M
838    /*
839    DPCT1010: SYCL uses exceptions to report errors and does not use the
840    error codes. The cudaGetLastError function call was replaced with 0. You
841    need to rewrite this code.
842    */
843    const dpct::err0 result = 0;
844    /*
845    DPCT1000: Error handling if-stmt was detected but could not be
846    rewritten.
847    */
848    if (result != 0)
849        /*
850        DPCT1009: SYCL reports errors using exceptions and does not use
851        error codes. Please replace the "get_error_string_dummy(...)" with a
852        real error-handling function.
853        */
854        /*
855        DPCT1001: The statement could not be removed.
856        */
857        throw std::runtime_error(
858            std::string("bf16_gemv_fp32_mmvf_multi launch: ") +
859            dpct::get_error_string_dummy(result));
860}
861
862void bf16_gemv_fp32_mmvf(const float* x, const uint16_t* w, float* y,
863                         int64_t n_in, int64_t n_out, void* stream) {
864    if (n_in <= 0 || (n_in & 1) != 0 || n_in > std::numeric_limits<int>::max() ||
865        n_out <= 0 || n_out > std::numeric_limits<int>::max())
866        throw std::invalid_argument("bf16_gemv_fp32_mmvf: require positive even n_in and positive n_out <= INT_MAX");
867    if (x == nullptr || w == nullptr || y == nullptr ||
868        (reinterpret_cast<uintptr_t>(x) & 7u) != 0 ||
869        (reinterpret_cast<uintptr_t>(w) & 3u) != 0 ||
870        (reinterpret_cast<uintptr_t>(y) & 3u) != 0)
871        throw std::invalid_argument("bf16_gemv_fp32_mmvf: null or misaligned pointer");
872    const dpct::queue_ptr st = strata::q_of(stream);
873#define STRATA_MMVF_CASE(N)                                                    \
874    case N: {                                                                  \
875        auto exp_props = sycl::ext::oneapi::experimental::properties{          \
876            sycl::ext::oneapi::experimental::use_root_sync};                   \
877                                                                               \
878        strata::kprof::kp(st, __FILE__, __func__, __LINE__)->submit([&](sycl::handler &cgh) {                                   \
879            auto x_ct0 = x;                                                    \
880            auto w_ct1 = w;                                                    \
881            auto y_ct2 = y;                                                    \
882            auto n_in_ct3 = (int)n_in;                                         \
883                                                                               \
884            cgh.parallel_for<dpct_kernel_name<                                 \
885                class bf16_f32_mmvf_kernel_2903a5, dpct_kernel_scalar<N>>>(    \
886                sycl::nd_range<3>(sycl::range(1, 1, (unsigned)n_out) *         \
887                                      sycl::range(1, 1, N),                    \
888                                  sycl::range(1, 1, N)),                       \
889                exp_props,                                                     \
890                [=](sycl::nd_item<3> item_ct1)                                 \
891                    [[sycl::reqd_sub_group_size(32)]] {                        \
892                        bf16_f32_mmvf_kernel<N>(x_ct0, w_ct1, y_ct2,           \
893                                                n_in_ct3);                     \
894                    });                                                        \
895        });                                                                    \
896    } break
897    switch (mmvf_block_size(n_in)) {
898        STRATA_MMVF_CASE(32);
899        STRATA_MMVF_CASE(64);
900        STRATA_MMVF_CASE(96);
901        STRATA_MMVF_CASE(128);
902        STRATA_MMVF_CASE(160);
903        STRATA_MMVF_CASE(192);
904        STRATA_MMVF_CASE(224);
905        STRATA_MMVF_CASE(256);
906    }
907#undef STRATA_MMVF_CASE
908    /*
909    DPCT1010: SYCL uses exceptions to report errors and does not use the
910    error codes. The cudaGetLastError function call was replaced with 0. You
911    need to rewrite this code.
912    */
913    const dpct::err0 result = 0;
914    /*
915    DPCT1000: Error handling if-stmt was detected but could not be
916    rewritten.
917    */
918    if (result != 0)
919        /*
920        DPCT1009: SYCL reports errors using exceptions and does not use
921        error codes. Please replace the "get_error_string_dummy(...)" with a
922        real error-handling function.
923        */
924        /*
925        DPCT1001: The statement could not be removed.
926        */
927        throw std::runtime_error(std::string("bf16_gemv_fp32_mmvf launch: ") +
928                                 dpct::get_error_string_dummy(result));
929}
930
931
932
933void bf16_gemv_fp32_mmvf_cols(const float* x, const uint16_t* w, float* y, int64_t n_in, int64_t n_out, int ncols,
934                              void* stream) {
935    // columns contiguous: the existing multi-row kernel (weight read once, each output bitwise its one-row call),
936    // up to 8 rows per launch
937    for (int c0 = 0; c0 < ncols; c0 += 8) {
938        const int nc = ncols - c0 < 8 ? ncols - c0 : 8;
939        if (nc == 1) bf16_gemv_fp32_mmvf(x + (size_t) c0 * n_in, w, y + (size_t) c0 * n_out, n_in, n_out, stream);
940        else bf16_gemv_fp32_mmvf_multi(x + (size_t) c0 * n_in, n_in, w, y + (size_t) c0 * n_out, n_out, n_in, n_out, nc,
941                                       stream);
942    }
943}
944
945}  // namespace strata::kernels