aclnnMatmulAbftVerify
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: Implements a GEMM fault-tolerance detection operator based on the variance estimation adaptive threshold algorithm (V-ABFT). The operator takes matrices A and B and the pre-computed matrix multiplication result C as inputs, performs block-wise ABFT verification on C, detects silent computation errors, and outputs row-wise detection results.
Features
Adaptive threshold: The operator automatically determines the threshold for comparison based on the matrix size and value range, ensuring the detection rate while avoiding false detection.
- For the adaptive threshold algorithm and its deduction, see the documentation at https://gitee.com/yihenggao/v-abft.
The computational cost is significantly lower than that of recomputation-based fault tolerance solutions. When the matrix dimensions are m=n=k=a, this operator requires only 8a^2 floating-point operations, while recomputation requires 2a^3 floating-point operations. The fault tolerance threshold also matches this algorithm.
Formula:
The threshold is dynamically estimated from the local statistics (mean and standard deviation) of input matrices A and B, without relying on the output of matrix C.
Description:
- Input matrices A and B and the pre-computed C undergo checksum encoding, threshold estimation, and verification comparison, and the compressed row-wise fault detection result tensor
comp_rowis output, where 1 indicates correctness and 0 indicates a detected error.
- Input matrices A and B and the pre-computed C undergo checksum encoding, threshold estimation, and verification comparison, and the compressed row-wise fault detection result tensor
Function Prototype
Each operator uses a two-phase API. You must first call the "aclnnMatmulAbftVerifyGetWorkspaceSize" API to obtain the input parameters and calculate the workspace size required for the computation, and then call the "aclnnMatmulAbftVerify" API to perform the computation.
aclnnStatus aclnnMatmulAbftVerifyGetWorkspaceSize(
const aclTensor *a,
const aclTensor *b,
const aclTensor *c,
const aclTensor *checksumWeight,
double eMax,
const aclTensor *compRow,
uint64_t *workspaceSize,
aclOpExecutor **executor);aclnnStatus aclnnMatmulAbftVerify(
void *workspace,
uint64_t workspaceSize,
aclOpExecutor *executor,
aclrtStream stream);aclnnMatmulAbftVerifyGetWorkspaceSize
Parameters:
where .
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 Values Error Code Description ACLNN_ERR_PARAM_NULLPTR 161001 A required input, output, or required attribute is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data type or data format of a, b, c, weight, eMax, beSplitFactor, or compRow is unsupported. The dimensions of a and b are not 2. The 1st dimension (K) of a is not equal to the 0th dimension (K) of b. eMax is negative. reduceCores is less than 0.
aclnnMatmulAbftVerify
Parameters:
Parameter Input/Output Description workspace Input Memory address of the workspace allocated on the device. workspaceSize Input Size of the workspace allocated on the device, obtained by calling the first-phase API aclnnMatmulAbftVerifyGetWorkspaceSize. executor Input Executor of the op, containing the operator computation flow. stream Input Stream used to execute the task. Return value
aclnnStatus: return status code. For details, see aclnn Return Codes.
Constraints
Deterministic computation:
- aclnnMatmulAbftVerify defaults to a deterministic implementation.
The input matrices a, b, and c must be 2D, with shapes [M, K], [K, N], and [M, N], respectively, and the first dimension (K) of a must be equal to the 0th dimension (K) of b.
The shape of the input vector weight must be [N].
The supported data type combinations are:
a b c checksumWeight FLOAT16 FLOAT16 FLOAT32 FLOAT16 BFLOAT16 BFLOAT16 FLOAT32 BFLOAT16 FLOAT32 FLOAT32 FLOAT32 FLOAT32 Unsupported use cases:
- Data formats other than ND are not supported.
- Non-contiguous tensors are not supported.
- Use cases where the dimensions of a, b, and c are not 2 are not supported.
Workspace Usage Design
The workspace consists of two parts:
- A fixed 16 MiB system workspace.
- The user workspace, which sequentially holds 13 internal intermediate tensors and the FT internal temporary area.
The operator tiling phase calculates the total size based on M, N, K, and the input precision, and returns it through workspaceSize. The caller must allocate contiguous device memory of no less than this size, rather than only the compRow size. The start addresses of all intermediate tensors are aligned up to 32 bytes.
Definition:
splitN = ceil(N / 256)
rowSplitLen = M * splitN
bStatLen = ceil(splitN / 8) * 8 + 8
beLen = K * splitN
align32(x) = ceil(x / 32) * 32Let W be the start address of the user workspace. Inside the kernel, W = AscendC::GetUserWorkspace(workspace); for device address calculation outside the operator, the current implementation is equivalent to W = (uint8_t *)workspace + 16 MiB.
The following offsets are all relative to W. Let S0 = 0. The start offset of each tensor is Oi = align32(Si), and its end position is Si+1 = Oi + elementCount × elementSize:
| Order | Intermediate Result | Start Address | Element Type | Element Count |
|---|---|---|---|---|
| 0 | z_row | W + O0 | FLOAT32 | rowSplitLen |
| 1 | d_row | W + O1 | FLOAT32 | rowSplitLen |
| 2 | threshold | W + O2 | FLOAT32 | rowSplitLen |
| 3 | b_mean_abs | W + O3 | FLOAT32 | bStatLen |
| 4 | b_mean_square | W + O4 | FLOAT32 | bStatLen |
| 5 | b_var | W + O5 | FLOAT32 | bStatLen |
| 6 | be | W + O6 | Same as a | beLen |
| 7 | be_for_aiv | W + O7 | FLOAT16 if a is FLOAT16, otherwise FLOAT32 | beLen |
| 8 | b_max_slice | W + O8 | FLOAT32 | beLen |
| 9 | b_min_slice | W + O9 | FLOAT32 | beLen |
| 10 | a_max | W + O10 | FLOAT32 | M |
| 11 | a_mean | W + O11 | FLOAT32 | M |
| 12 | a_min | W + O12 | FLOAT32 | M |
| FLOAT16 and BFLOAT16 elements occupy 2 bytes each, and FLOAT32 elements occupy 4 bytes each. After the 13th segment, 32-byte alignment is applied again. The remaining area is the FT internal temporary area, whose logical size is: |
M * (splitN + 1) * sizeof(float)The constant factor 1/K used in the AMean computation does not occupy the workspace. The tiling phase generates this scalar based on the input precision, and the kernel uses it to directly initialize the L1 internal buffer of FT when computing AMean for the first time. Therefore, this factor cannot be copied out as a workspace intermediate result.
If you need to debug and copy out a specific intermediate result, perform a device-to-host copy starting from the corresponding W + Oi in the preceding table after the operator finishes execution and before the workspace is released or reused. The number of bytes to copy is "element count × element byte size". Reading the workspace externally is a debugging capability, not a stable public output interface. If the layout changes, use the address partitioning code under the Workspace ABI comment in matmul_abft_verify.cpp as the authoritative reference; that code also serves as a sample implementation of offset calculation.
Example
The following example uses FLOAT32 inputs. The sample first calls aclnnGemm to compute C = A × B, and then passes the matmul result C to aclnnMatmulAbftVerify for verification.
#include <cstdint>
#include <cstdio>
#include <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_gemm.h"
#include "aclnnop/aclnn_matmul_abft_verify.h"
#define CHECK_RET(cond, action) \
do { \
if (!(cond)) { \
action; \
} \
} while (0)
int64_t GetElementCount(const std::vector<int64_t>& shape)
{
int64_t count = 1;
for (int64_t dim : shape) {
count *= dim;
}
return count;
}
template <typename T>
int CreateAclTensor(const std::vector<T>& hostData,
const std::vector<int64_t>& shape,
aclDataType dataType,
void** deviceAddr,
aclTensor** tensor)
{
const uint64_t bytes = static_cast<uint64_t>(hostData.size()) * sizeof(T);
auto ret = aclrtMalloc(deviceAddr, bytes, ACL_MEM_MALLOC_HUGE_FIRST);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = aclrtMemcpy(*deviceAddr, bytes, hostData.data(), bytes,
ACL_MEMCPY_HOST_TO_DEVICE);
CHECK_RET(ret == ACL_SUCCESS, return ret);
std::vector<int64_t> strides(shape.size(), 1);
for (int64_t i = static_cast<int64_t>(shape.size()) - 2; i >= 0; --i) {
strides[i] = shape[i + 1] * strides[i + 1];
}
*tensor = aclCreateTensor(shape.data(), shape.size(), dataType,
strides.data(), 0, ACL_FORMAT_ND,
shape.data(), shape.size(), *deviceAddr);
CHECK_RET(*tensor != nullptr, return ACL_ERROR_FAILURE);
return ACL_SUCCESS;
}
int main()
{
constexpr int32_t deviceId = 0;
constexpr int64_t M = 4096;
constexpr int64_t N = 4096;
constexpr int64_t K = 4096;
constexpr double eMax = 0.00002;
const int64_t splitN = (N + 255) / 256;
const std::vector<int64_t> aShape{M, K};
const std::vector<int64_t> bShape{K, N};
const std::vector<int64_t> cShape{M, N};
const std::vector<int64_t> checksumWeightShape{N};
const std::vector<int64_t> compRowShape{((M + 7) / 8) * splitN};
// When all checksumWeight values are 1, it corresponds to the ordinary blocked row checksum.
std::vector<float> hostA(GetElementCount(aShape), 0.5F);
std::vector<float> hostB(GetElementCount(bShape), 0.25F);
std::vector<float> hostC(GetElementCount(cShape), 0.0F);
std::vector<float> hostChecksumWeight(GetElementCount(checksumWeightShape), 1.0F);
std::vector<uint8_t> hostCompRow(GetElementCount(compRowShape), 0);
aclrtContext context = nullptr;
aclrtStream stream = nullptr;
auto ret = aclInit(nullptr);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = aclrtSetDevice(deviceId);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = aclrtCreateContext(&context, deviceId);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = aclrtSetCurrentContext(context);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = aclrtCreateStream(&stream);
CHECK_RET(ret == ACL_SUCCESS, return ret);
void* devA = nullptr;
void* devB = nullptr;
void* devC = nullptr;
void* devGemmAddend = nullptr;
void* devChecksumWeight = nullptr;
void* devCompRow = nullptr;
aclTensor* tensorA = nullptr;
aclTensor* tensorB = nullptr;
aclTensor* tensorC = nullptr;
aclTensor* tensorGemmAddend = nullptr;
aclTensor* tensorChecksumWeight = nullptr;
aclTensor* tensorCompRow = nullptr;
ret = CreateAclTensor(hostA, aShape, ACL_FLOAT, &devA, &tensorA);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(hostB, bShape, ACL_FLOAT, &devB, &tensorB);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(hostC, cShape, ACL_FLOAT, &devC, &tensorC);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(hostC, cShape, ACL_FLOAT,
&devGemmAddend, &tensorGemmAddend);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(hostChecksumWeight, checksumWeightShape, ACL_FLOAT,
&devChecksumWeight, &tensorChecksumWeight);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(hostCompRow, compRowShape, ACL_UINT8,
&devCompRow, &tensorCompRow);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Preceding matmul: C = 1.0 * A * B + 0.0 * gemmAddend.
uint64_t gemmWorkspaceSize = 0;
aclOpExecutor* gemmExecutor = nullptr;
ret = aclnnGemmGetWorkspaceSize(
tensorA, tensorB, tensorGemmAddend,
1.0F, 0.0F, 0, 0, tensorC, 0,
&gemmWorkspaceSize, &gemmExecutor);
CHECK_RET(ret == ACL_SUCCESS, return ret);
void* gemmWorkspace = nullptr;
if (gemmWorkspaceSize > 0) {
ret = aclrtMalloc(&gemmWorkspace, gemmWorkspaceSize,
ACL_MEM_MALLOC_HUGE_FIRST);
CHECK_RET(ret == ACL_SUCCESS, return ret);
}
ret = aclnnGemm(gemmWorkspace, gemmWorkspaceSize, gemmExecutor, stream);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = aclrtSynchronizeStream(stream);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Perform ABFT verification on the result of the preceding GEMM.
uint64_t workspaceSize = 0;
aclOpExecutor* executor = nullptr;
ret = aclnnMatmulAbftVerifyGetWorkspaceSize(
tensorA, tensorB, tensorC, tensorChecksumWeight,
eMax, tensorCompRow, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, return ret);
void* workspace = nullptr;
if (workspaceSize > 0) {
ret = aclrtMalloc(&workspace, workspaceSize,
ACL_MEM_MALLOC_HUGE_FIRST);
CHECK_RET(ret == ACL_SUCCESS, return ret);
}
ret = aclnnMatmulAbftVerify(workspace, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = aclrtSynchronizeStream(stream);
CHECK_RET(ret == ACL_SUCCESS, return ret);
aclDestroyTensor(tensorA);
aclDestroyTensor(tensorB);
aclDestroyTensor(tensorC);
aclDestroyTensor(tensorGemmAddend);
aclDestroyTensor(tensorChecksumWeight);
aclDestroyTensor(tensorCompRow);
aclrtFree(devA);
aclrtFree(devB);
aclrtFree(devC);
aclrtFree(devGemmAddend);
aclrtFree(devChecksumWeight);
aclrtFree(devCompRow);
if (gemmWorkspace != nullptr) {
aclrtFree(gemmWorkspace);
}
if (workspace != nullptr) {
aclrtFree(workspace);
}
aclrtDestroyStream(stream);
aclrtDestroyContext(context);
aclrtResetDevice(deviceId);
aclFinalize();
return ACL_SUCCESS;
}