ella_tensor/
tensor.rs

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
mod data;
pub mod fmt;
mod iter;

pub use data::TensorData;
pub(crate) use iter::ShapedIter;
pub use iter::TensorIter;

use crate::{Column, Const, Dyn, Mask, Shape, TensorValue};
use arrow::array::{make_array, Array, ArrayData, ArrayRef, FixedSizeListArray};
use ella_common::array::flatten;
use std::sync::Arc;

pub type Tensor1<T> = Tensor<T, Const<1>>;
pub type Tensor2<T> = Tensor<T, Const<2>>;
pub type Tensor3<T> = Tensor<T, Const<3>>;
pub type Tensor4<T> = Tensor<T, Const<4>>;
pub type TensorD<T> = Tensor<T, Dyn>;

#[derive(Clone)]
pub struct Tensor<T: TensorValue, S> {
    shape: S,
    strides: S,
    values: TensorData<T, T::Array>,
}

impl<T, S> Tensor<T, S>
where
    T: TensorValue,
    S: Shape,
{
    #[inline]
    pub fn shape(&self) -> &S {
        &self.shape
    }

    #[inline]
    pub fn strides(&self) -> &S {
        &self.strides
    }

    #[inline]
    pub fn size(&self) -> usize {
        self.shape.size()
    }

    #[inline]
    pub fn ndim(&self) -> usize {
        self.shape.ndim()
    }

    pub fn is_standard_layout(&self) -> bool {
        self.shape.is_standard_layout(&self.strides)
    }

    #[doc(hidden)]
    #[inline]
    pub fn values(&self) -> &TensorData<T, T::Array> {
        &self.values
    }

    #[doc(hidden)]
    #[inline]
    pub fn into_values(self) -> TensorData<T, T::Array> {
        self.values
    }

    pub(crate) fn new<A>(values: A, shape: S, strides: S) -> Self
    where
        A: Into<TensorData<T, T::Array>>,
    {
        Self {
            values: values.into(),
            shape,
            strides,
        }
    }

    pub fn try_from_arrow<R>(array: ArrayRef, row_shape: R) -> crate::Result<Self>
    where
        R: Shape<Larger = S>,
    {
        let mut shape = row_shape.insert_axis(crate::Axis(0));
        shape[0] = array.len();
        let array = flatten(array)?;
        if shape.size() != array.len() {
            return Err(crate::ShapeError::ArraySize(array.len(), shape.slice().to_vec()).into());
        }
        if array.data_type() != &T::TENSOR_TYPE.to_arrow() {
            return Err(crate::Error::DataType(array.data_type().clone()));
        }
        let strides = shape.default_strides();
        Ok(Self::new(
            T::from_array_data(array.to_data()),
            shape,
            strides,
        ))
    }

    pub fn into_arrow(self) -> ArrayRef {
        let this = self.to_standard_layout();
        let dtype = this.arrow_type();
        if this.shape().ndim() > 1 {
            let data = unsafe {
                ArrayData::builder(dtype)
                    .add_child_data(this.values.into_values().to_data())
                    .len(this.shape[0])
                    .build_unchecked()
            };
            Arc::new(FixedSizeListArray::from(data))
        } else {
            let data = unsafe {
                this.values
                    .into_values()
                    .to_data()
                    .into_builder()
                    .data_type(dtype)
                    .build_unchecked()
            };
            make_array(data)
        }
    }

    pub fn to_standard_layout(mut self) -> Self {
        if self.is_standard_layout() {
            self
        } else {
            self.values = unsafe { T::from_trusted_len_iter(self.iter()).into() };
            self
        }
    }

    pub(crate) fn mask_inner(&self) -> Mask<S> {
        Mask::new(
            self.values.mask(),
            self.shape().clone(),
            self.strides().clone(),
        )
    }

    pub fn map<F, O>(&self, f: F) -> Tensor<O, S>
    where
        O: TensorValue,
        F: Fn(T) -> O,
    {
        unsafe { Tensor::from_trusted_len_iter(self.iter().map(f), self.shape().clone()) }
    }
}

#[macro_export]
macro_rules! tensor {
    ($([$([$($x:expr),* $(,)*]),+ $(,)*]),+ $(,)*) => {{
        $crate::Tensor3::from(vec![$([$([$($x,)*],)*],)*])
    }};
    ($([$($x:expr),* $(,)*]),+ $(,)*) => {{
        $crate::Tensor2::from(vec![$([$($x,)*],)*])
    }};
    ($($x:expr),* $(,)*) => {{
        $crate::Tensor::from(vec![$($x,)*])
    }};
}