aclnnAddbmm&aclnnInplaceAddbmm
Supported Products
| Product | Supported |
|---|---|
| √ | |
| √ | |
| × | |
| √ | |
| √ |
Function
Description: Multiplies batch1 and batch2 and sums up the results by the first dimension (batch dimension). Compresses the three-dimensional vector into a two-dimensional vector (the shape is that of the last two dimensions). Multiplies the result by α, and then adds the result to the product of β and self to obtain the result.
Formula:
Note: If β is 0, self is ignored and does not participate in the calculation. If α is 0, batch1 and batch2 are ignored and do not participate in the calculation.
Example:
The shape of self is [3,5], the shape of batch1 is [10,3,4], the shape of batch2 is [10,4,5], and the shape of the output out is [3,5].
Prototype
- aclnnAddbmm and aclnnInplaceAddbmm implement the same function in different ways. Select a proper operator based on your requirements.
- aclnnAddbmm: An output tensor object needs to be created to store the computation result.
- aclnnInplaceAddbmm: 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, aclnnAddbmmGetWorkspaceSize or aclnnInplaceAddbmmGetWorkspaceSize is called to obtain the workspace size required for computation and the executor that contains the operator computation process. Then, aclnnAddbmm or aclnnInplaceAddbmm is called to perform computation.
aclnnStatus aclnnAddbmmGetWorkspaceSize(
const aclTensor *self,
const aclTensor *batch1,
const aclTensor *batch2,
const aclScalar *beta,
const aclScalar *alpha,
aclTensor *out,
int8_t cubeMathType,
uint64_t *workspaceSize,
aclOpExecutor **executor)aclnnStatus aclnnAddbmm(
void *workspace,
uint64_t workspaceSize,
aclOpExecutor *executor,
aclrtStream stream)aclnnStatus aclnnInplaceAddbmmGetWorkspaceSize(
aclTensor *selfRef,
const aclTensor *batch1,
const aclTensor *batch2,
const aclScalar *beta,
const aclScalar *alpha,
int8_t cubeMathType,
uint64_t *workspaceSize,
aclOpExecutor **executor)aclnnStatus aclnnInplaceAddbmm(
void *workspace,
uint64_t workspaceSize,
aclOpExecutor *executor,
aclrtStream stream)aclnnAddbmmGetWorkspaceSize
Parameters:
Name Input/Output Description Precaution Data Type Data Format Dimension (Shape) Non-contiguous Tensor self Input Input self in the formula. - The data type must be the same as that of out.
- The shape of the last two dimensions of batch1@batch2 must meet the broadcast relationship. (Note: Only self can be broadcast to the same shape as the last two dimensions of batch1@batch2. The last two dimensions of batch1@batch2 cannot be broadcast to the same shape as self. For example, self: [3,5], batch1@batch2: [1,1,1]. A failure will occur.)
- Empty tensors are supported.
BFLOAT16, FLOAT16, FLOAT32 ND At least one dimension √ batch1 Input Input batch1 in the formula. - Its data type and the data types of self and batch2 must meet the type deduction rules (see Deduction Relationship and Constraints).
- The shape supports only three dimensions, and the first dimension batch1 must meet the Broadcast Relationship with the batch dimension of batch2.
- Empty tensors are supported.
BFLOAT16, FLOAT16, FLOAT32 ND 3 √ batch2 Input Input batch2 in the formula. - Its data type and the data types of self and batch1 must meet the type deduction rules (see Deduction Relationship and Constraints).
- The shape supports only three dimensions, and the first dimension batch must meet the Broadcast Relationship with the batch dimension of batch1.
- Empty tensors are supported.
BFLOAT16, FLOAT16, FLOAT32 ND 3 √ beta Input Input β in the formula. The data type can be converted to the data type deduced from self and batch1@batch2 (see Deduction Relationship and Constraints). FLOAT, FLOAT16, DOUBLE, INT32, INT64, INT16, INT8, UINT8, BOOL - - - alpha Input Input α in the formula. The data type can be converted to the data type deduced from self and batch1@batch2 (see Deduction Relationship and Constraints). FLOAT, FLOAT16, DOUBLE, INT32, INT64, INT16, INT8, UINT8, BOOL - - - out Output Output out in the formula. - The data type must be the same as that of self.
- The shape must be the same as the last two dimensions of batch1@batch2.
- Empty tensors are supported.
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, batch1, or batch2 is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data type of self, batch1, batch2, or out is not supported. The data type of self, batch1, batch2, or out does not meet the deduction rule. The broadcast operation cannot be performed between self and batch1@batch2. The first dimensions of batch1 and batch2 are not equal or cannot be broadcast. The shapes of batch1 and batch2 are not 3D. The last dimension of batch1 is not equal to the penultimate dimension of batch2. self and batch1@batch2 must be both empty tensors or non-empty tensors.
aclnnAddbmm
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 the first-phase API aclnnAddbmmGetWorkspaceSize. 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.
aclnnInplaceAddbmmGetWorkspaceSize
Parameters:
Name Input/Output Description Precaution Data Type Data Format Dimension (Shape) Non-contiguous Tensor selfRef Input | Output Input and output tensor, that is, input self and out in the formula. - Its data type and the data type of batch1@batch2 must meet the type deduction rules (see Deduction Relationship and Constraints).
- The shape must be consistent with the shape of the last two dimensions of batch1@batch2.
- Empty tensors are supported.
BFLOAT16, FLOAT16, FLOAT32 ND 2 √ batch1 Input Input batch1 in the formula. - Its data type and the data types of self and batch2 must meet the type deduction rules (see Deduction Relationship and Constraints).
- The shape supports only three dimensions, and the first dimension batch1 must meet the Broadcast Relationship with the batch dimension of batch2.
- Empty tensors are supported.
BFLOAT16, FLOAT16, FLOAT32 ND 3 √ batch2 Input Input batch2 in the formula. - Its data type and the data types of self and batch1 must meet the type deduction rules (see Deduction Relationship and Constraints).
- The shape supports only three dimensions, and the first dimension batch must meet the Broadcast Relationship with the batch dimension of batch1.
- Empty tensors are supported.
BFLOAT16, FLOAT16, FLOAT32 ND 3 √ beta Input Input β in the formula. The data type can be converted to the data type deduced from self and batch1@batch2 (see Deduction Relationship and Constraints). FLOAT, FLOAT16, DOUBLE, INT32, INT64, INT16, INT8, UINT8, BOOL - - - alpha Input Input α in the formula. The data type can be converted to the data type deduced from self and batch1@batch2 (see Deduction Relationship and Constraints). FLOAT, FLOAT16, DOUBLE, INT32, INT64, INT16, INT8, UINT8, BOOL - - - 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, batch1, or batch2 is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data type of selfRef, batch1, or batch2 is not supported. The data type of selfRef, batch1, or batch2 does not meet the deduction rule. The broadcast operation cannot be performed between selfRef and batch1@batch2. The first dimensions of batch1 and batch2 are not equal or cannot be broadcast. The shapes of batch1 and batch2 are not 3D. The last dimension of batch1 is not equal to the penultimate dimension of batch2. selfRef and batch1@batch2 must be both empty tensors or non-empty tensors.
aclnnInplaceAddbmm
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 the first-phase API aclnnInplaceAddbmmGetWorkspaceSize. 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 : aclnnAddbmm&aclnnInplaceAddbmm defaults to a deterministic implementation.
Atlas training series products andAtlas inference series products : The Cube unit does not support FLOAT32 computation. The input data type FLOAT32 can be converted to FLOAT16 in the API for computation by setting cubeMathType to 1 (ALLOW_FP32_DOWN_PRECISION).Atlas A2 training products/Atlas A2 inference products andAtlas A3 training series products/Atlas A3 inference series products : If one of the inputs of batch1 and batch2 is BFLOAT16 and the other is FLOAT or FLOAT16, the data type cannot be deduced.
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_addbmm.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> batch1Shape = {1, 2, 3};
std::vector<int64_t> batch2Shape = {1, 3, 4};
std::vector<int64_t> outShape = {2, 4};
void* selfDeviceAddr = nullptr;
void* batch1DeviceAddr = nullptr;
void* batch2DeviceAddr = nullptr;
void* outDeviceAddr = nullptr;
aclTensor* self = nullptr;
aclTensor* batch1 = nullptr;
aclTensor* batch2 = 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> batch1HostData = {1, 1, 1, 2, 2, 2};
std::vector<float> batch2HostData = {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 batch1 aclTensor.
ret = CreateAclTensor(batch1HostData, batch1Shape, &batch1DeviceAddr, aclDataType::ACL_FLOAT, &batch1);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create a batch2 aclTensor.
ret = CreateAclTensor(batch2HostData, batch2Shape, &batch2DeviceAddr, aclDataType::ACL_FLOAT, &batch2);
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 aclnnAddbmm.
ret = aclnnAddbmmGetWorkspaceSize(self, batch1, batch2, beta, alpha, out, cubeMathType, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnAddbmmGetWorkspaceSize 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 aclnnAddbmm.
ret = aclnnAddbmm(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnAddbmm 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]);
}
// aclnnInplaceAddbmm
// Step 3. Call the CANN operator library API.
LOG_PRINT("\ntest aclnnInplaceAddbmm\n");
// Call the first-phase API of aclnnInplaceAddbmm.
ret = aclnnInplaceAddbmmGetWorkspaceSize(self, batch1, batch2, beta, alpha, cubeMathType, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplaceAddbmmGetWorkspaceSize 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 aclnnInplaceAddbmm.
ret = aclnnInplaceAddbmm(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplaceAddbmm 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(batch1);
aclDestroyTensor(batch2);
aclDestroyScalar(alpha);
aclDestroyScalar(beta);
aclDestroyTensor(out);
// 7. Release device resources. Modify the configuration based on the API definition.
aclrtFree(selfDeviceAddr);
aclrtFree(batch1DeviceAddr);
aclrtFree(batch2DeviceAddr);
aclrtFree(outDeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}