aclnnFatreluMul
Applicable Products
| Product | Supported |
|---|---|
| × | |
| √ | |
| √ | |
| × | |
| × | |
| × |
Function
- Description:
Splits the input tensor into two tensors along the last dimension: x1 and x2. Applies the Threshold operation to x1 on the left, and multiplies the result with x2.
- Formula:
Given an input tensor input whose last dimension has a length of 2d, the following computation is performed:
Split input into two parts:
Apply the Threshold activation function to x1, defined as follows:
Therefore, compute:
The final output is the element-wise product of x1 and x2:
Function Prototype
Each operator is divided into a two-phase API. You must first call the "aclnnFatreluMulGetWorkspaceSize" API to obtain the input parameters and calculate the required workspace size based on the process, and then call the "aclnnFatreluMul" API to perform the computation.
aclnnStatus aclnnFatreluMulGetWorkspaceSize(
const aclTensor *input,
const aclTensor *threshold,
const aclTensor *out,
uint64_t *workspaceSize,
aclOpExecutor **executor)aclnnStatus aclnnFatreluMul(
void *workspace,
uint64_t workspaceSize,
aclOpExecutor *executor,
aclrtStream stream)aclnnFatreluMulGetWorkspaceSize
Parameters:
Parameter Input/Output Description Instruction Data Type Data Format Shape Non-contiguous Tensor input (aclTensor*) Input The input tensor, which is input in the formula. - Empty tensors are supported.
- The last dimension is an even number and less than or equal to 1024.
- The product of other dimensions is less than or equal to 200000.
BFLOAT16, FLOAT16, FLOAT ND 2-8 √ threshold (aclTensor*) Input Boundary value for the Threshold computation, i.e., threshold in the formula. - The data type and data format must be consistent with input.
- The tensor must contain only one element.
- NaN and Inf are not supported.
BFLOAT16, FLOAT16, FLOAT ND 0-8 √ out (aclTensor*) Output Output tensor, out in the formula. - The output data type must be consistent with the input data type.
- The value of the last dimension of the output shape is half of the value of the last dimension of the input shape.
- The values of other dimensions must be consistent with the values of other dimensions of the input shape.
BFLOAT16, FLOAT16, FLOAT ND 2-8 √ workspaceSize (uint64_t*) Output Returns the workspace size applied on the Device side. - - - - - executor(aclOpExecutor**) Output Returns the op executor, which contains the operator computation flow. - - - - - Return value
aclnnStatus: return code. For details, see aclnn Return Code.
The first-phase API performs input parameter validation and returns an error in the following scenarios:
Return Code Error Code Description ACLNN_ERR_PARAM_NULLPTR 161001 The input, threshold, or out is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data type of input is not within the supported range.
aclnnFatreluMul
Parameters:
Parameter Input/Output Description workspace Input Address of the workspace memory applied on the device side. workspaceSize Input Size of the workspace applied on the device side, obtained by the first-phase API aclnnFatreluMulGetWorkspaceSize. executor Input The op executor, which contains the operator computation flow. stream Input Specifies the stream for executing the task. Return value
aclnnStatus: return code. For details, see aclnn Return Code.
Constraints
Deterministic computation:
- aclnnFatreluMul defaults to a deterministic implementation.
In typical scenarios, the last axis is a multiple of 16. When the last axis is not 32-byte aligned, it is recommended to use the small operator splicing logic.
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_fatrelu_mul.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;
}
void PrintOutResult(std::vector<int64_t> &shape, void** deviceAddr) {
auto size = GetShapeSize(shape);
std::vector<float> resultData(size, 0);
auto ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]),
*deviceAddr, 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);
for (int64_t i = 0; i < size; i++) {
LOG_PRINT("mean result[%ld] is: %f\n", i, resultData[i]);
}
}
int Init(int32_t deviceId, aclrtStream* stream) {
// Boilerplate: initialize resources.
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);
// Apply for device memory by calling aclrtMalloc.
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);
// Copy host data to the device memory by calling aclrtMemcpy.
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 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];
}
// Create an aclTensor by calling aclCreateTensor.
*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. See the ACL API manual.
// Fill in the deviceId based on your 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);
// Construct the input and output. Customize the construction based on the API.
std::vector<int64_t> inputShape = {2, 4};
std::vector<float> inputHostData = {0, 1, 2, 3, 4, 5, 6, 7};
void* inputDeviceAddr = nullptr;
aclTensor* input = nullptr;
// Create the threshold aclTensor.
ret = CreateAclTensor(inputHostData, inputShape, &inputDeviceAddr, aclDataType::ACL_FLOAT, &input);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the threshold aclTensor.
std::vector<int64_t> threShape = {1};
std::vector<float> threData = {0.01};
void* threDeviceAddr = nullptr;
aclTensor* threshold = nullptr;
// Create the threshold aclTensor.
ret = CreateAclTensor(threData, threShape, &threDeviceAddr, aclDataType::ACL_FLOAT, &threshold);
CHECK_RET(ret == ACL_SUCCESS, return ret);
std::vector<int64_t> outShape = {2, 2};
std::vector<float> outHostData(2 * 2, 1);
aclTensor* out = nullptr;
void* outDeviceAddr = nullptr;
// Create the out aclTensor.
ret = CreateAclTensor(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_FLOAT, &out);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// 3. Call the CANN operator library API. Replace with the actual API name.
uint64_t workspaceSize = 16 * 1024 * 1024;
aclOpExecutor* executor;
// Call the first-phase API of aclnnFatreluMul.
ret = aclnnFatreluMulGetWorkspaceSize(input, threshold, out, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnFatreluMulGetWorkspaceSize 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 aclnnFatreluMul.
ret = aclnnFatreluMul(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnFatreluMul failed. ERROR: %d\n", ret); return ret);
// 4. (Boilerplate) Synchronously 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 memory to the host memory. Modify based on the specific API definition.
PrintOutResult(outShape, &outDeviceAddr);
// 6. Release aclTensor. Modify based on the specific API definition.
aclDestroyTensor(input);
aclDestroyTensor(threshold);
aclDestroyTensor(out);
// 7. Release device resources. Modify based on the specific API definition.
aclrtFree(inputDeviceAddr);
aclrtFree(threDeviceAddr);
aclrtFree(outDeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}