- kernels/hip: k_embed_bf16_gather (BF16 table -> f32 token rows, exact) and k_gemm_bf16 (y[M,N] = x[M,K] @ w[N,K]^T, BF16 in / f32 accumulate, correctness-first per-element; WMMA tiling is the later performance pass). C ABI + safe Rust wrappers (embed_bf16_gather, gemm_bf16) + DeviceBuf byte-level h2d/d2h. - Verified LIVE on gfx1150: embed exact, gemm max_rel_err 0.0 across (2,8,16)/(4,32,64)/(1,128,256), rms_norm ~1e-6. Three kernels now cover the trunk + projection compute path (embed, norm, gemm). - docs/ORIENTATION.md: a guide for newcomers arriving from vLLM/llama.cpp/ SGLang — what this engine is, the five ideas behind the mental-model shift (microkernel+services, models compiled to .vbundle, forward pass as a Lean-proven validated plan, proof-first, parity discipline), the crate map, the honest current state (CPU pipeline done, GPU begun, no token yet), and the ordered road to the first agent chat and beyond. Linked from README. Verified: RUSTFLAGS="-D warnings" cargo test --workspace = 97/97 PASS; lean proofs (4 files, 42 theorems) green; GPU parity 4/4 (--features hip).
167 lines
6.6 KiB
Plaintext
167 lines
6.6 KiB
Plaintext
// kernels/hip/vetta_kernels.hip
|
|
// First RDNA 3.5 (gfx1150) HIP microkernels + minimal runtime helpers.
|
|
// Built by vetta-infer-hip/build.rs via hipcc --offload-arch=gfx1150.
|
|
// Parity discipline: every kernel is checked against a CPU oracle (opt-in
|
|
// GPU test) before it is wired into the executor.
|
|
|
|
#include <hip/hip_runtime.h>
|
|
#include <cstdio>
|
|
#include <cstring>
|
|
#include "vetta_kernels.h"
|
|
|
|
#define HIP_CHECK(call) \
|
|
do { \
|
|
hipError_t _e = (call); \
|
|
if (_e != hipSuccess) { \
|
|
std::fprintf(stderr, "[vetta-hip] %s:%d %s -> %s\n", __FILE__, \
|
|
__LINE__, #call, hipGetErrorString(_e)); \
|
|
return 1; \
|
|
} \
|
|
} while (0)
|
|
|
|
// ---- device query ----------------------------------------------------------
|
|
|
|
extern "C" int vetta_hip_query_device(int dev_id, vetta_device_info_t* out) {
|
|
if (!out) return 1;
|
|
int n = 0;
|
|
HIP_CHECK(hipGetDeviceCount(&n));
|
|
if (dev_id < 0 || dev_id >= n) return 1;
|
|
hipDeviceProp_t p;
|
|
HIP_CHECK(hipGetDeviceProperties(&p, dev_id));
|
|
std::memset(out, 0, sizeof(*out));
|
|
std::snprintf(out->name, sizeof(out->name), "%s", p.name);
|
|
std::snprintf(out->gcn_arch, sizeof(out->gcn_arch), "%s", p.gcnArchName);
|
|
out->multi_processor_count = p.multiProcessorCount;
|
|
out->wave_size = p.warpSize;
|
|
out->total_vram_bytes = (uint64_t)p.totalGlobalMem;
|
|
int uma = 0;
|
|
hipDeviceGetAttribute(&uma, hipDeviceAttributeIntegrated, dev_id);
|
|
out->is_uma = uma;
|
|
return 0;
|
|
}
|
|
|
|
// ---- minimal device buffer lifecycle (UMA-aware) ---------------------------
|
|
|
|
extern "C" int vetta_buf_alloc(void** out, size_t bytes) {
|
|
if (!out) return 1;
|
|
HIP_CHECK(hipMalloc(out, bytes));
|
|
return 0;
|
|
}
|
|
extern "C" int vetta_buf_free(void* p) {
|
|
if (!p) return 0;
|
|
HIP_CHECK(hipFree(p));
|
|
return 0;
|
|
}
|
|
extern "C" int vetta_buf_h2d(void* dst, const void* src, size_t bytes) {
|
|
HIP_CHECK(hipMemcpy(dst, src, bytes, hipMemcpyHostToDevice));
|
|
return 0;
|
|
}
|
|
extern "C" int vetta_buf_d2h(void* dst, const void* src, size_t bytes) {
|
|
HIP_CHECK(hipMemcpy(dst, src, bytes, hipMemcpyDeviceToHost));
|
|
return 0;
|
|
}
|
|
|
|
// ---- k_rms_norm (kernel #2): y = x * rsqrt(mean(x^2)+eps) * w --------------
|
|
// One block per row; block-wide reduction over D. f32 in/out.
|
|
|
|
__global__ void k_rms_norm_f32(const float* __restrict__ x,
|
|
const float* __restrict__ w,
|
|
float* __restrict__ y, int D, float eps) {
|
|
const int row = blockIdx.x;
|
|
const float* xr = x + (size_t)row * D;
|
|
float* yr = y + (size_t)row * D;
|
|
const int tid = threadIdx.x;
|
|
const int nthreads = blockDim.x;
|
|
|
|
float ss = 0.f;
|
|
for (int i = tid; i < D; i += nthreads) {
|
|
float v = xr[i];
|
|
ss += v * v;
|
|
}
|
|
// block reduction via shared memory
|
|
__shared__ float red[256];
|
|
red[tid] = ss;
|
|
__syncthreads();
|
|
for (int s = nthreads / 2; s > 0; s >>= 1) {
|
|
if (tid < s) red[tid] += red[tid + s];
|
|
__syncthreads();
|
|
}
|
|
const float inv = rsqrtf(red[0] / (float)D + eps);
|
|
for (int i = tid; i < D; i += nthreads) {
|
|
yr[i] = xr[i] * inv * (w ? w[i] : 1.f);
|
|
}
|
|
}
|
|
|
|
extern "C" int vetta_rms_norm_f32(const float* x, const float* w, float* y,
|
|
int rows, int D, float eps,
|
|
vetta_stream_t stream) {
|
|
if (rows <= 0 || D <= 0) return 1;
|
|
int threads = D < 256 ? ((D + 31) / 32) * 32 : 256;
|
|
if (threads < 32) threads = 32;
|
|
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
|
|
k_rms_norm_f32<<<rows, threads, 0, s>>>(x, w, y, D, eps);
|
|
HIP_CHECK(hipGetLastError());
|
|
return 0;
|
|
}
|
|
|
|
// ---- BF16 helpers ----------------------------------------------------------
|
|
|
|
__device__ __forceinline__ float bf16_to_f32(uint16_t h) {
|
|
return __uint_as_float(((uint32_t)h) << 16);
|
|
}
|
|
|
|
// ---- k_embed_bf16 (kernel #1): gather token rows from a BF16 table ---------
|
|
// out[b, :] = bf16_to_f32(table[token[b], :]). Exact (no arithmetic).
|
|
|
|
__global__ void k_embed_bf16_gather(const uint16_t* __restrict__ table,
|
|
const int32_t* __restrict__ tokens,
|
|
float* __restrict__ out, int D, int vocab) {
|
|
const int b = blockIdx.x;
|
|
const int tok = tokens[b];
|
|
const int tid = threadIdx.x;
|
|
const int nthreads = blockDim.x;
|
|
if (tok < 0 || tok >= vocab) return;
|
|
const uint16_t* row = table + (size_t)tok * D;
|
|
float* orow = out + (size_t)b * D;
|
|
for (int i = tid; i < D; i += nthreads) orow[i] = bf16_to_f32(row[i]);
|
|
}
|
|
|
|
extern "C" int vetta_embed_bf16_gather(const uint16_t* table, const int32_t* tokens,
|
|
float* out, int B, int D, int vocab,
|
|
vetta_stream_t stream) {
|
|
if (B <= 0 || D <= 0) return 1;
|
|
int threads = D < 256 ? ((D + 31) / 32) * 32 : 256;
|
|
if (threads < 32) threads = 32;
|
|
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
|
|
k_embed_bf16_gather<<<B, threads, 0, s>>>(table, tokens, out, D, vocab);
|
|
HIP_CHECK(hipGetLastError());
|
|
return 0;
|
|
}
|
|
|
|
// ---- k_gemm_bf16: y[M,N] = x[M,K] @ w[N,K]^T, f32 accumulate ---------------
|
|
// x BF16 row-major [M,K], w BF16 row-major [N,K], y f32 [M,N]. One thread per
|
|
// output element (correctness-first; WMMA tiling is the performance pass).
|
|
|
|
__global__ void k_gemm_bf16(const uint16_t* __restrict__ x,
|
|
const uint16_t* __restrict__ w,
|
|
float* __restrict__ y, int M, int N, int K) {
|
|
const int n = blockIdx.x * blockDim.x + threadIdx.x;
|
|
const int m = blockIdx.y * blockDim.y + threadIdx.y;
|
|
if (m >= M || n >= N) return;
|
|
const uint16_t* xr = x + (size_t)m * K;
|
|
const uint16_t* wr = w + (size_t)n * K;
|
|
float acc = 0.f;
|
|
for (int kk = 0; kk < K; ++kk) acc += bf16_to_f32(xr[kk]) * bf16_to_f32(wr[kk]);
|
|
y[(size_t)m * N + n] = acc;
|
|
}
|
|
|
|
extern "C" int vetta_gemm_bf16(const uint16_t* x, const uint16_t* w, float* y,
|
|
int M, int N, int K, vetta_stream_t stream) {
|
|
if (M <= 0 || N <= 0 || K <= 0) return 1;
|
|
dim3 block(16, 16);
|
|
dim3 grid((N + 15) / 16, (M + 15) / 16);
|
|
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
|
|
k_gemm_bf16<<<grid, block, 0, s>>>(x, w, y, M, N, K);
|
|
HIP_CHECK(hipGetLastError());
|
|
return 0;
|
|
} |