Skip to main content
`Ctrl`+`K`

# LossFunction

MIOpen: LossFunction
Enumerations | Functions
LossFunction

## Enumerations

enum | miopenCTCLossAlgo_t { MIOPEN_CTC_LOSS_ALGO_DETERMINISTIC = 0 }
enum | miopenLossReductionMode_t { MIOPEN_LOSS_REDUCTION_NONE = 0 , MIOPEN_LOSS_REDUCTION_SUM = 1 , MIOPEN_LOSS_REDUCTION_MEAN = 2 }

## Functions

| MIOPEN_DECLARE_OBJECT (miopenCTCLossDescriptor)
| Creates the miopenCTCLossDescriptor_t type.
miopenStatus_t | miopenCreateCTCLossDescriptor (miopenCTCLossDescriptor_t *ctcLossDesc)
| Create a CTC loss function Descriptor.
miopenStatus_t | miopenGetCTCLossDescriptor (miopenCTCLossDescriptor_t ctcLossDesc, miopenDataType_t *dataType, int *blank_label_id, bool *apply_softmax_layer)
| Retrieves a CTC loss function descriptor's details.
miopenStatus_t | miopenDestroyCTCLossDescriptor (miopenCTCLossDescriptor_t ctcLossDesc)
| Destroys a CTC loss function descriptor object.
miopenStatus_t | miopenSetCTCLossDescriptor (miopenCTCLossDescriptor_t ctcLossDesc, miopenDataType_t dataType, const int blank_label_id, bool apply_softmax_layer)
| Set the details of a CTC loss function descriptor.
miopenStatus_t | miopenGetCTCLossWorkspaceSize (miopenHandle_t handle, const miopenTensorDescriptor_t probsDesc, const miopenTensorDescriptor_t gradientsDesc, const int *labels, const int *labelLengths, const int *inputLengths, miopenCTCLossAlgo_t algo, const miopenCTCLossDescriptor_t ctcLossDesc, size_t *workSpaceSize)
| Query the amount of memory required to execute miopenCTCLoss.
miopenStatus_t | miopenCTCLoss (miopenHandle_t handle, const miopenTensorDescriptor_t probsDesc, const void *probs, const int *labels, const int *labelLengths, const int *inputLengths, void *losses, const miopenTensorDescriptor_t gradientsDesc, void *gradients, miopenCTCLossAlgo_t algo, const miopenCTCLossDescriptor_t ctcLossDesc, void *workSpace, size_t workSpaceSize)
| Execute forward inference for CTCLoss layer.
miopenStatus_t | miopenGetSoftMarginLossForwardWorkspaceSize (miopenHandle_t handle, miopenTensorDescriptor_t inputDesc, miopenTensorDescriptor_t targetDesc, miopenTensorDescriptor_t outputDesc, miopenLossReductionMode_t reduction, size_t *sizeInBytes)
| Helper function to query the minimum workspace size required by the SoftMarginLossForward call.
miopenStatus_t | miopenSoftMarginLossForward (miopenHandle_t handle, miopenTensorDescriptor_t inputDesc, const void *input, miopenTensorDescriptor_t targetDesc, const void *target, miopenTensorDescriptor_t outputDesc, void *output, miopenLossReductionMode_t reduction, void *workspace=nullptr, size_t workspaceSizeInBytes=0)
| Execute a SoftMarginLoss forward layer.
miopenStatus_t | miopenSoftMarginLossBackward (miopenHandle_t handle, miopenTensorDescriptor_t inputDesc, const void *input, miopenTensorDescriptor_t targetDesc, const void *target, miopenTensorDescriptor_t doutputDesc, const void *doutput, miopenTensorDescriptor_t dinputDesc, void *dinput, miopenLossReductionMode_t reduction)
| Execute a SoftMarginLoss backward layer.
miopenStatus_t | miopenGetMultiMarginLossForwardWorkspaceSize (miopenHandle_t handle, miopenTensorDescriptor_t inputDesc, miopenTensorDescriptor_t targetDesc, miopenTensorDescriptor_t weightDesc, miopenTensorDescriptor_t outputDesc, long p, float margin, miopenLossReductionMode_t reduction, size_t *sizeInBytes)
| Helper function to query the minimum workspace size required by the MultiMarginLossForward call.
miopenStatus_t | miopenMultiMarginLossForward (miopenHandle_t handle, miopenTensorDescriptor_t inputDesc, const void *input, miopenTensorDescriptor_t targetDesc, const void *target, miopenTensorDescriptor_t weightDesc, const void *weight, miopenTensorDescriptor_t outputDesc, void *output, long p, float margin, miopenLossReductionMode_t reduction, void *workspace, size_t workspaceSizeInBytes)
| Execute a MultiMarginLoss forward layer.

## Detailed Description

## Enumeration Type Documentation

## ◆ miopenCTCLossAlgo_t

enum miopenCTCLossAlgo_t

Algorithms available to execute the CTC loss operation

Enumerator
MIOPEN_CTC_LOSS_ALGO_DETERMINISTIC | Results are guaranteed to be reproducible

## ◆ miopenLossReductionMode_t

enum miopenLossReductionMode_t

Reduction mode for loss function

Enumerator
MIOPEN_LOSS_REDUCTION_NONE | output tensor elements are not reduced
MIOPEN_LOSS_REDUCTION_SUM | output tensor elements are summed up
MIOPEN_LOSS_REDUCTION_MEAN | output tensor elements are summed up and divided with total number of elements to get mean value

## Function Documentation

## ◆ MIOPEN_DECLARE_OBJECT()

MIOPEN_DECLARE_OBJECT | ( | miopenCTCLossDescriptor |  | ) |

Creates the miopenCTCLossDescriptor_t type.

## ◆ miopenCreateCTCLossDescriptor()

miopenStatus_t miopenCreateCTCLossDescriptor | ( | miopenCTCLossDescriptor_t * | ctcLossDesc | ) |

Create a CTC loss function Descriptor.

API for creating an uninitialized CTC loss function descriptor.

Parameters

  ctcLossDesc | Pointer to the CTC loss function descriptor type (output)

Returns
  miopenStatus_t

## ◆ miopenCTCLoss()

miopenStatus_t miopenCTCLoss | ( | miopenHandle_t | handle,
|  | const miopenTensorDescriptor_t | probsDesc,
|  | const void * | probs,
|  | const int * | labels,
|  | const int * | labelLengths,
|  | const int * | inputLengths,
|  | void * | losses,
|  | const miopenTensorDescriptor_t | gradientsDesc,
|  | void * | gradients,
|  | miopenCTCLossAlgo_t | algo,
|  | const miopenCTCLossDescriptor_t | ctcLossDesc,
|  | void * | workSpace,
|  | size_t | workSpaceSize )

Execute forward inference for CTCLoss layer.

Interface for executing the forward inference pass on a CTCLoss.

Parameters

  handle | MIOpen handle (input)
  probsDesc | Tensor descriptor for probabilities (input)
  probs | Pointer to the probabilities tensor (input)
  labels | Pointer to the flattened labels list (input)
  labelLengths | Pointer to the lengths list for "labels" (input)
  inputLengths | Pointer to the list of the time steps in each batch (input)
  losses | Pointer to the computed losses of CTC (Output)
  gradientsDesc | Tensor descriptor for gradients (input)
  gradients | Pointer to the computed gradients of CTC (Output)
  algo | CTC loss algorithm selected (input)
  ctcLossDesc | CTC loss function descriptor type (input)
  workSpace | Pointer to memory allocated for execute CTC loss operation (input)
  workSpaceSize | Number of bytes of workspace required for CTC loss operation with selected algorithm (input)

Returns
  miopenStatus_t

## ◆ miopenDestroyCTCLossDescriptor()

miopenStatus_t miopenDestroyCTCLossDescriptor | ( | miopenCTCLossDescriptor_t | ctcLossDesc | ) |

Destroys a CTC loss function descriptor object.

Parameters

  ctcLossDesc | CTC loss function descriptor type (input)

Returns
  miopenStatus_t

## ◆ miopenGetCTCLossDescriptor()

miopenStatus_t miopenGetCTCLossDescriptor | ( | miopenCTCLossDescriptor_t | ctcLossDesc,
|  | miopenDataType_t * | dataType,
|  | int * | blank_label_id,
|  | bool * | apply_softmax_layer )

Retrieves a CTC loss function descriptor's details.

Parameters

  ctcLossDesc | CTC loss function descriptor (input)
  dataType | Data type used in this CTC loss operation, only fp32 currently supported (output)
  blank_label_id | User defined index for blank label (output)
  apply_softmax_layer | Boolean to toggle input layer property (output)

Returns
  miopenStatus_t

## ◆ miopenGetCTCLossWorkspaceSize()

miopenStatus_t miopenGetCTCLossWorkspaceSize | ( | miopenHandle_t | handle,
|  | const miopenTensorDescriptor_t | probsDesc,
|  | const miopenTensorDescriptor_t | gradientsDesc,
|  | const int * | labels,
|  | const int * | labelLengths,
|  | const int * | inputLengths,
|  | miopenCTCLossAlgo_t | algo,
|  | const miopenCTCLossDescriptor_t | ctcLossDesc,
|  | size_t * | workSpaceSize )

Query the amount of memory required to execute miopenCTCLoss.

This function calculates the amount of memory required to run the CTC loss function given a CTC loss function descriptor with the specified algorithm.

Parameters

  handle | MIOpen handle (input)
  probsDesc | Tensor descriptor for probabilities (input)
  gradientsDesc | Tensor descriptor for gradients (input)
  labels | Pointer to the flattened labels list (input)
  labelLengths | Pointer to the lengths list for "labels" (input)
  inputLengths | Pointer to the list of the time steps in each batch (input)
  algo | CTC loss algorithm selected (input)
  ctcLossDesc | CTC loss function descriptor type (input)
  workSpaceSize | Number of bytes of workspace required for CTC loss operation with selected algorithm (output)

Returns
  miopenStatus_t

## ◆ miopenGetMultiMarginLossForwardWorkspaceSize()

miopenStatus_t miopenGetMultiMarginLossForwardWorkspaceSize | ( | miopenHandle_t | handle,
|  | miopenTensorDescriptor_t | inputDesc,
|  | miopenTensorDescriptor_t | targetDesc,
|  | miopenTensorDescriptor_t | weightDesc,
|  | miopenTensorDescriptor_t | outputDesc,
|  | long | p,
|  | float | margin,
|  | miopenLossReductionMode_t | reduction,
|  | size_t * | sizeInBytes )

Helper function to query the minimum workspace size required by the MultiMarginLossForward call.

Parameters

  [in] | handle | MIOpen Handle
  [in] | inputDesc | Tensor descriptor for input tensor (N, C) where N is the batch size and C is the number of classes
  [in] | targetDesc | Tensor descriptor for target tensor, must have shape (N). Each value is between 0 and C - 1
  [in] | weightDesc | Tensor descriptor for weight tensor. It is a manual rescaling weight given to each class. It has to be a Tensor of size C
  [in] | outputDesc | Tensor descriptor for output tensor. If reduction is 'none, then it must have shape (N). Otherwise, it is a scalar
  [in] | p | Has a default value of 1. The only supported values are 1 and 2
  [in] | margin | Has a default value of 1
  [in] | reduction | Reduction mode (sum, mean)
  [out] | sizeInBytes | Pointer to data to return the minimum workspace size

Returns
  miopenStatus_t

## ◆ miopenGetSoftMarginLossForwardWorkspaceSize()

miopenStatus_t miopenGetSoftMarginLossForwardWorkspaceSize | ( | miopenHandle_t | handle,
|  | miopenTensorDescriptor_t | inputDesc,
|  | miopenTensorDescriptor_t | targetDesc,
|  | miopenTensorDescriptor_t | outputDesc,
|  | miopenLossReductionMode_t | reduction,
|  | size_t * | sizeInBytes )

Helper function to query the minimum workspace size required by the SoftMarginLossForward call.

Parameters

  [in] | handle | MIOpen Handle
  [in] | inputDesc | Tensor descriptor for input tensor
  [in] | targetDesc | Tensor descriptor for target tensor
  [in] | outputDesc | Tensor descriptor for output tensor
  [in] | reduction | Reduction mode (sum, mean). For none reduction we don't need to use this function
  [out] | sizeInBytes | Pointer to data to return the minimum workspace size

Returns
  miopenStatus_t

## ◆ miopenMultiMarginLossForward()

miopenStatus_t miopenMultiMarginLossForward | ( | miopenHandle_t | handle,
|  | miopenTensorDescriptor_t | inputDesc,
|  | const void * | input,
|  | miopenTensorDescriptor_t | targetDesc,
|  | const void * | target,
|  | miopenTensorDescriptor_t | weightDesc,
|  | const void * | weight,
|  | miopenTensorDescriptor_t | outputDesc,
|  | void * | output,
|  | long | p,
|  | float | margin,
|  | miopenLossReductionMode_t | reduction,
|  | void * | workspace,
|  | size_t | workspaceSizeInBytes )

Execute a MultiMarginLoss forward layer.

Parameters

  [in] | handle | MIOpen handle
  [in] | inputDesc | Tensor descriptor for input tensor (N, C) where N is the batch size and C is the number of classes.
  [in] | input | Data tensor input
  [in] | targetDesc | Tensor descriptor for target tensor, must have shape (N). Each value is between 0 and C - 1
  [in] | target | Data tensor target
  [in] | weightDesc | Tensor descriptor for weight tensor. It is a manual rescaling weight given to each class. It has to be a Tensor of size C
  [in] | weight | Data tensor weight
  [in] | outputDesc | Tensor descriptor for output tensor. If reduction is 'none, then it must have shape (N). Otherwise, it is a scalar.
  [out] | output | Data tensor output
  [in] | p | Has a default value of 1. The only supported values are 1 and 2
  [in] | margin | Has a default value of 1
  [in] | reduction | Reduction mode. If reduction mode is mean or sum, you must provide param workspace and workspaceSizeInBytes. Call miopenGetMultiMarginLossForwardWorkspaceSize to get workspaceSizeInBytes
  [in] | workspace | Address of the allocated workspace data. Set = nullptr if reduction = 'none'
  [in] | workspaceSizeInBytes | Size in bytes of the allocated workspace data. Set = 0 if reduction = 'none

Returns
  miopenStatus_t

## ◆ miopenSetCTCLossDescriptor()

miopenStatus_t miopenSetCTCLossDescriptor | ( | miopenCTCLossDescriptor_t | ctcLossDesc,
|  | miopenDataType_t | dataType,
|  | const int | blank_label_id,
|  | bool | apply_softmax_layer )

Set the details of a CTC loss function descriptor.

Parameters

  ctcLossDesc | CTC loss function descriptor type (input)
  dataType | Data type used in this CTC loss operation, only fp32 currently supported (input)
  blank_label_id | User defined index for blank label, default 0 (input)
  apply_softmax_layer | Boolean to toggle input layer property (input)

Returns
  miopenStatus_t

## ◆ miopenSoftMarginLossBackward()

miopenStatus_t miopenSoftMarginLossBackward | ( | miopenHandle_t | handle,
|  | miopenTensorDescriptor_t | inputDesc,
|  | const void * | input,
|  | miopenTensorDescriptor_t | targetDesc,
|  | const void * | target,
|  | miopenTensorDescriptor_t | doutputDesc,
|  | const void * | doutput,
|  | miopenTensorDescriptor_t | dinputDesc,
|  | void * | dinput,
|  | miopenLossReductionMode_t | reduction )

Execute a SoftMarginLoss backward layer.

Parameters

  [in] | handle | MIOpen handle
  [in] | inputDesc | Tensor descriptor for input tensor
  [in] | input | Data tensor input
  [in] | targetDesc | Tensor descriptor for target tensor
  [in] | target | Data tensor target
  [in] | doutputDesc | Tensor descriptor for output gradient
  [in] | doutput | Output gradient
  [in] | dinputDesc | Tensor descriptor for input gradient
  [out] | dinput | Input gradient
  [in] | reduction | Reduction mode (none, sum, mean)

Returns
  miopenStatus_t

## ◆ miopenSoftMarginLossForward()

miopenStatus_t miopenSoftMarginLossForward | ( | miopenHandle_t | handle,
|  | miopenTensorDescriptor_t | inputDesc,
|  | const void * | input,
|  | miopenTensorDescriptor_t | targetDesc,
|  | const void * | target,
|  | miopenTensorDescriptor_t | outputDesc,
|  | void * | output,
|  | miopenLossReductionMode_t | reduction,
|  | void * | workspace = nullptr,
|  | size_t | workspaceSizeInBytes = 0 )

Execute a SoftMarginLoss forward layer.

Parameters

  [in] | handle | MIOpen handle
  [in] | inputDesc | Tensor descriptor for input tensor
  [in] | input | Data tensor input
  [in] | targetDesc | Tensor descriptor for target tensor
  [in] | target | Data tensor target
  [in] | outputDesc | Tensor descriptor for output tensor
  [out] | output | Data tensor output
  [in] | reduction | Reduction mode. If reduction mode is mean or sum, you must provide param workspace and workspaceSizeInBytes. Call miopenGetSoftMarginLossForwardWorkspaceSize to get workspaceSizeInBytes
  [in] | workspace | Address of the allocated workspace data (Default = null)
  [in] | workspaceSizeInBytes | Size in bytes of the allocated workspace data (Default = 0)

Returns
  miopenStatus_t