aclnnLinalgQr
Applicable Products
| Product | Supported |
|---|---|
| × | |
| √ | |
| √ | |
| × | |
| × | |
| √ |
Function
Description: Performs orthogonal decomposition on the input tensor.
Formula:
$$ A = QR $$
Where $A$ is the input tensor with at least two dimensions, and $A$ can be expressed as the product of an orthogonal matrix $Q$ and an upper triangular matrix $R$.
Example:
A = tensor([[1, 2], [3, 4]], dtype=torch.float) q,r = linalg_qr(A, mode='reduced') q = tensor([[-0.3162, -0.9487], [-0.9487, 0.3162]]) r = tensor([[-3.1623, -4.4272], [0.0000, -0.6325]])
Function Prototype
Each operator is divided into a two-phase API. You must first call the aclnnLinalgQrGetWorkspaceSize API to obtain the workspace size required for computation and the executor that encapsulates the operator computation flow, and then call the aclnnLinalgQr API to perform the computation.
aclnnStatus aclnnLinalgQrGetWorkspaceSize(
const aclTensor* self,
int64_t mode,
aclTensor* Q,
aclTensor* R,
uint64_t* workspaceSize,
aclOpExecutor** executor)aclnnStatus aclnnLinalgQr(
void* workspace,
uint64_t workspaceSize,
aclOpExecutor* executor,
aclrtStream stream)aclnnLinalgQrGetWorkspaceSize
Parameters
Parameter Input/Output Description Instruction Data Type Data Format Dimension (Shape) Non-contiguous Tensor self (aclTensor*) Input A in the formula. The shape must satisfy the constraints with Q and R. FLOAT, FLOAT16, DOUBLE, COMPLEX64, COMPLEX128 ND 2-8 √ mode (int64_t) Input Controls the computation attribute of the output forms of Q and R in the formula. - When mode is 0, the
reducedmode is used. For input A(*, m, n), it outputs reduced-size Q(*, m, k) and R(*, k, n), where k is the minimum of m and n. - When mode is 1, the
completemode is used. For input A(*, m, n), it outputs full-size Q(*, m, m) and R(*, m, n). - When mode is 2, the
rmode is used. Only R(*, k, n) in thereducedscenario is computed, where k is the minimum of m and n, and Q is returned as an empty tensor.
INT64 - - - Q (aclTensor*) Output Q in the formula, the orthogonal matrix output from the orthogonal decomposition. Shape is Q(*, m, m) or Q(*, m, k) or empty, where k is the minimum of m and n, and the data format must be consistent with selfand R.FLOAT, FLOAT16, DOUBLE, COMPLEX64, COMPLEX128 ND Deduced from mode. √ R (aclTensor*) Output R in the formula, the upper triangular matrix output from QR decomposition. Shape is R(*, m, n) or R(*, k, n), where k is the minimum of m and n, and the data format must be consistent with selfand Q.FLOAT, FLOAT16, DOUBLE, COMPLEX64, COMPLEX128 ND Derived from mode. √ workspaceSize (uint64_t*) Output Returns the workspace size to be requested on the Device side. - - - - - executor (aclOpExecutor**) Output Returns the operator executor, which contains the operator computation flow. - - - - - - When mode is 0, the
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 Value Error Code Description ACLNN_ERR_PARAM_NULLPTR 161001 A null pointer exists in the passed self, Q, or R. ACLNN_ERR_PARAM_INVALID 161002 The data types or data formats of self, Q, and R are not within the supported range. The shapes of self, Q, and R do not meet the constraints. The mode is not within the valid range.
aclnnLinalgQr
Parameters
Parameter Input/Output Description workspace Input Address of the workspace memory applied for on the Device side. workspaceSize Input Workspace size obtained from the first-phase API aclnnLinalgQrGetWorkspaceSize.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 Codes.
Constraints
- Deterministic computation description:
aclnnLinalgQrdefaults 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_linalg_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: 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 the 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 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;
}
aclError InitAcl(int32_t deviceId, aclrtStream* stream)
{
auto ret = Init(deviceId, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);
return ACL_SUCCESS;
}
aclError CreateInputs(
std::vector<int64_t>& selfShape, std::vector<int64_t>& qOutShape, std::vector<int64_t>& rOutShape,
void** selfDeviceAddr, void** qOutDeviceAddr, void** rOutDeviceAddr, aclTensor** self, aclTensor** qOut,
aclTensor** rOut)
{
std::vector<float> selfHostData = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
std::vector<float> qOutHostData = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
std::vector<float> rOutHostData = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
auto ret = CreateAclTensor(selfHostData, selfShape, selfDeviceAddr, aclDataType::ACL_FLOAT, self);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(qOutHostData, qOutShape, qOutDeviceAddr, aclDataType::ACL_FLOAT, qOut);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(rOutHostData, rOutShape, rOutDeviceAddr, aclDataType::ACL_FLOAT, rOut);
CHECK_RET(ret == ACL_SUCCESS, return ret);
return ACL_SUCCESS;
}
aclError ExecOpApi(
aclTensor* self, aclTensor* qOut, aclTensor* rOut, int64_t mode, void** workspaceAddrOut, uint64_t& workspaceSize,
void* qOutDeviceAddr, void* rOutDeviceAddr, std::vector<int64_t>& qOutShape, std::vector<int64_t>& rOutShape,
aclrtStream stream)
{
aclOpExecutor* executor;
auto ret = aclnnLinalgQrGetWorkspaceSize(self, mode, qOut, rOut, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnLinalgQrGetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
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);
}
*workspaceAddrOut = workspaceAddr;
ret = aclnnLinalgQr(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnLinalgQr failed. ERROR: %d\n", ret); return ret);
ret = aclrtSynchronizeStream(stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret);
// Copy qOut.
auto size1 = GetShapeSize(qOutShape);
std::vector<double> resultData1(size1, 0);
ret = aclrtMemcpy(
resultData1.data(), resultData1.size() * sizeof(resultData1[0]), qOutDeviceAddr, size1 * sizeof(resultData1[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 < size1; i++) {
LOG_PRINT("result[%ld] is: %f\n", i, resultData1[i]);
}
// Copy rOut.
auto size2 = GetShapeSize(rOutShape);
std::vector<float> resultData2(size2, 0);
ret = aclrtMemcpy(
resultData2.data(), resultData2.size() * sizeof(resultData2[0]), rOutDeviceAddr, size2 * sizeof(resultData2[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 < size2; i++) {
LOG_PRINT("result[%ld] is: %f\n", i, resultData2[i]);
}
return ACL_SUCCESS;
}
int main()
{
int32_t deviceId = 0;
aclrtStream stream;
auto ret = InitAcl(deviceId, &stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("InitAcl failed. ERROR: %d\n", ret); return ret);
std::vector<int64_t> selfShape = {1, 1, 4, 4};
std::vector<int64_t> qOutShape = {1, 1, 4, 4};
std::vector<int64_t> rOutShape = {1, 1, 4, 4};
void* selfDeviceAddr = nullptr;
void* qOutDeviceAddr = nullptr;
void* rOutDeviceAddr = nullptr;
aclTensor* self = nullptr;
aclTensor* qOut = nullptr;
aclTensor* rOut = nullptr;
ret = CreateInputs(
selfShape, qOutShape, rOutShape, &selfDeviceAddr, &qOutDeviceAddr, &rOutDeviceAddr, &self, &qOut, &rOut);
CHECK_RET(ret == ACL_SUCCESS, return ret);
int64_t mode = 0;
uint64_t workspaceSize = 0;
void* workspaceAddr = nullptr;
ret = ExecOpApi(
self, qOut, rOut, mode, &workspaceAddr, workspaceSize, qOutDeviceAddr, rOutDeviceAddr, qOutShape, rOutShape,
stream);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Destroy the tensor.
aclDestroyTensor(self);
aclDestroyTensor(qOut);
aclDestroyTensor(rOut);
// Free the device memory.
aclrtFree(selfDeviceAddr);
aclrtFree(qOutDeviceAddr);
aclrtFree(rOutDeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}