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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
use tenferro_tensor::{GatherConfig, Tensor, TensorDeviceTransfer, TypedTensor};
use crate::eager::EagerTensor;
use crate::error::{Error, Result};
fn normalize_existing_axis(op: &'static str, axis: isize, rank: usize) -> Result<usize> {
let normalized = if axis >= 0 {
axis as usize
} else {
rank.checked_sub(axis.unsigned_abs())
.ok_or(tenferro_tensor::Error::AxisOutOfBounds {
op,
axis: axis.unsigned_abs(),
rank,
})?
};
if normalized >= rank {
return Err(tenferro_tensor::Error::AxisOutOfBounds {
op,
axis: axis.unsigned_abs(),
rank,
}
.into());
}
Ok(normalized)
}
fn normalize_insert_axis(op: &'static str, axis: isize, rank: usize) -> Result<usize> {
let insert_rank = rank
.checked_add(1)
.ok_or(tenferro_tensor::Error::AxisOutOfBounds {
op,
axis: axis.unsigned_abs(),
rank,
})?;
let normalized = if axis >= 0 {
axis as usize
} else {
insert_rank.checked_sub(axis.unsigned_abs()).ok_or(
tenferro_tensor::Error::AxisOutOfBounds {
op,
axis: axis.unsigned_abs(),
rank: insert_rank,
},
)?
};
if normalized > rank {
return Err(tenferro_tensor::Error::AxisOutOfBounds {
op,
axis: axis.unsigned_abs(),
rank: insert_rank,
}
.into());
}
Ok(normalized)
}
fn index_select_config(
shape: &[usize],
axis: isize,
positions: &[usize],
) -> Result<(Tensor, GatherConfig)> {
let axis = normalize_existing_axis("index_select", axis, shape.len())?;
let axis_extent = shape[axis];
for &position in positions {
if position >= axis_extent {
return Err(tenferro_tensor::Error::InvalidConfig {
op: "index_select",
message: format!(
"position {position} out of bounds for axis {axis} with extent {axis_extent}"
),
}
.into());
}
}
let mut slice_sizes = shape.to_vec();
slice_sizes[axis] = 1;
let offset_dims = (0..shape.len()).filter(|&dim| dim != axis).collect();
let index_data = positions
.iter()
.map(|&position| {
i64::try_from(position).map_err(|_| tenferro_tensor::Error::InvalidConfig {
op: "index_select",
message: format!("position {position} cannot be represented as i64"),
})
})
.collect::<tenferro_tensor::Result<Vec<_>>>()?;
let indices = Tensor::I64(TypedTensor::from_vec_col_major(
vec![positions.len(), 1],
index_data,
)?);
let config = GatherConfig {
offset_dims,
collapsed_slice_dims: vec![axis],
start_index_map: vec![axis],
index_vector_dim: 1,
slice_sizes,
};
Ok((indices, config))
}
fn validate_stack_shapes(op: &'static str, shapes: &[&[usize]]) -> Result<()> {
let Some(first) = shapes.first() else {
return Err(tenferro_tensor::Error::InvalidConfig {
op,
message: "stack requires at least one input".into(),
}
.into());
};
for shape in shapes.iter().skip(1) {
if *shape != *first {
return Err(tenferro_tensor::Error::ShapeMismatch {
op,
lhs: first.to_vec(),
rhs: shape.to_vec(),
}
.into());
}
}
Ok(())
}
impl EagerTensor {
/// Select entries from one axis using host-known indices.
///
/// The index list is primal metadata: gradients flow to `self`, including
/// accumulation for repeated indices, but not to the selected positions.
///
/// # Examples
///
/// ```
/// use tenferro_cpu::CpuBackend;
/// use tenferro_ad::{EagerRuntime, EagerTensor, Tensor};
///
/// let ctx = EagerRuntime::with_cpu_backend(CpuBackend::new());
/// let x = EagerTensor::from_tensor_in(
/// Tensor::from_vec_col_major(vec![3], vec![10.0_f64, 20.0, 30.0]).unwrap(),
/// ctx,
/// ).unwrap();
/// let y = x.take_axis(0, &[2, 0]).unwrap();
///
/// assert_eq!(y.materialized().unwrap().as_slice::<f64>().unwrap(), &[30.0, 10.0]);
/// ```
pub fn take_axis(&self, axis: usize, indices: &[usize]) -> Result<Self> {
let axis = isize::try_from(axis).map_err(|_| {
Error::TensorRuntime(tenferro_tensor::Error::InvalidConfig {
op: "take_axis",
message: format!("axis {axis} cannot be represented as isize"),
})
})?;
self.index_select(axis, indices)
}
/// Select matrix rows using host-known row indices.
///
/// # Examples
///
/// ```
/// use tenferro_cpu::CpuBackend;
/// use tenferro_ad::{EagerRuntime, EagerTensor, Tensor};
///
/// let ctx = EagerRuntime::with_cpu_backend(CpuBackend::new());
/// let x = EagerTensor::from_tensor_in(
/// Tensor::from_vec_col_major(vec![2, 2], vec![1.0_f64, 2.0, 3.0, 4.0]).unwrap(),
/// ctx,
/// ).unwrap();
/// let y = x.take_rows(&[1]).unwrap();
///
/// assert_eq!(y.shape(), &[1, 2]);
/// assert_eq!(y.materialized().unwrap().as_slice::<f64>().unwrap(), &[2.0, 4.0]);
/// ```
pub fn take_rows(&self, rows: &[usize]) -> Result<Self> {
self.take_axis(0, rows)
}
/// Select matrix columns using host-known column indices.
///
/// # Examples
///
/// ```
/// use tenferro_cpu::CpuBackend;
/// use tenferro_ad::{EagerRuntime, EagerTensor, Tensor};
///
/// let ctx = EagerRuntime::with_cpu_backend(CpuBackend::new());
/// let x = EagerTensor::from_tensor_in(
/// Tensor::from_vec_col_major(vec![2, 2], vec![1.0_f64, 2.0, 3.0, 4.0]).unwrap(),
/// ctx,
/// ).unwrap();
/// let y = x.take_cols(&[1]).unwrap();
///
/// assert_eq!(y.shape(), &[2, 1]);
/// assert_eq!(y.materialized().unwrap().as_slice::<f64>().unwrap(), &[3.0, 4.0]);
/// ```
pub fn take_cols(&self, cols: &[usize]) -> Result<Self> {
self.take_axis(1, cols)
}
/// Select a matrix block using host-known row and column indices.
///
/// This is a convenience wrapper over row selection followed by column
/// selection. The row and column lists, plus the approximation rank implied
/// by their lengths, are fixed primal metadata.
///
/// # Examples
///
/// ```
/// use tenferro_cpu::CpuBackend;
/// use tenferro_ad::{EagerRuntime, EagerTensor, Tensor};
///
/// let ctx = EagerRuntime::with_cpu_backend(CpuBackend::new());
/// let x = EagerTensor::from_tensor_in(
/// Tensor::from_vec_col_major(vec![2, 2], vec![1.0_f64, 2.0, 3.0, 4.0]).unwrap(),
/// ctx,
/// ).unwrap();
/// let y = x.take_block(&[1], &[0]).unwrap();
///
/// assert_eq!(y.shape(), &[1, 1]);
/// assert_eq!(y.materialized().unwrap().as_slice::<f64>().unwrap(), &[2.0]);
/// ```
pub fn take_block(&self, rows: &[usize], cols: &[usize]) -> Result<Self> {
self.take_rows(rows)?.take_cols(cols)
}
/// Select entries from one axis using host-known positions.
///
/// # Examples
///
/// ```
/// use tenferro_cpu::CpuBackend;
/// use tenferro_ad::{EagerRuntime, EagerTensor, Tensor};
///
/// let ctx = EagerRuntime::with_cpu_backend(CpuBackend::new());
/// let x = EagerTensor::from_tensor_in(
/// Tensor::from_vec_col_major(vec![3], vec![10.0_f64, 20.0, 30.0]).unwrap(),
/// ctx,
/// ).unwrap();
/// let y = x.index_select(-1, &[2, 0]).unwrap();
///
/// assert_eq!(y.materialized().unwrap().as_slice::<f64>().unwrap(), &[30.0, 10.0]);
/// ```
pub fn index_select(&self, axis: isize, positions: &[usize]) -> Result<Self> {
let (indices, config) = index_select_config(self.shape(), axis, positions)?;
let indices = {
let mut backend = self
.ctx
.backend
.lock()
.map_err(|_| Error::Internal("backend lock poisoned".to_string()))?;
backend.upload_host_tensor(&indices)?
};
let indices = self.ctx.constant_from(indices)?;
self.gather(&indices, config)
}
/// Stack tensors along a newly inserted axis.
///
/// The returned tensor uses the context of the first input, matching
/// [`Self::concatenate`]. All inputs must belong to that same context.
///
/// # Examples
///
/// ```
/// use tenferro_cpu::CpuBackend;
/// use tenferro_ad::{EagerRuntime, EagerTensor, Tensor};
///
/// let ctx = EagerRuntime::with_cpu_backend(CpuBackend::new());
/// let a = EagerTensor::from_tensor_in(Tensor::from_vec_col_major(vec![], vec![1.0_f64]).unwrap(), ctx.clone()).unwrap();
/// let b = EagerTensor::from_tensor_in(Tensor::from_vec_col_major(vec![], vec![2.0_f64]).unwrap(), ctx).unwrap();
/// let out = EagerTensor::stack(&[&a, &b], -1).unwrap();
///
/// assert_eq!(out.shape(), &[2]);
/// assert_eq!(out.materialized().unwrap().as_slice::<f64>().unwrap(), &[1.0, 2.0]);
/// ```
pub fn stack(tensors: &[&Self], dim: isize) -> Result<Self> {
let first = tensors.first().copied().ok_or_else(|| {
Error::TensorRuntime(tenferro_tensor::Error::InvalidConfig {
op: "stack",
message: "stack requires at least one input".into(),
})
})?;
let shapes = tensors
.iter()
.map(|tensor| tensor.shape())
.collect::<Vec<_>>();
validate_stack_shapes("stack", &shapes)?;
let axis = normalize_insert_axis("stack", dim, first.shape().len())?;
let mut expanded_shape = first.shape().to_vec();
expanded_shape.insert(axis, 1);
let expanded = tensors
.iter()
.map(|tensor| tensor.reshape(&expanded_shape))
.collect::<Result<Vec<_>>>()?;
let refs = expanded.iter().collect::<Vec<_>>();
Self::concatenate(&refs, axis)
}
}
#[cfg(test)]
mod tests {
use super::{normalize_existing_axis, normalize_insert_axis};
#[test]
fn axis_normalization_handles_ranks_larger_than_isize_max() {
assert_eq!(normalize_existing_axis("test", 0, usize::MAX).unwrap(), 0);
assert_eq!(
normalize_existing_axis("test", -1, usize::MAX).unwrap(),
usize::MAX - 1
);
assert_eq!(
normalize_insert_axis("test", -1, usize::MAX - 1).unwrap(),
usize::MAX - 1
);
assert!(normalize_insert_axis("test", -1, usize::MAX).is_err());
}
}