1use crate::tensor::backend::Backend;
2use crate::tensor::graph::NodeKind;
3use crate::tensor::mem_formats::layout::Layout;
4use crate::tensor::storage::TensorData;
5
6pub trait Dimension {
31 fn layout(&self) -> &Layout;
32
33 fn shape(&self) -> &'_ [usize] {
34 self.layout().shape()
35 }
36
37 fn stride(&self) -> &'_ [i32] {
38 self.layout().stride()
39 }
40
41 fn adj_stride(&self) -> &'_ [i32] {
42 self.layout().adj_stride()
43 }
44
45 fn len(&self) -> usize {
46 self.layout().len()
47 }
48
49 fn is_empty(&self) -> bool {
50 self.layout().is_empty()
51 }
52
53 fn offset(&self) -> usize {
54 self.layout().offset()
55 }
56
57 fn is_contiguous(&self) -> bool {
58 self.layout().is_contiguous()
59 }
60
61 fn is_contiguous_at_axis(&self, axis: usize) -> bool {
62 self.layout().is_contiguous_at_axis(axis)
63 }
64
65 fn is_transposed(&self) -> bool {
66 self.layout().is_transposed()
67 }
68
69 fn is_transposed_at_axis(&self, axis: usize) -> bool {
70 self.layout().is_transposed_at_axis(axis)
71 }
72}
73
74pub trait Promising {
75 type Output;
76
77 fn compute(&self) -> TensorData<Self::Output>;
78}
79
80pub(crate) trait Operand<T, B: Backend>: Dimension {
85 fn to_node(&self) -> NodeKind<T, B>;
86}
87
88pub trait Composable<T, B: Backend>: Operand<T, B> {}
113
114pub trait StreamingIterator {
115 type Item<'a>
116 where
117 Self: 'a;
118
119 fn next_stream<'a>(&'a mut self) -> Option<Self::Item<'a>>;
120
121 #[allow(unused)]
122 fn zip<Other>(self, other: Other) -> StreamingZip<Self, Other>
123 where
124 Self: Sized,
125 Other: StreamingIterator,
126 {
127 StreamingZip {
128 left: self,
129 right: other,
130 }
131 }
132}
133
134#[allow(unused)]
135pub struct StreamingZip<A: StreamingIterator, B: StreamingIterator> {
136 left: A,
137 right: B,
138}
139
140impl<A, B> StreamingIterator for StreamingZip<A, B>
141where
142 A: StreamingIterator,
143 B: StreamingIterator,
144{
145 type Item<'a>
146 = (A::Item<'a>, B::Item<'a>)
147 where
148 Self: 'a;
149
150 fn next_stream<'a>(&'a mut self) -> Option<Self::Item<'a>> {
151 let l = self.left.next_stream()?;
152 let r = self.right.next_stream()?;
153 Some((l, r))
154 }
155}
156
157pub(crate) trait Numeric: crate::tensor::definitions::NumberLike {
158 const MUL_NEUTRAL: Self;
159 const SUM_NEUTRAL: Self;
160 const ONE: Self;
161 const ZERO: Self;
162 const MIN: Self;
163}
164
165impl Numeric for f64 {
166 const MUL_NEUTRAL: Self = 1.0;
167 const SUM_NEUTRAL: Self = 0.0;
168 const ONE: Self = 1.0;
169 const ZERO: Self = 0.0;
170 const MIN: Self = f64::NEG_INFINITY;
171}
172
173impl Numeric for f32 {
174 const MUL_NEUTRAL: Self = 1.0;
175 const SUM_NEUTRAL: Self = 0.0;
176 const ONE: Self = 1.0;
177 const ZERO: Self = 0.0;
178 const MIN: Self = f32::NEG_INFINITY;
179}
180
181pub(crate) trait FromIndex {
182 fn from_index(i: usize) -> Self;
183}
184
185impl FromIndex for f64 {
186 #[inline]
187 fn from_index(i: usize) -> Self {
188 i as f64
189 }
190}
191
192impl FromIndex for f32 {
193 #[inline]
194 fn from_index(i: usize) -> Self {
195 i as f32
196 }
197}