Mila
Deep Neural Network Library
Loading...
Searching...
No Matches
Mila::Dnn::Loss< TDeviceType, TPrecision > Class Template Referenceexport
module Dnn.Loss

Abstract base class for neural network loss functions. More...

Inheritance diagram for Mila::Dnn::Loss< TDeviceType, TPrecision >:
Mila::Dnn::Component< TDeviceType, TPrecision >

Public Types

using ExecutionContextType = ExecutionContext<TDeviceType>
using MR = typename DeviceTypeTraits<TDeviceType>::memory_resource
using TensorType = Tensor<TPrecision, MR>

Additional Inherited Members

Public Member Functions inherited from Mila::Dnn::Component< TDeviceType, TPrecision >
 Component (const std::string &name)
 Construct component with required name identifier.
virtual void build (const BuildContext &context) final
 Build the component with the provided BuildContext (canonical overload).
virtual DeviceId getDeviceId () const =0
 Get the compute device id associated with this component.
virtual std::vector< ITensor * > getGradients () const =0
 Return non-owning pointers to parameter gradient tensors.
virtual MemoryStats getMemoryStats () const =0
 Return the current memory allocation breakdown for this component.
const std::string getName () const
 Get the component's name identifier.
virtual std::vector< std::string > getParameterNames () const
 List all available parameter names for this component.
virtual std::vector< ITensor * > getParameters () const =0
 Return non-owning pointers to parameter tensors.
virtual MemoryStats getRequiredMemory (const BuildContext &context) const
 Report what build( context ) would allocate, without allocating it.
TrainingMode getTrainingMode () const noexcept
 The current runtime behavioral mode of this Component.
virtual const ComponentType getType () const =0
 Get the component type identifier.
virtual bool isBuilt () const final
 Returns true if build() has completed successfully.
virtual void load_ (ModelArchive &archive, SerializationMode mode)
 Restore this component's parameters from its archive scope.
virtual void loadParameter (const std::string &, const Serialization::ITensorBlob &)
 Load a parameter from serialized tensor data.
virtual dim_t parameterCount () const =0
 Return number of trainable parameters.
virtual void requireSerializableParameters () const
 Verify this component can serialize whatever parameters it owns.
virtual void save_ (ModelArchive &archive, SerializationMode mode) const =0
virtual void saveFlatTensors (Serialization::SafeTensorsWriter &writer, const std::string &prefix, Serialization::TensorSavePass pass) const
 Drive this component's tensors through one pass of a flat safetensors save.
void setTrainingMode (TrainingMode mode)
 Set the runtime behavioral mode for this Component.
virtual void synchronize ()=0
virtual std::string toString () const =0
 Produce a short, human-readable description of the component.
virtual void zeroGradients ()
 Clear all model-owned gradients for this component.
Static Public Member Functions inherited from Mila::Dnn::Component< TDeviceType, TPrecision >
static constexpr DeviceType getDeviceType ()
 Compile-time device type for this component instance.
static constexpr TensorDataType getPrecision () noexcept
 Compile-time tensor precision for this component instance.
Protected Types inherited from Mila::Dnn::Component< TDeviceType, TPrecision >
using HostStagingMemoryResource
 Host memory a device-resident parameter stages through.
Protected Member Functions inherited from Mila::Dnn::Component< TDeviceType, TPrecision >
IExecutionContextgetExecutionContext () const
 Get the shared execution context.
bool hasExecutionContext () const noexcept
 Check if execution context has been set.
template<TensorDataType TParameterPrecision, typename TMemoryResource>
void loadParameterFromBlob (const std::string &param_name, const Serialization::ITensorBlob &blob, Tensor< TParameterPrecision, TMemoryResource > &target, const shape_t &expected_shape)
 Load a tensor blob into a parameter tensor with validation.
virtual void onBuilding (const BuildContext &)
 Hook invoked by build() to allocate component buffers.
virtual void onExecutionContextSet ()
 Lifecycle hook: Called immediately after ExecutionContext is set.
virtual void onTrainingModeChanging (TrainingMode)
 Hook called before TrainingMode transitions.
template<TensorDataType TParameterPrecision, typename TMemoryResource>
void saveParameterToArchive (ModelArchive &archive, const std::string &parameter_name, const Tensor< TParameterPrecision, TMemoryResource > &parameter) const
 Write one parameter tensor into the archive under "tensors/<name>".
template<TensorDataType TParameterPrecision, typename TMemoryResource>
void saveParameterToWriter (Serialization::SafeTensorsWriter &writer, const std::string &flat_name, const Tensor< TParameterPrecision, TMemoryResource > &parameter, Serialization::TensorSavePass pass) const
 Drive one parameter through one pass of a flat safetensors save.
void setExecutionContext (IExecutionContext *context)
 Set the execution context for this component.
Protected Attributes inherited from Mila::Dnn::Component< TDeviceType, TPrecision >
BuildContext build_context_ { shape_t{ 1 }, RuntimeMode::Training }
 The BuildContext stored at build time.

Detailed Description

template<Compute::DeviceType TDeviceType, TensorDataType TPrecision>
requires PrecisionSupportedOnDevice<TPrecision, TDeviceType>
class Mila::Dnn::Loss< TDeviceType, TPrecision >

Abstract base class for neural network loss functions.

Template Parameters
TDeviceTypeCompile-time device identifier for this loss.
TPrecisionData type used for computations.

Loss functions compute a scalar loss value given model predictions and target values. They may also provide hooks for optimizing the network graph and configuring reduction modes.


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