Mila
Deep Neural Network Library
Loading...
Searching...
No Matches
Mila::Dnn::Serialization::ModelArchive Class Referenceexport

ModelArchive provides high-level helpers for component serialization. More...

Classes

class  ScopedScope

Public Member Functions

 ModelArchive (const ModelArchive &)=delete
 ModelArchive (const std::string &filepath, std::unique_ptr< ArchiveSerializer > serializer, OpenMode mode)
 Construct archive with serializer and automatically open for specified mode.
 ModelArchive (ModelArchive &&other) noexcept
 ~ModelArchive ()
 Destructor automatically closes the serializer.
void addMetadata (const std::string &key, const std::string &value)
 Add simple metadata key-value pair.
void close ()
const std::string & getFilepath () const noexcept
size_t getFileSize (const std::string &path) const
std::string getMetadata (const std::string &key) const
 Retrieve simple metadata value.
OpenMode getMode () const noexcept
bool hasFile (const std::string &path) const
bool isClosed () const noexcept
std::vector< std::string > listFiles () const
ModelArchiveoperator= (const ModelArchive &)=delete
ModelArchiveoperator= (ModelArchive &&other) noexcept
void popScope ()
void pushScope (const std::string &scope)
std::vector< uint8_t > readBlob (const std::string &path) const
 Read raw binary blob from archive into returned vector.
size_t readBlobInto (const std::string &path, void *buffer, size_t buffer_size) const
 Read binary blob directly into provided buffer.
SerializationMetadata readMetadata (const std::string &path) const
 Read type-safe metadata from the archive.
void writeBlob (const std::string &path, const void *data, size_t size)
 Write raw binary blob to archive.
void writeMetadata (const std::string &path, const SerializationMetadata &metadata)

Detailed Description

ModelArchive provides high-level helpers for component serialization.

Responsibilities:

  • Coordinate ModelSerializer lifecycle (open/close)
  • Write/read type-safe metadata (abstracts JSON implementation)
  • Write/read arbitrary binary blobs
  • Enforce OpenMode for type-safe operations

Metadata Abstraction:

  • User code interacts with SerializationMetadata (type-safe, format-agnostic)
  • ModelArchive handles conversion to/from JSON internally
  • Future format changes (XML, binary, etc.) won't break user code

A simple scoping API (pushScope/popScope / ScopedScope) allows callers to set a logical directory prefix used by subsequent read/write operations.

Examples
/home/runner/work/Mila/Mila/Mila/Src/Dnn/Core/ComponentFactory.ixx, and /home/runner/work/Mila/Mila/Mila/Src/Dnn/Core/NetworkFactory.ixx.

Constructor & Destructor Documentation

◆ ModelArchive()

Mila::Dnn::Serialization::ModelArchive::ModelArchive ( const std::string & filepath,
std::unique_ptr< ArchiveSerializer > serializer,
OpenMode mode )
inlineexplicit

Construct archive with serializer and automatically open for specified mode.

The serializer is automatically opened for reading or writing based on the specified mode. Throws if the serializer cannot be opened.

Parameters
filepathPath to the archive file
serializerOwned serializer instance (typically ZipSerializer)
modeRead or Write mode for the archive
Exceptions
std::invalid_argumentif serializer is null
std::runtime_errorif serializer cannot be opened in the specified mode
Examples
/home/runner/work/Mila/Mila/Mila/Src/Dnn/Serialization/ModelArchive.ixx.

Member Function Documentation

◆ addMetadata()

void Mila::Dnn::Serialization::ModelArchive::addMetadata ( const std::string & key,
const std::string & value )
inline

Add simple metadata key-value pair.

For simple string metadata (archive-level tags, format markers, etc.). Use writeMetadata() for structured component metadata.

Examples
/home/runner/work/Mila/Mila/Mila/Src/Dnn/Serialization/ModelArchive.ixx.

◆ getMetadata()

std::string Mila::Dnn::Serialization::ModelArchive::getMetadata ( const std::string & key) const
inline

Retrieve simple metadata value.

For simple string metadata (archive-level tags, format markers, etc.). Use readMetadata() for structured component metadata.

Examples
/home/runner/work/Mila/Mila/Mila/Src/Dnn/Serialization/ModelArchive.ixx.

◆ readBlob()

std::vector< uint8_t > Mila::Dnn::Serialization::ModelArchive::readBlob ( const std::string & path) const
inline

Read raw binary blob from archive into returned vector.

Parameters
pathArchive path for binary data
Returns
Vector containing file contents
Exceptions
std::runtime_errorif archive is not in Read mode
std::runtime_errorif file missing or read fails
Examples
/home/runner/work/Mila/Mila/Mila/Src/Dnn/Serialization/ModelArchive.ixx.

◆ readBlobInto()

size_t Mila::Dnn::Serialization::ModelArchive::readBlobInto ( const std::string & path,
void * buffer,
size_t buffer_size ) const
inline

Read binary blob directly into provided buffer.

Parameters
pathArchive path
bufferPre-allocated buffer
buffer_sizeSize of buffer
Returns
Number of bytes read
Exceptions
std::runtime_errorif file missing or buffer too small
Examples
/home/runner/work/Mila/Mila/Mila/Src/Dnn/Serialization/ModelArchive.ixx.

◆ readMetadata()

SerializationMetadata Mila::Dnn::Serialization::ModelArchive::readMetadata ( const std::string & path) const
inline

Read type-safe metadata from the archive.

User-facing API that abstracts JSON deserialization format. Components use SerializationMetadata to read metadata without knowledge of underlying format.

Parameters
pathArchive path for metadata file
Returns
Type-safe metadata container
Exceptions
std::runtime_errorif archive is not in Read mode
std::runtime_errorif file missing or parse fails
auto meta = archive.readMetadata("meta.json");
std::string type = meta.getString("type");
int64_t version = meta.getInt("version");
int64_t features = meta.getInt("input_features");
Examples
/home/runner/work/Mila/Mila/Mila/Src/Dnn/Core/ComponentFactory.ixx, /home/runner/work/Mila/Mila/Mila/Src/Dnn/Core/NetworkFactory.ixx, and /home/runner/work/Mila/Mila/Mila/Src/Dnn/Serialization/ModelArchive.ixx.

◆ writeBlob()

void Mila::Dnn::Serialization::ModelArchive::writeBlob ( const std::string & path,
const void * data,
size_t size )
inline

Write raw binary blob to archive.

Parameters
pathArchive path for binary data
dataPointer to source bytes
sizeNumber of bytes to write
Exceptions
std::runtime_errorif archive is not in Write mode
std::runtime_erroron write failure
Examples
/home/runner/work/Mila/Mila/Mila/Src/Dnn/Serialization/ModelArchive.ixx.

The documentation for this class was generated from the following file: