1use std::ops::Range;
2use std::sync::Arc;
3
4use computegraph::graph::GraphBuilder;
5use computegraph::types::{OperationRole, ValueRef};
6use tenferro_ops::std_tensor_op::StdTensorOp;
7use tenferro_tensor::{
8 GatherConfig, ShapeMismatch, ShapeVec, SliceConfig, Tensor, TypedTensor, ValidationError,
9};
10
11use crate::checkpoint::CheckpointNode;
12use crate::error::{Error, ErrorPhase, Result};
13use crate::metadata::{register_scoped_value_metadata, tensor_meta, MetadataScopeChain};
14use crate::shape_constraint::ConstraintScopeChain;
15use crate::shape_infer::promote_dtypes;
16use crate::sym_dim::SymDim;
17use crate::traced::{
18 apply_binary_preserve_input_dtypes, infer_traced_single_output_shape, merge_traced_inputs_map,
19 merge_traced_leaf_metas, next_traced_id, try_concrete_shape,
20};
21use crate::TracedTensor;
22
23fn normalize_existing_axis(op: &'static str, axis: isize, rank: usize) -> Result<usize> {
24 let normalized = if axis >= 0 {
25 axis as usize
26 } else {
27 rank.checked_sub(axis.unsigned_abs()).ok_or_else(|| {
28 Error::validation(
29 op,
30 ErrorPhase::GraphBuild,
31 ValidationError::AxisOutOfBounds {
32 axis: axis.unsigned_abs(),
33 rank,
34 },
35 )
36 })?
37 };
38 if normalized >= rank {
39 return Err(Error::validation(
40 op,
41 ErrorPhase::GraphBuild,
42 ValidationError::AxisOutOfBounds {
43 axis: axis.unsigned_abs(),
44 rank,
45 },
46 ));
47 }
48 Ok(normalized)
49}
50
51fn normalize_insert_axis(op: &'static str, axis: isize, rank: usize) -> Result<usize> {
52 let insert_rank = rank.checked_add(1).ok_or_else(|| {
53 Error::validation(
54 op,
55 ErrorPhase::GraphBuild,
56 ValidationError::AxisOutOfBounds {
57 axis: axis.unsigned_abs(),
58 rank,
59 },
60 )
61 })?;
62 let normalized = if axis >= 0 {
63 axis as usize
64 } else {
65 insert_rank
66 .checked_sub(axis.unsigned_abs())
67 .ok_or_else(|| {
68 Error::validation(
69 op,
70 ErrorPhase::GraphBuild,
71 ValidationError::AxisOutOfBounds {
72 axis: axis.unsigned_abs(),
73 rank: insert_rank,
74 },
75 )
76 })?
77 };
78 if normalized > rank {
79 return Err(Error::validation(
80 op,
81 ErrorPhase::GraphBuild,
82 ValidationError::AxisOutOfBounds {
83 axis: axis.unsigned_abs(),
84 rank: insert_rank,
85 },
86 ));
87 }
88 Ok(normalized)
89}
90
91fn index_select_config(
92 shape: &[usize],
93 axis: isize,
94 positions: &[usize],
95) -> Result<(Tensor, GatherConfig, Vec<usize>)> {
96 let axis = normalize_existing_axis("index_select", axis, shape.len())?;
97 let axis_extent = shape[axis];
98 for &position in positions {
99 if position >= axis_extent {
100 return Err(Error::validation(
101 "index_select",
102 ErrorPhase::GraphBuild,
103 ValidationError::InvalidArgument {
104 argument: "positions",
105 message: format!(
106 "position {position} out of bounds for axis {axis} with extent {axis_extent}"
107 ),
108 },
109 ));
110 }
111 }
112
113 let mut out_shape = shape.to_vec();
114 out_shape[axis] = positions.len();
115
116 let mut slice_sizes = shape.to_vec();
117 slice_sizes[axis] = 1;
118
119 let offset_dims = (0..shape.len()).filter(|&dim| dim != axis).collect();
120 let index_data = positions
121 .iter()
122 .map(|&position| {
123 i64::try_from(position).map_err(|_| {
124 Error::validation(
125 "index_select",
126 ErrorPhase::GraphBuild,
127 ValidationError::InvalidArgument {
128 argument: "positions",
129 message: format!("position {position} cannot be represented as i64"),
130 },
131 )
132 })
133 })
134 .collect::<Result<Vec<_>>>()?;
135 let indices = Tensor::I64(TypedTensor::from_vec_col_major(
136 vec![positions.len(), 1],
137 index_data,
138 )?);
139
140 let config = GatherConfig {
141 offset_dims,
142 collapsed_slice_dims: vec![axis],
143 start_index_map: vec![axis],
144 index_vector_dim: 1,
145 slice_sizes,
146 };
147
148 Ok((indices, config, out_shape))
149}
150
151fn validate_stack_shapes(op: &'static str, shapes: &[&[usize]]) -> Result<()> {
152 let Some(first) = shapes.first() else {
153 return Err(Error::validation(
154 op,
155 ErrorPhase::GraphBuild,
156 ValidationError::InvalidArgument {
157 argument: "tensors",
158 message: "stack requires at least one input".into(),
159 },
160 ));
161 };
162 for shape in shapes.iter().skip(1) {
163 if *shape != *first {
164 return Err(Error::validation(
165 op,
166 ErrorPhase::GraphBuild,
167 ShapeMismatch::IncompatibleShapes {
168 lhs: ShapeVec::from_vec(first.to_vec()),
169 rhs: ShapeVec::from_vec(shape.to_vec()),
170 }
171 .into(),
172 ));
173 }
174 }
175 Ok(())
176}
177
178#[derive(Clone, Debug)]
179enum AxisSelection {
180 Slice {
181 axis: usize,
182 range: Range<usize>,
183 step: usize,
184 },
185 Take {
186 axis: usize,
187 indices: Vec<usize>,
188 },
189}
190
191fn concrete_shape_for_axis_slice(tensor: &TracedTensor, op: &'static str) -> Result<Vec<usize>> {
192 try_concrete_shape(tensor).ok_or_else(|| {
193 Error::validation(
194 op,
195 ErrorPhase::GraphBuild,
196 ValidationError::InvalidArgument {
197 argument: "shape",
198 message: format!("{op} requires a concrete shape hint"),
199 },
200 )
201 })
202}
203
204fn validate_axis_selection(
205 op: &'static str,
206 rank: usize,
207 seen: &mut [bool],
208 axis: usize,
209) -> Result<()> {
210 if axis >= rank {
211 return Err(Error::validation(
212 op,
213 ErrorPhase::GraphBuild,
214 ValidationError::AxisOutOfBounds { axis, rank },
215 ));
216 }
217 if seen[axis] {
218 return Err(Error::validation(
219 op,
220 ErrorPhase::GraphBuild,
221 ValidationError::DuplicateAxis {
222 axis,
223 role: "selection",
224 },
225 ));
226 }
227 seen[axis] = true;
228 Ok(())
229}
230
231fn apply_slice_axis_config(
232 op: &'static str,
233 shape: &[usize],
234 selections: &[AxisSelection],
235) -> Result<Option<SliceConfig>> {
236 let mut starts = vec![0; shape.len()];
237 let mut limits = shape.to_vec();
238 let mut strides = vec![1; shape.len()];
239 let mut has_slice = false;
240 for selection in selections {
241 let AxisSelection::Slice { axis, range, step } = selection else {
242 continue;
243 };
244 if *step == 0 {
245 return Err(Error::validation(
246 op,
247 ErrorPhase::GraphBuild,
248 ValidationError::InvalidSliceStep { step: 0 },
249 ));
250 }
251 let extent = shape[*axis];
252 if range.start > range.end || range.end > extent {
253 let start = isize::try_from(range.start).map_err(|_| {
254 Error::validation(op, ErrorPhase::GraphBuild, ValidationError::IntegerOverflow)
255 })?;
256 let end = isize::try_from(range.end).map_err(|_| {
257 Error::validation(op, ErrorPhase::GraphBuild, ValidationError::IntegerOverflow)
258 })?;
259 return Err(Error::validation(
260 op,
261 ErrorPhase::GraphBuild,
262 ValidationError::InvalidSliceBounds {
263 start,
264 end,
265 axis_len: extent,
266 },
267 ));
268 }
269 starts[*axis] = range.start;
270 limits[*axis] = range.end;
271 strides[*axis] = *step;
272 has_slice = true;
273 }
274 Ok(has_slice.then_some(SliceConfig {
275 starts,
276 limits,
277 strides,
278 }))
279}
280
281#[derive(Clone, Debug)]
297pub struct TracedSliceBuilder<'a> {
298 tensor: &'a TracedTensor,
299 selections: Vec<AxisSelection>,
300}
301
302impl<'a> TracedSliceBuilder<'a> {
303 fn new(tensor: &'a TracedTensor) -> Self {
304 Self {
305 tensor,
306 selections: Vec::new(),
307 }
308 }
309
310 pub fn axis(mut self, axis: usize, range: Range<usize>) -> Self {
322 self.selections.push(AxisSelection::Slice {
323 axis,
324 range,
325 step: 1,
326 });
327 self
328 }
329
330 pub fn axis_step(mut self, axis: usize, 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: usize, indices: &[usize]) -> Self {
359 self.selections.push(AxisSelection::Take {
360 axis,
361 indices: indices.to_vec(),
362 });
363 self
364 }
365
366 pub fn apply(self) -> Result<TracedTensor> {
385 let shape = concrete_shape_for_axis_slice(self.tensor, "slice_builder")?;
386 let mut seen = vec![false; shape.len()];
387 for selection in &self.selections {
388 let axis = match selection {
389 AxisSelection::Slice { axis, .. } | AxisSelection::Take { axis, .. } => *axis,
390 };
391 validate_axis_selection("slice_builder", shape.len(), &mut seen, axis)?;
392 }
393
394 let mut output = self.tensor.clone();
395 if let Some(config) = apply_slice_axis_config("slice_builder", &shape, &self.selections)? {
396 output = output.slice(config)?;
397 }
398 for selection in self.selections {
399 if let AxisSelection::Take { axis, indices } = selection {
400 output = output.take_axis(axis, &indices)?;
401 }
402 }
403 Ok(output)
404 }
405}
406
407impl TracedTensor {
408 pub fn slice_axis(&self, axis: usize, range: Range<usize>) -> Result<Self> {
426 self.slice_builder().axis(axis, range).apply()
427 }
428
429 pub fn slice_builder(&self) -> TracedSliceBuilder<'_> {
441 TracedSliceBuilder::new(self)
442 }
443
444 pub fn take_axis(&self, axis: usize, indices: &[usize]) -> Result<Self> {
462 let axis = isize::try_from(axis).map_err(|_| {
463 Error::validation(
464 "take_axis",
465 ErrorPhase::GraphBuild,
466 ValidationError::InvalidArgument {
467 argument: "axis",
468 message: format!("axis {axis} cannot be represented as isize"),
469 },
470 )
471 })?;
472 self.index_select(axis, indices)
473 }
474
475 pub fn index_select(&self, axis: isize, positions: &[usize]) -> Result<Self> {
512 let shape = try_concrete_shape(self).ok_or_else(|| {
513 Error::validation(
514 "index_select",
515 ErrorPhase::GraphBuild,
516 ValidationError::InvalidArgument {
517 argument: "shape",
518 message: "index_select requires a concrete shape hint".into(),
519 },
520 )
521 })?;
522 let (indices_tensor, config, out_shape) = index_select_config(&shape, axis, positions)?;
523 let indices = TracedTensor::from_tensor_concrete_shape(indices_tensor)?;
524 apply_binary_preserve_input_dtypes(
525 StdTensorOp::Gather(config),
526 self,
527 &indices,
528 out_shape.len(),
529 Some(out_shape.into_iter().map(SymDim::from).collect()),
530 self.dtype,
531 )
532 }
533
534 pub fn stack(tensors: &[&Self], dim: isize) -> Result<Self> {
568 let first = tensors.first().copied().ok_or_else(|| {
569 Error::validation(
570 "stack",
571 ErrorPhase::GraphBuild,
572 ValidationError::InvalidArgument {
573 argument: "tensors",
574 message: "stack requires at least one input".into(),
575 },
576 )
577 })?;
578 let first_shape = try_concrete_shape(first).ok_or_else(|| {
579 Error::validation(
580 "stack",
581 ErrorPhase::GraphBuild,
582 ValidationError::InvalidArgument {
583 argument: "shape",
584 message: "stack requires concrete shape hints".into(),
585 },
586 )
587 })?;
588 let mut shapes = Vec::with_capacity(tensors.len());
589 shapes.push(first_shape.as_slice());
590 let mut owned_shapes = Vec::with_capacity(tensors.len().saturating_sub(1));
591 for tensor in tensors.iter().copied().skip(1) {
592 owned_shapes.push(try_concrete_shape(tensor).ok_or_else(|| {
593 Error::validation(
594 "stack",
595 ErrorPhase::GraphBuild,
596 ValidationError::InvalidArgument {
597 argument: "shape",
598 message: "stack requires concrete shape hints".into(),
599 },
600 )
601 })?);
602 }
603 shapes.extend(owned_shapes.iter().map(Vec::as_slice));
604 validate_stack_shapes("stack", &shapes)?;
605
606 let axis = normalize_insert_axis("stack", dim, first.rank)?;
607 let mut expanded_shape = first_shape;
608 expanded_shape.insert(axis, 1);
609 let mut out_shape = expanded_shape.clone();
610 out_shape[axis] = tensors.len();
611 let expanded = tensors
612 .iter()
613 .map(|tensor| tensor.reshape(&expanded_shape))
614 .collect::<Result<Vec<_>>>()?;
615 let refs = expanded.iter().collect::<Vec<_>>();
616 apply_nary_concatenate(
617 &refs,
618 axis,
619 out_shape.into_iter().map(SymDim::from).collect(),
620 )
621 }
622
623 pub fn concatenate(tensors: &[&Self], axis: usize) -> Result<Self> {
631 let first = tensors.first().copied().ok_or_else(|| {
632 Error::validation(
633 "concatenate",
634 ErrorPhase::GraphBuild,
635 ValidationError::InvalidArgument {
636 argument: "tensors",
637 message: "concatenate requires at least one input".into(),
638 },
639 )
640 })?;
641 if axis >= first.rank {
642 return Err(Error::validation(
643 "concatenate",
644 ErrorPhase::GraphBuild,
645 ValidationError::AxisOutOfBounds {
646 axis,
647 rank: first.rank,
648 },
649 ));
650 }
651 for tensor in tensors.iter().copied().skip(1) {
652 if tensor.rank != first.rank {
653 return Err(Error::validation(
654 "concatenate",
655 ErrorPhase::GraphBuild,
656 ValidationError::RankMismatch {
657 expected: first.rank,
658 actual: tensor.rank,
659 },
660 ));
661 }
662 }
663
664 let op = StdTensorOp::Concatenate {
665 axis,
666 input_count: tensors.len(),
667 };
668 let (_, out_shape_hint) =
669 infer_traced_single_output_shape("TracedTensor::concatenate", &op, tensors)?;
670 let out_shape = out_shape_hint.ok_or_else(|| {
671 Error::Internal("concatenate shape inference returned no shape hint".into())
672 })?;
673 apply_nary_concatenate(tensors, axis, out_shape)
674 }
675}
676
677fn apply_nary_concatenate(
678 tensors: &[&TracedTensor],
679 axis: usize,
680 out_shape: Vec<SymDim>,
681) -> Result<TracedTensor> {
682 let out_dtype = promote_dtypes(tensors.iter().map(|tensor| tensor.dtype));
683 let tensors = tensors
684 .iter()
685 .map(|tensor| {
686 if tensor.dtype != out_dtype {
687 tensor.cast(out_dtype)
688 } else {
689 Ok((*tensor).clone())
690 }
691 })
692 .collect::<Result<Vec<_>>>()?;
693
694 let mut builder = GraphBuilder::new();
695 for tensor in &tensors {
696 builder.add_parent(tensor.graph.clone());
697 }
698 let input_refs = tensors
699 .iter()
700 .map(|tensor| ValueRef::External(tensor.graph.values()[tensor.val].key.clone()))
701 .collect::<Vec<_>>();
702 let outputs = builder.add_operation(
703 StdTensorOp::Concatenate {
704 axis,
705 input_count: tensors.len(),
706 },
707 input_refs,
708 OperationRole::Primary,
709 );
710 builder.set_outputs(outputs.clone());
711 let graph = Arc::new(builder.build());
712 let metadata_scope =
714 super::traced::register_metadata_or_runtime_state(register_scoped_value_metadata(
715 graph.values()[outputs[0]].key.clone(),
716 tensor_meta(out_dtype, out_shape.clone()),
717 ))?;
718
719 let inputs_map = merge_traced_inputs_map(tensors.iter());
720 let leaf_metas = merge_traced_leaf_metas(tensors.iter());
721 let mut extra_roots = Vec::new();
722 let mut checkpoint_chain = None;
723 for tensor in &tensors {
724 extra_roots.extend(tensor.extra_roots.iter().cloned());
725 checkpoint_chain =
726 CheckpointNode::merge_chains(checkpoint_chain, tensor.checkpoint_chain.clone());
727 }
728 Ok(TracedTensor {
729 id: next_traced_id(),
730 rank: out_shape.len(),
731 dtype: out_dtype,
732 graph,
733 val: outputs[0],
734 data: None,
735 shape_hint: Some(out_shape),
736 inputs_map,
737 leaf_metas,
738 extra_roots,
739 checkpoint_chain,
740 metadata_scopes: MetadataScopeChain::with_new(
741 metadata_scope,
742 tensors.iter().map(|tensor| &tensor.metadata_scopes),
743 ),
744 constraint_scopes: ConstraintScopeChain::merge(
745 tensors.iter().map(|tensor| &tensor.constraint_scopes),
746 ),
747 })
748}
749
750#[cfg(test)]
751mod tests;