112 lines
5.0 KiB
Plaintext
112 lines
5.0 KiB
Plaintext
// Offline compilation source; not a target runtime/toolkit dependency.
|
|
// Default PLE arithmetic adapted from original Strata ple.cu SHA
|
|
// afcd6efac591acf6c7ff95d8c756a782cfa6dbb4490146f924cb32a55d9d6262.
|
|
// Source build excludes ple.cu from native adapters' --use_fast_math:
|
|
// nvcc -O3 --ftz=false --prec-div=true --prec-sqrt=true --fmad=true -ptx -arch=sm_120.
|
|
// Key projection remains the separately qualified native BF16/F32 MMVF.
|
|
// Separate immutable prior/candidate buffers and failure-only NaN signaling.
|
|
#include <cuda_runtime.h>
|
|
#include <cuda_fp16.h>
|
|
#include <stdint.h>
|
|
#include <math.h>
|
|
constexpr int N=2560, HC=10240, HIST=9;
|
|
__device__ float finite(float x) { return isfinite(x)?x:__int_as_float(0x7fc00000); }
|
|
__device__ double finite64(double x) { return isfinite(x)?x:(double)__int_as_float(0x7fc00000); }
|
|
__device__ float bf16(uint16_t h) { return __uint_as_float((uint32_t)h<<16); }
|
|
__device__ uint16_t round_bf16(float x) {
|
|
uint32_t i=__float_as_uint(x);
|
|
return (uint16_t)((i+((i>>16)&1u)+0x7fffu)>>16);
|
|
}
|
|
__device__ double block_sum(double v,double* scratch) {
|
|
__syncthreads();
|
|
const int lane=threadIdx.x&31,warp=threadIdx.x>>5;
|
|
for(int off=16;off>0;off>>=1) v=finite64(v+__shfl_down_sync(0xffffffffu,v,off));
|
|
if(lane==0) scratch[warp]=v;
|
|
__syncthreads();
|
|
v=threadIdx.x<8?scratch[threadIdx.x]:0.0;
|
|
if(warp==0) for(int off=16;off>0;off>>=1) v=finite64(v+__shfl_down_sync(0xffffffffu,v,off));
|
|
if(threadIdx.x==0) scratch[0]=v;
|
|
__syncthreads();
|
|
return scratch[0];
|
|
}
|
|
__device__ float sigmoid(float x) {
|
|
const float e=finite(expf(-finite(x)));
|
|
return finite(1.0f/finite(1.0f+e));
|
|
}
|
|
extern "C" __global__ void leleka_ple_value_bf16_f32(
|
|
const float* __restrict__ x,const uint16_t* __restrict__ w,float* __restrict__ out) {
|
|
const int row=threadIdx.x;
|
|
if(row>=64) return;
|
|
double acc=0.0;
|
|
for(int i=0;i<N;++i) {
|
|
const float a=finite(bf16(round_bf16(finite(x[i]))));
|
|
const float b=finite(bf16(w[row*N+i]));
|
|
acc=finite64(acc+(double)a*(double)b);
|
|
}
|
|
out[row]=finite((float)acc);
|
|
}
|
|
extern "C" __global__ void leleka_ple_grouped_norm_f32(
|
|
const float* __restrict__ x,const float* __restrict__ gamma,float* __restrict__ out) {
|
|
__shared__ double scratch[8];
|
|
const int base=blockIdx.x*N;
|
|
double acc=0.0;
|
|
for(int d=threadIdx.x;d<N;d+=256) {
|
|
const float v=finite(x[base+d]);
|
|
const float sq=finite(__fmul_rn(v,v));
|
|
acc=finite64(acc+(double)sq);
|
|
}
|
|
const float mean=finite((float)finite64(block_sum(acc,scratch)/(double)N));
|
|
const float denominator=finite(mean+1.0e-6f);
|
|
const float scale=denominator>0.0f?finite(1.0f/sqrtf(denominator)):__int_as_float(0x7fc00000);
|
|
for(int d=threadIdx.x;d<N;d+=256) {
|
|
const float normalized=finite(__fmul_rn(finite(x[base+d]),scale));
|
|
out[base+d]=finite(__fmul_rn(normalized,finite(gamma[base+d])));
|
|
}
|
|
}
|
|
extern "C" __global__ void leleka_ple_signed_gate_f32(
|
|
const float* __restrict__ key,const float* __restrict__ query,float* __restrict__ out) {
|
|
__shared__ double scratch[8];
|
|
const int base=blockIdx.x*N;
|
|
double acc=0.0;
|
|
for(int d=threadIdx.x;d<N;d+=256) {
|
|
const float p=finite(__fmul_rn(finite(key[base+d]),finite(query[base+d])));
|
|
acc=finite64(acc+(double)p);
|
|
}
|
|
const float sum=finite((float)block_sum(acc,scratch));
|
|
const float s=finite(__fmul_rn(sum,0.019764235243201256f));
|
|
// Guard before fmaxf: fmaxf would otherwise conceal NaN as the finite floor.
|
|
const float mag=isfinite(s)?finite(sqrtf(fmaxf(fabsf(s),1.0e-6f))):__int_as_float(0x7fc00000);
|
|
const float sign=s>0.0f?1.0f:(s<0.0f?-1.0f:0.0f);
|
|
if(threadIdx.x==0) out[blockIdx.x]=sigmoid(finite(__fmul_rn(sign,mag)));
|
|
}
|
|
extern "C" __global__ void leleka_ple_broadcast_f32(
|
|
const float* __restrict__ value,const float* __restrict__ gate,float* __restrict__ out) {
|
|
const int c=blockIdx.x*256+threadIdx.x;
|
|
if(c<HC) out[c]=finite(__fmul_rn(finite(value[c%N]),finite(gate[c/N])));
|
|
}
|
|
extern "C" __global__ void leleka_ple_conv_history_f32(
|
|
const float* __restrict__ hist,const float* __restrict__ normalized,
|
|
const uint16_t* __restrict__ weights,float* __restrict__ out,float* __restrict__ next) {
|
|
const int c=blockIdx.x*256+threadIdx.x;
|
|
if(c>=HC) return;
|
|
float acc=0.0f;
|
|
for(int tap=0;tap<4;++tap) {
|
|
const float v=finite(tap==3?normalized[c]:hist[c*HIST+tap*3]);
|
|
const float w=finite(__half2float(__ushort_as_half(weights[4*c+tap])));
|
|
acc=finite(__fmaf_rn(w,v,acc));
|
|
}
|
|
const float denominator=finite(1.0f+finite(expf(-acc)));
|
|
out[c]=finite(acc/denominator);
|
|
for(int r=0;r<8;++r) next[c*HIST+r]=finite(hist[c*HIST+r+1]);
|
|
next[c*HIST+8]=finite(normalized[c]);
|
|
}
|
|
extern "C" __global__ void leleka_ple_residual_f32(
|
|
const float* __restrict__ r,const float* __restrict__ gated,
|
|
const float* __restrict__ conv,float* __restrict__ out) {
|
|
const int c=blockIdx.x*256+threadIdx.x;
|
|
if(c<HC) {
|
|
const float first=finite(__fadd_rn(finite(r[c]),finite(gated[c])));
|
|
out[c]=finite(__fadd_rn(first,finite(conv[c])));
|
|
}
|
|
}
|