Skip to main content

aclnnInplaceReciprocal

aclnnInplacePut

接口原型

每个算子有两段接口,必须先调用“aclnnXxxGetWorkspaceSize”接口获取入参并根据计算流程计算所需workspace大小,再调用“aclnnXxx”接口执行计算。两段式接口如下:

  • 第一段接口:aclnnStatus aclnnInplacePutGetWorkspaceSize(const aclTensor *selfRef, const aclTensor *index, const aclTensor *source, bool accumulate, uint64_t *workspaceSize, aclOpExecutor **executor)
  • 第二段接口:aclnnStatus aclnnInplacePut(void *workspace, uint64_t workspaceSize, aclOpExecutor *executor, aclrtStream stream)

功能描述

算子功能:将selfRef视为一维张量,把index张量中元素值作为索引,如果accumulate为True,将张量source中元素和selfRef对应位置上的元素做累加操作;否则,将张量source中元素替换为selfRef对应位置上的元素。

aclnnInplacePutGetWorkspaceSize

  • 接口定义:

    aclnnStatus aclnnInplacePutGetWorkspaceSize(const aclTensor *selfRef, const aclTensor *index, const aclTensor *source, bool accumulate, uint64_t *workspaceSize, aclOpExecutor **executor)

  • 参数说明:

    • selfRef:Device侧的aclTensor,支持非连续的Tensor,数据格式支持ND,数据类型支持情况如下:
      • 当accumulate为True时,支持FLOAT16、FLOAT、INT32、INT8、UINT8。
      • 当accumulate为False时,支持BOOL、FLOAT、FLOAT16、DOUBLE、INT8、INT16、INT32、INT64、UINT8、COMPLEX64、COMPLEX128。
    • index:Device侧的aclTensor,数据类型支持INT32、INT64,元素个数要求和source保持一致。支持非连续的Tensor,数据格式支持ND。
    • source:Device侧的aclTensor,数据类型和selfRef一致,元素个数和index一致。支持非连续的Tensor,数据格式支持ND。
    • accumulate:Host侧的布尔类型,用于设定是否进行累加操作。
    • workspaceSize:返回用户需要在Device侧申请的workspace大小。
    • executor:返回op执行器,包含了算子计算流程。
  • 返回值:

    返回aclnnStatus状态码,具体参见aclnn返回码

    :::note 说明 第一段接口完成入参校验,出现以下场景时报错:

    • 返回161001(ACLNN_ERR_PARAM_NULLPTR):传入的selfRef、index、source空指针。
    • 返回161002(ACLNN_ERR_PARAM_INVALID):
      • selfRef和index的数据类型不在支持的范围内。
      • selfRef和source的数据类型不同。
      • source和index的元素数量不等。
      • selfRef是空tensor,index不是空tensor。 :::

aclnnInplacePut

  • 接口定义:

    aclnnStatus aclnnInplacePut(void *workspace, uint64_t workspaceSize, aclOpExecutor *executor, aclrtStream stream)

  • 参数说明:

    • workspace:在Device侧申请的workspace内存起址。
    • workspaceSize:在Device侧申请的workspace大小,由第一段接口aclnnInplacePutGetWorkspaceSize获取。
    • executor:op执行器,包含了算子计算流程。
    • stream:指定执行任务的AscendCL stream流。
  • 返回值:

    返回aclnnStatus状态码,具体参见aclnn返回码

调用示例

#include <iostream>
#include <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_put.h"

#define CHECK_RET(cond, return_expr) \
do &#123; \
if (!(cond)) &#123; \
return_expr; \
&#125; \
&#125; while (0)

#define LOG_PRINT(message, ...) \
do &#123; \
printf(message, ##__VA_ARGS__); \
&#125; while (0)

int64_t GetShapeSize(const std::vector<int64_t>& shape) &#123;
int64_t shape_size = 1;
for (auto i : shape) &#123;
shape_size *= i;
&#125;
return shape_size;
&#125;

int Init(int32_t deviceId, aclrtContext* context, aclrtStream* stream) &#123;
// 固定写法,AscendCL初始化
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 = aclrtCreateContext(context, deviceId);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtCreateContext failed. ERROR: %d\n", ret); return ret);
ret = aclrtSetCurrentContext(*context);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSetCurrentContext 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;
&#125;

template <typename T>
int CreateAclTensor(const std::vector<T>& hostData, const std::vector<int64_t>& shape, void** deviceAddr,
aclDataType dataType, aclTensor** tensor) &#123;
auto size = GetShapeSize(shape) * sizeof(T);
// 调用aclrtMalloc申请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);

// 调用aclrtMemcpy将Host侧数据拷贝到device侧内存上
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);

// 计算连续tensor的strides
std::vector<int64_t> strides(shape.size(), 1);
for (int64_t i = shape.size() - 2; i >= 0; i--) &#123;
strides[i] = shape[i + 1] * strides[i + 1];
&#125;

// 调用aclCreateTensor接口创建aclTensor
*tensor = aclCreateTensor(shape.data(), shape.size(), dataType, strides.data(), 0, aclFormat::ACL_FORMAT_ND,
shape.data(), shape.size(), *deviceAddr);
return 0;
&#125;

int main() &#123;
// 1. (固定写法)device/context/stream初始化, 参考AscendCL对外接口列表
// 根据自己的实际device填写deviceId
int32_t deviceId = 0;
aclrtContext context;
aclrtStream stream;
auto ret = Init(deviceId, &context, &stream);
// check根据自己的需要处理
CHECK_RET(ret == 0, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);

// 2. 构造输入与输出,需要根据API的接口自定义构造
std::vector<int64_t> selfShape = &#123;4, 2&#125;;
std::vector<int64_t> indexShape = &#123;4, 2&#125;;
std::vector<int64_t> sourceShape = &#123;4, 2&#125;;
void* selfDeviceAddr = nullptr;
void* indexDeviceAddr = nullptr;
void* sourceDeviceAddr = nullptr;
aclTensor* self = nullptr;
aclTensor* index = nullptr;
aclTensor* source = nullptr;
std::vector<float> selfHostData = &#123;0, 0,0,0,0,0,0,0&#125;;
std::vector<int64_t> indexHostData = &#123;0,1,2,3,4,5,6,7&#125;;
std::vector<float> sourceHostData = &#123;10, 10, 10, 10, 10, 10, 10, 10&#125;;
// 创建self aclTensor
ret = CreateAclTensor(selfHostData, selfShape, &selfDeviceAddr, aclDataType::ACL_INT32, &self);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// 创建index aclTensor
ret = CreateAclTensor(indexHostData, indexShape, &indexDeviceAddr, aclDataType::ACL_INT64, &index);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// 创建source aclTensor
ret = CreateAclTensor(sourceHostData, sourceShape, &sourceDeviceAddr, aclDataType::ACL_INT32, &source);
CHECK_RET(ret == ACL_SUCCESS, return ret);

// 3.调用CANN算子库API,需要修改为具体的算子接口
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
// 调用aclnnInplacePut第一段接口
ret = aclnnInplacePutGetWorkspaceSize(self, index, source,false, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplacePutGetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
// 根据第一段接口计算出的workspaceSize申请device内存
void* workspaceAddr = nullptr;
if (workspaceSize > 0) &#123;
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;);
&#125;
// 调用aclnnInplacePut第二段接口
ret = aclnnInplacePut(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplacePut failed. ERROR: %d\n", ret); return ret);

// 4. (固定写法)同步等待任务执行结束
ret = aclrtSynchronizeStream(stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret);

// 5. 获取输出的值,将device侧内存上的结果拷贝至Host侧,需要根据具体API的接口定义修改
auto size = GetShapeSize(selfShape);
std::vector<float> resultData(size, 0);
ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), selfDeviceAddr, size * sizeof(float),
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++) &#123;
LOG_PRINT("result[%ld] is: %f\n", i, resultData[i]);
&#125;

// 6. 释放aclTensor和aclScalar,需要根据具体API的接口定义修改
aclDestroyTensor(self);
aclDestroyTensor(index);
aclDestroyTensor(source);
return 0;
&#125;

父主题: NN类算子接口

aclnnInplaceRandom

接口原型

每个算子有两段接口,必须先调用“aclnnXxxGetWorkspaceSize”接口获取入参并根据计算流程计算所需workspace大小,再调用“aclnnXxx”接口执行计算。两段式接口如下:

  • 第一段接口:aclnnStatus aclnnInplaceRandomGetWorkspaceSize(const aclTensor *selfRef, int64_t from, int64_t to, int64_t seed, int64_t offset, uint64_t *workspaceSize, aclOpExecutor **executor)
  • 第二段接口:aclnnStatus aclnnInplaceRandom(void *workspace, uint64_t workspaceSize, aclOpExecutor *executor, const aclrtStream stream)

功能描述

算子功能:从[from, to-1]的离散均匀分布中,按种子(seed)随机采样数值填充selfRef张量。

aclnnInplaceRandomGetWorkspaceSize

  • 接口定义:

    aclnnStatus aclnnInplaceRandomGetWorkspaceSize(const aclTensor *selfRef, int64_t from, int64_t to, int64_t seed, int64_t offset, uint64_t *workspaceSize, aclOpExecutor **executor)

  • 参数说明:

    • selfRef:Device侧的aclTensor,数据类型支持FLOAT、FLOAT16、DOUBLE、INT32、INT64、INT16、INT8、UINT8、BOOL。支持非连续的Tensor,数据格式支持ND。
    • from:Host侧的整形,离散均匀分布取值的左边界,from值需要在selfRef的数据类型取值范围内。
    • to:Host侧的整形,离散均匀分布取值的右边界,to值需要在selfRef的数据类型取值范围内。
    • seed:随机数生成器的种子,它影响生成的随机数序列。
    • offset:随机数生成器的偏移量,它影响生成的随机数序列的位置。设置偏移量后,生成的随机数序列会从指定位置开始。
    • workspaceSize:返回用户需要在Device侧申请的workspace大小。
    • executor:返回op执行器,包含了算子计算流程。
  • 返回值:

    返回aclnnStatus状态码,具体参见aclnn返回码

    :::note 说明 第一段接口完成入参校验,出现以下场景时报错:

    • 返回161001(ACLNN_ERR_PARAM_NULLPTR):传入的selfRef是空指针。
    • 返回161002(ACLNN_ERR_PARAM_INVALID):
      • selfRef的数据类型和数据格式不在支持的范围内。
      • 参数from取值≥to取值。
      • from或者(to-1)取值超出selfRef数据类型的取值范围。 :::

aclnnInplaceRandom

  • 接口定义:

    aclnnStatus aclnnInplaceRandom(void *workspace, uint64_t workspaceSize, aclOpExecutor *executor, const aclrtStream stream)

  • 参数说明:

    • workspace:在Device侧申请的workspace内存起址。
    • workspaceSize:在Device侧申请的workspace大小,由第一段接口aclnnInplaceRandomGetWorkspaceSize获取。
    • executor:op执行器,包含了算子计算流程。
    • stream:指定执行任务的AscendCL stream流。
  • 返回值:

    返回aclnnStatus状态码,具体参见aclnn返回码

调用示例

#include <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_random.h"

#define CHECK_RET(cond, return_expr) \
do &#123; \
if (!(cond)) &#123; \
return_expr; \
&#125; \
&#125; while (0)

#define LOG_PRINT(message, ...) \
do &#123; \
printf(message, ##__VA_ARGS__); \
&#125; while (0)

int64_t GetShapeSize(const std::vector<int64_t>& shape) &#123;
int64_t shapeSize = 1;
for (auto i : shape) &#123;
shapeSize *= i;
&#125;
return shapeSize;
&#125;

int Init(int32_t deviceId, aclrtContext* context, aclrtStream* stream) &#123;
// 固定写法,AscendCL初始化
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 = aclrtCreateContext(context, deviceId);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtCreateContext failed. ERROR: %d\n", ret); return ret);
ret = aclrtSetCurrentContext(*context);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSetCurrentContext 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;
&#125;

template <typename T>
int CreateAclTensor(const std::vector<T>& hostData, const std::vector<int64_t>& shape, void** deviceAddr,
aclDataType dataType, aclTensor** tensor) &#123;
auto size = GetShapeSize(shape) * sizeof(T);
// 调用aclrtMalloc申请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);
// 调用aclrtMemcpy将Host侧数据拷贝到device侧内存上
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);

// 计算连续tensor的strides
std::vector<int64_t> strides(shape.size(), 1);
for (int64_t i = shape.size() - 2; i >= 0; i--) &#123;
strides[i] = shape[i + 1] * strides[i + 1];
&#125;

// 调用aclCreateTensor接口创建aclTensor
*tensor = aclCreateTensor(shape.data(), shape.size(), dataType, strides.data(), 0, aclFormat::ACL_FORMAT_ND,
shape.data(), shape.size(), *deviceAddr);
return 0;
&#125;

int main() &#123;
// 1. (固定写法)device/context/stream初始化,参考AscendCL对外接口列表
// 根据自己的实际device填写deviceId
int32_t deviceId = 0;
aclrtContext context;
aclrtStream stream;
auto ret = Init(deviceId, &context, &stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);

// 2. 构造输入与输出,需要根据API的接口自定义构造
std::vector<int64_t> selfShape = &#123;4, 2&#125;;
void* selfDeviceAddr = nullptr;
aclTensor* self = nullptr;
std::vector<float> selfHostData = &#123;1,2,3,4,5,6,7,8&#125;;
int64_t from = 0;
int64_t to = 10;
int64_t seed = 1234;
int64_t offset = 0;
// 创建self aclTensor
ret = CreateAclTensor(selfHostData, selfShape, &selfDeviceAddr, aclDataType::ACL_FLOAT, &self);
CHECK_RET(ret == ACL_SUCCESS, return ret);

// 3. 调用CANN算子库API,需要修改为具体的API名称
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
// 调用aclnnInplaceRandom第一段接口
ret = aclnnInplaceRandomGetWorkspaceSize(self, from, to, seed, offset, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplaceRandomGetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
// 根据第一段接口计算出的workspaceSize申请device内存
void* workspaceAddr = nullptr;
if (workspaceSize > 0) &#123;
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);
&#125;
// 调用aclnnInplaceRandom第二段接口
ret = aclnnInplaceRandom(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplaceRandom failed. ERROR: %d\n", ret); return ret);

// 4. (固定写法)同步等待任务执行结束
ret = aclrtSynchronizeStream(stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret);

// 5. 获取输出的值,将device侧内存上的结果拷贝至Host侧,需要根据具体API的接口定义修改
auto size = GetShapeSize(selfShape);
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++) &#123;
LOG_PRINT("result[%ld] is: %f\n", i, resultData[i]);
&#125;

// 6. 释放aclTensor和aclScalar,需要根据具体API的接口定义修改
aclDestroyTensor(self);
return 0;
&#125;

父主题: NN类算子接口

aclnnInplaceReciprocal

接口原型

每个算子有两段接口,必须先调用“aclnnXxxGetWorkspaceSize”接口获取入参并根据计算流程计算所需workspace大小,再调用“aclnnXxx”接口执行计算。两段式接口如下:

  • 第一段接口:aclnnStatus aclnnInplaceReciprocalGetWorkspaceSize(const aclTensor *selfRef, uint64_t *workspaceSize, aclOpExecutor **executor)
  • 第二段接口:aclnnStatus aclnnInplaceReciprocal(void *workspace, uint64_t workspaceSize, aclOpExecutor *executor, aclrtStream stream)

功能描述

  • 算子功能:计算张量每个元素的倒数,并返回一个新张量。

  • 计算公式:

  • 示例:

    x = tensor([1.00, 2.00, 3.00, 4.00])
    // 经过reciprocal计算后
    x = tensor([1.00, 0.50, 0.33, 0.25])

aclnnInplaceReciprocalGetWorkspaceSize

  • 接口定义:

    aclnnStatus aclnnInplaceReciprocalGetWorkspaceSize(const aclTensor *selfRef, uint64_t *workspaceSize, aclOpExecutor **executor)

  • 参数说明:

    • selfRef:Device侧的aclTensor,数据类型支持FLOAT16、FLOAT32、BFLOAT16(仅Atlas A2训练系列产品支持)。支持非连续的Tensor,支持空Tensor传入,数据格式支持ND。
    • workspaceSize:返回用户需要在Device侧申请的workspace大小。
    • executor:返回op执行器,包含了算子计算流程。
  • 返回值:

    返回aclnnStatus状态码,具体参见aclnn返回码

    :::note 说明 第一段接口完成入参校验,出现以下场景时报错:

    • 返回161001(ACLNN_ERR_PARAM_NULLPTR):传入的selfRef是空指针。
    • 返回161002(ACLNN_ERR_PARAM_INVALID):
      • selfRef的数据类型和数据格式不在支持的范围内。
      • selfRef的维度超过8维。 :::

aclnnInplaceReciprocal

  • 接口定义:

    aclnnStatus aclnnInplaceReciprocal(void *workspace, uint64_t workspaceSize, aclOpExecutor *executor, aclrtStream stream)

  • 参数说明:

    • workspace:在Device侧申请的workspace内存起址。
    • workspaceSize:在Device侧申请的workspace大小,由第一段接口aclnnInplaceReciprocalGetWorkspaceSize获取。
    • executor:op执行器,包含了算子计算流程。
    • stream:指定执行任务的AscendCL stream流。
  • 返回值:

    返回aclnnStatus状态码,具体参见aclnn返回码

调用示例

#include <iostream>
#include <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_reciprocal.h"

#define CHECK_RET(cond, return_expr) \
do &#123; \
if (!(cond)) &#123; \
return_expr; \
&#125; \
&#125; while (0)

#define LOG_PRINT(message, ...) \
do &#123; \
printf(message, ##__VA_ARGS__); \
&#125; while (0)

int64_t GetShapeSize(const std::vector<int64_t>& shape) &#123;
int64_t shapeSize = 1;
for (auto i : shape) &#123;
shapeSize *= i;
&#125;
return shapeSize;
&#125;

int Init(int32_t deviceId, aclrtContext* context, aclrtStream* stream) &#123;
// 固定写法,AscendCL初始化
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 = aclrtCreateContext(context, deviceId);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtCreateContext failed. ERROR: %d\n", ret); return ret);
ret = aclrtSetCurrentContext(*context);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSetCurrentContext 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;
&#125;

template <typename T>
int CreateAclTensor(const std::vector<T>& hostData, const std::vector<int64_t>& shape, void** deviceAddr,
aclDataType dataType, aclTensor** tensor) &#123;
auto size = GetShapeSize(shape) * sizeof(T);
// 调用aclrtMalloc申请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);
// 调用aclrtMemcpy将Host侧数据拷贝到device侧内存上
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);

// 计算连续tensor的strides
std::vector<int64_t> strides(shape.size(), 1);
for (int64_t i = shape.size() - 2; i >= 0; i--) &#123;
strides[i] = shape[i + 1] * strides[i + 1];
&#125;

// 调用aclCreateTensor接口创建aclTensor
*tensor = aclCreateTensor(shape.data(), shape.size(), dataType, strides.data(), 0, aclFormat::ACL_FORMAT_ND,
shape.data(), shape.size(), *deviceAddr);
return 0;
&#125;

int main() &#123;
// 1. (固定写法)device/context/stream初始化,参考AscendCL对外接口列表
// 根据自己的实际device填写deviceId
int32_t deviceId = 0;
aclrtContext context;
aclrtStream stream;
auto ret = Init(deviceId, &context, &stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);

// 2. 构造输入与输出,需要根据API的接口自定义构造
std::vector<int64_t> selfRefShape = &#123;1, 2, 4&#125;;
void* selfRefDeviceAddr = nullptr;
aclTensor* selfRef = nullptr;
std::vector<float> selfRefHostData = &#123;0, 1, 2, 3, 4, 5, 6, 7&#125;;
// 创建selfRef aclTensor
ret = CreateAclTensor(selfRefHostData, selfRefShape, &selfRefDeviceAddr, aclDataType::ACL_FLOAT, &selfRef);
CHECK_RET(ret == ACL_SUCCESS, return ret);

// 3. 调用CANN算子库API,需要修改为具体的API名称
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
// 调用aclnnInplaceReciprocal第一段接口
ret = aclnnInplaceReciprocalGetWorkspaceSize(selfRef, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplaceReciprocalGetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
// 根据第一段接口计算出的workspaceSize申请device内存
void* workspaceAddr = nullptr;
if (workspaceSize > 0) &#123;
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);
&#125;
// 调用aclnnInplaceReciprocal第二段接口
ret = aclnnInplaceReciprocal(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplaceReciprocal failed. ERROR: %d\n", ret); return ret);

// 4. (固定写法)同步等待任务执行结束
ret = aclrtSynchronizeStream(stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret);

// 5. 获取输出的值,将device侧内存上的结果拷贝至Host侧,需要根据具体API的接口定义修改
auto size = GetShapeSize(selfRefShape);
std::vector<float> resultData(size, 0);
ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), selfRefDeviceAddr,
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++) &#123;
LOG_PRINT("result[%ld] is: %f\n", i, resultData[i]);
&#125;

// 6. 释放aclTensor和aclScalar,需要根据具体API的接口定义修改
aclDestroyTensor(selfRef);
return 0;
&#125;

父主题: NN类算子接口

aclnnInplaceRemainderTensorScalar

接口原型

每个算子有两段接口,必须先调用“aclnnXxxGetWorkspaceSize”接口获取入参并根据计算流程计算所需workspace大小,再调用“aclnnXxx”接口执行计算。两段式接口如下:

  • **第一段接口:**aclnnStatus aclnnInplaceRemainderTensorScalarGetWorkspaceSize(aclTensor *selfRef, const aclScalar *other, uint64_t *workspaceSize, aclOpExecutor **executor)
  • **第二段接口:**aclnnStatus aclnnInplaceRemainderTensorScalar(void *workspace, uint64_t workspaceSize, aclOpExecutor *executor, aclrtStream stream)

功能描述

  • 算子功能:将selfRef(张量)中每个元素都转换为除以other(标量)后得到的余数。该结果与除数other同符号,并且该结果的绝对值是小于other的绝对值。

  • 计算公式:

  • 示例:

    selfRef = tensor([[-1, -2], [-3, -4]]).type(torch.int32)
    other = 3.5 # float
    selfRef = remainder(selfRef, other)

    # selfRef的值
    # tensor([[2.5000, 1.5000],
    # [0.5000, 3.0000]])

    # 对于元素selfRef中的-1来说,计算结果为 -1 - floor(-1 / 3.5) * 3.5 = 2.5
    # 可以看到,最终结果2.5的绝对值小于other 3.5。

aclnnInplaceRemainderTensorScalarGetWorkspaceSize

  • 接口定义:

    aclnnStatus aclnnInplaceRemainderTensorScalarGetWorkspaceSize(aclTensor *selfRef, const aclScalar *other, uint64_t *workspaceSize, aclOpExecutor **executor)

  • 参数说明:

    • selfRef:Device侧的aclTensor,输入/输出张量,数据类型与other的数据类型需满足数据类型推导规则,推导后的数据类型支持INT32、INT64、FLOAT16、FLOAT、DOUBLE,该数据类型必须能转换为selfRef的数据类型。支持非连续的Tensor,数据格式支持ND。
    • other:Host侧的aclScalar,数据类型与selfRef的数据类型需满足数据类型推导规则,且推导出的数据类型必须能转换为selfRef的数据类型。
    • workspaceSize:返回用户需要在Device侧申请的workspace大小。
    • executor:返回op执行器,包含了算子计算流程。
  • 返回值:

    返回aclnnStatus状态码,具体参见aclnn返回码

    :::note 说明 第一段接口完成入参校验,出现以下场景时报错:

    • 返回161001(ACLNN_ERR_PARAM_NULLPTR):传入的selfRef、other是空指针。
    • 返回161002(ACLNN_ERR_PARAM_INVALID):
      • selfRef与other不能推导出数据类型。
      • selfRef与other推导出的数据类型不属于支持的数据类型。
      • selfRef和other推导出的数据类型无法转换为selfRef的类型。
      • selfRef的维度数大于8维。 :::

aclnnInplaceRemainderTensorScalar

  • 接口定义:

    aclnnStatus aclnnInplaceRemainderTensorScalar(void *workspace, uint64_t workspaceSize, aclOpExecutor *executor, aclrtStream stream)

  • 参数说明:

    • workspace:在Device侧申请的workspace内存起址。
    • workspaceSize:在Device侧申请的workspace大小,由第一段接口aclnnInplaceRemainderTensorScalarGetWorkspaceSize获取。
    • executor:op执行器,包含了算子计算流程。
    • stream:指定执行任务的AscendCL stream流。
  • 返回值:

    返回aclnnStatus状态码,具体参见aclnn返回码

调用示例

#include <iostream>
#include <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_remainder.h"

#define CHECK_RET(cond, return_expr) \
do &#123; \
if (!(cond)) &#123; \
return_expr; \
&#125; \
&#125; while (0)

#define LOG_PRINT(message, ...) \
do &#123; \
printf(message, ##__VA_ARGS__); \
&#125; while (0)

int64_t GetShapeSize(const std::vector<int64_t>& shape) &#123;
int64_t shapeSize = 1;
for (auto i : shape) &#123;
shapeSize *= i;
&#125;
return shapeSize;
&#125;

int Init(int32_t deviceId, aclrtContext* context, aclrtStream* stream) &#123;
// 固定写法,AscendCL初始化
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 = aclrtCreateContext(context, deviceId);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtCreateContext failed. ERROR: %d\n", ret); return ret);
ret = aclrtSetCurrentContext(*context);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSetCurrentContext 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;
&#125;

template <typename T>
int CreateAclTensor(const std::vector<T>& hostData, const std::vector<int64_t>& shape, void** deviceAddr,
aclDataType dataType, aclTensor** tensor) &#123;
auto size = GetShapeSize(shape) * sizeof(T);
// 调用aclrtMalloc申请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);
// 调用aclrtMemcpy将Host侧数据拷贝到device侧内存上
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);

// 计算连续tensor的strides
std::vector<int64_t> strides(shape.size(), 1);
for (int64_t i = shape.size() - 2; i >= 0; i--) &#123;
strides[i] = shape[i + 1] * strides[i + 1];
&#125;

// 调用aclCreateTensor接口创建aclTensor
*tensor = aclCreateTensor(shape.data(), shape.size(), dataType, strides.data(), 0, aclFormat::ACL_FORMAT_ND,
shape.data(), shape.size(), *deviceAddr);
return 0;
&#125;

int main() &#123;
// 1. (固定写法)device/context/stream初始化,参考AscendCL对外接口列表
// 根据自己的实际device填写deviceId
int32_t deviceId = 0;
aclrtContext context;
aclrtStream stream;
auto ret = Init(deviceId, &context, &stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);

// 2. 构造输入与输出,需要根据API的接口自定义构造
std::vector<int64_t> selfRefShape = &#123;3, 3&#125;;
void* selfRefDeviceAddr = nullptr;
aclTensor* selfRef = nullptr;
aclScalar* other = nullptr;
std::vector<int64_t> selfRefHostData = &#123;0, 1, 2, 3, 4, 5, 6, 7, 8&#125;;
int64_t Other = 3;

// 创建self aclTensor
ret = CreateAclTensor(selfRefHostData, selfRefShape, &selfRefDeviceAddr, aclDataType::ACL_INT64, &selfRef);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// 创建other aclScalar
other = aclCreateScalar(&Other, aclDataType::ACL_INT64);
CHECK_RET(other != nullptr, return ret);

// 3. 调用CANN算子库API,需要修改为具体的API名称
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
// 调用aclnnInplaceRemainderTensorScalar第一段接口
ret = aclnnInplaceRemainderTensorScalarGetWorkspaceSize(selfRef, other, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplaceRemainderTensorScalarGetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
// 根据第一段接口计算出的workspaceSize申请device内存
void* workspaceAddr = nullptr;
if (workspaceSize > 0) &#123;
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);
&#125;
// 调用aclnnInplaceRemainderTensorScalar第二段接口
ret = aclnnInplaceRemainderTensorScalar(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplaceRemainderTensorScalar failed. ERROR: %d\n", ret); return ret);

// 4. (固定写法)同步等待任务执行结束
ret = aclrtSynchronizeStream(stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret);

// 5. 获取输出的值,将device侧内存上的结果拷贝至Host侧,需要根据具体API的接口定义修改
auto size = GetShapeSize(selfRefShape);
std::vector<int64_t> resultData(size, 0);
ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), selfRefDeviceAddr,
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++) &#123;
LOG_PRINT("result[%ld] is: %ld\n", i, resultData[i]);
&#125;

// 6. 释放aclTensor和aclScalar,需要根据具体API的接口定义修改
aclDestroyTensor(selfRef);
aclDestroyScalar(other);
return 0;
&#125;

父主题: NN类算子接口

aclnnInplaceRemainderTensorTensor

接口原型

每个算子有两段接口,必须先调用“aclnnXxxGetWorkspaceSize”接口获取入参并根据计算流程计算所需workspace大小,再调用“aclnnXxx”接口执行计算。两段式接口如下:

  • **第一段接口:**aclnnStatus aclnnInplaceRemainderTensorTensorGetWorkspaceSize(aclTensor* selfRef, const aclTensor *other, uint64_t *workspaceSize, aclOpExecutor **executor)
  • **第二段接口:**aclnnStatus aclnnInplaceRemainderTensorTensor(void *workspace, uint64_t workspaceSize, aclOpExecutor *executor, aclrtStream stream)

功能描述

  • 算子功能:将selfRef(张量)broadcast成和other(张量)一样的shape后,将其每个元素都转换为除以other对应元素后得到的余数。该结果与除数other同符号,并且该结果的绝对值是小于other的绝对值。

  • 计算公式:

  • 示例:

    selfRef = tensor([[-1, -2], [-3, -4]]).type(torch.float16)
    other = tensor([-3, -3]).type(torch.int64)
    selfRef = remainder(selfRef, other)

    # selfRef的值
    # tensor([[-1., -2.],
    # [-0., -1.]], dtype=torch.float16)

    # 首先是将other broadcast成和selfRef一致的shape,成为 [[-3, -3], [-3, -3]],然后再进行计算。
    # 对于元素selfRef中的-3来说,计算结果为 (-3) % (-3) = 0
    # 可以看到,最终结果0的绝对值小于原来的-3的绝对值。

aclnnInplaceRemainderTensorTensorGetWorkspaceSize

  • 接口定义:

    aclnnStatus aclnnInplaceRemainderTensorTensorGetWorkspaceSize(aclTensor* selfRef, const aclTensor *other, uint64_t *workspaceSize, aclOpExecutor **executor)

  • 参数说明:

    • selfRef:Device侧的aclTensor,数据类型与other的数据类型需满足数据类型推导规则,且推导出的数据类型支持INT32、INT64、FLOAT16、FLOAT、DOUBLE,且需要是推导之后可转换为selfRef的数据类型。shape需要与other满足broadcast关系,且shape与最终broadcast后的shape一致。支持非连续的Tensor,数据格式支持ND。
    • other:Device侧的aclTensor, 数据类型与selfRef的数据类型需满足数据类型推导规则,且推导出的数据类型支持INT32、INT64、FLOAT16、FLOAT、DOUBLE。shape需要与selfRef满足broadcast关系,支持非连续的Tensor,数据格式支持ND。
    • workspaceSize:返回用户需要在Device侧申请的workspace大小。
    • executor:返回op执行器,包含了算子计算流程。
  • 返回值:

    返回aclnnStatus状态码,具体参见aclnn返回码

    :::note 说明 第一段接口完成入参校验,出现以下场景时报错:

    • 返回161001(ACLNN_ERR_PARAM_NULLPTR):传入的selfRef、other是空指针。
    • 返回161002(ACLNN_ERR_PARAM_INVALID):
      • selfRef和other无法做数据类型推导。
      • selfRef和other推导出的数据类型不属于支持的数据类型。
      • selfRef和other推导出的数据类型无法转换为selfRef的类型。
      • selfRef和other的shape无法做broadcast。
      • selfRef和other broadcast以后的shape与selfRef的shape不一致。
      • selfRef、other的维度数大于8维。 :::

aclnnInplaceRemainderTensorTensor

  • 接口定义:

    aclnnStatus aclnnInplaceRemainderTensorTensor(void *workspace, uint64_t workspaceSize, aclOpExecutor *executor, aclrtStream stream)

  • 参数说明:

    • workspace:在Device侧申请的workspace内存起址。
    • workspaceSize:在Device侧申请的workspace大小,由第一段接口aclnnInplaceRemainderTensorTensorGetWorkspaceSize获取。
    • executor:op执行器,包含了算子计算流程。
    • stream:指定执行任务的AscendCL stream流。
  • 返回值:

    返回aclnnStatus状态码,具体参见aclnn返回码

调用示例

#include <iostream>
#include <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_remainder.h"

#define CHECK_RET(cond, return_expr) \
do &#123; \
if (!(cond)) &#123; \
return_expr; \
&#125; \
&#125; while (0)

#define LOG_PRINT(message, ...) \
do &#123; \
printf(message, ##__VA_ARGS__); \
&#125; while (0)

int64_t GetShapeSize(const std::vector<int64_t>& shape) &#123;
int64_t shapeSize = 1;
for (auto i : shape) &#123;
shapeSize *= i;
&#125;
return shapeSize;
&#125;

int Init(int32_t deviceId, aclrtContext* context, aclrtStream* stream) &#123;
// 固定写法,AscendCL初始化
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 = aclrtCreateContext(context, deviceId);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtCreateContext failed. ERROR: %d\n", ret); return ret);
ret = aclrtSetCurrentContext(*context);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSetCurrentContext 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;
&#125;

template <typename T>
int CreateAclTensor(const std::vector<T>& hostData, const std::vector<int64_t>& shape, void** deviceAddr,
aclDataType dataType, aclTensor** tensor) &#123;
auto size = GetShapeSize(shape) * sizeof(T);
// 调用aclrtMalloc申请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);
// 调用aclrtMemcpy将Host侧数据拷贝到device侧内存上
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);

// 计算连续tensor的strides
std::vector<int64_t> strides(shape.size(), 1);
for (int64_t i = shape.size() - 2; i >= 0; i--) &#123;
strides[i] = shape[i + 1] * strides[i + 1];
&#125;

// 调用aclCreateTensor接口创建aclTensor
*tensor = aclCreateTensor(shape.data(), shape.size(), dataType, strides.data(), 0, aclFormat::ACL_FORMAT_ND,
shape.data(), shape.size(), *deviceAddr);
return 0;
&#125;

int main() &#123;
// 1. (固定写法)device/context/stream初始化,参考AscendCL对外接口列表
// 根据自己的实际device填写deviceId
int32_t deviceId = 0;
aclrtContext context;
aclrtStream stream;
auto ret = Init(deviceId, &context, &stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);

// 2. 构造输入与输出,需要根据API的接口自定义构造
std::vector<int64_t> selfRefShape = &#123;3, 3&#125;;
std::vector<int64_t> otherShape = &#123;3, 3&#125;;
void* selfRefDeviceAddr = nullptr;
void* otherDeviceAddr = nullptr;
aclTensor* selfRef = nullptr;
aclTensor* other = nullptr;
std::vector<int64_t> selfRefHostData = &#123;1, 2, 3, 4, 5, 6, 7, 8, 9&#125;;
std::vector<int64_t> otherHostData = &#123;0, 1, 2, 3, 4, 5, 6, 7, 8&#125;;

// 创建selfRef aclTensor
selfRef = CreateAclTensor(selfRefHostData, selfRefShape, &selfRefDeviceAddr, aclDataType::ACL_INT64, &selfRef);
CHECK_RET(selfRef != nullptr, return ret);
// 创建other aclTensor
ret = CreateAclTensor(otherHostData, otherShape, &otherDeviceAddr, aclDataType::ACL_INT64, &other);
CHECK_RET(ret == ACL_SUCCESS, return ret);

// 3. 调用CANN算子库API,需要修改为具体的API名称
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
// 调用aclnnInplaceRemainderTensorTensor第一段接口
ret = aclnnInplaceRemainderTensorTensorGetWorkspaceSize(selfRef, other, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplaceRemainderTensorTensorGetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
// 根据第一段接口计算出的workspaceSize申请device内存
void* workspaceAddr = nullptr;
if (workspaceSize > 0) &#123;
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);
&#125;
// 调用aclnnInplaceRemainderTensorTensor第二段接口
ret = aclnnInplaceRemainderTensorTensor(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplaceRemainderTensorTensor failed. ERROR: %d\n", ret); return ret);

// 4. (固定写法)同步等待任务执行结束
ret = aclrtSynchronizeStream(stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret);

// 5. 获取输出的值,将device侧内存上的结果拷贝至Host侧,需要根据具体API的接口定义修改
auto size = GetShapeSize(selfRefShape);
std::vector<int64_t> resultData(size, 0);
ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), selfRefDeviceAddr,
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++) &#123;
LOG_PRINT("result[%ld] is: %ld\n", i, resultData[i]);
&#125;

// 6. 释放aclTensor和aclScalar,需要根据具体API的接口定义修改
aclDestroyTensor(selfRef);
aclDestroyTensor(other);
return 0;
&#125;

父主题: NN类算子接口

在线提单