aclnnFusedCrossEntropyLossWithMaxSum
Applicable Products
| Product | Supported |
|---|---|
| × | |
| √ | |
| √ | |
| × | |
| × | |
| × |
Function
Description: This operator is part of the cross entropy computation module in vocabulary parallelism scenarios, addressing memory and computational efficiency issues for extremely large vocabularies. This part computes the loss and SoftMax results.
Formula:
Function Prototype
Each operator uses a two-phase API. You must first call the "aclnnFusedCrossEntropyLossWithMaxSumGetWorkspaceSize" API to obtain the workspace size required for computation and the executor that encapsulates the operator computation process, and then call the "aclnnFusedCrossEntropyLossWithMaxSum" API to perform the computation.
aclnnStatus aclnnFusedCrossEntropyLossWithMaxSumGetWorkspaceSize(
const aclTensor* logitsMax,
const aclTensor* sumExpLogits,
const aclTensor* predictedLogits,
float labelSmoothing,
const aclTensor* inputOptional,
const aclTensor* weightOptional,
const aclTensor* vocabParallelLogitsOptional,
aclTensor* lossOut,
aclTensor* softMaxOutOptional,
uint64_t* workspaceSize,
aclOpExecutor** executor)aclnnStatus aclnnFusedCrossEntropyLossWithMaxSum(
void* workspace,
uint64_t workspaceSize,
aclOpExecutor* executor,
aclrtStream stream)aclnnFusedCrossEntropyLossWithMaxSumGetWorkspaceSize
Parameters
Parameter Input/Output Description Instruction Data Type Data Format Dimension (Shape) Non-contiguous Tensor logitsMax (aclTensor*) Input Maximum value of each row after the matmul computation, corresponding to logitsMax in the formula. - FLOAT ND 1 √ sumExpLogits (aclTensor*) Input The result of applying exp to the difference between the matmul result and the maximum value of each row, i.e., sumExpLogits in the formula. Same as the shape of logitsMax. FLOAT ND 1 √ predictedLogits (aclTensor*) Input Represents the result after subtracting the maximum value of each row from the matmul computation result and then filtering with maskedTargetOut, i.e., predictedLogits in the formula. Has the same shape as logitsMax. FLOAT ND 1 √ labelSmoothing (float) Input Label smoothing coefficient, used to mitigate overfitting. Currently only 0 is supported. FLOAT - - - inputOptional (aclTensor*) Input Left matrix input for matmul. Currently only supports null pointer input. FLOAT - - √ weightOptional (aclTensor*) Input Right matrix input for matmul, i.e., the weight matrix. Currently only supports a null pointer as input. FLOAT - - √ vocabParallelLogitsOptional (aclTensor*) Output Result of the matmul computation, i.e., vocabParallelLogits in the formula. The first dimension of shape must be consistent with the first dimension of logitsMax. FLOAT, FLOAT16, BFLOAT16 ND 2 √ lossOut (aclTensor*) Output Intermediate variable, representing the loss in the formula. The shape is consistent with logitsMax. FLOAT ND 1 √ softMaxOutOptional(aclTensor*) Output Intermediate variable, corresponding to vocabParallelLogits in the formula. Shape is the same as vocabParallelLogitsOptional. FLOAT ND 2 √ workspaceSize (uint64_t*) Output Returns the workspace size to be allocated on the Device side. - - - - - executor (aclOpExecutor**) Output Returns the operator executor, which contains the operator computation flow. - - - - - Return Value
aclnnStatus: return code. For details, see aclnn Return Code.
The first-phase API performs input parameter validation and returns an error in the following scenarios:
Return Value Error Code Description ACLNN_ERR_PARAM_NULLPTR 161001 logitsMax, sumExpLogits, predictedLogits, and lossOut are null pointers. ACLNN_ERR_PARAM_INVALID 161002 The data types of the input and output are not within the supported range. The shapes of the input and output do not meet the constraints. The dimensions of the input and output do not meet the constraints. labelSmoothing is not equal to 0.
aclnnFusedCrossEntropyLossWithMaxSum
Parameters:
Parameter Input/Output Description workspace Input Workspace memory address applied for on the Device side. workspaceSize Input Workspace size applied for on the Device side, obtained through the first-phase API aclnnFusedCrossEntropyLossWithMaxSumGetWorkspaceSize. executor Input Operator executor that contains the operator computation flow. stream Input Specifies the stream for task execution. Return Value
aclnnStatus: return code. For details, see aclnn Return Code.
Constraints
Deterministic computation:
- aclnnFusedCrossEntropyLossWithMaxSum defaults to a deterministic implementation.
Example
The following provides example code for reference only. For details about compilation and running, see Compile and Run Samples.
#include <iostream>
#include <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_fused_cross_entropy_loss_with_max_sum.h"
#define CHECK_RET(cond, return_expr) \
do { \
if (!(cond)) { \
return_expr; \
} \
} while (0)
#define LOG_PRINT(message, ...) \
do { \
printf(message, ##__VA_ARGS__); \
} while (0)
int64_t GetShapeSize(const std::vector<int64_t>& shape) {
int64_t shapeSize = 1;
for (auto i : shape) {
shapeSize *= i;
}
return shapeSize;
}
int Init(int32_t deviceId, aclrtStream* stream) {
// Boilerplate. Initialize resources.
auto ret = aclInit(nullptr);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclInit failed. ERROR: %d\n", ret); return ret);
ret = aclrtSetDevice(deviceId);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSetDevice failed. ERROR: %d\n", ret); return ret);
ret = aclrtCreateStream(stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtCreateStream failed. ERROR: %d\n", ret); return ret);
return 0;
}
template <typename T>
int CreateAclTensor(const std::vector<T>& hostData, const std::vector<int64_t>& shape, void** deviceAddr, aclDataType dataType, aclTensor** tensor) {
auto size = GetShapeSize(shape) * sizeof(T);
// Call aclrtMalloc to apply for device-side memory.
auto ret = aclrtMalloc(deviceAddr, size, ACL_MEM_MALLOC_HUGE_FIRST);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMalloc failed. ERROR: %d\n", ret); return ret);
// Call aclrtMemcpy to copy host-side data to device-side memory.
ret = aclrtMemcpy(*deviceAddr, size, hostData.data(), size, ACL_MEMCPY_HOST_TO_DEVICE);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMemcpy failed. ERROR: %d\n", ret); return ret);
// Calculate the strides of a contiguous tensor.
std::vector<int64_t> strides(shape.size(), 1);
for (int64_t i = shape.size() - 2; i >= 0; i--) {
strides[i] = shape[i + 1] * strides[i + 1];
}
// Call the aclCreateTensor API to create an aclTensor.
*tensor = aclCreateTensor(shape.data(), shape.size(), dataType, strides.data(), 0, aclFormat::ACL_FORMAT_ND,
shape.data(), shape.size(), *deviceAddr);
return 0;
}
int main() {
// 1. (Boilerplate) Initialize the device/stream. For details, see the ACL API manual.
// Set deviceId based on the actual device.
int32_t deviceId = 0;
aclrtStream stream;
auto ret = Init(deviceId, &stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);
// 2. Construct inputs and outputs based on the API interface.
std::vector<int64_t> logitsMaxShape = {2};
std::vector<int64_t> sumExpLogitsShape = {2};
std::vector<int64_t> predictedLogitsShape = {2};
std::vector<int64_t> inputShape = {2};
std::vector<int64_t> weightShape = {2};
std::vector<int64_t> vocabParallelLogitsOptionalShape = {2, 2};
std::vector<int64_t> lossOutShape = {2};
std::vector<int64_t> softMaxOutOptionalShape = {2, 2};
float labelSmoothing = 0;
void* logitsMaxDeviceAddr = nullptr;
void* sumExpLogitsDeviceAddr = nullptr;
void* predictedLogitsDeviceAddr = nullptr;
void* inputDeviceAddr = nullptr;
void* weightDeviceAddr = nullptr;
void* vocabParallelLogitsOptionalDeviceAddr = nullptr;
void* lossOutDeviceAddr = nullptr;
void* softMaxOutOptionalDeviceAddr = nullptr;
aclTensor* logitsMax = nullptr;
aclTensor* sumExpLogits = nullptr;
aclTensor* predictedLogits = nullptr;
aclTensor* input = nullptr;
aclTensor* weight = nullptr;
aclTensor* vocabParallelLogitsOptional = nullptr;
aclTensor* lossOut = nullptr;
aclTensor* softMaxOutOptional = nullptr;
std::vector<float> logitsMaxHostData = {0.5, 1};
std::vector<float> sumExpLogitsHostData = {0.5, 1};
std::vector<float> predictedLogitsHostData = {0.5, 1};
std::vector<float> inputHostData = {0, 1};
std::vector<float> weightHostData = {0, 1};
std::vector<float> vocabParallelLogitsOptionalHostData = {1, 0.5, 0.5, 1};
std::vector<float> lossOutHostData = {0, 0};
std::vector<float> softMaxOutOptionalHostData = {0, 0, 0, 0};
// Create an aclTensor.
ret = CreateAclTensor(logitsMaxHostData, logitsMaxShape, &logitsMaxDeviceAddr, aclDataType::ACL_FLOAT, &logitsMax);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(sumExpLogitsHostData, sumExpLogitsShape, &sumExpLogitsDeviceAddr, aclDataType::ACL_FLOAT, &sumExpLogits);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(predictedLogitsHostData, predictedLogitsShape, &predictedLogitsDeviceAddr, aclDataType::ACL_FLOAT, &predictedLogits);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(inputHostData, inputShape, &inputDeviceAddr, aclDataType::ACL_FLOAT, &input);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(weightHostData, weightShape, &weightDeviceAddr, aclDataType::ACL_FLOAT, &weight);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(vocabParallelLogitsOptionalHostData, vocabParallelLogitsOptionalShape, &vocabParallelLogitsOptionalDeviceAddr, aclDataType::ACL_FLOAT, &vocabParallelLogitsOptional);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(lossOutHostData, lossOutShape, &lossOutDeviceAddr, aclDataType::ACL_FLOAT, &lossOut);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(softMaxOutOptionalHostData, softMaxOutOptionalShape, &softMaxOutOptionalDeviceAddr, aclDataType::ACL_FLOAT, &softMaxOutOptional);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// 3. Call the CANN operator library API. Replace with the specific API name.
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
// Call the first-phase API of aclnnFusedCrossEntropyLossWithMaxSum.
ret = aclnnFusedCrossEntropyLossWithMaxSumGetWorkspaceSize(logitsMax, sumExpLogits, predictedLogits, labelSmoothing, input, weight,
vocabParallelLogitsOptional, lossOut, softMaxOutOptional, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnFusedCrossEntropyLossWithMaxSumGetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
// Apply for device memory based on the workspaceSize calculated by the first-phase API.
void* workspaceAddr = nullptr;
if (workspaceSize > 0) {
ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret);
}
// Call the second-phase API of aclnnFusedCrossEntropyLossWithMaxSum.
ret = aclnnFusedCrossEntropyLossWithMaxSum(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnFusedCrossEntropyLossWithMaxSum failed. ERROR: %d\n", ret); return ret);
// 4. (Boilerplate) Synchronize and wait for task execution to complete.
ret = aclrtSynchronizeStream(stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret);
// 5. Obtain the output values and copy the results from the device memory to the host memory. Modify based on the specific API definition.
auto size = GetShapeSize(lossOutShape);
std::vector<float> resultData(size, 0);
ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), lossOutDeviceAddr,
size * sizeof(resultData[0]), ACL_MEMCPY_DEVICE_TO_HOST);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return ret);
for (int64_t i = 0; i < size; i++) {
LOG_PRINT("result[%ld] is: %f\n", i, resultData[i]);
}
size = GetShapeSize(softMaxOutOptionalShape);
std::vector<float> secondResultData(size, 0);
ret = aclrtMemcpy(secondResultData.data(), secondResultData.size() * sizeof(secondResultData[0]), softMaxOutOptionalDeviceAddr,
size * sizeof(secondResultData[0]), ACL_MEMCPY_DEVICE_TO_HOST);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return ret);
for (int64_t i = 0; i < size; i++) {
LOG_PRINT("result[%ld] is: %f\n", i, secondResultData[i]);
}
// 6. Release aclTensor and aclScalar. Modify based on the specific API definition.
aclDestroyTensor(logitsMax);
aclDestroyTensor(sumExpLogits);
aclDestroyTensor(predictedLogits);
aclDestroyTensor(input);
aclDestroyTensor(weight);
aclDestroyTensor(vocabParallelLogitsOptional);
aclDestroyTensor(lossOut);
aclDestroyTensor(softMaxOutOptional);
// 7. Release device resources. Modify based on the specific API definition.
aclrtFree(logitsMaxDeviceAddr);
aclrtFree(sumExpLogitsDeviceAddr);
aclrtFree(predictedLogitsDeviceAddr);
aclrtFree(inputDeviceAddr);
aclrtFree(weightDeviceAddr);
aclrtFree(vocabParallelLogitsOptionalDeviceAddr);
aclrtFree(lossOutDeviceAddr);
aclrtFree(softMaxOutOptionalDeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}