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
use crate::err::{Error, Result};
use crate::exec::{ExecutionSite, LayoutClass, OperationIdentity, ShapeExpr};
use crate::shapes::error::OperationKind;
use crate::tensor::device::DeviceId;
use crate::tensor::dtype::DTypeDescriptor;
use alloc::boxed::Box;
use alloc::collections::{BTreeMap, BTreeSet};
use alloc::string::String;
use alloc::vec::Vec;

/// Identifies a tensor value in a graph.
pub type ValueId = usize;
/// Identifies an operation node in a graph.
pub type NodeId = usize;

/// Metadata for one graph value. Concrete shape vectors are retained for
/// eager tracing; compiler-facing symbolic metadata is attached by capture.
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct Value {
    /// Value identifier within the graph.
    pub id: ValueId,
    /// Concrete extents when statically known.
    pub shape: Vec<usize>,
    /// Symbolic shape supporting dynamic dims.
    pub shape_expr: ShapeExpr,
    /// Element dtype of the value.
    pub dtype: DTypeDescriptor,
    /// Logical placement when known at capture time.
    #[serde(default)]
    pub device: Option<DeviceId>,
    /// Layout fact when the capture backend can establish one.
    #[serde(default)]
    pub layout: Option<LayoutClass>,
    /// Human-readable name, when supplied.
    pub name: Option<String>,
}

/// A canonical operation node. Built-in identity comes directly from the
/// operation catalog. Custom operations use their namespaced OperationKey.
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct Node {
    /// Node identifier.
    pub id: NodeId,
    /// Operation identity executing here.
    pub operation: OperationIdentity,
    /// Where the node executes, when resolved.
    pub execution_site: Option<ExecutionSite>,
    /// Identifiers of consumed values.
    pub inputs: Vec<ValueId>,
    /// Identifiers of produced values.
    pub outputs: Vec<ValueId>,
    /// Named operation attributes.
    pub attributes: BTreeMap<String, AttributeValue>,
    /// Versioned bytes of the canonical typed descriptor, when capture runs
    /// with the standard serialization support enabled.
    pub descriptor_payload: Option<DescriptorPayload>,
}

#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
/// Encoded canonical descriptor carried by a captured node.
pub struct DescriptorPayload {
    /// Schema version of the payload.
    pub schema: u32,
    /// Encoded bytes.
    pub payload: Vec<u8>,
}

/// A graph attribute value. The canonical typed descriptor remains the source
/// of semantic validation; this is its stable graph serialization form.
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub enum AttributeValue {
    /// 64-bit signed integer attribute.
    Int(i64),
    /// Single-precision float attribute.
    Float(f32),
    /// UTF-8 string attribute.
    String(String),
    /// List of signed integers.
    Ints(Vec<i64>),
    /// List of single-precision floats.
    Floats(Vec<f32>),
}

#[derive(Debug, Default, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
/// Directed acyclic computation graph.
pub struct Graph {
    #[serde(with = "string_key_map")]
    /// All values by id.
    pub values: BTreeMap<ValueId, Value>,
    /// Topologically ordered nodes.
    pub nodes: Vec<Node>,
    /// Identifiers of consumed values.
    pub inputs: Vec<ValueId>,
    /// Identifiers of produced values.
    pub outputs: Vec<ValueId>,
    #[serde(with = "string_key_map")]
    /// Initializer payloads keyed by value id.
    pub initializers: BTreeMap<ValueId, Vec<u8>>,
    next_value_id: usize,
    next_node_id: usize,
    #[serde(default)]
    named_symbols: BTreeMap<String, crate::exec::SymbolId>,
    #[serde(default)]
    next_symbol_id: u32,
}

mod string_key_map {
    use alloc::collections::BTreeMap;
    use alloc::string::{String, ToString};
    use serde::{Deserialize, Deserializer, Serialize, Serializer};

    pub fn serialize<K, V, S>(map: &BTreeMap<K, V>, serializer: S) -> Result<S::Ok, S::Error>
    where
        K: ToString,
        V: Serialize,
        S: Serializer,
    {
        let string_map: BTreeMap<String, &V> = map
            .iter()
            .map(|(key, value)| (key.to_string(), value))
            .collect();
        string_map.serialize(serializer)
    }

    pub fn deserialize<'de, K, V, D>(deserializer: D) -> Result<BTreeMap<K, V>, D::Error>
    where
        K: core::str::FromStr + core::hash::Hash + Eq + Ord,
        K::Err: core::fmt::Display,
        V: Deserialize<'de>,
        D: Deserializer<'de>,
    {
        let string_map: BTreeMap<String, V> = BTreeMap::deserialize(deserializer)?;
        string_map
            .into_iter()
            .map(|(key, value)| {
                key.parse::<K>()
                    .map_err(serde::de::Error::custom)
                    .map(|key| (key, value))
            })
            .collect()
    }
}

impl Graph {
    /// Creates an empty graph.
    pub fn new() -> Self {
        Self::default()
    }

    /// Adds a value with shape/dtype validation.
    pub fn add_value<D: Into<DTypeDescriptor>>(
        &mut self,
        shape: Vec<usize>,
        dtype: D,
        name: Option<String>,
    ) -> ValueId {
        let id = self.next_value_id;
        self.next_value_id += 1;
        let shape_expr = ShapeExpr::concrete(&shape);
        self.values.insert(
            id,
            Value {
                id,
                shape,
                shape_expr,
                dtype: dtype.into(),
                device: None,
                layout: None,
                name,
            },
        );
        id
    }

    /// Records physical metadata that was available for a captured value.
    pub fn set_value_placement(
        &mut self,
        value_id: ValueId,
        device: Option<DeviceId>,
        layout: Option<LayoutClass>,
    ) -> Result<()> {
        let value = self.values.get_mut(&value_id).ok_or_else(|| {
            Error::Msg(alloc::format!(
                "cannot set metadata for unknown graph value {value_id}"
            ))
        })?;
        value.device = device;
        value.layout = layout;
        Ok(())
    }

    /// Adds a node validating references exist.
    pub fn add_node(
        &mut self,
        operation: OperationKind,
        inputs: Vec<ValueId>,
        outputs: Vec<ValueId>,
        attributes: BTreeMap<String, AttributeValue>,
    ) -> NodeId {
        self.add_node_with_identity(
            OperationIdentity::Builtin(operation),
            inputs,
            outputs,
            attributes,
        )
    }

    /// Adds a node with an explicit identity override.
    pub fn add_node_with_identity(
        &mut self,
        operation: OperationIdentity,
        inputs: Vec<ValueId>,
        outputs: Vec<ValueId>,
        attributes: BTreeMap<String, AttributeValue>,
    ) -> NodeId {
        let id = self.next_node_id;
        self.next_node_id += 1;
        let execution_site = operation.execution_site();
        self.nodes.push(Node {
            id,
            operation,
            execution_site,
            inputs,
            outputs,
            attributes,
            descriptor_payload: None,
        });
        id
    }

    /// Adds a node carrying an encoded descriptor payload.
    pub fn add_node_with_descriptor_payload(
        &mut self,
        operation: OperationIdentity,
        inputs: Vec<ValueId>,
        outputs: Vec<ValueId>,
        attributes: BTreeMap<String, AttributeValue>,
        descriptor_payload: Option<DescriptorPayload>,
    ) -> NodeId {
        let id = self.next_node_id;
        self.next_node_id += 1;
        let execution_site = operation.execution_site();
        self.nodes.push(Node {
            id,
            operation,
            execution_site,
            inputs,
            outputs,
            attributes,
            descriptor_payload,
        });
        id
    }

    /// Marks a value as a graph input.
    pub fn mark_input(&mut self, value_id: ValueId) {
        if !self.inputs.contains(&value_id) {
            self.inputs.push(value_id);
        }
        if let Some(shape) = self.values.get(&value_id).map(|value| value.shape.clone()) {
            let shape = ShapeExpr::symbolic(&shape, 1);
            let remapped = self.remap_input_symbols(shape);
            if let Some(value) = self.values.get_mut(&value_id) {
                value.shape_expr = remapped;
            }
        }
    }

    /// Marks an input while retaining the typed frontend shape proof that
    /// produced it. Runtime axes are assigned graph-local symbols; static
    /// axes remain constants and therefore do not receive redundant guards.
    pub fn mark_input_with_shape<S>(&mut self, value_id: ValueId)
    where
        S: crate::shapes::Shape + crate::exec::shape_projection::ShapeProjection,
    {
        if !self.inputs.contains(&value_id) {
            self.inputs.push(value_id);
        }
        let shape_expr =
            self.remap_input_symbols(crate::exec::shape_projection::shape_expr::<S>(1));
        if let Some(value) = self.values.get_mut(&value_id) {
            value.shape_expr = shape_expr;
        }
    }

    /// Marks a value as a graph output.
    pub fn mark_output(&mut self, value_id: ValueId) {
        if !self.outputs.contains(&value_id) {
            self.outputs.push(value_id);
        }
    }

    fn remap_input_symbols(&mut self, expr: ShapeExpr) -> ShapeExpr {
        let mut anonymous = BTreeMap::new();
        let mut used = BTreeSet::new();
        for value in self.values.values() {
            collect_shape_symbols(&value.shape_expr, &mut used);
        }
        used.extend(self.named_symbols.values().copied());
        let mut next = self.next_symbol_id;
        let mut named_next = next;
        let remapped = remap_shape_symbols(
            expr,
            &mut anonymous,
            &mut self.named_symbols,
            &mut named_next,
            || loop {
                let candidate = crate::exec::SymbolId(next);
                next = next.saturating_add(1);
                if used.insert(candidate) {
                    break candidate;
                }
            },
        );
        self.next_symbol_id = next;
        remapped
    }
}

fn remap_shape_symbols<F: FnMut() -> crate::exec::SymbolId>(
    expr: ShapeExpr,
    anonymous: &mut BTreeMap<crate::exec::SymbolId, crate::exec::SymbolId>,
    names: &mut BTreeMap<String, crate::exec::SymbolId>,
    next_id: &mut u32,
    mut fresh: F,
) -> ShapeExpr {
    fn dim(
        expr: crate::exec::DimExpr,
        anonymous: &mut BTreeMap<crate::exec::SymbolId, crate::exec::SymbolId>,
        names: &mut BTreeMap<String, crate::exec::SymbolId>,
        next_id: &mut u32,
        fresh: &mut impl FnMut() -> crate::exec::SymbolId,
    ) -> crate::exec::DimExpr {
        use crate::exec::DimExpr;
        match expr {
            DimExpr::Fresh(source) => {
                let source = crate::exec::SymbolId(source);
                let mapped = if let Some(mapped) = anonymous.get(&source) {
                    *mapped
                } else {
                    let mapped = fresh();
                    anonymous.insert(source, mapped);
                    mapped
                };
                DimExpr::Symbol(mapped)
            }
            DimExpr::Symbol(id) => {
                let mapped = if let Some(mapped) = anonymous.get(&id) {
                    *mapped
                } else {
                    let mapped = fresh();
                    anonymous.insert(id, mapped);
                    mapped
                };
                DimExpr::Symbol(mapped)
            }
            DimExpr::NamedSymbol { name, identity, .. } => {
                let id = if let Some(id) = names.get(&identity) {
                    *id
                } else {
                    let id = fresh();
                    *next_id = (*next_id).max(id.0.saturating_add(1));
                    names.insert(identity.clone(), id);
                    id
                };
                DimExpr::NamedSymbol { id, name, identity }
            }
            DimExpr::NamedFresh {
                source,
                name,
                identity,
            } => {
                let id = if let Some(id) = names.get(&identity) {
                    *id
                } else {
                    let id = fresh();
                    *next_id = (*next_id).max(id.0.saturating_add(1));
                    names.insert(identity.clone(), id);
                    id
                };
                let _ = source;
                DimExpr::NamedSymbol { id, name, identity }
            }
            DimExpr::NamedExpr {
                expr,
                id: _,
                name,
                identity,
            } => {
                let id = if let Some(id) = names.get(&identity) {
                    *id
                } else {
                    let id = fresh();
                    *next_id = (*next_id).max(id.0.saturating_add(1));
                    names.insert(identity.clone(), id);
                    id
                };
                let expr = dim(*expr, anonymous, names, next_id, fresh);
                match expr {
                    DimExpr::Symbol(_) | DimExpr::NamedSymbol { .. } => {
                        DimExpr::NamedSymbol { id, name, identity }
                    }
                    DimExpr::Const(value) => DimExpr::NamedExpr {
                        expr: Box::new(DimExpr::Const(value)),
                        id,
                        name,
                        identity,
                    },
                    expr => DimExpr::NamedExpr {
                        expr: Box::new(expr),
                        id,
                        name,
                        identity,
                    },
                }
            }
            DimExpr::Add(lhs, rhs) => DimExpr::Add(
                Box::new(dim(*lhs, anonymous, names, next_id, fresh)),
                Box::new(dim(*rhs, anonymous, names, next_id, fresh)),
            ),
            DimExpr::Sub(lhs, rhs) => DimExpr::Sub(
                Box::new(dim(*lhs, anonymous, names, next_id, fresh)),
                Box::new(dim(*rhs, anonymous, names, next_id, fresh)),
            ),
            DimExpr::Mul(lhs, rhs) => DimExpr::Mul(
                Box::new(dim(*lhs, anonymous, names, next_id, fresh)),
                Box::new(dim(*rhs, anonymous, names, next_id, fresh)),
            ),
            DimExpr::ExactDiv(lhs, rhs) => DimExpr::ExactDiv(
                Box::new(dim(*lhs, anonymous, names, next_id, fresh)),
                Box::new(dim(*rhs, anonymous, names, next_id, fresh)),
            ),
            DimExpr::Broadcast(lhs, rhs) => DimExpr::Broadcast(
                Box::new(dim(*lhs, anonymous, names, next_id, fresh)),
                Box::new(dim(*rhs, anonymous, names, next_id, fresh)),
            ),
            DimExpr::Min(lhs, rhs) => DimExpr::Min(
                Box::new(dim(*lhs, anonymous, names, next_id, fresh)),
                Box::new(dim(*rhs, anonymous, names, next_id, fresh)),
            ),
            DimExpr::Max(lhs, rhs) => DimExpr::Max(
                Box::new(dim(*lhs, anonymous, names, next_id, fresh)),
                Box::new(dim(*rhs, anonymous, names, next_id, fresh)),
            ),
            other => other,
        }
    }

    ShapeExpr {
        rank: expr.rank,
        dims: expr
            .dims
            .into_iter()
            .map(|value| dim(value, anonymous, names, next_id, &mut fresh))
            .collect(),
        constraints: expr
            .constraints
            .into_iter()
            .map(|constraint| match constraint {
                crate::exec::Constraint::Equal { lhs, rhs } => crate::exec::Constraint::Equal {
                    lhs: dim(lhs, anonymous, names, next_id, &mut fresh),
                    rhs: dim(rhs, anonymous, names, next_id, &mut fresh),
                },
                crate::exec::Constraint::BroadcastCompatible { lhs, rhs } => {
                    crate::exec::Constraint::BroadcastCompatible {
                        lhs: dim(lhs, anonymous, names, next_id, &mut fresh),
                        rhs: dim(rhs, anonymous, names, next_id, &mut fresh),
                    }
                }
                crate::exec::Constraint::LowerBound { value, bound } => {
                    crate::exec::Constraint::LowerBound {
                        value: dim(value, anonymous, names, next_id, &mut fresh),
                        bound,
                    }
                }
                crate::exec::Constraint::UpperBound { value, bound } => {
                    crate::exec::Constraint::UpperBound {
                        value: dim(value, anonymous, names, next_id, &mut fresh),
                        bound,
                    }
                }
                crate::exec::Constraint::Divisible { value, divisor } => {
                    crate::exec::Constraint::Divisible {
                        value: dim(value, anonymous, names, next_id, &mut fresh),
                        divisor,
                    }
                }
            })
            .collect(),
    }
}

fn collect_shape_symbols(expr: &ShapeExpr, symbols: &mut BTreeSet<crate::exec::SymbolId>) {
    fn dim(expr: &crate::exec::DimExpr, symbols: &mut BTreeSet<crate::exec::SymbolId>) {
        match expr {
            crate::exec::DimExpr::Symbol(id) | crate::exec::DimExpr::NamedSymbol { id, .. } => {
                symbols.insert(*id);
            }
            crate::exec::DimExpr::Add(lhs, rhs)
            | crate::exec::DimExpr::Sub(lhs, rhs)
            | crate::exec::DimExpr::Mul(lhs, rhs)
            | crate::exec::DimExpr::ExactDiv(lhs, rhs)
            | crate::exec::DimExpr::Broadcast(lhs, rhs)
            | crate::exec::DimExpr::Min(lhs, rhs)
            | crate::exec::DimExpr::Max(lhs, rhs) => {
                dim(lhs, symbols);
                dim(rhs, symbols);
            }
            crate::exec::DimExpr::NamedExpr { expr, .. } => dim(expr, symbols),
            crate::exec::DimExpr::Const(_)
            | crate::exec::DimExpr::Fresh(_)
            | crate::exec::DimExpr::NamedFresh { .. }
            | crate::exec::DimExpr::Unknown => {}
        }
    }
    for value in &expr.dims {
        dim(value, symbols);
    }
    for constraint in &expr.constraints {
        match constraint {
            crate::exec::Constraint::Equal { lhs, rhs }
            | crate::exec::Constraint::BroadcastCompatible { lhs, rhs } => {
                dim(lhs, symbols);
                dim(rhs, symbols);
            }
            crate::exec::Constraint::LowerBound { value, .. }
            | crate::exec::Constraint::UpperBound { value, .. }
            | crate::exec::Constraint::Divisible { value, .. } => dim(value, symbols),
        }
    }
}