1use std::ops::Range;
2
3use tenferro_ops::std_tensor_op::StdTensorOp;
4use tenferro_tensor::{
5 GatherConfig, ShapeMismatch, SliceConfig, Tensor, TypedTensor, ValidationError,
6};
7
8use crate::error::{Error, ErrorPhase, Result};
9use crate::shape_infer::promote_dtypes;
10use crate::sym_dim::SymDim;
11use crate::traced::{
12 apply_binary_preserve_input_dtypes, infer_traced_single_output_shape, try_concrete_shape,
13};
14use crate::TracedTensor;
15
16pub(crate) fn normalize_existing_axis(op: &'static str, axis: isize, rank: usize) -> Result<usize> {
19 tenferro_tensor::normalize_axis(axis, rank)
20 .map_err(|e| Error::validation(op, ErrorPhase::GraphBuild, e))
21}
22
23fn signed(axis: usize) -> Result<isize> {
27 isize::try_from(axis).map_err(|_| {
28 Error::validation(
29 "slice_builder",
30 ErrorPhase::GraphBuild,
31 ValidationError::IntegerOverflow,
32 )
33 })
34}
35
36pub(crate) fn normalize_existing_axes(
37 op: &'static str,
38 axes: &[isize],
39 rank: usize,
40) -> Result<Vec<usize>> {
41 axes.iter()
42 .map(|&axis| normalize_existing_axis(op, axis, rank))
43 .collect()
44}
45
46pub(crate) fn normalize_insert_axis(op: &'static str, axis: isize, rank: usize) -> Result<usize> {
47 let insert_rank = rank.checked_add(1).ok_or_else(|| {
48 Error::validation(
49 op,
50 ErrorPhase::GraphBuild,
51 ValidationError::AxisOutOfBounds { axis, rank },
52 )
53 })?;
54 let normalized = if axis >= 0 {
55 axis as usize
56 } else {
57 insert_rank
58 .checked_sub(axis.unsigned_abs())
59 .ok_or_else(|| {
60 Error::validation(
61 op,
62 ErrorPhase::GraphBuild,
63 ValidationError::AxisOutOfBounds {
64 axis,
65 rank: insert_rank,
66 },
67 )
68 })?
69 };
70 if normalized > rank {
71 return Err(Error::validation(
72 op,
73 ErrorPhase::GraphBuild,
74 ValidationError::AxisOutOfBounds {
75 axis,
76 rank: insert_rank,
77 },
78 ));
79 }
80 Ok(normalized)
81}
82
83fn index_select_config(
84 shape: &[usize],
85 axis: isize,
86 positions: &[usize],
87) -> Result<(Tensor, GatherConfig, Vec<usize>)> {
88 let axis = normalize_existing_axis("index_select", axis, shape.len())?;
89 let axis_extent = shape[axis];
90 for &position in positions {
91 if position >= axis_extent {
92 return Err(Error::validation(
93 "index_select",
94 ErrorPhase::GraphBuild,
95 ValidationError::InvalidArgument {
96 argument: "positions",
97 message: format!(
98 "position {position} out of bounds for axis {axis} with extent {axis_extent}"
99 ),
100 },
101 ));
102 }
103 }
104
105 let mut out_shape = shape.to_vec();
106 out_shape[axis] = positions.len();
107
108 let mut slice_sizes = shape.to_vec();
109 slice_sizes[axis] = 1;
110
111 let offset_dims = (0..shape.len()).filter(|&dim| dim != axis).collect();
112 let index_data = positions
113 .iter()
114 .map(|&position| {
115 i64::try_from(position).map_err(|_| {
116 Error::validation(
117 "index_select",
118 ErrorPhase::GraphBuild,
119 ValidationError::InvalidArgument {
120 argument: "positions",
121 message: format!("position {position} cannot be represented as i64"),
122 },
123 )
124 })
125 })
126 .collect::<Result<Vec<_>>>()?;
127 let indices = Tensor::from_typed::<i64>(TypedTensor::from_vec_col_major(
128 vec![positions.len(), 1],
129 index_data,
130 )?);
131
132 let config = GatherConfig {
133 offset_dims,
134 collapsed_slice_dims: vec![axis],
135 start_index_map: vec![axis],
136 index_vector_dim: 1,
137 slice_sizes,
138 };
139
140 Ok((indices, config, out_shape))
141}
142
143fn validate_stack_shapes(op: &'static str, shapes: &[&[usize]]) -> Result<()> {
144 let Some(first) = shapes.first() else {
145 return Err(Error::validation(
146 op,
147 ErrorPhase::GraphBuild,
148 ValidationError::InvalidArgument {
149 argument: "tensors",
150 message: "stack requires at least one input".into(),
151 },
152 ));
153 };
154 for shape in shapes.iter().skip(1) {
155 if *shape != *first {
156 return Err(Error::validation(
157 op,
158 ErrorPhase::GraphBuild,
159 ShapeMismatch::IncompatibleShapes {
160 lhs: first.iter().copied().collect(),
161 rhs: shape.iter().copied().collect(),
162 }
163 .into(),
164 ));
165 }
166 }
167 Ok(())
168}
169
170#[derive(Clone, Debug)]
171enum AxisSelection<A = usize> {
172 Slice {
173 axis: A,
174 range: Range<usize>,
175 step: usize,
176 },
177 Take {
178 axis: A,
179 indices: Vec<usize>,
180 },
181}
182
183fn concrete_shape_for_axis_slice(tensor: &TracedTensor, op: &'static str) -> Result<Vec<usize>> {
184 try_concrete_shape(tensor).ok_or_else(|| {
185 Error::validation(
186 op,
187 ErrorPhase::GraphBuild,
188 ValidationError::InvalidArgument {
189 argument: "shape",
190 message: format!("{op} requires a concrete shape hint"),
191 },
192 )
193 })
194}
195
196fn validate_axis_selection(
197 op: &'static str,
198 rank: usize,
199 seen: &mut [bool],
200 axis: usize,
201) -> Result<()> {
202 if axis >= rank {
203 return Err(Error::validation(
204 op,
205 ErrorPhase::GraphBuild,
206 ValidationError::AxisOutOfBounds {
207 axis: isize::try_from(axis).unwrap_or(isize::MAX),
208 rank,
209 },
210 ));
211 }
212 if seen[axis] {
213 return Err(Error::validation(
214 op,
215 ErrorPhase::GraphBuild,
216 ValidationError::DuplicateAxis {
217 axis,
218 role: "selection",
219 },
220 ));
221 }
222 seen[axis] = true;
223 Ok(())
224}
225
226fn apply_slice_axis_config(
227 op: &'static str,
228 shape: &[usize],
229 selections: &[AxisSelection],
230) -> Result<Option<SliceConfig>> {
231 let mut starts = vec![0; shape.len()];
232 let mut limits = shape.to_vec();
233 let mut strides = vec![1; shape.len()];
234 let mut has_slice = false;
235 for selection in selections {
236 let AxisSelection::Slice { axis, range, step } = selection else {
237 continue;
238 };
239 if *step == 0 {
240 return Err(Error::validation(
241 op,
242 ErrorPhase::GraphBuild,
243 ValidationError::InvalidSliceStep { step: 0 },
244 ));
245 }
246 let extent = shape[*axis];
247 if range.start > range.end || range.end > extent {
248 let start = isize::try_from(range.start).map_err(|_| {
249 Error::validation(op, ErrorPhase::GraphBuild, ValidationError::IntegerOverflow)
250 })?;
251 let end = isize::try_from(range.end).map_err(|_| {
252 Error::validation(op, ErrorPhase::GraphBuild, ValidationError::IntegerOverflow)
253 })?;
254 return Err(Error::validation(
255 op,
256 ErrorPhase::GraphBuild,
257 ValidationError::InvalidSliceBounds {
258 start,
259 end,
260 axis_len: extent,
261 },
262 ));
263 }
264 starts[*axis] = range.start;
265 limits[*axis] = range.end;
266 strides[*axis] = *step;
267 has_slice = true;
268 }
269 Ok(has_slice.then_some(SliceConfig {
270 starts,
271 limits,
272 strides,
273 }))
274}
275
276#[derive(Clone, Debug)]
293pub struct TracedSliceBuilder<'a> {
294 tensor: &'a TracedTensor,
295 selections: Vec<AxisSelection<isize>>,
296}
297
298impl<'a> TracedSliceBuilder<'a> {
299 fn new(tensor: &'a TracedTensor) -> Self {
300 Self {
301 tensor,
302 selections: Vec::new(),
303 }
304 }
305
306 pub fn axis(mut self, axis: isize, range: Range<usize>) -> Self {
320 self.selections.push(AxisSelection::Slice {
321 axis,
322 range,
323 step: 1,
324 });
325 self
326 }
327
328 pub fn axis_step(mut self, axis: isize, range: Range<usize>, step: usize) -> Self {
342 self.selections
343 .push(AxisSelection::Slice { axis, range, step });
344 self
345 }
346
347 pub fn take_axis(mut self, axis: isize, indices: &[usize]) -> Self {
361 self.selections.push(AxisSelection::Take {
362 axis,
363 indices: indices.to_vec(),
364 });
365 self
366 }
367
368 pub fn apply(self) -> Result<TracedTensor> {
389 let shape = concrete_shape_for_axis_slice(self.tensor, "slice_builder")?;
390 let selections = self
391 .selections
392 .into_iter()
393 .map(|selection| {
394 Ok(match selection {
395 AxisSelection::Slice { axis, range, step } => AxisSelection::Slice {
396 axis: normalize_existing_axis("slice_builder", axis, shape.len())?,
397 range,
398 step,
399 },
400 AxisSelection::Take { axis, indices } => AxisSelection::Take {
401 axis: normalize_existing_axis("slice_builder", axis, shape.len())?,
402 indices,
403 },
404 })
405 })
406 .collect::<Result<Vec<AxisSelection>>>()?;
407 let mut seen = vec![false; shape.len()];
408 for selection in &selections {
409 let axis = match selection {
410 AxisSelection::Slice { axis, .. } | AxisSelection::Take { axis, .. } => *axis,
411 };
412 validate_axis_selection("slice_builder", shape.len(), &mut seen, axis)?;
413 }
414
415 let mut output = self.tensor.clone();
416 if let Some(config) = apply_slice_axis_config("slice_builder", &shape, &selections)? {
417 output = output.slice(config)?;
418 }
419 for selection in selections {
420 if let AxisSelection::Take { axis, indices } = selection {
421 output = output.take_axis(signed(axis)?, &indices)?;
422 }
423 }
424 Ok(output)
425 }
426}
427
428impl TracedTensor {
429 pub fn slice_axis(&self, axis: isize, range: Range<usize>) -> Result<Self> {
449 self.slice_builder().axis(axis, range).apply()
450 }
451
452 pub fn slice_builder(&self) -> TracedSliceBuilder<'_> {
466 TracedSliceBuilder::new(self)
467 }
468
469 pub fn take_axis(&self, axis: isize, indices: &[usize]) -> Result<Self> {
489 self.index_select(axis, indices)
490 }
491
492 pub fn index_select(&self, axis: isize, positions: &[usize]) -> Result<Self> {
536 let shape = try_concrete_shape(self).ok_or_else(|| {
537 Error::validation(
538 "index_select",
539 ErrorPhase::GraphBuild,
540 ValidationError::InvalidArgument {
541 argument: "shape",
542 message: "index_select requires a concrete shape hint".into(),
543 },
544 )
545 })?;
546 let (indices_tensor, config, out_shape) = index_select_config(&shape, axis, positions)?;
547 let indices = self.context().from_tensor_concrete_shape(indices_tensor)?;
548 apply_binary_preserve_input_dtypes(
549 StdTensorOp::Gather(config),
550 self,
551 &indices,
552 out_shape.len(),
553 Some(out_shape.into_iter().map(SymDim::from).collect()),
554 self.dtype,
555 )
556 }
557
558 pub fn stack(tensors: &[&Self], dim: isize) -> Result<Self> {
599 let first = tensors.first().copied().ok_or_else(|| {
600 Error::validation(
601 "stack",
602 ErrorPhase::GraphBuild,
603 ValidationError::InvalidArgument {
604 argument: "tensors",
605 message: "stack requires at least one input".into(),
606 },
607 )
608 })?;
609 let first_shape = try_concrete_shape(first).ok_or_else(|| {
610 Error::validation(
611 "stack",
612 ErrorPhase::GraphBuild,
613 ValidationError::InvalidArgument {
614 argument: "shape",
615 message: "stack requires concrete shape hints".into(),
616 },
617 )
618 })?;
619 let mut shapes = Vec::with_capacity(tensors.len());
620 shapes.push(first_shape.as_slice());
621 let mut owned_shapes = Vec::with_capacity(tensors.len().saturating_sub(1));
622 for tensor in tensors.iter().copied().skip(1) {
623 owned_shapes.push(try_concrete_shape(tensor).ok_or_else(|| {
624 Error::validation(
625 "stack",
626 ErrorPhase::GraphBuild,
627 ValidationError::InvalidArgument {
628 argument: "shape",
629 message: "stack requires concrete shape hints".into(),
630 },
631 )
632 })?);
633 }
634 shapes.extend(owned_shapes.iter().map(Vec::as_slice));
635 validate_stack_shapes("stack", &shapes)?;
636
637 let axis = normalize_insert_axis("stack", dim, first.rank)?;
638 let mut expanded_shape = first_shape;
639 expanded_shape.insert(axis, 1);
640 let mut out_shape = expanded_shape.clone();
641 out_shape[axis] = tensors.len();
642 let expanded = tensors
643 .iter()
644 .map(|tensor| tensor.reshape(&expanded_shape))
645 .collect::<Result<Vec<_>>>()?;
646 let refs = expanded.iter().collect::<Vec<_>>();
647 apply_nary_concatenate(
648 &refs,
649 axis,
650 out_shape.into_iter().map(SymDim::from).collect(),
651 )
652 }
653
654 pub fn concatenate(tensors: &[&Self], axis: isize) -> Result<Self> {
662 let first = tensors.first().copied().ok_or_else(|| {
663 Error::validation(
664 "concatenate",
665 ErrorPhase::GraphBuild,
666 ValidationError::InvalidArgument {
667 argument: "tensors",
668 message: "concatenate requires at least one input".into(),
669 },
670 )
671 })?;
672 let axis = normalize_existing_axis("concatenate", axis, first.rank)?;
673 for tensor in tensors.iter().copied().skip(1) {
674 if tensor.rank != first.rank {
675 return Err(Error::validation(
676 "concatenate",
677 ErrorPhase::GraphBuild,
678 ValidationError::RankMismatch {
679 expected: first.rank,
680 actual: tensor.rank,
681 },
682 ));
683 }
684 }
685
686 let op = StdTensorOp::Concatenate {
687 axis,
688 input_count: tensors.len(),
689 };
690 let (_, out_shape_hint) =
691 infer_traced_single_output_shape("TracedTensor::concatenate", &op, tensors)?;
692 let out_shape = out_shape_hint.ok_or_else(|| {
693 Error::Internal("concatenate shape inference returned no shape hint".into())
694 })?;
695 apply_nary_concatenate(tensors, axis, out_shape)
696 }
697}
698
699fn apply_nary_concatenate(
700 tensors: &[&TracedTensor],
701 axis: usize,
702 out_shape: Vec<SymDim>,
703) -> Result<TracedTensor> {
704 let out_dtype = promote_dtypes(tensors.iter().map(|tensor| tensor.dtype));
705 let tensors = tensors
706 .iter()
707 .map(|tensor| {
708 if tensor.dtype != out_dtype {
709 tensor.cast(out_dtype)
710 } else {
711 Ok((*tensor).clone())
712 }
713 })
714 .collect::<Result<Vec<_>>>()?;
715
716 let Some(first) = tensors.first() else {
717 return Err(Error::invalid_argument(
718 "TracedSliceBuilder::concatenate",
719 ErrorPhase::GraphBuild,
720 "tensors",
721 "at least one tensor is required",
722 ));
723 };
724 for tensor in tensors.iter().skip(1) {
725 crate::traced::ensure_same_context(first, tensor)?;
726 }
727 let values: Vec<_> = tensors.iter().map(|tensor| tensor.value).collect();
728 let op = StdTensorOp::Concatenate {
729 axis,
730 input_count: tensors.len(),
731 };
732 let outputs = crate::trace_ops::append_core(&first.ctx, &op, &values)?;
733 crate::trace_ops::finish_value(
734 &first.ctx,
735 outputs[0],
736 out_shape.len(),
737 out_dtype,
738 Some(out_shape),
739 )
740}
741
742#[cfg(test)]
743mod tests;