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

CPU implementation of Softmax using abstract TensorDataType API. More...

Inheritance diagram for Mila::Dnn::Compute::CpuSoftmaxOp:
Collaboration diagram for Mila::Dnn::Compute::CpuSoftmaxOp:

Public Types

using CpuExecutionContext = ExecutionContext<DeviceType::Cpu>
using MR = CpuMemoryResource
using TensorType = Tensor<TensorDataType::FP32, MR>
using UnaryOperationBase = UnaryOperation<DeviceType::Cpu, TensorDataType::FP32>
Public Types inherited from Mila::Dnn::Compute::UnaryOperation< DeviceType::Cpu, TensorDataType::FP32 >
using MR
using TensorInputType
using TensorOutputType
Public Types inherited from Mila::Dnn::Compute::Operation< TDeviceType, TInput >
using DataTypeTraits

Public Member Functions

 CpuSoftmaxOp (IExecutionContext *context, const SoftmaxConfig &config)
void backward (const ITensor &input, const ITensor &output_grad, ITensor &input_grad) const override
 Backward pass - HOT PATH, pure dispatch to CPU kernel.
void build (const BuildContext &config) override
 Build the operation for a concrete input shape.
void forward (const ITensor &input, ITensor &output) const override
 Forward pass - HOT PATH, pure dispatch to CPU kernel.
const SoftmaxConfiggetConfig () const
std::string getName () const override
 Human-readable operation name.
OperationType getOperationType () const override
 Operation type identifier.
void setParameters (ITensor *weight, ITensor *bias) override
 Set parameter tensor references (no-op for Softmax - stateless operation).
Public Member Functions inherited from Mila::Dnn::Compute::UnaryOperation< DeviceType::Cpu, TensorDataType::FP32 >
virtual ~UnaryOperation ()=default
Public Member Functions inherited from Mila::Dnn::Compute::Operation< TDeviceType, TInput >
virtual ~Operation ()=default
virtual void clearGradients () noexcept
 Clear any cached gradient pointers held by the operation.
virtual TensorDataType getDataType () const
 Tensor data type for this operation.
virtual DeviceType getDeviceType () const
 Device type for this operation.
virtual std::size_t getStateMemorySize () const
 Returns the number of bytes of state memory allocated by this operation.
virtual bool isBuilt () const
 Whether build() completed successfully for a concrete input shape.
virtual bool isEvalMode () const
 Query whether operation is configured for training.
virtual void setGradients (ITensor *weight_grad, ITensor *bias_grad)
 Bind module-owned gradient tensors to the operation.
virtual void setTrainingMode (TrainingMode training_mode)
 Configure operation training-mode behavior.

Private Attributes

int64_t cached_axis_ { -1 }
int64_t cached_dim_size_ { 0 }
int64_t cached_inner_size_ { 0 }
int64_t cached_outer_size_ { 0 }
SoftmaxConfig config_
IExecutionContextcontext_
bool enable_omp_ { false }

Additional Inherited Members

Static Public Attributes inherited from Mila::Dnn::Compute::Operation< TDeviceType, TInput >
static constexpr TensorDataType data_type
static constexpr DeviceType device_type
Static Protected Member Functions inherited from Mila::Dnn::Compute::UnaryOperation< DeviceType::Cpu, TensorDataType::FP32 >
static const TensorInputTypeasInputTensor (const ITensor &t)
static TensorOutputTypeasOutputTensor (ITensor &t)
Protected Attributes inherited from Mila::Dnn::Compute::Operation< TDeviceType, TInput >
bool is_built_
TrainingMode training_mode_

Detailed Description

CPU implementation of Softmax using abstract TensorDataType API.

Template parameter TPrecision selects the abstract tensor precision (e.g. FP32). float is the corresponding CPU host representation for that precision.

Design philosophy:

  • Two-phase initialization: build() does all setup, forward()/backward() are pure dispatch
  • Softmax has no trainable parameters (stateless operation)
  • All dimension computation happens once in build()
  • Forward/backward are hot-path methods with minimal overhead
  • Uses numerically-stable softmax: exp(x - max(x))

Member Typedef Documentation

◆ CpuExecutionContext

◆ MR

◆ TensorType

◆ UnaryOperationBase

Constructor & Destructor Documentation

◆ CpuSoftmaxOp()

Mila::Dnn::Compute::CpuSoftmaxOp::CpuSoftmaxOp ( IExecutionContext * context,
const SoftmaxConfig & config )
inline

Member Function Documentation

◆ backward()

void Mila::Dnn::Compute::CpuSoftmaxOp::backward ( const ITensor & input,
const ITensor & output_grad,
ITensor & input_grad ) const
inlineoverridevirtual

Backward pass - HOT PATH, pure dispatch to CPU kernel.

Similar to forward(), this method does minimal work and dispatches directly to the backward kernel using cached dimensions from build().

Algorithm: dX = Y * (dY - dot(Y, dY)) where Y is the softmax output (probabilities) and dY is the gradient.

Implements Mila::Dnn::Compute::UnaryOperation< DeviceType::Cpu, TensorDataType::FP32 >.

Here is the call graph for this function:

◆ build()

void Mila::Dnn::Compute::CpuSoftmaxOp::build ( const BuildContext & config)
inlineoverridevirtual

Build the operation for a concrete input shape.

This is the COLD PATH where all setup, validation, and computation happens ONCE. After build() completes, forward() and backward() become pure dispatch methods.

Responsibilities:

  1. Validate input shape compatibility
  2. Compute and cache normalized axis
  3. Compute and cache dimension sizes (outer, dim, inner)
  4. Cache OMP parallelization threshold

After build(), the operation is ready for zero-overhead forward/backward dispatch.

Reimplemented from Mila::Dnn::Compute::Operation< TDeviceType, TInput >.

Here is the call graph for this function:

◆ forward()

void Mila::Dnn::Compute::CpuSoftmaxOp::forward ( const ITensor & input,
ITensor & output ) const
inlineoverridevirtual

Forward pass - HOT PATH, pure dispatch to CPU kernel.

All setup, validation, and dimension computation was done in build(). This method extracts raw pointers and dispatches directly to the numerically-stable softmax kernel using pre-computed cached dimensions.

Algorithm: softmax(x) = exp(x - max(x)) / sum(exp(x - max(x))) Zero redundant work - maximum performance.

Implements Mila::Dnn::Compute::UnaryOperation< DeviceType::Cpu, TensorDataType::FP32 >.

Here is the call graph for this function:

◆ getConfig()

const SoftmaxConfig & Mila::Dnn::Compute::CpuSoftmaxOp::getConfig ( ) const
inline

◆ getName()

std::string Mila::Dnn::Compute::CpuSoftmaxOp::getName ( ) const
inlineoverridevirtual

Human-readable operation name.

Implements Mila::Dnn::Compute::Operation< TDeviceType, TInput >.

◆ getOperationType()

OperationType Mila::Dnn::Compute::CpuSoftmaxOp::getOperationType ( ) const
inlineoverridevirtual

◆ setParameters()

void Mila::Dnn::Compute::CpuSoftmaxOp::setParameters ( ITensor * weight,
ITensor * bias )
inlineoverridevirtual

Set parameter tensor references (no-op for Softmax - stateless operation).

Softmax has no trainable parameters, so this method validates that the inputs are null and does nothing else.

Reimplemented from Mila::Dnn::Compute::Operation< TDeviceType, TInput >.

Member Data Documentation

◆ cached_axis_

int64_t Mila::Dnn::Compute::CpuSoftmaxOp::cached_axis_ { -1 }
private

◆ cached_dim_size_

int64_t Mila::Dnn::Compute::CpuSoftmaxOp::cached_dim_size_ { 0 }
private

◆ cached_inner_size_

int64_t Mila::Dnn::Compute::CpuSoftmaxOp::cached_inner_size_ { 0 }
private

◆ cached_outer_size_

int64_t Mila::Dnn::Compute::CpuSoftmaxOp::cached_outer_size_ { 0 }
private

◆ config_

SoftmaxConfig Mila::Dnn::Compute::CpuSoftmaxOp::config_
private

◆ context_

IExecutionContext* Mila::Dnn::Compute::CpuSoftmaxOp::context_
private

◆ enable_omp_

bool Mila::Dnn::Compute::CpuSoftmaxOp::enable_omp_ { false }
private

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