incin-core 0.1.0

Core tensor operations and static shape checking for incin
Documentation
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
//! The seal that makes a descriptor worth trusting.
//!
//! [`spec`](crate::exec::spec) resolves an operation into a descriptor. That
//! descriptor is internally consistent - its constructors derive every field
//! rather than accepting it - but internal consistency is not the property a
//! backend needs. A backend needs to know that the operation was checked
//! against the frontend's shape proof, and a bare exact descriptor cannot say
//! so, because anyone can build one.
//!
//! [`Validated<O>`] is that statement. It has private fields and a
//! `pub(crate)` constructor, so outside `incin-core` there is no way to
//! produce one except by asking a lowering rule (`EXE-003`) to produce it.
//! When `EXE-006` gives `Execute<O>` a `&Validated<O>` rather than a bare
//! descriptor, "this was validated" stops being a convention that kernels
//! re-check and becomes a type the compiler enforces.
//!
//! # Provenance, not just a boolean
//!
//! "Validated" alone would be too coarse. A shape proved entirely at compile
//! time and one checked at runtime a microsecond ago are both valid, but they
//! justify different amounts of work: the first can specialize a kernel on
//! constants, the second cannot. [`ProofLevel`] records which happened, and
//! travels with the descriptor so a backend can act on it.
//!
//! The level is derived from the shape types by `Shape::PROOF`, never passed
//! in by a caller. This matters: a caller who could assert `Static` for a
//! runtime shape would be handing kernels a constant that does not exist.

use crate::shapes::{ProofLevel, Shape};
use core::fmt;

#[cfg(feature = "paranoid-validation")]
use crate::shapes::error::ShapeError;

/// Runtime evidence lowered from a typed frontend proof.
///
/// This is the only shape information that crosses the backend boundary. It
/// preserves useful semantic facts without making backend executors generic
/// over the caller's concrete `Shape` type.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct ShapeEvidence {
    pub(crate) proof: ProofLevel,
    pub(crate) static_rank: Option<usize>,
    pub(crate) static_numel: Option<usize>,
}

impl ShapeEvidence {
    pub(crate) const fn dynamic() -> Self {
        Self {
            proof: ProofLevel::Dynamic,
            static_rank: None,
            static_numel: None,
        }
    }

    pub(crate) const fn of<S: Shape>() -> Self {
        Self {
            proof: S::PROOF,
            static_rank: S::RANK,
            static_numel: S::STATIC_NUMEL,
        }
    }

    #[must_use]
    /// Proof level proven at construction.
    pub const fn proof(self) -> ProofLevel {
        self.proof
    }

    /// Static rank, when fully static.
    pub const fn static_rank(self) -> Option<usize> {
        self.static_rank
    }

    /// Static element count, when derivable.
    pub const fn static_numel(self) -> Option<usize> {
        self.static_numel
    }
}

/// A [`ProofLevel`] that came from a shape *type* rather than from a caller's
/// claim about one.
///
/// [`dispatch::execute`](crate::exec::dispatch::execute) is generic over the
/// operation and the backend but not over the operand shapes, so it has no `S`
/// to read [`Shape::PROOF`] from and passes [`Dynamic`](ProofLevel::Dynamic)
/// for everything. The typed tensor frontend *does* hold `S`, and this is the
/// value that carries what it knows across that boundary.
///
/// The reason it is a distinct type rather than a bare `ProofLevel` parameter
/// is provenance. A plain enum argument would let any caller write
/// `ProofLevel::Static` beside whatever metadata it liked, which is precisely
/// the forgery [`Validated`] exists to prevent one layer down. The only
/// constructors here are `ShapeEvidence::of`, which reads the level off a shape
/// type and cannot be told a different answer, and
/// `ShapeEvidence::dynamic`, which claims nothing.
///
/// A `Shape` implemented outside this crate can still overstate its own
/// `PROOF`, exactly as it can today - the trait's default is
/// [`Dynamic`](ProofLevel::Dynamic) so silence is never credited, and a wrong
/// override is a wrong specialization rather than unsoundness. What this type
/// removes is the ability to assert a level with *no* type behind it at all.
///
/// [`Shape::PROOF`]: crate::shapes::Shape::PROOF
/// A descriptor together with the proof that produced it.
///
/// The only way to obtain one outside `incin-core` is from a lowering rule,
/// because its constructor is `pub(crate)` and the fields are private.
/// A backend receiving `&Validated<Descriptor<op::MatMulExact>>` therefore knows the geometry
/// was checked against the frontend's shape proof, and does not need to check
/// it again.
///
/// The wrapper is deliberately thin: it adds provenance and takes away the
/// ability to forge, and it does nothing else. Everything about the operation
/// is still readable through [`descriptor`](Self::descriptor).
///
/// # Why not just a `validated: bool` field on the descriptor
///
/// Because a field can be set. The guarantee has to live somewhere the caller
/// cannot reach, and in Rust that is the privacy boundary, not a value.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Validated<O> {
    descriptor: O,
    proof: ProofLevel,
    evidence: ShapeEvidence,
}

impl<O> Validated<O> {
    /// Seal `descriptor` with the proof that justified it.
    ///
    /// Crate-private on purpose, and the reason this type exists. `EXE-003`'s
    /// lowering rules are its only callers; they hold the shape types the
    /// proof is derived from, which no external caller does.
    pub(crate) const fn new(descriptor: O, proof: ProofLevel) -> Self {
        Self {
            descriptor,
            proof,
            evidence: ShapeEvidence {
                proof,
                ..ShapeEvidence::dynamic()
            },
        }
    }

    pub(crate) const fn new_with_evidence(descriptor: O, evidence: ShapeEvidence) -> Self {
        Self {
            descriptor,
            proof: evidence.proof,
            evidence,
        }
    }

    /// The resolved operation.
    #[must_use]
    pub const fn descriptor(&self) -> &O {
        &self.descriptor
    }

    /// How much of the operation the compiler settled.
    #[must_use]
    pub const fn proof_level(&self) -> ProofLevel {
        self.proof
    }

    /// Typed shape facts lowered alongside this validated descriptor.
    #[must_use]
    pub const fn shape_evidence(&self) -> ShapeEvidence {
        self.evidence
    }

    /// Take the descriptor back out, discarding the proof.
    ///
    /// For a backend that wants to store the geometry after dispatch. The
    /// result is an ordinary descriptor with no standing to be trusted, which
    /// is why this consumes `self` rather than copying: a `Validated` and a
    /// bare descriptor should not be alive at once and confusable.
    #[must_use]
    pub fn into_descriptor(self) -> O {
        self.descriptor
    }
}

impl<O: fmt::Debug> fmt::Display for Validated<O> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:?} [{} proof]", self.descriptor, self.proof)
    }
}

/// Recompute what the constructors already proved.
///
/// `PROPOSALS.md` §1.2.2 describes `paranoid-validation` as a debug aid that
/// "recomputes logical facts inside an executor", explicitly not the normal
/// contract. That framing sets the shape of this API: it is a check a test or
/// a debug build runs, never something a kernel's correctness depends on.
///
/// The fact worth rechecking is the one `EXE-001` made a constructor
/// obligation - that the output can actually be indexed. If this ever returns
/// `Err`, a descriptor reached a backend without going through a checked
/// constructor, which is a bug in the lowering layer rather than in the
/// caller's shapes.
#[cfg(feature = "paranoid-validation")]
impl<O: super::spec::ExecutionDescriptor> Validated<O> {
    /// Re-derive the descriptor's own invariants and report disagreement.
    ///
    /// Available only under the `paranoid-validation` feature, so a release
    /// build cannot pay for it by accident.
    pub fn audit(&self) -> Result<(), ShapeError> {
        self.descriptor.output_shape().map_or(Ok(()), |shape| {
            shape
                .checked_numel(crate::shapes::error::OperationKind::Storage)
                .map(|_| ())
        })
    }
}

/// Assert a descriptor's invariants when `paranoid-validation` is on.
///
/// Expands to nothing otherwise, so an executor can call it on a hot path and
/// pay only in the builds that asked to pay. Takes a [`Validated`] because a
/// bare descriptor has nothing to audit *against*.
#[macro_export]
macro_rules! paranoid_audit {
    ($validated:expr) => {{
        #[cfg(feature = "paranoid-validation")]
        {
            $crate::exec::proof::__audit_or_panic(&$validated);
        }
        #[cfg(not(feature = "paranoid-validation"))]
        {
            let _ = &$validated;
        }
    }};
}

/// Panicking form of [`Validated::audit`], called by [`paranoid_audit!`].
///
/// Panics rather than returning, because a failure here means an unvalidated
/// descriptor is already in flight and there is no correct way to continue.
#[cfg(feature = "paranoid-validation")]
#[doc(hidden)]
pub fn __audit_or_panic<O: super::spec::ExecutionDescriptor>(validated: &Validated<O>) {
    if let Err(error) = validated.audit() {
        panic!("paranoid-validation: descriptor failed its own invariants: {error:?}");
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::exec::catalog::NoAttributes;
    use crate::exec::{Descriptor, LogicalTensorMeta, op};
    use crate::shapes::{Dyn, ShapeBuf};
    use typenum::{U2, U3};
    type Static23 = crate::shapes::DimCons<U2, crate::shapes::DimCons<U3, crate::shapes::Nil>>;
    type Mixed23 = crate::shapes::DimCons<U2, crate::shapes::DimCons<usize, crate::shapes::Nil>>;
    type Mixed32 = crate::shapes::DimCons<usize, crate::shapes::DimCons<U3, crate::shapes::Nil>>;
    type MixedDyn =
        crate::shapes::DimCons<usize, crate::shapes::DimCons<usize, crate::shapes::Nil>>;

    // These live inside the module rather than in `tests/` because
    // `Validated::new` is `pub(crate)`. That is the point of the type, so the
    // seal is proved from outside by the `compile_fail` cases instead.

    #[test]
    fn a_fully_typed_shape_proves_itself_statically() {
        assert_eq!(ProofLevel::of::<Static23>(), ProofLevel::Static);
        assert_eq!(ProofLevel::of::<crate::shapes::Nil>(), ProofLevel::Static);
    }

    #[test]
    fn one_runtime_axis_weakens_the_whole_shape() {
        assert_eq!(ProofLevel::of::<Mixed23>(), ProofLevel::Mixed);
        assert_eq!(ProofLevel::of::<Mixed32>(), ProofLevel::Mixed);
        assert_eq!(ProofLevel::of::<MixedDyn>(), ProofLevel::Mixed);
    }

    #[test]
    fn a_ranked_shape_knows_its_rank_but_not_its_extents() {
        assert_eq!(
            ProofLevel::of::<crate::shapes::Ranked<typenum::U3>>(),
            ProofLevel::Mixed
        );
        assert!(ProofLevel::of::<crate::shapes::Ranked<typenum::U3>>().has_static_rank());
        assert!(!ProofLevel::of::<crate::shapes::Ranked<typenum::U3>>().is_static());
    }

    #[test]
    fn an_unranked_shape_proves_nothing_before_it_exists() {
        assert_eq!(ProofLevel::of::<Dyn>(), ProofLevel::Dynamic);
        assert!(!ProofLevel::of::<Dyn>().has_static_rank());
    }

    #[test]
    fn a_named_axis_is_typed_but_not_statically_sized() {
        crate::dim!(Batch);
        // Naming an axis is what lets the compiler reject mixing it with a
        // different name. It says nothing about how large it is.
        assert_eq!(
            ProofLevel::of::<
                crate::shapes::DimCons<
                    crate::shapes::dim::NamedDim<Batch, usize>,
                    crate::shapes::DimCons<U3, crate::shapes::Nil>,
                >,
            >(),
            ProofLevel::Mixed
        );
    }

    #[test]
    fn meet_takes_the_weaker_operand() {
        assert_eq!(
            ProofLevel::Static.meet(ProofLevel::Mixed),
            ProofLevel::Mixed
        );
        assert_eq!(
            ProofLevel::Mixed.meet(ProofLevel::Dynamic),
            ProofLevel::Dynamic
        );
        assert_eq!(
            ProofLevel::Static.meet(ProofLevel::Static),
            ProofLevel::Static
        );
    }

    #[test]
    fn meet_is_commutative_idempotent_and_topped_by_static() {
        let levels = [ProofLevel::Static, ProofLevel::Mixed, ProofLevel::Dynamic];
        for a in levels {
            assert_eq!(a.meet(a), a, "idempotent");
            assert_eq!(ProofLevel::Static.meet(a), a, "Static is the identity");
            for b in levels {
                assert_eq!(a.meet(b), b.meet(a), "commutative");
                for c in levels {
                    assert_eq!(
                        a.meet(b).meet(c),
                        a.meet(b.meet(c)),
                        "associative, so folding N operands is order-independent"
                    );
                }
            }
        }
    }

    #[test]
    fn a_sealed_descriptor_still_reads_back_exactly() {
        let spec = Descriptor::<op::Add>::infer_runtime(
            NoAttributes,
            vec![
                LogicalTensorMeta {
                    shape: Some(ShapeBuf::from_slice(&[2, 3])),
                    dtype: None,
                    device: None,
                },
                LogicalTensorMeta {
                    shape: Some(ShapeBuf::from_slice(&[1, 3])),
                    dtype: None,
                    device: None,
                },
            ],
        )
        .unwrap()
        .into_descriptor();
        let validated = Validated::new(spec.clone(), ProofLevel::Mixed);

        assert_eq!(validated.descriptor(), &spec);
        assert_eq!(validated.proof_level(), ProofLevel::Mixed);
        assert_eq!(validated.into_descriptor(), spec);
    }

    #[test]
    fn the_proof_is_carried_not_recomputed() {
        // A `Static` stamp on a runtime-built descriptor is a lie the type
        // system cannot catch, which is exactly why `new` is crate-private:
        // only a lowering rule holding the shape types may call it.
        let spec = Descriptor::<op::Add>::infer_runtime(
            NoAttributes,
            vec![
                LogicalTensorMeta {
                    shape: Some(ShapeBuf::from_slice(&[2, 3])),
                    dtype: None,
                    device: None,
                },
                LogicalTensorMeta {
                    shape: Some(ShapeBuf::from_slice(&[2, 3])),
                    dtype: None,
                    device: None,
                },
            ],
        )
        .unwrap()
        .into_descriptor();
        let validated = Validated::new(spec, ProofLevel::of::<Static23>());
        assert!(validated.proof_level().is_static());
    }

    #[cfg(feature = "paranoid-validation")]
    #[test]
    fn audit_passes_for_a_descriptor_from_a_checked_constructor() {
        let spec = Descriptor::<op::Add>::infer_runtime(
            NoAttributes,
            vec![
                LogicalTensorMeta {
                    shape: Some(ShapeBuf::from_slice(&[2, 3])),
                    dtype: None,
                    device: None,
                },
                LogicalTensorMeta {
                    shape: Some(ShapeBuf::from_slice(&[1, 3])),
                    dtype: None,
                    device: None,
                },
            ],
        )
        .unwrap()
        .into_descriptor();
        let validated = Validated::new(spec, ProofLevel::Mixed);
        assert!(validated.audit().is_ok());
    }
}