Model Bundling

Bundling converts model weights into the APR format for deployment.

The ModelBundle Builder

use apr_cookbook::bundle::ModelBundle;

let bundle = ModelBundle::new()
    .with_name("sentiment-v1")
    .with_description("BERT-based sentiment classifier")
    .with_compression(true)
    .with_payload(model_weights)
    .build();

Builder Methods

MethodDescription
with_name(s)Set model name (max 255 chars)
with_description(s)Set description (optional)
with_compression(bool)Enable zstd compression
with_payload(bytes)Set model weights
build()Create the APR bundle

Loading Bundles

use apr_cookbook::bundle::BundledModel;

// From bytes (zero-copy)
let model = BundledModel::from_bytes(&bundle_bytes)?;

// Access metadata
println!("Name: {}", model.name());
println!("Version: {:?}", model.version());
println!("Size: {} bytes", model.size());

// Check flags
if model.is_compressed() {
    println!("Payload is compressed");
}

BundledModel Methods

MethodReturnsDescription
name()&strModel name
version()(u8, u8)Format version
size()usizeTotal size in bytes
is_compressed()boolCompression flag
is_encrypted()boolEncryption flag
is_signed()boolSignature flag
as_bytes()&[u8]Raw bundle bytes

Compile-Time Embedding

The recommended pattern for production:

// Embed at compile time
const MODEL: &[u8] = include_bytes!("models/classifier.apr");

fn get_model() -> BundledModel<'static> {
    // This never fails if the file is valid APR
    BundledModel::from_bytes(MODEL).expect("embedded model is valid")
}

Benefits:

  • No file I/O at runtime
  • Model integrity verified at compile time
  • Single binary deployment