onnxruntime 中的 Gather 算子

上一篇文章中介绍了 Division by Invariant Integers using Multiplication 的原理,很多框架均才用该算法优化除法运算。onnxruntime 是已知实现中最为简洁的,因此本文结合 onnxruntime 的 Gather 实现进行介绍。 Gather 算子是一个索引类算子,kernel 中每个线程计算偏移时使用 fast_divmod 避免除法运算。

注意:ONNX 中的 Gather 功能与 numpy.take 相同,torch.index_select 是其简化版。而 ONNX 中的 GatherElements 与 torch.gather 和 paddle. take_along_axis 相对应。

Gather

Gather
CudaKernel
OpKernel
GatherBase

会话运行时,ExecuteKernel 函数会调用 OpKernel。
CudaKernel 是 CUDA kernel 的基类,提供了 CudaKernel::Compute 函数。
OpKernelInfo 是一个非常轻量级的类,它作为构建 Kernel 实例所需的所有数据的聚合视图。 注意:它不拥有/持有任何对象。

class Gather : public CudaKernel, public GatherBase {public:Gather(const OpKernelInfo& info) : CudaKernel(info), GatherBase(info) {}Status ComputeInternal(OpKernelContext* context) const override;
};

Gather::ComputeInternal

Gather::ComputeInternal
GatherBase::PrepareForCompute
GatherImpl

创建一个 GatherBase::Prepare 结构体,包含了两个输入和一个输出张量的指针。
GatherBase::PrepareForCompute 准备输入输出。输出张量的秩为input_rank - 1 + indices_rank,即将axis参数指定的轴替换为indices张量的形状。
ORT_RETURN_IF_ERROR 在表达式失败时返回错误。
TensorShape::SizeFromDimension 计算从指定维度开始的乘积大小。
axis参数会将输入张量划分为3部分:batch 维度、索引维度、分块维度。
block_size为每个索引对应的分块大小。
N为索引数量。
input_block_size为在输入上的分块大小。
indices_max即索引上限。

Status Gather::ComputeInternal(OpKernelContext* context) const {Prepare p;ORT_RETURN_IF_ERROR(PrepareForCompute(context, p));const TensorShape& input_shape = p.input_tensor->Shape();const int64_t block_size = input_shape.SizeFromDimension(p.axis + 1);size_t N = p.indices_tensor->Shape().Size();const int64_t input_block_size = input_shape.SizeFromDimension(p.axis);const int64_t output_block_size = N * block_size;const int64_t indices_max = input_shape[p.axis];
  const void* input_data = p.input_tensor->DataRaw();const void* indices_data = p.indices_tensor->DataRaw();void* output_data = p.output_tensor->MutableDataRaw();if (p.output_tensor->Shape().Size() == 0) {return Status::OK();}

gsl::narrow可确保无损失转换,并在无法转换时引发gsl::narrowing_error
fast_divmod 即 DivMod,用于快速计算除法。

  const fast_divmod divmod_output_block_size(gsl::narrow_cast<int>(output_block_size));const fast_divmod divmod_block_size(gsl::narrow_cast<int>(block_size));const size_t element_size = p.input_tensor->DataType()->Size();const size_t index_element_size = p.indices_tensor->DataType()->Size();

GatherImpl 函数索仅支持int32_tint64_t引类型。
传入的p.output_tensor->Shape().Size()即输出元素总数。

  // CUDA Kernel implementation supports element sizes of:// int8_t, int16_t, int32_t and int64_t which covers all supported// types since there is no computations necessary just data movementif (p.indices_tensor->IsDataType<int32_t>() ||p.indices_tensor->IsDataType<int64_t>()) {GatherImpl(Stream(context),input_block_size,indices_max,divmod_output_block_size,divmod_block_size,indices_data,index_element_size,input_data,element_size,output_data,p.output_tensor->Shape().Size());return Status::OK();}

ORT_MAKE_STATUS 创建一个 Status 对象。

  return ORT_MAKE_STATUS(ONNXRUNTIME, NOT_IMPLEMENTED, "Type for Tind not supported yet in Gather.");
}

GatherImpl

GatherImpl
_GatherKernel

GridDim 结构体中定义了美剧值。
N为输出元素数量。直接求出所需 threadblock 的数量,没有太多策略。

void GatherImpl(cudaStream_t stream,const int64_t input_block_size,const int64_t indices_max,const fast_divmod& output_block_size,const fast_divmod& block_size,const void* indices_data,size_t index_element_size,const void* input_data,size_t element_size,void* output_data,const size_t N) {int blocksPerGrid = (int)(ceil(static_cast<float>(N) / GridDim::maxThreadsPerBlock));

ToCudaType 模板类将类型枚举转换为数据类型。
根据元素大小调用 _GatherKernel 模板函数,这样减少了实例化类型。

  switch (element_size) {case sizeof(int8_t): {using CudaType = typename ToCudaType<int8_t>::MappedType;_GatherKernel<<<blocksPerGrid, GridDim::maxThreadsPerBlock, 0, stream>>>(input_block_size, indices_max, output_block_size, block_size, indices_data, index_element_size,reinterpret_cast<const CudaType*>(input_data), reinterpret_cast<CudaType*>(output_data), (CUDA_LONG)N);} break;case sizeof(int16_t): {using CudaType = typename ToCudaType<int16_t>::MappedType;_GatherKernel<<<blocksPerGrid, GridDim::maxThreadsPerBlock, 0, stream>>>(input_block_size, indices_max, output_block_size, block_size, indices_data, index_element_size,reinterpret_cast<const CudaType*>(input_data), reinterpret_cast<CudaType*>(output_data), (CUDA_LONG)N);} break;case sizeof(int32_t): {using CudaType = typename ToCudaType<int32_t>::MappedType;_GatherKernel<<<blocksPerGrid, GridDim::maxThreadsPerBlock, 0, stream>>>(input_block_size, indices_max, output_block_size, block_size, indices_data, index_element_size,reinterpret_cast<const CudaType*>(input_data), reinterpret_cast<CudaType*>(output_data), (CUDA_LONG)N);} break;case sizeof(int64_t): {using CudaType = typename ToCudaType<int64_t>::MappedType;_GatherKernel<<<blocksPerGrid, GridDim::maxThreadsPerBlock, 0, stream>>>(input_block_size, indices_max, output_block_size, block_size, indices_data, index_element_size,reinterpret_cast<const CudaType*>(input_data), reinterpret_cast<CudaType*>(output_data), (CUDA_LONG)N);} break;default:ORT_THROW("Unsupported element size by the Gather CUDA kernel");}
}

_GatherKernel

_GatherKernel
GetIndexValue

CALCULATE_ELEMENTWISE_INDEX_OR_EXIT 计算元素索引,并在超出范围时返回。

template <typename T>
__global__ void _GatherKernel(const int64_t input_block_size,const int64_t indices_max,const fast_divmod output_block_size,const fast_divmod block_size,const void* indices_data,const size_t index_element_size,const T* input_data,T* output_data,const CUDA_LONG N) {CALCULATE_ELEMENTWISE_INDEX_OR_EXIT(id, N);CUDA_LONG input_index = 0;

线程号id除以output_block_size得到输出元素所对应的输入分块索引input_block_index和输入分块内的偏移block_offset
根据block_offset计算对应的indices张量的索引以及分块内元素偏移。
GetIndexValue 取出indices张量的值。相比 TensorFlow 的 gather_functor_gpu.cu.h 没有进行向量化访存优化。
idx支持负数。索引值超出范围时赋零。

  int input_block_index, block_offset;output_block_size.divmod(id, input_block_index, block_offset);int indices_index, offset;block_size.divmod(block_offset, indices_index, offset);int64_t idx = GetIndexValue(indices_data, index_element_size, indices_index);idx = idx < 0 ? idx + indices_max : idx;if (idx < 0 || idx >= indices_max) {output_data[id] = 0;return;}

三部分相加得到输入张量索引。

  input_index = input_block_index * input_block_size + idx * block_size.d_ + offset;output_data[id] = input_data[input_index];
}

GetIndexValue

index_data指针转为相应类型,然后返回偏移位置的值。

__host__ __device__ inline int64_t GetIndexValue(const void* index_data, size_t index_element_size, size_t offset) {switch (index_element_size) {case sizeof(int32_t):return *(reinterpret_cast<const int32_t*>(index_data) + offset);break;case sizeof(int64_t):return *(reinterpret_cast<const int64_t*>(index_data) + offset);break;default:break;}// What is a sensible thing to do here?assert(false);return std::numeric_limits<int64_t>::max();
}

DivMod

除法取余实现基于 Division by Invariant Integers using Multiplication 中的 Figure 4.1。

在这里插入图片描述

// The code below is based on section 4 Unsigned division of paper https://gmplib.org/~tege/divcnst-pldi94.pdf
// In current ORT, fast_divmod is used for calculating the position of a element in tensor,
// so unsigned integer division from the paper is good enough for ORT. The advantage is that div is very simple,
// then GPU compiler can do loop unroll easilly when divmod is called in a loop.
template <>
struct DivMod<int> {DivMod(int d = 1) {d_ = d == 0 ? 1 : d;ORT_ENFORCE(d_ >= 1 && d_ <= static_cast<uint32_t>(std::numeric_limits<int>::max()));

l_ ℓ = ⌈ log ⁡ 2 x ⌉ \ell = \lceil \log_2 x \rceil =log2x

    for (l_ = 0; l_ < 32; l_++)if ((1U << l_) >= d_) break;

m m ′ = ⌊ 2 N ∗ ( 2 ℓ − d ) / d ⌋ + 1 m' = \lfloor 2^N ∗ (2^\ell − d)/d\rfloor + 1 m=2N(2d)/d+1

    uint64_t one = 1;uint64_t m = ((one << 32) * ((one << l_) - d_)) / d_ + 1;M_ = static_cast<uint32_t>(m);// according to paper, the value of m' should fit in a unsigned integer.ORT_ENFORCE(M_ > 0 && M_ == m);}

DivMod::div

t t 1 = M U L U H ( m ′ , n ) t_1 = \mathrm{MULUH}(m', n) t1=MULUH(m,n),使用uint64_t计算避免溢出。
对于 q q q

  • 如果 d = 1 d = 1 d=1,那么 ℓ = 0 \ell = 0 =0,所以 m ′ = 1 m' = 1 m=1 s h 1 = s h 2 = 0 sh_1 = sh_2 = 0 sh1=sh2=0。代码计算 t 1 = ⌊ 1 ∗ n / 2 N ⌋ = 0 t_1 = \lfloor 1 ∗ n/2^N \rfloor = 0 t1=1n/2N=0 q = n q = n q=n
  • d > 1 d > 1 d>1,则 ℓ ≥ 1 \ell≥1 1,故 s h 1 = 1 sh_1 = 1 sh1=1 s h 2 = ℓ − 1 sh_2 =\ell −1 sh2=1
    q = S R L ( t 1 + S R L ( n − t 1 , s h 1 ) , s h 2 ) = S R L ( t 1 + S R L ( n − t 1 , 1 ) , ℓ − 1 ) = ⌊ t 1 + ⌊ ( n − t 1 ) 2 ⌋ 2 ℓ − 1 ⌋ = ⌊ ⌊ 2 ∗ t 1 2 + ( n − t 1 ) 2 ⌋ 2 ℓ − 1 ⌋ (4.5) = ⌊ ⌊ ( t 1 + n ) / 2 ⌋ 2 ℓ − 1 ⌋ = ⌊ t 1 + n 2 ℓ ⌋ \begin{aligned} q &= \mathrm{SRL}(t_1 + \mathrm{SRL}(n − t_1, sh_1), sh_2)\\ &= \mathrm{SRL}(t_1 + \mathrm{SRL}(n − t_1, 1), \ell− 1)\\ &=\lfloor \frac{t_1 + \lfloor \frac{(n − t_1)}{2} \rfloor}{2^{\ell− 1}}\rfloor\\ &=\lfloor \frac{\lfloor \frac{2*t_1}{2} + \frac{(n − t_1)}{2} \rfloor}{2^{\ell− 1}}\rfloor \qquad\text{(4.5)}\\ &=\lfloor \frac{\lfloor(t_1 + n)/2\rfloor}{2^{\ell− 1}} \rfloor\\ &=\lfloor \frac{t_1 + n}{2^{\ell}} \rfloor \end{aligned} q=SRL(t1+SRL(nt1,sh1),sh2)=SRL(t1+SRL(nt1,1),1)=21t1+2(nt1)=2122t1+2(nt1)(4.5)=21⌊(t1+n)/2=2t1+n

__umulhi 计算两个 32 位无符号整数的乘积的最高有效 32 位。

  __host__ __device__ inline int div(int n) const {
#if defined(__CUDA_ARCH__) || defined(__HIP_DEVICE_COMPILE__)uint32_t t = __umulhi(M_, n);return (t + n) >> l_;
#else// Using uint64_t for t, then t + n won't overflow.uint64_t t = ((uint64_t)M_ * n) >> 32;return static_cast<int>((t + n) >> l_);
#endif}

DivMod::mod

n m o d d = n − d ∗ ⌊ n / d ⌋ n \enspace \mathrm{mod} \enspace d = n − d ∗ \lfloor n/d \rfloor nmodd=ndn/d

  __host__ __device__ inline int mod(int n) const {return n - div(n) * d_;}

DivMod::divmod

  __host__ __device__ inline void divmod(int n, int& q, int& r) const {q = div(n);r = n - q * d_;}
  uint32_t d_;  // divisoruint32_t M_;  // m' in the paper.uint32_t l_;  // l_ = ceil(log2(d_))
};
  • Gather
  • ONNXRuntime整体概览
  • ONNXRuntime源码之OpKernel注册
  • Ways to specify [[nodiscard]] before C++17
  • microsoft/GSL
  • How to use gsl narrow cast
  • 警告 C26472
  • GSL and C++ Core Guidelines
  • Gather
  • Gather
  • tf.gather
  • torch.gather
  • paddle.gather
  • [菁英计划] 索引取值及gather函数 #36815
  • paddle. take_along_axis
  • torch.gather in pytorch.onnx and onnxruntime #31464
  • Replace torch.gather by other operator?
  • Problem compiling onnx model using GLOW compiler: constant not found
  • pytorch导出onnx的原则-以SwinTransformer和DETR在trt8.0.3.4部署为例
  • GatherElements
  • tf2onnx Gather
  • OpenVINO Gather
  • Pytorch equivalent of numpy.take()
  • torch.index_select
  • wkentaro/pytorch-for-numpy-users
  • Similar operation like numpy.take
  • numpy.take
  • tensorflow/tensorflow/core/kernels/gather_functor.h
  • tensorflow/core/kernels/gather_functor_batched.h
  • abseil中的微操

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/586975.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

汽车贴膜改色小程序源码 汽车配色小程序源码 车身改色app源码 带后台 带数据

汽车贴膜改色小程序源码 车身改色app源码 汽车配色小程序源码 带后台 带数据 整站源码&#xff0c;包含完整前端小程序&#xff0c;后台源码&#xff0c;数据库数据。 直接部署&#xff0c;就能使用&#xff0c;源码素材远程开发&#xff0c;可以定制开发。 全开源&#xff0c;…

【测试篇】接口测试

接口测试&#xff0c;可以用可视化工具 postman。 如何做接口测试&#xff1f;&#xff1f; 我们可以先在浏览器中随机进入一个网页&#xff0c;打开开发者工具&#xff08;F12&#xff09;。 随便找一个接口Copy–>Copy as cURL(bash) 打开postman 复制地址 进行发送。 …

关系(二)利用python绘制热图

关系&#xff08;二&#xff09;利用python绘制热图 热图 &#xff08;Heatmap&#xff09;简介 热图适用于显示多个变量之间的差异&#xff0c;通过颜色判断彼此之间是否存在相关性。 快速绘制 基于seaborn import seaborn as sns import pandas as pd import numpy as np i…

Windows系统下载安装SQLServer

Windows系统下载安装SQLServer Microsoft SQL Server 是一个全面的数据库平台&#xff0c;使用集成的商业智能 (BI)工具提供了企业级的数据管理。Microsoft SQL Server 数据库引擎为关系型数据和结构化数据提供了更安全可靠的存储功能&#xff0c;使您可以构建和管理用于业务的…

如何从只会 C++ 语法的水平到达完成项目编写软件的水平?

初学者在学习C的过程中&#xff0c;通常首先接触的是基本语法和结构。然而&#xff0c;仅停留在“会用语法”的阶段并不能胜任实际项目的编写工作。要真正具备独立完成项目的能力&#xff0c;需要对C有更深入的理解和实践积累&#xff0c;这包括但不限于面向对象设计、数据结构…

Python学习:面相对象

面向对象 面向对象技术简介 类(Class): 用来描述具有相同的属性和方法的对象的集合。它定义了该集合中每个对象所共有的属性和方法。对象是类的实例。方法:类中定义的函数。类变量:类变量在整个实例化的对象中是公用的。类变量定义在类中且在函数体之外。类变量通常不作为实…

k8s calico由IPIP模式切换为BGP模式

按照官网calico.yaml部署后&#xff0c;默认是IPIP模式 查看route -n &#xff0c; 看到是tunl0口进行转发 怎么切换到BGP模式呢&#xff1f; kubectl edit ippool 将ipipMode由Always修改为Never &#xff0c;修改后保存文件即可。无需做任何操作&#xff0c;自动就切换为BG…

基于深度学习的端到端自动驾驶的最新进展:调研综述

基于深度学习的端到端自动驾驶的最新进展&#xff1a;调研综述 附赠自动驾驶学习资料和量产经验&#xff1a;链接 论文链接&#xff1a;https://arxiv.org/pdf/2307.04370.pdf 调研链接&#xff1a;https://github.com/Pranav-chib/ 摘要 本文介绍了基于深度学习的端到端自…

C语言联合体,枚举详解

1. 前言 前边我们已经了解了结构体在C语言当中是如何使用的&#xff0c;今天咱来一起聊一聊联合体与枚举在C语言当中又是如何运用的呢 2. 联合体的了解与运用 2.1 联合体的声明&#xff1a; 相比于结构体来说&#xff0c;联合体最大的区别就在于它是联合体当中所有成员共用一…

FreeRTOS作业day3

1.总结任务调度算法之间的区别&#xff0c;重新实现一遍任务调度算法的代码。 区别&#xff1a; 抢占式调度适用于任务优先级不同的任务&#xff0c;时间片轮转适用于相同优先级的任务 优先级调度&#xff1a;根据任务的优先级进行调度&#xff0c;高优先级的任务优先执行。 时…

国际贸易中的O/A付款方式及其风险管理 | 全球数字贸易发展联盟的作用

在复杂多变的国际贸易领域中&#xff0c;O/A&#xff08;Open Account&#xff09;付款方式作为一种常见的交易方式&#xff0c;既为买卖双方提供了灵活的操作空间&#xff0c;也带来了一定的风险。本文将对O/A付款方式进行深入探讨&#xff0c;并介绍全球数字贸易发展联盟如何…

vue 加 websocket 聊天

<template><div style="height: 100%; width: 100%; background-color: #fff"><div class="wrap"><!-- 头部 --><div class="titleBox"><imgsrc="@/assets/image/avatar.png"style="argin: 10p…