pub trait TensorReduction {
// Required methods
fn reduce_sum_read(
&mut self,
input: TensorRead<'_>,
axes: &[usize],
) -> Result<Tensor>;
fn reduce_prod_read(
&mut self,
input: TensorRead<'_>,
axes: &[usize],
) -> Result<Tensor>;
fn reduce_max_read(
&mut self,
input: TensorRead<'_>,
axes: &[usize],
) -> Result<Tensor>;
fn reduce_min_read(
&mut self,
input: TensorRead<'_>,
axes: &[usize],
) -> Result<Tensor>;
}Expand description
Reduction operations.
Reducing over an axis whose extent is zero returns an error for every
reduction operation. Passing an empty axes slice is a no-op for the public
reductions and returns the input values unchanged. Internal mapped
reductions document their own empty-axis semantics.
§Examples
use tenferro_tensor::TensorReduction;
fn accepts_reduction<B: TensorReduction>(_backend: &mut B) {}Required Methods§
Sourcefn reduce_sum_read(
&mut self,
input: TensorRead<'_>,
axes: &[usize],
) -> Result<Tensor>
fn reduce_sum_read( &mut self, input: TensorRead<'_>, axes: &[usize], ) -> Result<Tensor>
Sum elements across axes from an owned tensor or borrowed view.
§Examples
use tenferro_tensor::{Tensor, TensorRead, TensorReduction};
fn sum_owned<B: TensorReduction>(
backend: &mut B,
input: &Tensor,
) -> tenferro_tensor::Result<Tensor> {
backend.reduce_sum_read(TensorRead::from_tensor(input), &[0])
}§Errors
Returns crate::Error::Validation with a typed ValidationError source
for invalid shapes, ranks, axes, dtypes, or output metadata. It returns
crate::Error::BackendFailure or crate::Error::BackendSource when
backend execution or storage access cannot provide the requested result.
Sourcefn reduce_prod_read(
&mut self,
input: TensorRead<'_>,
axes: &[usize],
) -> Result<Tensor>
fn reduce_prod_read( &mut self, input: TensorRead<'_>, axes: &[usize], ) -> Result<Tensor>
Multiply elements across axes from an owned tensor or borrowed view.
§Overflow policy
Backends may reassociate a floating-point or complex product across
lanes, chunks and threads, and the CPU and CUDA kernels do so
differently. Rounding and range behaviour therefore depend on the
backend, the layout and the thread count: a partial product can overflow
to inf or underflow to 0, and combining the partials can then give
inf, 0 or NaN even where the sequential product is representable.
The #2150 alternating 2.0, 0.5 axis is one example: an f32
sequential product is exactly 1.0, while a reassociated one may return
NaN. This is the same trade-off torch.prod makes. Integer products
are exact and wrap on overflow.
§Examples
use tenferro_tensor::{Tensor, TensorRead, TensorReduction};
fn prod_owned<B: TensorReduction>(
backend: &mut B,
input: &Tensor,
) -> tenferro_tensor::Result<Tensor> {
backend.reduce_prod_read(TensorRead::from_tensor(input), &[0])
}§Errors
Returns crate::Error::Validation with a typed ValidationError source
for invalid shapes, ranks, axes, dtypes, or output metadata. It returns
crate::Error::BackendFailure or crate::Error::BackendSource when
backend execution or storage access cannot provide the requested result.
Sourcefn reduce_max_read(
&mut self,
input: TensorRead<'_>,
axes: &[usize],
) -> Result<Tensor>
fn reduce_max_read( &mut self, input: TensorRead<'_>, axes: &[usize], ) -> Result<Tensor>
Take maximum values across axes from an owned tensor or borrowed view.
§Examples
use tenferro_tensor::{Tensor, TensorRead, TensorReduction};
fn max_owned<B: TensorReduction>(
backend: &mut B,
input: &Tensor,
) -> tenferro_tensor::Result<Tensor> {
backend.reduce_max_read(TensorRead::from_tensor(input), &[0])
}§Errors
Returns crate::Error::Validation with a typed ValidationError source
for invalid shapes, ranks, axes, dtypes, or output metadata. It returns
crate::Error::BackendFailure or crate::Error::BackendSource when
backend execution or storage access cannot provide the requested result.
Sourcefn reduce_min_read(
&mut self,
input: TensorRead<'_>,
axes: &[usize],
) -> Result<Tensor>
fn reduce_min_read( &mut self, input: TensorRead<'_>, axes: &[usize], ) -> Result<Tensor>
Take minimum values across axes from an owned tensor or borrowed view.
§Examples
use tenferro_tensor::{Tensor, TensorRead, TensorReduction};
fn min_owned<B: TensorReduction>(
backend: &mut B,
input: &Tensor,
) -> tenferro_tensor::Result<Tensor> {
backend.reduce_min_read(TensorRead::from_tensor(input), &[0])
}§Errors
Returns crate::Error::Validation with a typed ValidationError source
for invalid shapes, ranks, axes, dtypes, or output metadata. It returns
crate::Error::BackendFailure or crate::Error::BackendSource when
backend execution or storage access cannot provide the requested result.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".