Mila
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 = std::conditional_t< TDeviceType==DeviceType::Cuda, CudaMemoryResource, HostMemoryResource >
 Memory resource type based on device type.
 

Public Member Functions

 CpuCrossEntropyOp (const CrossEntropyConfig &config)
 Constructs a new CPU Cross Entropy operation with the default device context.
 
 CpuCrossEntropyOp (std::shared_ptr< DeviceContext > context, const CrossEntropyConfig &config)
 Constructs a new CPU Cross Entropy operation with a specific 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< Tensor< float, MR > > > &parameters, std::vector< std::shared_ptr< Tensor< float, MR > > > &parameter_gradients, Tensor< int, MR > &input_gradient, const OperationAttributes &attributes, 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< Tensor< float, MR > > > &parameters, const OperationAttributes &attributes, 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 >
 UnaryOperation (OperationType operation_type)
 Constructs a UnaryOperation with the specified operation type.
 
 UnaryOperation (OperationType operation_type, std::shared_ptr< DeviceContext > context)
 Constructs a UnaryOperation with the specified operation type and device context.
 
virtual ~UnaryOperation ()=default
 Virtual destructor for proper cleanup of derived classes.
 
virtual void backward (const Tensor< int, MR > &grad, const std::vector< std::shared_ptr< Tensor< float, MR > > > &parameters, std::vector< std::shared_ptr< Tensor< float, MR > > > &output_grads) const
 Executes the backward pass of a unary operation.
 
virtual void backward (const Tensor< int, MR > &input, const Tensor< float, MR > &output_grad, const std::vector< std::shared_ptr< Tensor< float, MR > > > &parameters, std::vector< std::shared_ptr< Tensor< float, MR > > > &parameter_grads, Tensor< int, MR > &input_grad, const OperationAttributes &properties, const std::vector< std::shared_ptr< Tensor< float, MR > > > &output_state) const
 Executes the comprehensive backward pass of a unary operation.
 
virtual void forward (const Tensor< int, MR > &input, const std::vector< std::shared_ptr< Tensor< float, MR > > > &parameters, const OperationAttributes &properties, Tensor< float, MR > &output, std::vector< std::shared_ptr< Tensor< float, MR > > > &output_state) const=0
 Executes the forward pass of a unary operation.
 
- Public Member Functions inherited from Mila::Dnn::Compute::OperationBase< TDeviceType, TInput1, TInput2, TOutput >
 OperationBase (OperationType operation_type, std::shared_ptr< DeviceContext > context)
 Constructs an OperationBase object with a specific device context and compute precision.
 
virtual ~OperationBase ()=default
 Virtual destructor for the OperationBase class.
 
std::shared_ptr< DeviceContextgetDeviceContext () const
 Gets the device context associated with this operation.
 
DeviceType getDeviceType () const
 Gets the device type for this operation.
 
OperationType getOperationType () const
 Gets the operation type enumeration value.
 

Private Attributes

CrossEntropyConfig config_
 Configuration for the CrossEntropy operation.
 

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

◆ OperationBase

Constructor & Destructor Documentation

◆ CpuCrossEntropyOp() [1/2]

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.

◆ CpuCrossEntropyOp() [2/2]

Mila::Dnn::Compute::CpuCrossEntropyOp::CpuCrossEntropyOp ( std::shared_ptr< DeviceContext context,
const CrossEntropyConfig config 
)
inline

Constructs a new CPU Cross Entropy operation with a specific device context.

Parameters
contextThe device context to use for this operation.
Exceptions
std::runtime_errorIf the context is not for a CPU device.

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< Tensor< float, MR > > > &  parameters,
std::vector< std::shared_ptr< Tensor< float, MR > > > &  parameter_gradients,
Tensor< int, MR > &  input_gradient,
const OperationAttributes attributes,
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< Tensor< float, MR > > > &  parameters,
const OperationAttributes attributes,
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::OperationBase< TDeviceType, TInput1, TInput2, TOutput >.

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: