Struct rten::TensorPool

source ·
pub struct TensorPool { /* private fields */ }
Expand description

A pool which enables reuse of data buffers from tensors and other containers.

Reusing buffers for operator outputs, as opposed to allocating a fresh buffer from the global allocator and freeing it when no longer needed, can provide a significant performance improvement.

TensorPool implements the Alloc trait, enabling tensors to be allocated from the pool using the various Tensor::*_in methods, eg. [Tensor::zeros_in]. Allocation requests will be satisfied from the pool if there is a suitable buffer available, or it will fall back to the global allocator otherwise.

When a tensor is no longer needed, it’s buffer can be added to the pool using pool.add(tensor.extract_buffer()), making it available for future allocations. A more convenient method is to wrap the tensor in a [PoolRef] smart pointer which will auto-return the tensor to the pool when dropped. A tensor can be wrapped using tensor.auto_return(pool). The [PoolRef] smart pointer can also be used with other container types, by implementing the [ExtractBuffer] trait for them.

Implementations§

source§

impl TensorPool

source

pub fn new() -> TensorPool

Return a new, empty pool.

This is a cheap operation that does not allocate, so it can be used to create a temporary pool to pass to a function that requires one, if the caller does not have a pool otherwise available.

source

pub fn alloc<T>(&self, capacity: usize) -> Vec<T>

Allocate an empty vec with a given capacity from the pool.

source

pub fn add<T>(&self, vec: Vec<T>)

Add a data buffer to the pool.

The buffer will be cleared using Vec::clear and then made available to fulfill future allocation requests.

source

pub fn alloc_count(&self) -> usize

Return the total number of allocation requests.

source

pub fn hit_count(&self) -> usize

Return the number of allocation requests that were fulfilled using items in the pool.

source

pub fn len(&self) -> usize

Return the number of buffers currently in the pool.

source

pub fn is_empty(&self) -> bool

Return true if the pool is empty.

Trait Implementations§

source§

impl Alloc for TensorPool

source§

fn alloc<T>(&self, capacity: usize) -> Vec<T>

Allocate storage for an owned tensor. Read more
source§

impl Default for TensorPool

source§

fn default() -> Self

Returns the “default value” for a type. 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> 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> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

impl<T> Pointable for T

source§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

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

§

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

§

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.