Mila 0.13.48
Deep Neural Network Library
Loading...
Searching...
No Matches
Mila::Dnn::BuildContext Class Referenceexport

Build-time context for Component::build(). More...

Collaboration diagram for Mila::Dnn::BuildContext:

Public Member Functions

 BuildContext ()
 Default constructor — sentinel value for pre-build state.
 BuildContext (shape_t input_shape, RuntimeMode runtime_mode, bool initialize_parameters=true)
 Construct from all six concerns explicitly.
int64_t getPrefillSize () const noexcept
 Number of tokens processed per prefill pass.
RuntimeMode getRuntimeMode () const noexcept
 Return a copy of this context with a different quantization config.
const shape_tinputShape () const noexcept
 The full input shape this component receives.
bool isInferenceMode () const noexcept
 True if output buffers should be allocated at T=1.
bool isTrainingMode () const noexcept
 True if output buffers should be allocated at full input shape sequence length with gradient buffers.
bool shouldInitializeParameters () const noexcept
 True if components should initialize parameter values after allocation.
BuildContext withPrefillSize (int64_t prefill_size) const
 Return a copy of this context with a different prefill size.
BuildContext withShape (shape_t new_shape) const
 Return a copy of this context with a different input shape.

Private Attributes

bool initialize_parameters_ { true }
shape_t input_shape_
 The weight storage dtype and scale allocation policy.
int64_t prefill_size_ { 0 }
RuntimeMode runtime_mode_

Detailed Description

Build-time context for Component::build().

Carries six orthogonal concerns down the Component hierarchy:

  1. Input shape — the full input shape the component receives. Used for parameter sizing, output buffer allocation, and build-time validation against component config.
  2. RuntimeMode — allocation policy governing output buffer sizing and gradient buffer allocation.

    Inference — T=1 decode path output buffers. Training — full sequence output buffers, gradient buffers allocated.

  3. Parameter initialization — whether components should initialize parameter tensors after allocation. Set to false when building for a pretrained weight load to avoid computing initializers (Xavier, normal, zeros) that are immediately overwritten by loadParameter(). Defaults to true (training from scratch).

Caller responsibility

The Network or Transformer constructing BuildContext is responsible for providing the correct full input shape for each child component. Each component validates this shape against its own config in onBuilding().

Threading

Not synchronized. Used only during the single-threaded build phase.

Examples
/__w/Mila/Mila/Mila/Src/Dnn/Components/Activations/Gelu/Gelu.ixx, and /__w/Mila/Mila/Mila/Src/Dnn/Components/Normalization/Softmax.ixx.

Constructor & Destructor Documentation

◆ BuildContext() [1/2]

Mila::Dnn::BuildContext::BuildContext ( )
inline

Default constructor — sentinel value for pre-build state.

Produces a minimal valid BuildContext with parameter initialization enabled, Auto precision policy, and no quantization. Never read before build() is called — Component::ensureBuilt() guards all access paths.

Here is the caller graph for this function:

◆ BuildContext() [2/2]

Mila::Dnn::BuildContext::BuildContext ( shape_t input_shape,
RuntimeMode runtime_mode,
bool initialize_parameters = true )
inlineexplicit

Construct from all six concerns explicitly.

precision_policy and quantization are extracted from ModelConfig by fromPretrained() and passed here as raw values, keeping BuildContext free of any model-layer dependency.

Parameters
input_shapeComplete input shape this component receives. Must have at least one dimension.
runtime_modeAllocation policy: Inference or Training.
initialize_parametersWhen false, components allocate parameter tensors but skip value initialization.
Exceptions
std::invalid_argumentif input_shape is empty.

Member Function Documentation

◆ getPrefillSize()

int64_t Mila::Dnn::BuildContext::getPrefillSize ( ) const
inlinenoexcept

Number of tokens processed per prefill pass.

The tuned prefill chunk size, computed once at network build time and threaded down to every component that sizes prefill buffers or attention scratch. Zero on training-mode contexts (no chunking).

Here is the caller graph for this function:

◆ getRuntimeMode()

RuntimeMode Mila::Dnn::BuildContext::getRuntimeMode ( ) const
inlinenoexcept

Return a copy of this context with a different quantization config.

All other parameters are preserved. Provided for sub-graphs that require a different quantization policy than the parent context.

Parameters
quantizationReplacement quantization config.
Returns
New BuildContext with quantization and all other fields unchanged.

The runtime mode governing output buffer allocation.

Here is the caller graph for this function:

◆ inputShape()

const shape_t & Mila::Dnn::BuildContext::inputShape ( ) const
inlinenoexcept

The full input shape this component receives.

Examples
/__w/Mila/Mila/Mila/Src/Dnn/Components/Activations/Gelu/Gelu.ixx, and /__w/Mila/Mila/Mila/Src/Dnn/Components/Normalization/Softmax.ixx.
Here is the caller graph for this function:

◆ isInferenceMode()

bool Mila::Dnn::BuildContext::isInferenceMode ( ) const
inlinenoexcept

True if output buffers should be allocated at T=1.

Here is the caller graph for this function:

◆ isTrainingMode()

bool Mila::Dnn::BuildContext::isTrainingMode ( ) const
inlinenoexcept

True if output buffers should be allocated at full input shape sequence length with gradient buffers.

Examples
/__w/Mila/Mila/Mila/Src/Dnn/Components/Activations/Gelu/Gelu.ixx.
Here is the caller graph for this function:

◆ shouldInitializeParameters()

bool Mila::Dnn::BuildContext::shouldInitializeParameters ( ) const
inlinenoexcept

True if components should initialize parameter values after allocation.

Here is the caller graph for this function:

◆ withPrefillSize()

BuildContext Mila::Dnn::BuildContext::withPrefillSize ( int64_t prefill_size) const
inlinenodiscard

Return a copy of this context with a different prefill size.

All other fields are preserved. Used by the network to stamp the tuned prefill chunk size onto the contexts it builds its child components with.

Parameters
prefill_sizeTokens per prefill pass.
Returns
New BuildContext with prefill_size and all other fields unchanged.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ withShape()

BuildContext Mila::Dnn::BuildContext::withShape ( shape_t new_shape) const
inlinenodiscard

Return a copy of this context with a different input shape.

All other fields are preserved — RuntimeMode, prefill_size, and initialize_parameters.

Parameters
new_shapeReplacement input shape. Must be non-empty.
Returns
New BuildContext with new_shape and all other fields unchanged.
Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ initialize_parameters_

bool Mila::Dnn::BuildContext::initialize_parameters_ { true }
private

◆ input_shape_

shape_t Mila::Dnn::BuildContext::input_shape_
private

The weight storage dtype and scale allocation policy.

Consumed by Linear::build() and Linear::loadParameters() only. All other components ignore this field.

◆ prefill_size_

int64_t Mila::Dnn::BuildContext::prefill_size_ { 0 }
private

◆ runtime_mode_

RuntimeMode Mila::Dnn::BuildContext::runtime_mode_
private

The documentation for this class was generated from the following file: