Rust · Elementwise and Reductions

Elementwise and Reductions

use numkong::BlendOps;use numkong::EachBlend;use numkong::EachFma;use numkong::EachRmsCast;use numkong::EachRmsNorm;use numkong::EachScale;use numkong::EachSum;use numkong::EachSwiGlu;use numkong::FmaOps;use numkong::ScaleOps;use numkong::SumOps;use numkong::BitwiseReductionsOps;use numkong::MinMaxOps;use numkong::MomentsOps;use numkong::ReduceMinMax;use numkong::ReduceMoments;use numkong::Reductions;use numkong::TrigAtan;use numkong::TrigAtanOps;use numkong::TrigCos;use numkong::TrigCosOps;use numkong::TrigSin;use numkong::TrigSinOps;use numkong::Trigonometry;use numkong::MinMaxResult; 8.0.0-rc.1 · b1f866ab

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-squares

The 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
each.rs:4315 ↗
pub trait BlendOps<Scalar: Clone + EachBlend, const MAX_RANK: usize>: TensorRef<Scalar, MAX_RANK>
where
    Scalar::Scalar: From<f32> + Copy,

Implementors

  • impl<Scalar: Clone + EachBlend, const R: usize, C: TensorRef<Scalar, R> + ?Sized> BlendOps<Scalar, R> for C where Scalar::Scalar: From<f32> + Copy
sub_tensorElement-wise difference of two tensors into a new tensor.
each.rs:4320 ↗
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.
each.rs:4331 ↗
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.
each.rs:4346 ↗
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
each.rs:2474 ↗
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

ScalarType of the blend coefficients.
each.rs:2476 ↗
type Scalar;
each_blendWrites resultᵢ = α × aᵢ + β × bᵢ into the pre-sized output slice.
each.rs:2478 ↗
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ᵢ.
each.rs:2491 ↗
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
each.rs:3298 ↗
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

ScalarType of the fused multiply-add coefficients.
each.rs:3300 ↗
type Scalar;
each_fmaWrites resultᵢ = α × aᵢ × bᵢ + β × cᵢ into the pre-sized output slice.
each.rs:3302 ↗
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ᵢ.
each.rs:3318 ↗
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
each.rs:4939 ↗
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

InputWhat dot products of Self write: f32 for the narrow floats, f64 for f32, and i32 or u32 for i8 or u8.
each.rs:4942 ↗
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.
each.rs:4947 ↗
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
each.rs:4707 ↗
pub trait EachRmsNorm: Sized + StorageElement

gamma is an optional per-column gain of length columns, which defaults to unit scale when None.

Implementors

rmsnorm_intoGrouped RMSNorm of a 2D [rows,groups×columns] tensor into y of the same shape.
each.rs:4713 ↗
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
each.rs:1089 ↗
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

ScalarType of the scale and shift coefficients.
each.rs:1091 ↗
type Scalar;
each_scaleWrites resultᵢ = α × aᵢ + β into the pre-sized output slice.
each.rs:1107 ↗
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ᵢ + β.
each.rs:1113 ↗
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
each.rs:1799 ↗
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

each_sumWrites resultᵢ = aᵢ + bᵢ into the pre-sized output slice.
each.rs:1801 ↗
fn each_sum(a: &[Self], b: &[Self], result: &mut [Self]) -> Result<(), Error>
each_sum_inplaceIn-place sum: dataᵢ = dataᵢ + otherᵢ.
each.rs:1808 ↗
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
each.rs:4450 ↗
pub trait EachSwiGlu: Sized + StorageElement

Implementors

swiglu_intoFused SwiGLU of 2D tensors: y = silu(gate_scale × gate) × up × output_scale.
each.rs:4456 ↗
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
each.rs:4379 ↗
pub trait FmaOps<Scalar: Clone + EachFma, const MAX_RANK: usize>: TensorRef<Scalar, MAX_RANK>
where
    Scalar::Scalar: From<f32> + Copy,

Implementors

  • impl<Scalar: Clone + EachFma, const R: usize, C: TensorRef<Scalar, R> + ?Sized> FmaOps<Scalar, R> for C where Scalar::Scalar: From<f32> + Copy
mul_tensorElement-wise product of two tensors into a new tensor.
each.rs:4384 ↗
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.
each.rs:4395 ↗
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.
each.rs:4412 ↗
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
each.rs:4183 ↗
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

  • impl<Scalar: Clone + EachScale, const R: usize, C: TensorRef<Scalar, R> + ?Sized> ScaleOps<Scalar, R> for C where Scalar::Scalar: From<f32> + Mul<Output = Scalar::Scalar> + Copy
add_scalarElement-wise add scalar into a new tensor.
each.rs:4188 ↗
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.
each.rs:4196 ↗
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.
each.rs:4204 ↗
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.
each.rs:4212 ↗
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.
each.rs:4222 ↗
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.
each.rs:4231 ↗
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.
each.rs:4240 ↗
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
each.rs:4275 ↗
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.
each.rs:4277 ↗
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.
each.rs:4288 ↗
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
reduce.rs:1073 ↗
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

popcountNumber of set bits across the entire tensor.
reduce.rs:1075 ↗
fn popcount(&self) -> Result<u64, Error>
any_settrue if at least one bit in the tensor is set.
reduce.rs:1078 ↗
fn any_set(&self) -> Result<bool, Error>
none_settrue if no bit in the tensor is set.
reduce.rs:1081 ↗
fn none_set(&self) -> Result<bool, Error>
all_settrue if every bit in the tensor is set.
reduce.rs:1084 ↗
fn all_set(&self) -> Result<bool, Error>
TraitMinMaxOpsExtension trait: min/max reductions for any TensorRef implementor.11
reduce.rs:1220 ↗
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.
reduce.rs:1225 ↗
fn minmax_all(&self) -> Result<MinMaxResult<Scalar::Output>, Error>
minmax_axisMinimum and maximum with their indices along one axis, into new tensors.
reduce.rs:1228 ↗
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.
reduce.rs:1240 ↗
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.
reduce.rs:1259 ↗
fn min_all(&self) -> Result<Scalar::Output, Error>
argmin_allIndex of the minimum over all elements.
reduce.rs:1262 ↗
fn argmin_all(&self) -> Result<usize, Error>
max_allMaximum over all elements.
reduce.rs:1265 ↗
fn max_all(&self) -> Result<Scalar::Output, Error>
argmax_allIndex of the maximum over all elements.
reduce.rs:1268 ↗
fn argmax_all(&self) -> Result<usize, Error>
min_axisMinimum along one axis, into a new tensor.
reduce.rs:1271 ↗
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.
reduce.rs:1283 ↗
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.
reduce.rs:1295 ↗
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.
reduce.rs:1307 ↗
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
reduce.rs:1117 ↗
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.
reduce.rs:1123 ↗
fn moments_all(&self) -> Result<(Scalar::SumOutput, Scalar::SumSqOutput), Error>
moments_axisSum and sum of squares along one axis, into new tensors.
reduce.rs:1126 ↗
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.
reduce.rs:1138 ↗
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.
reduce.rs:1154 ↗
fn sum_all(&self) -> Result<Scalar::SumOutput, Error>
sum_axisSum along one axis, into a new tensor.
reduce.rs:1157 ↗
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.
reduce.rs:1169 ↗
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.
reduce.rs:1183 ↗
fn norm_all(&self) -> Result<f64, Error>
norm_axisEuclidean norm along one axis, into a new tensor.
reduce.rs:1186 ↗
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.
reduce.rs:1198 ↗
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
reduce.rs:646 ↗
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

OutputOutput type for the min/max values — matches the C layer's native type.
reduce.rs:648 ↗
type Output: StorageElement;
NONE_ON_SENTINELWhether NUMKONG_SIZE_MAX indicates that the reduction produced no value.
reduce.rs:650 ↗
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.
reduce.rs:656 ↗
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
reduce.rs:425 ↗
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

SumOutputType for the sum output.
reduce.rs:427 ↗
type SumOutput: StorageElement;
SumSqOutputType for the sum-of-squares output.
reduce.rs:429 ↗
type SumSqOutput: StorageElement;
reduce_momentsCompute (sum, sum_of_squares) for data with the given stride in bytes.
reduce.rs:435 ↗
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.
reduce.rs:1027 ↗
pub trait Reductions: ReduceMoments + ReduceMinMax

Implementors

TraitTrigAtanComputes element-wise arctangent of a vector.2
trigonometry.rs:276 ↗
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.
trigonometry.rs:278 ↗
fn atan(inputs: &[Self], outputs: &mut [Self]) -> Result<(), Error>
atan_inplaceIn-place arctangent: data[i] = atan(data[i]).
trigonometry.rs:284 ↗
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
trigonometry.rs:413 ↗
pub trait TrigAtanOps<Scalar: Clone + TrigAtan, const MAX_RANK: usize>: TensorRef<Scalar, MAX_RANK>

Implementors

atanElement-wise arctangent into a new tensor.
trigonometry.rs:415 ↗
fn atan(&self) -> Result<Tensor<Scalar, Self::Alloc, MAX_RANK>, Error>
where
    Self::Alloc: Clone,
atan_intoElement-wise arctangent into the output tensor.
trigonometry.rs:423 ↗
fn atan_into<OutputTensor: TensorMut<Scalar, MAX_RANK> + ?Sized>(
    &self,
    out: &mut OutputTensor,
) -> Result<(), Error>
TraitTrigCosComputes element-wise cosine of a vector.2
trigonometry.rs:182 ↗
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.
trigonometry.rs:184 ↗
fn cos(inputs: &[Self], outputs: &mut [Self]) -> Result<(), Error>
cos_inplaceIn-place cosine: data[i] = cos(data[i]).
trigonometry.rs:190 ↗
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
trigonometry.rs:392 ↗
pub trait TrigCosOps<Scalar: Clone + TrigCos, const MAX_RANK: usize>: TensorRef<Scalar, MAX_RANK>

Implementors

cosElement-wise cosine into a new tensor.
trigonometry.rs:394 ↗
fn cos(&self) -> Result<Tensor<Scalar, Self::Alloc, MAX_RANK>, Error>
where
    Self::Alloc: Clone,
cos_intoElement-wise cosine into the output tensor.
trigonometry.rs:402 ↗
fn cos_into<OutputTensor: TensorMut<Scalar, MAX_RANK> + ?Sized>(
    &self,
    out: &mut OutputTensor,
) -> Result<(), Error>
TraitTrigSinComputes element-wise sine of a vector.2
trigonometry.rs:88 ↗
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.
trigonometry.rs:90 ↗
fn sin(inputs: &[Self], outputs: &mut [Self]) -> Result<(), Error>
sin_inplaceIn-place sine: data[i] = sin(data[i]).
trigonometry.rs:96 ↗
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
trigonometry.rs:371 ↗
pub trait TrigSinOps<Scalar: Clone + TrigSin, const MAX_RANK: usize>: TensorRef<Scalar, MAX_RANK>

Implementors

sinElement-wise sine into a new tensor.
trigonometry.rs:373 ↗
fn sin(&self) -> Result<Tensor<Scalar, Self::Alloc, MAX_RANK>, Error>
where
    Self::Alloc: Clone,
sin_intoElement-wise sine into the output tensor.
trigonometry.rs:381 ↗
fn sin_into<OutputTensor: TensorMut<Scalar, MAX_RANK> + ?Sized>(
    &self,
    out: &mut OutputTensor,
) -> Result<(), Error>
TraitTrigonometryTrigonometry bundles trigonometric functions: TrigSin, TrigCos, and TrigAtan.
trigonometry.rs:365 ↗
pub trait Trigonometry: TrigSin + TrigCos + TrigAtan

Implementors

StructMinMaxResultNamed result from min/max reduction operations.
tensor.rs:546 ↗
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_valueValueSmallest value.
min_indexAnyIndexIndex of the smallest value.
max_valueValueLargest value.
max_indexAnyIndexIndex of the largest value.

Implements

Debug, Clone, StructuralPartialEq, PartialEq, Eq