[源码解析] PyTorch 流水线并行实现 (5)--计算依赖

0x00 摘要

前几篇文章我们介绍了 PyTorch 流水线并行的基本知识,自动平衡机制和切分数据等,本文我们结合论文内容来看看如何实现流水线依赖,核心就是如何建立这些小批次之间的跨设备依赖关系

流水线并行其他文章链接如下:

[源码解析] 深度学习流水线并行Gpipe(1)---流水线基本实现

[源码解析] 深度学习流水线并行GPipe (2) ----- 梯度累积

[源码解析] 深度学习流水线并行 GPipe(3) ----重计算

[源码解析] 深度学习流水线并行之PipeDream(1)--- Profile阶段

[源码解析] 深度学习流水线并行 PipeDream(2)--- 计算分区

[源码解析] 深度学习流水线并行 PipeDream(3)--- 转换模型

[源码解析] 深度学习流水线并行 PipeDream(4)--- 运行时引擎

[源码解析] 深度学习流水线并行 PipeDream(5)--- 通信模块

[源码解析] 深度学习流水线并行 PipeDream(6)--- 1F1B策略

[源码解析] PyTorch 流水线并行实现 (1)--基础知识

[源码解析] PyTorch 流水线并行实现 (2)--如何划分模型

[源码解析] PyTorch 流水线并行实现 (3)--切分数据和运行时系统

[源码解析] PyTorch 流水线并行实现 (4)--前向计算

本文图片来自论文和github源码。

0x01 前文回顾

为了更好的理解本文,我们首先看看前文之中的关键部分。

  • 原始流水线状态如下:

    • 管道并行的策略是根据分区索引 j 分配任务,以便第 j 个分区完全位于第 j 个设备中。
    • 持有模型后期部分的设备必须等待,直到持有模型早期部分的设备计算结束。

  • 目标流水线状态如下:

  • 目前问题

    • 如果分成若干个微批次,则需要强制要求 \(F_{i,j}\) 必须在 \(F_{i+1,j}\) 之前完成,以及 \(B{i,j}\) 必须在执行\(B{i-1,j}\) 之前完成
    • 后向传播的计算图是在前向传播过程中动态构造的。PyTorch既不记录正向计算图,也不维护一个梯度磁带(gradient tape),PyTorch的自动微分(autograd)引擎仅对计算图进行反向传播。这意味着自动加载引擎可能不会完全按照与正向过程相反的执行顺序运行,除非由图的结构强制执行
  • 目前难点

    • 如何在每个设备中以正确的顺序发布那些绑定到设备的任务,以避免由于Python解释器未能提前请求而延迟在设备上(与CPU异步)执行任务。[这个前文已经介绍]
    • 如何建立这些小批次之间的跨设备依赖关系
  • 实现方案

    • 如何保证正确执行顺序?torchgpipe引入了确定性时钟周期(deterministic clock-cycle),它给出了任务的总体顺序[这个前文已经介绍]
    • 如何保证计算图中的动态显式依赖关系?针对clock_cycles产生的每一个运行计划:
      • 利用 fence 函数调用“fork”和“join”,以此在向后计算图中动态创建显式后向传播依赖关系。
      • 利用 compute(schedule, skip_trackers, in_queues, out_queues) 进行计算。

因为前文已经介绍了执行顺序方案,所以本文介绍如何计算依赖。

0x02 计算依赖

  1. +-----------------------------------------------------------------------------------------+
  2. | |
  3. | Layer 1 +---> Layer 2 +-----> Layer 3 +-----> Layer 4 +-----> Layer 5 +---> Layer 6 |
  4. | |
  5. +--------------------------+---------------------------+----------------------------------+
  6. +
  7. |
  8. |
  9. v
  10. +------------------------------------------------------------------------------------+
  11. | +--------------------+ +---------------------+ +--------------------+ |
  12. | |Partition 1 | |Partition 2 | |Partition 3 | |
  13. | | | | | | | |
  14. | | Layer 1 | +---------> Layer 4 | | | |
  15. | | + | | | + | +-------> Layer 6 | |
  16. | | | | | | | | | | | |
  17. | | v | | | | | | | | |
  18. | | Layer 2 | | | | | | | | |
  19. | | + | | | v | | | | |
  20. | | | | | | Layer 5 +---------+ | | |
  21. | | v | | | | | | |
  22. | | Layer 3 +---------+ | | | | |
  23. | | | | | | | |
  24. | +---------+----------+ +---------+-----------+ +-----------+--------+ |
  25. | |
  26. +------------------------------------------------------------------------------------+

为什么需要计算依赖?

  • 因为模型已经被分层,模型的不同部分拆开放到不同设备上,数据也分成微批次,所以本来模型内部是线性依赖关系,现在需要变成流水线依赖关系。因此原始计算图不能满足需求,因此需要有针对性的补充。就像上图那样,6个层被分成了三个partitions,这三个partitons 之间的依赖如何构建
  • 之前的线性依赖关系其实是在模型定义时候就基本确定了,现在则需要每次运行时候建立一个动态依赖关系。

所以针对流水线并行,torchgpipe需要自己补充一个本机跨设备伪分布式依赖关系。torchgpipe 通过在前向计算图和后向计算图做各种调整来达到目的。计算图就意味着各种依赖逻辑,依赖逻辑的补足就是依靠本节介绍的 Fork 和 Join 两个函数完成的。

这里最初有一个疑问,就是Torchgpipe怎么在不使用 PyTorch RPC 和 p2p的情况下,构建出来一个异地反向计算图。后来发现,原来是我想多了,因为Torchgpipe没有考虑到这种情况,它针对都是在同一个主机之上的GPU,不涉及异地多机器计算。

Torchgpipe 本质上还是一个进程内运行多个线程进行计算,是 DP 的替代。比如源码中就有对比如下:

  1. ### ResNet-101 Accuracy Benchmark
  2. Batch size | torchgpipe | nn.DataParallel | Goyal et al.
  3. ---------- | ---------: | --------------: | -----------:
  4. 256 | 21.99±0.13 | 22.02±0.11 | 22.08±0.06
  5. 1K | 22.24±0.19 | 22.04±0.24 | N/A
  6. 4K | 22.13±0.09 | N/A | N/A

再比如代码中明确提到:

  1. If you decide not to use checkpointing at all, :class:`nn.DataParallel
  2. <torch.nn.DataParallel>` might be more efficient than GPipe.

0x03 反向传播依赖

我们首先看看反向传播依赖,这个是论文的重点。

2.1 解析

我们还是要回忆一下前面两个图例。

图1

图2

这里需要完成两种依赖:

  • 行间依赖,就是 batch 之间的依赖,就是设备内的依赖。从图上看,就是蓝色列内的 \(F_{1,1}\) 必须在 \(F_{2,1}\)之前完成,\(B_{2,1}\) 必须在\(B_{1,1}\) 之前完成。
  • 列间依赖,就是 partitions(设备) 之间的依赖。从图上看,就是蓝色 \(F_{1,1}\) 必须在黄色 \(F_{1,2}\)之前完成,即第一个设备必须在第二个设备之前完成,而且第一个设备的输出是第二个设备的输入。

假定我们依据确定性时钟周期(deterministic clock-cycle)算法来运行一个前向传播。即使前向传播是按照在第j个设备上应该执行的顺序来执行任务 \(F_{1,j},...,F_{m,j}\) ,得到的后向传播结果计算图看起来也更像图1而非图2,

从图1上看,PyTorch 的 autograd 引擎不知道 \(B_{i+1,j}\) 必须在 \(B_{i,j}\) 之前运行,因此会打乱后向传播的时间流。因此,虚拟依赖(图2的虚线箭头)必须在前向传播中被显式绘制出来。

我们再仔细分析一下图2。图2之中,每一行都表示一个 micro-batch 在训练中的运行流,这个流的前向是由clock算法确定的。后向关系是由前向传播中自动确定完成的

现在的问题是:一个 mini-batch 被分成了4个 micro-batch,分别在不同时钟周期进入训练。就是每一列。这一列由上到下的传播也是由clock算法确定,但是反向传播(由下自上)目前是不确定的。比如最后一列中,反向传播的顺序应是:\(B_{4,1},B_{3,1},B_{2,1},B_{1,1}\)。但是这个目前从前向传播的结果来看,无法确定这个顺序。

所以需要依靠本节介绍的 Fork 和 Join 两个函数完成这个依赖关系。图中斜线表示checkpoint之中需要先有一个重计算,然后才能由下往上走

因此,torchpipe定义两个基础函数,Fork 和 Join 来表达这种依赖关系:

  • Fork 是 auto grad 函数,其把一个张量 x 映射到 pair(x, \(\phi\)),这里 \(\phi\) 是一个空张量。

  • Join 是 auto grad 函数,其把 pair(x, \(\phi\)) 映射到一个张量 x ,这里 \(\phi\) 是一个空张量。

现在,\(F_{i+1,j}\) 对于 \(F_{i,j}\) 的依赖(其在后向传播计算图中被转换为 \(B_{i,j}\) 到 $B_{i+1,j} $ 的依赖关系)可以被如下表示

所以,图中这里实线都是前向传播时候构建的,虚线是由 fork & join 构建的。

原则上,表示虚拟依赖关系的张量可以是任意的。然而,torchgpipe选择使用空张量,以消除由张量引起的任何不必要的计算,例如PyTorch中的梯度累积。

具体如下图。就是使用 Fork 和 Join 的后向计算图。图中,不同颜色对应不同的设备。箭头依据后向传播图的方向来绘制,这些联系是在前向传播中被构建的。因此,\(F^{'}_{i,j}\) 对于 \(B_{i+1,j}\) 的虚拟依赖通过 Fork 和 Join 被构建出来,用虚线表示。

2.2 基础功能

2.2.1 Function

首先,我们要看看 torch.autograd.Function 的作用。

torch.autograd.Function类实际上是一个操作函数的基础父类,这样的操作函数必须具备两个基本的过程,即前向的运算过程和反向的求导过程,

如果某些操作无法通过 PyTorch 已有的层或者是已有的方法实现不了,就需要实现一个新的方法对 PyTorch 进行拓展。当不使用自动求导机制,需要自定义求导规则的时候,就应该拓展torch.autograd.Function类。 由于pytorch不再提供自动求导机制,就要用户自己定义实现前向传播和反向传播的计算过程,这就是 "Extending torch.autograd"。

我们接下来介绍Backward Dependency 的关键算法:Fork and Join。

2.2.2 Fork

Fork 是auto grad 函数,其把一个张量 x 映射到 pair(x, \(\phi\)),这里 \(\phi\) 是一个空张量。Fork 方法就是拓展了torch.autograd.Function

  1. def fork(input: Tensor) -> Tuple[Tensor, Tensor]:
  2. """Branches out from an autograd lane of the given tensor."""
  3. if torch.is_grad_enabled() and input.requires_grad:
  4. input, phony = Fork.apply(input)
  5. else:
  6. phony = get_phony(input.device, requires_grad=False)
  7. return input, phony
  8. class Fork(torch.autograd.Function):
  9. @staticmethod
  10. def forward(ctx: 'Fork', input: Tensor) -> Tuple[Tensor, Tensor]: # type: ignore
  11. phony = get_phony(input.device, requires_grad=False)
  12. return input.detach(), phony.detach()
  13. @staticmethod
  14. def backward(ctx: 'Fork', grad_input: Tensor, grad_grad: Tensor) -> Tensor: # type: ignore
  15. return grad_input

2.2.3 Join

Join 是auto grad 函数,其把 pair(x, \(\phi\)) 映射到一个张量 x ,这里 \(\phi\) 是一个空张量。Join 方法也是拓展了torch.autograd.Function

  1. def join(input: Tensor, phony: Tensor) -> Tensor:
  2. """Merges two autograd lanes."""
  3. if torch.is_grad_enabled() and (input.requires_grad or phony.requires_grad):
  4. input = Join.apply(input, phony)
  5. return input
  6. class Join(torch.autograd.Function):
  7. @staticmethod
  8. def forward(ctx: 'Join', input: Tensor, phony: Tensor) -> Tensor: # type: ignore
  9. return input.detach()
  10. @staticmethod
  11. def backward(ctx: 'Join', grad_input: Tensor) -> Tuple[Tensor, None]: # type: ignore
  12. return grad_input, None

2.2.4 Phony

Phony是没有空间的张量,因为它不需要任何梯度累积,所以可以在 autograd 图中构建任意的依赖。

  1. def get_phony(device: torch.device, *, requires_grad: bool) -> Tensor:
  2. """Gets a phony. Phony is tensor without space. It is useful to make
  3. arbitrary dependency in a autograd graph because it doesn't require any
  4. gradient accumulation.
  5. .. note::
  6. Phonies for each device are cached. If an autograd function gets a phony
  7. internally, the phony must be detached to be returned. Otherwise, the
  8. autograd engine will mutate the cached phony in-place::
  9. class Phonify(torch.autograd.Function):
  10. @staticmethod
  11. def forward(ctx, input):
  12. phony = get_phony(input.device, requires_grad=False)
  13. return phony.detach() # detach() is necessary.
  14. """
  15. key = (device, requires_grad)
  16. try:
  17. phony = _phonies[key]
  18. except KeyError:
  19. with use_stream(default_stream(device)):
  20. phony = torch.empty(0, device=device, requires_grad=requires_grad)
  21. _phonies[key] = phony
  22. return phony

2.2.5 detach

在代码中,经常可以见到 detach 的使用,这个从注释可以看出来,是为了解决 PyTorch 的一个bug。

  1. # A Python autograd function might fail with this error:
  2. #
  3. # RuntimeError: Returning Variables sharing storage with other Variables
  4. # that require grad is not supported in Python functions. Please submit a
  5. # feature request if you hit this error.
  6. #
  7. # It doesn't look like an essential restriction. But it happens on the
  8. # current PyTorch version. To avoid it, we should detach the tensor before
  9. # returning by identity autograd functions, such as Wait, Fork, and Join.
  10. #

2.3 使用

在 Pipeline 之中我们可以看到具体的使用方法,fence 方法(省略部分代码)利用 depend 来构建后向传播的依赖关系,确保 batches[i-1] 在 batches[i] 之后完成。

  1. def fence(self,
  2. schedule: List[Tuple[int, int]],
  3. skip_trackers: List[SkipTrackerThroughPotals],
  4. ) -> None:
  5. """Copies micro-batches after computation for the previous
  6. micro-batches.
  7. """
  8. batches = self.batches
  9. copy_streams = self.copy_streams
  10. skip_layout = self.skip_layout
  11. for i, j in schedule:
  12. # Ensure that batches[i-1] is executed after batches[i] in
  13. # backpropagation by an explicit dependency.
  14. if i != 0:
  15. depend(batches[i-1], batches[i]) # 在这里建立了后向传播依赖关系
  16. next_stream = copy_streams[j][i]
  17. for prev_j, ns, name in skip_layout.copy_policy(j):
  18. prev_stream = copy_streams[prev_j][i]
  19. skip_trackers[i].copy(batches[i], prev_stream, next_stream, ns, name)
  20. if j != 0:
  21. prev_stream = copy_streams[j-1][i]
  22. copy(batches[i], prev_stream, next_stream)

具体 depend 代码如下:

  1. def depend(fork_from: Batch, join_to: Batch) -> None:
  2. fork_from[0], phony = fork(fork_from[0])
  3. join_to[0] = join(join_to[0], phony)

我们结合示例代码把传入的参数赋值一下,重新把方法解释如下,这样大家就可以更好的理解。

  1. def depend(batches[i-1]: Batch, batches[i]: Batch) -> None:
  2. batches[i-1][0], phony = fork(batches[i-1][0])
  3. batches[i][0] = join(batches[i][0], phony)

具体逻辑如下,通过 phony 完成了一个桥接,即在正向传播之中,batches[i] 依赖 batches[i-1] 的执行结果

  1. +----------------+ +--------------+
  2. | | | |
  3. | batches[i-1] | | batches[i] |
  4. | | | |
  5. +----------+-----+ +-----+--------+
  6. | |
  7. | |
  8. | |
  9. v v
  10. +--------------------------------------------------------+
  11. | depend | | |
  12. | | | |
  13. | | | |
  14. | v | |
  15. | +-----------------------+ | |
  16. | | fork | | | |
  17. | | | get_phony | | |
  18. | | | + | | |
  19. | | | | | | |
  20. | | | | | | |
  21. | +-----------------------+ | |
  22. | | | | |
  23. | | | | |
  24. | | | | |
  25. | v v | |
  26. | +-----------+--+ +--+-----+ | |
  27. | | | | | | |
  28. | | batches[i-1] | | phony | | |
  29. | | | | | | |
  30. | +--------------+ +--+-----+ | |
  31. | | | |
  32. | | | |
  33. | v v |
  34. | +--+------------------+ |
  35. | |Join | | |
  36. | | | | |
  37. | | | | |
  38. | | v | |
  39. | +---------------------+ |
  40. | | |
  41. | | |
  42. | | |
  43. | v |
  44. | +-----+------+ |
  45. | | | |
  46. | | batches[i] | |
  47. | | | |
  48. | +------------+ |
  49. | |
  50. +--------------------------------------------------------+

我们把多个 batches 联合起来看看,这样就能看出来一个依赖链条。

  1. +----------------------------------------------------------+
  2. | depend |
  3. | |
  4. | +------------+ |
  5. +------------- | |fork | +-----------+ |
  6. | | | | | | | |
  7. |batches[i] +----------------------> | batches[i]| |
  8. | | | | | | | |
  9. +------------- | | | +-----------+ |
  10. | | | +-------+ |
  11. | | +-----------> | Join | |
  12. | | | | | |
  13. | +------------+ | | |
  14. +------------- | | | +--------------+ |
  15. | | | | | | | |
  16. |batches[i+1]+-------------------------------------------->+ batches[i+1] | |
  17. | | | | | | | |
  18. +---------+--- | | | +--------------+ |
  19. | | +-------+ |
  20. | | |
  21. | +----------------------------------------------------------+
  22. | +----------------------------------------------------------+
  23. | | depend |
  24. | | |
  25. | | +-------------+ |
  26. | | |fork | +------------+ |
  27. | | | | | | |
  28. +--------------------------> |batches[i+1]| |
  29. | | | | | |
  30. | | | +------------+ |
  31. | | | +-------+ |
  32. | | +---------> |Join | |
  33. | +-------------+ | | |
  34. +------------+ | | | +-------------+ |
  35. | | | | | | | |
  36. |batches[i+2]+--------------------------------------------> | batches[i+2]| |
  37. | | | | | | | |
  38. +----------+-+ | | | +-------------+ |
  39. | | +-------+ |
  40. | | |
  41. | +----------------------------------------------------------+
  42. |
  43. | +-----------------------------------------------------------+
  44. | | depend |
  45. | | |
  46. +-----------------------------> ...... |
  47. | |
  48. | |
  49. +-----------------------------------------------------------+

这样,上图就是前向计算图,于是在后向传播之中,batches[i] 就 必须在 batches[i-1] 之前完成了

我们再结合论文的图来看看。

本来示例代码中是:

  1. depend(batches[i-1], batches[i])

为了和论文中的图对应,我们修改为:

  1. depend(batches[i], batches[i+1])

depend 代码也变化为:

  1. def depend(batches[i]: Batch, batches[i+1]: Batch) -> None:
  2. batches[i][0], phony = fork(batches[i][0])
  3. batches[i+1][0] = join(batches[i+1][0], phony)

对应下图,就是在后向传播计算图之中 batches[i+1] 通过一个join, 一个fork,排在了 batches[i] 前面,就是下面大箭头所示,具体细化一下:

  • 从这个图上,PyTorch 的 autograd 引擎不知道 \(B_{i+1,j}\) 必须在 \(B_{i,j}\) 之前运行,因此会打乱后向传播的时间流。因此,虚拟依赖(前面图的虚线箭头)必须在前向传播中被显式绘制出来。

  • 图上的实线箭头依据后向传播图的方向来绘制,这些联系是在前向传播中被构建的。就是说,对于 \({Batch}_i\) 来说,其反向传播顺序是固定的。就是上面一行内顺序是固定的,下面一行内顺序也是固定的

  • 但是,上下两行之间的顺序是不可知的,需要用虚线来保证,就是用 Join & Fork 来保证。

0x03 正向传播依赖

我们回头再来看正向依赖。因为正向传播的部分目的就是完成反向传播依赖,而目前反向传播只完成了行之间的依赖,列之间的依赖没有完成,我们现在补全

列之间的依赖就是设备之间的依赖,即前一个设备的输出是后一个设备的输入

3.1 分割模型

首先还是需要回顾下如何切分模型,从 split_module 可以看到,

GPipe 的 partitions 成员变量是 nn.ModuleList 类型。nn.ModuleList是一个容器,其储存不同 module,并自动将每个 module 的 parameters 添加到网络中。但是nn.ModuleList 并没有定义一个网络,而只是将不同的模块储存在一起,这些模块之间并没有什么先后顺序,网络的执行顺序是根据 forward 函数来决定的。

  1. def split_module(module: nn.Sequential,
  2. balance: Iterable[int],
  3. devices: List[torch.device],
  4. ) -> Tuple[List[nn.Sequential], List[int], List[torch.device]]:
  5. balance = list(balance)
  6. j = 0
  7. partitions = []
  8. layers: NamedModules = OrderedDict()
  9. for name, layer in module.named_children(): # 遍历模型包含的层
  10. layers[name] = layer # 把新的层加入到数组中
  11. if len(layers) == balance[j]: # 如果数组大小等于balance[j],就是达到了device j应该包含的层数
  12. # Group buffered layers as a partition.
  13. partition = nn.Sequential(layers) # 把层数组组合成一个sequential module
  14. device = devices[j]
  15. partition.to(device) # 把层放置到相关设备之上
  16. partitions.append(partition) # 这个新module加入到分区数组中
  17. # Prepare for the next partition.
  18. layers.clear()
  19. j += 1 # 去下一个device看看
  20. partitions = cast(List[nn.Sequential], nn.ModuleList(partitions))
  21. del devices[j:]
  22. return partitions, balance, devices

随之而来问题就是:partition内部可以用Sequential来进行一系列的前向操作,但是如何配置partitions 之间的执行顺序?

  1. +-----------------------------------------------------------------------------------------+
  2. | |
  3. | Layer 1 +---> Layer 2 +-----> Layer 3 +-----> Layer 4 +-----> Layer 5 +---> Layer 6 |
  4. | |
  5. +-----------------------------------------+-----------------------------------------------+
  6. |
  7. |
  8. |
  9. v
  10. +-----------------------------------------------------------------------------------------+
  11. | +--------------------+ +---------------------+ +--------------------+ |
  12. | |Partition 1 | |Partition 2 | |Partition 3 | |
  13. | | | ??? | | | | |
  14. | | Layer 1 | +----------> Layer 4 | ??? | | |
  15. | | + | | | + | +-------> Layer 6 | |
  16. | | | | | | | | | | | |
  17. | | v | | | | | | | | |
  18. | | Layer 2 | | | | | | | | |
  19. | | + | | | v | | | | |
  20. | | | | | | Layer 5 +------------+ | | |
  21. | | v | | | | | | |
  22. | | Layer 3 +----------+ | | | | |
  23. | | | | | | | |
  24. | +--------------------+ +---------------------+ +--------------------+ |
  25. | |
  26. +-----------------------------------------------------------------------------------------+

3.2 建立依赖

我们还是从论文中入手。假定我们有一个神经网络,其由一系列子网络构成。我们假定这些子网络是 \(f^1,...,f^n\),其参数分别是 \(\theta^1,...,\theta^n\),则整个网络是:

参数是 \(\theta = (\theta^1,...,\theta^n)\),为了清楚起见,我们称 \(f^j\) 表示 f 的第 j 个分区,并假设分区的参数是相互不相交的。

在训练网络时,基于梯度的方法(如随机梯度下降法)需要在给定小批量训练数据 x 和相应损失之后,计算网络的输出结果f(x)。以及损失相对于网络参数 \(\theta\) 的梯度g。这两个阶段分别称为向前传播和向后传播。

既然 f 由其 L 层 子模块 (\(f^L, f^{L-1},...f^1\)) 顺序组成,那么前向传播\(f(x)\) 可以通过如下方式计算:让 \(x^0=x\)(就是输入x),然后顺序应用每一个 partition,即 \(x^j = f^j (x^{j-1})\),这里 $ j = 1, ..., L$。就是 \(f(x)\) 可以表示为 :

\[f(x) = f^L(f^{L-1}(f^{L-2}(... f^1(x))))
\]

于是我们知道了,前向传播的顺序是由 \(f(x) = f^L(f^{L-1}(f^{L-2}(... f^1(x))))\) 来确定的

我们可以针对代码,进一步解析,看看如何实施partitions之间的顺序依赖。

  1. def run(self) -> None:
  2. """Runs pipeline parallelism.
  3. It modifies the given batches in place.
  4. """
  5. batches = self.batches
  6. partitions = self.partitions
  7. devices = self.devices
  8. skip_layout = self.skip_layout
  9. m = len(batches)
  10. n = len(partitions)
  11. skip_trackers = [SkipTrackerThroughPotals(skip_layout) for _ in batches]
  12. with spawn_workers(devices) as (in_queues, out_queues):
  13. for schedule in clock_cycles(m, n): # 这里使用,给出了执行序列计划,后续按照这个来执行
  14. self.fence(schedule, skip_trackers)
  15. self.compute(schedule, skip_trackers, in_queues, out_queues)

解析的目标是 for schedule in clock_cycles(m, n) 这个 for 循环,其:

  • 针对clock_cycles产生的每一个运行计划:

    • 利用 fence(schedule, skip_trackers) 构建后向传播依赖关系。
    • 利用 compute(schedule, skip_trackers, in_queues, out_queues) 进行计算。

现在我们完成了两步:

  1. 确定性时钟周期算法给定了前向传播的执行顺序,我们只要按照 clock_cycles 方法提供的计划一一运行即可
  2. fence 方法通过调用 join 和 fork,我们做到了在后向传播之中,batches[i] 就 必须在 batches[i-1] 之前完成了,即 \(B_{i+1,j}\) 必须在 \(B_{i,j}\) 之前运行。

对于我们的图来说,第二步就是完成了下图的列依赖。

我们的问题是:怎么通过这个 for 循环,做到 \(B_{i,{j+1}}\) 必须在 \(B_{i,j}\) 之前运行?,即怎么安排反向传播逐次运行?就是怎么完成行内的依赖?

这就要通过 compute 的源码进行分析。重点说明的是:

  • batches[i] 这里是会变化的,比如 batches[0] 在经过 partitions[j] 的计算之后,会变成 batches[0][j]
  • 对于 compute 方法,关键就是在最底部的代码 batches[i] = batch。就是把 第 j 个device 对 第 i 个 batch 的计算结果 赋值到 batches[i],赋值之后,batches[i]就是 batches[i][j],这样,在下次计算时候,构建的就是 F[i, j+1], 下一次 fence 之中的 depend 操作,就是针对 batches[i, j+1]
  • 因此,在前向计算图上,通过这个赋值操作, batches[i, j+1] 就依赖 batches[i, j],所以反向计算时候,batches[i, j + 1] 就必须在 batches[i, j] 之前完成
  1. def compute(self,
  2. schedule: List[Tuple[int, int]],
  3. skip_trackers: List[SkipTrackerThroughPotals],
  4. in_queues: List[InQueue],
  5. out_queues: List[OutQueue],
  6. ) -> None:
  7. """Runs tasks with synchronization to copy streams."""
  8. batches = self.batches
  9. partitions = self.partitions
  10. devices = self.devices
  11. n = len(partitions)
  12. streams = [current_stream(d) for d in devices]
  13. for i, j in schedule: # 针对 schedule 之中的每一对 i,j
  14. batch = batches[i]
  15. partition = partitions[j]
  16. # Synchronize with the copied input. ([1] in the diagram)
  17. # Determine whether checkpointing or not.
  18. if checkpoint:
  19. # 忽略
  20. else:
  21. def compute(batch: Batch = batch,
  22. partition: nn.Sequential = partition,
  23. skip_tracker: SkipTrackerThroughPotals = skip_trackers[i],
  24. ) -> Batch:
  25. with use_skip_tracker(skip_tracker):
  26. return batch.call(partition) # 前向计算,计算以 partition为单位计算,partition内部的层是顺序计算,由 Sequential保证。
  27. task = Task(streams[j], compute=compute, finalize=None)
  28. del compute
  29. # Compute tasks in parallel. ([2] in the diagram)
  30. in_queues[j].put(task) # 让 worker计算
  31. for i, j in schedule:
  32. ok, payload = out_queues[j].get() # 获取 worker 的前向计算结果,就是 第 j 个device 对 第 i 个 batch 的计算结果
  33. task, batch = cast(Tuple[Task, Batch], payload)
  34. # The copy stream synchronizes to copy the output. ([3] in the
  35. # diagram)
  36. # Finalize tasks. If checkpointing is enabled, here the
  37. # recomputation is scheduled at backpropagation. ([4] in the
  38. # diagram)
  39. # 第 j 个device 对 第 i 个 batch 的计算 就是 F[i,j]
  40. batches[i] = batch # 这里是关键,就是把 第 j 个device 对 第 i 个 batch 的计算结果 赋值到 batches[i],batches[i]就是 batches[i][j],在下次计算时候,构建的就是 F[i,j+1], 下一次 fence 之中的 depend 操作,就是针对 batches[i,j+1]

关于这个赋值操作,其对应的grad_fn 是 PermuteBackward,比如:

  1. a = torch.tensor([2., 3.], requires_grad=True)
  2. c = a
  3. c.backward(gradient=external_grad)
  4. print(c)

具体是:

  1. c = {Tensor: 2} tensor([2., 3.], requires_grad=True)
  2. T = {Tensor: 2} tensor([2., 3.], grad_fn=<PermuteBackward>)

现在,我们把下图进行升级。

  1. +-------------------------------------------------------------------+
  2. | depend |
  3. | |
  4. | +---------------+ |
  5. | |fork | |
  6. +------------- | | | +-----------+ |
  7. | | | | | | | |
  8. |batches[i] +-------------------------> | batches[i]| |
  9. | | | | | | | |
  10. +------------- | | | +-----------+ |
  11. | | | |
  12. | | | |
  13. | | | +--------+ +-------+ |
  14. | | get_phony +------> | +--->+ Join | |
  15. | | | | phony | | | |
  16. | +---------------+ | | | | |
  17. | +--------+ | | |
  18. | | | |
  19. +------------- | | | +--------------+ |
  20. | | | | | | | |
  21. |batches[i+1]+----------------------------------------------------->+ batches[i+1] | |
  22. | | | | | | | |
  23. +------------- | | | +--------------+ |
  24. | +-------+ |
  25. | |
  26. +-------------------------------------------------------------------+

我们进行横向拓展,得到如下,即一个batch 被分成两个小批次: batches[i],batches[i+1] ,它们在两个设备 partitions[j],partitions[j + 1] 之上流水线,这样行和列都有反向传播的依赖。

  1. F[i,j] F[i,j+1]
  2. +------------------------------------------------+ +-----------------------------------------------+
  3. | partitions[j] | | partitions[j+1] |
  4. | | | |
  5. | +--------------------+ +------------------+ | | +-------------------+ +------------------+ |
  6. | |fence | | compute | | | | fence | | compute | |
  7. | | | | | | | | | | | |
  8. +--------------+ | | +--------------+ | | +------------+ | | +-----------------+ | | +-------------+ | | +------------+ | | +-----------------+
  9. | | | | | depend | | | |forward | | | | | | | | depend | | | |forward | | | | |
  10. | batches[i] +---------------------------------------------------------> | batches[i][j] +----------------------------------------------------------> | batches[i][j+1] |
  11. | | | | | | | | | | | | | | | | | | | | | | | | | |
  12. +--------------+ | | | | | | | | | | +-----------------+ | | | | | | | | | | +-----------------+
  13. | | | | | | +------------+ | | | | | | | | +------------+ | |
  14. | | | | | | | | | | | | | | | |
  15. +--------------+ | | | | | +------------------+ | +-----------------+ | | | | | +------------------+ | +-------------------+
  16. | | | | | | | | | | | | | | | | | |
  17. | batches[i+1]+---------------------------------------------------------> | batches[i+1][j] +----------------------------------------------------------> | batches[i+1][j+1] |
  18. | | | | | | | | | | | | | | | | | |
  19. +--------------+ | | +--------------+ | | +-----------------+ | | +-------------+ | | +-------------------+
  20. | | | | | | | |
  21. | +--------------------+ | | +-------------------+ |
  22. +------------------------------------------------+ +-----------------------------------------------+

手机如下:

0x04 总结

下图 $ m = 4, n = 3$。即,模型被分成3个子网络,小批次被分割成 4个微批次。F 和 B 的下标是 (m, n)。

如上图,这里需要完成两种依赖:

  • 行间依赖,就是 batch 之间的依赖,就是设备内的依赖。从图上看是虚线,就是 \(F_{1,1}\) 必须在 \(F_{2,1}\)之前完成,\(B_{2,1}\) 必须在\(B_{1,1}\) 之前完成。
  • 列间依赖,就是 partitions(设备) 之间的依赖。从图上看是实线,就是 \(F_{1,1}\) 必须在 \(F_{1,2}\)之前完成,即第一个设备必须在第二个设备之前完成,而且第一个设备的输出是第二个设备的输入。

如上图,我们需要完成行,列两方面的依赖。

  • 行间依赖是用 Join & Fork 来保证,利用空张量完成了依赖关系的设定,确保 batches[i-1] 在 batches[i] 之后完成。
  • 列间依赖是通过 batches[i] = batch 完成,利用 PermuteBackward 来完成了设备之间的依赖。

至此,我们完成了执行顺序和依赖关系的设定,下一篇我们介绍如何并行处理。

0xFF 参考

Markdown公式用法大全

markdown中公式编辑教程

https://docs.nvidia.com/cuda/cuda-runtime-api/stream-sync-behavior.html#stream-sync-behavior

CUDA学习:基础知识小结

CUDA随笔之Stream的使用

NVIDIA解决方案架构师深度解析大规模参数语言模型Megatron-BERT

Accelerating Wide & Deep Recommender Inference on GPUs

HugeCTR: High-Performance Click-Through Rate Estimation Training

https://discuss.pytorch.org/t/how-to-prefetch-data-when-processing-with-gpu/548

https://github.com/NVIDIA/apex/

https://github.com/justheuristic/prefetch_generator

https://pytorch.org/tutorials/intermediate/model_parallel_turotial.html

https://pytorch.org/docs/stable/autograd.html

https://pytorch.org/docs/notes/cuda.html

https://zhuanlan.zhihu.com/p/61765561

https://pytorch.apachen.org/docs/1.7/64.html

https://zhidx.com/p/217999.html

[源码解析] PyTorch 流水线并行实现 (5)--计算依赖的更多相关文章

  1. [源码解析] PyTorch 流水线并行实现 (6)--并行计算

    [源码解析] PyTorch 流水线并行实现 (6)--并行计算 目录 [源码解析] PyTorch 流水线并行实现 (6)--并行计算 0x00 摘要 0x01 总体架构 1.1 使用 1.2 前向 ...

  2. [源码解析] PyTorch 流水线并行实现 (1)--基础知识

    [源码解析] PyTorch 流水线并行实现 (1)--基础知识 目录 [源码解析] PyTorch 流水线并行实现 (1)--基础知识 0x00 摘要 0x01 历史 1.1 GPipe 1.2 t ...

  3. [源码解析] PyTorch 流水线并行实现 (2)--如何划分模型

    [源码解析] PyTorch 流水线并行实现 (2)--如何划分模型 目录 [源码解析] PyTorch 流水线并行实现 (2)--如何划分模型 0x00 摘要 0x01 问题 0x01 自动平衡 1 ...

  4. [源码解析] PyTorch 流水线并行实现 (3)--切分数据和运行时系统

    [源码解析] PyTorch 流水线并行实现 (3)--切分数据和运行时系统 目录 [源码解析] PyTorch 流水线并行实现 (3)--切分数据和运行时系统 0x00 摘要 0x01 分割小批次 ...

  5. [源码解析] PyTorch 流水线并行实现 (4)--前向计算

    [源码解析] PyTorch 流水线并行实现 (4)--前向计算 目录 [源码解析] PyTorch 流水线并行实现 (4)--前向计算 0x00 摘要 0x01 论文 1.1 引论 1.1.1 数据 ...

  6. [源码解析] PyTorch分布式优化器(3)---- 模型并行

    [源码解析] PyTorch分布式优化器(3)---- 模型并行 目录 [源码解析] PyTorch分布式优化器(3)---- 模型并行 0x00 摘要 0x01 前文回顾 0x02 单机模型 2.1 ...

  7. [源码解析] PyTorch 分布式(18) --- 使用 RPC 的分布式管道并行

    [源码解析] PyTorch 分布式(18) --- 使用 RPC 的分布式管道并行 目录 [源码解析] PyTorch 分布式(18) --- 使用 RPC 的分布式管道并行 0x00 摘要 0x0 ...

  8. [源码解析] PyTorch分布式优化器(2)----数据并行优化器

    [源码解析] PyTorch分布式优化器(2)----数据并行优化器 目录 [源码解析] PyTorch分布式优化器(2)----数据并行优化器 0x00 摘要 0x01 前文回顾 0x02 DP 之 ...

  9. [源码解析] PyTorch 分布式(1) --- 数据加载之DistributedSampler

    [源码解析] PyTorch 分布式(1) --- 数据加载之DistributedSampler 目录 [源码解析] PyTorch 分布式(1) --- 数据加载之DistributedSampl ...

随机推荐

  1. Linux命令全训练

    一.实验目的 为后续上机实验做准备,熟悉常用的Linux操作. 二.实验平台 操作系统:Ubuntu14.04 三.实验内容和要求 1.Linux系统中的常用快捷键 Tab 自动补全 上下箭头 显示历 ...

  2. java8 lambda表达式和函数式编程

    什么是函数式接口(Functional Interface) 其实之前在讲Lambda表达式的时候提到过,所谓的函数式接口,当然首先是一个接口,然后就是在这个接口里面只能有一个抽象方法 (可以有def ...

  3. 使用dom4j工具:获得文本内容(四)

    package dom4j_read; import java.io.File; import org.dom4j.Document; import org.dom4j.Element; import ...

  4. Spring boot中注册Servlet

    Spring boot中注册Servlet 如何在spring boot项目中注册Servlet呢? 如何在spring boot项目中注册Servlet呢? 由于没有web.xml,无法直接在xml ...

  5. python 中最好用的身份证规则解析工具,地区码、性别、出生年月、身份证编码等快速校验!

    安装并导入依赖库 # pip install parseIdCard from parseIdCard import parseIdCard from pprint import pprint 地区码 ...

  6. Mysql常用sql语句(6)- limit 限制查询结果的条数

    测试必备的Mysql常用sql语句系列 https://www.cnblogs.com/poloyy/category/1683347.html 前言 实际工作中,我们的数据表数据肯定都是万级别的,如 ...

  7. Golang入门学习(四):常用的函数汇总

    文章目录 2.4 常用的内置函数 2.4.1 字符串常用内置函数 2.4.2 常用的时间和日期相关函数 2.4.3 内置函数 2.4 常用的内置函数 2.4.1 字符串常用内置函数 https://g ...

  8. GO安装golang.org/x/net扩展库

    在学习golang过程中,有部分示例代码使用到了非标准库golang.org/x/net/html相关的库函数,但是标准代码库中没有该库,因此需要自己安装: 我这里使用git下载源码进行的安装. 为了 ...

  9. 推荐一款编程字体:Iosevka

    最近发现一款很好用的编程字体:Iosevka.它是一款现代化的编程字体集合,除了等宽.oO0 iIl1明显区分等基本特性外,还有很多非常现代的特性,比如: 多种风格:有非常多的字形可供选择,衬线/非衬 ...

  10. Linux学习笔记--快捷键

    桌面 ALT+空格 打开窗口菜单 ALT+F1 聚焦到桌面左侧任务导航栏,可按上下键导航 ALT+F2     运行命令 ALT+F4 关闭窗口 ALT+TAB 切换程序窗口 PRINT 桌面截图 S ...