aclnnNeTensor&aclnnInplaceNeTensor
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 whether the values of elements in self (selfRef) are not equal to the values in other.
- Formula:
Function Prototype
- aclnnNeTensor and aclnnInplaceNeTensor implement the same function. The differences in usage are as follows. Choose a proper operator based on your actual use case.
- aclnnNeTensor: requires creating a new output tensor object to store the calculation result.
- aclnnInplaceNeTensor: does not require a new output tensor object. The calculation result is stored directly in the memory of the input tensor.
- Each operator uses a two-phase API. You must first call the "aclnnNeTensorGetWorkspaceSize" or "aclnnInplaceNeTensorGetWorkspaceSize" API to obtain the input parameters and calculate the required workspace size based on the computation process, and then call the "aclnnNeTensor" or "aclnnInplaceNeTensor" API to perform the calculation.
aclnnStatus aclnnNeTensorGetWorkspaceSize(
const aclTensor *self,
const aclTensor *other,
aclTensor *out,
uint64_t *workspaceSize,
aclOpExecutor **executor)aclnnStatus aclnnNeTensor(
void *workspace,
uint64_t workspaceSize,
aclOpExecutor *executor,
aclrtStream stream)aclnnStatus aclnnInplaceNeTensorGetWorkspaceSize(
aclTensor *selfRef,
const aclTensor *other,
uint64_t *workspaceSize,
aclOpExecutor **executor)aclnnStatus aclnnInplaceNeTensor(
void *workspace,
uint64_t workspaceSize,
aclOpExecutor *executor,
aclrtStream stream)aclnnNeTensorGetWorkspaceSize
Parameters:
Return value:
aclnnStatus: Return status code. See specifically 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 When self, other, and out are null pointers. ACLNN_ERR_PARAM_INVALID 161002 When the data type of self, other, or out is not in the supported range. When the dimension of self, other, or out is greater than 8. When the data types of self and other cannot be deduced. When the shapes of self and other cannot be broadcast. When the shape of out is inconsistent with the shape after broadcast.
aclnnNeTensor
Parameters:
Parameter Input/Output Description workspace Input Workspace memory address requested on the Device. workspaceSize Input Workspace size requested on the Device, obtained by calling the first-phase API aclnnNeTensorGetWorkspaceSize. executor Input Executor of the op that includes the calculation process of the operator. stream Input Stream for executing the task. Return value:
aclnnStatus: return status code, see specifically aclnn return codes.
aclnnInplaceNeTensorGetWorkspaceSize
Parameters:
Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor selfRef (aclTensor*) Input/Output selfRef in the formula. DOUBLE, FLOAT16, FLOAT, BFLOAT16, INT64, INT32, INT8, UINT8, BOOL, INT16, COMPLEX64, COMPLEX128, UINT64 ND 0-8 √ √ other (aclTensor*) Input other in the formula. DOUBLE, FLOAT16, FLOAT, BFLOAT16, INT64, INT32, INT8, UINT8, BOOL, INT16, COMPLEX64, COMPLEX128, UINT64 ND 0-8 √ √ workspaceSize (uint64_t*) Output Returns the workspace size that needs to be requested on the Device side. - - - - - executor (aclOpExecutor**) Output Returns the op executor, which includes the calculation flow of the operator. - - - - - Return value:
aclnnStatus: Return status code. For details, see specifically 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 When selfRef and other are null pointers. ACLNN_ERR_PARAM_INVALID 161002 When the data types of selfRef and other are not within the supported range. When the data types of selfRef and other cannot be deduced. When the shapes of selfRef and other cannot be broadcast. When the shape of selfRef and other after broadcast is not equal to the shape of selfRef. When the dimensions of selfRef and other are greater than 8.
aclnnInplaceNeTensor
Parameters:
Parameter Input/Output Description workspace Input Address of the workspace memory requested by the Device. workspaceSize Input Size of the workspace memory requested by the Device, obtained by the first-phase API aclnnInplaceNeTensorGetWorkspaceSize. executor Input Executor of the op, which includes the calculation flow of the operator. stream Input Stream for task execution. Return value:
aclnnStatus: return status code. See specifically aclnn return codes.
Constraints
- Deterministic computation:
- aclnnNeTensor&aclnnInplaceNeTensor defaults to a deterministic implementation.
Example
The following is sample code for reference only. See Compile and Run Samples for the specific compilation and execution process.
Sample code of the aclnnNeTensor&aclnnInplaceNeTensor API call:
#include <iostream>
#include <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_ne_tensor.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); aclFinalize(); return ret);
ret = aclrtCreateStream(stream);
CHECK_RET(ret == ACL_SUCCESS,
LOG_PRINT("aclrtCreateStream failed. ERROR: %d\n", ret);
aclrtResetDevice(deviceId); aclFinalize(); 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 request 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 the host-side data onto 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 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/stream. For details, see the acl API documentation.
// 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. The construction needs to be based on the interface definition of the API.
std::vector<int64_t> selfShape = {4, 2};
std::vector<int64_t> otherShape = {4, 2};
std::vector<int64_t> outShape = {4, 2};
void *selfDeviceAddr = nullptr;
void *otherDeviceAddr = nullptr;
void *outDeviceAddr = nullptr;
aclTensor *self = nullptr;
aclTensor *other = nullptr;
aclTensor *out = nullptr;
std::vector<double> selfHostData = {0, 1, 2, 3, 4, 5, 6, 7};
std::vector<double> otherHostData = {1, 1, 1, 2, 2, 2, 3, 3};
std::vector<double> outHostData = {0, 0, 0, 0, 0, 0, 0, 0};
// Create the self aclTensor.
ret = CreateAclTensor(selfHostData, selfShape, &selfDeviceAddr, aclDataType::ACL_DOUBLE, &self);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the other aclTensor.
ret = CreateAclTensor(otherHostData, otherShape, &otherDeviceAddr, aclDataType::ACL_DOUBLE, &other);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the out aclTensor.
ret = CreateAclTensor(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_DOUBLE, &out);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// aclnnNeTensor API call example
// 3. Call the CANN operator library API.
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
// Call the first-phase API of aclnnNeTensor.
ret = aclnnNeTensorGetWorkspaceSize(self, other, out, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnNeTensorGetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
// Allocate device memory based on the workspaceSize computed 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 aclnnNeTensor.
ret = aclnnNeTensor(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnNeTensor failed. ERROR: %d\n", ret); return ret);
// 4. (Boilerplate) Synchronize and wait for the task execution to complete.
ret = aclrtSynchronizeStream(stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret);
// 5. Obtain the output values and copy the result on the device-side memory to the host side.
auto size = GetShapeSize(outShape);
std::vector<double> 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]);
}
//aclnnInplaceNeTensor API call example
// 3. Call the CANN operator library API.
LOG_PRINT("\ntest aclnnInplaceNeTensor\n");
// For the in-place API, selfRef is the input and output tensor (updates self in place). Pass selfRef as the parameter.
aclTensor *selfRef = self;
// Call the first-phase API of aclnnInplaceNeTensor.
ret = aclnnInplaceNeTensorGetWorkspaceSize(selfRef, other, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplaceNeTensorGetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
// Allocate device memory based on the workspaceSize computed by the first-phase API. Release the workspace requested by the first-phase API first to prevent memory leaks.
if (workspaceAddr != nullptr) {
aclrtFree(workspaceAddr);
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 aclnnInplaceNeTensor.
ret = aclnnInplaceNeTensor(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplaceNeTensor failed. ERROR: %d\n", ret); return ret);
// 4. (Boilerplate) Synchronize and wait for the task execution to complete.
ret = aclrtSynchronizeStream(stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret);
// 5. Obtain the output values and copy the result on the device-side memory to the host side. This needs to be based on the interface definition of the specific API.
ret = aclrtMemcpy(resultData.data(),
resultData.size() * sizeof(resultData[0]), selfDeviceAddr,
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]);
}
// 6. Release the aclTensors. This needs to be based on the interface definition of the specific API.
aclDestroyTensor(self);
aclDestroyTensor(other);
aclDestroyTensor(out);
// 7. Release the device resources. This needs to be based on the interface definition of the specific API.
aclrtFree(selfDeviceAddr);
aclrtFree(otherDeviceAddr);
aclrtFree(outDeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}