aclnnTril&aclnnInplaceTril
Applicable Products
| Product | Supported |
|---|---|
| √ | |
| √ | |
| √ | |
| × | |
| × | |
| √ |
Function
Operator function: Zeros out the upper-right portion of the last two dimensions (counted from left to right by shape) of the input self Tensor along the diagonal. The diagonal parameter can be positive or negative, and defaults to zero. A positive value shifts the main diagonal toward the upper-right direction, and a negative value shifts it toward the lower-left direction.
Formula: In the following, i denotes the index for iterating over elements of the second-to-last dimension (i is the row index), j denotes the index for iterating over elements of the last dimension (j is the column index), and d denotes diagonal. In the 2D coordinate graph corresponding to (i, j), i+d==j indicates being on the diagonal.
$$ On and to the lower-left of the diagonal, i.e., i+d>=j, retain the original value: out_{i, j} = self_{i, j}\\ To the upper-right of the diagonal, i.e., i+d<j, set to zero (excluding the diagonal): out_{i, j} = 0 $$Example:
$self = \begin{bmatrix} [9&6&3] \\ [1&2&3] \\ [3&4&1] \end{bmatrix}$, the result of triu(self, diagonal=0) is: $\begin{bmatrix} [9&0&0] \\ [1&2&0] \\ [3&4&1] \end{bmatrix}$; with diagonal adjusted, the result of triu(self, diagonal=1) is: $\begin{bmatrix} [9&6&0] \\ [1&2&3] \\ [3&4&1] \end{bmatrix}$; with diagonal set to -1, the result of triu(self, diagonal=-1) is: $\begin{bmatrix} [0&0&0] \\ [1&0&0] \\ [3&4&0] \end{bmatrix}$.
Function Prototype
aclnnTril and aclnnInplaceTril implement the same function. The differences in usage are as follows. Select the appropriate operator based on your actual scenario.
aclnnTril: Create a new output tensor object to store the computation result.
aclnnInplaceTril: No need to create a new output tensor object. The computation result is stored directly in the memory of the input tensor.
Each operator is divided into a two-phase API. You must first call the "aclnnTrilGetWorkspaceSize" or "aclnnInplaceTrilGetWorkspaceSize" API to obtain the input parameters and calculate the required workspace size based on the computation process, and then call the "aclnnTril" or "aclnnInplaceTril" API to perform the computation.
aclnnStatus aclnnTrilGetWorkspaceSize(
const aclTensor* self,
int64_t diagonal,
aclTensor* out,
uint64_t* workspaceSize,
aclOpExecutor** executor)aclnnStatus aclnnTril(
void* workspace,
uint64_t workspaceSize,
aclOpExecutor* executor,
const aclrtStream stream)aclnnStatus aclnnInplaceTrilGetWorkspaceSize(
const aclTensor* selfRef,
int64_t diagonal,
uint64_t* workspaceSize,
aclOpExecutor** executor)aclnnStatus aclnnInplaceTril(
void* workspace,
uint64_t workspaceSize,
aclOpExecutor* executor,
const aclrtStream stream)aclnnTrilGetWorkspaceSize
Parameters
Parameter Input/Output Description Instruction Data Type Data Format Dimension (Shape) Non-contiguous Tensor self (aclTensor*) Input Represents the target tensor to be transformed, which is self in the formula. - DOUBLE, FLOAT, FLOAT16, INT16, INT32, INT64, INT8, UINT16, UINT32, UINT64, UINT8, BOOL, BFLOAT16, COMPLEX32, COMPLEX64 ND 2-8 √ diagonal (int64_t) Input Position of the diagonal. - int64_t - - - out (aclTensor*) Input Represents the output tensor. - DOUBLE, FLOAT, FLOAT16, INT16, INT32, INT64, INT8, UINT16, UINT32, UINT64, UINT8, BOOL, BFLOAT16, COMPLEX32, COMPLEX64 - 2-8 - workspaceSize (uint64_t*) Output Parameter Returns the workspace size to be applied for on the Device side. - - - - - executor (aclOpExecutor**) Output Parameter Returns the op executor, which contains the operator computation flow. - - - - - Ascend 950PR/Ascend 950DT : COMPLEX32 and COMPLEX64 are only supported on this product.Atlas inference products andAtlas 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 Value Return Code Description ACLNN_ERR_PARAM_NULLPTR 161001 The input self or out is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data type of self or out is not within the supported range. The data types of self and out are inconsistent. The shapes of self and out are inconsistent. The shapes of self and out are inconsistent. The data formats of self and out are inconsistent. The number of dimensions of self is greater than 8 or less than 2.
aclnnTril
Parameters
Parameter Input/Output Description workspace Input Specifies the workspace memory address applied for on the device side. workspaceSize Input Specifies the workspace size applied for on the device side, obtained through the first-phase API aclnnTrilGetWorkspaceSize. Executor Input Operator executor that contains the operator computation flow. stream Input Specifies the stream on which the task is executed. Return Value
aclnnStatus: return code. For details, see aclnn Return Codes.
aclnnInplaceTrilGetWorkspaceSize
Parameters
Parameter Input/Output Description Instruction Data Type Data Format Dimension (Shape) Non-contiguous Tensor selfRef (aclTensor*) Input Represents the target tensor to be transformed, which is self in the formula. - DOUBLE, FLOAT, FLOAT16, INT16, INT32, INT64, INT8, UINT16, UINT32, UINT64, UINT8, BOOL, BFLOAT16, COMPLEX32, COMPLEX64 ND 2-8 √ diagonal (int64_t) Input Position of the diagonal. - int64 - - - workspaceSize (uint64_t*) Output Parameter Returns the workspace size to be applied for on the Device side. - - - - - executor (aclOpExecutor**) Output Parameter Returns the operator executor, which contains the operator computation process. - - - - - Ascend 950PR/Ascend 950DT : COMPLEX32 and COMPLEX64 are only supported on this product.Atlas inference products andAtlas 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 Value Return Code Description ACLNN_ERR_PARAM_NULLPTR 161001 The passed selfRef is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data type of selfRef is not within the supported range. The dimension of self is greater than 8, or less than 2 and not 0.
aclnnInplaceTril
Parameters
Parameter Input/Output Description workspace Input Address of the workspace memory applied for on the device side. workspaceSize Input Size of the workspace applied for on the device side, obtained from the first-phase API aclnnInplaceTrilGetWorkspaceSize. 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:
- aclnnTril&aclnnInplaceTril default 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 "acl/acl.h"
#include "aclnnop/aclnn_tril.h"
#include <iostream>
#include <vector>
#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 shape_size = 1;
for (auto i : shape) {
shape_size *= i;
}
return shape_size;
}
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 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 data 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() {
// 1. Boilerplate: Initialize the device and stream. See the ACL API manual.
// Set deviceId based on the actual device.
int32_t deviceId = 0;
aclrtStream stream;
auto ret = Init(deviceId, &stream);
// Handle the check result as needed.
CHECK_RET(ret == 0, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);
// 2. Construct the input and output based on the API definition.
std::vector<int64_t> selfShape = {3, 3};
std::vector<int64_t> outShape = {3, 3};
void* selfDeviceAddr = nullptr;
void* outDeviceAddr = nullptr;
aclTensor* self = nullptr;
aclTensor* out = nullptr;
std::vector<int> selfHostData = {1, 2, 3, 4, 5, 6, 7, 8, 9};
std::vector<int> outHostData ={1, 2, 3, 4, 5, 6, 7, 8, 9};
int diagonal = 0;
// Create the self aclTensor.
ret = CreateAclTensor(selfHostData, selfShape, &selfDeviceAddr, aclDataType::ACL_INT32, &self);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the out aclTensor.
ret = CreateAclTensor(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_INT32, &out);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// 3. Call the CANN operator library API. Replace this with the specific API.
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
// Call the first-phase API of aclnnTril.
ret = aclnnTrilGetWorkspaceSize(self, diagonal, out, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnTrilGetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
// Apply for 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 aclnnTril.
ret = aclnnTril(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnTril failed. ERROR: %d\n", ret); return ret);
uint64_t inplaceWorkspaceSize = 0;
aclOpExecutor* inplaceExecutor;
// Call the first-phase API of aclnnInplaceTril.
ret = aclnnInplaceTrilGetWorkspaceSize(self, diagonal, &inplaceWorkspaceSize, &inplaceExecutor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplaceTrilGetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
// Apply for device memory based on the workspaceSize calculated by the first-phase API.
void* inplaceWorkspaceAddr = nullptr;
if (inplaceWorkspaceSize > 0) {
ret = aclrtMalloc(&inplaceWorkspaceAddr, inplaceWorkspaceSize, 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 aclnnInplaceTril.
ret = aclnnInplaceTril(inplaceWorkspaceAddr, inplaceWorkspaceSize, inplaceExecutor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplaceTril failed. ERROR: %d\n", ret); return ret);
// 4. Boilerplate: synchronize and wait for task execution to finish.
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 side memory to the host side. Modify based on the specific API definition.
auto size = GetShapeSize(outShape);
std::vector<int> resultData(size, 0);
ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), outDeviceAddr, size * sizeof(int),
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: %d\n", i, resultData[i]);
}
auto inplaceSize = GetShapeSize(selfShape);
std::vector<int> inplaceResultData(inplaceSize, 0);
ret = aclrtMemcpy(inplaceResultData.data(), inplaceResultData.size() * sizeof(inplaceResultData[0]), selfDeviceAddr,
inplaceSize * sizeof(int), 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 < inplaceSize; i++) {
LOG_PRINT("inplaceResult[%ld] is: %d\n", i, inplaceResultData[i]);
}
// 6. Release aclTensor and aclScalar. Modify based on the specific API definition.
aclDestroyTensor(self);
aclDestroyTensor(out);
// 7. Release device resources. Modify based on the specific API definition.
aclrtFree(selfDeviceAddr);
aclrtFree(outDeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}