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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
//! GGML / GGUF / ONNX tensor element-type alphabet per [Wiki ADR-031][09].
//!
//! Each dtype is a sealed unit struct implementing [`ConstrainedTypeShape`]
//! with the generic IRI per [ADR-017][09]'s empty-CONSTRAINTS closure rule:
//! at the framework level two shapes with identical block-byte counts
//! content-address identically. The Rust-type distinction is the
//! application-level ergonomics surface; per-dtype block-layout metadata is
//! exposed through the [`Dtype`] trait's associated constants and through
//! the [`TensorDtypeRegistry`] entries populated by `register_shape!` per
//! [ADR-057][09].
//!
//! Per [ADR-058][09] the κ-derivation is the framework's
//! compression-to-canonical-form operator. Tensor element types occupy the
//! R-level of the operator-geometry codomain per [ADR-059][09] (algebra
//! dimension 1; characteristic identity *existence* — the
//! byte-element-present distinction). Tensor composition through
//! `partition_product!` per ADR-033/044 ascends the convergence tower from
//! R to higher levels.
//!
//! # Container-format consumption
//!
//! Container-format realizations (e.g., `uor-addr` GGUF / ONNX realizations
//! as Layer-2 sibling format families) consume this alphabet through the
//! `TensorDtypeRegistry` per ADR-057's `Term::Recurse` lowering rule. A
//! container's `address_inference` verb body references a dtype shape
//! through
//!
//! ```ignore
//! ConstraintRef::Recurse {
//! shape_iri: "https://uor.foundation/type/ConstrainedType",
//! descent_bound: MAX_TENSOR_DEPTH,
//! }
//! ```
//!
//! and the runtime ψ_1 NerveResolver expands the reference through the
//! `TensorDtypeRegistry`'s entries, decrementing `descent_bound` on each
//! traversal per ADR-057.
//!
//! # Element types
//!
//! ## Continuous floating-point
//!
//! | Dtype | Block bytes | Block elems | Notes |
//! |-------|-------------|-------------|-------|
//! | [`F32`] | 4 | 1 | IEEE 754 binary32 |
//! | [`F16`] | 2 | 1 | IEEE 754 binary16 |
//! | [`BF16`] | 2 | 1 | Brain Float 16 |
//! | [`F64`] | 8 | 1 | IEEE 754 binary64 |
//!
//! ## ONNX 8-bit floating-point (TensorProto.DataType 17–20)
//!
//! | Dtype | Block bytes | Block elems | Notes |
//! |-------|-------------|-------------|-------|
//! | [`F8_E4M3`] | 1 | 1 | FLOAT8E4M3FN (no inf, finite-NaN) |
//! | [`F8_E4M3_FNUZ`] | 1 | 1 | FLOAT8E4M3FNUZ (no neg-zero variant) |
//! | [`F8_E5M2`] | 1 | 1 | FLOAT8E5M2 |
//! | [`F8_E5M2_FNUZ`] | 1 | 1 | FLOAT8E5M2FNUZ |
//!
//! ## ONNX complex
//!
//! | Dtype | Block bytes | Block elems | Notes |
//! |-------|-------------|-------------|-------|
//! | [`C64`] | 8 | 1 | COMPLEX64: real + imag as `(F32, F32)` |
//! | [`C128`] | 16 | 1 | COMPLEX128: real + imag as `(F64, F64)` |
//!
//! ## Signed / unsigned integer
//!
//! | Dtype | Block bytes | | Dtype | Block bytes |
//! |-------|-------------|---|-------|-------------|
//! | [`I8`] | 1 | | [`U8`] | 1 |
//! | [`I16`] | 2 | | [`U16`] | 2 |
//! | [`I32`] | 4 | | [`U32`] | 4 |
//! | [`I64`] | 8 | | [`U64`] | 8 |
//!
//! [`BOOL`] is 1 byte (`0x00` = false, any non-zero = true).
//!
//! ## ONNX packed 4-bit (TensorProto.DataType 21–23)
//!
//! | Dtype | Block bytes | Block elems | Layout |
//! |-------|-------------|-------------|--------|
//! | [`I4`] | 1 | 2 | INT4: two signed 4-bit nibbles |
//! | [`U4`] | 1 | 2 | UINT4: two unsigned 4-bit nibbles |
//! | [`F4_E2M1`] | 1 | 2 | FLOAT4E2M1: two 4-bit floats |
//!
//! ## GGML legacy block-32 quantization (`block_q*_*`)
//!
//! | Dtype | Block bytes | Block elems | Layout (canonical) |
//! |-------|-------------|-------------|--------------------|
//! | [`Q4_0`] | 18 | 32 | `{d:f16, qs:[u8;16]}` |
//! | [`Q4_1`] | 20 | 32 | `{d:f16, m:f16, qs:[u8;16]}` |
//! | [`Q5_0`] | 22 | 32 | `{d:f16, qh:[u8;4], qs:[u8;16]}` |
//! | [`Q5_1`] | 24 | 32 | `{d:f16, m:f16, qh:[u8;4], qs:[u8;16]}` |
//! | [`Q8_0`] | 34 | 32 | `{d:f16, qs:[i8;32]}` |
//! | [`Q8_1`] | 36 | 32 | `{d:f16, s:f16, qs:[i8;32]}` |
//!
//! ## GGML K-series block-256 quantization (`block_q*_K`)
//!
//! | Dtype | Block bytes | Block elems |
//! |-------|-------------|-------------|
//! | [`Q2_K`] | 84 | 256 |
//! | [`Q3_K`] | 110 | 256 |
//! | [`Q4_K`] | 144 | 256 |
//! | [`Q5_K`] | 176 | 256 |
//! | [`Q6_K`] | 210 | 256 |
//! | [`Q8_K`] | 292 | 256 |
//!
//! ## GGML IQ-series importance-aware quantization (`block_iq*`)
//!
//! | Dtype | Block bytes | Block elems | Bits per weight |
//! |-------|-------------|-------------|-----------------|
//! | [`IQ1_S`] | 50 | 256 | ~1.5625 |
//! | [`IQ1_M`] | 56 | 256 | ~1.75 |
//! | [`IQ2_XXS`] | 66 | 256 | ~2.0625 |
//! | [`IQ2_XS`] | 74 | 256 | ~2.3125 |
//! | [`IQ2_S`] | 82 | 256 | ~2.5625 |
//! | [`IQ3_XXS`] | 98 | 256 | ~3.0625 |
//! | [`IQ3_S`] | 110 | 256 | ~3.4375 |
//! | [`IQ4_NL`] | 18 | 32 | ~4.5 (block-32) |
//! | [`IQ4_XS`] | 136 | 256 | ~4.25 |
//!
//! Note: under [ADR-017][09]'s closure rule, dtypes with identical
//! `BLOCK_BYTES` content-address identically at the framework level.
//! Examples include: [`F16`] / [`BF16`] / [`I16`] / [`U16`] (2-byte
//! shapes); [`Q4_0`] / [`IQ4_NL`] (18-byte shapes); [`Q3_K`] /
//! [`IQ3_S`] (110-byte shapes). The Rust-type distinction carries the
//! application-level interpretation; framework canonical-form emission
//! depends only on `(SITE_COUNT, CONSTRAINTS)`.
//!
//! # See also
//!
//! - [Wiki: 09 Architecture Decisions § ADR-017 — empty-CONSTRAINTS closure rule][09]
//! - [Wiki: 09 Architecture Decisions § ADR-031 — `prism` is the standard library][09]
//! - [Wiki: 09 Architecture Decisions § ADR-057 — Bounded recursive structural typing][09]
//! - [Wiki: 09 Architecture Decisions § ADR-058 — κ-derivation as compression operator][09]
//! - [Wiki: 09 Architecture Decisions § ADR-059 — Atlas image / Hopf tower as codomain][09]
//!
//! [09]: https://github.com/UOR-Foundation/UOR-Framework/wiki/09-Architecture-Decisions
use GroundedShape;
use ;
use register_shape;
// ---- GGML quantization-block parameters per `ggml-common.h` ----
//
// Every per-dtype `BLOCK_BYTES` constant in this module is derived
// from these named parameters using the same arithmetic the ggml
// header's `static_assert` lines use. No per-dtype literal byte
// counts are written; updating the ggml side reduces to bumping the
// parameters here.
/// Super-block size for K-series and IQ-series quantization
/// (`QK_K` in ggml-common.h).
const QK_K: usize = 256;
/// Legacy 4-bit (linear) quantization block size
/// (`QK4_0` / `QK4_1` in ggml-common.h).
const QK4_0: usize = 32;
/// Legacy 5-bit quantization block size
/// (`QK5_0` / `QK5_1` in ggml-common.h).
const QK5_0: usize = 32;
/// Legacy 8-bit quantization block size
/// (`QK8_0` / `QK8_1` in ggml-common.h).
const QK8_0: usize = 32;
/// Non-linear 4-bit block size
/// (`QK4_NL` in ggml-common.h — distinct named constant despite
/// numeric equality with `QK4_0`).
const QK4_NL: usize = 32;
/// `sizeof(ggml_half)`: 16-bit float byte count — the canonical
/// per-block scale carrier across the K-series and most IQ-series
/// quantization types.
const GGML_HALF: usize = 2;
/// `sizeof(uint16_t)`: the IQ4_XS high-scales field byte count.
const GGML_U16: usize = 2;
/// `sizeof(float)`: 32-bit float byte count used by `block_q8_K`'s
/// super-block scale.
const GGML_FLOAT: usize = 4;
/// `sizeof(int16_t)`: per-sub-block sum carrier in `block_q8_K`.
const GGML_I16: usize = 2;
/// K-quant scales array byte count
/// (`K_SCALE_SIZE` in ggml-common.h).
const K_SCALE_SIZE: usize = 12;
/// IQ3_S per-block extra scale byte count
/// (`IQ3S_N_SCALE` in ggml-common.h).
const IQ3S_N_SCALE: usize = 4;
// ---- Native-width byte parameters (continuous IEEE 754 / integer
// element sizes — sized in bits, byte count = bits / 8) ----
const BITS_PER_BYTE: usize = 8;
/// Bytes in one IEEE 754 binary16 / Brain Float 16 element.
const F16_BYTES: usize = 16 / BITS_PER_BYTE;
/// Bytes in one IEEE 754 binary32 element.
const F32_BYTES: usize = 32 / BITS_PER_BYTE;
/// Bytes in one IEEE 754 binary64 element.
const F64_BYTES: usize = 64 / BITS_PER_BYTE;
/// Bytes in one 8-bit ONNX FLOAT8 element.
const F8_BYTES: usize = 8 / BITS_PER_BYTE;
/// Bytes in one signed/unsigned 8-bit integer.
const INT8_BYTES: usize = 8 / BITS_PER_BYTE;
/// Bytes in one signed/unsigned 16-bit integer.
const INT16_BYTES: usize = 16 / BITS_PER_BYTE;
/// Bytes in one signed/unsigned 32-bit integer.
const INT32_BYTES: usize = 32 / BITS_PER_BYTE;
/// Bytes in one signed/unsigned 64-bit integer.
const INT64_BYTES: usize = 64 / BITS_PER_BYTE;
/// Bytes in one byte-sized boolean.
const BOOL_BYTES: usize = 8 / BITS_PER_BYTE;
/// Bytes in one ONNX 4-bit packed block (two nibbles per byte).
const PACKED_NIBBLE_BYTES: usize = 8 / BITS_PER_BYTE;
/// Elements per ONNX 4-bit packed block (two nibbles per byte).
const PACKED_NIBBLE_ELEMS: usize = 2;
/// Bytes in one ONNX COMPLEX64 element (`(F32, F32)` real + imag).
const C64_BYTES: usize = 2 * F32_BYTES;
/// Bytes in one ONNX COMPLEX128 element (`(F64, F64)` real + imag).
const C128_BYTES: usize = 2 * F64_BYTES;
/// Tensor element-type discipline.
///
/// A `Dtype` declares the canonical block layout of one tensor element
/// (continuous types) or one quantization block (block-32 / block-256
/// quantized types). The trait is sealed per ADR-014; impls are confined
/// to this module and reachable through [`TensorDtypeRegistry`] for
/// container-format-realization enumeration per ADR-057.
///
/// Per ADR-017's closure rule, two `Dtype` impls with identical
/// [`BLOCK_BYTES`](Dtype::BLOCK_BYTES) content-address identically at the
/// framework level. The Rust-type distinction is the application-level
/// ergonomics surface (e.g., `F16` vs `BF16` are framework-equivalent
/// 2-byte shapes but Rust-distinct dtypes carrying distinct
/// floating-point interpretations at the container-format layer).
// ---- Continuous floating-point ----
decl_dtype!;
decl_dtype!;
decl_dtype!;
decl_dtype!;
// ---- ONNX 8-bit floating-point ----
decl_dtype!;
decl_dtype!;
decl_dtype!;
decl_dtype!;
// ---- ONNX complex ----
decl_dtype!;
decl_dtype!;
// ---- Signed integers ----
decl_dtype!;
decl_dtype!;
decl_dtype!;
decl_dtype!;
// ---- Unsigned integers ----
decl_dtype!;
decl_dtype!;
decl_dtype!;
decl_dtype!;
decl_dtype!;
// ---- ONNX packed 4-bit (TensorProto.DataType 21–23) ----
decl_dtype!;
decl_dtype!;
decl_dtype!;
// ---- GGML legacy block-32 quantization ----
//
// Per `ggml-common.h` `static_assert` lines, the block sizes are:
//
// block_q4_0: sizeof(ggml_half) + QK4_0 / 2
// block_q4_1: 2*sizeof(ggml_half) + QK4_1 / 2
// block_q5_0: sizeof(ggml_half) + sizeof(uint32_t) + QK5_0 / 2
// block_q5_1: 2*sizeof(ggml_half) + sizeof(uint32_t) + QK5_1 / 2
// block_q8_0: sizeof(ggml_half) + QK8_0
// block_q8_1: 2*sizeof(ggml_half) + QK8_1
//
// (Q5_0/Q5_1 use a `uint32_t qh` for high-bit storage, encoded as
// `2 * GGML_U16` here.)
decl_dtype!;
decl_dtype!;
decl_dtype!;
decl_dtype!;
decl_dtype!;
decl_dtype!;
// ---- GGML K-series block-256 quantization ----
//
// Per `ggml-common.h` `static_assert` lines:
//
// block_q2_K: 2*sizeof(ggml_half) + QK_K/16 + QK_K/4
// block_q3_K: sizeof(ggml_half) + QK_K/4 + QK_K/8 + K_SCALE_SIZE
// block_q4_K: 2*sizeof(ggml_half) + K_SCALE_SIZE + QK_K/2
// block_q5_K: 2*sizeof(ggml_half) + K_SCALE_SIZE + QK_K/2 + QK_K/8
// block_q6_K: sizeof(ggml_half) + QK_K/16 + 3*QK_K/4
// block_q8_K: sizeof(float) + QK_K + QK_K/16 * sizeof(int16_t)
decl_dtype!;
decl_dtype!;
decl_dtype!;
decl_dtype!;
decl_dtype!;
decl_dtype!;
// ---- GGML IQ-series importance-aware quantization ----
//
// Per `ggml-common.h` `static_assert` lines:
//
// block_iq1_s: sizeof(ggml_half) + QK_K/8 + QK_K/16
// block_iq1_m: QK_K/8 + QK_K/16 + QK_K/32
// (no half-prefix; per-block
// scales replace the half scale)
// block_iq2_xxs: sizeof(ggml_half) + QK_K/8 * sizeof(uint16_t)
// block_iq2_xs: sizeof(ggml_half) + QK_K/8 * sizeof(uint16_t) + QK_K/32
// block_iq2_s: sizeof(ggml_half) + QK_K/4 + QK_K/16
// block_iq3_xxs: sizeof(ggml_half) + 3*(QK_K/8)
// block_iq3_s: sizeof(ggml_half) + 13*(QK_K/32) + IQ3S_N_SCALE
// block_iq4_nl: sizeof(ggml_half) + QK4_NL/2
// block_iq4_xs: sizeof(ggml_half) + sizeof(uint16_t) + QK_K/64 + QK_K/2
decl_dtype!;
decl_dtype!;
decl_dtype!;
decl_dtype!;
decl_dtype!;
decl_dtype!;
decl_dtype!;
decl_dtype!;
decl_dtype!;
// ---- Shape-IRI registry per ADR-057 ----
register_shape!;