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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
//! CPU implementation of GEMM epilogue operations.
use crate::dtype::Element;
use crate::error::{Error, Result};
use crate::ops::{GemmActivation, GemmEpilogueOps};
use crate::ops::{matmul_bias_output_shape, validate_gemm_epilogue_dtypes};
use crate::runtime::cpu::helpers::{dispatch_dtype, ensure_contiguous};
use crate::runtime::cpu::kernels::{
matmul_bias_activation_bwd_kernel, matmul_bias_activation_kernel, matmul_bias_residual_kernel,
};
use crate::runtime::cpu::{CpuClient, CpuRuntime};
use crate::tensor::Tensor;
impl GemmEpilogueOps<CpuRuntime> for CpuClient {
fn matmul_bias_activation(
&self,
a: &Tensor<CpuRuntime>,
b: &Tensor<CpuRuntime>,
bias: &Tensor<CpuRuntime>,
activation: GemmActivation,
) -> Result<Tensor<CpuRuntime>> {
let dtype = validate_gemm_epilogue_dtypes(
a.dtype(),
b.dtype(),
bias.dtype(),
"matmul_bias_activation",
)?;
let out_shape = matmul_bias_output_shape(a.shape(), b.shape(), bias.shape()).ok_or(
Error::ShapeMismatch {
expected: a.shape().to_vec(),
got: b.shape().to_vec(),
},
)?;
let a_shape = a.shape();
let b_shape = b.shape();
let m = if a_shape.len() >= 2 {
a_shape[a_shape.len() - 2]
} else {
1
};
let k = a_shape[a_shape.len() - 1];
let n = b_shape[b_shape.len() - 1];
let a_contig = ensure_contiguous(a)?;
let b_contig = ensure_contiguous(b)?;
let bias_contig = ensure_contiguous(bias)?;
// 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.
let batch_size: usize = out_shape
.iter()
.take(out_shape.len().saturating_sub(2))
.product();
let out = Tensor::<CpuRuntime>::empty(&out_shape, dtype, &self.device)?;
// A zero-element output has nothing to compute. The dispatch below takes the
// single-matmul branch for any `batch_size <= 1`, and a batch of 0 lands
// there too — it would then write one full m*n tile into an empty
// allocation. Return before that.
if out.numel() == 0 {
return Ok(out);
}
let a_ptr = a_contig.ptr();
let b_ptr = b_contig.ptr();
let bias_ptr = bias_contig.ptr();
let out_ptr = out.ptr();
let lda = k;
let ldb = n;
let ldc = n;
dispatch_dtype!(dtype, T => {
#[cfg(feature = "rayon")]
{
use rayon::prelude::*;
if batch_size > 1 {
let min_len = self.rayon_min_len();
self.install_parallelism(|| {
(0..batch_size)
.into_par_iter()
.with_min_len(min_len)
.for_each(|batch| unsafe {
matmul_bias_activation_kernel::<T>(
(a_ptr as *const T).add(batch * m * k),
(b_ptr as *const T).add(batch * k * n),
bias_ptr as *const T,
(out_ptr as *mut T).add(batch * m * n),
m, n, k, lda, ldb, ldc,
activation,
);
});
});
} else {
unsafe {
matmul_bias_activation_kernel::<T>(
a_ptr as *const T,
b_ptr as *const T,
bias_ptr as *const T,
out_ptr as *mut T,
m, n, k, lda, ldb, ldc,
activation,
);
}
}
}
#[cfg(not(feature = "rayon"))]
unsafe {
for batch in 0..batch_size {
matmul_bias_activation_kernel::<T>(
(a_ptr as *const T).add(batch * m * k),
(b_ptr as *const T).add(batch * k * n),
bias_ptr as *const T,
(out_ptr as *mut T).add(batch * m * n),
m, n, k, lda, ldb, ldc,
activation,
);
}
}
}, "matmul_bias_activation");
Ok(out)
}
fn matmul_bias_residual(
&self,
a: &Tensor<CpuRuntime>,
b: &Tensor<CpuRuntime>,
bias: &Tensor<CpuRuntime>,
residual: &Tensor<CpuRuntime>,
) -> Result<Tensor<CpuRuntime>> {
let dtype = validate_gemm_epilogue_dtypes(
a.dtype(),
b.dtype(),
bias.dtype(),
"matmul_bias_residual",
)?;
if residual.dtype() != dtype {
return Err(Error::DTypeMismatch {
lhs: dtype,
rhs: residual.dtype(),
});
}
let out_shape = matmul_bias_output_shape(a.shape(), b.shape(), bias.shape()).ok_or(
Error::ShapeMismatch {
expected: a.shape().to_vec(),
got: b.shape().to_vec(),
},
)?;
// Validate residual shape matches output shape
if residual.shape() != out_shape.as_slice() {
return Err(Error::ShapeMismatch {
expected: out_shape.clone(),
got: residual.shape().to_vec(),
});
}
let a_shape = a.shape();
let b_shape = b.shape();
let m = if a_shape.len() >= 2 {
a_shape[a_shape.len() - 2]
} else {
1
};
let k = a_shape[a_shape.len() - 1];
let n = b_shape[b_shape.len() - 1];
let a_contig = ensure_contiguous(a)?;
let b_contig = ensure_contiguous(b)?;
let bias_contig = ensure_contiguous(bias)?;
let residual_contig = ensure_contiguous(residual)?;
// 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.
let batch_size: usize = out_shape
.iter()
.take(out_shape.len().saturating_sub(2))
.product();
let out = Tensor::<CpuRuntime>::empty(&out_shape, dtype, &self.device)?;
// A zero-element output has nothing to compute. The dispatch below takes the
// single-matmul branch for any `batch_size <= 1`, and a batch of 0 lands
// there too — it would then write one full m*n tile into an empty
// allocation. Return before that.
if out.numel() == 0 {
return Ok(out);
}
let a_ptr = a_contig.ptr();
let b_ptr = b_contig.ptr();
let bias_ptr = bias_contig.ptr();
let res_ptr = residual_contig.ptr();
let out_ptr = out.ptr();
let lda = k;
let ldb = n;
let ldc = n;
dispatch_dtype!(dtype, T => {
#[cfg(feature = "rayon")]
{
use rayon::prelude::*;
if batch_size > 1 {
let min_len = self.rayon_min_len();
self.install_parallelism(|| {
(0..batch_size)
.into_par_iter()
.with_min_len(min_len)
.for_each(|batch| unsafe {
matmul_bias_residual_kernel::<T>(
(a_ptr as *const T).add(batch * m * k),
(b_ptr as *const T).add(batch * k * n),
bias_ptr as *const T,
(res_ptr as *const T).add(batch * m * n),
(out_ptr as *mut T).add(batch * m * n),
m, n, k, lda, ldb, ldc,
);
});
});
} else {
unsafe {
matmul_bias_residual_kernel::<T>(
a_ptr as *const T,
b_ptr as *const T,
bias_ptr as *const T,
res_ptr as *const T,
out_ptr as *mut T,
m, n, k, lda, ldb, ldc,
);
}
}
}
#[cfg(not(feature = "rayon"))]
unsafe {
for batch in 0..batch_size {
matmul_bias_residual_kernel::<T>(
(a_ptr as *const T).add(batch * m * k),
(b_ptr as *const T).add(batch * k * n),
bias_ptr as *const T,
(res_ptr as *const T).add(batch * m * n),
(out_ptr as *mut T).add(batch * m * n),
m, n, k, lda, ldb, ldc,
);
}
}
}, "matmul_bias_residual");
Ok(out)
}
fn matmul_bias_activation_bwd(
&self,
grad: &Tensor<CpuRuntime>,
a: &Tensor<CpuRuntime>,
b: &Tensor<CpuRuntime>,
bias: &Tensor<CpuRuntime>,
activation: GemmActivation,
) -> Result<(Tensor<CpuRuntime>, Tensor<CpuRuntime>, Tensor<CpuRuntime>)> {
let dtype = validate_gemm_epilogue_dtypes(
a.dtype(),
b.dtype(),
bias.dtype(),
"matmul_bias_activation_bwd",
)?;
if grad.dtype() != dtype {
return Err(Error::DTypeMismatch {
lhs: dtype,
rhs: grad.dtype(),
});
}
let a_shape = a.shape();
let b_shape = b.shape();
let m = if a_shape.len() >= 2 {
a_shape[a_shape.len() - 2]
} else {
1
};
let k = a_shape[a_shape.len() - 1];
let n = b_shape[b_shape.len() - 1];
let a_contig = ensure_contiguous(a)?;
let b_contig = ensure_contiguous(b)?;
let bias_contig = ensure_contiguous(bias)?;
let grad_contig = ensure_contiguous(grad)?;
// 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.
let batch_size: usize = a_shape
.iter()
.take(a_shape.len().saturating_sub(2))
.product();
// Output gradients
let d_a = Tensor::<CpuRuntime>::empty(a_shape, dtype, &self.device)?;
let d_b = Tensor::<CpuRuntime>::zeros(b_shape, dtype, &self.device)?;
// No batch contributes. The batched branch below zeroes `d_b` over k*n
// elements before accumulating, which overruns `d_b` when its own batch dim
// is 0. Both gradients sum over nothing here, so both are the additive
// identity `d_b` was already seeded with.
if batch_size == 0 {
let d_bias_full = Tensor::<CpuRuntime>::zeros(&[n], dtype, &self.device)?;
return Ok((d_a, d_b, d_bias_full));
}
// d_bias is always [N] — we need to sum across batches
let d_bias_full = Tensor::<CpuRuntime>::empty(&[n], dtype, &self.device)?;
let a_ptr = a_contig.ptr();
let b_ptr = b_contig.ptr();
let bias_ptr = bias_contig.ptr();
let grad_ptr = grad_contig.ptr();
let d_a_ptr = d_a.ptr();
let d_b_ptr = d_b.ptr();
let d_bias_ptr = d_bias_full.ptr();
let lda = k;
let ldb = n;
let ld_grad = n;
dispatch_dtype!(dtype, T => {
if batch_size == 1 {
unsafe {
matmul_bias_activation_bwd_kernel::<T>(
grad_ptr as *const T,
a_ptr as *const T,
b_ptr as *const T,
bias_ptr as *const T,
d_a_ptr as *mut T,
d_b_ptr as *mut T,
d_bias_ptr as *mut T,
m, n, k, lda, ldb, ld_grad,
activation,
);
}
} else {
// For batched: compute per-batch, accumulate d_b and d_bias
// Zero out d_b and d_bias first
unsafe {
for i in 0..k * n {
*(d_b_ptr as *mut T).add(i) = T::zero();
}
for j in 0..n {
*(d_bias_ptr as *mut T).add(j) = T::zero();
}
}
let mut temp_d_b = vec![T::zero(); k * n];
let mut temp_d_bias = vec![T::zero(); n];
for batch in 0..batch_size {
unsafe {
matmul_bias_activation_bwd_kernel::<T>(
(grad_ptr as *const T).add(batch * m * n),
(a_ptr as *const T).add(batch * m * k),
(b_ptr as *const T).add(batch * k * n),
bias_ptr as *const T,
(d_a_ptr as *mut T).add(batch * m * k),
temp_d_b.as_mut_ptr(),
temp_d_bias.as_mut_ptr(),
m, n, k, lda, ldb, ld_grad,
activation,
);
// Accumulate d_b
for i in 0..k * n {
let ptr = (d_b_ptr as *mut T).add(i);
*ptr += temp_d_b[i];
}
// Accumulate d_bias
for j in 0..n {
let ptr = (d_bias_ptr as *mut T).add(j);
*ptr += temp_d_bias[j];
}
}
}
}
}, "matmul_bias_activation_bwd");
Ok((d_a, d_b, d_bias_full))
}
}