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
//! Shared validation and epilogue-lowering surface for the view adapters (layer L8a support)
//!
//! The checked core entries in `crate::api` validate slice-backed [`crate::MatRef`] and
//! [`crate::MatMut`] views. Their `C`-overlap test compares the bias against `C`'s full backing
//! slice
//!
//! The out-of-crate view adapters (`gemmkit-ndarray`, `gemmkit-nalgebra`, `gemmkit-faer`) hold
//! raw-pointer views instead. A raw-pointer view may be gappy, with padded columns, or reversed,
//! with negative strides, so the slice-based tier cannot describe it. The bias and requantize
//! checks these adapters need live here, in one pointer-level form
//!
//! Every function here takes a raw pointer plus 1 `(dim, element-stride)` pair per axis, and
//! never forms a reference over the span. This lets it describe a `C` view the caller still
//! holds as an exclusive borrow and has not yet proven in-bounds
//!
//! This module holds the single source for the panic wording. Both the adapters and the checked
//! core entries share it, and the checked entries delegate here, treating `C`'s backing slice as
//! a unit-stride footprint
//!
//! This is a `#[doc(hidden)]` support surface for L8a, not a layer of its own. It is not part of
//! the documented API, and it is versioned in lockstep with the adapters that consume it. It
//! uses only `core` arithmetic, so it works without `std`
use crateBias;
use crateRequantScale;
use crateBiasDim;
/// The half-open byte range `[lo, hi)` a strided view touches
///
/// The view is based at `cp` (its element `(0, ..., 0)`). A negative stride reverses the view
/// and extends `lo` below the base. A positive stride extends `hi` above it. A length-1 axis
/// contributes neither bound. A `dim == 0` axis makes the view empty, and the range collapses
/// to `[cp, cp)`
///
/// This function uses raw pointer arithmetic only and never forms a reference over the
/// (possibly gappy) span. It can therefore describe a `C` view the caller still holds as an
/// exclusive borrow and has not yet proven in-bounds
///
/// # Parameters
///
/// - `cp` - the raw pointer at the view's `(0, ..., 0)` element
/// - `dims` - the `(dim, element-stride)` pair for each axis
///
/// # Returns
///
/// - `(usize, usize)` - the half-open byte range `[lo, hi)` the view touches
/// Whether a `len`-element `TB` slice at `bias` overlaps the byte range the strided `C` view
/// touches
///
/// This runs the standard half-open interval test `a0 < b1 && b0 < a1` over [`c_byte_range`].
/// It detects a bias and `C` overlap without ever forming a `C` slice
///
/// # Parameters
///
/// - `cp`/`c_dims` - the `C` view's base pointer and per-axis `(dim, element-stride)` pairs
/// - `bias`/`len` - the bias slice's base pointer and element length
///
/// # Returns
///
/// - `bool` - `true` when the 2 ranges overlap
/// Validate a fused bias against `(m, n)` and `C`'s footprint
///
/// This checks the `Option<Bias<'_, T>>` against `(m, n)` and `C`'s footprint (`cp`/`c_dims`).
/// It panics with the exact wording every checked fused entry uses. It then lowers the bias to
/// the raw `(ptr, BiasDim, has_bias)` triple the `_unchecked` core entries take
///
/// A `PerRow` bias must have length `m`. A `PerCol` bias must have length `n`. Neither may
/// overlap `C` ([`bias_overlaps_c`], raw pointer math only)
///
/// # Parameters
///
/// - `bias` - the optional per-row or per-col bias to validate
/// - `m` - row count of `A` and `C`, the required `PerRow` length
/// - `n` - column count of `B` and `C`, the required `PerCol` length
/// - `cp`/`c_dims` - `C`'s base pointer and per-axis `(dim, element-stride)` pairs
///
/// # Returns
///
/// - `(*const T, BiasDim, bool)` - the lowered pointer, its dimension, and whether a bias is
/// present
///
/// # Panics
///
/// When the bias length does not match `m` or `n`, or when the bias overlaps `C`
/// Validate an optional requantize `i32` bias against `C`'s footprint
///
/// The bias must have length `m == A.rows`. `C`'s footprint is its `i8`/`u8` output
/// (`cp`/`c_dims`). This panics with the requantizing entries' wording on a length mismatch or
/// an overlap. It then lowers the bias to the raw `(ptr, has_bias)` pair the `_unchecked`
/// requant entries take
///
/// # Parameters
///
/// - `m` - row count of `A`, the required bias length
/// - `cp`/`c_dims` - the `i8`/`u8` `C` view's base pointer and per-axis `(dim, element-stride)`
/// pairs
/// - `bias` - the optional `i32` bias to validate
///
/// # Returns
///
/// - `(*const i32, bool)` - the lowered pointer and whether a bias is present
///
/// # Panics
///
/// When the bias length does not match `m`, or when the bias overlaps `C`
/// Validate a [`RequantScale`] against `C`'s footprint
///
/// `C`'s footprint is its `i8`/`u8` output (`cp`/`c_dims`). This panics with the requantizing
/// entries' wording on an invalid value. It then lowers the scale to the raw `(scale,
/// row_scales, has_row_scales)` triple the `_unchecked` requant entries take
///
/// A `PerTensor(s)` value must be finite and greater than 0. A `PerRow` value must have length
/// `m == A.rows`, with every element finite and greater than 0, and must not overlap `C`
///
/// # Parameters
///
/// - `m` - row count of `A`, the required `PerRow` length
/// - `cp`/`c_dims` - the `i8`/`u8` `C` view's base pointer and per-axis `(dim, element-stride)`
/// pairs
/// - `scale` - the per-tensor or per-row scale to validate
///
/// # Returns
///
/// - `(f32, *const f32, bool)` - the tensor scale (0 when per-row), the row-scale pointer, and
/// whether row scales are present
///
/// # Panics
///
/// When a scale value is not finite or not positive, or when per-row scales overlap `C`