|
Mila
Deep Neural Network Library
|
Build-time context for Component::build(). More...
Public Member Functions | |
| BuildContext () | |
| Default constructor – sentinel value for pre-build state. | |
| BuildContext (shape_t input_shape, RuntimeMode runtime_mode, std::optional< bool > initialize_parameters=std::nullopt) | |
| Construct from all six concerns explicitly. | |
| int64_t | getPrefillSize () const noexcept |
| Number of tokens processed per prefill pass. | |
| RuntimeMode | getRuntimeMode () const noexcept |
| The runtime mode governing output buffer allocation. | |
| bool | hasInstalledOutput () const noexcept |
| True if the caller will install this component's output buffer. | |
| const shape_t & | inputShape () 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 | withInstalledOutput (bool installed) const |
| Declare that the caller will install this component's output buffer. | |
| 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. | |
Build-time context for Component::build().
Carries six orthogonal concerns down the Component hierarchy:
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.
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().
Not synchronized. Used only during the single-threaded build phase.
|
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.
|
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.
| input_shape | Complete input shape this component receives. Must have at least one dimension. |
| runtime_mode | Allocation policy: Inference or Training. |
| initialize_parameters | When false, components allocate parameter tensors but skip value initialization. When omitted (nullopt), the default is derived from runtime_mode – Training initializes, Inference skips – so a load path cannot regress by forgetting the flag. |
| std::invalid_argument | if input_shape is empty. |
|
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).
|
inlinenodiscard |
Declare that the caller will install this component's output buffer.
A composite that pools activations installs a shared slot into each child before calling build(), so the child skips self-allocating its output. That decision is invisible to getRequiredMemory(), which runs before any installation has happened and would otherwise count a buffer the build never allocates – once in the child and again in the pooling parent.
Only prediction reads this; onBuilding() continues to use the component's own installed flag, which by then is accurate.
|
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.
| prefill_size | Tokens per prefill pass. |
|
inlinenodiscard |
Return a copy of this context with a different input shape.
All other fields are preserved – RuntimeMode, prefill_size, and initialize_parameters.
| new_shape | Replacement input shape. Must be non-empty. |