okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-nvfp4 into f-144-rel66db34dc4

main
2.6 KiB53 linesraw
1// src/prefill/mmq_nvfp4_w4a4.cu - NVFP4 MMQ with FP4 activations on Blackwell (W4A4, opt-in: STRATA_PREFILL_NVFP4=w4a4).
2//
3// RTX 50's FP4 MMA exists only in the arch-specific target (sm_120a), so this is the one translation unit CMake
4// builds with 12x -> 12xa (strata_mmq_w4a4); everything else, the default NVFP4 path included, keeps the plain
5// architectures and runs on every Blackwell card (sm_121 too). Here mmq.cuh's NVFP4 tile loader and quantize.cu's
6// FP4 activation quantizer see BLACKWELL_MMA_AVAILABLE together - they must agree on the activations' layout.
7// Their non-static symbols are renamed so they cannot fold with strata_mmq's plain-arch copies.
8#include "common.cuh"
9
10#define mul_mat_q_switch_J strata_w4a4_mul_mat_q_switch_J
11#define mul_mat_q_case strata_w4a4_mul_mat_q_case
12#define quantize_row_q8_1_cuda strata_w4a4_quantize_row_q8_1_cuda
13#define quantize_mmq_q8_1_cuda strata_w4a4_quantize_mmq_q8_1_cuda
14#define quantize_scatter_mmq_q8_1_cuda strata_w4a4_quantize_scatter_mmq_q8_1_cuda
15#define quantize_scatter_mmq_fp4_cuda strata_w4a4_quantize_scatter_mmq_fp4_cuda
16#define quantize_mmq_fp4_cuda strata_w4a4_quantize_mmq_fp4_cuda
17#include "mmq.cuh"
18#include "quantize.cu"
19
20DECL_MMQ_CASE(GGML_TYPE_NVFP4);
21
22namespace {
23// 1 when this unit's device code has the FP4 MMA (built for the running card's 12xa); a card without an image
24// here (an architecture not in the list, or sm_121 against a 120a build) fails the launch: W4A4 is then off
25__global__ void w4a4_probe_kernel(int* out) {
26#if defined(BLACKWELL_MMA_AVAILABLE)
27    *out = 1;
28#else
29    *out = 0;
30#endif
31}
32}  // namespace
33
34namespace strata::prefill::mmq {
35bool w4a4_available() {
36    int* d = nullptr;
37    int h = 0;
38    if (cudaMalloc(&d, sizeof(int)) != cudaSuccess) { (void) cudaGetLastError(); return false; }
39    w4a4_probe_kernel<<<1, 1>>>(d);
40    const bool ran = cudaGetLastError() == cudaSuccess && cudaMemcpy(&h, d, sizeof(int), cudaMemcpyDeviceToHost) == cudaSuccess;
41    (void) cudaGetLastError();
42    cudaFree(d);
43    return ran && h == 1;
44}
45void run_nvfp4_w4a4(ggml_backend_cuda_context& ctx, const mmq_args& a, cudaStream_t s) {
46    strata_w4a4_mul_mat_q_case<GGML_TYPE_NVFP4>(ctx, a, s);
47}
48void quantize_nvfp4_w4a4(const float* x, const int32_t* ids, void* xq, float* yscale, bool aligned, int64_t cols,
49                         int64_t ld, int64_t rows, int64_t padded, cudaStream_t s) {
50    strata_w4a4_quantize_mmq_fp4_cuda(x, ids, xq, yscale, GGML_TYPE_NVFP4, aligned, cols, ld, rows * ld, rows * ld,
51                                      padded, rows, 1, 1, s);
52}
53}  // namespace strata::prefill::mmq