|
Mila 0.13.48
Deep Neural Network Library
|
A component that contains and manages child components. More...


Public Types | |
| using | ComponentBase = Component<TDeviceType, TPrecision> |
| using | ComponentPtr = std::shared_ptr<Component<TDeviceType, TPrecision>> |
Public Member Functions | |
| CompositeComponent (CompositeComponent &&) noexcept=default | |
| CompositeComponent (const CompositeComponent &)=delete | |
| CompositeComponent (const std::string &name) | |
| Construct composite component with name. | |
| virtual | ~CompositeComponent ()=default |
| CompositeComponent & | addComponent (ComponentPtr component) |
| Add a pre-constructed child component (chainable). | |
| size_t | childCount () const noexcept |
| Get the number of direct children. | |
| void | clearComponents () |
| Clear all child components. | |
| ComponentPtr | findComponent (const std::string &path) const |
| Resolve a dot-separated component path within this composite. | |
| ComponentPtr | getComponent (const std::string &name) const |
| Retrieve a direct child component by name. | |
| const std::vector< ComponentPtr > & | getComponents () const |
| Get all child components in insertion order. | |
| DeviceId | getDeviceId () const override |
| Get the compute device for this composite. | |
| std::vector< ITensor * > | getGradients () const override |
| Get all parameter gradients from all children. | |
| std::vector< ITensor * > | getParameters () const override |
| Get all parameters from all children. | |
| bool | hasChildren () const noexcept |
| Check if this composite has any children. | |
| bool | hasComponent (const std::string &name) const |
| Check if a named child component exists. | |
| CompositeComponent & | operator= (CompositeComponent &&) noexcept=default |
| CompositeComponent & | operator= (const CompositeComponent &)=delete |
| size_t | parameterCount () const override |
| Count parameters across all children. | |
| bool | removeComponent (const std::string &name) |
| Get the named child components map. | |
| void | synchronize () override |
| Synchronize all child components. | |
| std::string | toString () const override |
| Generate a human-readable description. | |
| ComponentPtr | tryFindComponent (const std::string &path) const |
| Try to resolve a dot-separated component path within this composite. | |
| 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). | |
| 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. | |
| 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 const ComponentType | getType () const =0 |
| Get the component type identifier. | |
| 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 | |
| template<typename TComponent> | |
| std::shared_ptr< TComponent > | getComponentAs (const std::string &name) const |
| Retrieve a typed child component by name. | |
| void | onExecutionContextSet () override |
| Hook invoked after ExecutionContext is set. | |
| void | onTrainingModeChanging (TrainingMode training_mode) override |
| Hook invoked when training mode is about to change. | |
| virtual void | optimize () |
| Virtual hook for graph optimization after construction. | |
| void | save_ (ModelArchive &archive, SerializationMode mode) const override |
| Save all child components recursively. | |
| 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. | |
| virtual void | onBuilding (const BuildContext &config) |
| Hook invoked by build() to allocate component buffers. | |
| void | setExecutionContext (IExecutionContext *context) |
| Set the execution context for this component. | |
Private Attributes | |
| std::unordered_map< std::string, ComponentPtr, std::hash< std::string_view >, std::equal_to<> > | child_component_map_ |
| Lookup map from child name to component pointer. | |
| std::vector< ComponentPtr > | child_components_ |
| Child components in insertion order. | |
Friends | |
| class | ComponentFactory |
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. | |
A component that contains and manages child components.
CompositeComponent is a device-parameterized abstract container that manages child component lifecycle, aggregates operations (parameters, gradients, training mode), and provides context propagation. Derived types implement execution semantics (forward/backward) and architecture definition (createGraph()).
Architecture Philosophy:
NOTE:
| using Mila::Dnn::CompositeComponent< TDeviceType, TPrecision >::ComponentBase = Component<TDeviceType, TPrecision> |
| using Mila::Dnn::CompositeComponent< TDeviceType, TPrecision >::ComponentPtr = std::shared_ptr<Component<TDeviceType, TPrecision>> |
|
inlineexplicit |
Construct composite component with name.
The composite component is named for identification in hierarchical structures. Derived classes should call createGraph() from their constructor to define the architecture graph (context-independent).
All child components added via addComponent() will receive ExecutionContext automatically when the composite receives its context (via onExecutionContextSet).
| name | Component name identifier (mandatory) |
| std::invalid_argument | if name is not a valid identifier |

|
virtualdefault |
|
delete |

|
defaultnoexcept |

|
inline |
Add a pre-constructed child component (chainable).
Registers a component that was constructed externally (typically by the derived class in its createGraph() method). The component's getName() is used as the lookup key.
Components are expected to be created in shared mode (no ExecutionContext). Context will be automatically propagated to all children when this composite receives its context via onExecutionContextSet().
Usage pattern in derived class:
| component | Shared pointer to the constructed component |
| std::runtime_error | if called after build() |
| std::invalid_argument | if component is null |
| std::invalid_argument | if component name already exists |
| std::invalid_argument | if component already has its own ExecutionContext |


|
inlinenoexcept |
Get the number of direct children.

|
inline |
Clear all child components.
| std::runtime_error | if called after build() |

|
inline |
Resolve a dot-separated component path within this composite.
Supports both relative paths ("lenc.wte") and absolute paths ("gpt2.lenc.wte"). If path starts with this component's name, strips it before searching.


|
inline |
Retrieve a direct child component by name.
This performs direct (non-recursive) lookup of immediate children only. Use findComponent() to resolve dot-separated paths across the subgraph.
| name | Name of the direct child component |
| std::out_of_range | if the direct child is not found |

|
inlineprotected |
Retrieve a typed child component by name.
Helper method for derived composites (like MLP) that need to cache typed pointers to children in their onBuilding() hook. Performs dynamic_pointer_cast and validates the cast succeeded.
Note: This resolves direct children only. For full-path resolution use findComponent() on the appropriate root composite or Network.
| TComponent | Expected component type |
| name | Name of the direct child component |
| std::out_of_range | if component name not found |
| std::runtime_error | if dynamic cast fails (type mismatch) |


|
inline |
Get all child components in insertion order.

|
inlineoverridevirtual |
Get the compute device for this composite.
Returns the device from the shared execution context.
Implements Mila::Dnn::Component< TDeviceType, TPrecision >.
Reimplemented in Mila::Dnn::Network< TDeviceType, TPrecision >.


|
inlineoverridevirtual |
Get all parameter gradients from all children.
| std::runtime_error | if called before build() or not in training mode |
Implements Mila::Dnn::Component< TDeviceType, TPrecision >.


|
inlineoverridevirtual |
Get all parameters from all children.
| std::runtime_error | if called before build() |
Implements Mila::Dnn::Component< TDeviceType, TPrecision >.


|
inlinenoexcept |
Check if this composite has any children.
|
inline |
Check if a named child component exists.
| name | Name to query |
|
inlineoverrideprotectedvirtual |
Hook invoked after ExecutionContext is set.
Propagates the execution context to all child components that don't already have one. This enables the pattern where composites define their architecture graph in the constructor (context-independent) and context is bound later when available.
Called by Component::setExecutionContext() after the context is registered. Automatically invoked for both standalone mode (component creates own context) and shared mode (parent provides context).
Override this in derived classes if additional context-dependent initialization is required beyond context propagation to children.
Reimplemented from Mila::Dnn::Component< TDeviceType, TPrecision >.

|
inlineoverrideprotectedvirtual |
Hook invoked when training mode is about to change.
Propagates the new mode to all child components. The hook runs with the Component's training mutex held; it MUST NOT call setTraining().
| is_training | New training mode (true = training, false = eval) |
Reimplemented from Mila::Dnn::Component< TDeviceType, TPrecision >.
Reimplemented in Mila::Dnn::GptBlock< TDeviceType, TPrecision >, Mila::Dnn::GptTransformer< TDeviceType, TPrecision >, Mila::Dnn::LlamaBlock< TDeviceType, TPrecision, TWeightQuant, TKvPolicy >, Mila::Dnn::LlamaBlock< TDeviceType, TPrecision, TWeightQuantization, TKvCachePolicy >, Mila::Dnn::LlamaTransformer< TDeviceType, TPrecision, TWeightQuantization, TKvCachePolicy >, and Mila::Dnn::MLP< TDeviceType, TPrecision >.
|
defaultnoexcept |

|
delete |

|
inlineprotectedvirtual |
Virtual hook for graph optimization after construction.
Called automatically after createGraph() completes. Derived classes can override to perform fusion, pruning, or other optimizations.
Default implementation does nothing. Override to perform architecture-specific graph optimizations.

|
inlineoverridevirtual |
Count parameters across all children.
| std::runtime_error | if called before build() |
Implements Mila::Dnn::Component< TDeviceType, TPrecision >.


|
inline |
Get the named child components map.
Remove a child component by name.
| name | Name of the component to remove |
| std::runtime_error | if called after build() |

|
inlineoverrideprotectedvirtual |
Save all child components recursively.
Follows the component serialization contract:
| archive | Archive to write to |
| mode | What to save (Checkpoint, WeightsOnly, Architecture) |
Implements Mila::Dnn::Component< TDeviceType, TPrecision >.
Reimplemented in Mila::Dnn::GptBlock< TDeviceType, TPrecision >, Mila::Dnn::GptTransformer< TDeviceType, TPrecision >, Mila::Dnn::LlamaBlock< TDeviceType, TPrecision, TWeightQuant, TKvPolicy >, Mila::Dnn::LlamaBlock< TDeviceType, TPrecision, TWeightQuantization, TKvCachePolicy >, Mila::Dnn::LlamaTransformer< TDeviceType, TPrecision, TWeightQuantization, TKvCachePolicy >, Mila::Dnn::MLP< TDeviceType, TPrecision >, and Mila::Dnn::Network< TDeviceType, TPrecision >.

|
inlineoverridevirtual |
Synchronize all child components.
Waits for outstanding device operations on all children.
Implements Mila::Dnn::Component< TDeviceType, TPrecision >.
Reimplemented in Mila::Dnn::Network< TDeviceType, TPrecision >.

|
inlineoverridevirtual |
Generate a human-readable description.
Implements Mila::Dnn::Component< TDeviceType, TPrecision >.
Reimplemented in Mila::Dnn::GptBlock< TDeviceType, TPrecision >, Mila::Dnn::GptTransformer< TDeviceType, TPrecision >, Mila::Dnn::LlamaTransformer< TDeviceType, TPrecision, TWeightQuantization, TKvCachePolicy >, Mila::Dnn::MLP< TDeviceType, TPrecision >, and Mila::Dnn::Network< TDeviceType, TPrecision >.


|
inline |
Try to resolve a dot-separated component path within this composite.
Non-throwing version: returns nullptr if any segment is not found or if a path segment attempts to traverse into a non-composite leaf.
Example: auto ptr = composite->tryFindComponent("encoder.mlp.fc1");
| path | Dot-separated path (e.g. "layer_0.mlp.fc_1") |
|
friend |
|
private |
Lookup map from child name to component pointer.
Provides O(1) name-based lookup for children. Keys are component names (obtained via component->getName()) and must be unique within the composite.
Used for:
Note: insertion order is preserved by child_components_; this unordered_map does not guarantee ordering but provides fast lookup.
|
private |
Child components in insertion order.
This vector holds shared ownership of the composite's direct children and preserves the order in which children were added. The insertion order is used for build sequencing, ordered iteration, and serialization ordering.
Lifecycle invariants:
Threading: access is not internally synchronized. Mutations and lifecycle operations must be externally serialized.