aclnnQr
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: Performs orthogonal decomposition on the input tensor.
Formula:
Where is the input tensor with at least two dimensions, and can be expressed as the product of an orthogonal matrix and an upper triangular matrix .
Example:
A = tensor([[1, 2], [3, 4]], dtype=torch.float) Q, R = QR(A, some=False) Q = tensor([[-0.3162, -0.9487], [-0.9487, 0.3162]]) R = tensor([[-3.1623, -4.4272], [0.0000, -0.6325]])
Function Prototype
Each operator uses a two-phase API. Call the aclnnQrGetWorkspaceSize API first to obtain the workspace size required for computation and the executor that contains the computation process, and then call the aclnnQr API to execute the computation.
aclnnStatus aclnnQrGetWorkspaceSize(
const aclTensor* self,
bool some,
aclTensor* Q,
aclTensor* R,
uint64_t* workspaceSize,
aclOpExecutor** executor)aclnnStatus aclnnQr(
void* workspace,
uint64_t workspaceSize,
aclOpExecutor* executor,
aclrtStream stream)aclnnQrGetWorkspaceSize
Parameters
Parameter Input/Output Description Instruction Data Type Data Format Shape Non-contiguous Tensor self (aclTensor*) Input A in the formula. The shape must be 2-8 dimensional, in the form of [..., M, N], where ... indicates 0-6 dimensions. FLOAT, FLOAT16, DOUBLE, COMPLEX64, COMPLEX128 ND 2-8 √ some (bool) Input Controls the calculation attributes of the Q and R output shapes in the formula. - When set to false, Q is a square matrix. For example, given A[..., M, N], the outputs are the complete Q[..., M, M] and R[..., M, N].
- When set to true, Q is a thin matrix. For example, given A[..., M, N], the outputs are Q[..., M, K] and R[..., K, N], where K is the minimum of M and N.
BOOL - - - Q (aclTensor*) Output Q in the formula, the orthogonal matrix output by the orthogonal decomposition. For shape constraints, see the description of the someparameter. The data format must be the same as that ofselfand R.FLOAT, FLOAT16, DOUBLE, COMPLEX64, COMPLEX128 ND Determined by some √ R (aclTensor*) Output R in the formula, the upper triangular matrix output by the orthogonal decomposition. Shape constraints are the same as those for some. The data format must be the same as that ofselfand Q.FLOAT, FLOAT16, DOUBLE, COMPLEX64, COMPLEX128 ND Determined by some √ workspaceSize (uint64_t*) Output Returns the workspace size to be allocated on the Device side. - - - - - executor (aclOpExecutor**) Output Returns the op executor containing the operator computation flow. - - - - - Return Value
aclnnStatus: return status code. For details, see aclnn Return Codes.
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 Null pointer in self, Q, or R. ACLNN_ERR_PARAM_INVALID 161002 The data types or data formats of self, Q, or R are out of the supported range. The shapes of self, Q, or R do not meet the constraints.
aclnnQr
Parameters
Parameter Input/Output Description workspace Input Workspace memory address requested on the device. workspaceSize Input Workspace size obtained by the first-phase API aclnnQrGetWorkspaceSize.executor Input op executor that contains the operator computation flow. stream Input Stream where tasks are executed. Return Value
aclnnStatus: return status code. For details, see aclnn Return Codes.
Constraints
- Determinism:
aclnnQrdefaults to a deterministic implementation.
Example
The following sample code is for reference only. For details about compilation and execution, see Compile and Run Samples.
#include <iostream>
#include <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_qr.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, 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 device 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 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 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 PrepareInputAndOutput(
std::vector<int64_t>& selfShape, std::vector<int64_t>& qShape, std::vector<int64_t>& rShape, void** selfDeviceAddr,
aclTensor** self, void** qDeviceAddr, aclTensor** q, void** rDeviceAddr, aclTensor** r)
{
std::vector<float> selfHostData = {1, 2, 3, 4};
std::vector<float> qHostData = {0, 0, 0, 0};
std::vector<float> rHostData = {0, 0, 0, 0};
// Create the self aclTensor.
auto ret = CreateAclTensor(selfHostData, selfShape, selfDeviceAddr, aclDataType::ACL_FLOAT, self);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the q and r aclTensors.
ret = CreateAclTensor(qHostData, qShape, qDeviceAddr, aclDataType::ACL_FLOAT, q);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(rHostData, rShape, rDeviceAddr, aclDataType::ACL_FLOAT, r);
CHECK_RET(ret == ACL_SUCCESS, return ret);
return ACL_SUCCESS;
}
void ReleaseTensorAndScalar(aclTensor* self, aclTensor* q, aclTensor* r)
{
aclDestroyTensor(self);
aclDestroyTensor(q);
aclDestroyTensor(r);
}
void ReleaseDevice(
void* selfDeviceAddr, void* qDeviceAddr, void* rDeviceAddr, uint64_t workspaceSize, void* workspaceAddr, aclrtStream stream,
int32_t deviceId)
{
aclrtFree(selfDeviceAddr);
aclrtFree(qDeviceAddr);
aclrtFree(rDeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
}
int main() {
// 1. (Boilerplate) device/stream initialization. For details, see the acl API manual.
// 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);
// 2. Construct inputs and outputs as required by the API.
std::vector<int64_t> selfShape = {2, 2};
bool some = false;
std::vector<int64_t> qShape = {2, 2};
std::vector<int64_t> rShape = {2, 2};
void* selfDeviceAddr = nullptr;
void* qDeviceAddr = nullptr;
void* rDeviceAddr = nullptr;
aclTensor* self = nullptr;
aclTensor* q = nullptr;
aclTensor* r = nullptr;
ret = PrepareInputAndOutput(selfShape, qShape, rShape, &selfDeviceAddr, &self, &qDeviceAddr, &q, &rDeviceAddr, &r);
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;
// Call the first-phase API of aclnnQr.
ret = aclnnQrGetWorkspaceSize(self, some, q, r, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnQrGetWorkspaceSize 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 aclnnQr second-phase API.
ret = aclnnQr(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnQr failed. ERROR: %d\n", ret); return ret);
// 4. (boilerplate) Wait for the task execution to complete synchronously.
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 device memory to host memory. Modify the code based on the specific API definition.
auto size = GetShapeSize(qShape);
std::vector<float> resultQData(size, 0);
ret = aclrtMemcpy(resultQData.data(), resultQData.size() * sizeof(resultQData[0]), qDeviceAddr,
size * sizeof(resultQData[0]), ACL_MEMCPY_DEVICE_TO_HOST);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result Q from device to host failed. ERROR: %d\n", ret); return ret);
for (int64_t i = 0; i < size; i++) {
LOG_PRINT("result Q[%ld] is: %f\n", i, resultQData[i]);
}
std::vector<float> resultRData(size, 0);
ret = aclrtMemcpy(resultRData.data(), resultRData.size() * sizeof(resultRData[0]), rDeviceAddr,
size * sizeof(resultRData[0]), ACL_MEMCPY_DEVICE_TO_HOST);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result R from device to host failed. ERROR: %d\n", ret); return ret);
for (int64_t i = 0; i < size; i++) {
LOG_PRINT("result R[%ld] is: %f\n", i, resultRData[i]);
}
// 6. Release aclTensor and aclScalar. Modify the code based on the specific API definition.
ReleaseTensorAndScalar(self, q, r);
// 7. Release device resources. Modify the parameters based on the specific API definition.
ReleaseDevice(selfDeviceAddr, qDeviceAddr, rDeviceAddr, workspaceSize, workspaceAddr, stream, deviceId);
return 0;
}