tokitai-operator 0.1.0

Verified DL kernel compiler: formally-checked GEMM, p-adic, sheaf, contract-carrying ops. Paper-artifact grade.
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
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
//! Shape-manipulation operators.
//!
//! Each op takes one or more dense tensors and produces a dense tensor
//! with a different shape, same underlying element order (Reshape /
//! Permute / Transpose / Slice) or different element order (Concat /
//! Broadcast). The shape ops never change the element type or the
//! mathematical domain; they only change layout metadata. As a result,
//! they are backend-neutral and lowering is purely a data-movement
//! operation that preserves exact i64 values.
//!
//! Permute vs. Transpose:
//!   - `Transpose` swaps exactly two axes; rank is preserved.
//!   - `Permute` takes a full permutation vector of length `rank`.
//!     `Transpose` is a strict subset of `Permute`; we keep both as
//!     separate ops so the planner can pick the cheaper one when only
//!     two axes are involved.
//!
//! Slice semantics:
//!   - `Slice` uses the Python half-open convention `start..end`
//!     (the slice covers elements at indices `start, start+1, ...,
//!     end-1`). An end of `usize::MAX` means "to the end of the
//!     axis".
//!
//! Concat semantics:
//!   - `Concat` accepts 2 or more input tensors. All inputs must
//!     have the same rank, and the same shape on every axis except
//!     the concatenation axis. Output rank equals input rank.

use crate::domain::{Claim, Contract, ContractId, ContractSet, Evidence, Scope};
use crate::object::{Dim, ObjectKind, ObjectMeta, Shape};
use crate::{Error, Result};

use super::{LayerBehavior, OpInput, OpOutput, OpSignature, Operator};

// ---------------------------------------------------------------------------
// Small helper: deterministic contracts for shape-preserving data movement.
// ---------------------------------------------------------------------------
fn shape_arithmetic_contracts(scope: Scope) -> ContractSet {
    ContractSet::from_iter([
        Contract::new(
            ContractId(30),
            Claim::Deterministic,
            scope.clone(),
            Evidence::Axiom,
        ),
        Contract::new(ContractId(31), Claim::Exact, scope, Evidence::Axiom),
    ])
}

fn tensor_input_check(op: &str, inputs: &[ObjectMeta], expected: usize) -> Result<()> {
    if inputs.len() != expected {
        return Err(Error::operator(format!(
            "{op} expects {expected} input(s), got {}",
            inputs.len()
        )));
    }
    for (i, m) in inputs.iter().enumerate() {
        if m.object_kind != ObjectKind::Tensor {
            return Err(Error::operator(format!(
                "{op} only supports tensor inputs, input {i} is {:?}",
                m.object_kind
            )));
        }
    }
    Ok(())
}

fn parse_target_shape(meta: &ObjectMeta) -> Result<Vec<Dim>> {
    if meta.object_kind != ObjectKind::Tensor {
        return Err(Error::operator(format!(
            "target shape input must be a tensor, got {:?}",
            meta.object_kind
        )));
    }
    Ok(meta.shape.dims.clone())
}

// ---------------------------------------------------------------------------
// Reshape: out = input reshaped to a target shape (element count must match).
// ---------------------------------------------------------------------------

/// Reshape a tensor to a new shape. Element count must match exactly
/// (when both source and target shapes are fully static).
#[derive(Debug, Clone, Copy, Default)]
pub struct ReshapeOp;

impl Operator for ReshapeOp {
    fn name(&self) -> &'static str {
        "reshape"
    }

    fn signature(&self) -> OpSignature {
        OpSignature {
            inputs: vec![
                OpInput {
                    name: "input".to_string(),
                },
                OpInput {
                    name: "target_shape".to_string(),
                },
            ],
            outputs: vec![OpOutput {
                name: "out".to_string(),
            }],
        }
    }

    fn infer(&self, inputs: &[ObjectMeta]) -> Result<Vec<ObjectMeta>> {
        tensor_input_check(self.name(), inputs, 2)?;
        let target_dims = parse_target_shape(&inputs[1])?;
        let mut output = inputs[0].clone();
        output.shape = Shape::new(target_dims);
        Ok(vec![output])
    }

    fn required_contracts(&self) -> ContractSet {
        ContractSet::new()
    }
    fn provided_contracts(&self) -> ContractSet {
        shape_arithmetic_contracts(Scope::Operator(self.name().to_string()))
    }
    fn layer_behavior(&self) -> Vec<LayerBehavior> {
        vec![LayerBehavior::Pointwise, LayerBehavior::Global]
    }
}

// ---------------------------------------------------------------------------
// Transpose: swap exactly two axes.
// ---------------------------------------------------------------------------

/// Transpose (swap two axes). The two axes are read from a 2-element
/// i32 tensor input (axis0, axis1). The lowerings support negative
/// indexing.
#[derive(Debug, Clone, Copy, Default)]
pub struct TransposeOp;

impl Operator for TransposeOp {
    fn name(&self) -> &'static str {
        "transpose"
    }

    fn signature(&self) -> OpSignature {
        OpSignature {
            inputs: vec![
                OpInput {
                    name: "input".to_string(),
                },
                OpInput {
                    name: "axes".to_string(),
                },
            ],
            outputs: vec![OpOutput {
                name: "out".to_string(),
            }],
        }
    }

    fn infer(&self, inputs: &[ObjectMeta]) -> Result<Vec<ObjectMeta>> {
        tensor_input_check(self.name(), inputs, 2)?;
        let rank = inputs[0].shape.rank();
        let mut output = inputs[0].clone();
        // We don't know the axis values at type-erased plan time, so
        // we keep the same rank but record the op as a generic
        // permutation. The lowering will re-order the data
        // according to the runtime axes tensor.
        output.shape = Shape::new(
            (0..rank)
                .map(|i| inputs[0].shape.dims[i].clone())
                .collect::<Vec<Dim>>(),
        );
        Ok(vec![output])
    }

    fn required_contracts(&self) -> ContractSet {
        ContractSet::new()
    }
    fn provided_contracts(&self) -> ContractSet {
        shape_arithmetic_contracts(Scope::Operator(self.name().to_string()))
    }
    fn layer_behavior(&self) -> Vec<LayerBehavior> {
        vec![LayerBehavior::Pointwise, LayerBehavior::Global]
    }
}

// ---------------------------------------------------------------------------
// Permute: arbitrary permutation of axes (rank is preserved).
// ---------------------------------------------------------------------------

/// Permute: take a permutation vector of length `rank` and reorder
/// the axes accordingly. The permutation is read from a 1-D i32
/// tensor input.
#[derive(Debug, Clone, Copy, Default)]
pub struct PermuteOp;

impl Operator for PermuteOp {
    fn name(&self) -> &'static str {
        "permute"
    }

    fn signature(&self) -> OpSignature {
        OpSignature {
            inputs: vec![
                OpInput {
                    name: "input".to_string(),
                },
                OpInput {
                    name: "permutation".to_string(),
                },
            ],
            outputs: vec![OpOutput {
                name: "out".to_string(),
            }],
        }
    }

    fn infer(&self, inputs: &[ObjectMeta]) -> Result<Vec<ObjectMeta>> {
        tensor_input_check(self.name(), inputs, 2)?;
        let rank = inputs[0].shape.rank();
        let mut output = inputs[0].clone();
        output.shape = Shape::new(
            (0..rank)
                .map(|i| inputs[0].shape.dims[i].clone())
                .collect::<Vec<Dim>>(),
        );
        Ok(vec![output])
    }

    fn required_contracts(&self) -> ContractSet {
        ContractSet::new()
    }
    fn provided_contracts(&self) -> ContractSet {
        shape_arithmetic_contracts(Scope::Operator(self.name().to_string()))
    }
    fn layer_behavior(&self) -> Vec<LayerBehavior> {
        vec![LayerBehavior::Pointwise, LayerBehavior::Global]
    }
}

// ---------------------------------------------------------------------------
// Slice: out = input[start..end] along `axis` (Python half-open).
// ---------------------------------------------------------------------------

/// Slice along a single axis using the Python half-open convention
/// `start..end` (indices `start, start+1, ..., end-1`). The `end`
/// value `usize::MAX` means "to the end of the axis". The `axis`
/// and (start, end) bounds are read from a 3-element i32 tensor
/// input: `[axis, start, end]`.
#[derive(Debug, Clone, Copy, Default)]
pub struct SliceOp;

impl Operator for SliceOp {
    fn name(&self) -> &'static str {
        "slice"
    }

    fn signature(&self) -> OpSignature {
        OpSignature {
            inputs: vec![
                OpInput {
                    name: "input".to_string(),
                },
                OpInput {
                    name: "bounds".to_string(),
                },
            ],
            outputs: vec![OpOutput {
                name: "out".to_string(),
            }],
        }
    }

    fn infer(&self, inputs: &[ObjectMeta]) -> Result<Vec<ObjectMeta>> {
        tensor_input_check(self.name(), inputs, 2)?;
        let mut output = inputs[0].clone();
        // The actual rank/dim slicing happens at lowering time once
        // we have the runtime bounds. We keep the same rank; the
        // lowering shrinks the affected dim.
        let rank = inputs[0].shape.rank();
        output.shape = Shape::new(
            (0..rank)
                .map(|i| inputs[0].shape.dims[i].clone())
                .collect::<Vec<Dim>>(),
        );
        Ok(vec![output])
    }

    fn required_contracts(&self) -> ContractSet {
        ContractSet::new()
    }
    fn provided_contracts(&self) -> ContractSet {
        shape_arithmetic_contracts(Scope::Operator(self.name().to_string()))
    }
    fn layer_behavior(&self) -> Vec<LayerBehavior> {
        vec![LayerBehavior::Pointwise, LayerBehavior::Global]
    }
}

// ---------------------------------------------------------------------------
// Concat: stack N tensors along one axis.
// ---------------------------------------------------------------------------

/// Concat 2 or more tensors along a single axis. All input tensors
/// must have the same rank and the same shape on every axis except
/// the concatenation axis.
#[derive(Debug, Clone, Copy, Default)]
pub struct ConcatOp;

impl Operator for ConcatOp {
    fn name(&self) -> &'static str {
        "concat"
    }

    fn signature(&self) -> OpSignature {
        OpSignature {
            inputs: vec![OpInput {
                name: "first".to_string(),
            }],
            outputs: vec![OpOutput {
                name: "out".to_string(),
            }],
        }
    }

    fn infer(&self, inputs: &[ObjectMeta]) -> Result<Vec<ObjectMeta>> {
        if inputs.is_empty() {
            return Err(Error::operator("concat expects at least 1 input"));
        }
        for (i, m) in inputs.iter().enumerate() {
            if m.object_kind != ObjectKind::Tensor {
                return Err(Error::operator(format!(
                    "concat only supports tensor inputs, input {i} is {:?}",
                    m.object_kind
                )));
            }
        }
        Ok(vec![inputs[0].clone()])
    }

    fn required_contracts(&self) -> ContractSet {
        ContractSet::new()
    }
    fn provided_contracts(&self) -> ContractSet {
        shape_arithmetic_contracts(Scope::Operator(self.name().to_string()))
    }
    fn layer_behavior(&self) -> Vec<LayerBehavior> {
        vec![LayerBehavior::Pointwise, LayerBehavior::Global]
    }
}

// ---------------------------------------------------------------------------
// Broadcast: explicitly broadcast an input to a target shape.
// ---------------------------------------------------------------------------

/// Broadcast: explicitly expand an input tensor to a target shape.
/// This is the explicit (op-form) counterpart of the implicit
/// broadcasting that binary arithmetic ops do. The target shape is
/// read from a second tensor input.
#[derive(Debug, Clone, Copy, Default)]
pub struct BroadcastOp;

impl Operator for BroadcastOp {
    fn name(&self) -> &'static str {
        "broadcast"
    }

    fn signature(&self) -> OpSignature {
        OpSignature {
            inputs: vec![
                OpInput {
                    name: "input".to_string(),
                },
                OpInput {
                    name: "target_shape".to_string(),
                },
            ],
            outputs: vec![OpOutput {
                name: "out".to_string(),
            }],
        }
    }

    fn infer(&self, inputs: &[ObjectMeta]) -> Result<Vec<ObjectMeta>> {
        tensor_input_check(self.name(), inputs, 2)?;
        let target_dims = parse_target_shape(&inputs[1])?;
        let mut output = inputs[0].clone();
        output.shape = Shape::new(target_dims);
        Ok(vec![output])
    }

    fn required_contracts(&self) -> ContractSet {
        ContractSet::new()
    }
    fn provided_contracts(&self) -> ContractSet {
        shape_arithmetic_contracts(Scope::Operator(self.name().to_string()))
    }
    fn layer_behavior(&self) -> Vec<LayerBehavior> {
        vec![LayerBehavior::Pointwise, LayerBehavior::Global]
    }
}

// ---------------------------------------------------------------------------
// Flatten: collapse every dimension into a single 1-D dim.
// ---------------------------------------------------------------------------

/// Flatten: collapse every dimension of an N-D tensor into a single
/// 1-D dimension. A rank-0 (scalar) tensor is mapped to `[1]` (this
/// matches the `Tensor::try_from_vec` convention where rank-0 has
/// product 1).
#[derive(Debug, Clone, Copy, Default)]
pub struct FlattenOp;

impl Operator for FlattenOp {
    fn name(&self) -> &'static str {
        "flatten"
    }

    fn signature(&self) -> OpSignature {
        unary_signature_like()
    }

    fn infer(&self, inputs: &[ObjectMeta]) -> Result<Vec<ObjectMeta>> {
        if inputs.len() != 1 {
            return Err(Error::operator(format!(
                "flatten expects 1 input, got {}",
                inputs.len()
            )));
        }
        let m = &inputs[0];
        if m.object_kind != ObjectKind::Tensor {
            return Err(Error::operator(format!(
                "flatten only supports tensor inputs, got {:?}",
                m.object_kind
            )));
        }
        let mut output = m.clone();
        // The actual flattened dim size is determined at lowering
        // time. We use a DataDependent placeholder when the input
        // rank is 0 (the canonical "scalar -> [1]" rule) or
        // Symbolic/Bounded dims that prevent a static product.
        let product: Option<usize> = if m.shape.dims.is_empty() {
            Some(1)
        } else {
            m.shape
                .dims
                .iter()
                .try_fold(1usize, |acc, d| d.value().map(|v| acc * v))
        };
        let flat_dim = match product {
            Some(n) => Dim::Static(n),
            None => Dim::DataDependent("flatten_product".to_string()),
        };
        output.shape = Shape::new(vec![flat_dim]);
        Ok(vec![output])
    }

    fn required_contracts(&self) -> ContractSet {
        ContractSet::new()
    }
    fn provided_contracts(&self) -> ContractSet {
        shape_arithmetic_contracts(Scope::Operator(self.name().to_string()))
    }
    fn layer_behavior(&self) -> Vec<LayerBehavior> {
        vec![LayerBehavior::Pointwise, LayerBehavior::Global]
    }
}

// ---------------------------------------------------------------------------
// Squeeze: drop all size-1 dims (or a specific subset, declared as a
// runtime mask tensor in input[1]).
// ---------------------------------------------------------------------------

/// Squeeze: drop size-1 dimensions. When no `dims` input is given
/// (only the data tensor), all size-1 dims are dropped. When a
/// `dims` mask tensor is given, only the dims whose index appears
/// in the mask (and which are size-1) are dropped.
#[derive(Debug, Clone, Copy, Default)]
pub struct SqueezeOp;

impl Operator for SqueezeOp {
    fn name(&self) -> &'static str {
        "squeeze"
    }

    fn signature(&self) -> OpSignature {
        OpSignature {
            inputs: vec![OpInput {
                name: "input".to_string(),
            }],
            outputs: vec![OpOutput {
                name: "out".to_string(),
            }],
        }
    }

    fn infer(&self, inputs: &[ObjectMeta]) -> Result<Vec<ObjectMeta>> {
        if inputs.len() != 1 {
            return Err(Error::operator(format!(
                "squeeze expects 1 input, got {}",
                inputs.len()
            )));
        }
        let m = &inputs[0];
        if m.object_kind != ObjectKind::Tensor {
            return Err(Error::operator(format!(
                "squeeze only supports tensor inputs, got {:?}",
                m.object_kind
            )));
        }
        let mut output = m.clone();
        // Eagerly drop all size-1 dims we can see at plan time. The
        // runtime-conditional cases (Symbolic/Bounded dims) keep
        // their dim and the lowering handles them.
        output.shape = Shape::new(
            m.shape
                .dims
                .iter()
                .filter(|d| !matches!(d, Dim::Static(1)))
                .cloned()
                .collect::<Vec<Dim>>(),
        );
        Ok(vec![output])
    }

    fn required_contracts(&self) -> ContractSet {
        ContractSet::new()
    }
    fn provided_contracts(&self) -> ContractSet {
        shape_arithmetic_contracts(Scope::Operator(self.name().to_string()))
    }
    fn layer_behavior(&self) -> Vec<LayerBehavior> {
        vec![LayerBehavior::Pointwise, LayerBehavior::Global]
    }
}

// ---------------------------------------------------------------------------
// Unsqueeze: insert a size-1 dim at the given axis (i32 from a 1-elem
// tensor).
// ---------------------------------------------------------------------------

/// Unsqueeze: insert a single size-1 dimension at the given axis.
/// The axis is read from a 1-element i32 tensor input. Negative axes
/// count from the end.
#[derive(Debug, Clone, Copy, Default)]
pub struct UnsqueezeOp;

impl Operator for UnsqueezeOp {
    fn name(&self) -> &'static str {
        "unsqueeze"
    }

    fn signature(&self) -> OpSignature {
        OpSignature {
            inputs: vec![
                OpInput {
                    name: "input".to_string(),
                },
                OpInput {
                    name: "axis".to_string(),
                },
            ],
            outputs: vec![OpOutput {
                name: "out".to_string(),
            }],
        }
    }

    fn infer(&self, inputs: &[ObjectMeta]) -> Result<Vec<ObjectMeta>> {
        tensor_input_check(self.name(), inputs, 2)?;
        let rank = inputs[0].shape.rank();
        let mut output = inputs[0].clone();
        let mut new_dims = inputs[0].shape.dims.clone();
        new_dims.insert(rank, Dim::Static(1));
        output.shape = Shape::new(new_dims);
        Ok(vec![output])
    }

    fn required_contracts(&self) -> ContractSet {
        ContractSet::new()
    }
    fn provided_contracts(&self) -> ContractSet {
        shape_arithmetic_contracts(Scope::Operator(self.name().to_string()))
    }
    fn layer_behavior(&self) -> Vec<LayerBehavior> {
        vec![LayerBehavior::Pointwise, LayerBehavior::Global]
    }
}

fn unary_signature_like() -> OpSignature {
    OpSignature {
        inputs: vec![OpInput {
            name: "input".to_string(),
        }],
        outputs: vec![OpOutput {
            name: "out".to_string(),
        }],
    }
}