aclnnOneHot
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 : Supported
Atlas training products : Supported
Function
Description: For an input self of length n, the one_hot computation produces an output out with n*k elements, where the value of k is numClasses.
Formula:
Function Prototype
Each operator uses a two-phase API. Call the "aclnnOneHotGetWorkspaceSize" API first to obtain the workspace size required for the computation and the executor that contains the computation process of the operator, and then call the "aclnnOneHot" API to execute the computation.
aclnnStatus aclnnOneHotGetWorkspaceSize(
const aclTensor* self,
int numClasses,
const aclTensor* onValue,
const aclTensor* offValue,
int64_t axis,
aclTensor* out,
uint64_t* workspaceSize,
aclOpExecutor** executor)aclnnStatus aclnnOneHot(
void* workspace,
uint64_t workspaceSize,
aclOpExecutor* executor,
aclrtStream stream)aclnnOneHotGetWorkspaceSize
Parameters:
Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor self (aclTensor*) Input Input tensor. - UINT8, INT32, INT64 ND 1-7 √ numClasses (int) Input Indicates the number of classes. - When self is an empty tensor, numClasses must be greater than 0;
- When self is not an empty tensor, numClasses must be greater than or equal to 0.
- If numClasses is 0, an empty tensor is returned.
- If self contains elements greater than numClasses, these elements are encoded as all offValue.
INT64 - - - onValue (aclTensor*) Input Represents the fill value at the index position, which is onValue in the formula. - Only the value of the first element is used in the computation.
- The data type must be the same as that of out.
FLOAT16, FLOAT, INT8, UINT8, INT32, INT64 ND 1-8 √ offValue (aclTensor*) Input Represents the fill value at non-index positions, that is, the offValue in the formula. - Only the first element value is used for computation.
- The data type is the same as that of out.
FLOAT16, FLOAT, INT8, UINT8, INT32, INT64 ND 1-8 √ axis (int64_t) Input Represents the dimension along which the encoding vector is inserted. - Minimum value: -1. Maximum value: the number of dimensions of self.
- If the value is -1, the encoding vector is inserted along the last dimension of self.
INT64 - - - out (aclTensor*) Output Represents the one-hot tensor, that is, output out in the formula. The output has one more dimension than self. The output shape is the same as the shape of self after numClasses is inserted along the axis dimension. FLOAT16, FLOAT, INT8, UINT8, INT32, INT64 ND 1-8 √ workspaceSize (uint64_t*) Output Returns the size of the workspace to be allocated on the Device. - - - - - executor (aclOpExecutor**) Output Returns the op executor, which contains the operator computation flow. - - - - - Atlas A2 products andAtlas A3 products : INT8 and UINT8 data types are not supported.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, onValue, offValue, or out is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data type of self, onValue, offValue, or out is not supported. The data type of onValue or offValue does not match the data type of out. self is an empty Tensor and numClasses is less than or equal to 0. self is not an empty Tensor and numClasses is less than 0. The value of axis is less than -1. The value of axis is greater than the number of dimensions of self. The dimensions of out are not exactly one more than the dimensions of self. The shape of out is inconsistent with the shape of self after numClasses is inserted along the axis dimension. The dimension of self, onValue, offValue, or out exceeds 8.
aclnnOneHot
Parameters:
Parameter Input/Output Description workspace Input Memory address of the workspace allocated on the device. workspaceSize Input Size of the workspace allocated on the device, obtained by the first-phase API aclnnOneHotGetWorkspaceSize. executor Input Op executor, containing the operator computation flow. stream Input Stream for task execution. Return value
aclnnStatus: return status code. For details, see aclnn Return Codes.
Constraints
- Deterministic computation:
- aclnnOneHot defaults to a deterministic implementation.
Example
The following is sample code 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_one_hot.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 shape_size = 1;
for (auto i : shape) {
shape_size *= i;
}
return shape_size;
}
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 allocate memory on the device.
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 data from the host 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 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 the device/stream. For details, see the acl public API list.
// Set deviceId based on the actual device.
int32_t deviceId = 0;
aclrtStream stream;
auto ret = Init(deviceId, &stream);
// Handle the check as required.
CHECK_RET(ret == 0, 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};
int numClasses = 4;
std::vector<int64_t> outShape = {4, 2, 4};
std::vector<int64_t> onValueShape = {1};
std::vector<int64_t> offValueShape = {1};
void* selfDeviceAddr = nullptr;
void* outDeviceAddr = nullptr;
void* onValueDeviceAddr = nullptr;
void* offValueDeviceAddr = nullptr;
aclTensor* self = nullptr;
aclTensor* out = nullptr;
aclTensor* onValue = nullptr;
aclTensor* offValue = nullptr;
std::vector<int32_t> selfHostData = {0, 1, 2, 3, 3, 2, 1, 0};
std::vector<int32_t> outHostData = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
std::vector<int32_t> onValueHostData = {1};
std::vector<int32_t> offValueHostData = {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);
// Create the onValue aclTensor.
ret = CreateAclTensor(onValueHostData, onValueShape, &onValueDeviceAddr, aclDataType::ACL_INT32, &onValue);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the offValue aclTensor.
ret = CreateAclTensor(offValueHostData, offValueShape, &offValueDeviceAddr, aclDataType::ACL_INT32, &offValue);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// 3. Call the CANN operator library API. Replace it with the specific API.
uint64_t workspaceSize = 0;
int64_t axis = -1;
aclOpExecutor* executor;
// Call the first-phase API of aclnnOneHot.
ret = aclnnOneHotGetWorkspaceSize(self, numClasses, onValue, offValue, axis, out, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnOneHotGetWorkspaceSize 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 aclnnOneHot.
ret = aclnnOneHot(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnOneHot 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 values and copy the result from device memory to host memory. Modify the code based on the specific API definition.
auto size = GetShapeSize(outShape);
std::vector<int32_t> resultData(size, 0);
ret = aclrtMemcpy(
resultData.data(), resultData.size() * sizeof(resultData[0]), outDeviceAddr, size * sizeof(int32_t),
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]);
}
// 6. Release the aclTensor and aclScalar. Modify the code based on the specific API definition.
aclDestroyTensor(self);
aclDestroyTensor(onValue);
aclDestroyTensor(offValue);
aclDestroyTensor(out);
// 7. Release device resources. Modify the code based on the specific API definition.
aclrtFree(selfDeviceAddr);
aclrtFree(onValueDeviceAddr);
aclrtFree(offValueDeviceAddr);
aclrtFree(outDeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}