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
use crate::internal::*; use std::fmt; #[derive(Copy, Clone, Debug, PartialEq)] pub enum DataFormat { NCHW, NHWC, CHW, HWC, } impl Default for DataFormat { fn default() -> DataFormat { DataFormat::NCHW } } impl DataFormat { pub fn dispose_n_axis(&self) -> DataFormat { match self { &DataFormat::NCHW => DataFormat::CHW, &DataFormat::NHWC => DataFormat::HWC, _ => panic!("Attempt at removing N axis on {:?}", self), } } pub fn shape<D, S>(&self, shape: S) -> TractResult<BaseDataShape<D, S>> where D: DimLike, S: AsRef<[D]> + fmt::Debug, { if shape.as_ref().iter().filter(|d| d.to_integer().is_err()).count() > 1 { panic!("Can not work out a data format with two actual symbolic dim") } let mut strides: Vec<D> = vec![D::one()]; for dim in shape.as_ref().iter().skip(1).rev() { let previous = strides.last().unwrap().clone(); strides.push(previous.maybe_mul(dim)?) } strides.reverse(); Ok(BaseDataShape { fmt: *self, shape, strides }) } pub fn from_n_c_hw<D, S>(&self, n: D, c: D, shape: S) -> TractResult<BaseDataShape<D, TVec<D>>> where D: DimLike, S: AsRef<[D]> + fmt::Debug, { let mut me = tvec!(); if *self == DataFormat::NCHW || *self == DataFormat::NHWC { me.push(n); } if *self == DataFormat::NCHW || *self == DataFormat::CHW { me.push(c.clone()); } me.extend(shape.as_ref().iter().cloned()); if *self == DataFormat::NHWC || *self == DataFormat::HWC { me.push(c.clone()); } self.shape(me) } } pub type DataShape = BaseDataShape<usize, TVec<usize>>; #[derive(Clone, Debug, PartialEq)] pub struct BaseDataShape<D, S> where D: DimLike, S: AsRef<[D]> + fmt::Debug, { pub fmt: DataFormat, pub shape: S, pub strides: Vec<D>, } impl<D, S> BaseDataShape<D, S> where D: DimLike, S: AsRef<[D]> + fmt::Debug, { #[inline] pub fn rank(&self) -> usize { self.shape.as_ref().len() } #[inline] pub fn hw_rank(&self) -> usize { self.rank() - 1 - self.n_axis().is_some() as usize } #[inline] pub fn n_axis(&self) -> Option<usize> { match self.fmt { DataFormat::NHWC | DataFormat::NCHW => Some(0), DataFormat::HWC | DataFormat::CHW => None, } } #[inline] pub fn c_axis(&self) -> usize { match self.fmt { DataFormat::NHWC => self.shape.as_ref().len() - 1, DataFormat::HWC => self.shape.as_ref().len() - 1, DataFormat::NCHW => 1, DataFormat::CHW => 0, } } #[inline] pub fn h_axis(&self) -> usize { match self.fmt { DataFormat::NHWC => 1, DataFormat::HWC => 0, DataFormat::NCHW => 2, DataFormat::CHW => 1, } } #[inline] pub fn hw_axes(&self) -> ::std::ops::Range<usize> { self.h_axis()..self.h_axis() + self.hw_rank() } #[inline] pub fn n_dim(&self) -> Option<&D> { self.n() } #[inline] pub fn c_dim(&self) -> &D { self.c() } #[inline] pub fn hw_dims(&self) -> &[D] { unsafe { self.shape.as_ref().get_unchecked(self.hw_axes()) } } #[inline] pub fn n(&self) -> Option<&D> { unsafe { self.n_axis().map(|axis| self.shape.as_ref().get_unchecked(axis)) } } #[inline] pub fn c(&self) -> &D { unsafe { self.shape.as_ref().get_unchecked(self.c_axis()) } } #[inline] pub fn n_stride(&self) -> Option<&D> { unsafe { self.n_axis().map(|axis| self.strides.get_unchecked(axis)) } } #[inline] pub fn h_stride(&self) -> &D { unsafe { &self.hw_strides().get_unchecked(0) } } #[inline] pub fn hw_strides(&self) -> &[D] { unsafe { self.strides.get_unchecked(self.hw_axes()) } } #[inline] pub fn w_stride(&self) -> &D { unsafe { self.hw_strides().get_unchecked(self.hw_rank() - 1) } } #[inline] pub fn c_stride(&self) -> &D { unsafe { self.strides.get_unchecked(self.c_axis()) } } }