Skip to main content

Tensor

Struct Tensor 

Source
pub struct Tensor { /* private fields */ }
Expand description

An immutable, strided tensor whose packed backing storage is shared by views.

Construction accepts at most MAX_RANK axes and MAX_ELEMENTS elements. Slices and transposes retain the same storage; reshaping a noncontiguous view materializes row-major bytes. Binary operations require matching types and use trailing-axis broadcasting. Serialization is {dtype, shape, data}, where every data entry is an exact decimal scalar string.

Implementations§

Source§

impl Tensor

Source

pub fn inspect( &self, options: TensorInspectionOptions, ) -> Result<TensorInspection, DataError>

Inspects this tensor without cloning or materializing its backing storage.

Views are visited in logical row-major order through their shape and strides. The inspection reads no more than options.max_samples, capped by MAX_INSPECTION_SAMPLES.

Source§

impl Tensor

Source

pub fn from_scalars( dtype: DType, shape: Vec<usize>, values: Vec<Scalar>, ) -> Result<Self, DataError>

Creates a contiguous tensor from scalars of dtype.

Source

pub fn from_le_bytes( dtype: DType, shape: Vec<usize>, bytes: Vec<u8>, ) -> Result<Self, DataError>

Creates a contiguous tensor from packed little-endian scalar bytes.

Source

pub fn zeros(dtype: DType, shape: Vec<usize>) -> Result<Self, DataError>

Creates a zero-filled contiguous tensor.

Source

pub fn dtype(&self) -> DType

Returns the scalar type.

Source

pub fn shape(&self) -> &[usize]

Returns the dimensions in logical axis order.

Source

pub fn strides(&self) -> &[isize]

Returns element strides for each logical axis.

Source

pub fn len(&self) -> usize

Returns the logical element count.

Source

pub fn is_empty(&self) -> bool

Reports whether the tensor has no logical elements.

Source

pub fn byte_len(&self) -> usize

Returns the packed byte count of the logical tensor.

Source

pub fn storage_byte_len(&self) -> usize

Returns the physical byte length of the shared backing allocation.

Source

pub fn get(&self, indices: &[isize]) -> Result<Scalar, DataError>

Reads one scalar using Python-style negative indices.

Source

pub fn slice(&self, indices: &[AxisIndex]) -> Result<Self, DataError>

Produces a shared-storage slice using Python indexing rules.

Source

pub fn with_slice( &self, indices: &[AxisIndex], replacement: &Self, ) -> Result<Self, DataError>

Replaces a selection in a new contiguous tensor of the original shape.

Replacement values broadcast across the selected shape using trailing axes. A rank-zero replacement fills the selection with a single scalar. Source and replacement may share storage: every replacement value is read from its original immutable allocation, so overlapping assignment is safe. Noncontiguous source views are copied in logical row-major order.

§Errors

Rejects invalid selectors, different element types, or replacement shapes that cannot broadcast into exactly the selection’s shape. On error, source and replacement remain unchanged.

use konjure_sdk::data::{AxisIndex, DType, Scalar, Tensor};
let original = Tensor::zeros(DType::I16, vec![2, 3])?;
let row = Tensor::from_scalars(DType::I16, vec![3],
    ["1", "2", "3"].into_iter().map(|text| Scalar::parse(DType::I16, text))
        .collect::<Result<Vec<_>, _>>()?)?;
let updated = original.with_slice(&[AxisIndex::Index(-1)], &row)?;
assert_eq!(updated.get(&[1, 2])?.to_string(), "3");
assert_eq!(original.get(&[1, 2])?.to_string(), "0");
Source

pub fn with_index_path( &self, path: &[Vec<AxisIndex>], replacement: &Self, ) -> Result<Self, DataError>

Replaces a selection formed by successive indexing expressions.

Each path entry indexes the preceding view. All entries are composed before one logical payload copy, including for a noncontiguous source. An empty path selects the entire tensor. Broadcasting and immutability follow Self::with_slice.

§Errors

Rejects paths longer than MAX_RANK, invalid selectors, mixed types, and replacement shapes that cannot broadcast into the final selection.

Source

pub fn reshape(&self, shape: Vec<usize>) -> Result<Self, DataError>

Changes dimensions while preserving logical row-major order.

Source

pub fn transpose(&self, axes: &[usize]) -> Result<Self, DataError>

Reorders axes; each old axis must occur exactly once.

Source

pub fn binary(&self, operation: BinaryOp, rhs: &Self) -> Result<Self, DataError>

Applies a scalar operation with NumPy trailing-axis broadcasting.

Source

pub fn scalar_binary( &self, operation: BinaryOp, rhs: Scalar, ) -> Result<Self, DataError>

Applies a scalar operation to every element.

Source

pub fn convert(&self, dtype: DType) -> Result<Self, DataError>

Converts every scalar to dtype.

Source

pub fn sum(&self) -> Result<Scalar, DataError>

Returns the additive reduction, or zero for an empty tensor.

Source

pub fn neg(&self) -> Result<Self, DataError>

Returns the element-wise arithmetic negation.

Source

pub fn matmul(&self, rhs: &Self) -> Result<Self, DataError>

Multiplies rank-two-or-greater tensors, broadcasting leading batch axes.

Source

pub fn to_scalars(&self) -> Vec<Scalar>

Materializes logical elements in row-major order.

Source

pub fn to_le_bytes(&self) -> Vec<u8>

Materializes logical elements as packed little-endian scalar bytes.

Trait Implementations§

Source§

impl Clone for Tensor

Source§

fn clone(&self) -> Tensor

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Tensor

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for Tensor

Source§

fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for Tensor

Source§

fn fmt(&self, formatter: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for Tensor

Source§

impl PartialEq for Tensor

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for Tensor

Source§

fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error>

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.