Skip to main content

Tensor

Struct Tensor 

Source
pub struct Tensor {
    pub name: String,
    /* private fields */
}
Expand description

A tensor in a checkpoint, with its bytes not yet read.

Opening a checkpoint parses its metadata only. read is what produces the tensor’s bytes, so a checkpoint can be inspected without loading it, or loaded one tensor at a time. ZIP and legacy containers read the file at that point; a TAR container’s storages are held in memory from open.

The element type and shape are fixed at parse time, when the reader checks that the tensor fits its storage, so they are read-only: byte_len is what read returns because nothing can move them apart.

Fields§

§name: String

Dotted path of the tensor through the checkpoint’s dictionaries ("encoder.0.weight"). Free to rename; only error messages look at it.

Implementations§

Source§

impl Tensor

Source

pub fn dtype(&self) -> DType

Element type.

Source

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

Extents, outermost first. Empty for a scalar.

Source

pub fn num_elements(&self) -> usize

Number of elements.

Source

pub fn byte_len(&self) -> usize

Number of bytes read returns.

Source

pub fn read(&self) -> Result<Vec<u8>>

Read the tensor’s bytes from the checkpoint.

The bytes are contiguous in row-major order and native-endian, whatever layout the file stored the tensor in: a strided or offset view is gathered, and a bool tensor holds only 0 and 1. Nothing is cached, so each call reads the storage again.

Reads from different threads run at once: a ZIP read holds the archive lock only to locate its entry. A compressed entry, which torch.save does not write, streams under the lock instead, as does every entry on a target other than Unix.

§Errors

A storage that is missing, corrupt or unreadable by its container fails with io::ErrorKind::InvalidData (a ZIP storage’s checksum is verified when the tensor spans all of it, unless the file was saved without checksums, as compute_crc32=False does), and one shorter than the pickle declared with io::ErrorKind::UnexpectedEof: the file disagrees with itself. Any other kind is the operating system’s, and means the file could not be read at all. Every error names the tensor and its storage.

Source

pub fn into_parts( self, ) -> (String, DType, Vec<usize>, impl Fn() -> Result<Vec<u8>> + Send + Sync + 'static)

Split the tensor into its metadata and its read handle.

For a caller that keeps the metadata elsewhere and wants only the handle, without a second copy of the name and shape held alive with it. The handle reads and fails exactly as read does.

Trait Implementations§

Source§

impl Clone for Tensor

Source§

fn clone(&self) -> Self

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

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> 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 = !

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

fn try_from(value: U) -> Result<T, !>

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.