pub struct Reader { /* private fields */ }Expand description
Reader for loading burnpack containers.
Implementations§
Source§impl Reader
impl Reader
Sourcepub fn from_bytes(bytes: Bytes) -> Result<Self, Error>
pub fn from_bytes(bytes: Bytes) -> Result<Self, Error>
Load a pack from an in-memory Bytes buffer.
Loading is lazy: only the header and metadata are parsed here. The buffer is kept as-is
(no copy, no share) and is only turned into zero-copy Bytes::view windows when you
consume the reader with into_tensors. For large models, prefer
from_file, which keeps tensor data file-backed and lazy.
Sourcepub fn from_file<P: AsRef<Path>>(path: P) -> Result<Self, Error>
pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Self, Error>
Load a pack from a file.
Only the header and metadata are read up front; the whole file is wrapped in a single
lazy Bytes::from_file source, and each tensor’s data is a Bytes::view window into
it, read from disk only when accessed. This integrates with the Burn ecosystem’s file
allocation (pinned-memory staging) for fast file-to-GPU transfers.
If path has no extension and does not exist as given, the canonical
crate::EXTENSION (.bpk) is appended.
Sourcepub fn into_tensors(self) -> Result<Vec<Tensor>, Error>
pub fn into_tensors(self) -> Result<Vec<Tensor>, Error>
Consume the reader, returning all tensors in sorted (alphabetical) name order.
Each tensor’s bytes are a zero-copy Bytes::view window into the source — no per-tensor
copy. For an in-memory source the buffer is shared once here (a cheap
Arc move, never a data copy) to make those views available; for a file source the windows
are file-backed and read lazily on access. Consuming self lets us hand the source’s
ownership to the views directly, so loading never reads or copies tensor data eagerly.
Sourcepub fn metadata(&self) -> &BTreeMap<String, String>
pub fn metadata(&self) -> &BTreeMap<String, String>
The user-supplied key/value metadata stored alongside the tensors.
For per-tensor info (dtype/shape/param id), use into_tensors — for a
file-backed reader that does not read any tensor data until a tensor’s bytes are accessed.
Sourcepub fn scalars(&self) -> &BTreeMap<String, Scalar>
pub fn scalars(&self) -> &BTreeMap<String, Scalar>
The typed scalars stored alongside the tensors.
Empty for files written before scalar support.
Sourcepub fn tensor_names(&self) -> Vec<&str>
pub fn tensor_names(&self) -> Vec<&str>
The names of all tensors in the pack, in sorted (alphabetical) order.
Sourcepub fn tensor_data(&self, name: &str) -> Result<Vec<u8>, Error>
pub fn tensor_data(&self, name: &str) -> Result<Vec<u8>, Error>
Read a single tensor’s raw little-endian bytes by name (always copies).
Returns Error::TensorNotFound if no tensor with that name exists.