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
//! Batched GEMM: `batch` independent products `C_b <- alpha*A_b*B_b + beta*C_b` in 1 call
//!
//! This is not a new compute strategy. Every element re-enters the ordinary single-GEMM path
//! through [`crate::dispatch::execute`]. A batched call therefore gets the same driver,
//! small_k, small_mn, or gemv routing that 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. The batch then pays a single fork/join instead of one per element.
//! This is the shape that wins for many small matrices
//!
//! [`Parallelism::resolve_batch`] picks the schedule per call. It splits across the batch once
//! there is enough total work and enough elements to keep every worker busy. It loops the
//! batch on 1 thread and hands each element the engine's full worker count in turn. This
//! applies when elements are few but large enough to be worth splitting. Otherwise it runs
//! serially. 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
//! schedules 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. It is offered only for `m, n >
//! 1` shapes. That 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`]). It
/// resolves the [`BatchPlan`] once from the common per-element shape. For every element `bi`
/// in `0..batch`, it builds that element's [`Task`]. The shared `a`/`b`/`c` base pointers
/// advance by `bi * {a,b,c}_bs`. It then hands the task 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`]. 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 one complete GEMM call. Under `BatchPlan::BatchParallel`, it
/// runs on the workspace it is given. Under `Serial` or `SequentialInternal`, it runs with the
/// passed [`Parallelism`] on the shared `ws`
unsafe
/// Run a strided-batched GEMM. Element `bi` reads and writes `A + bi*a_bs`, `B + bi*b_bs`,
/// and `C + bi*c_bs`. The whole batch shares 1 shape `(m, k, n)` and 1 set of strides. Each
/// element's `alpha == 0`, `k == 0`, or `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 and writes
/// `A + bi*a_bs`, `B + bi*b_bs`, and `C + bi*c_bs`. The whole batch shares 1 shape
/// `(m, k, n)` and 1 set of strides. Every element applies the same `epi`, meaning 1 bias
/// vector and 1 activation shared across the whole batch. So
/// `C_bi <- act(alpha*A_bi*B_bi + beta*C_bi + bias)`
///
/// This mirrors [`run`] exactly, with [`crate::dispatch::execute`] replaced by
/// [`crate::dispatch::execute_fused`] in every schedule arm. Element `bi`'s output therefore
/// 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`, or `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, so they 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`]. `epi`'s bias pointer must also 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. This is the pointer-array, or 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 rather than a per-element cache-residency test. It never splits
/// a single problem's own work across workers, so the result never depends on the worker
/// count. It reads each [`Task`] straight out of the `problems` slice, with 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