Kyojin: open-source engine that runs 100 GB-class open models (GLM-5.3, Qwen3.8, DeepSeek V4.1) on one desktop machine. AMD Strix Halo, NVIDIA RTX and Apple Mac. OpenAI API, built on ExLlamaV3.
Python amdapple-siliconcudadeepseekexl3exllamav3glminferencellmllm-inferencelocal-llmnvidiaopenai-apiquantizationqwenrocmstrix-halo
// FP8 (E4M3, stored as is) decode GEMV in C++ (CUDA and HIP, wave32 on RDNA). Replaces the Triton _gemv_fp8 for 1..16 activation rows:
// the Triton tile read 64 rows x 128 B per iteration and streamed DRAM at 42-55 GB/s on gfx1151 once the weights fall out of the 32 MB
// MALL (every served step). Here one warp owns one output column and walks its K bytes in 16-byte lanes (512 contiguous bytes per
// warp step = long DRAM bursts), each lane keeping U loads in flight (BOLT_FP8_U1 for 1 x-row, BOLT_FP8_UM for 2..16 x-rows in groups
// of 4); each weight chunk is decoded once for all the rows of its group.
// E4M3 -> fp16 by the same bit move as quant/fp8.py (sign | exponent-mantissa << 7, x 256), fp32 accumulation; scales per output row
// (s[N], applied at the end) or per [bo x bi] block (s[ceil(N/bo), s_cols], bi % 16 == 0, applied per 16-byte chunk) as the Triton kernel.
#include <cuda_fp16.h>
#include <cstdint>
#include <cstdlib>
#include <torch/extension.h>
#include <c10/cuda/CUDAGuard.h>
#include <ATen/cuda/CUDAContext.h>
#include "../util.h"
#include "fq_api.h"
#if defined(USE_ROCM)
#define __shfl_xor_sync(mask, var, ...) __shfl_xor(var, __VA_ARGS__)
#endif
#define FQ_FP8_WARP 32
#define FQ_FP8_WARPS 4
__device__ __forceinline__ float fq_e4m3_f(uint32_t b)
{
uint16_t h = (uint16_t)(((b & 0x80u) << 8) | ((b & 0x7Fu) << 7));
return __half2float(__ushort_as_half(h)) * 256.0f;
}
template <int MR, int U>
__global__ void fq_gemv_fp8_kernel(const __half* __restrict__ x, const uint8_t* __restrict__ q, const float* __restrict__ s,
float* __restrict__ out, int M, int N, int K, int s_cols, int bo, int bi, bool grid)
{
// One warp per output column; each lane issues U 16-byte weight loads (chunks k, k + 512, ...) before it consumes them, so a lane
// keeps U requests in flight instead of one; each weight chunk is decoded once and dotted against up to MR x-rows.
int lane = threadIdx.x % FQ_FP8_WARP;
int n = blockIdx.x * FQ_FP8_WARPS + threadIdx.x / FQ_FP8_WARP;
int m0 = blockIdx.y * MR;
int mc = min(MR, M - m0);
if (n >= N) return; // whole warp leaves together (no block-wide sync below)
const uint8_t* wr = q + (int64_t)n * K;
const float* srow = grid ? s + (int64_t)(n / bo) * s_cols : nullptr;
constexpr int STEP = FQ_FP8_WARP * 16;
float acc[MR];
#pragma unroll
for (int r = 0; r < MR; r++) acc[r] = 0.0f;
for (int k0 = lane * 16; k0 < K; k0 += STEP * U)
{
uint4 wv4[U];
#pragma unroll
for (int u = 0; u < U; u++)
if (k0 + u * STEP < K) wv4[u] = *reinterpret_cast<const uint4*>(wr + k0 + u * STEP);
#pragma unroll
for (int u = 0; u < U; u++)
{
int k = k0 + u * STEP;
if (k >= K) break;
float wf[16];
uint32_t wv[4] = {wv4[u].x, wv4[u].y, wv4[u].z, wv4[u].w};
#pragma unroll
for (int j = 0; j < 4; j++)
{
#pragma unroll
for (int b = 0; b < 4; b++) wf[j * 4 + b] = fq_e4m3_f((wv[j] >> (8 * b)) & 0xFFu);
}
float sc = grid ? srow[k / bi] : 1.0f;
#pragma unroll
for (int r = 0; r < MR; r++)
{
if (r < mc)
{
const uint4* xr = reinterpret_cast<const uint4*>(x + (int64_t)(m0 + r) * K + k);
uint4 xa = xr[0], xb = xr[1];
const __half2* h2a = reinterpret_cast<const __half2*>(&xa);
const __half2* h2b = reinterpret_cast<const __half2*>(&xb);
float sum = 0.0f;
#pragma unroll
for (int j = 0; j < 4; j++)
{
float2 fa = __half22float2(h2a[j]);
float2 fb = __half22float2(h2b[j]);
sum = fmaf(wf[2 * j], fa.x, sum);
sum = fmaf(wf[2 * j + 1], fa.y, sum);
sum = fmaf(wf[8 + 2 * j], fb.x, sum);
sum = fmaf(wf[8 + 2 * j + 1], fb.y, sum);
}
acc[r] = fmaf(sum, sc, acc[r]);
}
}
}
}
#pragma unroll
for (int r = 0; r < MR; r++)
{
float v = acc[r];
#pragma unroll
for (int o = FQ_FP8_WARP / 2; o > 0; o >>= 1) v += __shfl_xor_sync(0xffffffffu, v, o, FQ_FP8_WARP);
if (lane == 0 && r < mc) out[(int64_t)(m0 + r) * N + n] = grid ? v : v * s[n];
}
}
template <int MR, int U>
static void fq_gemv_fp8_launch(const __half* xp, torch::Tensor q, torch::Tensor s, torch::Tensor out, int M, int N, int K,
int s_cols, int bo, int bi, bool grid, cudaStream_t stream)
{
dim3 g((N + FQ_FP8_WARPS - 1) / FQ_FP8_WARPS, (M + MR - 1) / MR);
fq_gemv_fp8_kernel<MR, U><<<g, dim3(FQ_FP8_WARP * FQ_FP8_WARPS), 0, stream>>>(xp, q.data_ptr<uint8_t>(), s.data_ptr<float>(),
out.data_ptr<float>(), M, N, K, s_cols, bo, bi, grid);
}
static int fq_fp8_env(const char* name, int dflt)
{
const char* e = getenv(name);
return e ? atoi(e) : dflt;
}
void fq_gemv_fp8(torch::Tensor x, torch::Tensor q, torch::Tensor s, torch::Tensor out, int64_t s_cols, int64_t bo, int64_t bi, bool grid)
{
const at::cuda::OptionalCUDAGuard device_guard(x.device());
TORCH_CHECK(x.dtype() == torch::kHalf && x.dim() == 2 && x.is_contiguous(), "fq_gemv_fp8: x must be contiguous half [M, K]");
TORCH_CHECK(q.dtype() == torch::kUInt8 && q.dim() == 2 && q.is_contiguous(), "fq_gemv_fp8: q must be contiguous uint8 [N, K]");
TORCH_CHECK(s.dtype() == torch::kFloat && s.is_contiguous(), "fq_gemv_fp8: s must be contiguous float32");
TORCH_CHECK(out.dtype() == torch::kFloat && out.is_contiguous(), "fq_gemv_fp8: out must be contiguous float32 [M, N]");
int M = x.size(0), K = x.size(1), N = q.size(0);
TORCH_CHECK(q.size(1) == K && out.size(0) == M && out.size(1) == N, "fq_gemv_fp8: shape mismatch");
TORCH_CHECK(K % 16 == 0 && (!grid || bi % 16 == 0), "fq_gemv_fp8: K and the scale block width must be multiples of 16");
TORCH_CHECK(((uintptr_t)x.data_ptr() % 16) == 0 && ((uintptr_t)q.data_ptr() % 16) == 0, "fq_gemv_fp8: x and q must be 16-byte aligned");
auto stream = at::cuda::getCurrentCUDAStream().stream();
const __half* xp = reinterpret_cast<const __half*>(x.data_ptr());
static const int u1 = fq_fp8_env("BOLT_FP8_U1", 4); // weight loads in flight per lane, 1 x-row
static const int um = fq_fp8_env("BOLT_FP8_UM", 4); // same, 2..16 x-rows (groups of 4)
int sc = (int)s_cols, ibo = (int)bo, ibi = (int)bi;
if (M == 1)
{
if (u1 >= 4) fq_gemv_fp8_launch<1, 4>(xp, q, s, out, M, N, K, sc, ibo, ibi, grid, stream);
else if (u1 >= 2) fq_gemv_fp8_launch<1, 2>(xp, q, s, out, M, N, K, sc, ibo, ibi, grid, stream);
else fq_gemv_fp8_launch<1, 1>(xp, q, s, out, M, N, K, sc, ibo, ibi, grid, stream);
}
else
{
if (um >= 4) fq_gemv_fp8_launch<4, 4>(xp, q, s, out, M, N, K, sc, ibo, ibi, grid, stream);
else if (um >= 2) fq_gemv_fp8_launch<4, 2>(xp, q, s, out, M, N, K, sc, ibo, ibi, grid, stream);
else fq_gemv_fp8_launch<4, 1>(xp, q, s, out, M, N, K, sc, ibo, ibi, grid, stream);
}
}