|
Mila
Deep Neural Network Library
|
Gaussian Error Linear Unit (GELU) activation component. More...
Public Types | |
| using | ComponentBase = Component<TDeviceType, TPrecision> |
| using | MR = typename DeviceTypeTraits<TDeviceType>::memory_resource |
| using | TensorType = Tensor<TPrecision, MR> |
Public Member Functions | |
| Gelu (const std::string &name, const GeluConfig &config, std::optional< DeviceId > device_id=std::nullopt) | |
| Construct GELU activation component with optional ExecutionContext ownership. | |
| TensorType & | backward (const TensorType &input, const TensorType &output_grad) |
| Compute gradients with respect to the component input. | |
| TensorType & | forward (const TensorType &input) |
| Run the forward computation for this GELU component. | |
| ApproximationMethod | getApproximationMethod () const |
| Return the configured GELU approximation method. | |
| DeviceId | getDeviceId () const override |
| Get the device identifier for this component. | |
| std::vector< ITensor * > | getGradients () const override |
| Get parameter gradient tensors. | |
| MemoryStats | getMemoryStats () const override |
| Return memory allocation breakdown. | |
| std::vector< ITensor * > | getParameters () const override |
| Get trainable parameter tensors. | |
| const ComponentType | getType () const override |
| Get the component type identifier. | |
| dim_t | parameterCount () const override |
| Number of trainable parameters. | |
| void | save_ (ModelArchive &archive, SerializationMode mode) const override |
| Persist component state to archive. | |
| void | synchronize () override |
| Wait for all asynchronous work submitted by this component to complete. | |
| std::string | toString () const override |
| Generate human-readable description of the component. | |
| 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. | |
| virtual std::vector< std::string > | getParameterNames () const |
| List all available parameter names for this component. | |
| virtual MemoryStats | getRequiredMemory (const BuildContext &context) const |
| Report what build( context ) would allocate, without allocating it. | |
| TrainingMode | getTrainingMode () const noexcept |
| The current runtime behavioral mode of this Component. | |
| virtual bool | isBuilt () const final |
| Returns true if build() has completed successfully. | |
| virtual void | load_ (ModelArchive &archive, SerializationMode mode) |
| Restore this component's parameters from its archive scope. | |
| virtual void | loadParameter (const std::string &, const Serialization::ITensorBlob &) |
| Load a parameter from serialized tensor data. | |
| virtual void | requireSerializableParameters () const |
| Verify this component can serialize whatever parameters it owns. | |
| virtual void | saveFlatTensors (Serialization::SafeTensorsWriter &writer, const std::string &prefix, Serialization::TensorSavePass pass) const |
| Drive this component's tensors through one pass of a flat safetensors save. | |
| 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 std::unique_ptr< Gelu > | fromArchive_ (ModelArchive &archive, const std::string &component_name, IExecutionContext *exec_context) |
| 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 &build_context) override |
| Hook invoked during build() to initialize backend operation. | |
| void | onExecutionContextSet () override |
| Hook invoked after ExecutionContext is set. | |
| void | onTrainingModeChanging (TrainingMode training_mode) override |
| Hook invoked when training mode changes. | |
| 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. | |
Gaussian Error Linear Unit (GELU) activation component.
Device-templated GELU component that performs forward (and optionally backward) computation by delegating to a registered device-specific UnaryOperation implementation found via the OperationRegistry.
| TDeviceType | Compile-time device identifier (DeviceType::Cpu or DeviceType::Cuda). |
| TPrecision | Tensor data precision used by this component. |
Construction Modes:
Ownership:
Preconditions:
Behavior:
Threading / Synchronization:
|
inlineexplicit |
Construct GELU activation component with optional ExecutionContext ownership.
Supports two construction modes:
Standalone mode (device_id provided):
Shared mode (device_id not provided):
| name | Component name identifier (mandatory). |
| config | GELU configuration (approximation method). |
| device_id | Optional device identifier. If provided, creates owned ExecutionContext for standalone mode. If nullopt, expects shared context from parent. |
| std::invalid_argument | if config is invalid (via config.validate()). |
| std::invalid_argument | if device_id.type does not match TDeviceType. |
| std::runtime_error | if ExecutionContext creation fails (standalone mode). |
| std::runtime_error | if backend operation creation fails in onExecutionContextSet(). |
|
inline |
Compute gradients with respect to the component input.
Delegates to the backend UnaryOperation::backward implementation to compute the gradient of GELU with respect to the input. The component owns the input gradient buffer which is allocated during onBuilding().
The gradient computation follows the chain rule: dL/dinput = dL/doutput * dGELU(input)/dinput
| input | Const reference to the original forward input. |
| output_grad | Const reference to the gradient w.r.t. component output (?L/?output). |
| std::runtime_error | if component has not been built via build(). |
| std::runtime_error | if component is not in training mode. |
| std::runtime_error | if operation backend is not initialized. |
|
inline |
Run the forward computation for this GELU component.
Dispatches to the device-specific UnaryOperation backend via a cached output view. The view is pre-initialized at build time and rebuilt only when the input shape changes (e.g. prefill vs decode). Zero heap allocation in steady-state.
| input | Const reference to the input tensor. |
| std::runtime_error | if component has not been built via build(). |
|
inline |
Return the configured GELU approximation method.
|
inlineoverridevirtual |
Get the device identifier for this component.
Returns the DeviceId from the ExecutionContext. In standalone mode, this is the device specified at construction. In shared mode, this is the parent's device.
| std::runtime_error | if ExecutionContext has not been set. |
Implements Mila::Dnn::Component< TDeviceType, TPrecision >.
|
inlineoverridevirtual |
Get parameter gradient tensors.
GELU has no trainable parameters, therefore no gradients.
Implements Mila::Dnn::Component< TDeviceType, TPrecision >.
|
inlineoverridevirtual |
Return memory allocation breakdown.
Implements Mila::Dnn::Component< TDeviceType, TPrecision >.
|
inlineoverridevirtual |
Get trainable parameter tensors.
GELU has no trainable parameters.
Implements Mila::Dnn::Component< TDeviceType, TPrecision >.
|
inlineoverridevirtual |
Get the component type identifier.
Used for serialization and runtime type identification.
Implements Mila::Dnn::Component< TDeviceType, TPrecision >.
|
inlineoverrideprotectedvirtual |
Hook invoked during build() to initialize backend operation.
Delegates shape-dependent initialization to the backend UnaryOperation. Must be called before forward() or backward().
State guards:
| build_context | Build-time context carrying the expected input shape. |
| std::invalid_argument | if the input shape is incompatible with the component configuration. |
| std::runtime_error | if backend allocation or build fails. |
| std::runtime_error | if operation is not initialized. |
Reimplemented from Mila::Dnn::Component< TDeviceType, TPrecision >.
|
inlineoverrideprotectedvirtual |
Hook invoked after ExecutionContext is set.
Called by Component::setExecutionContext() after the context is registered. Creates the backend UnaryOperation using the OperationRegistry.
This hook is triggered in two scenarios:
State guards:
| std::runtime_error | if operation creation fails. |
| std::runtime_error | if "GeluOp" is not registered for this device/precision. |
Reimplemented from Mila::Dnn::Component< TDeviceType, TPrecision >.
|
inlineoverrideprotectedvirtual |
Hook invoked when training mode changes.
Propagates training mode to the backend operation. Called by Component::setTrainingMode() with the training mutex held.
State guards:
| training_mode | New training mode state. |
Reimplemented from Mila::Dnn::Component< TDeviceType, TPrecision >.
|
inlineoverridevirtual |
Number of trainable parameters.
GELU is stateless and exposes no trainable parameters.
Implements Mila::Dnn::Component< TDeviceType, TPrecision >.
|
inlineoverridevirtual |
Persist component state to archive.
GELU is stateless (no trainable tensors) but persists:
Files written:
| archive | Archive to write to. |
| mode | Serialization mode (currently unused, all state is always saved). |
Implements Mila::Dnn::Component< TDeviceType, TPrecision >.
|
inlineoverridevirtual |
Wait for all asynchronous work submitted by this component to complete.
Synchronizes the underlying ExecutionContext. On CPU implementations this may be a no-op. Use to ensure results are visible on the host or to measure synchronous timings.
| std::runtime_error | if ExecutionContext has not been set. |
Implements Mila::Dnn::Component< TDeviceType, TPrecision >.
|
inlineoverridevirtual |
Generate human-readable description of the component.
Produces a multi-line string showing:
Implements Mila::Dnn::Component< TDeviceType, TPrecision >.