1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
//! CUDA implementation of FP8 matrix multiplication operations.
use crate::dtype::DType;
use crate::error::{Error, Result};
use crate::ops::matmul::matmul_mkn;
use crate::ops::{Fp8MatmulOps, matmul_output_shape};
use crate::runtime::cuda::kernels::{
launch_fp8_matmul_e4m3, launch_fp8_matmul_e4m3_batched, launch_fp8_matmul_e5m2,
launch_fp8_matmul_e5m2_batched,
};
use crate::runtime::cuda::ops::matmul_broadcast::expand_batched_operands;
use crate::runtime::cuda::{CudaClient, CudaRuntime};
use crate::tensor::Tensor;
/// Validate FP8 matmul inputs and extract dimensions.
fn validate_and_extract(
a: &Tensor<CudaRuntime>,
b: &Tensor<CudaRuntime>,
expected_a_dtype: DType,
expected_b_dtype: DType,
out_dtype: DType,
) -> Result<(Vec<usize>, usize, usize, usize, usize)> {
if a.dtype() != expected_a_dtype {
return Err(Error::DTypeMismatch {
lhs: a.dtype(),
rhs: expected_a_dtype,
});
}
if b.dtype() != expected_b_dtype {
return Err(Error::DTypeMismatch {
lhs: b.dtype(),
rhs: expected_b_dtype,
});
}
match out_dtype {
DType::F32 | DType::F16 | DType::BF16 => {}
_ => {
return Err(Error::UnsupportedDType {
dtype: out_dtype,
op: "fp8_matmul output",
});
}
}
let a_shape = a.shape();
let b_shape = b.shape();
// Rank-1 operands are refused: the FP8 kernels take matrices only.
if a_shape.len() < 2 || b_shape.len() < 2 {
return Err(Error::ShapeMismatch {
expected: a_shape.to_vec(),
got: b_shape.to_vec(),
});
}
// `matmul_output_shape` checks `k` against `b`; the geometry then comes from
// the shared matmul rule.
let out_shape = matmul_output_shape(a_shape, b_shape).ok_or(Error::ShapeMismatch {
expected: a_shape.to_vec(),
got: b_shape.to_vec(),
})?;
let (m, k, n) = matmul_mkn(a_shape, b_shape);
let batch_size: usize = out_shape
.iter()
.take(out_shape.len().saturating_sub(2))
.product();
// No `.max(1)`: an unbatched matmul takes 0 dims and already products to 1, so
// a clamp would only fabricate a batch for a genuinely zero batch dim.
Ok((out_shape, batch_size, m, k, n))
}
impl Fp8MatmulOps<CudaRuntime> for CudaClient {
fn fp8_matmul(
&self,
a: &Tensor<CudaRuntime>,
b: &Tensor<CudaRuntime>,
scale_a: f32,
scale_b: f32,
out_dtype: DType,
) -> Result<Tensor<CudaRuntime>> {
let (out_shape, batch_size, m, k, n) =
validate_and_extract(a, b, DType::FP8E4M3, DType::FP8E4M3, out_dtype)?;
// The batched kernels read both operands with the same batch stride and no
// wrapping, so a broadcast batch dim is expanded on device first.
let (a_contig, b_contig) = expand_batched_operands(a, b, &out_shape)?;
let out = Tensor::<CudaRuntime>::empty(&out_shape, out_dtype, &self.device)?;
// A zero-element output has nothing to compute, and the launcher takes its
// grid from `ceil(n / TILE_N)`, `ceil(m / TILE_M)` and the batch count, so a
// zero `m`, `n` or batch is a launch error rather than a wrong answer.
if out.numel() == 0 {
return Ok(out);
}
// A zero-length contraction leaves a NON-empty output whose every element
// sums over no term. CPU answers zeros; the kernel would read off the end of
// the empty operands.
if k == 0 {
return Tensor::<CudaRuntime>::zeros(&out_shape, out_dtype, &self.device);
}
unsafe {
if batch_size > 1 {
launch_fp8_matmul_e4m3_batched(
&self.context,
&self.stream,
self.device.index,
out_dtype,
a_contig.ptr(),
b_contig.ptr(),
out.ptr(),
scale_a,
scale_b,
batch_size,
m,
n,
k,
)?;
} else {
launch_fp8_matmul_e4m3(
&self.context,
&self.stream,
self.device.index,
out_dtype,
a_contig.ptr(),
b_contig.ptr(),
out.ptr(),
scale_a,
scale_b,
m,
n,
k,
)?;
}
}
Ok(out)
}
fn fp8_matmul_e5m2(
&self,
a: &Tensor<CudaRuntime>,
b: &Tensor<CudaRuntime>,
scale_a: f32,
scale_b: f32,
out_dtype: DType,
) -> Result<Tensor<CudaRuntime>> {
let (out_shape, batch_size, m, k, n) =
validate_and_extract(a, b, DType::FP8E5M2, DType::FP8E4M3, out_dtype)?;
// The batched kernels read both operands with the same batch stride and no
// wrapping, so a broadcast batch dim is expanded on device first.
let (a_contig, b_contig) = expand_batched_operands(a, b, &out_shape)?;
let out = Tensor::<CudaRuntime>::empty(&out_shape, out_dtype, &self.device)?;
// A zero-element output has nothing to compute, and the launcher takes its
// grid from `ceil(n / TILE_N)`, `ceil(m / TILE_M)` and the batch count, so a
// zero `m`, `n` or batch is a launch error rather than a wrong answer.
if out.numel() == 0 {
return Ok(out);
}
// A zero-length contraction leaves a NON-empty output whose every element
// sums over no term. CPU answers zeros; the kernel would read off the end of
// the empty operands.
if k == 0 {
return Tensor::<CudaRuntime>::zeros(&out_shape, out_dtype, &self.device);
}
unsafe {
if batch_size > 1 {
launch_fp8_matmul_e5m2_batched(
&self.context,
&self.stream,
self.device.index,
out_dtype,
a_contig.ptr(),
b_contig.ptr(),
out.ptr(),
scale_a,
scale_b,
batch_size,
m,
n,
k,
)?;
} else {
launch_fp8_matmul_e5m2(
&self.context,
&self.stream,
self.device.index,
out_dtype,
a_contig.ptr(),
b_contig.ptr(),
out.ptr(),
scale_a,
scale_b,
m,
n,
k,
)?;
}
}
Ok(out)
}
}