|
Mila
Deep Neural Network Library
|
One Gemma 4 decoder block; kGlobal selects the global (full-attention) geometry. More...
Classes | |
| struct | BlockBuildContexts |
| The per-child build contexts and split-scratch geometry this block implies. More... | |
Public Types | |
| using | AttentionType = GroupedQueryAttention<TDeviceType, TPrecision, TKvPolicy> |
| using | CompositeComponentBase = CompositeComponent<TDeviceType, TPrecision> |
| using | GeGLUType = Swiglu<TDeviceType, TPrecision, ActivationType::Gelu> |
| using | LinearType = Linear<TDeviceType, TPrecision, TWeightQuant> |
| using | MR = typename DeviceTypeTraits<TDeviceType>::memory_resource |
| using | ResidualType = Residual<TDeviceType, TPrecision> |
| using | RmsNormType = RmsNorm<TDeviceType, TPrecision> |
| using | RopeType = Rope<TDeviceType, TPrecision> |
| using | TensorType = Tensor<TPrecision, MR> |
| Public Types inherited from Mila::Dnn::CompositeComponent< TDeviceType, TPrecision > | |
| using | ComponentBase = Component<TDeviceType, TPrecision> |
| using | ComponentPtr = std::shared_ptr<Component<TDeviceType, TPrecision>> |
| Public Types inherited from Mila::Dnn::IDecoderLayer< TDeviceType, TPrecision > | |
| using | MR = typename DeviceTypeTraits<TDeviceType>::memory_resource |
| using | TensorType = Tensor<TPrecision, MR> |
Public Member Functions | |
| GemmaBlock (const std::string &name, const GemmaConfig &config, std::optional< DeviceId > device_id=std::nullopt) | |
| TensorType & | decode (const TensorType &input, dim_t position) override |
| Single-token decode at an absolute position (T == 1). | |
| MemoryStats | getMemoryStats () const override |
| Return the current memory allocation breakdown for this component. | |
| std::vector< std::string > | getParameterNames () const override |
| List all available parameter names for this component. | |
| MemoryStats | getRequiredMemory (const BuildContext &context) const override |
| What onBuilding() would allocate for this context, without allocating. | |
| const ComponentType | getType () const override |
| Get the component type identifier. | |
| dim_t | headDim () const noexcept |
| void | installSharedWorkspace (const GemmaBlockWorkspace< TDeviceType, TPrecision > &workspace) |
| Install the transformer-owned shared activation workspace (pooling). | |
| bool | keyEqualsValue () const noexcept |
| dim_t | kvProjWidth () const noexcept |
| void | load_ (ModelArchive &archive, SerializationMode mode) override |
| Children through the base traversal, then this block's own layer_scalar. | |
| void | loadParameter (const std::string &name, const ITensorBlob &blob) override |
| Load the block's own parameters. | |
| dim_t | numKVHeads () const noexcept |
| dim_t | packedQKVWidth () const noexcept |
| TensorType & | prefill (const TensorType &input, dim_t position_offset) override |
| Chunked prefill: process [B, T_chunk, model_dim] at an absolute offset. | |
| dim_t | qProjWidth () const noexcept |
| void | resetKVCache () override |
| Reset the KV cache (new generation session). | |
| bool | rewindKvCache (dim_t position) override |
| Rewind the KV cache fill position for prompt-prefix reuse. | |
| float | ropeTheta () const noexcept |
| dim_t | rotaryDim () const noexcept |
| void | save_ (ModelArchive &archive, SerializationMode mode) const override |
| void | saveFlatTensors (Serialization::SafeTensorsWriter &writer, const std::string &prefix, Serialization::TensorSavePass pass) const override |
| Children through the base traversal, plus the block's own layer_scalar. | |
| void | setState (const GqaState &state) override |
| Wire the shared GQA transient workspace (owned by the transformer). | |
| void | setUseFlashDecode (bool enabled) |
| void | setUseFlashPrefill (bool enabled) |
| bool | supportsKVCache () const noexcept override |
| True when the block's attention supports the KV-cache inference path. | |
| dim_t | window () const noexcept |
| 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. | |
| 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 |
| dim_t | parameterCount () const override |
| Count parameters across all children. | |
| bool | removeComponent (const std::string &name) |
| 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 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. | |
| TrainingMode | getTrainingMode () const noexcept |
| The current runtime behavioral mode of this Component. | |
| virtual bool | isBuilt () const final |
| Returns true if build() has completed successfully. | |
| void | setTrainingMode (TrainingMode mode) |
| Set the runtime behavioral mode for this Component. | |
| virtual void | zeroGradients () |
| Clear all model-owned gradients for this component. | |
Static Public Member Functions | |
| static constexpr bool | isGlobal () noexcept |
| 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 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. | |
| BlockBuildContexts | resolveBlockBuildContexts (const BuildContext &context) const |
| 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. | |
| void | requireSerializableParameters () const override |
| No-op override: a composite names no parameters of its own. | |
| 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. | |
| template<TensorDataType TParameterPrecision, typename TMemoryResource> | |
| void | saveParameterToArchive (ModelArchive &archive, const std::string ¶meter_name, const Tensor< TParameterPrecision, TMemoryResource > ¶meter) 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 > ¶meter, 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. | |
Additional Inherited Members | |
| Protected Types inherited from Mila::Dnn::Component< TDeviceType, TPrecision > | |
| using | HostStagingMemoryResource |
| Host memory a device-resident parameter stages through. | |
| Protected Attributes inherited from Mila::Dnn::Component< TDeviceType, TPrecision > | |
| BuildContext | build_context_ { shape_t{ 1 }, RuntimeMode::Training } |
| The BuildContext stored at build time. | |
One Gemma 4 decoder block; kGlobal selects the global (full-attention) geometry.
|
inlineoverridevirtual |
Single-token decode at an absolute position (T == 1).
Implements Mila::Dnn::IDecoderLayer< TDeviceType, TPrecision >.
|
inlineoverridevirtual |
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 – nothing; construction allocates none After build( Inference ) – parameters + T=1 state buffers After build( Training ) – parameters + T=full state buffers After setTrainingMode( Train ) – 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 >.
|
inlineoverridevirtual |
List all available parameter names for this component.
Returns an empty vector by default. Leaf components with parameters should override to return their canonical parameter name list in the same stable order used by save_() and loadParameter().
Reimplemented from Mila::Dnn::Component< TDeviceType, TPrecision >.
|
inlineoverridevirtual |
What onBuilding() would allocate for this context, without allocating.
Children are named rather than walked, because each is built with its OWN context and a generic recursion over getComponents() would size every one of them against the block's shape. Children are fetched by name rather than read from the member pointers, which onBuilding() assigns and are therefore null before a build. See Specifications/MemoryFootprint.md section 4.4.
Reimplemented from Mila::Dnn::Component< TDeviceType, TPrecision >.
|
inlineoverridevirtual |
Get the component type identifier.
Used for serialization and runtime type identification.
Implements Mila::Dnn::Component< TDeviceType, TPrecision >.
|
inline |
Install the transformer-owned shared activation workspace (pooling).
One workspace serves every layer: the inference path is strictly sequential, so exactly one block is live at a time. Must be called before build(); onBuilding then routes each slot into the matching child component via installSharedOutput (and keeps the q/k/v split scratch for the block's own views), skipping all per-layer output self-allocation. Self-allocation remains the default for standalone blocks (tests).
|
inlineoverridevirtual |
Children through the base traversal, then this block's own layer_scalar.
CompositeComponent::load_ only recurses – the own-parameter half is Component's default, which a composite does not inherit – so both are driven explicitly.
Reimplemented from Mila::Dnn::CompositeComponent< TDeviceType, TPrecision >.
|
inlineoverridevirtual |
Load the block's own parameters.
Sub-component weights are routed by the reader directly to the sub-components; the only parameter owned by the block itself is the Gemma 4 Unified per-layer output scale layer_scalar (a [1] FP32 value).
Reimplemented from Mila::Dnn::Component< TDeviceType, TPrecision >.
|
inlineoverrideprotectedvirtual |
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.
Takes the build-time configuration; use its allocationSeqLen() to obtain the correct output buffer sequence dimension.
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 setTrainingMode().
| training_mode | New training mode (Normal or Eval) |
Reimplemented from Mila::Dnn::CompositeComponent< TDeviceType, TPrecision >.
|
inlineoverridevirtual |
Chunked prefill: process [B, T_chunk, model_dim] at an absolute offset.
Implements Mila::Dnn::IDecoderLayer< TDeviceType, TPrecision >.
|
inlineoverridevirtual |
Reset the KV cache (new generation session).
Implements Mila::Dnn::IDecoderLayer< TDeviceType, TPrecision >.
|
inlineoverridevirtual |
Rewind the KV cache fill position for prompt-prefix reuse.
Keeps the cache session live; positions [0, position) stay valid.
Implements Mila::Dnn::IDecoderLayer< TDeviceType, TPrecision >.
|
inlineoverridevirtual |
Reimplemented from Mila::Dnn::CompositeComponent< TDeviceType, TPrecision >.
|
inlineoverridevirtual |
Children through the base traversal, plus the block's own layer_scalar.
This block is the one composite in the tree that owns a parameter itself, so it needs both halves. The hand-rolled version this replaces did neither correctly: it re-implemented the base recursion without pushing a scope per child, so every child collided on one path, and it never wrote layer_scalar at all – a Gemma archive silently lost the per-layer output scales, which is a numerics change, not a missing extra.
Children through the base recursion, then this block's own layer_scalar.
CompositeComponent::saveFlatTensors only recurses – the own-parameter half is Component's default, which a composite overrides away – so a composite owning a parameter must drive both halves explicitly. Omitting this dropped all 48 layer_scalar tensors from a Gemma export while every structural check passed; they are in the converted .bin, so they are part of the flat vocabulary.
Order is identical across both passes, which the writer requires.
Reimplemented from Mila::Dnn::CompositeComponent< TDeviceType, TPrecision >.
|
inlineoverridevirtual |
Wire the shared GQA transient workspace (owned by the transformer).
Called once after build, before any prefill/decode.
Implements Mila::Dnn::IDecoderLayer< TDeviceType, TPrecision >.
|
inlineoverridevirtualnoexcept |
True when the block's attention supports the KV-cache inference path.
Implements Mila::Dnn::IDecoderLayer< TDeviceType, TPrecision >.