Tensor

Struct Tensor 

Source
pub struct Tensor<'data> { /* private fields */ }
Expand description

Tensor, can own, or borrow the underlying tensor

Implementations§

Source§

impl<'data> Tensor<'data>

Source

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

The shape of the tensor

use smelte-rs::cpu::f32::Tensor;

let tensor = Tensor::zeros(vec![2, 2]);
assert_eq!(tensor.shape(), vec![2, 2]);
Source

pub fn data(&self) -> &[f32]

A slice to the underlying tensor data

use smelte-rs::cpu::f32::Tensor;

let tensor = Tensor::zeros(vec![2, 2]);
assert_eq!(tensor.data(), vec![0.0; 4]);
Source

pub fn data_mut(&mut self) -> &mut [f32]

A mutable slice to the underlying tensor data

use smelte-rs::cpu::f32::Tensor;

let mut tensor = Tensor::zeros(vec![2, 2]);
tensor.data_mut().iter_mut().for_each(|v| *v += 1.0);
assert_eq!(tensor.data(), vec![1.0; 4]);
Source

pub fn zeros(shape: Vec<usize>) -> Self

Creates a new nulled tensor with given shape

use smelte-rs::cpu::f32::Tensor;

let tensor = Tensor::zeros(vec![2, 2]);
Source

pub fn borrowed( data: &'data [f32], shape: Vec<usize>, ) -> Result<Self, TensorError>

Creates a new borrowed tensor with given shape. Can fail if data doesn’t match the shape

use smelte-rs::cpu::f32::Tensor;

let data = [1.0, 2.0, 3.0, 4.0];
let tensor = Tensor::borrowed(&data, vec![2, 2]).unwrap();
Source

pub fn new<T>(data: T, shape: Vec<usize>) -> Result<Self, TensorError>
where T: Into<Cow<'data, [f32]>>,

Creates a new tensor with given shape. Can fail if data doesn’t match the shape

use smelte-rs::cpu::f32::Tensor;

let data = vec![1.0, 2.0, 3.0, 4.0];
let tensor = Tensor::new(data, vec![2, 2]).unwrap();

Trait Implementations§

Source§

impl<'data> Clone for Tensor<'data>

Source§

fn clone(&self) -> Tensor<'data>

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug<Tensor<'a>> for Tensor<'a>

Source§

fn data(&self) -> &[f32]

TODO
Source§

impl<'a> Tensor for Tensor<'a>

Source§

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

TODO
Source§

fn zeros(shape: Vec<usize>) -> Self

TODO
Source§

impl<'a> TensorAdd<Tensor<'a>> for Tensor<'a>

Source§

fn add(x: &Self, y: &mut Self) -> Result<(), SmeltError>

TODO
Source§

impl<'a> TensorAttention<Tensor<'a>> for Tensor<'a>

Source§

fn attention( query: &Linear<F32Tensor<'a>>, key: &Linear<F32Tensor<'a>>, value: &Linear<F32Tensor<'a>>, ctx: &mut BertContext<F32Tensor<'a>>, ) -> Result<(), SmeltError>

TODO
Source§

impl<'a> TensorGelu<Tensor<'a>> for Tensor<'a>

Source§

fn gelu(x: &mut Tensor<'a>) -> Result<(), SmeltError>

TODO
Source§

impl<'a> TensorMatmul<Tensor<'a>> for Tensor<'a>

Source§

fn matmul(x: &Self, y: &Self, out: &mut Self) -> Result<(), SmeltError>

TODO
Source§

impl<'a> TensorMatmulT<Tensor<'a>> for Tensor<'a>

Source§

fn matmul_t(x: &Self, y: &Self, out: &mut Self) -> Result<(), SmeltError>

TODO
Source§

impl<'a> TensorMul<Tensor<'a>> for Tensor<'a>

Source§

fn mul(x: &Self, y: &mut Self) -> Result<(), SmeltError>

TODO
Source§

impl<'a> TensorNormalize<Tensor<'a>> for Tensor<'a>

Source§

fn normalize(x: &mut Self, epsilon: f32) -> Result<(), SmeltError>

TODO
Source§

impl<'a> TensorSelect<Tensor<'a>> for Tensor<'a>

Source§

fn select(x: &[usize], weight: &Self, out: &mut Self) -> Result<(), SmeltError>

TODO
Source§

impl<'a> TensorSoftmax<Tensor<'a>> for Tensor<'a>

Source§

fn softmax(x: &mut Tensor<'a>) -> Result<(), SmeltError>

TODO
Source§

impl<'a> TensorTanh<Tensor<'a>> for Tensor<'a>

Source§

fn tanh(x: &mut Tensor<'a>) -> Result<(), SmeltError>

TODO
Source§

impl<'a> BertOps<Tensor<'a>> for Tensor<'a>

Source§

impl<'a> TensorOps<Tensor<'a>> for Tensor<'a>

Auto Trait Implementations§

§

impl<'data> Freeze for Tensor<'data>

§

impl<'data> RefUnwindSafe for Tensor<'data>

§

impl<'data> Send for Tensor<'data>

§

impl<'data> Sync for Tensor<'data>

§

impl<'data> Unpin for Tensor<'data>

§

impl<'data> UnwindSafe for Tensor<'data>

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> 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, 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.