Files
leleka/crates/cuda-driver/src/offline_moe_kernel.cu
T

118 lines
6.0 KiB
Plaintext

// Fixed STRA-167 default-profile MoE assets. Offline regeneration only.
// Pinned upstream d9ab8435f654c368c586340d490915f6addf56a3.
// shared_expert.cu SHA256 bcb63f8b6f2dd92d5035ceae33f9b6b513fe34e3e2c5c54a4c9bbf867dbad4ca
// Native SwiGLU62-68; default scalar109-125; scale128-130; legacy combine143-156.
// bf16_bits.hpp supplies exact ties-even integer conversion.
// Regenerate: nvcc --ptx -std=c++17 -O3 -arch=sm_120 --fmad=true --ftz=false
// No global --use_fast_math: original shared_expert.cu uses ordinary flags;
// native SwiGLU explicitly invokes __expf/__fdividef.
// This candidate source is not the independent original-source oracle.
// Exact Context-owned whole buffers; grid/block fixed in the private registry.
// Source-defined positiveInf exponential saturation remains admissible.
// Invalid input/NaN/invalid denominator/nonfinite output signals canonical qNaN.
// Cargo neither builds nor links CUDA; no Toolkit/Strata serving dependency.
//
// MIT License
//
// Copyright (c) 2026 Niko1221 and the Strata contributors
//
// 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 <stdint.h>
__device__ __forceinline__ float refusal() { return __uint_as_float(0x7fc00000u); }
__device__ __forceinline__ uint16_t original_bf16(float value) {
uint32_t word = __float_as_uint(value);
if ((word & 0x7fffffffu) > 0x7f800000u) return uint16_t((word >> 16) | 64u);
word = (word + ((word >> 16) & 1u) + 0x7fffu) & 0xffff0000u;
return uint16_t(word >> 16);
}
__device__ __forceinline__ float original_f32(uint16_t value) { return __uint_as_float(uint32_t(value) << 16); }
__device__ __forceinline__ double original_warp_sum(double value) {
for (int offset = 16; offset > 0; offset >>= 1) value += __shfl_down_sync(0xffffffffu, value, offset);
return __shfl_sync(0xffffffffu, value, 0);
}
extern "C" __global__ void leleka_moe_swiglu_f32(const float* gate, const float* up, float* output) {
const int i = int(blockIdx.x) * int(blockDim.x) + int(threadIdx.x);
if (i >= 640) return;
const float g = gate[i], u = up[i];
if (!isfinite(g) || !isfinite(u)) { output[i] = refusal(); return; }
const float exponential = __expf(-g);
const float denominator = 1.0f + exponential;
if (isnan(exponential) || !(denominator > 0.0f)) { output[i] = refusal(); return; }
// Preserve source operation order, including signedzero from +Inf exp.
const float result = __fdividef(g, denominator) * u;
output[i] = isfinite(result) ? result : refusal();
}
extern "C" __global__ void leleka_moe_shared_gate_f32_bf16(const float* input, const uint16_t* weights, float* output) {
__shared__ double scratch[8];
__shared__ int invalid_scratch[8];
double acc = 0.0;
int invalid = 0;
for (int i = int(threadIdx.x); i < 2560; i += int(blockDim.x)) {
const float source = input[i];
const float x = original_f32(original_bf16(source));
const float w = original_f32(weights[i]);
invalid |= !isfinite(source) || !isfinite(x) || !isfinite(w);
acc += double(x) * double(w);
}
__syncthreads();
const int lane = int(threadIdx.x) & 31, warp = int(threadIdx.x) >> 5;
acc = original_warp_sum(acc);
invalid = __any_sync(0xffffffffu, invalid);
if (lane == 0) { scratch[warp] = acc; invalid_scratch[warp] = invalid; }
__syncthreads();
if (warp == 0) {
acc = threadIdx.x < 8 ? scratch[threadIdx.x] : 0.0;
invalid = threadIdx.x < 8 ? invalid_scratch[threadIdx.x] : 0;
acc = original_warp_sum(acc);
invalid = __any_sync(0xffffffffu, invalid);
if (threadIdx.x == 0) {
// Original DOUBLE exp can overflow to +Inf and legitimately give0.
const double exponential = exp(-acc);
const double denominator = 1.0 + exponential;
const float result = float(1.0 / denominator);
output[0] = !invalid && isfinite(acc) && !isnan(exponential) && denominator > 0.0 && isfinite(result) ? result : refusal();
}
}
}
extern "C" __global__ void leleka_moe_scale_shared_f32(const float* input, const float* scalar, float* output) {
const int i = int(blockIdx.x) * int(blockDim.x) + int(threadIdx.x);
if (i >= 2560) return;
const float x = input[i], g = scalar[0];
const float result = x * g;
output[i] = isfinite(x) && isfinite(g) && isfinite(result) ? result : refusal();
}
extern "C" __global__ void leleka_moe_combine_f32(const float* parts, const float* weights, const float* shared, float* output) {
const int j = int(blockIdx.x) * int(blockDim.x) + int(threadIdx.x);
if (j >= 2560) return;
double acc = 0.0;
int invalid = 0;
for (int expert = 0; expert < 10; ++expert) {
const float weight = weights[expert], part = parts[expert * 2560 + j];
invalid |= !isfinite(weight) || !isfinite(part);
acc += double(weight) * double(part);
}
const float plain_shared = shared[j];
acc += double(plain_shared);
const float result = float(acc);
output[j] = !invalid && isfinite(plain_shared) && isfinite(result) ? result : refusal();
}