注意这个和前面的《Python与C语言混合编程:通过distutils或setuptools实现的一个简单的C扩展》不同,这个是pytorch的扩展,不是python的扩展。
在pytorch的utils中,集成了setuptools模块。
官方文档在这里:https://pytorch.org/docs/master/cpp_extension.html
中文说明在这里:https://ptorch.com/news/188.html
创建一个C++
的setuptools.Extension
。
便捷地创建一个setuptools.Extension
具有最小(但通常是足够)的参数来构建C++
扩展的方法。
所有参数都被转发给setuptools.Extension
构造函数。
例
>>> from setuptools import setup
>>> from torch.utils.cpp_extension import BuildExtension, CppExtension
>>> setup(
name='extension',
ext_modules=[
CppExtension(
name='extension',
sources=['extension.cpp'],
extra_compile_args=['-g'])),
],
cmdclass={
'build_ext': BuildExtension
})
为CUDA/C++
创建一个setuptools.Extension
。
创建一个setuptools.Extension
用于构建CUDA/C ++
扩展的最少参数(但通常是足够的)的便捷方法。这里包括CUDA
路径,库路径和运行库。 所有参数都被转发给setuptools.Extension
构造函数。
例
>>> from setuptools import setup
>>> from torch.utils.cpp_extension import BuildExtension, CppExtension
>>> setup(
name='cuda_extension',
ext_modules=[
CUDAExtension(
name='cuda_extension',
sources=['extension.cpp', 'extension_kernel.cu'],
extra_compile_args={'cxx': ['-g'],
'nvcc': ['-O2']})
],
cmdclass={
'build_ext': BuildExtension
})
自定义setuptools
构建扩展。
setuptools.build_ext
子类负责传递所需的最小编译器参数(例如-std=c++11
)以及混合的C ++/CUDA
编译(以及一般对CUDA
文件的支持)。
当使用BuildExtension
时,它将提供一个用于extra_compile_args
(不是普通列表)的词典,通过语言(cxx
或cuda
)映射到参数列表提供给编译器。这样可以在混合编译期间为C ++
和CUDA
编译器提供不同的参数。
即时加载(JIT)PyTorch C ++
扩展。
为了加载扩展,会创建一个Ninja
构建文件,该文件用于将指定的源编译为动态库。随后将该库作为模块加载到当前Python
进程中,并从该函数返回,以备使用。
默认情况下,构建文件创建的目录以及编译结果库是<tmp>/torch_extensions/<name>
,其中<tmp>
是当前平台上的临时文件夹以及<name>
为扩展名。这个位置可以通过两种方式被覆盖。首先,如果TORCH_EXTENSIONS_DIR
设置了环境变量,它将替换<tmp>/torch_extensions
并将所有扩展编译到此目录的子文件夹中。其次,如果build_directory
函数设置了参数,它也将覆盖整个路径,即,库将直接编译到该文件夹中。
要编译源文件,使用默认的系统编译器(c++),可以通过设置CXX
环境变量来覆盖它。将其他参数传递给编译过程,extra_cflags
或者extra_ldflags
可以提供。例如,要通过优化来编译您的扩展,你可以传递extra_cflags=['-O3']
,也可以使用 extra_cflags
传递进一步包含目录。
提供了混合编译的CUDA
支持。只需将CUDA
源文件(.cu
或.cuh
)与其他源一起传递即可。这些文件将被检测,并且使用nvcc
而不是C ++
编译器进行编译。包括将CUDA lib64
目录作为库目录传递并进行cudart
链接。您可以将其他参数传递给nvcc extra_cuda_cflags
,就像使用C ++
的extra_cflags
一样。使用了各种原始方法来查找CUDA
安装目录,通常情况下可以正常运行。如果不可以,最好设置CUDA_HOME
环境变量。
pybind11
模块的名字一样!C++
源文件的相对或绝对路径列表。CUDA
源时转发给nvcc
。True
,打开加载步骤的详细记录。PyTorch
扩展作为Python
模块。例
>>> from torch.utils.cpp_extension import load
>>> module = load(
name='extension',
sources=['extension.cpp', 'extension_kernel.cu'],
extra_cflags=['-O2'],
verbose=True)
Loads a PyTorch C++ extension just-in-time (JIT) from string sources.
This function behaves exactly like load()
, but takes its sources as strings rather than filenames. These strings are stored to files in the build directory, after which the behavior of load_inline()
is identical to load()
.
See the tests for good examples of using this function.
Sources may omit two required parts of a typical non-inline C++ extension: the necessary header includes, as well as the (pybind11) binding code. More precisely, strings passed to cpp_sources
are first concatenated into a single .cpp
file. This file is then prepended with #include <torch/extension.h>
.
Furthermore, if the functions
argument is supplied, bindings will be automatically generated for each function specified. functions
can either be a list of function names, or a dictionary mapping from function names to docstrings. If a list is given, the name of each function is used as its docstring.
The sources in cuda_sources
are concatenated into a separate .cu
file and prepended with torch/types.h
, cuda.h
and cuda_runtime.h
includes. The .cpp
and .cu
files are compiled separately, but ultimately linked into a single library. Note that no bindings are generated for functions in cuda_sources
per se. To bind to a CUDA kernel, you must create a C++ function that calls it, and either declare or define this C++ function in one of the cpp_sources
(and include its name in functions
).
See load()
for a description of arguments omitted below.
Parameters: |
|
---|
Example
>>> from torch.utils.cpp_extension import load_inline
>>> source = '''
at::Tensor sin_add(at::Tensor x, at::Tensor y) {
return x.sin() + y.sin();
}
'''
>>> module = load_inline(name='inline_extension',
cpp_sources=[source],
functions=['sin_add'])
获取构建C++
或CUDA
扩展所需的路径。
cuda
- 如果为True,则包含CUDA
特定的包含路径。例如:
from setuptools import setup
from torch.utils.cpp_extension import BuildExtension, CppExtension
torch.utils.cpp_extension.include_paths(cuda=False)
#
['/usr/local/lib/python3.6/site-packages/torch/lib/include',
'/usr/local/lib/python3.6/site-packages/torch/lib/include/TH',
'/usr/local/lib/python3.6/site-packages/torch/lib/include/THC']
验证给定的编译器是否与PyTorch
ABI兼容。
shell
进程中可执行。PyTorch
ABI不兼容,则为False
,否则返回True
。如果可以在ninja上运行则返回True
。
文章浏览阅读467次。计算机毕业设计Java疫情防控医用品管理(系统+源码+mysql数据库+Lw文档)springboot基于SpringBoot的婚庆策划系统的设计与实现。JSP健身俱乐部网站设计与实现sqlserver和mysql。JSP网上测试系统的研究与设计sqlserver。ssm基于SpringMvC的流浪狗领养系统。ssm基于Vue.js的音乐播放器设计与实现。ssm校园流浪猫图鉴管理系统的设计与实现。_疫情防护用品销售管理系统 论文
文章浏览阅读988次,点赞28次,收藏28次。最后小编想说:不论以后选择什么方向发展,目前重要的是把Android方面的技术学好,毕竟其实对于程序员来说,要学习的知识内容、技术有太多太多,要想不被环境淘汰就只有不断提升自己,从来都是我们去适应环境,而不是环境来适应我们!这里附上我整理的几十套腾讯、字节跳动,京东,小米,头条、阿里、美团等公司19年的Android面试题。把技术点整理成了视频和PDF(实际上比预期多花了不少精力),包含知识脉络 + 诸多细节。由于篇幅有限,这里以图片的形式给大家展示一小部分。
文章浏览阅读600次,点赞11次,收藏6次。*单片机设计介绍,基于单片机数码管秒表控制系统设计。
文章浏览阅读235次。python小程序之验证码图片的生成定义随机字母的生成函数定义随机颜色生成函数,采用RGB格式,生成一个元组调用Image,生成画布,填充底色为白色调用画笔函数Draw,传入画布对象填充画布的每一个色块,作为背景在画布上控制间距,填上每一个字在最后的图上进行模糊操作代码# 生成一个随机的二维码小程序from PIL import Image,ImageDraw,ImageF..._小程序图片验证码后端生成
文章浏览阅读2.2k次。 1. 用户需求分析客户规模:客户有一个总部,具有一定规模的园区网络; 一个分支机构,约有20-50名员工; 用户有很多移动办公用户 客户需求:组建安全可靠的总部和分支LAN和WAN; 总部和分支的终端需要提供安全防护,并实现网络准入控制,未来实现对VPN用户的网络准入检查; 需要提供IPSEC/SSLVPN接入; 在内部各主要部门间,及内外网络间进_思科设备怎么ranga)服务器区域独立防护;
文章浏览阅读445次。4、转移账号生成的 p8 文件(证书文件)1、转移苹果账号的 teamID。2、接受苹果账号的 teamID。5、接受账号生成的 p8 文件。3、转移应用的 AppID。_apple 账号迁移
文章浏览阅读1k次。https://blog.csdn.net/u012328159/article/details/80311892_momentum seg
文章浏览阅读816次。主要的原理就是替换模板里的特殊字符。 1、静态模板页面 template.html,主要是定义了一些特殊字符,用来被替换。 HTML code DOCTYPE HT_监听数据变更自动生成静态html
文章浏览阅读494次。 今日在做一个新闻系统的评论时. 想到了预防"提交"按钮的多次点击的问提 (prevent multiple clicks of a submit button in ASP.NET). 以前碰到此类问提总是用重定位页面来解决. 这次我想找到一个一劳永逸的办法. 通过查讯Google,找到了一些代码,挑选一些较好的修改了一下。public void pa
文章浏览阅读4.7k次。近来公司业务有需要做socks5代理的需求,研究了一下,主要的开源实现有2个:dante http://www.inet.no/dante/ss5 http://ss5.sourceforge.net/比较了一下,还是比较倾向于dante,因为看到有人这样评价ss5:Project has an incredibly poor source code quality. Th_dante 代理 配置pam用户名密码 模式
文章浏览阅读809次。在excel vba 中用到countifs 函数,但用来统计带有特殊符号* 时总是统计chu_vba countifs 源码
文章浏览阅读2.6k次。当两个效果之间变换时,可以使用transition过渡属性,但是有多个效果来回变换时,就需要使用动画效果,且动画过程可控(重复播放,画面暂停,最终画面等)文章目录1、简介2、实现步骤3、复合属性animation4、动画属性1、简介动画的本质是快速切换大量图片在人脑中形成的具有连续性的画面构成动画的最小单元:帧或者动画帧2、实现步骤定义动画@keyframes 动画名称{ from{} to{}}@keyframes 动画名称{ 0%{} 10%{} 20%{} 50._web前端实现图片动画效果