|
Mila 0.13.48
Deep Neural Network Library
|
Device-templated Residual connection component. More...


Public Types | |
| using | ComponentBase = Component<TDeviceType, TPrecision> |
| using | MR = typename DeviceTypeTraits<TDeviceType>::memory_resource |
| using | TensorType = Tensor<TPrecision, MR> |
Public Member Functions | |
| Residual (const std::string &name, const ResidualConfig &config, std::optional< DeviceId > device_id=std::nullopt) | |
| Construct Residual component with optional ExecutionContext ownership. | |
| ~Residual () override=default | |
| std::pair< TensorType &, TensorType & > | backward (const TensorType &input_a, const TensorType &input_b, const TensorType &output_grad) |
| Execute the backward pass and return component-owned gradient for input_a. | |
| TensorType & | forward (const TensorType &input_a, const TensorType &input_b) |
| Execute the forward pass and return component-owned output tensor. | |
| DeviceId | getDeviceId () const override |
| Get the compute device id associated with this component. | |
| std::vector< ITensor * > | getGradients () const override |
| Return non-owning pointers to parameter gradient tensors. | |
| MemoryStats | getMemoryStats () const override |
| Return the current memory allocation breakdown for this component. | |
| std::vector< ITensor * > | getParameters () const override |
| Return non-owning pointers to parameter tensors. | |
| const ComponentType | getType () const override |
| Get the component type identifier. | |
| size_t | parameterCount () const override |
| Number of trainable parameters. | |
| void | save_ (ModelArchive &archive, SerializationMode mode) const override |
| Serialize component parameters into the provided archive. | |
| void | synchronize () override |
| Block until all device operations submitted by this component complete. | |
| std::string | toString () const override |
| Return a human-readable description of the component. | |
| Public Member Functions inherited from Mila::Dnn::Component< TDeviceType, TPrecision > | |
| Component (const std::string &name) | |
| Construct component with required name identifier. | |
| virtual | ~Component ()=default |
| virtual void | build (const BuildContext &context) final |
| Build the component with the provided BuildContext (canonical overload). | |
| 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. | |
| RuntimeMode | getRuntimeMode () const noexcept |
| Convenience accessor — true if currently in Eval mode. | |
| TrainingMode | getTrainingMode () const noexcept |
| The current runtime behavioral mode of this Component. | |
| virtual bool | isBuilt () const final |
| Returns true if build() has completed successfully. | |
| bool | isInferenceMode () const noexcept |
| bool | isTrainingMode () const noexcept |
| virtual void | loadParameter (const std::string &name, const Serialization::ITensorBlob &blob) |
| Load a parameter from serialized tensor data. | |
| void | setTrainingMode (TrainingMode mode) |
| Set the runtime behavioral mode for this Component. | |
| virtual void | zeroGradients () |
| Clear all model-owned gradients for this component. | |
Protected Member Functions | |
| void | onBuilding (const BuildContext &build_config) override |
| Build the Residual component from the provided BuildContext. | |
| void | onExecutionContextSet () override |
| Hook invoked after ExecutionContext is set on the base Component. | |
| void | onTrainingModeChanging (TrainingMode training_mode) override |
| Hook invoked when training mode is about to change. | |
| Protected Member Functions inherited from Mila::Dnn::Component< TDeviceType, TPrecision > | |
| IExecutionContext * | getExecutionContext () 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 ¶m_name, const Serialization::ITensorBlob &blob, Tensor< TParameterPrecision, TMemoryResource > &target, const shape_t &expected_shape) |
| Load a tensor blob into a parameter tensor with validation. | |
| void | setExecutionContext (IExecutionContext *context) |
| Set the execution context for this component. | |
Private Types | |
| using | OpType = typename OperationTraits<OperationType::ResidualOp, TDeviceType, TPrecision>::type |
Private Member Functions | |
| void | createOperation () |
| Create backend BinaryOperation from OperationRegistry. | |
Private Attributes | |
| ResidualConfig | config_ |
| std::unique_ptr< TensorType > | input_a_grad_ { nullptr } |
| std::unique_ptr< TensorType > | input_b_grad_ { nullptr } |
| shape_t | leading_shape_ |
| std::shared_ptr< OpType > | operation_ { nullptr } |
| std::unique_ptr< TensorType > | output_ { nullptr } |
| std::unique_ptr< TensorType > | output_view_ { nullptr } |
| std::unique_ptr< IExecutionContext > | owned_exec_context_ { nullptr } |
Additional Inherited Members | |
| 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 Attributes inherited from Mila::Dnn::Component< TDeviceType, TPrecision > | |
| BuildContext | build_context_ { shape_t{ 1 }, RuntimeMode::Training } |
| The BuildContext stored at build time. | |
Device-templated Residual connection component.
Delegates binary residual computation to a device-specific backend operation. Parameters (if any) and any projection tensors are stored as Tensor instances bound to the associated execution context.
New API:
| TDeviceType | Device type (DeviceType::Cpu or DeviceType::Cuda). |
| TPrecision | Abstract tensor precision (TensorDataType). |
|
inlineexplicitexport |
Construct Residual component with optional ExecutionContext ownership.
Supports two construction modes:
| name | Component name identifier (mandatory). |
| build_config | Residual configuration. |
| device_id | Optional device identifier to create owned ExecutionContext. |
| std::invalid_argument | if build_config is invalid or device type mismatches. |
| std::runtime_error | if ExecutionContext creation fails (standalone mode). |

|
overrideexportdefault |
|
inlineexport |
Execute the backward pass and return component-owned gradient for input_a.
Component owns both input gradients (for input_a and input_b). The method returns the gradient tensor for input_a. The gradient for input_b can be accessed via getInputBGrad() after calling backward().
| input_a | Left forward input. |
| input_b | Right forward input. |
| output_grad | Gradient with respect to the component output. |
| std::runtime_error | if backend not initialized or if component not built/training. |

|
inlineexportprivate |
Create backend BinaryOperation from OperationRegistry.
Called by onExecutionContextSet(). Looks up "ResidualOp" in the OperationRegistry and creates a device-specific implementation.
| std::runtime_error | if operation creation fails. |


|
inlineexport |
Execute the forward pass and return component-owned output tensor.
The returned pointer is owned by the component and is valid until the component is destroyed or rebuilt. The backend BinaryOperation signature is unchanged; the component provides the owned output tensor when invoking the backend.
| input_a | Left input tensor. |
| input_b | Right input tensor. |
| std::runtime_error | if component has not been built or backend missing. |

|
inlineoverrideexportvirtual |
Get the compute device id associated with this component.
Must return the device on which parameters and operations execute.
Implements Mila::Dnn::Component< TDeviceType, TPrecision >.

|
inlineoverrideexportvirtual |
Return non-owning pointers to parameter gradient tensors.
Only valid in training mode. Residual has no trainable parameters by default.
Implements Mila::Dnn::Component< TDeviceType, TPrecision >.
|
inlineoverrideexportvirtual |
Return the current memory allocation breakdown for this component.
Reflects allocations at the moment of the call. The returned stats naturally track the component lifecycle:
After construction — parameters only After build( Inference ) — parameters + T=1 state buffers After build( Training ) — parameters + T=full state buffers After setEvaluation( false ) — parameters + state + gradients
For CompositeComponent and Network, the returned stats are the recursive aggregate of all child components.
May be called at any time — no lifecycle preconditions.
Implements Mila::Dnn::Component< TDeviceType, TPrecision >.
|
inlineoverrideexportvirtual |
Return non-owning pointers to parameter tensors.
Residual has no trainable parameter tensors by default; return empty list.
Implements Mila::Dnn::Component< TDeviceType, TPrecision >.
|
inlineoverrideexportvirtual |
Get the component type identifier.
Used for serialization and runtime type identification.
Implements Mila::Dnn::Component< TDeviceType, TPrecision >.
|
inlineoverrideexportprotectedvirtual |
Build the Residual component from the provided BuildContext.
Allocates the component-owned output buffer and, for Training-mode builds, the gradient buffers for both inputs.
The output buffer matches the full input shape. Residual is a pure elementwise addition with no sequence dimension concern — RuntimeMode does not influence output buffer allocation.
input_a_grad_ and input_b_grad_ are allocated only for RuntimeMode::Training builds. Both are the same shape as the input — Residual is elementwise addition and both inputs are always symmetric in shape.
| build_config | Full input shape and RuntimeMode for this build. |
Reimplemented from Mila::Dnn::Component< TDeviceType, TPrecision >.

|
inlineoverrideexportprotectedvirtual |
Hook invoked after ExecutionContext is set on the base Component.
Create the device-specific BinaryOperation backend via the OperationRegistry.
Reimplemented from Mila::Dnn::Component< TDeviceType, TPrecision >.

|
inlineoverrideexportprotectedvirtual |
Hook invoked when training mode is about to change.
Inform backend operation of the new training mode. When leaving training, explicitly unbind any parameter-gradient pointers on the backend to avoid accidental use or pinned memory.
Called with Component's training mutex held; do not call setTraining() here.
Reimplemented from Mila::Dnn::Component< TDeviceType, TPrecision >.
|
inlineoverrideexportvirtual |
Number of trainable parameters.
Residual has no trainable parameters.
Implements Mila::Dnn::Component< TDeviceType, TPrecision >.
|
inlineoverrideexportvirtual |
Serialize component parameters into the provided archive.
Placeholder; concrete implementations should write named parameter tensors into the archive.
Implements Mila::Dnn::Component< TDeviceType, TPrecision >.
|
inlineoverrideexportvirtual |
Block until all device operations submitted by this component complete.
| std::runtime_error | if ExecutionContext has not been set. |
Implements Mila::Dnn::Component< TDeviceType, TPrecision >.

|
inlineoverrideexportvirtual |
Return a human-readable description of the component.
Includes configured name, training/built state, backend presence, device information and parameter count to aid debugging and logging.
Implements Mila::Dnn::Component< TDeviceType, TPrecision >.
