第13章計算の性能
13.3.2. Parallel Computation and Communication13.3.2 並列の計算と通信
In many cases we need to move data between different devices, say between the CPU and GPU, or between different GPUs. For instance, this occurs when we want to perform distributed optimization where we need to aggregate the gradients over multiple accelerator cards. Let’s simulate this by computing on the GPU and then copying the results back to the CPU.
英語のヒント
多くの場合、CPUとGPUのあいだや、異なるGPUのあいだのように、異なる装置のあいだでデータを動かす必要がある。たとえばこれは、複数のアクセラレータカードにわたって勾配を集める必要がある分散最適化を行いたいときに起こる。GPUで計算し、その結果をCPUにコピーして戻すことで、これをシミュレートしよう。
def copy_to_cpu(x, non_blocking=False):
return [y.to('cpu', non_blocking=non_blocking) for y in x]
with d2l.Benchmark('Run on GPU1'):
y = run(x_gpu1)
torch.cuda.synchronize()
with d2l.Benchmark('Copy to CPU'):
y_cpu = copy_to_cpu(y)
torch.cuda.synchronize()Run on GPU1: 0.4656 sec
Copy to CPU: 2.3125 secThis is somewhat inefficient. Note that we could already start copying parts of
y to the CPU while the remainder of the list is still being computed. This situation occurs, e.g., when we compute the (backprop) gradient on a minibatch. The gradients of some of the parameters will be available earlier than that of others. Hence it works to our advantage to start using PCI-Express bus bandwidth while the GPU is still running. In PyTorch, several functions such as to() and copy_() admit an explicit non_blocking argument, which lets the caller bypass synchronization when it is unnecessary. Setting non_blocking=True allows us to simulate this scenario.英語のヒント
これはいくらか効率が悪い。リストの残りがまだ計算されているあいだに、
yの一部をCPUにコピーし始められることに注意してほしい。この状況は、たとえばミニバッチについて(逆伝播の)勾配を計算するときに起こる。一部のパラメータの勾配は、ほかのものより早く使えるようになる。したがって、GPUがまだ動いているあいだにPCI-Expressバスの帯域を使い始めるのが有利に働く。PyTorchでは、to()やcopy_()のようないくつかの関数が明示的なnon_blocking引数を受け付け、それにより呼び出し側は、不要なときに同期を省ける。non_blocking=Trueと設定すると、このシナリオをシミュレートできる。with d2l.Benchmark('Run on GPU1 and copy to CPU'):
y = run(x_gpu1)
y_cpu = copy_to_cpu(y, True)
torch.cuda.synchronize()Run on GPU1 and copy to CPU: 1.6907 secThe total time required for both operations is (as expected) less than the sum of their parts. Note that this task is different from parallel computation as it uses a different resource: the bus between the CPU and GPUs. In fact, we could compute on both devices and communicate, all at the same time. As noted above, there is a dependency between computation and communication:
y[i] must be computed before it can be copied to the CPU. Fortunately, the system can copy y[i-1] while computing y[i] to reduce the total running time.英語のヒント
両方の演算に必要な全体の時間は、(予想どおり)各部分の和より短い。このタスクは、異なる資源、つまりCPUとGPUのあいだのバスを使うので、並列計算とは異なることに注意してほしい。実際、両方の装置で計算しながら通信することを、すべて同時に行える。上で述べたように、計算と通信のあいだには依存がある。
y[i]はCPUにコピーされる前に計算されなければならない。幸い、システムはy[i]を計算しながらy[i-1]をコピーして、全体の実行時間を減らせる。We conclude with an illustration of the computational graph and its dependencies for a simple two-layer MLP when training on a CPU and two GPUs, as depicted in Fig. 13.3.1. It would be quite painful to schedule the parallel program resulting from this manually. This is where it is advantageous to have a graph-based computing backend for optimization.
英語のヒント
最後に、図13.3.1に描いたように、CPUと二つのGPUで訓練するときの、単純な2層MLPの計算グラフとその依存を図示して締めくくる。ここから生じる並列プログラムを手でスケジュールするのは、かなりつらいだろう。ここで、最適化のためにグラフにもとづく計算のバックエンドをもつことが有利になる。
Fig. 13.3.1図13.3.1
The computational graph and its dependencies of a two-layer MLP on a CPU and two GPUs.
英語のヒント
CPUと二つのGPUでの2層MLPの計算グラフとその依存。