aclnnAddr&aclnnInplaceAddr
Applicable Products
| Product | Supported |
|---|---|
| √ | |
| √ | |
| √ | |
| × | |
| × | |
| √ |
Function
Description: Computes the outer product of one-dimensional vectors vec1 and vec2 to obtain a two-dimensional matrix, multiplies the outer product result matrix by a factor, adds it to self multiplied by a factor, and outputs the result.
Formula:
$$ \text{out} = \beta\ \text{self} + \alpha\ (\text{vec1} \otimes\text{vec2}) $$
Function Prototype
aclnnAddr and aclnnInplaceAddr implement the same function. The differences in usage are as follows. Select the appropriate operator based on your actual scenario.
aclnnAddr: Create a new output tensor object to store the computation result.
aclnnInplaceAddr: No need to create a new output tensor object. The computation result is stored directly in the memory of the input tensor.
Each operator uses the two-phase API. You must first call the "aclnnAddrGetWorkspaceSize" or "aclnnInplaceAddrGetWorkspaceSize" API to obtain input parameters and compute the required workspace size based on the process, and then call the "aclnnAddr" or "aclnnInplaceAddr" API to perform the computation.
aclnnStatus aclnnAddrGetWorkspaceSize(
const aclTensor* self,
const aclTensor* vec1,
const aclTensor* vec2,
const aclScalar* betaOptional,
const aclScalar* alphaOptional,
aclTensor* out,
uint64_t* workspaceSize,
aclOpExecutor** executor)aclnnStatus aclnnAddr(
void* workspace,
uint64_t workspaceSize,
aclOpExecutor* executor,
const aclrtStream stream)aclnnStatus aclnnInplaceAddrGetWorkspaceSize(
aclTensor* selfRef,
const aclTensor* vec1,
const aclTensor* vec2,
const aclScalar* betaOptional,
const aclScalar* alphaOptional,
uint64_t* workspaceSize,
aclOpExecutor** executor)aclnnStatus aclnnInplaceAddr(
void* workspace,
uint64_t workspaceSize,
aclOpExecutor* executor,
const aclrtStream stream)aclnnAddrGetWorkspaceSize
Parameters:
Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor self (aclTensor*) Input Outer product extension matrix. The shape dimension cannot exceed 2, and must satisfy the broadcast relationship with vec1 and vec2. FLOAT, FLOAT16, DOUBLE, INT8, INT16, INT32, INT64, UINT8, BOOL, BFLOAT16 ND - √ vec1 (aclTensor*) Input The first vector of the outer product input parameter, which is a one-dimensional vector. The shape must satisfy the broadcast relationship with self. FLOAT, FLOAT16, DOUBLE, INT8, INT16, INT32, INT64, UINT8, BOOL, BFLOAT16 ND - √ vec2 (aclTensor*) Input The second outer product input parameter, a one-dimensional vector. The shape must satisfy the broadcast relationship with self. FLOAT, FLOAT16, DOUBLE, INT8, INT16, INT32, INT64, UINT8, BOOL, BFLOAT16 ND - √ betaOptional (aclScalar*) Input Scaling factor for the outer product extension matrix, i.e., β in the formula. If betaOptional is of the bool type, the data type of self, vec1, and vec2 must be bool. If self, vec1, or vec2 is an integer type, betaOptional and alphaOptional cannot be a floating-point type. FLOAT, FLOAT16, DOUBLE, INT8, INT16, INT32, INT64, UINT8, BOOL, BFLOAT16 ND - - alphaOptional (aclScalar*) Input Outer product scaling factor, i.e., α in the formula. If alphaOptional is of the bool type, the data type of self/vec1/vec2 can only be bool. If self/vec1/vec2 is of an integer type, betaOptional and alphaOptional cannot be of a floating-point type. FLOAT, FLOAT16, DOUBLE, INT8, INT16, INT32, INT64, UINT8, BOOL, BFLOAT16 ND - - out (aclTensor*) Output Output result. - FLOAT, FLOAT16, DOUBLE, INT8, INT16, INT32, INT64, UINT8, BOOL, BFLOAT16 ND - √ workspaceSize (uint64_t*) Output Returns the workspace size to be requested on the Device side. - - - - - executor (aclOpExecutor**) Output Returns the op executor, which contains the operator computation flow. - - - - - Atlas training products : BFLOAT16 data type is not supported.
Return value
aclnnStatus: return code. For details, see aclnn Return Codes.
The first-phase API performs input parameter validation and returns an error in the following scenarios:
return code Error Code Description ACLNN_ERR_PARAM_NULLPTR 161001 The input tensor or out is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data type and data format of self, vec1, and vec2 are not within the supported range. The dimension of vec1 and vec2 is not 1, or the dimension of self exceeds 2. self cannot be broadcast to the shape of the outer product result of vec1 and vec2. When beta or alpha is of the bool type, the data types of self, vec1, and vec2 are not of the bool type. When the types of self, vec1, and vec2 are all integer types, bool, or a combination of integer and bool, beta or alpha is of a floating-point type.
aclnnAddr
Parameters:
Parameter Input/Output Description workspace Input Address of the workspace memory applied on the Device side. workspaceSize Input Size of the workspace applied on the Device side, obtained from the first-phase API aclnnAddrGetWorkspaceSize. executor Input Operator executor, which contains the operator computation process. stream Input Specifies the stream on which the task is executed. Return value
aclnnStatus: return code. For details, see aclnn Return Codes.
aclnnInplaceAddrGetWorkspaceSize
Parameters:
Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor selfRef (aclTensor*) Input/Output Outer product extension matrix and output matrix. The shape dimension is 2. Empty tensors are not supported. Must satisfy the broadcast relationship with vec1 and vec2. FLOAT, FLOAT16, DOUBLE, INT8, INT16, INT32, INT64, UINT8, BOOL, BFLOAT16 ND - √ vec1 (aclTensor*) Input First vector of the outer product input parameter, a one-dimensional vector. shape must satisfy the broadcast relationship with selfRef. FLOAT, FLOAT16, DOUBLE, INT8, INT16, INT32, INT64, UINT8, BOOL, BFLOAT16 ND - √ vec2 (aclTensor*) Input Second vector of the outer product input parameter, a one-dimensional vector. The shape must satisfy the broadcast relationship with selfRef. FLOAT, FLOAT16, DOUBLE, INT8, INT16, INT32, INT64, UINT8, BOOL, BFLOAT16 ND - √ betaOptional (aclScalar*) Input Outer product extension matrix scale factor, i.e., β in the formula. If betaOptional is of the bool type, the data type of self/vec1/vec2 can only be bool. If self/vec1/vec2 is of an integer type, betaOptional and alphaOptional cannot be of a floating-point type. FLOAT, FLOAT16, DOUBLE, INT8, INT16, INT32, INT64, UINT8, BOOL, BFLOAT16 ND - - alphaOptional (aclScalar*) Input Outer product scaling factor, i.e., α in the formula. If alphaOptional is of bool type, the data type of self/vec1/vec2 must be bool; if self/vec1/vec2 is of integer type, betaOptional and alphaOptional cannot be of floating-point type. FLOAT, FLOAT16, DOUBLE, INT8, INT16, INT32, INT64, UINT8, BOOL, BFLOAT16 ND - - workspaceSize (uint64_t*) Output Returns the workspace size to be applied for on the Device side. - - - - - executor (aclOpExecutor**) Output Returns the op executor, which contains the operator computation flow. - - - - - Atlas training products : The BFLOAT16 data type is not supported.
Return value
aclnnStatus: return code. For details, see aclnn Return Codes.
The first-phase API performs input parameter validation and returns an error in the following scenarios:
Return Code Error Code Description ACLNN_ERR_PARAM_NULLPTR 161001 The input tensor is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data type or data format of selfRef, vec1, or vec2 is not within the supported range. The dimensions of vec1 and vec2 are not 1, or the dimension of selfRef is not 2. selfRef cannot be broadcast to the shape of the outer product of vec1 and vec2. When beta or alpha is of bool type, the data type of selfRef, vec1, or vec2 is not bool. When the data types of selfRef, vec1, and vec2 are all integer types, bool, or a combination of integer and bool, beta or alpha is of floating-point type.
aclnnInplaceAddr
Parameters:
Parameter Input/Output Description workspace Input Address of the workspace memory allocated on the Device side. workspaceSize Input Size of the workspace allocated on the Device side, obtained from the first-phase API aclnnInplaceAddrGetWorkspaceSize. executor Input Operator executor that contains the operator computation flow. stream Input Specifies the stream for task execution. Return value
aclnnStatus: return code. For details, see aclnn Return Codes.
Constraints
Deterministic computation:
- aclnnAddr&aclnnInplaceAddr defaults to a deterministic implementation.
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_addr.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: 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 side.
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 data from the host side 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);
// Compute 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() {
// (Boilerplate) Initialize the device/stream. See the ACL API manual.
// Enter the deviceId based on your 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);
// Construct the input and output. Customize the construction based on the API.
std::vector<int64_t> inputShape = {3, 2};
std::vector<int64_t> vec1Shape = {3};
std::vector<int64_t> vec2Shape = {2};
std::vector<int64_t> outShape = {3, 2};
void* inputDeviceAddr = nullptr;
void* vec1DeviceAddr = nullptr;
void* vec2DeviceAddr = nullptr;
void* outDeviceAddr = nullptr;
aclTensor* input = nullptr;
aclTensor* vec1 = nullptr;
aclTensor* vec2 = nullptr;
aclScalar* beta = nullptr;
aclScalar* alpha = nullptr;
aclTensor* out = nullptr;
std::vector<float> inputHostData = {6, 0};
std::vector<float> vec1HostData = {1, 2, 3};
std::vector<float> vec2HostData = {4, 5};
std::vector<float> outHostData = {6, 0};
float betaValue = 1.5f;
float alphaValue = 1.5f;
ret = CreateAclTensor(inputHostData, inputShape, &inputDeviceAddr, aclDataType::ACL_FLOAT, &input);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(vec1HostData, vec1Shape, &vec1DeviceAddr, aclDataType::ACL_FLOAT, &vec1);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(vec2HostData, vec2Shape, &vec2DeviceAddr, aclDataType::ACL_FLOAT, &vec2);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_FLOAT, &out);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create beta and alpha scalar values.
beta = aclCreateScalar(&betaValue, aclDataType::ACL_FLOAT);
CHECK_RET(beta != nullptr, return ret);
alpha = aclCreateScalar(&alphaValue, aclDataType::ACL_FLOAT);
CHECK_RET(alpha != nullptr, return ret);
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
// Example of calling the aclnnAddr API.
// Call the first-phase API of aclnnAddr.
ret = aclnnAddrGetWorkspaceSize(input, vec1, vec2, beta, alpha, out, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnAddrGetWorkspaceSize 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 aclnnAddr.
ret = aclnnAddr(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnAddr failed. ERROR: %d\n", ret); return ret);
// (Boilerplate) Synchronously wait for task execution to complete.
ret = aclrtSynchronizeStream(stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret);
// Obtain the output value and copy the result from the device memory to the host memory. Modify this 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", ret); return ret);
for (int64_t i = 0; i < size; i++) {
LOG_PRINT("result[%ld] is: %f\n", i, resultData[i]);
}
// Example of calling aclnnInplaceAddr
// Call the first-phase API of aclnnInplaceAddr.
ret = aclnnInplaceAddrGetWorkspaceSize(input, vec1, vec2, beta, alpha, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnAddrGetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
// Apply for device memory based on the 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 aclnnInplaceAddr second-phase API.
ret = aclnnInplaceAddr(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnAddr failed. ERROR: %d\n", ret); return ret);
// (Boilerplate) Synchronously wait for task execution to complete.
ret = aclrtSynchronizeStream(stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret);
// Obtain the output value and copy the result from the device memory to the host memory. Modify this based on the specific API definition.
ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), inputDeviceAddr,
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]);
}
// Release aclTensor and aclScalar.
aclDestroyTensor(input);
aclDestroyTensor(vec1);
aclDestroyTensor(vec2);
aclDestroyTensor(out);
// Release device resources.
aclrtFree(inputDeviceAddr);
aclrtFree(vec1DeviceAddr);
aclrtFree(vec2DeviceAddr);
aclrtFree(outDeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}