Mila
Deep Neural Network Library
Loading...
Searching...
No Matches
Mila::Dnn::Model< TDeviceType, TPrecision > Class Template Referenceabstractexport
module Dnn.Model
Inheritance diagram for Mila::Dnn::Model< TDeviceType, TPrecision >:
Mila::Dnn::LanguageModel< TDeviceType, TPrecision > Mila::Dnn::GemmaModel< TDeviceType, TPrecision > Mila::Dnn::GptModel< TDeviceType, TPrecision > Mila::Dnn::LlamaModel< TDeviceType, TPrecision >

Public Types

using NetworkType = Network<TDeviceType, TPrecision>

Public Member Functions

 Model (const Model &)=delete
 Model (Model &&)=default
DeviceId getDeviceId () const noexcept
 The device this model runs on.
MemoryStats getMemoryStats () const
 Current memory allocation breakdown for this model.
RuntimeMode getRuntimeMode () const noexcept
 The runtime mode this model was constructed for.
std::size_t getScratchHighWaterBytes () const
 Context-owned scratch device memory, in bytes, at its high-water mark.
bool isEval () const noexcept
 True if this model is currently in eval sub-state.
bool isInferenceMode () const noexcept
 True if this model was constructed for inference.
bool isTrainingMode () const noexcept
 True if this model was constructed for training.
Model & operator= (const Model &)=delete
Model & operator= (Model &&)=default
void setEval (bool eval)
 Toggle eval sub-state for this model.
virtual std::string toString () const =0
 Human-readable summary of this model's configuration.
void train ()
 Run the training loop for this model.

Protected Member Functions

 Model (std::unique_ptr< NetworkType > network, RuntimeMode runtime_mode)
 Construct with a fully built network and runtime mode.
virtual void onTraining ()=0
 Training loop hook – derived class owns the implementation.

Protected Attributes

std::unique_ptr< NetworkType > network_
 The owned Network instance.

Constructor & Destructor Documentation

◆ Model()

template<DeviceType TDeviceType, TensorDataType TPrecision>
Mila::Dnn::Model< TDeviceType, TPrecision >::Model ( std::unique_ptr< NetworkType > network,
RuntimeMode runtime_mode )
inlineexplicitprotected

Construct with a fully built network and runtime mode.

Called by derived class constructors only. The network must already be built and have parameters loaded before this constructor is called.

Parameters
networkFully built and loaded Network.
runtime_modeInference or Training – immutable after construction.

Member Function Documentation

◆ getRuntimeMode()

template<DeviceType TDeviceType, TensorDataType TPrecision>
RuntimeMode Mila::Dnn::Model< TDeviceType, TPrecision >::getRuntimeMode ( ) const
inlinenoexcept

The runtime mode this model was constructed for.

Immutable after construction. Governs which public API methods are valid.

◆ getScratchHighWaterBytes()

template<DeviceType TDeviceType, TensorDataType TPrecision>
std::size_t Mila::Dnn::Model< TDeviceType, TPrecision >::getScratchHighWaterBytes ( ) const
inline

Context-owned scratch device memory, in bytes, at its high-water mark.

Deliberately NOT folded into getMemoryStats(): that reports what components allocate, and this buffer belongs to the shared execution context, is allocated lazily during forward passes, and is sized by whichever operation needed the most at any point. Callers wanting the true resident total add the two.

Zero before the first forward pass, and on any backend that allocates no scratch.

◆ isEval()

template<DeviceType TDeviceType, TensorDataType TPrecision>
bool Mila::Dnn::Model< TDeviceType, TPrecision >::isEval ( ) const
inlinenoexcept

True if this model is currently in eval sub-state.

For RuntimeMode::Inference models always returns true – inference models never compute gradients. For RuntimeMode::Training models reflects the last setEval() call.

◆ isInferenceMode()

template<DeviceType TDeviceType, TensorDataType TPrecision>
bool Mila::Dnn::Model< TDeviceType, TPrecision >::isInferenceMode ( ) const
inlinenoexcept

True if this model was constructed for inference.

The model-family inference API (e.g. generate()) is valid. train() will throw.

◆ isTrainingMode()

template<DeviceType TDeviceType, TensorDataType TPrecision>
bool Mila::Dnn::Model< TDeviceType, TPrecision >::isTrainingMode ( ) const
inlinenoexcept

True if this model was constructed for training.

train() is valid. The model-family inference API will throw.

◆ onTraining()

template<DeviceType TDeviceType, TensorDataType TPrecision>
virtual void Mila::Dnn::Model< TDeviceType, TPrecision >::onTraining ( )
protectedpure virtual

Training loop hook – derived class owns the implementation.

Called by train() after precondition enforcement. The derived class has total control over data loading, optimizer construction, loss computation, backward pass, checkpointing, and sampling.

Pure virtual – a model declaring RuntimeMode::Training must provide a training loop.

Implemented in Mila::Dnn::GemmaModel< TDeviceType, TPrecision >, Mila::Dnn::GptModel< TDeviceType, TPrecision >, and Mila::Dnn::LlamaModel< TDeviceType, TPrecision >.

◆ setEval()

template<DeviceType TDeviceType, TensorDataType TPrecision>
void Mila::Dnn::Model< TDeviceType, TPrecision >::setEval ( bool eval)
inline

Toggle eval sub-state for this model.

When eval is true, the forward pass runs without gradients, dropout is disabled, and batch norm uses running statistics. When eval is false, the full training pass is restored.

Cascades through Network to every Component and Operation in the graph via their onEvalChanging() hooks.

Only valid on models constructed with RuntimeMode::Training. Inference-mode models are always in eval state by definition.

Parameters
evaltrue to enter eval sub-state, false to restore training.
Exceptions
std::runtime_errorif called on a RuntimeMode::Inference model.

◆ toString()

template<DeviceType TDeviceType, TensorDataType TPrecision>
virtual std::string Mila::Dnn::Model< TDeviceType, TPrecision >::toString ( ) const
pure virtual

◆ train()

template<DeviceType TDeviceType, TensorDataType TPrecision>
void Mila::Dnn::Model< TDeviceType, TPrecision >::train ( )
inline

Run the training loop for this model.

Enforces RuntimeMode::Training precondition then delegates entirely to onTraining(). The derived class owns the loop – data loading, optimizer, loss, checkpointing, and sampling are all derived class concerns.

Exceptions
std::runtime_errorif called on an Inference-mode model.

Member Data Documentation

◆ network_

template<DeviceType TDeviceType, TensorDataType TPrecision>
std::unique_ptr<NetworkType> Mila::Dnn::Model< TDeviceType, TPrecision >::network_
protected

The owned Network instance.

Accessible to derived classes for model-specific operations not covered by the base class API.


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