aclnnPrecisionCompare
Applicable Products
Ascend 950PR&950DT products : Supported
Atlas A3 products : Supported
Atlas A2 products : Supported
Atlas 200I/500 A2 inference products : Not supported
Atlas inference products : Not supported
Atlas training products : Not supported
Function
Description: Compares whether the elements of two tensors are identical, performs precision comparison, and returns the status code of the operator execution. It does not trigger management-plane fault reporting.
Formula:
Performs element-wise comparison between golden and realdata to check whether they are identical. out returns the comparison result status code (0 indicates pass, non-zero indicates an exception).
Function Prototype
Each operator uses a two-phase API. First call "aclnnPrecisionCompareGetWorkspaceSize" to obtain the workspace size required for computation and the executor that contains the operator computation process. Then call "aclnnPrecisionCompare" to execute the computation.
aclnnStatus aclnnPrecisionCompareGetWorkspaceSize(
const aclTensor* golden,
const aclTensor* realdata,
aclTensor* out,
uint64_t* workspaceSize,
aclOpExecutor** executor)aclnnStatus aclnnPrecisionCompare(
void* workspace,
uint64_t workspaceSize,
aclOpExecutor* executor,
aclrtStream stream)aclnnPrecisionCompareGetWorkspaceSize
Parameters:
Parameter Input/Output Description Instruction Data Type Data Format Shape Non-contiguous Tensor golden (aclTensor*) Input Reference data for precision comparison. The data type and shape must be consistent with realdata. FLOAT16, FLOAT, BFLOAT16 ND 0-8 √ realdata (aclTensor*) Input Data under test for precision comparison. The data type and shape must be consistent with those of golden. FLOAT16, FLOAT, BFLOAT16 ND 0-8 √ out (aclTensor*) Output Status code of the precision comparison result. Only 0-dim tensors are supported. Return value descriptions:
0: Success.
1: Internal input parameter validation of the precision comparison operator fails.
2: Internal execution of the precision comparison operator fails.
503: Precision comparison error. Management plane fault reporting is not triggered.UINT32 ND 0 - workspaceSize (uint64_t*) Output Returns the size of the workspace to be allocated on the Device side. - - - - - executor (aclOpExecutor**) Output Returns the op executor, which contains the computation flow of the operator. - - - - - Return value
aclnnStatus: return status code. For details, see aclnn Return Codes.
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 The input golden, realdata, or out is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data type of golden, realdata, or out is out of the supported range. The data types of golden and realdata are not consistent. The number of dimensions of golden or realdata is greater than 8. The shapes of golden and realdata are inconsistent. The number of dimensions of out is not 0.
aclnnPrecisionCompare
Parameters:
Parameter Input/Output Description workspace Input Workspace memory address allocated on the Device. workspaceSize Input Size of the workspace allocated on the Device, obtained by calling the first-phase API aclnnPrecisionCompareGetWorkspaceSize. executor Input Op executor that contains the operator computation flow. stream Input Stream for executing tasks. Return value
aclnnStatus: return value. For details, see aclnn return codes.
Constraints
- Deterministic computation:
- aclnnPrecisionCompare defaults to a deterministic implementation.
Example
The following sample code is for reference only. For details about compilation and execution, see Compile and Run Samples.
#include <iostream>
#include <vector>
#include <cmath>
#include <limits>
#include "acl/acl.h"
#include "aclnnop/aclnn_precision_compare.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 shape_size = 1;
for (auto i : shape) {
shape_size *= i;
}
return shape_size;
}
int Init(int32_t deviceId, aclrtStream* stream) {
// Boilerplate code for resource initialization.
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 allocate 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 strides for the 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 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);
// Handle the check as required.
CHECK_RET(ret == 0, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);
// 2. Construct inputs and outputs. Custom construction is required based on the API.
std::vector<int64_t> goldenShape = {4, 2};
std::vector<int64_t> realdataShape = {4, 2};
std::vector<int64_t> outShape = {};
void* goldenDeviceAddr = nullptr;
void* realdataDeviceAddr = nullptr;
void* outDeviceAddr = nullptr;
aclTensor* golden = nullptr;
aclTensor* realdata = nullptr;
aclTensor* out = nullptr;
std::vector<float> goldenHostData = {0, 1, 2, 3, 4, 5, 6, 7};
std::vector<float> realdataHostData = {0, 1, 2, 3, 4, 5, 6, 7};
std::vector<uint32_t> outHostData = {0};
// Create the self aclTensor.
ret = CreateAclTensor(goldenHostData, goldenShape, &goldenDeviceAddr, aclDataType::ACL_FLOAT, &golden);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the other aclTensor.
ret = CreateAclTensor(realdataHostData, realdataShape, &realdataDeviceAddr, aclDataType::ACL_FLOAT, &realdata);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the out aclTensor.
ret = CreateAclTensor(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_UINT32, &out);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// 3. Call the CANN operator library API. Replace it with the specific API.
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
// Call the first-phase API of aclnnPrecisionCompare.
ret = aclnnPrecisionCompareGetWorkspaceSize(golden, realdata, out, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnPrecisionCompareGetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
// Allocate device memory based on the workspaceSize computed 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 aclnnPrecisionCompare.
ret = aclnnPrecisionCompare(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnPrecisionCompare failed. ERROR: %d\n", ret); return ret);
// 4. (boilerplate) Synchronously wait for the task to complete.
ret = aclrtSynchronizeStream(stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret);
// 5. Obtain the output value and copy the result from device memory to the host side. Modify it based on the specific API definition.
auto size = GetShapeSize(outShape);
std::vector<uint32_t> resultData(size, 0);
ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), outDeviceAddr, size * sizeof(uint32_t), 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: %d\n", i, resultData[i]);
}
// 6. Release the aclTensor. Modify it based on the specific API definition.
aclDestroyTensor(golden);
aclDestroyTensor(realdata);
aclDestroyTensor(out);
// 7. Release device resources. Modify it based on the specific API definition.
aclrtFree(goldenDeviceAddr);
aclrtFree(realdataDeviceAddr);
aclrtFree(outDeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}