okami.mom/strata

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

Niko1221perf(gr): multi-token hyper-connection read/write, exact-T GR and BF16 projections (#783 PR-g)104a8485e

main
4.5 KiB109 linesraw
1// Adapted from llama.cpp 3cf03257f219afbe7334045ff7c6a06ac68c627d:
2// ggml/src/ggml-cuda/{norm.cu,common.cuh}. Scope: contiguous weighted F32 RMSNorm.
3//
4// MIT License
5// Copyright (c) 2023-2026 The ggml authors
6//
7// Permission is hereby granted, free of charge, to any person obtaining a copy
8// of this software and associated documentation files (the "Software"), to deal
9// in the Software without restriction, including without limitation the rights
10// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11// copies of the Software, and to permit persons to whom the Software is
12// furnished to do so, subject to the following conditions:
13//
14// The above copyright notice and this permission notice shall be included in all
15// copies or substantial portions of the Software.
16//
17// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23// SOFTWARE.
24
25#include "strata/kernels/native_gr_norm.hpp"
26
27#include <cuda_runtime.h>
28#include <cmath>
29#include <cstddef>
30#include <cstdint>
31#include <stdexcept>
32#include <string>
33
34namespace strata::kernels {
35namespace {
36
37__device__ __forceinline__ float norm_warp_sum(float value) {
38#pragma unroll
39    for (int offset = 16; offset > 0; offset >>= 1) {
40        value += __shfl_xor_sync(0xffffffffu, value, offset, 32);
41    }
42    return value;
43}
44
45template<int BlockSize>
46__global__ void weighted_rms_norm(const float* __restrict__ input,
47                                   const float* __restrict__ gamma,
48                                   float* __restrict__ output, int n_cols, float epsilon) {
49    const int tid = threadIdx.x;
50    const std::size_t row = std::size_t(blockIdx.y) * gridDim.x + blockIdx.x;
51    const std::size_t row_offset = row * n_cols;
52    input += row_offset;
53    gamma += std::size_t(blockIdx.x) * n_cols;
54    output += row_offset;
55    float partial = 0.0f;
56    for (int col = tid; col < n_cols; col += BlockSize) {
57        const float value = input[col];
58        partial += value * value;
59    }
60
61    // Pinned block_reduce<SUM,BlockSize>: every warp repeats the final XOR
62    // reduction. There is no warp-0-only broadcast or downward-shuffle tree.
63    __shared__ float sums[32];
64    partial = norm_warp_sum(partial);
65    const int lane = tid % 32;
66    if (lane == 0) sums[tid / 32] = partial;
67    __syncthreads();
68    partial = 0.0f;
69    if (lane < BlockSize / 32) partial = sums[lane];
70    partial = norm_warp_sum(partial);
71
72    const float mean = partial / n_cols;
73    const float scale = rsqrtf(mean + epsilon);
74    for (int col = tid; col < n_cols; col += BlockSize) {
75        output[col] = scale * input[col] * gamma[col];
76    }
77}
78
79void check_pointer(const void* p) {
80    if (!p || reinterpret_cast<std::uintptr_t>(p) % alignof(float) != 0)
81        throw std::invalid_argument("native GR RMSNorm requires non-null four-byte aligned pointers");
82}
83
84} // namespace
85
86void native_gr_rms_norm_weighted(const float* input, const float* gamma, float* output,
87                                 int n_cols, int n_rows, float epsilon, void* stream) {
88    native_gr_rms_norm_weighted_multi(input, gamma, output, n_cols, n_rows, 1, epsilon, stream);
89}
90
91void native_gr_rms_norm_weighted_multi(const float* input, const float* gamma, float* output,
92                                       int n_cols, int n_rows, int n_tok, float epsilon, void* stream) {
93    if (n_cols <= 0 || n_rows <= 0 || n_tok <= 0 || !std::isfinite(epsilon) || epsilon < 0.0f)
94        throw std::invalid_argument("native GR RMSNorm requires positive dimensions and finite nonnegative epsilon");
95    check_pointer(input);
96    check_pointer(gamma);
97    check_pointer(output);
98    const auto cuda_stream = static_cast<cudaStream_t>(stream);
99    const dim3 grid{unsigned(n_rows), unsigned(n_tok), 1u};
100    if (n_cols < 1024)
101        weighted_rms_norm<256><<<grid, 256, 0, cuda_stream>>>(input, gamma, output, n_cols, epsilon);
102    else
103        weighted_rms_norm<1024><<<grid, 1024, 0, cuda_stream>>>(input, gamma, output, n_cols, epsilon);
104    const auto error = cudaGetLastError();
105    if (error != cudaSuccess)
106        throw std::runtime_error(std::string("native GR RMSNorm launch: ") + cudaGetErrorString(error));
107}
108
109} // namespace strata::kernels