<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>PyTorch on cc</title><link>https://ccalwayscc.com/tags/pytorch/</link><description>Recent content in PyTorch on cc</description><generator>Hugo</generator><language>zh-CN</language><lastBuildDate>Sat, 01 Aug 2026 00:00:00 +0800</lastBuildDate><atom:link href="https://ccalwayscc.com/tags/pytorch/index.xml" rel="self" type="application/rss+xml"/><item><title>PyTorch Distributed 与 Collective Communication</title><link>https://ccalwayscc.com/posts/torch-distributed/</link><pubDate>Sat, 01 Aug 2026 00:00:00 +0800</pubDate><guid>https://ccalwayscc.com/posts/torch-distributed/</guid><description>&lt;h1 id="pytorch-distributed-与-collective-communication"&gt;PyTorch Distributed 与 Collective Communication&lt;/h1&gt;
&lt;h2 id="torch-distributed"&gt;torch distributed&lt;/h2&gt;
&lt;p&gt;torch distributed以进程组(process group, PG) 作为整个通信系统，每个PG有自己的world_size与成员process，每个process拥有自己的rank，以此为基础进行通信。通信后端支持nccl(n卡GPU), gloo(CPU), MPI(各种Message Passing Interface是西安)以及自定义PG后端, torch 2.x还支持ucc(Nvidia推出的统一collective通信库, xccl(XPU Collective Communication Library, 围绕Intel GPU/XPU).&lt;/p&gt;
&lt;p&gt;torch distributed通常需要定义一些进程内环境变量：&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;MASTER_ADDR&lt;/code&gt;: 表示RANK0所在机器的地址(IP/hostname)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;WORLD_SIZE&lt;/code&gt;: PG成员数量&lt;/li&gt;
&lt;li&gt;&lt;code&gt;RANK&lt;/code&gt;: 当前process的全局ID(0~WORLD_SIZE-1)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;LOCAL_RANK&lt;/code&gt;: process在本机内的编号&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;code&gt;LOCAL_RANK&lt;/code&gt; 通常用来设置初始时torch cuda设置device，比如:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;local_rank&lt;span style="color:#f92672"&gt;=&lt;/span&gt;init(os&lt;span style="color:#f92672"&gt;.&lt;/span&gt;environ[&lt;span style="color:#e6db74"&gt;&amp;#34;LOCAL_RANK&amp;#34;&lt;/span&gt;])
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;torch&lt;span style="color:#f92672"&gt;.&lt;/span&gt;cuda&lt;span style="color:#f92672"&gt;.&lt;/span&gt;set_device(local_rank)
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;torch.distributed里和PG相关的操作主要有:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;init_process_group&lt;/code&gt; : 初始化默认PG&lt;/li&gt;
&lt;li&gt;&lt;code&gt;destroy_process_group&lt;/code&gt;: 摧毁释放指定PG,未指定则释放默认PG&lt;/li&gt;
&lt;li&gt;&lt;code&gt;new_group&lt;/code&gt; : 在默认PG的基础上，按照指定ranks创建子PG&lt;/li&gt;
&lt;li&gt;&lt;code&gt;is_initialized&lt;/code&gt;: 默认PG是否被初始化&lt;/li&gt;
&lt;li&gt;&lt;code&gt;get_backend&lt;/code&gt;: 获取指定PG的backend&lt;/li&gt;
&lt;li&gt;&lt;code&gt;get_world_size&lt;/code&gt;:获取指定PG的world_size&lt;/li&gt;
&lt;li&gt;&lt;code&gt;MASTER_PORT&lt;/code&gt;: RANK0上的一个空闲窗口&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;与communication相关的操作主要有:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;torch.distributed.broadcast(tensor, src, group=None)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;torch.distributed.all_reduce(tensor, op=ReduceOp.SUM, group=None)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;torch.distributed.reduce(tensor, dst, op=..., group=None)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;torch.distributed.all_gather(tensor_list, tensor, group=None)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;torch.distributed.gather(tensor, dst, group=None)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;torch.distributed.scatter(tensor, scatter_list, src, group=None)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;torch.distributed.reduce_scatter(output, input_list, op=..., group=None)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;torch.distributed.all_to_all(output_tensor_list, input_tensor_list, group=None)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;torch.distributed.all_gather_object(object_list, obj, group=None)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;torch.distributed.broadcast_object_list(obj_list, src, group=None)&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;除去上述这8中collective(还有一些和python object相关)，还支持P2P通信:&lt;/p&gt;</description></item></channel></rss>