aclnnAddmm&aclnnInplaceAddmm
Applicable Products
| Product | Supported |
|---|---|
| √ | |
| √ | |
| √ | |
| × | |
| √ | |
| √ |
Function
Description: Computes α multiplied by the product of mat1 and mat2, then adds the product of β and self.
Formula:
Example:
For the aclnnAddmm API, 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 the aclnnAddmm API, 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 the aclnnAddmm API, 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 the aclnnInplaceAddmm API, the computation result is directly stored in the memory of the input tensor selfRef. The shape of selfRef is [m, n], the shape of mat1 is [m, k], and the shape of mat2 is [k, n].
Function Prototype
aclnnAddmm and aclnnInplaceAddmm implement the same functionality. The differences are as follows. Select the appropriate operator based on your actual scenario.
aclnnAddmm: Requires creating a new output tensor object to store the computation result.
aclnnInplaceAddmm: Does not require creating a new output tensor object. The computation result is stored directly in the memory of the input tensor.
Each operator uses a two-phase API. You must first call the "aclnnAddmmGetWorkspaceSize" or "aclnnInplaceAddmmGetWorkspaceSize" API to obtain the input parameters and calculate the required workspace size based on the computation flow, and then call the "aclnnAddmm" or "aclnnInplaceAddmm" API to perform the 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
Parameter Input/Output Description Instruction Data Type Data Format Dimension (Shape) Non-contiguous Tensor self Input Represents the bias matrix, i.e., self in the formula. The data type must satisfy the data type deduction rules with mat1@mat2 (see Deduction Relationship and Constraints). BFLOAT16, FLOAT16, FLOAT32 ND 1~2 √ Must satisfy the broadcast relationship with mat1@mat2. mat1 Input Represents the first matrix of the matrix multiplication, i.e., mat1 in the formula. The data type must satisfy the data type deduction rules with mat2 (see Deduction Relationship and Constraints). BFLOAT16, FLOAT16, FLOAT32 ND 2 √ Must satisfy the broadcast relationship with self and mat2. When mat1 is not transposed, each dimension represents: (m, k). When mat1 is transposed, each dimension represents: (k, m). mat2 Input Represents the second matrix in Matrix Multiplication, i.e., mat2 in the formula. The data type must satisfy the data type deduction rules with mat1 (see deduction relationship and Constraints). BFLOAT16, FLOAT16, FLOAT32 ND 2 √ The reduce dimension of mat2 must have the same size as the reduce dimension of mat1. Must satisfy the broadcast relationship with self and mat1. When mat2 is not transposed, each dimension is represented as: (k, n). When mat2 is transposed, each dimension is expressed as (n, k). beta(β) Input Represents β in the formula. - FLOAT, FLOAT16, DOUBLE, INT8, INT16, INT32, INT64, UINT8 - - - alpha(α) Input Represents α in the formula. - FLOAT, FLOAT16, DOUBLE, INT8, INT16, INT32, INT64, UINT8 - - - out Output Represents the output matrix of the matrix multiplication, i.e., out in the formula. The data type must be consistent with the data type deduced from self and mat2 (see deduction relationship and Constraints). BFLOAT16, FLOAT16, FLOAT32 ND 2 - cubeMathType Input Specifies the computation logic of the Cube unit. If the input data types have a deduction relationship, this parameter processes the data types after deduction by default. The supported enumeration values are as follows: - 0: KEEP_DTYPE. Keeps the input data type for computation.
- 1: ALLOW_FP32_DOWN_PRECISION. Allows reduced precision computation of the input data.
- 2: USE_FP16. Allows reducing the input precision to FLOAT16 for computation.
- 3: USE_HF32. Allows reducing the input precision to HFLOAT32 for computation.
- 4: USE_FP32_ADD. Uses high-precision mode for computation.
INT8 - - - workspaceSize Output Size of the workspace to be applied on the Device side. - - - - - executor Output Returns the operator executor, which contains the operator computation flow. - - - - - Atlas training products ,Atlas inference products :BFLOAT16 data type is not supported.
When the input data type is FLOAT32, cubeMathType=0 is not supported.
cubeMathType=1: When the input data type is FLOAT32, it is converted to FLOAT16 for computation; when the input is of other data types, no processing is performed.
cubeMathType=3 and 4 are not supported.
Atlas A2 training products/Atlas A2 inference products ,Atlas A3 training products/Atlas A3 inference products :cubeMathType=1: When the input data type is FLOAT32, it is converted to HFLOAT32 for computation; when the input is of other data types, no processing is performed.
cubeMathType=2: This option is not supported when the input data type is BFLOAT16.
cubeMathType=3: When the input data type is FLOAT32, it is converted to HFLOAT32 for computation. This option is not supported for other input data types.
cubeMathType=4: When the input data type is FLOAT16 or BFLOAT16, the addmm process uses high-precision computation. In this case, broadcast between the self input and the matmul result matrix is not supported. When the input data type is FLOAT32 and the K-axis dimension is greater than 2048, grouped accumulation is used for computation.
Ascend 950PR/Ascend 950DT :cubeMathType=1: When the input data type is FLOAT32, it is converted to HFLOAT32 for computation. No processing is performed for other input data types.
cubeMathType=2: This option is not supported when the input data type is BFLOAT16.
cubeMathType=3: When the input data type is FLOAT32, it is converted to HFLOAT32 for computation. This option is not supported for other input data types.
cubeMathType=4 is not supported.
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 The passed self, mat1, mat2, or out is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data type and data format of self and mat2 are not within the supported range. mat1 and mat2 do not meet the multiplication condition. The shapes of out and mat1@mat2 are inconsistent.
aclnnAddmm
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 from the first-phase API aclnnAddmmGetWorkspaceSize. Executor Input Operator executor, which contains the operator computation flow. Stream Input Specifies the stream for task execution. Return Value
aclnnStatus: return code. For details, see aclnn Return Code.
aclnnInplaceAddmmGetWorkspaceSize
Parameters
Parameter Input/Output Description Instruction Data Type Data Format Dimension (Shape) Non-contiguous Tensor selfRef Input That is, the input self and out in the formula. - The data type must satisfy the data type deduction rules with mat2 (see deduction relationship and Constraints).
- The shapes of self and mat1@mat2 must be consistent.
BFLOAT16, FLOAT16, FLOAT32 ND 2 √ mat1 Input Represents the first matrix in the matrix multiplication, i.e., mat1 in the formula. - The data type must satisfy the data type deduction rules with selfRef (see deduction relationship and Constraints).
- The Reduce dimension of mat1 must have the same size as the Reduce dimension of selfRef.
- Must satisfy the broadcast relationship with mat2.
BFLOAT16, FLOAT16, FLOAT32 ND 2 √ mat2 Input Represents the second matrix in the matrix multiplication, i.e., mat2 in the formula. - The data type must satisfy the data type deduction rules with selfRef (see Deduction Relationship and Constraints).
- The Reduce dimension of mat2 must have the same size as the Reduce dimension of selfRef.
- Must satisfy the broadcast relationship with mat1.
BFLOAT16, FLOAT16, FLOAT32 ND 2 √ beta(β) Input Represents β in the formula. - FLOAT, FLOAT16, DOUBLE, INT8, INT16, INT32, INT64, UINT8 - - - alpha(α) Input Represents α in the formula. - FLOAT, FLOAT16, DOUBLE, INT8, INT16, INT32, INT64, UINT8 - - - cubeMathType Input Specifies the computation logic of the Cube unit. If the input data types have a deduction relationship, this parameter processes the data type after deduction by default. Supported enumeration values are as follows: - 0: KEEP_DTYPE, keeps the input data type for computation.
- 1: ALLOW_FP32_DOWN_PRECISION, allows reduced precision computation of the input data.
- 2: USE_FP16, allows reducing the input precision to FLOAT16 for computation.
- 3: USE_HF32, allows reducing the input precision to HFLOAT32 for computation.
- 4: USE_FP32_ADD, uses high-precision mode for computation.
INT8 - - - workspaceSize Output Returns the workspace size to be applied on the Device side. - - - - - executor Output Returns the operator executor, which contains the Operator Computation Flow. - - - - - Atlas training products ,Atlas inference products :BFLOAT16 data type is not supported.
cubeMathType=0 is not supported when the input data type is FLOAT32.
cubeMathType=1: When the input data type is FLOAT32, it is converted to FLOAT16 for computation; when the input is of other data types, no processing is performed.
cubeMathType=3 and 4 are not supported.
Atlas A2 training products/Atlas A2 inference products ,Atlas A3 training products/Atlas A3 inference products :cubeMathType=1: When the input data type is FLOAT32, it is converted to HFLOAT32 for computation; when the input is of other data types, no processing is performed.
cubeMathType=2: This option is not supported when the input data type is BFLOAT16.
cubeMathType=3: When the input data type is FLOAT32, the computation is converted to HFLOAT32. This option is not supported for other input data types.
cubeMathType=4: When the input data type is FLOAT16/BFLOAT16, the addmm process uses high-precision computation. In this case, broadcast between the input self and the matmul result matrix is not supported. When the input data type is FLOAT32 and the k-axis is greater than 2048, grouped accumulation is used for computation.
Ascend 950PR/Ascend 950DT :cubeMathType=1: When the input data type is FLOAT32, the computation is converted to HFLOAT32. No processing is performed for other input data types.
cubeMathType=2: This option is not supported when the input data type is BFLOAT16.
cubeMathType=3: When the input data type is FLOAT32, the computation is converted to HFLOAT32. This option is not supported for other input data types.
cubeMathType=4 is not supported.
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 The passed selfRef, mat1, or mat2 is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data type and data format of selfRef and mat2 are not within the supported range. mat1 and mat2 do not meet the multiplication conditions. The shapes of selfRef and mat1@mat2 are inconsistent.
aclnnInplaceAddmm
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 by the first-phase API aclnnInplaceAddmmGetWorkspaceSize. executor Input Op executor, which 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
Determinism description:
Atlas training products ,Atlas inference products : aclnnAddmm&aclnnInplaceAddmm defaults to a non-deterministic implementation. Deterministic computation can be enabled through aclrtCtxSetSysParamOpt.Ascend 950PR/Ascend 950DT : aclnnAddmm&aclnnInplaceAddmm defaults to a deterministic implementation.Computation consistency description
Atlas training products ,Atlas inference products :When the strong consistency computation feature is enabled, the computation result is deterministic, and multiple executions produce the same output. In addition, the computation result is independent of the data location.
aclnnAddmm&aclnnInplaceAddmm default to a non-deterministic implementation. You can enable consistency by calling aclrtCtxSetSysParamOpt.
For example, during matrix multiplication, the accumulation order of different basic blocks may vary, which can cause slight differences in the computation results of the same data across different rows. However, when strong consistency computation is enabled, the computation result is identical across different rows as long as the input is the same.
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_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) {
// 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 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. See the ACL API manual.
// Fill in the 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 the inputs and outputs. Customize the construction 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 the self aclTensor.
ret = CreateAclTensor(selfHostData, selfShape, &selfDeviceAddr, aclDataType::ACL_FLOAT, &self);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the mat1 aclTensor.
ret = CreateAclTensor(mat1HostData, mat1Shape, &mat1DeviceAddr, aclDataType::ACL_FLOAT, &mat1);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the mat2 aclTensor.
ret = CreateAclTensor(mat2HostData, mat2Shape, &mat2DeviceAddr, aclDataType::ACL_FLOAT, &mat2);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the alpha aclScalar.
alpha = aclCreateScalar(&alphaValue,aclDataType::ACL_FLOAT);
CHECK_RET(alpha != nullptr, return ret);
// Create the beta aclScalar.
beta = aclCreateScalar(&betaValue,aclDataType::ACL_FLOAT);
CHECK_RET(beta != nullptr, return ret);
// Create the 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. Replace with the actual API name.
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[ERROR msg]%s", ret, aclGetRecentErrMsg()); 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[ERROR msg]%s", ret, aclGetRecentErrMsg()); 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[ERROR msg]%s", ret, aclGetRecentErrMsg()); return ret);
// 4. (Boilerplate) Synchronously wait for task execution to complete.
ret = aclrtSynchronizeStream(stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d.\n[ERROR msg]%s", ret, aclGetRecentErrMsg()); return ret);
// 5. Obtain the output value and copy the result from the device-side memory to the host side. Modify based on the specific 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[ERROR msg]%s", ret, aclGetRecentErrMsg()); return ret);
for (int64_t i = 0; i < size; i++) {
LOG_PRINT("result[%ld] is: %f\n", i, resultData[i]);
}
// aclnnInplaceAddmm
// step3: 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[ERROR msg]%s", ret, aclGetRecentErrMsg()); return ret);
// Apply for device memory based on the workspaceSize computed 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[ERROR msg]%s", ret, aclGetRecentErrMsg()); 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[ERROR msg]%s", ret, aclGetRecentErrMsg()); return ret);
// step4 (boilerplate): Synchronously wait for task execution to complete.
ret = aclrtSynchronizeStream(stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d.\n[ERROR msg]%s", ret, aclGetRecentErrMsg()); return ret);
// step5: Obtain the output value and copy the result from the device memory to the host side.
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[ERROR msg]%s", ret, aclGetRecentErrMsg()); 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 based on the specific API definition.
aclDestroyTensor(self);
aclDestroyTensor(mat1);
aclDestroyTensor(mat2);
aclDestroyScalar(beta);
aclDestroyScalar(alpha);
aclDestroyTensor(out);
// 7. Release device resources. Modify based on the specific API definition.
aclrtFree(selfDeviceAddr);
aclrtFree(mat1DeviceAddr);
aclrtFree(mat2DeviceAddr);
aclrtFree(outDeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}