Skip to main content

rusttorch/
tensor.rs

1//! Validated tensor workflows built from native LibTorch operations.
2//!
3//! Helpers allocate outputs and preserve native automatic differentiation; none
4//! mutate an input. [`flatten_features`] is the explicit exception to independent
5//! storage: its `copy` argument selects a view or an independent allocation.
6//! COO coordinate construction and quantization are boundaries without gradients. For individual
7//! operations, use the existing [`Tensor`] `f_*` methods directly. See the
8//! [complete recipes](https://github.com/newpoluton-alt/RustTorch/blob/main/docs/tensor-workflows.md)
9//! for linear algebra, broadcasting, complex FFTs, special functions and CSR.
10
11use crate::{Device, Kind, Result, RustTorchError, Tensor};
12
13fn invalid(field: &'static str, reason: impl Into<String>) -> RustTorchError {
14    RustTorchError::InvalidConfiguration {
15        field,
16        reason: reason.into(),
17    }
18}
19
20fn shape(value: &Tensor, field: &'static str) -> Result<Vec<i64>> {
21    if !value.defined() {
22        return Err(invalid(field, "tensor must be defined"));
23    }
24    // Unlike size(), this catches unsupported nested-tensor metadata as an error.
25    Ok(Vec::<i64>::try_from(value.f_internal_shape_as_tensor()?)?)
26}
27
28fn same_device(left: &Tensor, right: &Tensor, field: &'static str) -> Result<()> {
29    if left.device() != right.device() {
30        return Err(RustTorchError::DeviceMismatch {
31            context: field.into(),
32            expected: left.device(),
33            actual: right.device(),
34        });
35    }
36    Ok(())
37}
38
39fn same_kind(left: &Tensor, right: &Tensor, field: &'static str) -> Result<()> {
40    if left.f_kind()? != right.f_kind()? {
41        return Err(RustTorchError::DtypeMismatch {
42            name: field.into(),
43            expected: left.f_kind()?,
44            actual: right.f_kind()?,
45        });
46    }
47    Ok(())
48}
49
50fn real(value: &Tensor, field: &'static str) -> Result<()> {
51    if !matches!(value.f_kind()?, Kind::Float | Kind::Double) {
52        return Err(invalid(field, "expected float32 or float64"));
53    }
54    Ok(())
55}
56
57fn indices(value: &Tensor, count: i64, field: &'static str) -> Result<Vec<i64>> {
58    let size = shape(value, field)?;
59    if value.f_kind()? != Kind::Int64 {
60        return Err(invalid(field, "indices must have int64 dtype"));
61    }
62    if !size.contains(&0)
63        && (value.f_min()?.int64_value(&[]) < 0 || value.f_max()?.int64_value(&[]) >= count)
64    {
65        return Err(invalid(field, format!("indices must be in [0, {count})")));
66    }
67    Ok(size)
68}
69
70/// Select samples from `[samples, ...]` in the order of a one-dimensional ID tensor.
71///
72/// Repeated IDs are allowed and their gradients accumulate. IDs must be int64,
73/// in bounds, and on the input device; negative Python-style IDs are rejected.
74/// The result owns new storage; changing it does not change `input`.
75///
76/// ```
77/// use rusttorch::{Result, Tensor, tensor::select_rows};
78/// # fn main() -> Result<()> {
79/// let samples = Tensor::f_from_slice(&[10_f32, 20., 30.])?;
80/// let selected = select_rows(&samples, &Tensor::f_from_slice(&[2_i64, 0, 2])?)?;
81/// assert_eq!(Vec::<f32>::try_from(selected)?, [30., 10., 30.]);
82/// # Ok(()) }
83/// ```
84pub fn select_rows(input: &Tensor, ids: &Tensor) -> Result<Tensor> {
85    let size = shape(input, "select_rows.input")?;
86    if size.is_empty() {
87        return Err(invalid("select_rows.input", "expected at least one axis"));
88    }
89    let id_size = indices(ids, size[0], "select_rows.ids")?;
90    same_device(input, ids, "select_rows.ids")?;
91    if id_size.len() != 1 {
92        return Err(invalid("select_rows.ids", "expected one-dimensional IDs"));
93    }
94    Ok(input.f_index_select(0, ids)?)
95}
96
97/// Select one class score per row of a `[batch, classes]` tensor.
98///
99/// `classes` has shape `[batch]`, int64 dtype, and values in `[0, classes)`.
100/// This is useful for action values or the log-probability of observed labels.
101/// Gradients flow only to selected scores. No broadcasting or mutation occurs.
102///
103/// ```
104/// use rusttorch::{Result, Tensor, tensor::gather_rows};
105/// # fn main() -> Result<()> {
106/// let scores = Tensor::f_from_slice(&[1_f32, 2., 3., 4.])?.f_reshape([2, 2])?;
107/// let chosen = gather_rows(&scores, &Tensor::f_from_slice(&[1_i64, 0])?)?;
108/// assert_eq!(Vec::<f32>::try_from(chosen)?, [2., 3.]);
109/// # Ok(()) }
110/// ```
111pub fn gather_rows(input: &Tensor, classes: &Tensor) -> Result<Tensor> {
112    let size = shape(input, "gather_rows.input")?;
113    if size.len() != 2 {
114        return Err(invalid("gather_rows.input", "expected [batch, classes]"));
115    }
116    let id_size = indices(classes, size[1], "gather_rows.classes")?;
117    same_device(input, classes, "gather_rows.classes")?;
118    if id_size != [size[0]] {
119        return Err(invalid("gather_rows.classes", "expected one ID per row"));
120    }
121    Ok(input
122        .f_gather(1, &classes.f_unsqueeze(1)?, false)?
123        .f_squeeze_dim(1)?)
124}
125
126/// Sum `[items, ...]` contributions into `rows` output groups.
127///
128/// IDs have shape `[items]` and int64 dtype. Repeated IDs are added; missing
129/// groups are zero. This out-of-place scatter is useful for graph-neighbor
130/// aggregation, and preserves gradients to each contribution. Accelerator
131/// reductions can have the backend's usual nondeterminism.
132///
133/// ```
134/// use rusttorch::{Result, Tensor, tensor::scatter_add_rows};
135/// # fn main() -> Result<()> {
136/// let messages = Tensor::f_from_slice(&[2_f32, 3., 4.])?;
137/// let totals = scatter_add_rows(3, &Tensor::f_from_slice(&[0_i64, 0, 2])?, &messages)?;
138/// assert_eq!(Vec::<f32>::try_from(totals)?, [5., 0., 4.]);
139/// # Ok(()) }
140/// ```
141pub fn scatter_add_rows(rows: i64, ids: &Tensor, values: &Tensor) -> Result<Tensor> {
142    let mut size = shape(values, "scatter_add_rows.values")?;
143    if rows < 0 || size.is_empty() {
144        return Err(invalid(
145            "scatter_add_rows",
146            "rows must be nonnegative and values must have an item axis",
147        ));
148    }
149    let id_size = indices(ids, rows, "scatter_add_rows.ids")?;
150    same_device(values, ids, "scatter_add_rows.ids")?;
151    if id_size != [size[0]] {
152        return Err(invalid("scatter_add_rows.ids", "expected one ID per item"));
153    }
154    size[0] = rows;
155    Ok(Tensor::f_zeros(&size, (values.f_kind()?, values.device()))?.f_index_add(0, ids, values)?)
156}
157
158/// Replace masked values without changing the original tensor.
159///
160/// All three inputs must have identical shapes and devices; `mask` must be
161/// Boolean, and replacements must match the input dtype. Requiring exact shapes
162/// avoids accidentally broadcasting a sample mask along the wrong axis.
163/// Gradients follow the selected input or replacement at each element.
164///
165/// ```
166/// use rusttorch::{Result, Tensor, tensor::replace_masked};
167/// # fn main() -> Result<()> {
168/// let x = Tensor::f_from_slice(&[-1_f32, 2.])?;
169/// let clean = replace_masked(&x, &x.f_lt(0.)?, &x.f_zeros_like()?)?;
170/// assert_eq!(Vec::<f32>::try_from(clean)?, [0., 2.]);
171/// # Ok(()) }
172/// ```
173pub fn replace_masked(input: &Tensor, mask: &Tensor, replacements: &Tensor) -> Result<Tensor> {
174    let size = shape(input, "replace_masked.input")?;
175    let mask_size = shape(mask, "replace_masked.mask")?;
176    let replacement_size = shape(replacements, "replace_masked.replacements")?;
177    same_device(input, mask, "replace_masked.mask")?;
178    same_device(input, replacements, "replace_masked.replacements")?;
179    same_kind(input, replacements, "replace_masked.replacements")?;
180    if size != mask_size || size != replacement_size || mask.f_kind()? != Kind::Bool {
181        return Err(invalid(
182            "replace_masked",
183            "expected equal shapes and a Boolean mask",
184        ));
185    }
186    Ok(replacements.f_where_self(mask, input)?)
187}
188
189/// Flatten `[batch, ...features]` into `[batch, features]` with explicit storage.
190///
191/// `copy = false` requires a view-compatible layout and shares storage; a
192/// noncontiguous layout that cannot be viewed returns an error. `copy = true`
193/// always allocates independent storage, including when the input is contiguous.
194/// Both choices preserve gradients. Empty batches and zero-size feature axes
195/// are supported because the feature count is computed without `-1` inference.
196///
197/// ```
198/// use rusttorch::{Device, Kind, Result, Tensor, tensor::flatten_features};
199/// # fn main() -> Result<()> {
200/// let images = Tensor::f_zeros([2, 3, 4, 4], (Kind::Float, Device::Cpu))?;
201/// assert_eq!(flatten_features(&images, false)?.size(), [2, 48]);
202/// # Ok(()) }
203/// ```
204pub fn flatten_features(input: &Tensor, copy: bool) -> Result<Tensor> {
205    let size = shape(input, "flatten_features.input")?;
206    if size.len() < 2 {
207        return Err(invalid(
208            "flatten_features.input",
209            "expected [batch, ...features]",
210        ));
211    }
212    let features = size[1..]
213        .iter()
214        .try_fold(1_i64, |n, axis| n.checked_mul(*axis))
215        .ok_or_else(|| invalid("flatten_features.input", "feature count overflows int64"))?;
216    let target = [size[0], features];
217    if copy {
218        Ok(input.f_view_copy(target)?)
219    } else {
220        Ok(input.f_view(target)?)
221    }
222}
223
224/// Standardize float32/float64 values along one axis using population variance.
225///
226/// Computes `(x - mean) / max(std, epsilon)` with retained reduction dimensions
227/// for broadcasting. Constant features become zero; `epsilon` must be finite
228/// and positive after conversion to the input dtype. The chosen axis must exist
229/// and contain at least one value.
230/// Negative axes are accepted, and gradients remain connected to the input.
231///
232/// ```
233/// use rusttorch::{Result, Tensor, tensor::standardize};
234/// # fn main() -> Result<()> {
235/// let x = Tensor::f_from_slice(&[1_f64, 3.])?;
236/// assert_eq!(Vec::<f64>::try_from(standardize(&x, 0, 1e-8)?)?, [-1., 1.]);
237/// # Ok(()) }
238/// ```
239pub fn standardize(input: &Tensor, dim: i64, epsilon: f64) -> Result<Tensor> {
240    let size = shape(input, "standardize.input")?;
241    real(input, "standardize.input")?;
242    let rank = size.len() as i64;
243    let effective_epsilon = if input.f_kind()? == Kind::Float {
244        f64::from(epsilon as f32)
245    } else {
246        epsilon
247    };
248    if !effective_epsilon.is_finite() || effective_epsilon <= 0. || dim < -rank || dim >= rank {
249        return Err(invalid(
250            "standardize",
251            "expected positive finite epsilon representable in the input dtype and a valid axis",
252        ));
253    }
254    let dim = if dim < 0 { dim + rank } else { dim };
255    if size[dim as usize] == 0 {
256        return Err(invalid(
257            "standardize.input",
258            "reduction axis must not be empty",
259        ));
260    }
261    let mean = input.f_mean_dim([dim].as_slice(), true, None)?;
262    let std = input
263        .f_std_dim([dim].as_slice(), false, true)?
264        .f_clamp_min(epsilon)?;
265    Ok(input.f_sub(&mean)?.f_div(&std)?)
266}
267
268/// Fit diagnostics from [`least_squares`], matching the native `gelsd` outputs.
269///
270/// See [`least_squares`] for a complete fitting example. Empty residual tensors
271/// are meaningful: rank-deficient and non-overdetermined systems have none.
272#[derive(Debug)]
273pub struct LeastSquares {
274    /// Coefficients of shape `[features, targets]`.
275    pub solution: Tensor,
276    /// Squared residuals per target, or an empty tensor when unavailable.
277    pub residuals: Tensor,
278    /// Scalar numerical rank under the chosen singular-value cutoff.
279    pub rank: Tensor,
280    /// Singular values of the design matrix in decreasing order.
281    pub singular_values: Tensor,
282}
283
284/// Fit a possibly rank-deficient CPU system using the SVD-based `gelsd` driver.
285///
286/// `design` is `[observations, features]`, `targets` is `[observations, targets]`;
287/// both must be float32/float64, finite, and on CPU with matching dtype. `rcond`
288/// is an optional finite nonnegative rank cutoff; `None` uses the native default.
289/// Outputs retain native autograd. For batched/complex systems or GPU drivers,
290/// call [`Tensor::f_linalg_lstsq`] directly and choose the backend driver.
291///
292/// ```
293/// use rusttorch::{Result, Tensor, tensor::least_squares};
294/// # fn main() -> Result<()> {
295/// let design = Tensor::f_from_slice(&[1_f64, 0., 1., 1., 1., 2.])?.f_reshape([3, 2])?;
296/// let targets = Tensor::f_from_slice(&[1_f64, 3., 5.])?.f_reshape([3, 1])?;
297/// let fit = least_squares(&design, &targets, None)?;
298/// assert!((fit.solution.double_value(&[1, 0]) - 2.).abs() < 1e-10);
299/// # Ok(()) }
300/// ```
301pub fn least_squares(
302    design: &Tensor,
303    targets: &Tensor,
304    rcond: Option<f64>,
305) -> Result<LeastSquares> {
306    let a = shape(design, "least_squares.design")?;
307    let b = shape(targets, "least_squares.targets")?;
308    real(design, "least_squares.design")?;
309    same_kind(design, targets, "least_squares.targets")?;
310    same_device(design, targets, "least_squares.targets")?;
311    if a.len() != 2 || b.len() != 2 || a[0] != b[0] || a.contains(&0) || b.contains(&0) {
312        return Err(invalid(
313            "least_squares",
314            "expected nonempty [observations, features] and [observations, targets]",
315        ));
316    }
317    if design.device() != Device::Cpu || rcond.is_some_and(|r| !r.is_finite() || r < 0.) {
318        return Err(invalid(
319            "least_squares",
320            "expected CPU inputs and a finite nonnegative rcond",
321        ));
322    }
323    if design.f_isfinite()?.f_all()?.int64_value(&[]) == 0
324        || targets.f_isfinite()?.f_all()?.int64_value(&[]) == 0
325    {
326        return Err(invalid("least_squares", "inputs must be finite"));
327    }
328    let (solution, residuals, rank, singular_values) =
329        design.f_linalg_lstsq(targets, rcond, "gelsd")?;
330    Ok(LeastSquares {
331        solution,
332        residuals,
333        rank,
334        singular_values,
335    })
336}
337
338/// Filter real signals along their last axis with real frequency-bin gains.
339///
340/// Input shape is `[..., samples]`; gain shape is exactly `[samples / 2 + 1]`.
341/// Both tensors use the same float32/float64 dtype and device. A gain of one
342/// reconstructs the input, including odd sample counts; zero removes a bin.
343/// Uses the native `backward` FFT normalization and preserves gradients to both
344/// input and gains. This is a periodic spectral filter, not padded convolution.
345///
346/// ```
347/// use rusttorch::{Device, Kind, Result, Tensor, tensor::spectral_filter};
348/// # fn main() -> Result<()> {
349/// let x = Tensor::f_from_slice(&[1_f64, 2., 3., 4., 5.])?;
350/// let gains = Tensor::f_ones([3], (Kind::Double, Device::Cpu))?;
351/// assert!(spectral_filter(&x, &gains)?.allclose(&x, 1e-10, 1e-10, false));
352/// # Ok(()) }
353/// ```
354pub fn spectral_filter(input: &Tensor, gains: &Tensor) -> Result<Tensor> {
355    let size = shape(input, "spectral_filter.input")?;
356    let gain_size = shape(gains, "spectral_filter.gains")?;
357    real(input, "spectral_filter.input")?;
358    same_kind(input, gains, "spectral_filter.gains")?;
359    same_device(input, gains, "spectral_filter.gains")?;
360    let n = *size
361        .last()
362        .ok_or_else(|| invalid("spectral_filter.input", "expected a sample axis"))?;
363    if n == 0 || gain_size != [n / 2 + 1] {
364        return Err(invalid(
365            "spectral_filter",
366            "expected nonempty signals and [samples / 2 + 1] gains",
367        ));
368    }
369    Ok(input
370        .f_fft_rfft(None, -1, "backward")?
371        .f_mul(gains)?
372        .f_fft_irfft(n, -1, "backward")?)
373}
374
375/// Construct a coalesced sparse COO matrix from checked row/column coordinates.
376///
377/// Coordinates have shape `[2, nonzeros]` and int64 dtype; values have shape
378/// `[nonzeros]` with float32/float64 dtype on the same device. Duplicate entries
379/// are summed, coordinates must be in bounds, and dimensions may be zero.
380/// Explicit uncoalesced construction followed by coalescing gives independent
381/// coordinates and values, so later input mutation cannot invalidate the matrix.
382/// The native C++ factory disconnects value gradients, so values requiring
383/// gradients are rejected. For differentiable sparse construction, convert a
384/// dense tensor with [`Tensor::f_to_sparse_sparse_dim`] instead.
385///
386/// ```
387/// use rusttorch::{Result, Tensor, tensor::sparse_coo_matrix};
388/// # fn main() -> Result<()> {
389/// let ids = Tensor::f_from_slice(&[0_i64, 1, 0, 1])?.f_reshape([2, 2])?;
390/// let matrix = sparse_coo_matrix(&ids, &Tensor::f_from_slice(&[2_f32, 3.])?, [2, 2])?;
391/// assert!(matrix.is_sparse());
392/// # Ok(()) }
393/// ```
394pub fn sparse_coo_matrix(coordinates: &Tensor, values: &Tensor, size: [i64; 2]) -> Result<Tensor> {
395    let ids = shape(coordinates, "sparse_coo_matrix.coordinates")?;
396    let data = shape(values, "sparse_coo_matrix.values")?;
397    real(values, "sparse_coo_matrix.values")?;
398    if values.requires_grad() {
399        return Err(invalid(
400            "sparse_coo_matrix.values",
401            "native coordinate construction disconnects gradients; detach explicitly or convert a dense tensor to COO",
402        ));
403    }
404    same_device(values, coordinates, "sparse_coo_matrix.coordinates")?;
405    if size.iter().any(|n| *n < 0)
406        || data.len() != 1
407        || ids != [2, data[0]]
408        || coordinates.f_kind()? != Kind::Int64
409    {
410        return Err(invalid(
411            "sparse_coo_matrix",
412            "expected nonnegative dimensions, int64 [2, nnz] coordinates and [nnz] values",
413        ));
414    }
415    indices(
416        &coordinates.f_select(0, 0)?,
417        size[0],
418        "sparse_coo_matrix.rows",
419    )?;
420    indices(
421        &coordinates.f_select(0, 1)?,
422        size[1],
423        "sparse_coo_matrix.columns",
424    )?;
425    Ok(Tensor::f_sparse_coo_tensor_indices_size(
426        coordinates,
427        values,
428        size,
429        (values.f_kind()?, values.device()),
430        false,
431    )?
432    .f_coalesce()?)
433}
434
435/// Multiply a two-dimensional sparse COO/CSR matrix by dense feature columns.
436///
437/// Input shapes are `[rows, columns]` and `[columns, features]`, with matching
438/// float32/float64 dtype and device. Layout errors are returned by LibTorch.
439/// COO converted from dense and dense-input gradients are covered by CPU tests; CSR forward
440/// conversion/multiplication is covered without claiming full sparse autograd.
441///
442/// ```
443/// use rusttorch::{Device, Kind, Result, Tensor, tensor::{sparse_coo_matrix, sparse_matmul}};
444/// # fn main() -> Result<()> {
445/// let ids = Tensor::f_from_slice(&[0_i64, 0])?.f_reshape([2, 1])?;
446/// let matrix = sparse_coo_matrix(&ids, &Tensor::f_from_slice(&[2_f32])?, [1, 1])?;
447/// let y = sparse_matmul(&matrix, &Tensor::f_ones([1, 2], (Kind::Float, Device::Cpu))?)?;
448/// assert_eq!(y.size(), [1, 2]);
449/// # Ok(()) }
450/// ```
451pub fn sparse_matmul(matrix: &Tensor, dense: &Tensor) -> Result<Tensor> {
452    let a = shape(matrix, "sparse_matmul.matrix")?;
453    let b = shape(dense, "sparse_matmul.dense")?;
454    real(matrix, "sparse_matmul.matrix")?;
455    same_kind(matrix, dense, "sparse_matmul.dense")?;
456    same_device(matrix, dense, "sparse_matmul.dense")?;
457    if a.len() != 2 || b.len() != 2 || a[1] != b[0] {
458        return Err(invalid(
459            "sparse_matmul",
460            "expected [rows, columns] and [columns, features]",
461        ));
462    }
463    if matrix.f_sparse_dim()? != 2 || dense.f_sparse_dim()? != 0 {
464        return Err(invalid(
465            "sparse_matmul",
466            "expected a COO/CSR matrix and dense features",
467        ));
468    }
469    if matrix.is_sparse() {
470        let coordinates = matrix.f_internal_indices()?;
471        let values = matrix.f_internal_values()?;
472        let coordinate_size = shape(&coordinates, "sparse_matmul.coordinates")?;
473        let value_size = shape(&values, "sparse_matmul.values")?;
474        if value_size.len() != 1 || coordinate_size != [2, value_size[0]] {
475            return Err(invalid(
476                "sparse_matmul.matrix",
477                "invalid COO coordinate/value shapes",
478            ));
479        }
480        indices(&coordinates.f_select(0, 0)?, a[0], "sparse_matmul.rows")?;
481        indices(&coordinates.f_select(0, 1)?, a[1], "sparse_matmul.columns")?;
482    } else {
483        Tensor::f_internal_validate_sparse_csr_tensor_args(
484            &matrix.f_crow_indices()?,
485            &matrix.f_col_indices()?,
486            &matrix.f_values()?,
487            &a,
488            false,
489        )?;
490    }
491    Ok(Tensor::f_internal_sparse_mm(matrix, dense)?)
492}
493
494/// Quantize finite CPU float32 data with explicit per-tensor affine parameters.
495///
496/// The output kind must be `QInt8` or `QUInt8`; scale must remain finite and
497/// positive after float32 conversion (the dequantized output dtype),
498/// and zero point must fit the selected integer range. Values saturate to that
499/// range and use native rounding. Inputs requiring gradients are
500/// rejected: explicitly detach before crossing this inference-only boundary.
501/// This does not convert models or provide quantized training/operators. These
502/// legacy quantized tensor constructors are deprecated by pinned PyTorch 2.13.
503///
504/// ```
505/// use rusttorch::{Kind, Result, Tensor, tensor::quantize_per_tensor};
506/// # fn main() -> Result<()> {
507/// let x = Tensor::f_from_slice(&[-1_f32, 0., 1.])?;
508/// let packed = quantize_per_tensor(&x, 0.1, 0, Kind::QInt8)?;
509/// assert!(packed.f_dequantize()?.allclose(&x, 1e-6, 1e-6, false));
510/// # Ok(()) }
511/// ```
512pub fn quantize_per_tensor(
513    input: &Tensor,
514    scale: f64,
515    zero_point: i64,
516    kind: Kind,
517) -> Result<Tensor> {
518    shape(input, "quantize_per_tensor.input")?;
519    let bounds = match kind {
520        Kind::QInt8 => -128..=127,
521        Kind::QUInt8 => 0..=255,
522        _ => {
523            return Err(invalid(
524                "quantize_per_tensor.kind",
525                "expected QInt8 or QUInt8",
526            ));
527        }
528    };
529    if input.device() != Device::Cpu || input.f_kind()? != Kind::Float || input.requires_grad() {
530        return Err(invalid(
531            "quantize_per_tensor.input",
532            "expected detached CPU float32 data",
533        ));
534    }
535    let effective_scale = scale as f32;
536    if !effective_scale.is_finite() || effective_scale <= 0. || !bounds.contains(&zero_point) {
537        return Err(invalid(
538            "quantize_per_tensor",
539            "expected scale representable as positive finite float32 and a representable zero point",
540        ));
541    }
542    if input.f_isfinite()?.f_all()?.int64_value(&[]) == 0 {
543        return Err(invalid(
544            "quantize_per_tensor.input",
545            "values must be finite",
546        ));
547    }
548    Ok(input.f_quantize_per_tensor(scale, zero_point, kind)?)
549}