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
use ndarray::prelude::*;
use num_traits::AsPrimitive;
use crate::ops::prelude::*;
#[derive(Debug, Clone, new)]
pub struct Size {
dt: DatumType,
}
impl Size {
pub fn coerce_to<T>(size: usize) -> TractResult<SharedTensor>
where
T: Datum,
usize: AsPrimitive<T>,
{
Ok(Tensor::from(arr0(size.as_())).into())
}
}
impl Op for Size {
fn name(&self) -> Cow<str> {
"Size".into()
}
}
impl StatelessOp for Size {
fn eval(&self, inputs: TVec<SharedTensor>) -> TractResult<TVec<SharedTensor>> {
let size = inputs[0].shape().iter().product();
Ok(tvec![dispatch_numbers!(Self::coerce_to(self.dt)(size))?])
}
}
impl InferenceRulesOp for Size {
fn rules<'r, 'p: 'r, 's: 'r>(
&'s self,
s: &mut Solver<'r>,
inputs: &'p SharedTensorsProxy,
outputs: &'p SharedTensorsProxy,
) -> InferenceResult {
s.equals(&inputs.len, 1)?;
s.equals(&outputs.len, 1)?;
s.equals(&outputs[0].rank, 0)?;
Ok(())
}
}