当前位置: 首页 > news >正文

Pytorch autoload机制自动加载树外扩展(Autoload Device Extension)

1. 动机和原理

autoload device extension 机制的作用是:

# on Ascend devices(HUAWEI)
import torch
# import torch_npu # <---  No longer needed in torch_npu 2.5 and later versionsx = torch.randn(2, 2).npu()
y = torch.randn(2, 2).npu()
z = x.mm(y)# on Moore Threads devices
import torch
# import torch_musa # <---  No longer needed in torch_npu 2.5 and later versionsa = torch.tensor([1.2, 2.3], dtype=torch.float32, device='musa')
b = torch.tensor([1.2, 2.3], dtype=torch.float32, device='cpu').to('musa')
c = torch.tensor([1.2, 2.3], dtype=torch.float32).musa()

可以自动加载各家厂商的插件库,而不需要显式 import 多家厂商的 torch 插件库(从 torch 2.5 开始支持此特性)。

其实现原理是利用 Python的 EntryPoint 机制:

  1. torch的 __init__.py 文件中会遍历组名为"torch.backends"的所有backend_extensions,对于每个backend_extension,首先调用load()来导入插件模块并获得定义的入口函数,然后调用entrypoint() 来运行入口函数
  2. 各厂商在适配torch的时候,只需要在 各自的setup.py 中定义组名为"torch.backends"的 entrypoint 即可实现在import torch时自动导入对应厂商的torch插件,无需显式导入。

2. 源码

torch/**__**init**__**.py 相关函数节选如下:

  1. 可以将环境变量TORCH_DEVICE_BACKEND_AUTOLOAD = 0来关闭自动加载
# 这部分代码位于 __init__.py文件的最后(也就是在import torch时会最后才 import out-of-tree device
def _import_device_backends():"""Leverage the Python plugin mechanism to load out-of-the-tree device extensions.See this RFC: https://github.com/pytorch/pytorch/issues/122468"""from importlib.metadata import entry_pointsgroup_name = "torch.backends"if sys.version_info < (3, 10):backend_extensions = entry_points().get(group_name, ())else:backend_extensions = entry_points(group=group_name)for backend_extension in backend_extensions:try:# Load the extensionentrypoint = backend_extension.load()# Call the entrypointentrypoint()except Exception as err:raise RuntimeError(f"Failed to load the backend extension: {backend_extension.name}. "f"You can disable extension auto-loading with TORCH_DEVICE_BACKEND_AUTOLOAD=0.") from errdef _is_device_backend_autoload_enabled() -> builtins.bool:"""Whether autoloading out-of-the-tree device extensions is enabled.The switch depends on the value of the environment variable`TORCH_DEVICE_BACKEND_AUTOLOAD`.Returns:bool: Whether to enable autoloading the extensions. Enabled by default.Examples:>>> torch._is_device_backend_autoload_enabled()True"""# enabled by defaultreturn os.getenv("TORCH_DEVICE_BACKEND_AUTOLOAD", "1") == "1"if _is_device_backend_autoload_enabled():_import_device_backends()

torch_musa/__init__.py 相关内容如下:

is_loaded = Falsedef _autoload():print(f"call torch_musa/_autoload")global is_loadedif is_loaded:print("torch_musa already loaded.")returnprint("loading torch_musa into torch.musa...")import torch

torch_npu/__**init__**.py相关内容如下

...
# Disable autoloading before running 'import torch' to avoid circular dependencies
ORG_AUTOLOAD = os.getenv("TORCH_DEVICE_BACKEND_AUTOLOAD", "1")
os.environ["TORCH_DEVICE_BACKEND_AUTOLOAD"] = "0"
...# This function is an entrypoint called by PyTorch
# when running 'import torch'. There is no need to do anything.
def _autoload():# We should restore this switch as sub processes need to inherit its valueos.environ["TORCH_DEVICE_BACKEND_AUTOLOAD"] = ORG_AUTOLOAD

疑问:

  1. entrypoint() 如果省略会怎么样?:仍可以成功实现 autoload,autoload的核心是:在 import torch 时自动调用 torch_xx 的函数(因此需要 load 该模块),_autoload()函数其实什么也不做.
  2. 是否存在循环引用问题?在torch.__init__.py中,_import_device_backends 位于最后,因此(以下两条为个人分析,欢迎讨论
    1. 在导入 torch 触发 torch_musa 的导入时,torch的导入已经完成,所有 symbol 已经成功导出,sys.modules['torch'] 已经存在,不会出现循环引用的问题;
    2. torch_npu 的设计思路是:在导入 torch_npu 中 import torch 时,将 TORCH_DEVICE_BACKEND_AUTOLOAD 环境变量取值记录下来并且设置为0,在完成 torch_npu的导入后,在末尾将 TORCH_DEVICE_BACKEND_AUTOLOAD设为原始值,这不依赖于 _import_device_backendstorch.__init__.py中的位置。

3. References

  1. torch issue:https://github.com/pytorch/pytorch/issues/122468
  2. torch PR:https://github.com/pytorch/pytorch/pull/127074
  3. torch docs:https://docs.pytorch.ac.cn/tutorials/unstable/python_extension_autoload.html#how-it-works
  4. torch_npu:https://github.com/Ascend/pytorch
  5. torch_musa:https://github.com/MooreThreads/torch_musa/
http://www.dtcms.com/a/465982.html

相关文章:

  • 网站关键词在哪设置WordPress瀑布流图片站
  • 东莞网站建设备案关于建设网站的情况说明书
  • 格式工厂转换视频很慢、Pr视频剪辑导出很慢的优化设置(就用显卡的默认设置即可)
  • 有哪些网站做的符合企业风格公司网站建设怎么做
  • 高端大气的网站青岛软件开发公司排名
  • Skywalking从部署集成到动态调优(上)
  • Skywalking从部署集成到动态调优(下)
  • 游戏大规模数据存储与数据量增加之后扩容的思考
  • 电脑上做网站的软件网站建设中的服务器搭建方式
  • 「机器学习笔记9」回归分析:从理论到实践的全面指南
  • 如何起手做网站项目提高索引量的方法
  • JDBC初识
  • 用已存在的虚拟环境建立一个新项目(配置解释器)
  • 湖南省住房和城乡建设厅门户网站seo排名优化软件价格
  • Linux 系统启动过程详解
  • 死信队列vs延迟队列
  • 给实体店老板做的网站怎么网站排名seo
  • 商务网站建设毕业设计成都网站建设公司排行
  • 基于体素密度的几何重要性剔除(Voxel Density Culling)
  • 卷积神经网络详解
  • Redission
  • 前端框架深度解析:Vue 从入门到实战,掌握渐进式开发核心
  • 从暴力到滑动窗口全解析——力扣8. 字符串转换整数 (atoi)
  • 迅为RK3562开发板Android 系统动态替换开机logo的实现-替换logo
  • 基于Springboot + vue3实现的校园闲置物品交易平台
  • 学校网站 制作wordpress图片时间
  • MySQL分区分表实现方法详解
  • 缠论工具czsc快速使用入门(二)
  • Android 14 Input 事件派发机制深度剖析
  • 苏州做网站外包的公司有哪些许昌做网站团队