aclnnClampMaxTensor&aclnnInplaceClampMaxTensor
Applicable Products
| Product | Supported |
|---|---|
| √ | |
| √ | |
| √ | |
| × | |
| √ | |
| √ |
Function
Description: Clamps all elements of the input to the range [-inf, max].
Formula:
$$ {out}_{i} = min({{self}_{i}},{max}_{i}) $$
Function Prototype
aclnnClampMaxTensor and aclnnInplaceClampMaxTensor implement the same function. The differences are as follows. Select the appropriate operator based on your actual scenario.
aclnnClampMaxTensor: Creates a new output tensor object to store the computation result.
aclnnInplaceClampMaxTensor: Does not require creating a new output tensor object. Stores the computation result directly in the input tensor memory.
Each operator is a two-phase API. You must first call the "aclnnClampMaxTensorGetWorkspaceSize" or "aclnnInplaceClampMaxTensorGetWorkspaceSize" API to obtain the workspace size required for computation and the executor that contains the operator computation process, and then call the "aclnnClampMaxTensor" or "aclnnInplaceClampMaxTensor" API to perform the computation.
aclnnStatus aclnnClampMaxTensorGetWorkspaceSize(
const aclTensor* self,
const aclTensor* max,
aclTensor* out,
uint64_t* workspaceSize,
aclOpExecutor** executor)
```cpp
aclnnStatus aclnnClampMaxTensor(
void* workspace,
uint64_t workspaceSize,
aclOpExecutor* executor,
aclrtStream stream)
aclnnStatus aclnnInplaceClampMaxTensorGetWorkspaceSize(
aclTensor* selfRef,
const aclTensor* max,
uint64_t* workspaceSize,
aclOpExecutor** executor)
aclnnStatus aclnnInplaceClampMaxTensor(
void* workspace,
uint64_t workspaceSize,
aclOpExecutor* executor,
aclrtStream stream)
aclnnClampMaxTensorGetWorkspaceSize
Parameter Description:
Atlas training products ,Atlas inference products :- The Data Type of self and out does not support BOOL or BFLOAT16.
- The Data Type of max does not support BFLOAT16.
Atlas A2 training products/Atlas A2 inference products ,Atlas A3 training products/Atlas A3 inference products - The Data Type of self and out does not support BOOL.
Return Value:
aclnnStatus: Returns the status code. For details, see aclnn Return Codes.
The first-phase API completes input parameter validation and reports an error in the following scenarios:
aclnnClampMaxTensor
Parameter Description
Parameter Name Input/Output Description workspace Input The workspace memory address applied on the Device side. workspaceSize Input The workspace size applied on the Device side, obtained by the first-phase API aclnnClampMaxTensorGetWorkspaceSize. executor Input op executor that contains the operator computation flow. stream Input The Stream that specifies the task to execute. Return Value:
aclnnStatus: Returns the status code. For details, see aclnn Return Codes.
aclnnInplaceClampMaxTensorGetWorkspaceSize
Parameter Description:
Atlas training products ,Atlas inference products :- The Data Type of selfRef does not support BOOL or BFLOAT16.
- The Data Type of max does not support BFLOAT16.
Atlas A2 training products/Atlas A2 inference products ,Atlas A3 training products/Atlas A3 inference products ,Ascend 950PR/Ascend 950DT - selfRef and maxData Type must satisfy the Data Type deduction rules (see TensorScalar Deduction Relationship).
- The Data Type of selfRef does not support BOOL.
Return Value:
aclnnStatus: Returns the status code. For details, see aclnn Return Codes.
The first-phase API completes input parameter validation and reports an error in the following scenarios:
aclnnInplaceClampMaxTensor
Parameter Description
Parameter Name Input/Output Description workspace Input The workspace memory address applied on the Device side. workspaceSize Input The workspace size applied on the Device side, obtained by the first-phase API aclnnInplaceClampMaxTensorGetWorkspaceSize. executor Input op executor that contains the operator computation flow. stream Input The Stream that specifies the task to execute. Return Value:
aclnnStatus: Returns the status code. For details, see aclnn Return Codes.
Constraints
- Deterministic Computation:
- aclnnClampMaxTensor&aclnnInplaceClampMaxTensor uses a deterministic implementation by default.
Example
The sample code is for reference only. For the specific compilation and execution process, refer to Compile and Run Samples.
aclnnClampMaxTensor Call Example:
#include <iostream>
#include <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_clamp.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 on-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-side data to on-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);
// 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];
}
// 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 PrepareInputAndOutput(
std::vector<int64_t>& selfShape, std::vector<int64_t>& outShape, std::vector<int64_t>& maxShape,
void** selfDeviceAddr, aclTensor** self, void** maxDeviceAddr, aclTensor** max, void** outDeviceAddr,
aclTensor** out)
{
std::vector<double> selfHostData = {0, 1, 2, 3, 4, 5, 6, 7};
std::vector<double> maxHostData = {1, 1, 1, 2, 2, 2, 3, 3};
std::vector<double> outHostData(8, 0);
// Create the self aclTensor.
auto ret = CreateAclTensor(selfHostData, selfShape, selfDeviceAddr, aclDataType::ACL_DOUBLE, self);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the max aclTensor.
ret = CreateAclTensor(maxHostData, maxShape, maxDeviceAddr, aclDataType::ACL_DOUBLE, max);
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);
return ACL_SUCCESS;
}
void ReleaseTensorAndScalar(aclTensor* self, aclTensor* max, aclTensor* out)
{
aclDestroyTensor(self);
aclDestroyTensor(max);
aclDestroyTensor(out);
}
void ReleaseDevice(
void* selfDeviceAddr, void* maxDeviceAddr, void* outDeviceAddr, uint64_t workspaceSize, void* workspaceAddr, aclrtStream stream,
int32_t deviceId)
{
aclrtFree(selfDeviceAddr);
aclrtFree(maxDeviceAddr);
aclrtFree(outDeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
}
int main() {
// 1. (boilerplate) Initialize device/stream. See the ACL API manual.
// 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 input and output. Custom construction is required based on the API interface definition.
std::vector<int64_t> selfShape = {4, 2};
std::vector<int64_t> maxShape = {4, 2};
std::vector<int64_t> outShape = {4, 2};
void* selfDeviceAddr = nullptr;
void* maxDeviceAddr = nullptr;
void* outDeviceAddr = nullptr;
aclTensor* self = nullptr;
aclTensor* max = nullptr;
aclTensor* out = nullptr;
ret = PrepareInputAndOutput(
selfShape, maxShape, outShape, &selfDeviceAddr, &self, &maxDeviceAddr, &max, &outDeviceAddr, &out);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// 3. Call the CANN operator library API. Replace with the specific API name.
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
// Call the first-phase API of aclnnClampMaxTensor.
ret = aclnnClampMaxTensorGetWorkspaceSize(self, max, out, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnClampMaxTensorGetWorkspaceSize 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 aclnnClampMaxTensor.
ret = aclnnClampMaxTensor(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnClampMaxTensor failed. ERROR: %d\n", ret); return ret);
// 4. (Boilerplate) Synchronize and wait for task completion.
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 on-device memory to the host side. Modify based on the specific API interface definition.
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]);
}
// 6. Release aclTensor and aclScalar. Modify based on the specific API interface definition.
ReleaseTensorAndScalar(self, max, out);
// 7. Release device resources. Modify based on the specific API interface definition.
ReleaseDevice(selfDeviceAddr, maxDeviceAddr, outDeviceAddr, workspaceSize, workspaceAddr, stream, deviceId);
return 0;
}
aclnnInplaceClampMaxTensor Call Example:
#include <iostream>
#include <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_clamp.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 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 apply for on-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 on-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 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 aclCreateTensor 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 PrepareInputAndOutput(
std::vector<int64_t>& selfShape, std::vector<int64_t>& maxShape, void** selfDeviceAddr, aclTensor** self,
void** maxDeviceAddr, aclTensor** max)
{
std::vector<double> selfHostData = {0, 1, 2, 3, 4, 5, 6, 7};
std::vector<double> maxHostData = {1, 1, 1, 2, 2, 2, 3, 3};
// Create the self aclTensor.
auto ret = CreateAclTensor(selfHostData, selfShape, selfDeviceAddr, aclDataType::ACL_DOUBLE, self);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the max aclTensor.
ret = CreateAclTensor(maxHostData, maxShape, maxDeviceAddr, aclDataType::ACL_DOUBLE, max);
CHECK_RET(ret == ACL_SUCCESS, return ret);
return ACL_SUCCESS;
}
void ReleaseTensorAndScalar(aclTensor* self, aclTensor* max)
{
aclDestroyTensor(self);
aclDestroyTensor(max);
}
void ReleaseDevice(
void* selfDeviceAddr, void* maxDeviceAddr, uint64_t workspaceSize, void* workspaceAddr, aclrtStream stream,
int32_t deviceId)
{
aclrtFree(selfDeviceAddr);
aclrtFree(maxDeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
}
int main() {
// 1. (boilerplate) Initialize the device/stream. See the ACL API manual.
// Set 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);
// 2. Construct the input and output. Customize the construction based on the API.
std::vector<int64_t> selfShape = {4, 2};
std::vector<int64_t> maxShape = {4, 2};
void* selfDeviceAddr = nullptr;
void* maxDeviceAddr = nullptr;
aclTensor* self = nullptr;
aclTensor* max = nullptr;
ret = PrepareInputAndOutput(selfShape, maxShape, &selfDeviceAddr, &self, &maxDeviceAddr, &max);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// 3. Call the CANN operator library API. Replace with the specific API name.
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
// Call the first-phase API of aclnnInplaceClampMaxTensor.
ret = aclnnInplaceClampMaxTensorGetWorkspaceSize(self, max, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplaceClampMaxTensorGetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
// Apply for on-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 aclnnInplaceClampMaxTensor.
ret = aclnnInplaceClampMaxTensor(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplaceClampMaxTensor 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 on-device memory to host memory. Modify based on the interface definition of the specific API.
auto size = GetShapeSize(selfShape);
std::vector<double> 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. Modify based on the interface definition of the specific API.
ReleaseTensorAndScalar(self, max);
// 7. Release device resources. Modify based on the interface definition of the specific API.
ReleaseDevice(selfDeviceAddr, maxDeviceAddr, workspaceSize, workspaceAddr, stream, deviceId);
return 0;
}