180 lines
7.8 KiB
Plaintext
180 lines
7.8 KiB
Plaintext
// STRA-163 candidate regeneration source; never the independent oracle.
|
|
// Decomposed native_gdn=true / fused_gdn=false, fixed S128 HK16 HV48.
|
|
// Original native_gdn.cu SHA0478b434053e080890ceed248581bbc67f4ce6b92ce29c09706180fd33bd7270
|
|
// Original native_gdn_preprocess.cu SHA940aa60cd42e77ad7f4b5a6e2fd8177b80016975d2aaf55ed7a620f6437f27c4
|
|
// Arithmetic attribution: llama.cpp 3cf03257f219afbe7334045ff7c6a06ac68c627d.
|
|
// Offline only: nvcc --use_fast_math -ptx -arch=sm_120 offline_gdn_kernel.cu.
|
|
// Prior state/history are immutable. All destinations are disjoint candidates.
|
|
// Qualification and retained producer/transaction integration remain open.
|
|
// MIT License
|
|
// Copyright (c) 2023-2026 The ggml authors
|
|
//
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
// in the Software without restriction, including without limitation the rights
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
// furnished to do so, subject to the following conditions:
|
|
//
|
|
// The above copyright notice and this permission notice shall be included in all
|
|
// copies or substantial portions of the Software.
|
|
//
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
// SOFTWARE.
|
|
|
|
#include <cuda_runtime.h>
|
|
#include <cmath>
|
|
#include <cstddef>
|
|
|
|
namespace {
|
|
constexpr int S = 128, HK = 16, HV = 48, C = 10240;
|
|
__device__ __forceinline__ float finite(float x) {
|
|
return isfinite(x) ? x : __uint_as_float(0x7fc00000u);
|
|
}
|
|
__device__ __forceinline__ float positive(float x) {
|
|
return isfinite(x) && x > 0.0f ? x : __uint_as_float(0x7fc00000u);
|
|
}
|
|
__device__ __forceinline__ float exponential(float x) {
|
|
// __expf scales by log2(e); reject overflowing scale as well as result.
|
|
x = finite(x);
|
|
const float scale = finite(x * 1.44269504088896341f);
|
|
return finite(__expf(isfinite(scale) ? x : scale));
|
|
}
|
|
__device__ __forceinline__ float sigmoid(float x) {
|
|
const float e = exponential(finite(-x));
|
|
return finite(1.0f / positive(finite(1.0f + e)));
|
|
}
|
|
__device__ __forceinline__ float warp_sum(float x) {
|
|
#pragma unroll
|
|
for (int d = 16; d > 0; d >>= 1)
|
|
x = finite(x + __shfl_xor_sync(0xffffffffu, x, d, 32));
|
|
return x;
|
|
}
|
|
__device__ __forceinline__ float norm_sum(float x, float* sums) {
|
|
const int lane = threadIdx.x % 32;
|
|
x = warp_sum(x);
|
|
if (lane == 0) sums[threadIdx.x / 32] = x;
|
|
__syncthreads();
|
|
x = lane < 8 ? sums[lane] : 0.0f;
|
|
return warp_sum(x);
|
|
}
|
|
__device__ __forceinline__ float epsilon_value(float e) {
|
|
return isfinite(e) && e >= 0.0f ? e : __uint_as_float(0x7fc00000u);
|
|
}
|
|
}
|
|
|
|
extern "C" __global__ void leleka_gdn_conv_silu_f32(
|
|
const float* __restrict__ history, const float* __restrict__ input,
|
|
const float* __restrict__ weights, float* __restrict__ next_history,
|
|
float* __restrict__ raw, float* __restrict__ h) {
|
|
const int c = blockIdx.x * blockDim.x + threadIdx.x;
|
|
if (c >= C) return;
|
|
const float values[4] = {finite(history[c*3]), finite(history[c*3+1]),
|
|
finite(history[c*3+2]), finite(input[c])};
|
|
float sum = 0.0f;
|
|
#pragma unroll
|
|
for (int tap = 0; tap < 4; ++tap)
|
|
sum = finite(sum + values[tap] * finite(weights[c*4+tap]));
|
|
sum = finite(__fadd_rn(sum, 0.0f));
|
|
raw[c] = sum;
|
|
h[c] = finite(sum / positive(finite(1.0f + exponential(finite(-sum)))));
|
|
#pragma unroll
|
|
for (int tap = 0; tap < 3; ++tap)
|
|
next_history[c*3+tap] = values[tap+1];
|
|
}
|
|
|
|
extern "C" __global__ void leleka_gdn_qk_l2_f32(
|
|
const float* __restrict__ h, float* __restrict__ normalized, float epsilon) {
|
|
const int col = threadIdx.x;
|
|
const size_t base = size_t(blockIdx.x) * S;
|
|
const float value = col < S ? finite(h[base+col]) : 0.0f;
|
|
float partial = 0.0f;
|
|
if (col < S) partial = finite(partial + value * value);
|
|
__shared__ float sums[32];
|
|
partial = norm_sum(partial, sums);
|
|
// Host native wrapper divides epsilon by S before its RMS kernel.
|
|
const float e = finite(epsilon_value(epsilon) / S);
|
|
const float mean = finite(partial / S);
|
|
const float scale = finite(rsqrtf(positive(finite(mean + e))));
|
|
if (col < S) {
|
|
const float norm = finite(__fmul_rn(scale, value));
|
|
// Explicit RMS/store/scale boundary from native_gdn_preprocess.
|
|
normalized[base+col] = finite(__fmaf_rn(norm, 0.0883883461356163f, 0.0f));
|
|
}
|
|
}
|
|
|
|
extern "C" __global__ void leleka_gdn_beta_sigmoid_f32(
|
|
const float* __restrict__ beta, float* __restrict__ out) {
|
|
const int i = blockIdx.x * blockDim.x + threadIdx.x;
|
|
if (i < HV) out[i] = sigmoid(finite(beta[i]));
|
|
}
|
|
|
|
extern "C" __global__ void leleka_gdn_gate_softplus_f32(
|
|
const float* __restrict__ alpha, const float* __restrict__ dt,
|
|
const float* __restrict__ a, float* __restrict__ gate) {
|
|
const int i = blockIdx.x * blockDim.x + threadIdx.x;
|
|
if (i >= HV) return;
|
|
const float value = finite(__fadd_rn(finite(alpha[i]), finite(dt[i])));
|
|
// Preserve log1pf: logf(1+exp) would erase small positive softplus values.
|
|
const float softplus = value > 20.0f ? value : finite(log1pf(exponential(value)));
|
|
gate[i] = finite(softplus * finite(a[i]));
|
|
}
|
|
|
|
extern "C" __global__ void __launch_bounds__(128,2) leleka_gdn_step_f32(
|
|
const float* __restrict__ state, const float* __restrict__ q,
|
|
const float* __restrict__ k, const float* __restrict__ v,
|
|
const float* __restrict__ gate, const float* __restrict__ beta,
|
|
float* __restrict__ next_state, float* __restrict__ output) {
|
|
const int head = blockIdx.x, lane = threadIdx.x;
|
|
const int col = blockIdx.z * blockDim.y + threadIdx.y;
|
|
const int qhead = head % HK;
|
|
float shard[4], kr[4], qr[4];
|
|
#pragma unroll
|
|
for (int r = 0; r < 4; ++r) {
|
|
const int row = r*32 + lane;
|
|
shard[r] = finite(state[(size_t(row)*HV + head)*S + col]);
|
|
kr[r] = finite(k[qhead*S + row]);
|
|
qr[r] = finite(q[qhead*S + row]);
|
|
}
|
|
const float decay = exponential(finite(gate[head]));
|
|
float kv = 0.0f;
|
|
#pragma unroll
|
|
for (int r = 0; r < 4; ++r) kv = finite(kv + shard[r]*kr[r]);
|
|
kv = warp_sum(kv);
|
|
const float difference = finite(finite(v[head*S+col]) - decay*kv);
|
|
const float delta = finite(difference * finite(beta[head]));
|
|
float readout = 0.0f;
|
|
#pragma unroll
|
|
for (int r = 0; r < 4; ++r) {
|
|
shard[r] = finite(decay*shard[r] + kr[r]*delta);
|
|
readout = finite(readout + shard[r]*qr[r]);
|
|
const int row = r*32 + lane;
|
|
next_state[(size_t(row)*HV + head)*S + col] = shard[r];
|
|
}
|
|
readout = warp_sum(readout);
|
|
if (lane == 0) output[head*S+col] = finite(readout * 0.0883883461356163f);
|
|
}
|
|
|
|
extern "C" __global__ void leleka_gdn_out_norm_f32(
|
|
const float* __restrict__ input, const float* __restrict__ z,
|
|
const float* __restrict__ gamma, float* __restrict__ out, float epsilon) {
|
|
const int col = threadIdx.x;
|
|
const size_t base = size_t(blockIdx.x)*S;
|
|
const float value = col < S ? finite(input[base+col]) : 0.0f;
|
|
float partial = 0.0f;
|
|
if (col < S) partial = finite(partial + value*value);
|
|
__shared__ float sums[32];
|
|
partial = norm_sum(partial,sums);
|
|
const float mean = finite(partial / S);
|
|
const float scale = finite(rsqrtf(positive(finite(mean + epsilon_value(epsilon)))));
|
|
if (col < S) {
|
|
const float weighted = finite(__fmul_rn(finite(__fmul_rn(scale,value)),finite(gamma[col])));
|
|
out[base+col] = finite(weighted * sigmoid(finite(z[base+col])));
|
|
}
|
|
}
|