pub trait BlendOps<Scalar: Clone + EachBlend, const MAX_RANK: usize>: TensorRef<Scalar, MAX_RANK>
where
Scalar::Scalar: From<f32> + Copy, Elementwise kernels live on tensors and views. They are not a promise that every arbitrary strided view gets the same SIMD path on every backend.
use numkong::Tensor;
let a = Tensor::<f32>::from_slice(&[1.0, 2.0, 3.0, 4.0], &[2, 2]).unwrap();
let b = Tensor::<f32>::full(&[2, 2], 2.0).unwrap();
let blended = a.view().blend_tensor(&b.view(), 0.25, 0.75).unwrap();
let sines = blended.sin().unwrap();
assert_eq!(sines.shape(), &[2, 2]);The _inplace methods update a tensor in its own storage and report a kernel failure:
use numkong::Tensor;
let mut t = Tensor::<f32>::full(&[4], 1.0).unwrap();
t.add_scalar_inplace(10.0).unwrap();
t.sub_scalar_inplace(0.5).unwrap();
t.mul_scalar_inplace(2.0).unwrap();§ Trigonometry
The trigonometric kernels share the tensor and view surface. They are useful both directly and as a sanity check that the container path is not just about matrix kernels.
use numkong::Tensor;
let a = Tensor::<f32>::from_slice(&[0.0, 1.0, 2.0, 3.0], &[2, 2]).unwrap();
let c = a.cos().unwrap();
let s = a.sin().unwrap();
assert_eq!(c.shape(), &[2, 2]);
assert_eq!(s.shape(), &[2, 2]);§ Moments Reductions
Moments reductions return both sum and sum-of-squares. That is the right building block for norms and variance-like workflows.
use numkong::{ReduceMoments, Tensor};
let narrow = Tensor::<u8>::full(&[1024], 255).unwrap();
let (sum, sumsq) = narrow.moments_all().unwrap();
assert!(sum > 255); // a naive u8 accumulation would overflow immediately
assert!(sumsq > 255u64); // same for sum-of-squaresThe important documentation point is not just “wider outputs exist”. It is that the API makes the widened outputs part of the type story.
§ Min/Max Reductions
Min/max reductions return a MinMaxResult with both the value and its flat index:
use numkong::Tensor;
let t = Tensor::<f32>::from_slice(&[
3.0, 0.0, 7.0,
1.0, 2.0, 5.0,
4.0, -1.0, 6.0,
], &[3, 3]).unwrap();
let second_column = t.slice((.., 1_usize)).unwrap(); // t[:, 1]
let idx = second_column.argmin_all().unwrap();
assert_eq!(idx, 2);API
TraitBlendOpsExtension trait: element-wise subtraction for any TensorRef implementor.3
sub_tensorElement-wise difference of two tensors into a new tensor.
fn sub_tensor(
&self,
other: &impl TensorRef<Scalar, MAX_RANK> + ?Sized,
) -> Result<Tensor<Scalar, Self::Alloc, MAX_RANK>, Error>
where
Self::Alloc: Clone, blend_tensor_intoWrites α × aᵢ + β × otherᵢ into the output tensor.
fn blend_tensor_into<OtherTensor, OutputTensor>(
&self,
other: &OtherTensor,
alpha: Scalar::Scalar,
beta: Scalar::Scalar,
out: &mut OutputTensor,
) -> Result<(), Error>
where
OtherTensor: TensorRef<Scalar, MAX_RANK> + ?Sized,
OutputTensor: TensorMut<Scalar, MAX_RANK> + ?Sized, sub_tensor_intoElement-wise difference of two tensors into the output tensor.
fn sub_tensor_into<OtherTensor, OutputTensor>(
&self,
other: &OtherTensor,
out: &mut OutputTensor,
) -> Result<(), Error>
where
OtherTensor: TensorRef<Scalar, MAX_RANK> + ?Sized,
OutputTensor: TensorMut<Scalar, MAX_RANK> + ?Sized, TraitEachBlendBlends two vectors with an element-wise weighted sum.3
pub trait EachBlend: Sized + StorageElement rᵢ = α × aᵢ + β × bᵢ
Fails with Error::ShapeMismatch if lengths differ, or Error::KernelFailed
carrying the kernel's status.
Implemented for: f64, f32, f16, bf16, i8, u8, i16, u16, i32, u32, i64,
u64, e4m3, e5m2, e2m3, and e3m2.
Implementors
impl EachBlend for f64impl EachBlend for f32impl EachBlend for f16impl EachBlend for bf16impl EachBlend for i8impl EachBlend for u8impl EachBlend for i16impl EachBlend for u16impl EachBlend for i32impl EachBlend for u32impl EachBlend for i64impl EachBlend for u64impl EachBlend for e4m3impl EachBlend for e5m2impl EachBlend for e2m3impl EachBlend for e3m2impl EachBlend for f64cimpl EachBlend for f32cimpl EachBlend for f16cimpl EachBlend for bf16c
ScalarType of the blend coefficients.
type Scalar; each_blendWrites resultᵢ = α × aᵢ + β × bᵢ into the pre-sized output slice.
fn each_blend(
a: &[Self],
b: &[Self],
alpha: Self::Scalar,
beta: Self::Scalar,
result: &mut [Self],
) -> Result<(), Error> each_blend_inplaceIn-place blend: dataᵢ = α × dataᵢ + β × otherᵢ.
fn each_blend_inplace(
data: &mut [Self],
other: &[Self],
alpha: Self::Scalar,
beta: Self::Scalar,
) -> Result<(), Error> data is both the a operand and the result; its source and destination pointers come from
the single &mut, while other is disjoint storage — no aliased &[Self] + &mut [Self]
over the same buffer is formed.
TraitEachFmaApplies fused multiply-add element-wise across three vectors.3
pub trait EachFma: Sized + StorageElement rᵢ = α × aᵢ × bᵢ + β × cᵢ
Fails with Error::ShapeMismatch if lengths differ, or Error::KernelFailed
carrying the kernel's status.
Implemented for: f64, f32, f16, bf16, i8, u8, i16, u16, i32, u32, i64,
u64, e4m3, e5m2, e2m3, and e3m2.
Implementors
impl EachFma for f64impl EachFma for f32impl EachFma for f16impl EachFma for bf16impl EachFma for i8impl EachFma for u8impl EachFma for e4m3impl EachFma for e5m2impl EachFma for e2m3impl EachFma for e3m2impl EachFma for i16impl EachFma for u16impl EachFma for i32impl EachFma for u32impl EachFma for i64impl EachFma for u64impl EachFma for f64cimpl EachFma for f32cimpl EachFma for f16cimpl EachFma for bf16c
ScalarType of the fused multiply-add coefficients.
type Scalar; each_fmaWrites resultᵢ = α × aᵢ × bᵢ + β × cᵢ into the pre-sized output slice.
fn each_fma(
a: &[Self],
b: &[Self],
c: &[Self],
alpha: Self::Scalar,
beta: Self::Scalar,
result: &mut [Self],
) -> Result<(), Error> each_fma_inplaceIn-place fused multiply-add with the c operand bound to a: dataᵢ = α × dataᵢ × bᵢ + β × dataᵢ.
fn each_fma_inplace(
data: &mut [Self],
b: &[Self],
alpha: Self::Scalar,
beta: Self::Scalar,
) -> Result<(), Error> data is the a operand, the c operand, and the result — all derived from the single
&mut, while b is disjoint storage. This matches out-of-place mul_tensor, which wires
c = self and is the only in-place FMA caller. No aliased &[Self] + &mut [Self] over
the same buffer is formed.
TraitEachRmsCastGrouped RMSNorm of the values a dot product of Self writes back into Self, rounding like the cast into Self, so a GEMM output becomes the next GEMM's input in one pass.2
pub trait EachRmsCast: Sized + StorageElement Integer outputs
round to nearest even and saturate, with gamma carrying the quantization scale, and u8 suits
only non-negative activations.
Implementors
impl EachRmsCast for bf16impl EachRmsCast for f16impl EachRmsCast for e4m3impl EachRmsCast for e5m2impl EachRmsCast for e2m3impl EachRmsCast for e3m2impl EachRmsCast for f32impl EachRmsCast for i8impl EachRmsCast for u8
InputWhat dot products of Self write: f32 for the narrow floats, f64 for f32, and i32 or u32 for i8 or u8.
type Input: StorageElement; rmscast_intoGrouped RMSNorm of a 2D [rows,groups×columns] tensor of Self::Input into y, a tensor of Self of the same shape, with ε added to the mean square of the raw input.
fn rmscast_into<XIn, YOut, const RX: usize, const RY: usize>(
x: &XIn,
gamma: Option<&[f32]>,
y: &mut YOut,
groups: usize,
epsilon: f32,
) -> Result<(), Error>
where
XIn: TensorRef<Self::Input, RX> + ?Sized,
YOut: TensorMut<Self, RY> + ?Sized, Returns Err
on a shape mismatch.
TraitEachRmsNormGrouped RMSNorm over a row-major [rows,groups×columns] slice, y = x / √(mean(x²) + ε) × γ, where each row holds groups independent columns-vectors, normalized separately, with columns equal to x.len() / rows / groups.1
pub trait EachRmsNorm: Sized + StorageElement gamma is an optional per-column gain of length
columns, which defaults to unit scale when None.
Implementors
impl EachRmsNorm for f32impl EachRmsNorm for bf16impl EachRmsNorm for e4m3
rmsnorm_intoGrouped RMSNorm of a 2D [rows,groups×columns] tensor into y of the same shape.
fn rmsnorm_into<XIn, YOut, const RX: usize, const RY: usize>(
x: &XIn,
gamma: Option<&[f32]>,
y: &mut YOut,
groups: usize,
epsilon: f32,
) -> Result<(), Error>
where
XIn: TensorRef<Self, RX> + ?Sized,
YOut: TensorMut<Self, RY> + ?Sized, Row strides are read from the tensors, so x and y may be non-contiguous sub-spans, for
example a strided section of a fused activation buffer. gamma is an optional per-column
gain of length columns (None = unit scale). Returns Err on a shape mismatch.
TraitEachScaleApplies an element-wise affine transform — scale and shift.3
pub trait EachScale: Sized + StorageElement rᵢ = α × aᵢ + β
Fails with Error::ShapeMismatch if a and result lengths differ, or
Error::KernelFailed carrying the kernel's status.
Implemented for: f64, f32, f16, bf16, i8, u8, i16, u16, i32, u32, i64,
u64, e4m3, e5m2, e2m3, and e3m2.
Implementors
impl EachScale for f64impl EachScale for f32impl EachScale for f16impl EachScale for bf16impl EachScale for i8impl EachScale for u8impl EachScale for i16impl EachScale for u16impl EachScale for i32impl EachScale for u32impl EachScale for i64impl EachScale for u64impl EachScale for e4m3impl EachScale for e5m2impl EachScale for e2m3impl EachScale for e3m2impl EachScale for f64cimpl EachScale for f32cimpl EachScale for f16cimpl EachScale for bf16c
ScalarType of the scale and shift coefficients.
type Scalar; each_scaleWrites resultᵢ = α × aᵢ + β into the pre-sized output slice.
fn each_scale(
a: &[Self],
alpha: Self::Scalar,
beta: Self::Scalar,
result: &mut [Self],
) -> Result<(), Error> Both slices, a and result, must have identical length — the kernel does not allocate.
Examples
use numkong::EachScale;
let input = [1.0_f32, 2.0, 3.0];
let mut output = [0.0_f32; 3];
// Rescale to [-1, 1]: alpha = 2/(max-min) = 1.0, beta = -1 - 1*min = -2.0
f32::each_scale(&input, 1.0, -2.0, &mut output).unwrap();
assert_eq!(output, [-1.0, 0.0, 1.0]);
each_scale_inplaceIn-place affine: dataᵢ = α × dataᵢ + β.
fn each_scale_inplace(
data: &mut [Self],
alpha: Self::Scalar,
beta: Self::Scalar,
) -> Result<(), Error> Both source and destination pointers are derived from the single &mut, so no aliased
&[Self] + &mut [Self] over the same storage is formed.
TraitEachSumApplies element-wise addition of two vectors.2
pub trait EachSum: Sized + StorageElement rᵢ = aᵢ + bᵢ
Fails with Error::ShapeMismatch if lengths differ, or Error::KernelFailed
carrying the kernel's status.
Implemented for: f64, f32, f16, bf16, i8, u8, i16, u16, i32, u32, i64,
u64, e4m3, e5m2, e2m3, and e3m2.
Implementors
impl EachSum for f64impl EachSum for f32impl EachSum for f16impl EachSum for bf16impl EachSum for i8impl EachSum for u8impl EachSum for i16impl EachSum for u16impl EachSum for i32impl EachSum for u32impl EachSum for i64impl EachSum for u64impl EachSum for e4m3impl EachSum for e5m2impl EachSum for e2m3impl EachSum for e3m2impl EachSum for f64cimpl EachSum for f32cimpl EachSum for f16cimpl EachSum for bf16c
each_sumWrites resultᵢ = aᵢ + bᵢ into the pre-sized output slice.
fn each_sum(a: &[Self], b: &[Self], result: &mut [Self]) -> Result<(), Error> each_sum_inplaceIn-place sum: dataᵢ = dataᵢ + otherᵢ.
fn each_sum_inplace(data: &mut [Self], other: &[Self]) -> Result<(), Error> data is both the a operand and the result; its source and destination pointers come from
the single &mut, while other is disjoint storage — no aliased &[Self] + &mut [Self]
over the same buffer is formed.
TraitEachSwiGluFused SwiGLU of a row-major [rows,columns] slice, where up = None reduces it to plain SiLU over gate.len() / rows columns: y = silu(gate_scale × gate) × up × output_scale.1
pub trait EachSwiGlu: Sized + StorageElement Implementors
impl EachSwiGlu for f32impl EachSwiGlu for bf16impl EachSwiGlu for e4m3
swiglu_intoFused SwiGLU of 2D tensors: y = silu(gate_scale × gate) × up × output_scale.
fn swiglu_into<GIn, UIn, YOut, const RG: usize, const RU: usize, const RY: usize>(
gate: &GIn,
up: Option<&UIn>,
y: &mut YOut,
gate_scale: f32,
output_scale: f32,
) -> Result<(), Error>
where
GIn: TensorRef<Self, RG> + ?Sized,
UIn: TensorRef<Self, RU> + ?Sized,
YOut: TensorMut<Self, RY> + ?Sized, Strides are read from the tensors, so gate, up, and y may be independent strided
sub-spans, for example the two column halves of a [rows,2×columns] gate|up buffer, while
up = None reduces to plain SiLU. Returns Err on a shape mismatch.
TraitFmaOpsExtension trait: element-wise multiplication for any TensorRef implementor.3
pub trait FmaOps<Scalar: Clone + EachFma, const MAX_RANK: usize>: TensorRef<Scalar, MAX_RANK>
where
Scalar::Scalar: From<f32> + Copy, Implementors
mul_tensorElement-wise product of two tensors into a new tensor.
fn mul_tensor(
&self,
other: &impl TensorRef<Scalar, MAX_RANK> + ?Sized,
) -> Result<Tensor<Scalar, Self::Alloc, MAX_RANK>, Error>
where
Self::Alloc: Clone, fma_tensors_intoWrites α × aᵢ × bᵢ + β × cᵢ into the output tensor.
fn fma_tensors_into<BTensor, CTensor, OutputTensor>(
&self,
b: &BTensor,
c: &CTensor,
alpha: Scalar::Scalar,
beta: Scalar::Scalar,
out: &mut OutputTensor,
) -> Result<(), Error>
where
BTensor: TensorRef<Scalar, MAX_RANK> + ?Sized,
CTensor: TensorRef<Scalar, MAX_RANK> + ?Sized,
OutputTensor: TensorMut<Scalar, MAX_RANK> + ?Sized, mul_tensor_intoElement-wise product of two tensors into the output tensor.
fn mul_tensor_into<OtherTensor, OutputTensor>(
&self,
other: &OtherTensor,
out: &mut OutputTensor,
) -> Result<(), Error>
where
OtherTensor: TensorRef<Scalar, MAX_RANK> + ?Sized,
OutputTensor: TensorMut<Scalar, MAX_RANK> + ?Sized, TraitScaleOpsExtension trait: scalar arithmetic for any TensorRef implementor.7
pub trait ScaleOps<Scalar: Clone + EachScale, const MAX_RANK: usize>: TensorRef<Scalar, MAX_RANK>
where
Scalar::Scalar: From<f32> + Mul<Output = Scalar::Scalar> + Copy, Implementors
add_scalarElement-wise add scalar into a new tensor.
fn add_scalar(&self, scalar: Scalar::Scalar) -> Result<Tensor<Scalar, Self::Alloc, MAX_RANK>, Error>
where
Self::Alloc: Clone, sub_scalarElement-wise subtract scalar into a new tensor.
fn sub_scalar(&self, scalar: Scalar::Scalar) -> Result<Tensor<Scalar, Self::Alloc, MAX_RANK>, Error>
where
Self::Alloc: Clone, mul_scalarElement-wise multiply scalar into a new tensor.
fn mul_scalar(&self, scalar: Scalar::Scalar) -> Result<Tensor<Scalar, Self::Alloc, MAX_RANK>, Error>
where
Self::Alloc: Clone, scale_tensor_intoWrites α × aᵢ + β into the output tensor.
fn scale_tensor_into<OutputTensor: TensorMut<Scalar, MAX_RANK> + ?Sized>(
&self,
alpha: Scalar::Scalar,
beta: Scalar::Scalar,
out: &mut OutputTensor,
) -> Result<(), Error> add_scalar_intoElement-wise add scalar into the output tensor.
fn add_scalar_into<OutputTensor: TensorMut<Scalar, MAX_RANK> + ?Sized>(
&self,
scalar: Scalar::Scalar,
out: &mut OutputTensor,
) -> Result<(), Error> sub_scalar_intoElement-wise subtract scalar into the output tensor.
fn sub_scalar_into<OutputTensor: TensorMut<Scalar, MAX_RANK> + ?Sized>(
&self,
scalar: Scalar::Scalar,
out: &mut OutputTensor,
) -> Result<(), Error> mul_scalar_intoElement-wise multiply scalar into the output tensor.
fn mul_scalar_into<OutputTensor: TensorMut<Scalar, MAX_RANK> + ?Sized>(
&self,
scalar: Scalar::Scalar,
out: &mut OutputTensor,
) -> Result<(), Error> TraitSumOpsExtension trait: element-wise addition for any TensorRef implementor.2
pub trait SumOps<Scalar: Clone + EachSum, const MAX_RANK: usize>: TensorRef<Scalar, MAX_RANK> Implementors
add_tensorElement-wise sum of two tensors into a new tensor.
fn add_tensor(
&self,
other: &impl TensorRef<Scalar, MAX_RANK> + ?Sized,
) -> Result<Tensor<Scalar, Self::Alloc, MAX_RANK>, Error>
where
Self::Alloc: Clone, add_tensor_intoElement-wise sum of two tensors into the output tensor.
fn add_tensor_into<OtherTensor, OutputTensor>(
&self,
other: &OtherTensor,
out: &mut OutputTensor,
) -> Result<(), Error>
where
OtherTensor: TensorRef<Scalar, MAX_RANK> + ?Sized,
OutputTensor: TensorMut<Scalar, MAX_RANK> + ?Sized, TraitBitwiseReductionsOpsPopulation count, any/none/all reductions over packed-bit (u1x8) tensors.4
pub trait BitwiseReductionsOps<const MAX_RANK: usize>: TensorRef<u1x8, MAX_RANK> Blanket-impl'd on every TensorRef<u1x8, R>, so Tensor<u1x8>, TensorView<u1x8>, and
TensorSpan<u1x8> all expose .popcount() / .any_set() / .none_set() / .all_set() with
identical spellings. Mirrors numkong::popcount(tensor_view) in cpp/numkong/reduce.hpp.
Method names use the _set suffix to leave room for a future generic any / none / all
over arbitrary scalar predicates and to avoid colliding with the all slice marker exported
from crate::vector.
Implementors
impl<Container, const MAX_RANK: usize> BitwiseReductionsOps<MAX_RANK> for Container where Container: TensorRef<u1x8, MAX_RANK> + ?Sized
popcountNumber of set bits across the entire tensor.
fn popcount(&self) -> Result<u64, Error> any_settrue if at least one bit in the tensor is set.
fn any_set(&self) -> Result<bool, Error> none_settrue if no bit in the tensor is set.
fn none_set(&self) -> Result<bool, Error> all_settrue if every bit in the tensor is set.
fn all_set(&self) -> Result<bool, Error> TraitMinMaxOpsExtension trait: min/max reductions for any TensorRef implementor.11
pub trait MinMaxOps<Scalar: Clone + ReduceMinMax, const MAX_RANK: usize>: TensorRef<Scalar, MAX_RANK>
where
Scalar::Output: Clone + Default + PartialOrd, Implementors
impl<Scalar: Clone + ReduceMinMax, const R: usize, C: TensorRef<Scalar, R>> MinMaxOps<Scalar, R> for C where Scalar::Output: Clone + Default + PartialOrd
minmax_allMinimum and maximum with their indices over all elements.
fn minmax_all(&self) -> Result<MinMaxResult<Scalar::Output>, Error> minmax_axisMinimum and maximum with their indices along one axis, into new tensors.
fn minmax_axis<AnyIndex: VectorIndex>(
&self,
axis: AnyIndex,
keep_dims: bool,
) -> Result<MinMaxResult<Tensor<<Scalar as ReduceMinMax>::Output, Self::Alloc, MAX_RANK>, Tensor<usize, Self::Alloc, MAX_RANK>>, Error>
where
Self::Alloc: Clone, minmax_axis_intoMinimum and maximum with their indices along one axis, into the output tensors.
fn minmax_axis_into<AnyIndex, ValueTensor, IndexTensor>(
&self,
axis: AnyIndex,
keep_dims: bool,
min_out: &mut ValueTensor,
argmin_out: &mut IndexTensor,
max_out: &mut ValueTensor,
argmax_out: &mut IndexTensor,
) -> Result<(), Error>
where
AnyIndex: VectorIndex,
ValueTensor: TensorMut<Scalar::Output, MAX_RANK> + ?Sized,
IndexTensor: TensorMut<usize, MAX_RANK> + ?Sized, min_allMinimum over all elements.
fn min_all(&self) -> Result<Scalar::Output, Error> argmin_allIndex of the minimum over all elements.
fn argmin_all(&self) -> Result<usize, Error> max_allMaximum over all elements.
fn max_all(&self) -> Result<Scalar::Output, Error> argmax_allIndex of the maximum over all elements.
fn argmax_all(&self) -> Result<usize, Error> min_axisMinimum along one axis, into a new tensor.
fn min_axis<AnyIndex: VectorIndex>(
&self,
axis: AnyIndex,
keep_dims: bool,
) -> Result<Tensor<Scalar::Output, Self::Alloc, MAX_RANK>, Error>
where
Self::Alloc: Clone, argmin_axisIndex of the minimum along one axis, into a new tensor.
fn argmin_axis<AnyIndex: VectorIndex>(
&self,
axis: AnyIndex,
keep_dims: bool,
) -> Result<Tensor<usize, Self::Alloc, MAX_RANK>, Error>
where
Self::Alloc: Clone, max_axisMaximum along one axis, into a new tensor.
fn max_axis<AnyIndex: VectorIndex>(
&self,
axis: AnyIndex,
keep_dims: bool,
) -> Result<Tensor<Scalar::Output, Self::Alloc, MAX_RANK>, Error>
where
Self::Alloc: Clone, argmax_axisIndex of the maximum along one axis, into a new tensor.
fn argmax_axis<AnyIndex: VectorIndex>(
&self,
axis: AnyIndex,
keep_dims: bool,
) -> Result<Tensor<usize, Self::Alloc, MAX_RANK>, Error>
where
Self::Alloc: Clone, TraitMomentsOpsExtension trait: statistical reductions for any TensorRef implementor.9
pub trait MomentsOps<Scalar: Clone + ReduceMoments, const MAX_RANK: usize>: TensorRef<Scalar, MAX_RANK>
where
Scalar::SumOutput: Clone + Default + AddAssign,
Scalar::SumSqOutput: Clone + Default + AddAssign + SumSqToF64, Implementors
impl<Scalar: Clone + ReduceMoments, const R: usize, C: TensorRef<Scalar, R>> MomentsOps<Scalar, R> for C where Scalar::SumOutput: Clone + Default + AddAssign, Scalar::SumSqOutput: Clone + Default + AddAssign + SumSqToF64
moments_allSum and sum of squares over all elements.
fn moments_all(&self) -> Result<(Scalar::SumOutput, Scalar::SumSqOutput), Error> moments_axisSum and sum of squares along one axis, into new tensors.
fn moments_axis<AnyIndex: VectorIndex>(
&self,
axis: AnyIndex,
keep_dims: bool,
) -> Result<(Tensor<<Scalar as ReduceMoments>::SumOutput, Self::Alloc, MAX_RANK>, Tensor<<Scalar as ReduceMoments>::SumSqOutput, Self::Alloc, MAX_RANK>), Error>
where
Self::Alloc: Clone, moments_axis_intoSum and sum of squares along one axis, into the output tensors.
fn moments_axis_into<AnyIndex, SumTensor, SumSqTensor>(
&self,
axis: AnyIndex,
keep_dims: bool,
sum_out: &mut SumTensor,
sumsq_out: &mut SumSqTensor,
) -> Result<(), Error>
where
AnyIndex: VectorIndex,
SumTensor: TensorMut<Scalar::SumOutput, MAX_RANK> + ?Sized,
SumSqTensor: TensorMut<Scalar::SumSqOutput, MAX_RANK> + ?Sized, sum_allSum over all elements.
fn sum_all(&self) -> Result<Scalar::SumOutput, Error> sum_axisSum along one axis, into a new tensor.
fn sum_axis<AnyIndex: VectorIndex>(
&self,
axis: AnyIndex,
keep_dims: bool,
) -> Result<Tensor<Scalar::SumOutput, Self::Alloc, MAX_RANK>, Error>
where
Self::Alloc: Clone, sum_axis_intoSum along one axis, into the output tensor.
fn sum_axis_into<AnyIndex, SumTensor>(
&self,
axis: AnyIndex,
keep_dims: bool,
out: &mut SumTensor,
) -> Result<(), Error>
where
AnyIndex: VectorIndex,
SumTensor: TensorMut<Scalar::SumOutput, MAX_RANK> + ?Sized, norm_allEuclidean norm over all elements.
fn norm_all(&self) -> Result<f64, Error> norm_axisEuclidean norm along one axis, into a new tensor.
fn norm_axis<AnyIndex: VectorIndex>(
&self,
axis: AnyIndex,
keep_dims: bool,
) -> Result<Tensor<f64, Self::Alloc, MAX_RANK>, Error>
where
Self::Alloc: Clone, norm_axis_intoEuclidean norm along one axis, into the output tensor.
fn norm_axis_into<AnyIndex, NormTensor>(
&self,
axis: AnyIndex,
keep_dims: bool,
out: &mut NormTensor,
) -> Result<(), Error>
where
AnyIndex: VectorIndex,
NormTensor: TensorMut<f64, MAX_RANK> + ?Sized, TraitReduceMinMaxFind minimum and maximum values with their indices, with stride support.3
pub trait ReduceMinMax: StorageElement Returns Ok(Some(MinMaxResult)) for all elements in a slice, or Ok(None) if all elements are
NaN, for NaN-masking formats. The output value type matches the logical reduced scalar type. A
refusing kernel fails with Error::KernelFailed.
Implementors
impl ReduceMinMax for f64impl ReduceMinMax for f32impl ReduceMinMax for i8impl ReduceMinMax for u8impl ReduceMinMax for i16impl ReduceMinMax for u16impl ReduceMinMax for i32impl ReduceMinMax for u32impl ReduceMinMax for i64impl ReduceMinMax for u64impl ReduceMinMax for f16impl ReduceMinMax for bf16impl ReduceMinMax for e4m3impl ReduceMinMax for e5m2impl ReduceMinMax for e2m3impl ReduceMinMax for e3m2impl ReduceMinMax for i4x2impl ReduceMinMax for u4x2impl ReduceMinMax for u1x8
OutputOutput type for the min/max values — matches the C layer's native type.
type Output: StorageElement; NONE_ON_SENTINELWhether NUMKONG_SIZE_MAX indicates that the reduction produced no value.
const NONE_ON_SENTINEL: bool; reduce_minmaxReturns Ok(Some(MinMaxResult)) for the given data with the specified stride, or Ok(None) if all elements are NaN.
fn reduce_minmax(data: &[Self], stride: usize) -> Result<Option<MinMaxResult<Self::Output>>, Error> Reads data.len() logical elements starting at data.as_ptr(), advancing by stride
between each — pass size_of::<Self>() for contiguous storage.
TraitReduceMomentsCompute first and second moments — sum and sum-of-squares — with stride support.3
pub trait ReduceMoments: StorageElement Returns (sum, sum_of_squares) for all elements in a slice, with optional striding.
The output types may be wider than the input to avoid overflow.
Errors
Error::KernelFailed when no capability of the current
Capabilities has the kernel.
Implementors
impl ReduceMoments for f64impl ReduceMoments for f32impl ReduceMoments for i8impl ReduceMoments for u8impl ReduceMoments for i16impl ReduceMoments for u16impl ReduceMoments for i32impl ReduceMoments for u32impl ReduceMoments for i64impl ReduceMoments for u64impl ReduceMoments for f16impl ReduceMoments for bf16impl ReduceMoments for e4m3impl ReduceMoments for e5m2impl ReduceMoments for e2m3impl ReduceMoments for e3m2impl ReduceMoments for i4x2impl ReduceMoments for u4x2impl ReduceMoments for u1x8
SumOutputType for the sum output.
type SumOutput: StorageElement; SumSqOutputType for the sum-of-squares output.
type SumSqOutput: StorageElement; reduce_momentsCompute (sum, sum_of_squares) for data with the given stride in bytes.
fn reduce_moments(
data: &[Self],
stride: usize,
) -> Result<(Self::SumOutput, Self::SumSqOutput), Error> Use stride = size_of::<Self>() for contiguous data.
Reads data.len() logical elements starting at data.as_ptr(), advancing by stride
between each — pass size_of::<Self>() for contiguous storage.
TraitReductionsReductions bundles reduction operations: ReduceMoments and ReduceMinMax.
pub trait Reductions: ReduceMoments + ReduceMinMax Implementors
impl<Scalar: ReduceMoments + ReduceMinMax> Reductions for Scalar
TraitTrigAtanComputes element-wise arctangent of a vector.2
pub trait TrigAtan: Sized + StorageElement Fails with Error::ShapeMismatch if lengths differ, or Error::KernelFailed
carrying the kernel's status.
Implementors
atanWrites the arctangent of every input element, in radians, into the output slice.
fn atan(inputs: &[Self], outputs: &mut [Self]) -> Result<(), Error> atan_inplaceIn-place arctangent: data[i] = atan(data[i]).
fn atan_inplace(data: &mut [Self]) -> Result<(), Error> Both source and destination pointers are derived from the single &mut, so no aliased
&[Self] + &mut [Self] over the same storage is formed.
TraitTrigAtanOpsExtension trait: element-wise arctangent for any TensorRef implementor.2
pub trait TrigAtanOps<Scalar: Clone + TrigAtan, const MAX_RANK: usize>: TensorRef<Scalar, MAX_RANK> Implementors
impl<Scalar: Clone + TrigAtan, const R: usize, C: TensorRef<Scalar, R> + ?Sized> TrigAtanOps<Scalar, R> for C
atanElement-wise arctangent into a new tensor.
fn atan(&self) -> Result<Tensor<Scalar, Self::Alloc, MAX_RANK>, Error>
where
Self::Alloc: Clone, atan_intoElement-wise arctangent into the output tensor.
fn atan_into<OutputTensor: TensorMut<Scalar, MAX_RANK> + ?Sized>(
&self,
out: &mut OutputTensor,
) -> Result<(), Error> TraitTrigCosComputes element-wise cosine of a vector.2
pub trait TrigCos: Sized + StorageElement Fails with Error::ShapeMismatch if lengths differ, or Error::KernelFailed
carrying the kernel's status.
Implementors
cosWrites the cosine of every input element, in radians, into the output slice.
fn cos(inputs: &[Self], outputs: &mut [Self]) -> Result<(), Error> cos_inplaceIn-place cosine: data[i] = cos(data[i]).
fn cos_inplace(data: &mut [Self]) -> Result<(), Error> Both source and destination pointers are derived from the single &mut, so no aliased
&[Self] + &mut [Self] over the same storage is formed.
TraitTrigCosOpsExtension trait: element-wise cosine for any TensorRef implementor.2
pub trait TrigCosOps<Scalar: Clone + TrigCos, const MAX_RANK: usize>: TensorRef<Scalar, MAX_RANK> Implementors
impl<Scalar: Clone + TrigCos, const R: usize, C: TensorRef<Scalar, R> + ?Sized> TrigCosOps<Scalar, R> for C
cosElement-wise cosine into a new tensor.
fn cos(&self) -> Result<Tensor<Scalar, Self::Alloc, MAX_RANK>, Error>
where
Self::Alloc: Clone, cos_intoElement-wise cosine into the output tensor.
fn cos_into<OutputTensor: TensorMut<Scalar, MAX_RANK> + ?Sized>(
&self,
out: &mut OutputTensor,
) -> Result<(), Error> TraitTrigSinComputes element-wise sine of a vector.2
pub trait TrigSin: Sized + StorageElement Fails with Error::ShapeMismatch if lengths differ, or Error::KernelFailed
carrying the kernel's status.
Implementors
sinWrites the sine of every input element, in radians, into the output slice.
fn sin(inputs: &[Self], outputs: &mut [Self]) -> Result<(), Error> sin_inplaceIn-place sine: data[i] = sin(data[i]).
fn sin_inplace(data: &mut [Self]) -> Result<(), Error> Both source and destination pointers are derived from the single &mut, so no aliased
&[Self] + &mut [Self] over the same storage is formed.
TraitTrigSinOpsExtension trait: element-wise sine for any TensorRef implementor.2
pub trait TrigSinOps<Scalar: Clone + TrigSin, const MAX_RANK: usize>: TensorRef<Scalar, MAX_RANK> Implementors
impl<Scalar: Clone + TrigSin, const R: usize, C: TensorRef<Scalar, R> + ?Sized> TrigSinOps<Scalar, R> for C
sinElement-wise sine into a new tensor.
fn sin(&self) -> Result<Tensor<Scalar, Self::Alloc, MAX_RANK>, Error>
where
Self::Alloc: Clone, sin_intoElement-wise sine into the output tensor.
fn sin_into<OutputTensor: TensorMut<Scalar, MAX_RANK> + ?Sized>(
&self,
out: &mut OutputTensor,
) -> Result<(), Error> TraitTrigonometryTrigonometry bundles trigonometric functions: TrigSin, TrigCos, and TrigAtan.
pub trait Trigonometry: TrigSin + TrigCos + TrigAtan Implementors
impl<Scalar: TrigSin + TrigCos + TrigAtan> Trigonometry for Scalar
StructMinMaxResultNamed result from min/max reduction operations.
pub struct MinMaxResult<Value, AnyIndex = usize> AnyIndex defaults to usize and Value is the scalar output type for scalar reductions such
as minmax_all, while for axis reductions such as minmax_axis, Value and AnyIndex are
both tensors.
Fields
min_value | Value | Smallest value. |
|---|---|---|
min_index | AnyIndex | Index of the smallest value. |
max_value | Value | Largest value. |
max_index | AnyIndex | Index of the largest value. |
Implements
Debug, Clone, StructuralPartialEq, PartialEq, Eq