昇腾社区首页
中文
注册

aclnnIndexSelect

支持的产品型号

  • Atlas 推理系列产品。
  • Atlas 训练系列产品。
  • Atlas A2训练系列产品/Atlas 800I A2推理产品。

接口原型

每个算子分为,必须先调用“aclnnIndexSelectGetWorkspaceSize”接口获取计算所需workspace大小以及包含了算子计算流程的执行器,再调用“aclnnIndexSelect”接口执行计算。

  • aclnnStatus aclnnIndexSelectGetWorkspaceSize(const aclTensor *self, int64_t dim, const aclTensor *index, aclTensor *out, uint64_t *workspaceSize, aclOpExecutor **executor)
  • aclnnStatus aclnnIndexSelect(void *workspace, uint64_t workspaceSize, aclOpExecutor *executor, const aclrtStream stream)

功能描述

算子功能: 从输入Tensor的指定维度dim,按index中的下标序号提取元素,保存到out Tensor中。 例如,对于输入张量 x=[123456789]x=\begin{bmatrix}1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9\end{bmatrix} 和索引张量 idx=[1, 0], x.index_select(0, idx)的结果: y=[456123]y=\begin{bmatrix}4 & 5 & 6 \\ 1 & 2 & 3\end{bmatrix};

x.index_select(1, idx)的结果: y=[215487]y=\begin{bmatrix}2 & 1\\ 5 & 4\\8 & 7\end{bmatrix};

具体计算过程如下: 以三维张量为例,shape为(3,2,2)的张量 x =[[[1,2],[3,4]],[[5,6],[7,8]],[[9,10],[11,12]]]\begin{bmatrix}[[1,&2],&[3,&4]], \\ [[5,&6],&[7,&8]], \\ [[9,&10],&[11,&12]]\end{bmatrix} idx=[1, 0], x张量dim=0,1,2对应的下标分别是l,m,nl, m, n, idx是一维(零维的情况:当成是size为1的一维) dim为0, index_select(0, idx): I=index[i];    y[i][m][n][i][m][n] = x[I][m][n][I][m][n]

dim为1, index_select(1, idx): J=index[j];     y[l][j][n][l][j][n] = x[l][J][n][l][J][n]

dim为2, index_select(2, idx): K=index[k];   y[l][m][k][l][m][k] = x[l][m][K][l][m][K]

aclnnIndexSelectGetWorkspaceSize

  • 参数说明:

    • self(aclTensor*, 计算输入):Device侧的aclTensor,数据类型支持FLOAT、FLOAT16、BFLOAT16(仅Atlas A2训练系列产品/Atlas 800I A2推理产品支持)、INT64、INT32、INT16、INT8、UINT8、BOOL、DOUBLE、COMPLEX64、COMPLEX128,支持非连续的Tensor,支持ND。维度不大于8。
    • dim(int64_t, 计算输入):指定的维度,int64类型。
    • index(aclTensor*, 计算输入):索引,Device侧的aclTensor,数据类型支持INT64、INT32。支持非连续的Tensor,支持ND,且只能是0D或1D。
    • out(aclTensor*, 计算输出):输出Tensor,Device侧的aclTensor,数据类型同self。支持FLOAT、FLOAT16、BFLOAT16(仅Atlas A2训练系列产品/Atlas 800I A2推理产品支持)、INT64、INT32、INT16、INT8、UINT8、BOOL、DOUBLE、COMPLEX64、COMPLEX128。维数与self一致。除dim维长度等于index长度外,其他维长度与self相应维一致支持ND。
    • workspaceSize(uint64_t*, 出参):返回用户需要在Device侧申请的workspace大小。
    • executor(aclOpExecutor**, 出参):返回op执行器,包含了算子计算流程。
  • 返回值:

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

    [object Object]

aclnnIndexSelect

  • 参数说明:

    • workspace(void*, 入参):在Device侧申请的workspace内存地址。
    • workspaceSize(uint64_t, 入参):在Device侧申请的workspace大小,由第一段接口aclnnIndexSelectGetWorkspaceSize获取。
    • executor(aclOpExecutor*, 入参):op执行器,包含了算子计算流程。
    • stream(aclrtStream, 入参):指定执行任务的 AscendCL Stream流。
  • 返回值:

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

约束与限制

无。

调用示例

示例代码如下,仅供参考,具体编译和执行过程请参考

[object Object]