aclnnSilentCheck
Applicable Products
Ascend 950PR&950DT products : Not 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: The SilentCheck operator identifies whether a silent detection fault is triggered by comparing the input feature value (
val) against absolute and relative thresholds. It also supports controlling whether to trigger an alarm or checkpoint-based training resumption upon a fault through the environment variable (npuAsdDetect) passed in from the framework side. By default (i.e., whennpuAsdDetect=1), only logs are printed.Formula:
- If the current input
valis inf/nan, orvalexceeds the absolute thresholdcThreshL1, or the jump exceeds the relative thresholdcCoeffL1, it is identified as an L1 fault. If the environment variablenpuAsdDetectis 2, a log is printed and checkpoint-based training resumption is triggered. If the environment variablenpuAsdDetectis 1,sfdaRefandstepRefare updated and the function returns normally. - If the current input
valexceeds the absolute thresholdcThreshL2, or the jump exceeds the relative thresholdcCoeffL2, it is identified as an L2 fault. An alarm is printed, andsfdaRefandstepRefare updated before normal return. - If neither an L1 fault nor an L2 alarm is triggered, it is a normal case: if
npuAsdDetectis 3, the eigenvalue is printed; otherwise,sfdaRefandstepRefare updated before normal return. sfdaRefis [pre_val, min_val, max_val], representing [last detected val, historical minimum val, historical maximum val].stepRefis the detection count, which increments by one for each detection.
- If the current input
Function Prototype
Each operator uses a two-phase API. You must first call the "aclnnSilentCheckGetWorkspaceSize" API to obtain the workspace size required for the computation and the executor that contains the computation process of the operator, and then call the "aclnnSilentCheck" API to perform the computation.
aclnnStatus aclnnSilentCheckGetWorkspaceSize(
const aclTensor *val,
aclTensor *inputGradRef,
aclTensor *sfdaRef,
aclTensor *stepRef,
const int32_t cMinSteps,
const float cThreshL1,
const float cCoeffL1,
const float cThreshL2,
const float cCoeffL2,
const int32_t npuAsdDetect,
aclTensor* result,
uint64_t *workspaceSize,
aclOpExecutor **executor)aclnnStatus aclnnSilentCheck(
void *workspace,
uint64_t workspaceSize,
aclOpExecutor *executor,
aclrtStream stream)aclnnSilentCheckGetWorkspaceSize
Parameters:
Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor val Input Current input value. - FLOAT, FLOAT16, BFLOAT16 ND 0 √ inputGradRef Input Gradient tensor of the model input. - FLOAT, FLOAT16, BFLOAT16 ND - √ sfdaRef Input Value from the previous check. - FLOAT ND 3 √ stepRef Input Current step. - INT64 ND - √ cMinSteps Input Minimum number of steps to trigger the jump detection. Current recommended value: 7 INT32 - - - cThreshL1 Input Absolute value that triggers the L1 fault threshold. Currently recommended value: 1000000 FLOAT - - - cCoeffL1 Input Value change that triggers the L1 fault threshold. Current recommended value: 100000 FLOAT - - - cThreshL2 Input Absolute value that triggers the L2 fault threshold. Current recommended value: 10000, and cThreshL1>cThreshL2 FLOAT - - - cCoeffL2 Input Fault threshold for triggering an L2 fault on a jump. Currently, the recommended value is 5000, and cCoeffL1>cCoeffL2. FLOAT - - - npuAsdDetect Input Environment variable. Value: 1, 2, 3 INT32 - - - result Output Indicates whether silent check is triggered and the fault level triggered. - INT32 ND 1 √ workspaceSize Output Returns the workspace size to be allocated on the Device. - - - - - executor Output Returns the op executor, which contains the operator computation flow. - - - - - 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 val, inputGradRef, sfdaRef, or stepRef is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data type of val, inputGradRef, sfdaRef, or stepRef is not within the supported range. The shape of val, inputGradRef, sfdaRef, or stepRef does not meet the requirements.
aclnnSilentCheck
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 the first-phase API aclnnSilentCheckGetWorkspaceSize. executor Input Executor of the operator, which contains the operator computation flow. stream Input Stream for task execution. Return value:
aclnnStatus: return status code. For details, see aclnn Return Codes.
Constraints
- Deterministic computation:
- aclnnSilentCheck defaults to a deterministic implementation.
Example
The sample code is for reference only. For details about compilation and execution, see Compile and Run Samples.
#include <iostream>
#include <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_silent_check.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;
}
void PrintOutResult(std::vector<int64_t> &shape, void** deviceAddr) {
auto size = GetShapeSize(shape);
std::vector<int32_t> resultData(size, 0);
auto ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]),
*deviceAddr, 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);
for (int64_t i = 0; i < size; i++) {
LOG_PRINT("result[%ld] is: %d\n", i, resultData[i]);
}
}
int Init(int32_t deviceId, aclrtStream* stream) {
// Boilerplate: ACL 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 memory on the device.
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 data to the device 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 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 aclCreateTensor 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 external API list.
// Set deviceId according to 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 the input and output, which must be custom-built based on the API.
std::vector<int64_t> valShape = {};
std::vector<int64_t> inputGradShape = {4, 2};
std::vector<int64_t> sfdaShape = {3};
std::vector<int64_t> stepShape = {1};
std::vector<int64_t> resultShape = {1};
void* valDeviceAddr = nullptr;
void* inputGradDeviceAddr = nullptr;
void* sfdaDeviceAddr = nullptr;
void* stepDeviceAddr = nullptr;
void* resultDeviceAddr = nullptr;
aclTensor* val = nullptr;
aclTensor* inputGrad = nullptr;
aclTensor* sfda = nullptr;
aclTensor* step = nullptr;
aclTensor* result = nullptr;
std::vector<float> valHostData = {160.0};
std::vector<float> inputGradHostData = {0, 1, 2, 3, 4, 5, 6, 7};
std::vector<float> sfdaHostData = {70.0, 200.0, 400.0};
std::vector<int64_t> stepHostData = {7};
std::vector<int32_t> resultHostData = {0};
int32_t c_min_steps = 7;
float c_thresh_l1 = 1000000;
float c_coeff_l1 = 100000;
float c_thresh_l2 = 10000;
float c_coeff_l2 = 5000;
int32_t npu_asd_detect = 3;
// Create the val aclTensor.
ret = CreateAclTensor(valHostData, valShape, &valDeviceAddr, aclDataType::ACL_FLOAT, &val);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the input_grad aclTensor.
ret = CreateAclTensor(inputGradHostData, inputGradShape, &inputGradDeviceAddr, aclDataType::ACL_FLOAT, &inputGrad);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the sfda aclTensor.
ret = CreateAclTensor(sfdaHostData, sfdaShape, &sfdaDeviceAddr, aclDataType::ACL_FLOAT, &sfda);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the step aclTensor.
ret = CreateAclTensor(stepHostData, stepShape, &stepDeviceAddr, aclDataType::ACL_INT64, &step);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the result aclTensor.
ret = CreateAclTensor(resultHostData, resultShape, &resultDeviceAddr, aclDataType::ACL_INT32, &result);
if (result == nullptr) {
std::cout << "result is nullptr!" << std::endl;
return 0;
}
CHECK_RET(ret == ACL_SUCCESS, return ret);
// 3. Call the CANN operator library API. Change it to the specific HostApi.
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
// Call the first-phase API of aclnnSilentCheck.
ret = aclnnSilentCheckGetWorkspaceSize(val, inputGrad, sfda, step, c_min_steps, c_thresh_l1, c_coeff_l1, c_thresh_l2, c_coeff_l2, npu_asd_detect, result, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnSilentCheckGetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
// Allocate 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 aclnnAdd.
ret = aclnnSilentCheck(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnSilentCheck failed. ERROR: %d\n", ret); return ret);
// 4. (boilerplate) Wait for the task execution to complete synchronously.
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 on the device to the host. Modify it according to the definition of the specific API.
PrintOutResult(resultShape, &resultDeviceAddr);
// 6. Release aclTensor and aclScalar. Modify it according to the definition of the specific API.
aclDestroyTensor(val);
aclDestroyTensor(inputGrad);
aclDestroyTensor(sfda);
aclDestroyTensor(step);
aclDestroyTensor(result);
// 7. Release device resources. Modify them according to the API definition.
aclrtFree(valDeviceAddr);
aclrtFree(inputGradDeviceAddr);
aclrtFree(sfdaDeviceAddr);
aclrtFree(stepDeviceAddr);
aclrtFree(resultDeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}