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
//! Abstractions for dealing with tuples of tensors.
//!
//! The main source of complexity in this module comes from jumping between the
//! world of types (e.g. an input parameter of `(Tensor<f32>, Tensor<u8>)`) and
//! the world of values (`&[Shape]`) so we can pass type information back to
//! the runtime.
//!
//! I apologise in advance for all the generic gymnastics.

use crate::{element_type::AsElementType, Shape, Tensor};

/// A helper trait which lets us get the shape from a tuple of different
/// tensors.
///
/// Note that this is implemented for references (i.e. `&Tensor<i16>` and
/// `&(Tensor<f32>, Tensor<u8>)`) so we can carry the lifetime around. Having
/// access to the lifetime means we can borrow from the `Shape`'s dimension
/// array instead of cloning them.
pub trait TensorList<'a> {
    // Note: we could get rid of all these XXXBuffer types if const generics
    // were more fleshed out. You'd just need to define an associated
    // `const RANK: usize` then have each of the methods return something like
    // `[_; Self::Rank]`.
    //
    // It would also let us get rid of the lifetime because we can use
    // `fn shape_list(&self) -> [Shape<'_>; Self::Rank]`.

    type ShapeBuffer: AsRef<[Shape<'a>]>;
    type ConstElementPtrBuffer: AsRef<[*const u8]>;

    /// Get an array containing the [`Shape`] for each [`Tensor`] in this tuple.
    fn shape_list(&self) -> Self::ShapeBuffer;

    /// Get an array containing pointers to the elements of each [`Tensor`] in
    /// this tuple.
    fn element_ptr(&self) -> Self::ConstElementPtrBuffer;
}

/// A set of tensors that can be mutated.
pub trait TensorListMut {
    type MutElementPtrBuffer: AsMut<[*mut u8]>;

    /// Create a new set of empty tensors with the specified shape.
    ///
    /// # Panics
    ///
    /// This will panic if the shape doesn't match this [`TensorListMut`]
    /// because there are a different number of tensors or one of the tensors
    /// is the wrong type.
    fn new_tensors(shape: &[Shape<'_>]) -> Self;

    /// Get a set of mutable pointers into each tensor's backing buffer.
    fn element_ptr_mut(&mut self) -> Self::MutElementPtrBuffer;
}

impl<'a, T> TensorList<'a> for &'a Tensor<T>
where
    T: AsElementType,
{
    type ConstElementPtrBuffer = [*const u8; 1];
    type ShapeBuffer = [Shape<'a>; 1];

    fn shape_list(&self) -> Self::ShapeBuffer { [self.shape()] }

    fn element_ptr(&self) -> Self::ConstElementPtrBuffer {
        [self.elements().as_ptr() as *const u8]
    }
}

impl<T> TensorListMut for Tensor<T>
where
    T: AsElementType + Default + Clone,
{
    type MutElementPtrBuffer = [*mut u8; 1];

    fn new_tensors(shape: &[Shape<'_>]) -> Self {
        match shape {
            [s] => {
                assert_eq!(s.element_type(), T::TYPE, "Incorrect element type");
                Tensor::zeroed(s.dimensions().to_vec())
            },
            _ => panic!("Expected a shape with 1 element, found {:?}", shape),
        }
    }

    fn element_ptr_mut(&mut self) -> Self::MutElementPtrBuffer {
        [self.make_elements_mut().as_mut_ptr() as *mut u8]
    }
}

/// Count the number of identifiers that were passed in.
macro_rules! count {
    ($first:ident $(, $rest:ident)* $(,)*) => { 1 + count!($($rest),*) };
    () => { 0 };
}

/// Recursively implement our traits for tuples of any length up to a finite
/// number.
macro_rules! reflection_type_list {
    ($first:ident $(, $dim:ident)* $(,)*) => {
        #[allow(non_snake_case)]
        impl<'a, $first, $($dim),*> TensorList<'a> for &'a (Tensor<$first>, $(Tensor<$dim>),*)
        where
            $first: AsElementType,
            $($dim: AsElementType),*
        {
            type ShapeBuffer = [Shape<'a>; count!($first, $($dim),*)];
            type ConstElementPtrBuffer  = [*const u8; count!($first, $($dim),*)];

            fn shape_list(&self) -> Self::ShapeBuffer {
                let ($first, $($dim),* ) = self;

                [
                    $first.shape(),
                    $( $dim.shape()),*
                ]
            }


            fn element_ptr(&self) -> Self::ConstElementPtrBuffer {
                let ($first, $($dim),* ) = self;

                [
                    $first.elements().as_ptr() as *const u8,
                    $( $dim.elements().as_ptr() as *const u8),*
                ]
            }
        }

        #[allow(non_snake_case)]
        impl<$first, $($dim),*> TensorListMut for (Tensor<$first>, $(Tensor<$dim>),*)
        where
            $first: AsElementType + Default + Clone,
            $( $dim: AsElementType + Default + Clone ),*
        {
            type MutElementPtrBuffer  = [*mut u8; count!($first, $($dim),*)];

            fn new_tensors(shape: &[Shape<'_>]) -> Self {
                match shape {
                    [$first, $($dim),*] => {
                        assert_eq!($first.element_type(), <$first>::TYPE, "Incorrect element type");
                        let $first = Tensor::zeroed($first.dimensions().to_vec());
                        $(
                            assert_eq!($dim.element_type(), <$dim>::TYPE, "Incorrect element type");
                            let $dim = Tensor::zeroed($dim.dimensions().to_vec());
                        )*

                        ($first, $($dim),*)
                    },
                    _ => panic!(
                        "Expected a shape with {} elements, found {:?}",
                        count!($first, $($dim),*),
                        shape,
                    ),
                }
            }

            fn element_ptr_mut(&mut self) -> Self::MutElementPtrBuffer {
                let ($first, $($dim),* ) = self;

                [
                    $first.make_elements_mut().as_mut_ptr() as *mut u8,
                    $( $dim.make_elements_mut().as_mut_ptr() as *mut u8),*
                ]
            }
        }

        reflection_type_list!($( $dim ),*);
    };
    ($(,)*) => {};
}

reflection_type_list!(
    A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y,
    Z, AA, AB, AC, AD, AF, AG, AH
);

#[cfg(test)]
mod tests {
    use std::prelude::v1::*;

    use super::*;
    use crate::element_type::ElementType;

    #[test]
    fn count_idents() {
        let inputs = vec![
            (count!(), 0),
            (count!(A), 1),
            (count!(A, B), 2),
            (count!(A, B, C), 3),
        ];

        for (count, should_be) in inputs {
            assert_eq!(count, should_be);
        }
    }

    #[test]
    fn create_empty_tensors_from_shapes_list() {
        let shapes = [
            Shape::new(ElementType::F32, [1_usize].as_ref()),
            Shape::new(ElementType::U8, [3_usize, 256, 256].as_ref()),
            Shape::new(ElementType::I16, [1920_usize].as_ref()),
        ];

        let (a, b, c) =
            <(Tensor<f32>, Tensor<u8>, Tensor<i16>)>::new_tensors(&shapes);

        let got = [a.shape(), b.shape(), c.shape()];
        assert_eq!(got, shapes);
    }

    #[test]
    #[should_panic = "Expected a shape with 3 elements, found [Shape { \
                      element_type: F32, dimensions: [1] }]"]
    fn incorrect_shape_list_length() {
        let shapes = [Shape::new(ElementType::F32, [1_usize].as_ref())];

        let _ = <(Tensor<f32>, Tensor<u8>, Tensor<i16>)>::new_tensors(&shapes);
    }

    #[test]
    #[should_panic = "Incorrect element type"]
    fn incorrect_type_in_shape_list() {
        let shapes = [Shape::new(ElementType::F32, [1_usize].as_ref())];

        let _ = <Tensor<i16>>::new_tensors(&shapes);
    }
}