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
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
use super::*;

/// One immutable row derived from the authoritative operation declaration.
pub struct OperationCatalogEntry {
    /// Exact catalog operation this row describes.
    pub operation: OperationKind,
    /// Snake_case catalog name.
    pub name: &'static str,
    /// Family grouping used for dispatch profiles.
    pub family: OperationKind,
    /// Compute profile classification.
    pub profile: SemanticProfile,
    /// Descriptor type name owning this row.
    pub descriptor: &'static str,
    /// Attribute type name for this row.
    pub attributes: &'static str,
    /// Accepted input count range.
    pub input_arity: core::ops::RangeInclusive<usize>,
    /// Produced output count range.
    pub output_arity: core::ops::RangeInclusive<usize>,
    /// Supported rank range.
    pub accepted_ranks: core::ops::RangeInclusive<usize>,
    /// How inputs combine element-wise.
    pub broadcasting: BroadcastingRule,
    /// Input dtype policy.
    pub dtype: DTypeRule,
    /// Output dtype policy.
    pub output: OutputRule,
    /// Whether all inputs must share one device.
    pub same_device: bool,
    /// Behavior on zero-element inputs.
    pub empty: EmptyRule,
    /// Numeric-domain guarantees.
    pub numeric: NumericRule,
    /// Derivative contract.
    pub gradient: GradientRule,
    /// Whether repeated runs are bitwise reproducible.
    pub deterministic: bool,
    /// Output layout behavior.
    pub layout: LayoutRule,
    /// Legacy method this row superseded.
    pub legacy_source: &'static str,
    /// Whether the op can be captured into compiled plans.
    pub capture_eligible: bool,
    /// Where the operation executes.
    pub site: ExecutionSite,
}

/// Classify one operation by where its result is produced.
///
/// The default is [`ExecutionSite::Kernel`], which is the fail-closed answer: a
/// newly declared operation is assumed to owe an executor. Being wrong that way
/// leaves an unmigrated operation visible in the migration report; being wrong
/// the other way would silently excuse it.
const fn execution_site(operation: OperationKind) -> ExecutionSite {
    match operation {
        // Every one of these calls its backend method with `&mut`, either on a
        // storage handle or on a `Var<K>`, and returns nothing.
        OperationKind::AddInPlace
        | OperationKind::SubInPlace
        | OperationKind::MulInPlace
        | OperationKind::DivInPlace
        | OperationKind::ZeroInPlace
        | OperationKind::FillInPlace
        | OperationKind::SgdStep
        | OperationKind::AdamStep
        | OperationKind::AdamWStep => ExecutionSite::Mutation,
        OperationKind::ToDevice => ExecutionSite::DeviceTransfer,
        OperationKind::RequireGrad | OperationKind::Detach | OperationKind::Backward => {
            ExecutionSite::GraphState
        }
        // Declared by `OutputRule::HostValue` too; kept as an explicit list
        // because the two answer different questions and a future host-value
        // operation might well be executable.
        OperationKind::TensorToBytes
        | OperationKind::ToHostFloatScalar
        | OperationKind::ToHostFloatVec
        | OperationKind::ToHostIntScalar
        | OperationKind::ToHostIntVec => ExecutionSite::HostReadback,
        OperationKind::Sample | OperationKind::Rnn | OperationKind::Lstm => ExecutionSite::Composed,
        OperationKind::TensorFromData
        | OperationKind::TensorFromBytes
        | OperationKind::Zeros
        | OperationKind::Ones
        | OperationKind::UniformRandom
        | OperationKind::NormalRandom
        | OperationKind::VariableZeros
        | OperationKind::VariableOnes
        | OperationKind::VariableUniformRandom
        | OperationKind::VariableNormalRandom
        | OperationKind::Full
        | OperationKind::Arange
        | OperationKind::Linspace => ExecutionSite::Creation,
        _ => ExecutionSite::Kernel,
    }
}

const fn profile_semantics(
    profile: SemanticProfile,
) -> (
    BroadcastingRule,
    DTypeRule,
    OutputRule,
    EmptyRule,
    NumericRule,
    GradientRule,
    LayoutRule,
) {
    use SemanticProfile::*;
    match profile {
        Creation => (
            BroadcastingRule::None,
            DTypeRule::ExplicitOutput,
            OutputRule::Created,
            EmptyRule::Allowed,
            NumericRule::TypedContract,
            GradientRule::None,
            LayoutRule::FreshContiguous,
        ),
        UnaryFloat => (
            BroadcastingRule::None,
            DTypeRule::Floating,
            OutputRule::Preserve,
            EmptyRule::Allowed,
            NumericRule::IeeePropagate,
            GradientRule::Defined,
            LayoutRule::FreshContiguous,
        ),
        BinaryBroadcast => (
            BroadcastingRule::Numpy,
            DTypeRule::NumericSame,
            OutputRule::Broadcast,
            EmptyRule::Allowed,
            NumericRule::IeeePropagate,
            GradientRule::Defined,
            LayoutRule::FreshContiguous,
        ),
        Comparison => (
            BroadcastingRule::Numpy,
            DTypeRule::BooleanResult,
            OutputRule::Broadcast,
            EmptyRule::Allowed,
            NumericRule::IeeePropagate,
            GradientRule::None,
            LayoutRule::FreshContiguous,
        ),
        Logical => (
            BroadcastingRule::Numpy,
            DTypeRule::Boolean,
            OutputRule::Broadcast,
            EmptyRule::Allowed,
            NumericRule::NotApplicable,
            GradientRule::None,
            LayoutRule::FreshContiguous,
        ),
        Mutation => (
            BroadcastingRule::None,
            DTypeRule::NumericSame,
            OutputRule::Preserve,
            EmptyRule::Allowed,
            NumericRule::IeeePropagate,
            GradientRule::Defined,
            LayoutRule::PreserveOrMaterialize,
        ),
        Shape => (
            BroadcastingRule::TypedContract,
            DTypeRule::Preserve,
            OutputRule::ShapeAttributes,
            EmptyRule::Allowed,
            NumericRule::NotApplicable,
            GradientRule::Defined,
            LayoutRule::ViewWhenPossible,
        ),
        Selection => (
            BroadcastingRule::Numpy,
            DTypeRule::TypedContract,
            OutputRule::Broadcast,
            EmptyRule::Allowed,
            NumericRule::TypedContract,
            GradientRule::Piecewise,
            LayoutRule::FreshContiguous,
        ),
        Indexing => (
            BroadcastingRule::None,
            DTypeRule::TypedContract,
            OutputRule::Indexing,
            EmptyRule::TypedContract,
            NumericRule::CheckedInteger,
            GradientRule::Defined,
            LayoutRule::FreshContiguous,
        ),
        MatMul => (
            BroadcastingRule::TypedContract,
            DTypeRule::Floating,
            OutputRule::MatMul,
            EmptyRule::IdentityOrDefined,
            NumericRule::StableAccumulation,
            GradientRule::Defined,
            LayoutRule::FreshContiguous,
        ),
        Attention => (
            BroadcastingRule::TypedContract,
            DTypeRule::Floating,
            OutputRule::TypedInference,
            EmptyRule::TypedContract,
            NumericRule::StableAccumulation,
            GradientRule::Defined,
            LayoutRule::FreshContiguous,
        ),
        Reduction => (
            BroadcastingRule::None,
            DTypeRule::Floating,
            OutputRule::Reduction,
            EmptyRule::RejectedWhenReductionIsEmpty,
            NumericRule::StableAccumulation,
            GradientRule::Defined,
            LayoutRule::FreshContiguous,
        ),
        IndexReduction => (
            BroadcastingRule::None,
            DTypeRule::IndexResult,
            OutputRule::Reduction,
            EmptyRule::RejectedWhenReductionIsEmpty,
            NumericRule::IeeePropagate,
            GradientRule::None,
            LayoutRule::FreshContiguous,
        ),
        Scan => (
            BroadcastingRule::None,
            DTypeRule::Preserve,
            OutputRule::Preserve,
            EmptyRule::Allowed,
            NumericRule::StableAccumulation,
            GradientRule::Defined,
            LayoutRule::FreshContiguous,
        ),
        Normalization => (
            BroadcastingRule::TypedContract,
            DTypeRule::Floating,
            OutputRule::Preserve,
            EmptyRule::RejectedWhenReductionIsEmpty,
            NumericRule::StableAccumulation,
            GradientRule::Defined,
            LayoutRule::FreshContiguous,
        ),
        Module | Composite => (
            BroadcastingRule::TypedContract,
            DTypeRule::TypedContract,
            OutputRule::TypedInference,
            EmptyRule::TypedContract,
            NumericRule::TypedContract,
            GradientRule::Defined,
            LayoutRule::FreshContiguous,
        ),
        Loss => (
            BroadcastingRule::TypedContract,
            DTypeRule::Floating,
            OutputRule::Reduction,
            EmptyRule::RejectedWhenReductionIsEmpty,
            NumericRule::StableAccumulation,
            GradientRule::Defined,
            LayoutRule::FreshContiguous,
        ),
        Quantized => (
            BroadcastingRule::TypedContract,
            DTypeRule::Quantized,
            OutputRule::TypedInference,
            EmptyRule::TypedContract,
            NumericRule::TypedContract,
            GradientRule::None,
            LayoutRule::FreshContiguous,
        ),
        Optimizer => (
            BroadcastingRule::None,
            DTypeRule::Floating,
            OutputRule::TypedInference,
            EmptyRule::Allowed,
            NumericRule::TypedContract,
            GradientRule::None,
            LayoutRule::FreshContiguous,
        ),
        Transfer => (
            BroadcastingRule::None,
            DTypeRule::TypedContract,
            OutputRule::TypedInference,
            EmptyRule::Allowed,
            NumericRule::NotApplicable,
            GradientRule::TypedContract,
            LayoutRule::PreserveOrMaterialize,
        ),
        Autograd => (
            BroadcastingRule::None,
            DTypeRule::Preserve,
            OutputRule::Preserve,
            EmptyRule::Allowed,
            NumericRule::NotApplicable,
            GradientRule::None,
            LayoutRule::PreserveOrMaterialize,
        ),
    }
}

#[allow(clippy::too_many_arguments)]
pub(super) const fn entry(
    operation: OperationKind,
    name: &'static str,
    family: OperationKind,
    profile: SemanticProfile,
    descriptor: &'static str,
    attributes: &'static str,
    min_arity: usize,
    max_arity: usize,
    legacy_source: &'static str,
) -> OperationCatalogEntry {
    let (mut broadcasting, dtype, mut output, mut empty, numeric, mut gradient, layout) =
        profile_semantics(profile);
    let accepted_ranks = match operation {
        OperationKind::MatMulExact | OperationKind::QuantizedMatMul => 2..=usize::MAX,
        OperationKind::Dot | OperationKind::Outer => 1..=1,
        OperationKind::BatchedMatMul | OperationKind::Addmm => 2..=3,
        OperationKind::Rnn | OperationKind::Lstm => 2..=3,
        OperationKind::Conv1dExact => 2..=3,
        OperationKind::Conv2dExact
        | OperationKind::ConvTranspose2d
        | OperationKind::MaxPool2d
        | OperationKind::AvgPool2d
        | OperationKind::AdaptiveAvgPool2dExact => 3..=4,
        OperationKind::Softmax
        | OperationKind::GroupNorm
        | OperationKind::InstanceNorm
        | OperationKind::LayerNorm
        | OperationKind::BatchNorm
        | OperationKind::RmsNorm => 1..=usize::MAX,
        _ => 0..=usize::MAX,
    };
    let output_arity = match operation {
        OperationKind::TopK => 2..=2,
        OperationKind::Chunk | OperationKind::Split => 0..=usize::MAX,
        OperationKind::Rnn => 2..=2,
        OperationKind::Lstm => 3..=3,
        OperationKind::ToHostFloatScalar
        | OperationKind::ToHostFloatVec
        | OperationKind::ToHostIntScalar
        | OperationKind::ToHostIntVec
        | OperationKind::TensorToBytes => 0..=0,
        OperationKind::Backward => 0..=0,
        OperationKind::AdamStep | OperationKind::AdamWStep => 3..=3,
        _ => 1..=1,
    };
    if matches!(
        operation,
        OperationKind::BroadcastAs | OperationKind::BroadcastLeft
    ) {
        broadcasting = BroadcastingRule::ExplicitTarget;
    }
    if matches!(operation, OperationKind::ToDType) {
        output = OutputRule::ExplicitDType;
    } else if matches!(operation, OperationKind::ToDevice) {
        output = OutputRule::Preserve;
    } else if matches!(
        operation,
        OperationKind::Dot
            | OperationKind::Outer
            | OperationKind::Addmm
            | OperationKind::ScaledDotProductAttention
            | OperationKind::Linear
    ) {
        output = OutputRule::TypedInference;
    } else if matches!(operation, OperationKind::QuantizedMatMul) {
        output = OutputRule::MatMul;
    } else if matches!(
        operation,
        OperationKind::Conv1dExact
            | OperationKind::Conv2dExact
            | OperationKind::ConvTranspose2d
            | OperationKind::MaxPool2d
            | OperationKind::AvgPool2d
            | OperationKind::AdaptiveAvgPool2dExact
            | OperationKind::Rnn
            | OperationKind::Lstm
    ) {
        output = OutputRule::ShapeAttributes;
    } else if matches!(
        operation,
        OperationKind::LayerNorm
            | OperationKind::BatchNorm
            | OperationKind::GroupNorm
            | OperationKind::InstanceNorm
            | OperationKind::RmsNorm
            | OperationKind::Dropout
            | OperationKind::MaskedFill
            | OperationKind::Scatter
    ) {
        output = OutputRule::Preserve;
    } else if matches!(
        operation,
        OperationKind::ToHostFloatScalar
            | OperationKind::ToHostFloatVec
            | OperationKind::ToHostIntScalar
            | OperationKind::ToHostIntVec
            | OperationKind::TensorToBytes
    ) {
        output = OutputRule::HostValue;
    }
    if matches!(
        operation,
        OperationKind::Step
            | OperationKind::Sign
            | OperationKind::Floor
            | OperationKind::Ceil
            | OperationKind::Round
            | OperationKind::Trunc
            | OperationKind::Frac
    ) {
        gradient = GradientRule::Undefined;
    }
    if matches!(operation, OperationKind::Scatter) {
        gradient = GradientRule::Undefined;
    } else if matches!(
        operation,
        OperationKind::TensorToBytes
            | OperationKind::ToHostFloatScalar
            | OperationKind::ToHostFloatVec
            | OperationKind::ToHostIntScalar
            | OperationKind::ToHostIntVec
    ) {
        gradient = GradientRule::None;
    } else if matches!(operation, OperationKind::ToDevice) {
        gradient = GradientRule::Defined;
    }
    if matches!(
        operation,
        OperationKind::SumAll
            | OperationKind::SumDim
            | OperationKind::SumKeepDim
            | OperationKind::ProdAll
            | OperationKind::ProdDim
            | OperationKind::Cumsum
    ) {
        empty = EmptyRule::IdentityOrDefined;
    }
    OperationCatalogEntry {
        operation,
        name,
        family,
        profile,
        descriptor,
        attributes,
        input_arity: min_arity..=max_arity,
        output_arity,
        accepted_ranks,
        broadcasting,
        dtype,
        output,
        same_device: !matches!(
            profile,
            SemanticProfile::Creation | SemanticProfile::Transfer
        ),
        empty,
        numeric,
        gradient,
        deterministic: !matches!(
            operation,
            OperationKind::UniformRandom
                | OperationKind::NormalRandom
                | OperationKind::VariableUniformRandom
                | OperationKind::VariableNormalRandom
                | OperationKind::Sample
                | OperationKind::Dropout
                | OperationKind::Scatter
                | OperationKind::TopK
                | OperationKind::Argsort
        ),
        layout,
        legacy_source,
        capture_eligible: !matches!(
            profile,
            SemanticProfile::Optimizer | SemanticProfile::Transfer
        ) && !matches!(
            operation,
            OperationKind::Sample | OperationKind::Backward
        ),
        site: execution_site(operation),
    }
}

macro_rules! define_catalog {
    ($(($variant:ident, $name:literal, $family:ident, $profile:ident, $attrs:ident, $min:expr, $max:expr, $legacy:literal),)*) => {
        /// Stable operation inventory. Its length is also the uniqueness proof:
        /// enum identities cannot repeat, and the meta-test rejects duplicate names.
        pub static OPERATION_CATALOG: &[OperationCatalogEntry] = &[
            $(entry(
                OperationKind::$variant,
                $name,
                OperationKind::$family,
                SemanticProfile::$profile,
                concat!("Descriptor<op::", stringify!($variant), ">"),
                stringify!($attrs),
                $min,
                $max,
                $legacy,
            ),)*
        ];

        /// Exact marker types used by `Descriptor<O>` and `Execute<O>`.
        pub mod op {
            $(
                #[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
                /// Unit struct marker generated per descriptor variant.
                pub struct $variant;
            )*
        }

        $(
            impl Operation for op::$variant {
                type Attributes = $attrs;
                const KEY: OperationKey = OperationKey {
                    namespace: Cow::Borrowed("incin"),
                    name: Cow::Borrowed($name),
                    version: 1,
                };
                fn infer_outputs(
                    attributes: &Self::Attributes,
                    inputs: &[LogicalTensorMeta],
                ) -> Result<Vec<LogicalTensorMeta>, DescriptorError> {
                    let row = catalog_entry(OperationKind::$variant).ok_or(
                        DescriptorError::MissingCatalogEntry {
                            operation: OperationKind::$variant,
                        },
                    )?;
                    infer_outputs(OperationKind::$variant, row, attributes, inputs)
                }

                fn infer_invocation(
                    attributes: Self::Attributes,
                    inputs: Vec<LogicalTensorMeta>,
                ) -> Result<ValidatedInvocation<Self>, DescriptorError> {
                    ValidatedInvocation::<Self>::infer_runtime(attributes, inputs)
                }

                fn infer_invocation_typed<S: crate::shapes::Shape>(
                    attributes: Self::Attributes,
                    inputs: Vec<LogicalTensorMeta>,
                    expected: &crate::shapes::ShapeValue<S>,
                ) -> Result<ValidatedInvocation<Self>, DescriptorError> {
                    ValidatedInvocation::<Self>::infer_typed(attributes, inputs, expected)
                }
            }

            impl CanonicalOperation for op::$variant {
                const ID: OperationKind = OperationKind::$variant;
            }
        )*
    };
}

// The `incin_operation_catalog!` invocation that expands this template lives
// in descriptor.rs (matching the original file's layout, right after
// `CanonicalOperation`), not here where it's defined - this re-export is
// what makes `define_catalog!` reachable there via `use super::*;`.
pub(crate) use define_catalog;