|
Mila 0.13.48
Deep Neural Network Library
|
LLaMA-style transformer (decoder-only) for autoregressive token prediction. More...


Public Types | |
| using | ComponentPtr = typename NetworkBase::ComponentPtr |
| using | LinearType = Linear<TDeviceType, TPrecision, TWeightQuantization> |
| using | LmHeadLinearType = Linear<TDeviceType, TPrecision> |
| using | MR = typename DeviceTypeTraits<TDeviceType>::memory_resource |
| using | NetworkBase = LanguageNetwork<TDeviceType, TPrecision> |
| using | RmsNormType = RmsNorm<TDeviceType, TPrecision> |
| using | TensorType = Tensor<TPrecision, MR> |
| using | TokenEmbeddingType = TokenEmbedding<TDeviceType, dtype_t::INT32, TPrecision> |
| using | TokenIndexType = Tensor<dtype_t::INT32, MR> |
| using | TransformerBlockType = LlamaBlock<TDeviceType, TPrecision, TWeightQuantization, TKvCachePolicy> |
| Public Types inherited from Mila::Dnn::LanguageNetwork< TDeviceType, TPrecision > | |
| using | MR = typename DeviceTypeTraits<TDeviceType>::memory_resource |
| using | NetworkBase = Network<TDeviceType, TPrecision> |
| using | TensorType = Tensor<TPrecision, MR> |
| using | TokenIndexType = Tensor<TensorDataType::INT32, MR> |
| Public Types inherited from Mila::Dnn::Network< TDeviceType, TPrecision > | |
| using | ComponentPtr = typename CompositeBase::ComponentPtr |
| using | CompositeBase = CompositeComponent<TDeviceType, TPrecision> |
| using | MR = typename DeviceTypeTraits<TDeviceType>::memory_resource |
| Public Types inherited from Mila::Dnn::CompositeComponent< TDeviceType, TPrecision > | |
| using | ComponentBase = Component<TDeviceType, TPrecision> |
| using | ComponentPtr = std::shared_ptr<Component<TDeviceType, TPrecision>> |
Public Member Functions | |
| LlamaTransformer (const std::string &name, const LlamaConfig &config, DeviceId device_id) | |
| ~LlamaTransformer () override=default | |
| TokenIndexType & | backward (const TokenIndexType &input, const TensorType &output_grad) override |
| TensorType & | decode (const TokenIndexType &input, int position) override |
| TensorType & | forward (const TokenIndexType &input) override |
| IExecutionContext * | getExecutionContext () const |
| MemoryStats | getMemoryStats () const override |
| Return the current memory allocation breakdown for this component. | |
| const ComponentType | getType () const override |
| Get the component type identifier. | |
| void | loadParameters (PretrainedModelReader &reader) |
| TensorType & | prefill (const TokenIndexType &input) override |
| std::string | toString () const override |
| Generate a human-readable description. | |
| void | zeroGradients () override |
| Clear all model-owned gradients for this component. | |
| Public Member Functions inherited from Mila::Dnn::LanguageNetwork< TDeviceType, TPrecision > | |
| LanguageNetwork (const std::string &name) | |
| ~LanguageNetwork () override=default | |
| virtual TokenIndexType & | backward (const TokenIndexType &input, const TensorType &output_grad)=0 |
| Full backward pass (training). | |
| virtual TensorType & | decode (const TokenIndexType &input, int position)=0 |
| Inference decode — single-token autoregressive step. | |
| virtual TensorType & | forward (const TokenIndexType &input)=0 |
| Full-sequence forward pass. | |
| virtual TensorType & | prefill (const TokenIndexType &input)=0 |
| Inference prefill — process full prompt and populate the KV cache. | |
| Public Member Functions inherited from Mila::Dnn::Network< TDeviceType, TPrecision > | |
| Network (const std::string &name) | |
| Construct network (context managed by derived class). | |
| ~Network () override=default | |
| template<typename TOptimizer, typename TConfig> | |
| std::shared_ptr< TOptimizer > | createOptimizer (const TConfig &config) |
| Create and configure an optimizer for this network's parameters. | |
| DeviceId | getDeviceId () const noexcept |
| Get the compute device for this composite. | |
| const ComponentType | getType () const override |
| Get the component type identifier. | |
| void | save (ModelArchive &archive, SerializationMode mode) const |
| Save network to archive. | |
| void | synchronize () override |
| Synchronize all child components. | |
| std::string | toString () const override |
| Generate a human-readable description. | |
| Public Member Functions inherited from Mila::Dnn::CompositeComponent< TDeviceType, TPrecision > | |
| 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. | |
| 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. | |
| 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). | |
| 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. | |
Protected Member Functions | |
| void | onBuilding (const BuildContext &context) override |
| Hook invoked by build() to allocate component buffers. | |
| void | onTrainingModeChanging (TrainingMode training_mode) override |
| Hook invoked when training mode is about to change. | |
| void | save_ (ModelArchive &archive, SerializationMode) const override |
| Hook for concrete classes to save type-specific state. | |
| Protected Member Functions inherited from Mila::Dnn::Network< TDeviceType, TPrecision > | |
| void | verifyArchitectureCompatibility (const PretrainedMetadata &metadata) |
| Verify that imported model is compatible with network architecture. | |
| Protected Member Functions inherited from Mila::Dnn::CompositeComponent< TDeviceType, TPrecision > | |
| 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. | |
| virtual void | optimize () |
| Virtual hook for graph optimization after construction. | |
| 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 Member Functions | |
| void | createGraph () |
| std::pair< std::string, std::string > | parseParameterPath (const std::string &full_name) const |
| void | validateBuildContext (const BuildContext &context) const |
| void | validateLeadingShape (const shape_t &leading_shape) const |
Static Private Member Functions | |
| static LlamaConfig | createConfigFromMetadata (const PretrainedMetadata &metadata) |
Private Attributes | |
| int64_t | batch_size_ { 0 } |
| std::vector< TensorType * > | block_input_ptrs_ |
| std::vector< TensorType * > | block_output_ptrs_ |
| LlamaConfig | config_ |
| shape_t | embedding_shape_ |
| std::unique_ptr< IExecutionContext > | exec_context_ { nullptr } |
| std::shared_ptr< RmsNormType > | final_rmsnorm_ { nullptr } |
| std::unique_ptr< TensorType > | gqa_att_ { nullptr } |
| std::unique_ptr< TensorType > | gqa_att_decode_ { nullptr } |
| std::unique_ptr< TensorType > | gqa_preatt_ { nullptr } |
| std::unique_ptr< TensorType > | gqa_preatt_decode_ { nullptr } |
| std::unique_ptr< TensorType > | gqa_q_permute_ { nullptr } |
| std::unique_ptr< TensorType > | gqa_v_out_ { nullptr } |
| std::unique_ptr< TensorType > | gqa_v_out_decode_ { nullptr } |
| shape_t | input_shape_ |
| std::shared_ptr< LmHeadLinearType > | lm_head_ { nullptr } |
| TensorType * | logits_ptr_ { nullptr } |
| TensorType * | normalized_ptr_ { nullptr } |
| shape_t | output_shape_ |
| std::unique_ptr< TensorType > | prefill_ { nullptr } |
| int64_t | prefill_chunk_size_ { 0 } |
| int64_t | seq_length_ { 0 } |
| TensorType * | token_embed_out_ptr_ { nullptr } |
| std::shared_ptr< TokenEmbeddingType > | token_embedding_ { nullptr } |
| std::vector< std::shared_ptr< TransformerBlockType > > | transformer_blocks_ |
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. | |
LLaMA-style transformer (decoder-only) for autoregressive token prediction.
Graph: TokenEmbedding → RoPE → LlamaBlock × N → RmsNorm → Linear (lm_head). RoPE is applied to the full embedding stream after the token lookup; each LlamaBlock receives rotary-encoded embeddings as input.
Template parameters:
|
inlineexplicitexport |

|
overrideexportdefault |
|
inlineoverrideexport |


|
inlinestaticexportprivate |

|
inlineexportprivate |


|
inlineoverrideexport |
|
inlineoverrideexport |

|
inlineexport |


|
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 |
Get the component type identifier.
Used for serialization and runtime type identification.
Implements Mila::Dnn::Component< TDeviceType, TPrecision >.
|
inlineexport |

|
inlineoverrideexportprotectedvirtual |
Hook invoked by build() to allocate component buffers.
Receives the stored BuildContext. Implementations must use config.allocationSeqLen() when sizing output buffers — this is the single call that makes Inference and Training allocate the correct buffer sizes automatically without per-component logic.
The default implementation forwards to the legacy onBuilding( const shape_t& ) overload for backwards compatibility. New components should override this overload directly.
| config | Build-time configuration. Use config.allocationSeqLen() to obtain the correct output buffer sequence dimension. |
Reimplemented from Mila::Dnn::Component< TDeviceType, TPrecision >.

|
inlineoverrideexportprotectedvirtual |
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::CompositeComponent< TDeviceType, TPrecision >.

|
inlineexportprivate |

|
inlineoverrideexport |

|
inlineoverrideexportprotectedvirtual |
Hook for concrete classes to save type-specific state.
REQUIRED override for concrete networks. Must write:
This metadata enables the concrete class's Load() method to reconstruct the network.
Example implementation:
| archive | Archive to write to |
| mode | Serialization mode (passed from save()) |
Implements Mila::Dnn::Network< TDeviceType, TPrecision >.

|
inlineoverrideexportvirtual |
Generate a human-readable description.
Reimplemented from Mila::Dnn::CompositeComponent< TDeviceType, TPrecision >.

|
inlineexportprivate |


|
inlineexportprivate |

|
inlineoverrideexportvirtual |
Clear all model-owned gradients for this component.
Default implementation is a no-op. Composite components should override to recurse to children. Leaf components should override to zero their parameter and activation gradients using device-aware helpers.
Reimplemented from Mila::Dnn::Component< TDeviceType, TPrecision >.
