Mila 0.13.48
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
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)

Private Member Functions

std::string currentPrefix () const
bool isAbsolutePath (const std::string &path) const noexcept
void requireMode (OpenMode expected, const char *op) const
void requireOpen (const char *op) const
std::string scopedPath (const std::string &path) const

Private Attributes

bool closed_ { false }
std::string filepath_
OpenMode mode_
std::vector< std::string > scope_stack_
std::unique_ptr< ArchiveSerializerserializer_

Static Private Attributes

static const std::vector< std::string > kAbsoluteRoots = { "network/", "components/", "modules/" }

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
/__w/Mila/Mila/Mila/Src/Dnn/Components/Activations/Gelu/Gelu.ixx, /__w/Mila/Mila/Mila/Src/Dnn/Components/Normalization/Softmax.ixx, /__w/Mila/Mila/Mila/Src/Dnn/Core/ComponentFactory.ixx, and /__w/Mila/Mila/Mila/Src/Dnn/Core/NetworkFactory.ixx.

Constructor & Destructor Documentation

◆ ModelArchive() [1/3]

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
/__w/Mila/Mila/Mila/Src/Dnn/Serialization/ModelArchive.ixx.
Here is the caller graph for this function:

◆ ~ModelArchive()

Mila::Dnn::Serialization::ModelArchive::~ModelArchive ( )
inline

Destructor automatically closes the serializer.

Examples
/__w/Mila/Mila/Mila/Src/Dnn/Serialization/ModelArchive.ixx.
Here is the call graph for this function:

◆ ModelArchive() [2/3]

Mila::Dnn::Serialization::ModelArchive::ModelArchive ( const ModelArchive & )
delete
Here is the call graph for this function:

◆ ModelArchive() [3/3]

Mila::Dnn::Serialization::ModelArchive::ModelArchive ( ModelArchive && other)
inlinenoexcept
Here is the call graph for this function:

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
/__w/Mila/Mila/Mila/Src/Dnn/Serialization/ModelArchive.ixx.
Here is the caller graph for this function:

◆ close()

void Mila::Dnn::Serialization::ModelArchive::close ( )
inline
Examples
/__w/Mila/Mila/Mila/Src/Dnn/Serialization/ModelArchive.ixx.
Here is the caller graph for this function:

◆ currentPrefix()

std::string Mila::Dnn::Serialization::ModelArchive::currentPrefix ( ) const
inlineprivate
Examples
/__w/Mila/Mila/Mila/Src/Dnn/Serialization/ModelArchive.ixx.
Here is the caller graph for this function:

◆ getFilepath()

const std::string & Mila::Dnn::Serialization::ModelArchive::getFilepath ( ) const
inlinenoexcept
Examples
/__w/Mila/Mila/Mila/Src/Dnn/Serialization/ModelArchive.ixx.
Here is the caller graph for this function:

◆ getFileSize()

size_t Mila::Dnn::Serialization::ModelArchive::getFileSize ( const std::string & path) const
inline
Examples
/__w/Mila/Mila/Mila/Src/Dnn/Serialization/ModelArchive.ixx.
Here is the call graph for this function:

◆ 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
/__w/Mila/Mila/Mila/Src/Dnn/Serialization/ModelArchive.ixx.

◆ getMode()

OpenMode Mila::Dnn::Serialization::ModelArchive::getMode ( ) const
inlinenoexcept

◆ hasFile()

bool Mila::Dnn::Serialization::ModelArchive::hasFile ( const std::string & path) const
inline
Examples
/__w/Mila/Mila/Mila/Src/Dnn/Serialization/ModelArchive.ixx.
Here is the call graph for this function:

◆ isAbsolutePath()

bool Mila::Dnn::Serialization::ModelArchive::isAbsolutePath ( const std::string & path) const
inlineprivatenoexcept
Examples
/__w/Mila/Mila/Mila/Src/Dnn/Serialization/ModelArchive.ixx.
Here is the caller graph for this function:

◆ isClosed()

bool Mila::Dnn::Serialization::ModelArchive::isClosed ( ) const
inlinenoexcept

◆ listFiles()

std::vector< std::string > Mila::Dnn::Serialization::ModelArchive::listFiles ( ) const
inline
Examples
/__w/Mila/Mila/Mila/Src/Dnn/Serialization/ModelArchive.ixx.
Here is the call graph for this function:

◆ operator=() [1/2]

ModelArchive & Mila::Dnn::Serialization::ModelArchive::operator= ( const ModelArchive & )
delete
Examples
/__w/Mila/Mila/Mila/Src/Dnn/Serialization/ModelArchive.ixx.
Here is the call graph for this function:

◆ operator=() [2/2]

ModelArchive & Mila::Dnn::Serialization::ModelArchive::operator= ( ModelArchive && other)
inlinenoexcept
Here is the call graph for this function:

◆ popScope()

void Mila::Dnn::Serialization::ModelArchive::popScope ( )
inline

◆ pushScope()

void Mila::Dnn::Serialization::ModelArchive::pushScope ( const std::string & scope)
inline

◆ 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
/__w/Mila/Mila/Mila/Src/Dnn/Serialization/ModelArchive.ixx.
Here is the call graph for this function:

◆ 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
/__w/Mila/Mila/Mila/Src/Dnn/Serialization/ModelArchive.ixx.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ readMetadata()

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

◆ requireMode()

void Mila::Dnn::Serialization::ModelArchive::requireMode ( OpenMode expected,
const char * op ) const
inlineprivate
Examples
/__w/Mila/Mila/Mila/Src/Dnn/Serialization/ModelArchive.ixx.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ requireOpen()

void Mila::Dnn::Serialization::ModelArchive::requireOpen ( const char * op) const
inlineprivate
Examples
/__w/Mila/Mila/Mila/Src/Dnn/Serialization/ModelArchive.ixx.
Here is the caller graph for this function:

◆ scopedPath()

std::string Mila::Dnn::Serialization::ModelArchive::scopedPath ( const std::string & path) const
inlineprivate
Examples
/__w/Mila/Mila/Mila/Src/Dnn/Serialization/ModelArchive.ixx.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ 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
/__w/Mila/Mila/Mila/Src/Dnn/Serialization/ModelArchive.ixx.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ writeMetadata()

void Mila::Dnn::Serialization::ModelArchive::writeMetadata ( const std::string & path,
const SerializationMetadata & metadata )
inline
Examples
/__w/Mila/Mila/Mila/Src/Dnn/Components/Activations/Gelu/Gelu.ixx, and /__w/Mila/Mila/Mila/Src/Dnn/Serialization/ModelArchive.ixx.
Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ closed_

bool Mila::Dnn::Serialization::ModelArchive::closed_ { false }
private

◆ filepath_

std::string Mila::Dnn::Serialization::ModelArchive::filepath_
private

◆ kAbsoluteRoots

const std::vector<std::string> Mila::Dnn::Serialization::ModelArchive::kAbsoluteRoots = { "network/", "components/", "modules/" }
inlinestaticprivate

◆ mode_

OpenMode Mila::Dnn::Serialization::ModelArchive::mode_
private

◆ scope_stack_

std::vector<std::string> Mila::Dnn::Serialization::ModelArchive::scope_stack_
private

◆ serializer_

std::unique_ptr<ArchiveSerializer> Mila::Dnn::Serialization::ModelArchive::serializer_
private

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