rten 0.24.0

Machine learning runtime
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
use std::borrow::Cow;
use std::fmt;
use std::sync::Arc;

use rten_tensor::prelude::*;
use rten_tensor::{ArcTensor, DynLayout, TensorView};

use super::NodeId;
use crate::constant_storage::ArcTensorView;
use crate::operator::Operator;
use crate::value::{DataType, ValueType, ValueView};

#[derive(Debug)]
pub enum Node {
    Operator(OperatorNode),
    Constant(Constant),
    Value(ValueNode),
}

impl Node {
    /// Return the debug name of this node
    pub fn name(&self) -> Option<&str> {
        match self {
            Node::Operator(node) => node.name(),
            Node::Constant(constant) => constant.name(),
            Node::Value(node) => node.name(),
        }
    }

    /// Return the tensor shape associated with this node.
    ///
    /// For constants this is the shape of the tensor. Operator nodes have no
    /// shape. For values (eg. inputs/outputs) this is the expected shape.
    pub fn shape(&self) -> Option<Cow<'_, [Dimension]>> {
        let dims_from_fixed_shape =
            |shape: &[usize]| shape.iter().copied().map(Dimension::Fixed).collect();

        match self {
            Node::Operator(_) => None,
            Node::Constant(node) => Some(Cow::Owned(dims_from_fixed_shape(node.layout().shape()))),
            Node::Value(node) => node.shape(),
        }
    }

    /// Return the data type associated with this node.
    ///
    /// - For constants this returns the element type of the tensor
    /// - For values this returns the expected element type of the tensor at
    ///   runtime, if known
    /// - For operators this always returns `None`.
    pub fn dtype(&self) -> Option<ValueType> {
        match self {
            Node::Value(node) => node.dtype,
            Node::Constant(constant) => Some(ValueType::Tensor(constant.dtype())),
            Node::Operator(_) => None,
        }
    }

    /// Return the contained operator, if this an operator node.
    pub fn as_operator(&self) -> Option<&OperatorNode> {
        match self {
            Node::Operator(op) => Some(op),
            _ => None,
        }
    }

    /// Return the contained constant, if this a constant node.
    pub fn as_constant(&self) -> Option<&Constant> {
        match self {
            Node::Constant(c) => Some(c),
            _ => None,
        }
    }
}

/// Represents the size of a dimension of a runtime-provided value, such as
/// an operator input, output or intermediate value.
#[derive(Clone, PartialEq)]
pub enum Dimension {
    /// A dimension whose expected size is fixed and specified as part of the
    /// model.
    Fixed(usize),

    /// A dimension whose size is determined at runtime. The symbol provides
    /// a name to identify when different values share a size.
    Symbolic(String),
}

impl From<usize> for Dimension {
    fn from(val: usize) -> Dimension {
        Dimension::Fixed(val)
    }
}

impl From<String> for Dimension {
    fn from(name: String) -> Dimension {
        Dimension::Symbolic(name)
    }
}

impl<'a> From<&'a str> for Dimension {
    fn from(name: &'a str) -> Dimension {
        Dimension::Symbolic(name.into())
    }
}

impl fmt::Debug for Dimension {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Fixed(size) => write!(f, "{}", size),
            Self::Symbolic(name) => write!(f, "\"{}\"", name),
        }
    }
}

#[derive(Debug)]
pub struct OperatorNode {
    name: Option<String>,
    inputs: Box<[Option<NodeId>]>,
    outputs: Box<[Option<NodeId>]>,
    operator: Arc<dyn Operator + Send + Sync>,

    // Cached names of nodes captured by operator's subgraphs.
    capture_names: Box<[String]>,
}

impl OperatorNode {
    pub fn new(
        name: Option<&str>,
        input_ids: &[Option<NodeId>],
        output_ids: &[Option<NodeId>],
        operator: Arc<dyn Operator + Send + Sync>,
    ) -> Self {
        let mut capture_names = Vec::new();
        if let Some(subgraph_op) = operator.as_subgraph_op() {
            for subgraph in subgraph_op.subgraphs() {
                capture_names.extend(subgraph.capture_names().iter().map(|s| s.to_string()));
            }
        }

        OperatorNode {
            name: name.map(|s| s.to_owned()),
            inputs: input_ids.into(),
            outputs: output_ids.into(),
            operator,
            capture_names: capture_names.into(),
        }
    }

    pub fn name(&self) -> Option<&str> {
        self.name.as_deref()
    }

    pub fn input_ids(&self) -> &[Option<NodeId>] {
        &self.inputs
    }

    pub fn output_ids(&self) -> &[Option<NodeId>] {
        &self.outputs
    }

    /// Return the names of nodes captured by this operator's subgraphs.
    ///
    /// Using this method is more efficient than iterating over subgraphs and
    /// collecting the capture names from each.
    pub fn capture_names(&self) -> impl Iterator<Item = &str> {
        self.capture_names.iter().map(|s| s.as_ref())
    }

    pub fn operator(&self) -> &dyn Operator {
        self.operator.as_ref()
    }

    /// Return a new `Arc` reference to this node's operator.
    ///
    /// Since operators are stateless and immutable once added to a graph, they
    /// can be "cloned" just be creating a new reference.
    pub fn clone_operator(&self) -> Arc<dyn Operator + Send + Sync> {
        self.operator.clone()
    }

    /// Replace an input in the operator's list of inputs.
    ///
    /// Consumers outside the graph module should use graph-level methods instead
    /// which update edge caches.
    pub(super) fn replace_input(&mut self, old_id: NodeId, new_id: NodeId) {
        for input_id in self.inputs.iter_mut() {
            if *input_id == Some(old_id) {
                *input_id = Some(new_id);
            }
        }
    }
}

#[derive(Debug)]
pub struct ValueNode {
    name: Option<String>,
    shape: Option<Vec<Dimension>>,
    dtype: Option<ValueType>,
}

impl ValueNode {
    pub fn new(
        name: Option<&str>,
        shape: Option<Vec<Dimension>>,
        dtype: Option<ValueType>,
    ) -> Self {
        ValueNode {
            name: name.map(|s| s.to_owned()),
            shape,
            dtype,
        }
    }

    /// Return the number of dimensions in this value, if it has shape information.
    pub fn ndim(&self) -> Option<usize> {
        self.shape.as_ref().map(|s| s.len())
    }

    pub fn shape(&self) -> Option<Cow<'_, [Dimension]>> {
        self.shape.as_deref().map(Cow::Borrowed)
    }

    pub fn dtype(&self) -> Option<ValueType> {
        self.dtype
    }

    pub fn update_shape(&mut self, shape: Vec<Dimension>) {
        self.shape = Some(shape);
    }

    pub fn update_type(&mut self, dtype: ValueType) {
        self.dtype = Some(dtype);
    }

    fn name(&self) -> Option<&str> {
        self.name.as_deref()
    }
}

#[derive(Debug)]
pub enum Constant {
    Float(ConstantNode<f32>),
    Int32(ConstantNode<i32>),
    Int8(ConstantNode<i8>),
    UInt8(ConstantNode<u8>),
}

impl Constant {
    pub fn new<T>(name: Option<&str>, tensor: impl Into<ConstantNodeData<T>>) -> Self
    where
        Self: From<ConstantNode<T>>,
    {
        ConstantNode::new(name, tensor.into()).into()
    }

    pub fn name(&self) -> Option<&str> {
        match self {
            Constant::Float(f) => f.name.as_deref(),
            Constant::Int32(i) => i.name.as_deref(),
            Constant::Int8(i) => i.name.as_deref(),
            Constant::UInt8(i) => i.name.as_deref(),
        }
    }

    pub fn shape(&self) -> &[usize] {
        self.layout().shape()
    }

    pub fn ndim(&self) -> usize {
        self.layout().ndim()
    }

    /// Clone this constant, but only if it can be done so cheaply by
    /// incrementing a ref count on the underlying data.
    pub fn clone_ref(&self) -> Option<Constant> {
        match self {
            Constant::Float(f) => f.clone_ref().map(Constant::Float),
            Constant::Int32(i) => i.clone_ref().map(Constant::Int32),
            Constant::Int8(i) => i.clone_ref().map(Constant::Int8),
            Constant::UInt8(i) => i.clone_ref().map(Constant::UInt8),
        }
    }

    pub fn layout(&self) -> &DynLayout {
        match self {
            Constant::Float(f) => f.layout(),
            Constant::Int32(i) => i.layout(),
            Constant::Int8(i) => i.layout(),
            Constant::UInt8(i) => i.layout(),
        }
    }

    /// Return the data for this constant as a tensor view.
    pub fn as_view(&self) -> ValueView<'_> {
        match self {
            Constant::Float(f) => ValueView::FloatTensor(f.view()),
            Constant::Int32(i) => ValueView::Int32Tensor(i.view()),
            Constant::Int8(i) => ValueView::Int8Tensor(i.view()),
            Constant::UInt8(i) => ValueView::UInt8Tensor(i.view()),
        }
    }

    fn dtype(&self) -> DataType {
        match self {
            Constant::Float(_) => DataType::Float,
            Constant::Int32(_) => DataType::Int32,
            Constant::Int8(_) => DataType::Int8,
            Constant::UInt8(_) => DataType::UInt8,
        }
    }
}

#[derive(Debug)]
pub struct ConstantNode<T> {
    name: Option<String>,
    data: ConstantNodeData<T>,
}

impl<T> ConstantNode<T> {
    pub fn new(name: Option<&str>, data: ConstantNodeData<T>) -> Self {
        ConstantNode {
            name: name.map(|s| s.to_owned()),
            data,
        }
    }

    pub fn view(&self) -> TensorView<'_, T> {
        match &self.data {
            ConstantNodeData::ArcSlice(data) => data.view(),
            ConstantNodeData::Arc(data) => data.view(),
        }
    }

    fn clone_ref(&self) -> Option<ConstantNode<T>> {
        let data = self.data.clone_ref()?;
        Some(ConstantNode {
            name: self.name.clone(),
            data,
        })
    }

    fn layout(&self) -> &DynLayout {
        match &self.data {
            ConstantNodeData::ArcSlice(data) => data.layout(),
            ConstantNodeData::Arc(data) => data.layout(),
        }
    }
}

macro_rules! impl_constant_node {
    ($scalar_type:ty, $variant:ident) => {
        impl From<ConstantNode<$scalar_type>> for Constant {
            fn from(node: ConstantNode<$scalar_type>) -> Constant {
                Constant::$variant(node)
            }
        }
    };
}

impl_constant_node!(f32, Float);
impl_constant_node!(i32, Int32);
impl_constant_node!(i8, Int8);
impl_constant_node!(u8, UInt8);

/// Data for a constant node (ie. model weights) in a [`Graph`].
///
/// Constant data uses shared storage to enable constants to be shared between
/// parent and sub-graphs.
#[derive(Debug)]
pub enum ConstantNodeData<T> {
    /// Slice of a reference-counted buffer that contains data for multiple
    /// tensors of heterogenous types.
    ///
    /// This is used when a file containing suitably aligned weights is read
    /// into a single buffer or mmap-ed.
    ArcSlice(ArcTensorView<T>),

    /// Reference-counted buffer.
    Arc(ArcTensor<T>),
}

impl<T> ConstantNodeData<T> {
    /// Perform a cheap copy of this constant data.
    ///
    /// This will succeed only for reference-counted types.
    fn clone_ref(&self) -> Option<ConstantNodeData<T>> {
        match self {
            ConstantNodeData::ArcSlice(view) => Some(ConstantNodeData::ArcSlice(view.clone())),
            ConstantNodeData::Arc(view) => Some(ConstantNodeData::Arc(view.clone())),
        }
    }
}

impl<T> From<ArcTensorView<T>> for ConstantNodeData<T> {
    fn from(val: ArcTensorView<T>) -> ConstantNodeData<T> {
        ConstantNodeData::ArcSlice(val)
    }
}

impl<T> From<ArcTensor<T>> for ConstantNodeData<T> {
    fn from(val: ArcTensor<T>) -> ConstantNodeData<T> {
        ConstantNodeData::Arc(val)
    }
}

/// Extract typed data from a [`Constant`].
pub trait TypedConstant<T> {
    fn as_typed_view(&self) -> Option<TensorView<'_, T>>;
    fn as_scalar(&self) -> Option<T>;
    fn as_vector(&self) -> Option<&[T]>;
}

macro_rules! impl_typed_constant {
    ($type:ty, $variant:ident) => {
        impl TypedConstant<$type> for Constant {
            fn as_typed_view(&self) -> Option<TensorView<'_, $type>> {
                match self {
                    Constant::$variant(tensor) => Some(tensor.view()),
                    _ => None,
                }
            }

            fn as_scalar(&self) -> Option<$type> {
                TypedConstant::as_typed_view(self).and_then(|view| view.item().copied())
            }

            fn as_vector(&self) -> Option<&[$type]> {
                TypedConstant::as_typed_view(self).and_then(|view| {
                    match (view.ndim(), view.data()) {
                        (1, Some(vec_data)) => Some(vec_data),
                        _ => None,
                    }
                })
            }
        }
    };
}

impl_typed_constant!(f32, Float);
impl_typed_constant!(i32, Int32);
impl_typed_constant!(i8, Int8);
impl_typed_constant!(u8, UInt8);