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

CPU implementation of the cross entropy loss operation for neural networks. More...

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

Public Types

using MR = typename CpuDevice::MR
using OperationBase = UnaryOperation<DeviceType::Cpu, int, float>
Public Types inherited from Mila::Dnn::Compute::UnaryOperation< DeviceType::Cpu, int, float >
using MR
using TensorInputType
using TensorOutputType
Public Types inherited from Mila::Dnn::Compute::Operation< TDeviceType, TPrecision >
using DataTypeTraits

Public Member Functions

 CpuCrossEntropyOp (const CrossEntropyConfig &config)
 Constructs a new CPU Cross Entropy operation with the default device context.
void backward (const Tensor< int, MR > &input, const Tensor< float, MR > &output, const Tensor< float, MR > &output_gradient, const std::vector< std::shared_ptr< ITensor > > &parameters, std::vector< std::shared_ptr< Tensor< float, MR > > > &parameter_gradients, Tensor< int, MR > &input_gradient, const std::vector< std::shared_ptr< Tensor< float, MR > > > &output_state) const
 Performs the backward pass of the cross entropy operation.
void backward_impl (float *dlogits, const float *dlosses, const float *probs, const Tensor< int, CpuMemoryResource > &targets, int B, int T, int V, int Vp) const
 Helper method for the backward pass implementation.
void forward (const Tensor< int, MR > &input, const std::vector< std::shared_ptr< ITensor > > &parameters, Tensor< float, MR > &output, std::vector< std::shared_ptr< Tensor< float, MR > > > &output_state) const override
 Performs the forward pass of the cross entropy operation.
std::string getName () const override
 Gets the name of this operation.
Public Member Functions inherited from Mila::Dnn::Compute::UnaryOperation< DeviceType::Cpu, int, float >
virtual ~UnaryOperation ()=default
virtual void backward (const ITensor &input, const ITensor &output_grad, ITensor &input_grad) const=0
 Backward pass: compute gradient wrt input given output gradient.
virtual void forward (const ITensor &input, ITensor &output) const=0
 Forward pass: compute output = f(input).
Public Member Functions inherited from Mila::Dnn::Compute::Operation< TDeviceType, TPrecision >
virtual ~Operation ()=default
virtual void build (const BuildContext &build_context)
 Prepare the operation for a concrete input shape.
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 OperationType getOperationType () const=0
 Operation type identifier.
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 setParameters (ITensor *weight, ITensor *bias)
 Bind module-owned parameter tensors to the operation.
virtual void setTrainingMode (TrainingMode training_mode)
 Configure operation training-mode behavior.

Private Attributes

CrossEntropyConfig config_
 Configuration for the CrossEntropy operation.

Additional Inherited Members

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

Detailed Description

CPU implementation of the cross entropy loss operation for neural networks.

This class provides a CPU-based implementation of the cross entropy loss function, which is commonly used in classification tasks. It computes the negative log likelihood of the correct class given the predicted probabilities.

Template Parameters
TInputThe data type of the input tensor elements (typically int for class indices).
TDataTypeThe data type used for computation and output (typically float).

Member Typedef Documentation

◆ MR

using Mila::Dnn::Compute::CpuCrossEntropyOp::MR = typename CpuDevice::MR

◆ OperationBase

Constructor & Destructor Documentation

◆ CpuCrossEntropyOp()

Mila::Dnn::Compute::CpuCrossEntropyOp::CpuCrossEntropyOp ( const CrossEntropyConfig & config)
inline

Constructs a new CPU Cross Entropy operation with the default device context.

Initializes the operation with a CPU device context.

Member Function Documentation

◆ backward()

void Mila::Dnn::Compute::CpuCrossEntropyOp::backward ( const Tensor< int, MR > & input,
const Tensor< float, MR > & output,
const Tensor< float, MR > & output_gradient,
const std::vector< std::shared_ptr< ITensor > > & parameters,
std::vector< std::shared_ptr< Tensor< float, MR > > > & parameter_gradients,
Tensor< int, MR > & input_gradient,
const std::vector< std::shared_ptr< Tensor< float, MR > > > & output_state ) const
inline

Performs the backward pass of the cross entropy operation.

Computes gradients with respect to inputs and probabilities.

Parameters
inputInput tensor from the forward pass (target indices).
outputOutput tensor from the forward pass (loss values).
output_gradientGradient of the loss with respect to the output.
parametersParameters tensor from forward pass (probabilities).
parameter_gradientsGradients for parameters (probabilities).
input_gradientGradient of the loss with respect to the input (unused for integer targets).
attributesAdditional attributes for the operation.
output_stateCache tensors from forward pass.
Here is the call graph for this function:

◆ backward_impl()

void Mila::Dnn::Compute::CpuCrossEntropyOp::backward_impl ( float * dlogits,
const float * dlosses,
const float * probs,
const Tensor< int, CpuMemoryResource > & targets,
int B,
int T,
int V,
int Vp ) const
inline

Helper method for the backward pass implementation.

Computes gradients for the combined softmax and cross entropy operation.

Parameters
dlogitsGradient buffer for logits/probabilities.
dlossesGradient buffer from output loss.
probsOriginal probability values.
targetsTarget class indices.
BBatch size.
TDataTypeSequence length.
VVocabulary size (without padding).
VpPadded vocabulary size.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ forward()

void Mila::Dnn::Compute::CpuCrossEntropyOp::forward ( const Tensor< int, MR > & input,
const std::vector< std::shared_ptr< ITensor > > & parameters,
Tensor< float, MR > & output,
std::vector< std::shared_ptr< Tensor< float, MR > > > & output_state ) const
inlineoverride

Performs the forward pass of the cross entropy operation.

Computes the negative log likelihood of the correct class for each sample.

Parameters
inputInput tensor containing target class indices of shape [B, TDataType].
parametersParameters tensor containing probabilities of shape [B, TDataType, V].
attributesAdditional attributes for the operation.
outputOutput tensor to store the cross entropy losses of shape [B, TDataType].
output_stateCache for storing intermediate results (used in backward pass).
Here is the call graph for this function:

◆ getName()

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

Gets the name of this operation.

Returns
std::string The name of the operation ("Cpu::CrossEntropyOp").

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

Member Data Documentation

◆ config_

CrossEntropyConfig Mila::Dnn::Compute::CpuCrossEntropyOp::config_
private

Configuration for the CrossEntropy operation.


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