aclnnAddr&aclnnInplaceAddr
Applicable Products
Ascend 950PR&950DT products : 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 : 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:
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 a two-phase API. First call the "aclnnAddrGetWorkspaceSize" or "aclnnInplaceAddrGetWorkspaceSize" API to obtain the input parameters and calculate 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:
Atlas training products : The BFLOAT16 data type is not supported.
Return value
aclnnStatus: return status code. For details, see aclnn Error Codes.
The first-phase API performs input parameter validation and returns an error in the following scenarios:
Error Code Error Code Description ACLNN_ERR_PARAM_NULLPTR 161001 The tensor or out is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data types and data formats 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 expanded to the shape of the outer product result of vec1 and vec2. If beta or alpha is of the bool type, the data types of self, vec1, and vec2 are not the bool type. If self, vec1, and vec2 are all of the integer type, bool type, or "integer+bool" combination, beta or alpha is of the floating-point type.
aclnnAddr
Parameters:
Parameter Input/Output Description workspace Input The workspace memory address allocated on the device. workspaceSize Input The size of the workspace allocated on the device, obtained by calling the first-phase API aclnnAddrGetWorkspaceSize. executor Input Executor of the op, containing the operator computation flow. stream Input Specifies the Stream on which tasks are executed. Return value
aclnnStatus: return status code. For details, see aclnn Error Codes.
aclnnInplaceAddrGetWorkspaceSize
Parameters:
Atlas training products : The BFLOAT16 data type is not supported.
Return value
aclnnStatus: return status code. For details, see aclnn Error Codes.
The first-phase API performs input parameter validation and returns an error in the following scenarios:
Error Code Error Code Description ACLNN_ERR_PARAM_NULLPTR 161001 The tensor is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data types and data formats of selfRef, vec1, and vec2 are not within the supported range. The dimensions of vec1 and vec2 are not 1, and the dimension of selfRef is not 2. selfRef cannot be expanded to the outer product result shape of vec1 and vec2. If beta or alpha is of the bool type, the data types of selfRef, vec1, and vec2 are not bool. If the types of selfRef, vec1, and vec2 are all integer, bool, or "integer+bool", beta or alpha is of the floating-point type.
aclnnInplaceAddr
Parameters:
Parameter Input/Output Description workspace Input Workspace memory address requested on the Device. workspaceSize Input Size of the workspace requested on the Device, obtained by calling the first-phase API aclnnInplaceAddrGetWorkspaceSize. executor Input Executor of the op, containing the operator computation flow. stream Input Stream for task execution. Return value
aclnnStatus: return status code. For details, see aclnn Error Codes.
Constraints
- Deterministic computation:
- aclnnAddr&aclnnInplaceAddr default to a deterministic implementation.
Example
The following sample code is for reference only. For details about the compilation and execution, 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 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 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 data from the host 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);
// Calculate 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() {
// (Boilerplate) Initialize device/stream. For details, see the acl API reference
// Set 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);
// Construct the inputs and outputs. Custom construction is required 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 = {1, 2, 3, 4, 5, 6};
std::vector<float> vec1HostData = {1, 2, 3};
std::vector<float> vec2HostData = {4, 5};
std::vector<float> outHostData = {0, 0, 0, 0, 0, 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 the 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) Synchronize and wait until the task is complete.
ret = aclrtSynchronizeStream(stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret);
// Obtain the output values and copy the results from the device side to the host side. Modify the code based on the definition of the specific API.
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 the aclnnInplaceAddr API call
// Call the first-phase API of aclnnInplaceAddr. selfRef is the input and output tensor, and out is passed in here.
ret = aclnnInplaceAddrGetWorkspaceSize(out, vec1, vec2, beta, alpha, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplaceAddrGetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
// Allocate 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 second-phase API of aclnnInplaceAddr.
ret = aclnnInplaceAddr(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplaceAddr failed. ERROR: %d\n", ret); return ret);
// (boilerplate) Synchronize and wait until the task is complete.
ret = aclrtSynchronizeStream(stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret);
// Obtain the output values and copy the results from the device side to the host side. Modify the code based on the definition of the specific API.
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]);
}
// Release the 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;
}