BurnpackStore

Struct BurnpackStore 

Source
pub struct BurnpackStore { /* private fields */ }
Expand description

BurnpackStore - A Burn-specific file format store using CBOR for metadata

Implementations§

Source§

impl BurnpackStore

Source

pub fn default_metadata() -> BTreeMap<String, String>

Get the default metadata that includes Burn framework information.

This includes:

  • format: “burnpack”
  • producer: “burn”
  • version: The version of burn-store crate (from CARGO_PKG_VERSION)

These metadata fields are automatically added to all saved models.

Source

pub fn from_file<P: AsRef<Path>>(path: P) -> Self

Create a new store from a file path

By default, automatically appends .bpk extension if the path doesn’t have one. Use .auto_extension(false) to disable this behavior.

§Examples
// Automatically appends .bpk
let store = BurnpackStore::from_file("model");  // creates "model.bpk"

// Already has extension, no append
let store = BurnpackStore::from_file("model.bpk");  // uses "model.bpk"
let store = BurnpackStore::from_file("model.myext");  // uses "model.myext"

// Disable auto-extension
let store = BurnpackStore::from_file("model").auto_extension(false);  // uses "model"
Source

pub fn from_bytes(bytes: Option<Bytes>) -> Self

Create a new store from bytes (for reading) or empty (for writing)

Source

pub fn from_static(data: &'static [u8]) -> Self

Create a new store from static bytes with zero-copy loading enabled.

This is optimized for embedded model weights where the data lives in the binary’s .rodata section. Tensor data is sliced without copying, keeping the static reference alive.

§Example
static MODEL_DATA: &[u8] = include_bytes!("model.bpk");
let store = BurnpackStore::from_static(MODEL_DATA);
Source

pub fn metadata(self, key: impl Into<String>, value: impl Into<String>) -> Self

Add metadata key-value pair

Source

pub fn clear_metadata(self) -> Self

Clear all metadata (including defaults)

This removes all metadata including the default format, producer, and version fields. Use with caution as some tools may expect these fields to be present.

Source

pub fn allow_partial(self, allow: bool) -> Self

Allow partial loading (ignore missing tensors)

When set to true, the store will not fail if some tensors are missing during loading. This is useful when loading a subset of a model’s parameters.

Default: false

Source

pub fn validate(self, validate: bool) -> Self

Enable or disable validation during loading

When validation is enabled, the store will check that loaded tensors match the expected shapes and data types. Disabling validation can improve performance but may lead to runtime errors if data is corrupted.

Default: true

Source

pub fn overwrite(self, overwrite: bool) -> Self

Allow overwriting existing files when saving

When set to false, attempting to save to an existing file will result in an error. When set to true, existing files will be overwritten without warning.

Default: false

Source

pub fn zero_copy(self, enable: bool) -> Self

Enable or disable zero-copy tensor loading.

When enabled and the backend supports it (memory-backed with shared bytes), tensor data is sliced from the source without copying. This keeps the source data alive as long as any tensor holds a reference.

Zero-copy is automatically enabled when using from_static. Use this method to enable it for other memory-backed stores created with from_bytes when using Bytes::from_shared().

Default: false (except for from_static which defaults to true)

Source

pub fn auto_extension(self, enable: bool) -> Self

Enable or disable automatic .bpk extension appending

When enabled (default), automatically appends .bpk to the file path if no extension is detected. If an extension is already present, it is preserved.

When disabled, uses the exact path provided without modification.

Default: true

§Examples
// With auto_extension enabled (default)
let store = BurnpackStore::from_file("model");  // -> "model.bpk"

// With auto_extension disabled
let store = BurnpackStore::from_file("model")
    .auto_extension(false);  // -> "model"
Source

pub fn with_filter(self, filter: PathFilter) -> Self

Set path filter for selective loading/saving

Source

pub fn with_regex(self, pattern: &str) -> Self

Add regex pattern to filter

Source

pub fn with_full_path(self, path: impl Into<String>) -> Self

Add exact path to filter

Source

pub fn match_all(self) -> Self

Match all tensors (no filtering)

Source

pub fn remap(self, remapper: KeyRemapper) -> Self

Set key remapper for tensor name transformations during loading

Source

pub fn with_remap_pattern<S1, S2>(self, from: S1, to: S2) -> Self
where S1: AsRef<str>, S2: Into<String>,

Add a single regex pattern for key remapping

Source

pub fn filter(self, filter: PathFilter) -> Self

Set the path filter

Source

pub fn get_bytes(&self) -> Result<Bytes, BurnpackError>

Get the bytes after writing (only valid for bytes mode after collecting)

Trait Implementations§

Source§

impl ModuleStore for BurnpackStore

Source§

type Error = BurnpackError

The error type that can be returned during storage operations. Read more
Source§

fn collect_from<B: Backend, M: ModuleSnapshot<B>>( &mut self, module: &M, ) -> Result<(), Self::Error>

Collect tensor data from a module and store it to storage. Read more
Source§

fn apply_to<B: Backend, M: ModuleSnapshot<B>>( &mut self, module: &mut M, ) -> Result<ApplyResult, Self::Error>

Load stored tensor data and apply it to a module. Read more
Source§

fn get_snapshot( &mut self, name: &str, ) -> Result<Option<&TensorSnapshot>, Self::Error>

Get a single tensor snapshot by name. Read more
Source§

fn get_all_snapshots( &mut self, ) -> Result<&BTreeMap<String, TensorSnapshot>, Self::Error>

Get all tensor snapshots from storage as an ordered map. Read more
Source§

fn keys(&mut self) -> Result<Vec<String>, Self::Error>

Get all tensor names/keys in storage. 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> Same for T

Source§

type Output = T

Should always be Self
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V