brotensor/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
8 / 8
100.0%
Branches:
-
-%
Functions:
1 / 1
100.0%

src/kernels/mir_coop_common.h
Line Branch Exec Source
1 #pragma once
2
3 // Shared Brass MIR helpers for 16x16x16 cooperative-matrix kernels
4 // (mir_gemm_cm, mir_conv_cm, mir_fa_cm, mir_fa_bwd_cm):
5 // - i32x4 vector zero / select & FP16/BF16/FP32 tile packing
6 // - 8-element quantized weight tile load + FP16 decode (INT8, FP8, Q8_0, Q4_K, Q6_K)
7 // - elementwise cooperative-matrix accumulator loops & 16-lane clustered shuffles
8
9 #include "mir_common.h"
10
11 #include <array>
12 #include <cstdint>
13 #include <functional>
14
15 namespace brotensor::kernels {
16
17 208 struct QRawMir {
18 208 Value* a = nullptr; // i32x4: Q4_K header (or unused)
19 208 Value* w0 = nullptr; // i32: INT8/FP8/Q4_K low 4 bytes
20 208 Value* w1 = nullptr; // i32: INT8/FP8/Q4_K high 4 bytes
21 208 std::array<Value*, 4> s0{}; // Q8_0 quants / Q6_K ql (4 x u16 as i32)
22 208 std::array<Value*, 4> s1{}; // Q6_K qh (4 x u16 as i32)
23 208 Value* c = nullptr; // i32: INT8/FP8 scale bits (or Q8_0 d u16 / Q6_K sc_pair u16)
24 208 Value* d = nullptr; // i32: Q6_K d u16
25 };
26
27 Value* emit_v4_zero(Ctx& c);
28 Value* emit_v4_pack(Ctx& c, Value* w0, Value* w1, Value* w2, Value* w3);
29 Value* emit_v4_select(Ctx& c, Value* cond, Value* a, Value* b);
30 Value* emit_pack_f32x8_to_f16_v4(Ctx& c, const std::array<Value*, 8>& v);
31 Value* emit_bf16x8_to_f16x8(Ctx& c, Value* v4);
32 Value* emit_bf16x8_to_f16x8_scaled(Ctx& c, Value* v4, const std::array<Value*, 8>& mul);
33 Value* emit_f32x8_to_f16x8_scaled(Ctx& c, Value* v0, Value* v1, const std::array<Value*, 8>& mul);
34
35 // Load 8 consecutive quantized weights starting at column `k` of `row_ptr` (byte address).
36 QRawMir emit_qload8_mir(Ctx& c, std::uint32_t fmt, bool vec, Value* row_ptr,
37 Value* scale_ptr, Value* ridx, Value* k, Value* kmax);
38
39 // Decode QRawMir into 8 FP16 values packed in an i32x4.
40 // Optional `s_fp8` (256-entry f32 shared LUT) speeds up QF_FP8 decode when non-null.
41 Value* emit_qdecode8_f16_mir(Ctx& c, std::uint32_t fmt, const QRawMir& q, Value* k,
42 Value* s_fp8 = nullptr);
43
44 // Iterate over every local element of an accumulator fragment (`0..coop_acc_len(acc)`).
45 void emit_coop_acc_map(Ctx& c, Value* acc, const std::function<Value*(Value* idx, Value* val)>& fn);
46
47 // Subgroup-local 16-lane clustered reductions and 32-lane votes built purely on shfl_bfly.
48 Value* emit_cluster16_max_f32(Ctx& c, Value* v);
49 Value* emit_cluster16_add_f32(Ctx& c, Value* v);
50 Value* emit_cluster16_min_i32(Ctx& c, Value* v);
51 Value* emit_cluster16_max_i32(Ctx& c, Value* v);
52 Value* emit_subgroup32_any_i32(Ctx& c, Value* cond);
53 Value* emit_subgroup32_all_i32(Ctx& c, Value* cond);
54
55 } // namespace brotensor::kernels
56