1#![allow(unused_imports)]
13
14use anyhow::Result;
15use metrale_gpu_runtime::gpu::{DevicePtr, GpuBackend, KernelHandle};
16use metrale_gpu_runtime::kernel_args::{KernelLaunch, div_ceil};
17
18use crate::layers::moe;
19use crate::weight_map::{DenseWeight, Fp8DenseWeight, Fp8Weight, QuantizedWeight};
20
21use super::*;
22#[path = "gemm_dense_bf16.rs"]
23mod bf16;
24pub use bf16::{
25 MOE_ROUTER_GEMM_COLS, MOE_ROUTER_GEMM_MAX_PREFILL_ROWS, MOE_ROUTER_GEMM_ROWS,
26 MOE_ROUTER_RT_MIN_ROWS, dense_gemm, dense_gemm_bf16_pipelined, dense_gemm_prefill,
27 dense_gemm_router, dense_gemm_splitk, dense_gemm_tc, dense_gemm_tc_scaled_acc, moe_router_gemm,
28 moe_router_gemm_rt,
29};
30
31pub fn w4a16_gemm(
38 gpu: &dyn GpuBackend,
39 kernel: KernelHandle,
40 input: DevicePtr,
41 weight: &QuantizedWeight,
42 output: DevicePtr,
43 m: u32,
44 n: u32,
45 k: u32,
46 stream: u64,
47) -> Result<()> {
48 check_e4m3_activation_range(gpu, kernel, input, m, k, stream)?;
49 KernelLaunch::new(gpu, kernel)
50 .grid([n_tile_blocks(gpu, kernel, n)?, div_ceil(m, 64), 1])
51 .block([128, 1, 1])
52 .arg_ptr(input)
53 .arg_ptr(weight.weight)
54 .arg_ptr(weight.weight_scale)
55 .arg_f32(weight.weight_scale_2)
56 .arg_ptr(output)
57 .arg_u32(m)
58 .arg_u32(n)
59 .arg_u32(k)
60 .launch(stream)
61}
62
63#[allow(clippy::too_many_arguments)]
64pub fn w4a16_gemm_n128_ldb(
73 gpu: &dyn GpuBackend,
74 kernel: KernelHandle,
75 input: DevicePtr,
76 weight: &QuantizedWeight,
77 output: DevicePtr,
78 m: u32,
79 n: u32,
80 k: u32,
81 ldb: u32,
82 stream: u64,
83) -> Result<()> {
84 check_e4m3_activation_range(gpu, kernel, input, m, k, stream)?;
85 KernelLaunch::new(gpu, kernel)
86 .grid([n_tile_blocks(gpu, kernel, n)?, div_ceil(m, 64), 1])
87 .block([128, 1, 1])
88 .arg_ptr(input)
89 .arg_ptr(weight.weight)
90 .arg_ptr(weight.weight_scale)
91 .arg_f32(weight.weight_scale_2)
92 .arg_ptr(output)
93 .arg_u32(m)
94 .arg_u32(n)
95 .arg_u32(k)
96 .arg_u32(ldb)
97 .launch(stream)
98}
99
100pub fn w4a16_gemm_n128(
103 gpu: &dyn GpuBackend,
104 kernel: KernelHandle,
105 input: DevicePtr,
106 weight: &QuantizedWeight,
107 output: DevicePtr,
108 m: u32,
109 n: u32,
110 k: u32,
111 stream: u64,
112) -> Result<()> {
113 w4a16_gemm_n128_ldb(gpu, kernel, input, weight, output, m, n, k, n, stream)
114}
115
116#[allow(clippy::too_many_arguments)]
120pub fn w4a16_gemm_n128_m128_v3(
121 gpu: &dyn GpuBackend,
122 kernel: KernelHandle,
123 input: DevicePtr,
124 weight: &QuantizedWeight,
125 output: DevicePtr,
126 m: u32,
127 n: u32,
128 k: u32,
129 stream: u64,
130) -> Result<()> {
131 check_e4m3_activation_range(gpu, kernel, input, m, k, stream)?;
132 KernelLaunch::new(gpu, kernel)
133 .grid([n_tile_blocks(gpu, kernel, n)?, div_ceil(m, 128), 1])
134 .block([256, 1, 1])
135 .arg_ptr(input)
136 .arg_ptr(weight.weight)
137 .arg_ptr(weight.weight_scale)
138 .arg_f32(weight.weight_scale_2)
139 .arg_ptr(output)
140 .arg_u32(m)
141 .arg_u32(n)
142 .arg_u32(k)
143 .launch(stream)
144}
145
146#[allow(clippy::too_many_arguments)]
152pub fn w4a16_gemm_n128_m128_v2(
153 gpu: &dyn GpuBackend,
154 kernel: KernelHandle,
155 input: DevicePtr,
156 weight: &QuantizedWeight,
157 output: DevicePtr,
158 m: u32,
159 n: u32,
160 k: u32,
161 stream: u64,
162) -> Result<()> {
163 check_e4m3_activation_range(gpu, kernel, input, m, k, stream)?;
164 KernelLaunch::new(gpu, kernel)
165 .grid([n_tile_blocks(gpu, kernel, n)?, div_ceil(m, 128), 1])
166 .block([256, 1, 1])
167 .arg_ptr(input)
168 .arg_ptr(weight.weight)
169 .arg_ptr(weight.weight_scale)
170 .arg_f32(weight.weight_scale_2)
171 .arg_ptr(output)
172 .arg_u32(m)
173 .arg_u32(n)
174 .arg_u32(k)
175 .launch(stream)
176}
177
178#[allow(clippy::too_many_arguments)]
189pub fn w4a16_gemm_n128_m128(
190 gpu: &dyn GpuBackend,
191 kernel: KernelHandle,
192 input: DevicePtr,
193 weight: &QuantizedWeight,
194 output: DevicePtr,
195 m: u32,
196 n: u32,
197 k: u32,
198 stream: u64,
199) -> Result<()> {
200 check_e4m3_activation_range(gpu, kernel, input, m, k, stream)?;
201 KernelLaunch::new(gpu, kernel)
202 .grid([n_tile_blocks(gpu, kernel, n)?, div_ceil(m, 128), 1])
203 .block([128, 1, 1])
204 .arg_ptr(input)
205 .arg_ptr(weight.weight)
206 .arg_ptr(weight.weight_scale)
207 .arg_f32(weight.weight_scale_2)
208 .arg_ptr(output)
209 .arg_u32(m)
210 .arg_u32(n)
211 .arg_u32(k)
212 .launch(stream)
213}
214
215#[allow(clippy::too_many_arguments)]
216pub fn w4a16_gemm_n128_m128_bf16_ldb(
220 gpu: &dyn GpuBackend,
221 kernel: KernelHandle,
222 input: DevicePtr,
223 weight: &QuantizedWeight,
224 output: DevicePtr,
225 m: u32,
226 n: u32,
227 k: u32,
228 ldb: u32,
229 stream: u64,
230) -> Result<()> {
231 check_e4m3_activation_range(gpu, kernel, input, m, k, stream)?;
232 KernelLaunch::new(gpu, kernel)
233 .grid([n_tile_blocks(gpu, kernel, n)?, div_ceil(m, 128), 1])
234 .block([128, 1, 1])
235 .arg_ptr(input)
236 .arg_ptr(weight.weight)
237 .arg_ptr(weight.weight_scale)
238 .arg_f32(weight.weight_scale_2)
239 .arg_ptr(output)
240 .arg_u32(m)
241 .arg_u32(n)
242 .arg_u32(k)
243 .arg_u32(ldb)
244 .launch(stream)
245}
246
247pub fn w4a16_gemm_n128_m128_bf16(
254 gpu: &dyn GpuBackend,
255 kernel: KernelHandle,
256 input: DevicePtr,
257 weight: &QuantizedWeight,
258 output: DevicePtr,
259 m: u32,
260 n: u32,
261 k: u32,
262 stream: u64,
263) -> Result<()> {
264 check_e4m3_activation_range(gpu, kernel, input, m, k, stream)?;
265 KernelLaunch::new(gpu, kernel)
266 .grid([n_tile_blocks(gpu, kernel, n)?, div_ceil(m, 128), 1])
267 .block([128, 1, 1])
268 .arg_ptr(input)
269 .arg_ptr(weight.weight)
270 .arg_ptr(weight.weight_scale)
271 .arg_f32(weight.weight_scale_2)
272 .arg_ptr(output)
273 .arg_u32(m)
274 .arg_u32(n)
275 .arg_u32(k)
276 .launch(stream)
277}
278
279#[cfg(test)]
280#[path = "gemm_dense_tests.rs"]
281mod gemm_dense_tests;