okami.mom/strata

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

Niko1221Merge f-latency into f-144-rel0b8d31013

main
22.8 KiB375 linesraw
1// Adapted from llama.cpp 3cf03257f219afbe7334045ff7c6a06ac68c627d:
2// src/models/qwen4exp.cpp; ggml/src/ggml-cuda/{set-rows.cu,norm.cu,rope.cu}.
3// MIT License
4// Copyright (c) 2023-2026 The ggml authors
5// Permission is hereby granted, free of charge, to any person obtaining a copy
6// of this software and associated documentation files (the "Software"), to deal
7// in the Software without restriction, including without limitation the rights
8// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9// copies of the Software, and to permit persons to whom the Software is
10// furnished to do so, subject to the following conditions:
11// The above copyright notice and this permission notice shall be included in all
12// copies or substantial portions of the Software.
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19// SOFTWARE.
20
21#include "strata/kernels/native_qsa_indexer.hpp"
22#include "strata/core/geometry.hpp"
23#include "strata/kernels/mrope.hpp"
24#include <cuda_runtime.h>
25#include <cuda_fp16.h>
26#include <atomic>
27#include <cmath>
28#include <cstddef>
29#include <cstdint>
30#include <limits>
31#include <stdexcept>
32
33namespace strata::kernels {
34namespace {
35std::atomic<bool> enabled{false};
36constexpr int D = 128, R = 4, ROT = 64, THREADS = 256;
37static_assert(D == strata::core::kCompiledGeometry.idx_key_dim && R == strata::core::kCompiledGeometry.idx_block && ROT == strata::core::kCompiledGeometry.n_rot,
38              "native_qsa_indexer is compiled for core/geometry.hpp");
39__device__ float warp_sum(float x) {
40#pragma unroll
41    for (int offset = 16; offset; offset >>= 1)
42        x += __shfl_xor_sync(0xffffffffu, x, offset);
43    return x;
44}
45// TAB (#280, STRATA_ROPE_TABLE=1): the angles from the session's float64 table.  The host launches <false> whenever
46// no table applies - the default - so the default kernel is 0.1.31's code exactly (the table read is not in it;
47// with it merely skipped at run time, the compiled default path changed its results).
48template <bool TAB>
49__device__ __forceinline__ void append_body(const float* __restrict__ raw, const int32_t* __restrict__ pos_dev,
50                        int pos_stride, int n_steps,
51                        int pos_base, const float* __restrict__ gamma, float epsilon,
52                        float* __restrict__ tail, float* __restrict__ dead,
53                        float* __restrict__ pooled, int32_t* __restrict__ block_pos,
54                        int max_cells, float theta_scale, float freq_scale, float corr_low,
55                        float corr_high, float ext_factor, float mscale,
56                        const int32_t* __restrict__ mtab, const RopeTab& rt) {
57    __shared__ float values[D];
58    __shared__ float partials[32];
59    const int d = threadIdx.x;
60    for (int step_idx = 0; step_idx < n_steps; ++step_idx) {
61        const int pos = pos_dev[(std::size_t) step_idx * pos_stride];
62        if (pos >= 0 && pos < max_cells) {
63            const float* raw_step = raw + (std::size_t) step_idx * D;
64            const int slot = pos % R;
65            float incoming = 0.0f;
66            if (d < D) {
67                // SET_ROWS stores F16; GET_ROWS expands those exact values to F32.
68                incoming = __half2float(__float2half_rn(raw_step[d]));
69                if (slot < R - 1) tail[slot * D + d] = incoming;
70            }
71            if (pos == 0 || slot == R - 1) {
72                float mean = 0.0f;
73                if (d < D) {
74                    // The spare's four gather indices all name cell zero. Completed blocks
75                    // use chronological slices; each graph ADD materializes an F32 sum.
76                    float sum = pos == 0 ? incoming : tail[d];
77#pragma unroll
78                    for (int j = 1; j < R; ++j)
79                        sum = __fadd_rn(sum, pos == 0 || j == R - 1 ? incoming : tail[j * D + d]);
80                    mean = __fmaf_rn(0.25f, sum, 0.0f); // SCALE includes a zero bias.
81                }
82                float square_sum = 0.0f;
83                if (d < D) square_sum += mean * mean;
84                square_sum = warp_sum(square_sum);
85                const int lane = d % 32;
86                if (lane == 0) partials[d / 32] = square_sum;
87                __syncthreads();
88                square_sum = lane < THREADS / 32 ? partials[lane] : 0.0f;
89                square_sum = warp_sum(square_sum);
90                const float scale = rsqrtf(square_sum / D + epsilon);
91                if (d < D) values[d] = scale * mean * gamma[d];
92                __syncthreads();
93                if (d < D) {
94                    const int b = pos / R;
95                    const int rope_pos = pos == 0 ? 0 : pos_base + R * b;
96                    float y = values[d];
97                    if (d < ROT) {
98                        const int pair = d % (ROT / 2);
99                        // The spare (pos == 0) keeps its zero angle; under YaRN the mscale magnitude still rides in
100                        // through cos(0) - which is exactly what the queries are scaled by, so the top-k is unmoved.
101                        float c, s;
102                        if (!(TAB && rope_tab_cs(rt, pos == 0 ? 0 : mrope_pos(mtab, rope_pos, pair), pair, c, s))) {
103                            const float theta_extrap = (pos == 0 ? 0 : mrope_pos(mtab, rope_pos, pair)) * powf(theta_scale, float(pair));
104                            rope_scaled_angle(theta_extrap, freq_scale, corr_low, corr_high, ext_factor, mscale, pair, c, s);
105                        }
106                        const float a = values[pair], z = values[pair + ROT / 2];
107                        y = d < ROT / 2 ? a * c - z * s : a * s + z * c;
108                    }
109                    pooled[std::size_t(b) * D + d] = y;
110                    if (pos == 0) dead[d] = y;
111                    else pooled[std::size_t(b + 1) * D + d] = dead[d];
112                    if (d == 0 && pos != 0) *block_pos = rope_pos;
113                }
114            }
115        }
116        if (n_steps > 1) __syncthreads();
117    }
118}
119template <bool TAB>
120__global__ void append(const float* __restrict__ raw, const int32_t* __restrict__ pos_dev,
121                        int pos_stride, int n_steps,
122                        int pos_base, const float* __restrict__ gamma, float epsilon,
123                        float* __restrict__ tail, float* __restrict__ dead,
124                        float* __restrict__ pooled, int32_t* __restrict__ block_pos,
125                        int max_cells, float theta_scale, float freq_scale, float corr_low,
126                        float corr_high, float ext_factor, float mscale,
127                        const int32_t* __restrict__ mtab, RopeTab rt) {
128    append_body<TAB>(raw, pos_dev, pos_stride, n_steps, pos_base, gamma, epsilon, tail, dead, pooled, block_pos, max_cells,
129                     theta_scale, freq_scale, corr_low, corr_high, ext_factor, mscale, mtab, rt);
130}
131// engine-latency: the commit's appends of several QSA layers in one launch, one block per layer (blockIdx.x); each block
132// first restores its layer's tail from the window's snapshot (the commit's copy_from_mapped), then runs append's body
133template <bool TAB>
134__global__ void append_layers(const float* __restrict__ raw0, long long raw_stride, const int32_t* __restrict__ pos_dev,
135                              int pos_stride, int n_steps, int pos_base, float epsilon, QsaIndexerLayers L, int max_cells,
136                              float theta_scale, float freq_scale, float corr_low, float corr_high, float ext_factor,
137                              float mscale, const int32_t* __restrict__ mtab, RopeTab rt) {
138    const int i = blockIdx.x;
139    float* tail = L.tail[i];
140    if (L.tail_src[i] != nullptr) {
141        for (int j = threadIdx.x; j < (R - 1) * D; j += blockDim.x) tail[j] = L.tail_src[i][j];
142        __syncthreads();
143    }
144    append_body<TAB>(raw0 + (long long) i * raw_stride, pos_dev, pos_stride, n_steps, pos_base, L.gamma[i], epsilon, tail,
145                     L.dead[i], L.pooled[i], L.block_pos[i], max_cells, theta_scale, freq_scale, corr_low, corr_high,
146                     ext_factor, mscale, mtab, rt);
147}
148// ---- C-2: the batched append.  The pooled key of a completed block b (its last cell pos = 4b+3), computed with
149// the single append's arithmetic: keys rounded through F16, summed tail[0]+tail[1]+tail[2]+incoming in that order,
150// the RMS norm, gamma, the rotation at pos_base + 4b.  `k(j)` is the key of the block's cell j: a row of this batch
151// (cell >= p0) or the tail the previous batch left (cell < p0).  Only the batch's last completed block writes the
152// spare after it (every earlier one's is overwritten by the next block in order).
153template <bool TAB>
154__device__ __forceinline__ float pooled_value(const float* values, int d, int rope_pos, float theta_scale,
155                                              float freq_scale, float corr_low, float corr_high,
156                                              float ext_factor, float mscale, const int32_t* mtab, bool zero_pos,
157                                              const RopeTab& rt) {
158    float y = values[d];
159    if (d < ROT) {
160        const int pair = d % (ROT / 2);
161        // The same rotation the single append applies, rope_scaling.hpp's helper shared: under none this is
162        // today's cosf/sinf; under linear/YaRN the pooled keys move with the cache's K, and the spare's zero
163        // angle carries the mscale magnitude through cos(0) exactly as the single append's does.
164        float c, s;
165        if (!(TAB && rope_tab_cs(rt, zero_pos ? 0 : mrope_pos(mtab, rope_pos, pair), pair, c, s))) {
166            const float theta_extrap = (zero_pos ? 0 : mrope_pos(mtab, rope_pos, pair)) * powf(theta_scale, float(pair));
167            rope_scaled_angle(theta_extrap, freq_scale, corr_low, corr_high, ext_factor, mscale, pair, c, s);
168        }
169        const float a = values[pair], z = values[pair + ROT / 2];
170        y = d < ROT / 2 ? a * c - z * s : a * s + z * c;
171    }
172    return y;
173}
174__device__ __forceinline__ float norm_scale(float mean, float* partials, int d) {
175    float square_sum = d < D ? mean * mean : 0.0f;
176    square_sum = warp_sum(square_sum);
177    const int lane = d % 32;
178    if (lane == 0) partials[d / 32] = square_sum;
179    __syncthreads();
180    square_sum = lane < THREADS / 32 ? partials[lane] : 0.0f;
181    square_sum = warp_sum(square_sum);
182    return square_sum;
183}
184// cell 0 of a sequence: the spare (every gather index names cell 0), written to pooled[0] and dead
185template <bool TAB>
186__global__ void append_first(const float* __restrict__ raw, const float* __restrict__ gamma, float epsilon,
187                             float* __restrict__ dead, float* __restrict__ pooled, float theta_scale,
188                             float freq_scale, float corr_low, float corr_high, float ext_factor, float mscale,
189                             const int32_t* __restrict__ mtab, RopeTab rt) {
190    const int d = threadIdx.x;
191    __shared__ float values[D];
192    __shared__ float partials[32];
193    float mean = 0.0f;
194    if (d < D) {
195        const float incoming = __half2float(__float2half_rn(raw[d]));
196        float sum = incoming;
197#pragma unroll
198        for (int j = 1; j < R; ++j) sum = __fadd_rn(sum, incoming);
199        mean = __fmaf_rn(0.25f, sum, 0.0f);
200    }
201    const float square_sum = norm_scale(mean, partials, d);
202    const float scale = rsqrtf(square_sum / D + epsilon);
203    if (d < D) values[d] = scale * mean * gamma[d];
204    __syncthreads();
205    if (d >= D) return;
206    const float y = pooled_value<TAB>(values, d, 0, theta_scale, freq_scale, corr_low, corr_high, ext_factor, mscale,
207                                 mtab, true, rt);
208    pooled[d] = y;
209    dead[d] = y;
210}
211template <bool TAB>
212__global__ void append_blocks(const float* __restrict__ raw, int64_t n, int64_t p0, int pos_base,
213                              const float* __restrict__ gamma, float epsilon, const float* __restrict__ tail,
214                              const float* __restrict__ dead, float* __restrict__ pooled, int32_t* __restrict__ block_pos,
215                              int64_t first_block, int64_t last_block, float theta_scale, float freq_scale,
216                              float corr_low, float corr_high, float ext_factor, float mscale,
217                              const int32_t* __restrict__ mtab, RopeTab rt) {
218    const int64_t b = first_block + blockIdx.x;
219    const int d = threadIdx.x;
220    __shared__ float values[D];
221    __shared__ float partials[32];
222    float mean = 0.0f;
223    if (d < D) {
224        auto key = [&](int j) -> float {
225            const int64_t cell = b * R + j;
226            return cell >= p0 ? __half2float(__float2half_rn(raw[(cell - p0) * D + d])) : tail[j * D + d];
227        };
228        float sum = key(0);
229#pragma unroll
230        for (int j = 1; j < R; ++j) sum = __fadd_rn(sum, key(j));
231        mean = __fmaf_rn(0.25f, sum, 0.0f);
232    }
233    const float square_sum = norm_scale(mean, partials, d);
234    const float scale = rsqrtf(square_sum / D + epsilon);
235    if (d < D) values[d] = scale * mean * gamma[d];
236    __syncthreads();
237    if (d >= D) return;
238    const int rope_pos = pos_base + R * (int) b;
239    pooled[std::size_t(b) * D + d] = pooled_value<TAB>(values, d, rope_pos, theta_scale, freq_scale, corr_low, corr_high,
240                                                  ext_factor, mscale, mtab, false, rt);
241    if (b == last_block) {
242        pooled[std::size_t(b + 1) * D + d] = dead[d];
243        if (d == 0) *block_pos = rope_pos;
244    }
245}
246// the tail after the batch: slot s holds the key of the batch's last cell with cell % 4 == s (s < 3), if any
247__global__ void append_tail(const float* __restrict__ raw, int64_t n, int64_t p0, float* __restrict__ tail) {
248    const int s = blockIdx.x, d = threadIdx.x;
249    if (d >= D) return;
250    const int64_t last = p0 + n - 1;
251    int64_t cell = last - ((last % R) - s + R) % R;       // the last cell <= last with cell % 4 == s
252    if (cell < p0) return;
253    tail[s * D + d] = __half2float(__float2half_rn(raw[(cell - p0) * D + d]));
254}
255struct Span { const void* p; std::size_t n; };
256void validate(Span s) {
257    const auto p = reinterpret_cast<std::uintptr_t>(s.p);
258    if (!p || p % 4 || s.n > UINTPTR_MAX - p)
259        throw std::invalid_argument("native QSA indexer requires aligned bounded spans");
260}
261bool overlaps(Span a, Span b) {
262    const auto x = reinterpret_cast<std::uintptr_t>(a.p), y = reinterpret_cast<std::uintptr_t>(b.p);
263    return x < y + b.n && y < x + a.n;
264}
265} // namespace
266
267void native_qsa_indexer_set_enabled(bool value) { enabled.store(value, std::memory_order_relaxed); }
268bool native_qsa_indexer_enabled() { return enabled.load(std::memory_order_relaxed); }
269void native_qsa_indexer_append_steps(const float* raw, const int32_t* relative_pos_device,
270                                     int pos_stride, int n_steps, int32_t pos_base,
271                                     const float* gamma, float epsilon, const QsaIndexerBuffers& b,
272                                     const QsaShapes& s, int64_t max_cells, const RopeScaling& scaling, void* stream) {
273    if (n_steps <= 0) return;
274    if (!stream || s.idx_dim != D || s.idx_block != R || s.n_rot != ROT ||
275        max_cells < 1 || max_cells > INT32_MAX || pos_base < 0 || pos_base % R ||
276        int64_t(pos_base) + max_cells > INT32_MAX || !std::isfinite(epsilon) || epsilon <= 0.0f ||
277        rope_scaling_invalid(scaling) != nullptr)
278        throw std::invalid_argument("native QSA indexer requires fixed geometry, aligned position base, positive capacity/epsilon, valid frequency/scaling and explicit stream");
279    const std::size_t pos_span = (n_steps == 1 || pos_stride <= 0) ? 4 : (std::size_t(n_steps - 1) * pos_stride + 1) * 4;
280    const Span spans[] = {{raw,std::size_t(n_steps)*D*4},{relative_pos_device,pos_span},{gamma,D*4},{b.tail,(R-1)*D*4},
281        {b.dead,D*4},{b.pooled,std::size_t(max_cells/R+1)*D*4},{b.block_pos,4}};
282    for (const auto& span : spans) validate(span);
283    for (int i = 0; i < 7; ++i) for (int j = i + 1; j < 7; ++j)
284        if (overlaps(spans[i], spans[j])) throw std::invalid_argument("native QSA indexer buffers overlap");
285    const float theta_scale = powf((float) scaling.freq_base, -2.0f / ROT);
286    const RopeKernelArgs k = scaling.kernel_args(ROT);   // none: the identity constants
287    const RopeTab rt = rope_table_for(scaling);
288    if (rt.cos != nullptr)
289        append<true><<<1,THREADS,0,static_cast<cudaStream_t>(stream)>>>(raw,relative_pos_device,pos_stride,n_steps,pos_base,gamma,epsilon,
290            b.tail,b.dead,b.pooled,b.block_pos,int(max_cells),theta_scale,k.freq_scale,
291            k.corr_low,k.corr_high,k.ext_factor,k.attn_factor,mrope_table(),rt);
292    else
293        append<false><<<1,THREADS,0,static_cast<cudaStream_t>(stream)>>>(raw,relative_pos_device,pos_stride,n_steps,pos_base,gamma,epsilon,
294            b.tail,b.dead,b.pooled,b.block_pos,int(max_cells),theta_scale,k.freq_scale,
295            k.corr_low,k.corr_high,k.ext_factor,k.attn_factor,mrope_table(),rt);
296    const auto error = cudaGetLastError();
297    if (error != cudaSuccess) throw std::runtime_error(cudaGetErrorString(error));
298}
299
300void native_qsa_indexer_append_steps_layers(const float* raw0, int64_t raw_stride, const int32_t* relative_pos_device,
301                                            int pos_stride, int n_steps, int32_t pos_base, float epsilon,
302                                            const QsaIndexerLayers& layers, int n_layers, const QsaShapes& s,
303                                            int64_t max_cells, const RopeScaling& scaling, void* stream) {
304    if (n_steps <= 0 || n_layers <= 0) return;
305    if (!stream || n_layers > QsaIndexerLayers::kMax || s.idx_dim != D || s.idx_block != R || s.n_rot != ROT ||
306        max_cells < 1 || max_cells > INT32_MAX || pos_base < 0 || pos_base % R ||
307        int64_t(pos_base) + max_cells > INT32_MAX || !std::isfinite(epsilon) || epsilon <= 0.0f ||
308        rope_scaling_invalid(scaling) != nullptr)
309        throw std::invalid_argument("native QSA indexer (layers): bad geometry, layer count or parameters");
310    for (int i = 0; i < n_layers; ++i) {   // the per-layer append's span checks, layer by layer
311        const std::size_t pos_span = (n_steps == 1 || pos_stride <= 0) ? 4 : (std::size_t(n_steps - 1) * pos_stride + 1) * 4;
312        const Span spans[] = {{raw0 + (std::size_t) i * (std::size_t) raw_stride, std::size_t(n_steps)*D*4},
313            {relative_pos_device,pos_span},{layers.gamma[i],D*4},{layers.tail[i],(R-1)*D*4},{layers.dead[i],D*4},
314            {layers.pooled[i],std::size_t(max_cells/R+1)*D*4},{layers.block_pos[i],4}};
315        for (const auto& span : spans) validate(span);
316        for (int a = 0; a < 7; ++a) for (int b = a + 1; b < 7; ++b)
317            if (overlaps(spans[a], spans[b])) throw std::invalid_argument("native QSA indexer buffers overlap");
318    }
319    const float theta_scale = powf((float) scaling.freq_base, -2.0f / ROT);
320    const RopeKernelArgs k = scaling.kernel_args(ROT);
321    const RopeTab rt = rope_table_for(scaling);
322    const cudaStream_t st = static_cast<cudaStream_t>(stream);
323    if (rt.cos != nullptr)
324        append_layers<true><<<n_layers, THREADS, 0, st>>>(raw0, (long long) raw_stride, relative_pos_device, pos_stride, n_steps,
325            pos_base, epsilon, layers, int(max_cells), theta_scale, k.freq_scale, k.corr_low, k.corr_high, k.ext_factor,
326            k.attn_factor, mrope_table(), rt);
327    else
328        append_layers<false><<<n_layers, THREADS, 0, st>>>(raw0, (long long) raw_stride, relative_pos_device, pos_stride, n_steps,
329            pos_base, epsilon, layers, int(max_cells), theta_scale, k.freq_scale, k.corr_low, k.corr_high, k.ext_factor,
330            k.attn_factor, mrope_table(), rt);
331    const auto error = cudaGetLastError();
332    if (error != cudaSuccess) throw std::runtime_error(cudaGetErrorString(error));
333}
334
335void native_qsa_indexer_append(const float* raw, const int32_t* relative_pos_device, int32_t pos_base,
336                               const float* gamma, float epsilon, const QsaIndexerBuffers& b,
337                               const QsaShapes& s, int64_t max_cells, const RopeScaling& scaling, void* stream) {
338    native_qsa_indexer_append_steps(raw, relative_pos_device, 0, 1, pos_base, gamma, epsilon, b, s, max_cells, scaling, stream);
339}
340void native_qsa_indexer_append_batch(const float* raw, int64_t n, int64_t p0, int32_t pos_base, const float* gamma,
341                                     float epsilon, const QsaIndexerBuffers& b, const QsaShapes& s, int64_t max_cells,
342                                     const RopeScaling& scaling, void* stream) {
343    if (n <= 0) return;
344    if (!stream || s.idx_dim != D || s.idx_block != R || s.n_rot != ROT || p0 < 0 || p0 + n > max_cells ||
345        max_cells > INT32_MAX || pos_base < 0 || pos_base % R || int64_t(pos_base) + max_cells > INT32_MAX ||
346        !std::isfinite(epsilon) || epsilon <= 0.0f || rope_scaling_invalid(scaling) != nullptr)
347        throw std::invalid_argument("native QSA indexer (batch): bad geometry, positions, parameters or scaling");
348    const float theta_scale = powf((float) scaling.freq_base, -2.0f / ROT);
349    const RopeKernelArgs k = scaling.kernel_args(ROT);   // none: the identity constants
350    const float fsf = k.freq_scale, cl = k.corr_low, ch = k.corr_high, ef = k.ext_factor, ms = k.attn_factor;
351    const cudaStream_t st = static_cast<cudaStream_t>(stream);
352    const int32_t* mtab = mrope_table();
353    const RopeTab rt = rope_table_for(scaling);
354    if (p0 == 0) {
355        if (rt.cos != nullptr)
356            append_first<true><<<1, THREADS, 0, st>>>(raw, gamma, epsilon, b.dead, b.pooled, theta_scale,
357                                                      fsf, cl, ch, ef, ms, mtab, rt);
358        else
359            append_first<false><<<1, THREADS, 0, st>>>(raw, gamma, epsilon, b.dead, b.pooled, theta_scale,
360                                                       fsf, cl, ch, ef, ms, mtab, rt);
361    }
362    // completed blocks: those whose last cell (4b+3) lies in [p0, p0 + n)
363    const int64_t first = p0 <= R - 1 ? 0 : (p0 - (R - 1) + R - 1) / R;       // the smallest b with 4b+3 >= p0
364    const int64_t hi = p0 + n - 1 >= R - 1 ? (p0 + n - 1 - (R - 1)) / R : -1;   // the largest b with 4b+3 <= p0+n-1
365    if (hi >= first && rt.cos != nullptr)
366        append_blocks<true><<<(unsigned) (hi - first + 1), THREADS, 0, st>>>(raw, n, p0, pos_base, gamma, epsilon,
367            b.tail, b.dead, b.pooled, b.block_pos, first, hi, theta_scale, fsf, cl, ch, ef, ms, mtab, rt);
368    else if (hi >= first)
369        append_blocks<false><<<(unsigned) (hi - first + 1), THREADS, 0, st>>>(raw, n, p0, pos_base, gamma, epsilon,
370            b.tail, b.dead, b.pooled, b.block_pos, first, hi, theta_scale, fsf, cl, ch, ef, ms, mtab, rt);
371    append_tail<<<R - 1, D, 0, st>>>(raw, n, p0, b.tail);
372    const auto error = cudaGetLastError();
373    if (error != cudaSuccess) throw std::runtime_error(cudaGetErrorString(error));
374}
375} // namespace strata::kernels