Skip to main content

tenferro_runtime/
shape_packing.rs

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
16/// Normalize one signed axis of a traced operation (#2552 ยง3.2) through the
17/// shared `tenferro_tensor::normalize_axis`, reported as a graph-build error.
18pub(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
23/// Normalize a signed axis list of a traced operation, keeping its order.
24/// Duplicates are left to the operation's own validation.
25/// An already normalized axis as the signed form of the public surface.
26fn 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/// Rank-preserving traced tensor slicing builder.
277///
278/// Unspecified axes are kept whole. Range selections become one `Slice`
279/// operation; host-known position selections become `Gather`/`index_select`
280/// operations.
281///
282/// # Examples
283///
284/// ```rust
285/// use tenferro_runtime::{TraceContext, TracedTensor};
286///
287/// let trace = TraceContext::new();
288/// let x = trace.from_vec_col_major(vec![3, 4], vec![0.0_f64; 12]).unwrap();
289/// let y = x.slice_builder().axis(0, 0..2).axis_step(1, 0..4, 2).apply().unwrap();
290/// assert_eq!(y.try_concrete_shape(), Some(vec![2, 2]));
291/// ```
292#[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    /// Add an exclusive-end range selection for one axis.
307    ///
308    /// # Examples
309    ///
310    /// ```rust
311    /// use tenferro_runtime::{TracedTensor};
312    ///
313    /// use tenferro_runtime::TraceContext;
314    /// let trace = TraceContext::new();
315    /// let x = trace.from_vec_col_major(vec![4], vec![1.0_f64, 2.0, 3.0, 4.0]).unwrap();
316    /// let y = x.slice_builder().axis(0, 1..3).apply().unwrap();
317    /// assert_eq!(y.try_concrete_shape(), Some(vec![2]));
318    /// ```
319    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    /// Add an exclusive-end strided range selection for one axis.
329    ///
330    /// # Examples
331    ///
332    /// ```rust
333    /// use tenferro_runtime::{TracedTensor};
334    ///
335    /// use tenferro_runtime::TraceContext;
336    /// let trace = TraceContext::new();
337    /// let x = trace.from_vec_col_major(vec![5], vec![1.0_f64, 2.0, 3.0, 4.0, 5.0]).unwrap();
338    /// let y = x.slice_builder().axis_step(0, 0..5, 2).apply().unwrap();
339    /// assert_eq!(y.try_concrete_shape(), Some(vec![3]));
340    /// ```
341    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    /// Add a host-known position selection for one axis.
348    ///
349    /// # Examples
350    ///
351    /// ```rust
352    /// use tenferro_runtime::{TracedTensor};
353    ///
354    /// use tenferro_runtime::TraceContext;
355    /// let trace = TraceContext::new();
356    /// let x = trace.from_vec_col_major(vec![3], vec![1.0_f64, 2.0, 3.0]).unwrap();
357    /// let y = x.slice_builder().take_axis(0, &[2, 0]).apply().unwrap();
358    /// assert_eq!(y.try_concrete_shape(), Some(vec![2]));
359    /// ```
360    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    /// Build and apply the requested slice/take operations.
369    ///
370    /// # Examples
371    ///
372    /// ```rust
373    /// use tenferro_runtime::{TracedTensor};
374    ///
375    /// use tenferro_runtime::TraceContext;
376    /// let trace = TraceContext::new();
377    /// let x = trace.from_vec_col_major(vec![4], vec![1.0_f64, 2.0, 3.0, 4.0]).unwrap();
378    /// let y = x.slice_builder().axis(0, 1..4).apply().unwrap();
379    /// assert_eq!(y.try_concrete_shape(), Some(vec![3]));
380    /// ```
381    ///
382    /// # Errors
383    ///
384    /// Returns [`Error::Validation`] with `AxisOutOfBounds` or
385    /// `DuplicateAxis` when selections are invalid, and propagates the
386    /// underlying [`Error::Validation`] from a slice/take graph operation
387    /// that cannot be built.
388    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    /// Slice one axis with an exclusive-end range, keeping all other axes.
430    ///
431    /// # Examples
432    ///
433    /// ```rust
434    /// use tenferro_runtime::{TracedTensor};
435    ///
436    /// use tenferro_runtime::TraceContext;
437    /// let trace = TraceContext::new();
438    /// let x = trace.from_vec_col_major(vec![4], vec![1.0_f64, 2.0, 3.0, 4.0]).unwrap();
439    /// let y = x.slice_axis(0, 1..3).unwrap();
440    /// assert_eq!(y.try_concrete_shape(), Some(vec![2]));
441    /// ```
442    ///
443    /// # Errors
444    ///
445    /// Returns [`Error::Validation`] with `AxisOutOfBounds` when `axis` is
446    /// outside the concrete rank, or `InvalidArgument` when `range` is
447    /// outside the selected axis extent.
448    pub fn slice_axis(&self, axis: isize, range: Range<usize>) -> Result<Self> {
449        self.slice_builder().axis(axis, range).apply()
450    }
451
452    /// Start a rank-preserving slicing builder for this tensor.
453    ///
454    /// # Examples
455    ///
456    /// ```rust
457    /// use tenferro_runtime::{TracedTensor};
458    ///
459    /// use tenferro_runtime::TraceContext;
460    /// let trace = TraceContext::new();
461    /// let x = trace.from_vec_col_major(vec![3], vec![1.0_f64, 2.0, 3.0]).unwrap();
462    /// let y = x.slice_builder().axis(0, 0..2).apply().unwrap();
463    /// assert_eq!(y.try_concrete_shape(), Some(vec![2]));
464    /// ```
465    pub fn slice_builder(&self) -> TracedSliceBuilder<'_> {
466        TracedSliceBuilder::new(self)
467    }
468
469    /// Select entries from one axis using host-known indices.
470    ///
471    /// # Examples
472    ///
473    /// ```rust
474    /// use tenferro_runtime::{TracedTensor};
475    ///
476    /// use tenferro_runtime::TraceContext;
477    /// let trace = TraceContext::new();
478    /// let x = trace.from_vec_col_major(vec![3], vec![10.0_f64, 20.0, 30.0]).unwrap();
479    /// let y = x.take_axis(0, &[2, 0]).unwrap();
480    /// assert_eq!(y.try_concrete_shape(), Some(vec![2]));
481    /// ```
482    ///
483    /// # Errors
484    ///
485    /// Returns [`Error::Validation`] with `AxisOutOfBounds` when `axis` is
486    /// outside the concrete rank, or `InvalidArgument` when an index list
487    /// cannot be applied to the selected axis.
488    pub fn take_axis(&self, axis: isize, indices: &[usize]) -> Result<Self> {
489        self.index_select(axis, indices)
490    }
491
492    /// Select entries from one axis using host-known positions.
493    ///
494    /// # Examples
495    ///
496    /// ```
497    /// use tenferro_cpu::CpuEngine;
498    /// use tenferro_runtime::{ExecutionEngine, GraphCompiler, Runtime, Tensor, TracedTensor};
499    ///
500    /// use tenferro_runtime::TraceContext;
501    /// let trace = TraceContext::new();
502    /// let x = trace.from_tensor_concrete_shape(
503    ///     Tensor::from_vec_col_major(vec![3], vec![10.0_f64, 20.0, 30.0]).unwrap(),
504    /// )
505    /// .unwrap();
506    /// let y = x.index_select(-1, &[2, 0]).unwrap();
507    /// let mut compiler = GraphCompiler::new();
508    /// let program = compiler.compile(&y).unwrap();
509    /// let mut engine = CpuEngine::serial();
510    /// let mut builder = Runtime::builder();
511    /// builder
512    ///     .register_engine(tenferro_cpu::runtime_engine_registration(&engine).unwrap())
513    ///     .unwrap();
514    /// let runtime = builder.build().unwrap();
515    /// let prepared = runtime
516    ///     .prepare(&program, &[], &[engine.target()])
517    ///     .unwrap();
518    /// let mut outputs = runtime
519    ///     .run(&mut [&mut engine], &prepared, &[])
520    ///     .unwrap();
521    /// let out = outputs.remove(0).into_tensor(&mut engine).unwrap();
522    ///
523    /// assert_eq!(
524    ///     out.as_slice::<f64>().unwrap(),
525    ///     &[30.0, 10.0],
526    /// );
527    /// ```
528    ///
529    /// # Errors
530    ///
531    /// Returns [`Error::Validation`] with `InvalidArgument` when the tensor
532    /// shape is not concrete, `AxisOutOfBounds` when `axis` is outside its
533    /// rank, or `InvalidArgument` when a position is outside the selected
534    /// axis extent.
535    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    /// Stack tensors along a newly inserted axis.
559    ///
560    /// # Examples
561    ///
562    /// ```
563    /// use tenferro_cpu::CpuEngine;
564    /// use tenferro_runtime::{ExecutionEngine, GraphCompiler, Runtime, Tensor, TracedTensor};
565    ///
566    /// use tenferro_runtime::TraceContext;
567    /// let trace = TraceContext::new();
568    /// let a = trace.from_tensor_concrete_shape(Tensor::from_vec_col_major(vec![], vec![1.0_f64]).unwrap()).unwrap();
569    /// let b = trace.from_tensor_concrete_shape(Tensor::from_vec_col_major(vec![], vec![2.0_f64]).unwrap()).unwrap();
570    /// let stacked = TracedTensor::stack(&[&a, &b], -1).unwrap();
571    /// let mut compiler = GraphCompiler::new();
572    /// let program = compiler.compile(&stacked).unwrap();
573    /// let mut engine = CpuEngine::serial();
574    /// let mut builder = Runtime::builder();
575    /// builder
576    ///     .register_engine(tenferro_cpu::runtime_engine_registration(&engine).unwrap())
577    ///     .unwrap();
578    /// let runtime = builder.build().unwrap();
579    /// let prepared = runtime
580    ///     .prepare(&program, &[], &[engine.target()])
581    ///     .unwrap();
582    /// let mut outputs = runtime
583    ///     .run(&mut [&mut engine], &prepared, &[])
584    ///     .unwrap();
585    /// let out = outputs.remove(0).into_tensor(&mut engine).unwrap();
586    ///
587    /// assert_eq!(
588    ///     out.as_slice::<f64>().unwrap(),
589    ///     &[1.0, 2.0],
590    /// );
591    /// ```
592    ///
593    /// # Errors
594    ///
595    /// Returns [`Error::Validation`] with `InvalidArgument` for an empty input
596    /// list, `ShapeMismatch` for incompatible input shapes, or
597    /// `AxisOutOfBounds` when `dim` is outside the output rank.
598    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    /// Concatenate tensors along one existing axis.
655    ///
656    /// # Errors
657    ///
658    /// Returns [`Error::Validation`] with `InvalidArgument` for an empty input
659    /// list, `RankMismatch`/`ShapeMismatch` for incompatible input shapes, or
660    /// `AxisOutOfBounds` when `axis` is outside the input rank.
661    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;