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: StringDotted 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
impl Tensor
Sourcepub fn num_elements(&self) -> usize
pub fn num_elements(&self) -> usize
Number of elements.
Sourcepub fn read(&self) -> Result<Vec<u8>>
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.
Sourcepub fn into_parts(
self,
) -> (String, DType, Vec<usize>, impl Fn() -> Result<Vec<u8>> + Send + Sync + 'static)
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.