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
//! Batched GEMM: `batch` independent products `C_b <- alpha*A_b*B_b + beta*C_b` in 1 call
//!
//! Not a new compute strategy: every element re-enters the ordinary single-GEMM path through
//! [`crate::dispatch::execute`], so a batched call automatically gets the driver / small_k /
//! small_mn / gemv routing each element's shape would get on its own. The gain over a plain loop
//! of `gemm()` calls is in how work reaches workers: whole elements go to workers instead of
//! splitting 1 GEMM across all of them, so each element runs serially on 1 core, cache-hot, and
//! the batch pays a single fork/join instead of one per element, the shape that wins for many
//! small matrices
//!
//! [`Parallelism::resolve_batch`] picks the schedule per call: split across the batch once there
//! is enough total work and enough elements to keep every worker busy; loop the batch on 1 thread
//! and hand each element the engine's full worker count in turn when elements are few but large
//! enough to be worth splitting; serial otherwise. Since every element is independent, the batch
//! result never depends on the worker count. The serial and batch-parallel schedules always run
//! an element whole on 1 worker, so those 2 agree bit-for-bit with each other at any worker
//! count; the few-large schedule instead splits a single element's own work across workers, and
//! is offered only for `m, n > 1` shapes, whose route already reduces every output cell within 1
//! worker regardless of how the driver tiles it
use crateFusedScalar;
use crate;
use crateFusedEpi;
use crate;
use crate;
/// Shared driver behind the 2 strided-batched entry points ([`run`], [`run_fused`]): resolves the
/// [`BatchPlan`] once from the common per-element shape, then for every element `bi` in
/// `0..batch` builds that element's [`Task`] (the shared `a`/`b`/`c` base pointers advanced by
/// `bi * {a,b,c}_bs`) and hands it to `exec` together with the schedule's per-element
/// [`Parallelism`] and a workspace. `exec` is the only difference between [`run`] and
/// [`run_fused`]: it wraps [`crate::dispatch::execute`] or [`crate::dispatch::execute_fused`], so
/// the `Task` construction, the schedule choice, the work partition, and the reproducibility
/// contract all live here once, and both entry points inherit them identically
///
/// # Safety
/// Every element's pointers must be valid for the region its strides and `m`/`k`/`n` imply, the
/// `batch` output regions must be pairwise disjoint, and none may alias any A/B input. `exec` must
/// run its `Task` as 1 serial GEMM: on the workspace it is given under `BatchPlan::BatchParallel`,
/// or with the passed [`Parallelism`] on the shared `ws` under `Serial`/`SequentialInternal`
unsafe
/// Run a strided-batched GEMM: element `bi` reads/writes `A + bi*a_bs`, `B + bi*b_bs`,
/// `C + bi*c_bs`, sharing 1 shape `(m, k, n)` and 1 set of strides across the whole batch. Each
/// element's `alpha == 0` / `k == 0` / `m,n == 0` degeneracy is handled individually by
/// [`crate::dispatch::execute`]
///
/// # Safety
/// Every element's pointers must be valid for the region its strides and sizes imply; the
/// `batch` output regions must be pairwise disjoint and none may alias any A/B input (the safe
/// API validates this)
pub unsafe
/// Run a strided-batched GEMM with a fused epilogue: element `bi` reads/writes `A + bi*a_bs`,
/// `B + bi*b_bs`, `C + bi*c_bs` (1 shared shape `(m, k, n)` and 1 set of strides), and every
/// element applies the SAME `epi` (1 bias vector, 1 activation shared across the whole batch), so
/// `C_bi <- act(alpha*A_bi*B_bi + beta*C_bi + bias)`
///
/// Mirrors [`run`] exactly, with [`crate::dispatch::execute`] replaced by
/// [`crate::dispatch::execute_fused`] in every schedule arm, so element `bi`'s output matches a
/// standalone `gemm_fused` call on that element bit-for-bit: for `f32`/`f64` that in turn matches
/// plain `gemm()` followed by the same map, for every shape; for `f16`/`bf16` the epilogue runs in
/// `f32` before the single narrowing round at the store. Per-element `alpha == 0` / `k == 0` /
/// `m,n == 0` degeneracy is handled by `execute_fused`. `epi` is `Copy`, captured into the
/// parallel workers exactly like the base pointers
///
/// Scheduling and reproducibility match [`run`] (the fused routes reuse the same kernels, so
/// [`Parallelism::resolve_batch`]'s policy carries over unchanged). Every element is independent,
/// so the batch result never depends on the worker count. Serial and batch-parallel run each
/// element whole on 1 worker and so agree bit-for-bit with each other at any worker count; the
/// few-large schedule instead splits a single element's own work across workers, and is offered
/// only for `m, n > 1` shapes
///
/// # Safety
/// As [`run`], plus: `epi`'s bias pointer must be valid for the element's `m` (`PerRow`) or `n`
/// (`PerCol`) and must not alias any `C` region (the safe API validates this)
pub unsafe
/// Run a heterogeneous batch: a slice of independent GEMM problems, each carrying its own shape
/// and pointers (the pointer-array / grouped form, unlike [`run`]'s single shared shape).
/// Problems are parallelized directly, 1 whole problem per worker, cache-hot, so the batch pays 1
/// fork/join and each worker's share is independent of the others. Since problems can differ in
/// size, this uses the flat [`Parallelism::resolve_batch_flat`] policy (a total-work gate, not a
/// per-element cache-residency test) and never splits a single problem's own work across workers,
/// so the result never depends on the worker count. Reads each [`Task`] straight out of the
/// `problems` slice, no intermediate `Vec<Task>` copy
///
/// # Safety
/// Each problem's pointers must be valid for its own shape and strides; the `problems` output
/// regions must be pairwise disjoint and none may alias any input (the safe API validates this;
/// the unchecked entry point instead takes the caller's word for it)
pub unsafe