aclnnDivMods&aclnnInplaceDivMods
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 division calculation and selects the rounding operation based on the mode parameter.
- Formula:
Function Prototype
aclnnDivMods and aclnnInplaceDivMods implement the same function. The differences in usage are as follows. Select the appropriate operator based on your actual scenario.
- aclnnDivMods: Create a new output tensor object to store the computation result.
- aclnnInplaceDivMods: 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. Call "aclnnDivModsGetWorkspaceSize" or "aclnnInplaceDivModsGetWorkspaceSize" first to obtain the workspace size required for the computation and the executor that contains the operator computation flow. Then call "aclnnDivMods" or "aclnnInplaceDivMods" to perform the computation.
aclnnStatus aclnnDivModsGetWorkspaceSize(
const aclTensor* self,
const aclScalar* other,
int mode,
aclTensor* out,
uint64_t* workspaceSize,
aclOpExecutor** executor)aclnnStatus aclnnDivMods(
void* workspace,
uint64_t workspaceSize,
aclOpExecutor* executor,
aclrtStream stream)aclnnStatus aclnnInplaceDivModsGetWorkspaceSize(
aclTensor* selfRef,
const aclScalar* other,
int mode,
uint64_t* workspaceSize,
aclOpExecutor** executor)aclnnStatus aclnnInplaceDivMods(
void* workspace,
uint64_t workspaceSize,
aclOpExecutor* executor,
aclrtStream stream)aclnnDivModsGetWorkspaceSize
Parameters:
Atlas training products : The BFLOAT16 data type is not supported.
Ascend 950PR&950DT products :- The data type must satisfy the TensorScalar deduction relationship.
- When mode is 0, the data type of out supports FLOAT, FLOAT16, DOUBLE, BFLOAT16, COMPLEX128, and COMPLEX64. If the data types of self and other after deduction are integer or Boolean types, the deducted data type is converted to FLOAT;
- When mode is 1 or 2, the out data type supports FLOAT, FLOAT16, DOUBLE, INT32, INT64, INT16, INT8, UINT8, BOOL, BFLOAT16, COMPLEX128, and COMPLEX64.
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 self, other, or out is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data types and data formats of self and other are not within the supported range. self and other do not comply with the data type deduction rule. The derived data type cannot be converted to the type of the specified output out. The dimensions of self and other are greater than 8. The value of mode is not within the range of 0, 1, or 2. When mode is 1 or 2, the derived data type of self and other is COMPLEX64 or COMPLEX128.
aclnnDivMods
Parameters:
Parameter Input/Output Description workspace Input Workspace memory address requested on the Device side. workspaceSize Input Workspace size requested on the Device side, obtained by the first-phase API aclnnDivModsGetWorkspaceSize. executor Input Executor of the op, including the operator computation process. stream Input Specifies the Stream for task execution. Return value
aclnnStatus: return status code. For details, see aclnn Return Codes.
aclnnInplaceDivModsGetWorkspaceSize
Parameters:
Atlas training products : BFLOAT16 data type is not supported.
Ascend 950PR&950DT products :- The data types must comply with the TensorScalar deduction relationship.
- When mode is 0 and the data type after deduction of selfRef and other is an integer type or Boolean type, the data type after deduction is converted to FLOAT.
- When mode is 0, selfRef supports FLOAT, FLOAT16, DOUBLE, and BFLOAT16. When mode is 1 or 2, selfRef supports FLOAT, FLOAT16, DOUBLE, INT32, INT64, INT16, INT8, UINT8, BOOL, and BFLOAT16. other does not support COMPLEX128 or COMPLEX64.
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 selfRef or other is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data types and data formats of selfRef and other are not within the supported range. selfRef and other do not satisfy the data type deduction rule. The dimensions of selfRef and other are greater than 8. The value of mode is not within the range of 0, 1, or 2. If mode is 1 or 2, the derived data types of selfRef and other are COMPLEX64 or COMPLEX128.
aclnnInplaceDivMods
Parameters:
Parameter Input/Output Description workspace Input Address of the workspace memory allocated on the Device. workspaceSize Input Size of the workspace allocated on the Device, obtained by the first-phase API aclnnInplaceDivModsGetWorkspaceSize. executor Input Executor of the operator, containing the computation flow of the operator. stream Input Stream for executing tasks. Return value
aclnnStatus: return status code. For details, see aclnn Return Codes.
Constraints
- Deterministic computation:
- aclnnDivMods&aclnnInplaceDivMods default to a deterministic implementation.
Atlas A2 products ,Atlas A3 products : When mode is 1 (trunc mode) or 2 (floor mode), the FLOAT16/BFLOAT16 data types have limited precision and cannot represent all decimals. Errors may occur during rounding/floor operations. Use a higher-precision data type such as FLOAT32.
Example
The sample code is as follows and 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_div.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 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 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 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 main() {
// 1. (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);
// 2. Construct inputs and outputs. Customize the construction based on the API.
std::vector<int64_t> selfShape = {4, 2};
std::vector<int64_t> outShape = {4, 2};
void* selfDeviceAddr = nullptr;
void* outDeviceAddr = nullptr;
aclTensor* self = nullptr;
aclScalar* other = nullptr;
aclTensor* out = nullptr;
std::vector<float> selfHostData = {0, 1, 2, 3, 4, 5, 6, 7};
std::vector<float> outHostData(8, 0);
float otherValue = 2.0f;
// Create the self aclTensor.
ret = CreateAclTensor(selfHostData, selfShape, &selfDeviceAddr, aclDataType::ACL_FLOAT, &self);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the other aclScalar.
other = aclCreateScalar(&otherValue, aclDataType::ACL_FLOAT);
CHECK_RET(other != nullptr, return ret);
// Create the out aclTensor.
ret = CreateAclTensor(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_FLOAT, &out);
CHECK_RET(ret == ACL_SUCCESS, return ret);
int mode = 2;
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
// aclnnDivMods API call example
LOG_PRINT("test aclnnDivMods\n");
// 3. Call the CANN operator library API and change it to the specific API name.
// Call the first-phase API of aclnnDivMods.
ret = aclnnDivModsGetWorkspaceSize(self, other, mode, out, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnDivModsGetWorkspaceSize 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 aclnnDivMods.
ret = aclnnDivMods(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnDivMods failed. ERROR: %d\n", ret); return ret);
// 4. (boilerplate) Synchronously 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 value and copy the result on the device side to the host side. Modify it based on the definition of the specific API.
ret = aclrtMemcpy(outHostData.data(), outHostData.size() * sizeof(outHostData[0]), outDeviceAddr,
8 * sizeof(outHostData[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 < 8; i++) {
LOG_PRINT("result[%ld] is: %f\n", i, outHostData[i]);
}
// aclnnInplaceDivMods API call example
LOG_PRINT("\ntest aclnnInplaceDivMods\n");
// 3. Call the CANN operator library API and change it to the specific API name.
// Call the first-phase API of aclnnInplaceDivMods.
ret = aclnnInplaceDivModsGetWorkspaceSize(self, other, mode, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplaceDivModsGetWorkspaceSize 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 aclnnInplaceDivMods.
ret = aclnnInplaceDivMods(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplaceDivMods failed. ERROR: %d\n", ret); return ret);
// 4. (Boilerplate) Synchronize and wait for 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 value and copy the result from device memory to host memory. The code needs to be modified based on the specific API definition.
auto size = GetShapeSize(outShape);
std::vector<float> resultData(size, 0);
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 aclTensor and aclScalar. The code needs to be modified based on the specific API definition.
aclDestroyTensor(self);
aclDestroyScalar(other);
aclDestroyTensor(out);
// 7. Release Device resources.
aclrtFree(selfDeviceAddr);
aclrtFree(outDeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}