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

Factory registry for Network deserialization. More...

Public Types

template<DeviceType TDeviceType, TensorDataType TPrecision>
using NetworkFactoryFunc

Static Public Member Functions

template<Compute::DeviceType TDeviceType, TensorDataType TPrecision>
static std::unique_ptr< Network< TDeviceType, TPrecision > > create (ModelArchive &archive, std::shared_ptr< ExecutionContext< TDeviceType > > exec_context)
 Create a network instance from archive using registered factories.
template<DeviceType TDeviceType, TensorDataType TPrecision>
static void registerNetwork (const std::string &network_type, NetworkFactoryFunc< TDeviceType, TPrecision > factory)

Detailed Description

Factory registry for Network deserialization.

Provides type-safe network reconstruction from archives using registered factory functions. Each concrete network type registers its own Load() method to enable polymorphic deserialization.

Design Pattern:

  • Registration: Concrete networks register factory functions at startup
  • Dispatch: Factory reads metadata to determine network type and precision
  • Construction: Invokes appropriate registered factory function

Usage:

// Registration (typically in network implementation file)
NetworkFactory::registerNetwork<DeviceType::Cpu, TensorDataType::FP32>(
"MnistClassifier",
[](ModelArchive& archive, auto exec_ctx) {
return MnistClassifier::Load(archive, exec_ctx->getDeviceId());
});
// Deserialization
archive, exec_context);
static std::unique_ptr< Network< TDeviceType, TPrecision > > create(ModelArchive &archive, std::shared_ptr< ExecutionContext< TDeviceType > > exec_context)
Create a network instance from archive using registered factories.
Definition NetworkFactory.ixx:117
ModelArchive provides high-level helpers for component serialization.
Definition ModelArchive.ixx:47
Examples
/home/runner/work/Mila/Mila/Mila/Src/Dnn/Core/NetworkFactory.ixx.

Member Typedef Documentation

◆ NetworkFactoryFunc

template<DeviceType TDeviceType, TensorDataType TPrecision>
using Mila::Dnn::NetworkFactory::NetworkFactoryFunc
Initial value:
std::function<std::unique_ptr<Network<TDeviceType, TPrecision>>(
std::shared_ptr<ExecutionContext<TDeviceType>>
)>

Member Function Documentation

◆ create()

template<Compute::DeviceType TDeviceType, TensorDataType TPrecision>
std::unique_ptr< Network< TDeviceType, TPrecision > > Mila::Dnn::NetworkFactory::create ( ModelArchive & archive,
std::shared_ptr< ExecutionContext< TDeviceType > > exec_context )
inlinestatic

Create a network instance from archive using registered factories.

Reads network metadata from the archive to determine the network type and dispatches to the appropriate registered factory function for reconstruction.

Metadata Requirements:

  • network/meta.json must contain:
    • "network_type": String identifying the concrete network class
    • "template_precision": String identifying the precision type
Template Parameters
TDeviceTypeDevice type for the network
TPrecisionPrecision type for the network
Parameters
archiveArchive containing serialized network
exec_contextExecution context for the reconstructed network
Returns
Unique pointer to reconstructed network
Exceptions
std::runtime_errorif network metadata is missing or malformed
std::runtime_errorif no factory is registered for the network type
std::runtime_errorif network reconstruction fails
auto exec_ctx = std::make_shared<ExecutionContext<DeviceType::Cpu>>(...);
archive, exec_ctx);
Examples
/home/runner/work/Mila/Mila/Mila/Src/Dnn/Core/NetworkFactory.ixx.

The documentation for this class was generated from the following file:
  • Mila/Src/Dnn/Core/NetworkFactory.ixx