aclnnAddmm&aclnnInplaceAddmm
Supported Products
| Product | Supported |
|---|---|
| √ | |
| √ | |
| × | |
| √ | |
| √ |
Function
Description: Computes the sum of the product of α multiplied by mat1 and mat2 and the product of β multiplied by self.
Formula:
Example:
- For aclnnAddmm, the shape of self is [1, n], the shape of mat1 is [m, k], the shape of mat2 is [k, n], the shape of the matrix multiplication result of mat1 and mat2 is [m, n], and the shape of self can be broadcast to [m, n].
- For aclnnAddmm, the shape of self is [m, 1], the shape of mat1 is [m, k], the shape of mat2 is [k, n], the shape of the matrix multiplication result of mat1 and mat2 is [m, n], and the shape of self can be broadcast to [m, n].
- For aclnnAddmm, the shape of self is [m, n], the shape of mat1 is [m, k], the shape of mat2 is [k, n], and the shape of the matrix multiplication result of mat1 and mat2 is [m, n].
- For aclnnInplaceAddmm, the computation result is directly stored in the memory of the input tensor selfRef. The shape of self is [m, n], the shape of mat1 is [m, k], and the shape of mat2 is [k, n].
Prototype
- aclnnAddmm and aclnnInplaceAddmm implement the same function in different ways. Select a proper operator based on your requirements.
- aclnnAddmm: An output tensor object needs to be created to store the computation result.
- aclnnInplaceAddmm: No output tensor object needs to be created, and the computation result is stored in the memory of the input tensor.
- Each operator has two-phase API calls. First, aclnnAddmmGetWorkspaceSize or aclnnInplaceAddmmGetWorkspaceSize is called to obtain input parameters and calculate the required workspace size based on the computation process. Then, aclnnAddmm or aclnnInplaceAddmm is called to perform computation.
aclnnStatus aclnnAddmmGetWorkspaceSize(
const aclTensor *self,
const aclTensor *mat1,
const aclTensor *mat2,
const aclScalar *beta,
const aclScalar *alpha,
aclTensor *out,
int8_t cubeMathType,
uint64_t *workspaceSize,
aclOpExecutor **executor)aclnnStatus aclnnAddmm(
void *workspace,
uint64_t workspaceSize,
aclOpExecutor *executor,
aclrtStream stream)aclnnStatus aclnnInplaceAddmmGetWorkspaceSize(
const aclTensor *selfRef,
const aclTensor *mat1,
const aclTensor *mat2,
const aclScalar *beta,
const aclScalar *alpha,
int8_t cubeMathType,
uint64_t *workspaceSize,
aclOpExecutor **executor)aclnnStatus aclnnInplaceAddmm(
void *workspace,
uint64_t workspaceSize,
aclOpExecutor *executor,
aclrtStream stream)aclnnAddmmGetWorkspaceSize
Parameters:
Name Input/Output Description Precaution Data Type Data Format Dimension (Shape) Non-contiguous Tensor self Input Bias matrix, which is self in the formula. - The data types of self and mat1@mat2 must meet the type deduction rules (see Deduction Relationship and Constraints).
- It must meet the Broadcast Relationship with mat1@mat2.
- When mat1 is not transposed, the dimensions are (m, k).
- When mat1 is transposed, the dimensions are (k, m).
BFLOAT16, FLOAT16, FLOAT32 ND 2 √ mat1 Input First matrix for matrix multiplication, which is mat1 in the formula. - The data types of self and mat2 must meet the type deduction rules (see Deduction Relationship and Constraints).
- It must meet the Broadcast Relationship with self and mat2.
- When mat1 is not transposed, the dimensions are (m, k).
- When mat1 is transposed, the dimensions are (k, m).
BFLOAT16, FLOAT16, FLOAT32 ND 2 √ mat2 Input Second matrix for matrix multiplication, which is mat2 in the formula. - The data types of self and mat1 must meet the type deduction rules (see Deduction Relationship and Constraints).
- The size of the Reduce dimension of mat2 must be the same as the size of the Reduce dimension of mat1.
- It must meet the Broadcast Relationship with self and mat2.
BFLOAT16, FLOAT16, FLOAT32 ND 2 √ beta(β) Input β in the formula. - FLOAT, FLOAT16, DOUBLE, INT8, INT16, INT32, INT64, UINT8 - - - alpha(α) Input α in the formula. - FLOAT, FLOAT16, DOUBLE, INT8, INT16, INT32, INT64, UINT8 - - - out Output Output matrix of matrix multiplication, which is out in the formula. Its data type and the data type deduced from self and mat2 must meet the type deduction rules (see Deduction Relationship and Constraints). BFLOAT16, FLOAT16, FLOAT32 ND 2 - cubeMathType Input Computation logic of the Cube unit. If the input data types can be deduced from each other, this parameter processes the deduced data type by default. The supported enumerated values are as follows: - 0: KEEP_DTYPE. The input data type is retained for computation.
- 1: ALLOW_FP32_DOWN_PRECISION. The input data can be computed with reduced precision.
- 2: USE_FP16. The input data can be downgraded to FLOAT16 for computation.
- 3: USE_HF32. The input data can be downgraded to HFLOAT32 for computation.
INT8 - - - workspaceSize Output Size of the workspace required to be allocated on the device. - - - - - executor Output Operator executor, containing the operator computation process. - - - - - Atlas training series products andAtlas inference series products :- The data type BFLOAT16 is not supported.
- cubeMathType=0 is not supported when the input data type is FLOAT32.
- cubeMathType=1: If the input data type is FLOAT32, it is converted to FLOAT16 for computation. If the input data type is not FLOAT32, no processing is performed.
- cubeMathType=3 is not supported.
Atlas A2 training products/Atlas A2 inference products andAtlas A3 training series products/Atlas A3 inference series products :- cubeMathType=1: If the input data type is FLOAT32, it is converted to HFLOAT32 for computation. If the input data type is not FLOAT32, no processing is performed.
- cubeMathType=2: If the input data type is BFLOAT16, this option is not supported.
- cubeMathType=3: If the input data type is FLOAT32, it is converted to HFLOAT32 for computation. If the input data type is not FLOAT32, this option is not supported.
Returns:
aclnnStatus: status code. For details, see aclnn Return Codes.
The first-phase API implements input parameter verification. The following errors may be thrown.
Return Error Code Description ACLNN_ERR_PARAM_NULLPTR 161001 The passed self, mat1, mat2, or out is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data type or format of self or mat2 is not supported. mat1 and mat2 do not meet the condition of multiplication. The shape of out is inconsistent with that of mat1@mat2.
aclnnAddmm
Parameters:
Name Input/Output Description workspace Input Address of the workspace to be allocated on the device. workspaceSize Input Size of the workspace to be allocated on the device, obtained by calling aclnnAddmmGetWorkspaceSize. executor Input Operator executor, containing the operator computation process. stream Input Stream for executing the task. Returns:
aclnnStatus: status code. For details, see aclnn Return Codes.
aclnnInplaceAddmmGetWorkspaceSize
Parameters:
Name Input/Output Description Precaution Data Type Data Format Dimension (Shape) Non-contiguous Tensor selfRef Input Inputs self and out in the formula. - The data types of self and mat2 must meet the type deduction rules (see Deduction Relationship and Constraints).
- It must meet the Broadcast Relationship with mat2.
BFLOAT16, FLOAT16, FLOAT32 ND 2 √ mat1 Input First matrix for matrix multiplication, which is mat1 in the formula. - Its data type and the data type of selfRef must meet the type deduction rules (see Deduction Relationship and Constraints).
- The size of the Reduce dimension of mat1 must be the same as the size of the Reduce dimension of selfRef.
- It must meet the Broadcast Relationship with mat2.
BFLOAT16, FLOAT16, FLOAT32 ND 2 √ mat2 Input Second matrix for matrix multiplication, which is mat2 in the formula. - Its data type and the data type of selfRef must meet the type deduction rules (see Deduction Relationship and Constraints).
- The size of the Reduce dimension of mat2 must be the same as the size of the Reduce dimension of selfRef.
- It must meet the Broadcast Relationship with mat1.
BFLOAT16, FLOAT16, FLOAT32 ND 2 √ beta(β) Input β in the formula. - FLOAT, FLOAT16, DOUBLE, INT8, INT16, INT32, INT64, UINT8 - - - alpha(α) Input α in the formula. - FLOAT, FLOAT16, DOUBLE, INT8, INT16, INT32, INT64, UINT8 - - - cubeMathType Input Computation logic of the Cube unit. If the input data types can be deduced from each other, this parameter processes the deduced data type by default. The supported enumerated values are as follows: - 0: KEEP_DTYPE. The input data type is retained for computation.
- 1: ALLOW_FP32_DOWN_PRECISION. The input data can be computed with reduced precision.
- 2: USE_FP16. The input data can be downgraded to FLOAT16 for computation.
- 3: USE_HF32. The input data can be downgraded to HFLOAT32 for computation.
INT8 - - - workspaceSize Output Size of the workspace required to be allocated on the device. - - - - - executor Output Operator executor, containing the operator computation process. - - - - - Atlas training series products andAtlas inference series products :- The data type BFLOAT16 is not supported.
- cubeMathType=0 is not supported when the input data type is FLOAT32.
- cubeMathType=1: If the input data type is FLOAT32, it is converted to FLOAT16 for computation. If the input data type is not FLOAT32, no processing is performed.
- cubeMathType=3 is not supported.
Atlas A2 training products/Atlas A2 inference products andAtlas A3 training series products/Atlas A3 inference series products :- cubeMathType=1: If the input data type is FLOAT32, it is converted to HFLOAT32 for computation. If the input data type is not FLOAT32, no processing is performed.
- cubeMathType=2: If the input data type is BFLOAT16, this option is not supported.
- cubeMathType=3: If the input data type is FLOAT32, it is converted to HFLOAT32 for computation. If the input data type is not FLOAT32, this option is not supported.
Returns:
aclnnStatus: status code. For details, see aclnn Return Codes.
The first-phase API implements input parameter verification. The following errors may be thrown.
Return Error Code Description ACLNN_ERR_PARAM_NULLPTR 161001 The passed selfRef, mat1, or mat2 is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data type or format of selfRef or mat2 is not supported. mat1 and mat2 do not meet the condition of multiplication. The shape of selfRef is inconsistent with that of mat1@mat2.
aclnnInplaceAddmm
Parameters:
Name Input/Output Description workspace Input Address of the workspace to be allocated on the device. workspaceSize Input Size of the workspace to be allocated on the device, obtained by calling aclnnInplaceAddmmGetWorkspaceSize. executor Input Operator executor, containing the operator computation process. stream Input Stream for executing the task. Returns:
aclnnStatus: status code. For details, see aclnn Return Codes.
Constraints
- Determinism:
Atlas training series products andAtlas inference series products : aclnnAddmm&aclnnInplaceAddmm defaults to a non-deterministic implementation. You can call aclrtCtxSetSysParamOpt to enable deterministic compute.
- Compute consistency:
Atlas training series products andAtlas inference series products :- If strong consistency compute is enabled, the compute result is deterministic, meaning that multiple executions will generate the same result. In addition, the compute result is irrelevant to the data location.
- aclnnAddmm&aclnnInplaceAddmm defaults to a non-consistent implementation. You can call aclrtCtxSetSysParamOpt to enable consistency compute.
- For example, when performing matrix multiplication, the order of accumulation across different basic blocks may vary, which may lead to slight differences in results for the same data in different rows. However, when strong consistency compute is enabled, the results will remain consistent across rows as long as the inputs are the same.
Example
The following example is for reference only. For details, see Compilation and Running Sample.
#include <iostream>
#include <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_addmm.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) {
// (Fixed writing) 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 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 the data from the host to the device.
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);
// Compute 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. (Fixed writing) Initialize the device and stream. For details, see the ACL API manual.
// Set the device ID in use.
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 the input and output based on the API.
std::vector<int64_t> selfShape = {2, 4};
std::vector<int64_t> mat1Shape = {2, 3};
std::vector<int64_t> mat2Shape = {3, 4};
std::vector<int64_t> outShape = {2, 4};
void* selfDeviceAddr = nullptr;
void* mat1DeviceAddr = nullptr;
void* mat2DeviceAddr = nullptr;
void* outDeviceAddr = nullptr;
aclTensor* self = nullptr;
aclTensor* mat1 = nullptr;
aclTensor* mat2 = nullptr;
aclScalar* alpha = nullptr;
aclScalar* beta = nullptr;
aclTensor* out = nullptr;
std::vector<float> selfHostData = {0, 1, 2, 3, 4, 5, 6, 7};
std::vector<float> mat1HostData = {1, 1, 1, 2, 2, 2};
std::vector<float> mat2HostData = {1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4};
std::vector<float> outHostData(8, 0);
int8_t cubeMathType = 1;
float alphaValue = 1.2f;
float betaValue = 1.0f;
// Create a self aclTensor.
ret = CreateAclTensor(selfHostData, selfShape, &selfDeviceAddr, aclDataType::ACL_FLOAT, &self);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create a mat1 aclTensor.
ret = CreateAclTensor(mat1HostData, mat1Shape, &mat1DeviceAddr, aclDataType::ACL_FLOAT, &mat1);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create a mat2 aclTensor.
ret = CreateAclTensor(mat2HostData, mat2Shape, &mat2DeviceAddr, aclDataType::ACL_FLOAT, &mat2);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create an alpha aclScalar.
alpha = aclCreateScalar(&alphaValue,aclDataType::ACL_FLOAT);
CHECK_RET(alpha != nullptr, return ret);
// Create a beta aclScalar.
beta = aclCreateScalar(&betaValue,aclDataType::ACL_FLOAT);
CHECK_RET(beta != nullptr, return ret);
// Create an out aclTensor.
ret = CreateAclTensor(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_FLOAT, &out);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// 3. Call the CANN operator library API, which needs to be replaced with the actual API.
uint64_t workspaceSize = 0;
aclOpExecutor* executor = nullptr;
// Call the first-phase API of aclnnAddmm.
ret = aclnnAddmmGetWorkspaceSize(self, mat1, mat2, beta, alpha, out, cubeMathType, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnAddmmGetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
// Allocate device memory based on 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 aclnnAddmm.
ret = aclnnAddmm(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnAddmm failed. ERROR: %d\n", ret); return ret);
// 4. (Fixed writing) Wait until the task execution is 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 the device to the host. Modify the code based on the API definition.
auto size = GetShapeSize(outShape);
std::vector<float> resultData(size, 0);
ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), outDeviceAddr,
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]);
}
// aclnnInplaceAddmm
// Step 3. Call the CANN operator library API.
LOG_PRINT("\ntest aclnnInplaceAddmm\n");
// Call the first-phase API of aclnnInplaceAddmm.
ret = aclnnInplaceAddmmGetWorkspaceSize(self, mat1, mat2, beta, alpha, cubeMathType, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplaceAddmmGetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
// Allocate device memory based on workspaceSize calculated by the first-phase API.
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 aclnnInplaceAddmm.
ret = aclnnInplaceAddmm(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplaceAddmm failed. ERROR: %d\n", ret); return ret);
// Step 4. (Fixed writing) Wait until the task execution is complete.
ret = aclrtSynchronizeStream(stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret);
// Step 5. Obtain the output value and copy the result from the device memory to the host.
ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), selfDeviceAddr,
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]);
}
// 6. Release aclTensor and aclScalar. Modify the configuration based on the API definition.
aclDestroyTensor(self);
aclDestroyTensor(mat1);
aclDestroyTensor(mat2);
aclDestroyScalar(beta);
aclDestroyScalar(alpha);
aclDestroyTensor(out);
// 7. Release device resources. Modify the configuration based on the API definition.
aclrtFree(selfDeviceAddr);
aclrtFree(mat1DeviceAddr);
aclrtFree(mat2DeviceAddr);
aclrtFree(outDeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}