プリンキピア

第13章計算の性能

13.2.1. Asynchrony via Backend13.2.1 バックエンドによる非同期性

For a warmup consider the following toy problem: we want to generate a random matrix and multiply it. Let’s do that both in NumPy and in PyTorch tensor to see the difference. Note that PyTorch tensor is defined on a GPU.
英語のヒント
準備運動として、次のおもちゃの問題を考えよう。ランダムな行列を生成し、それを掛けたい。違いを見るため、NumPyとPyTorchのtensorの両方でそれを行おう。PyTorchのテンソルはGPU上で定義されていることに注意してほしい。
# Warmup for GPU computation
device = d2l.try_gpu()
a = torch.randn(size=(1000, 1000), device=device)
b = torch.mm(a, a)

with d2l.Benchmark('numpy'):
    for _ in range(10):
        a = numpy.random.normal(size=(1000, 1000))
        b = numpy.dot(a, a)

with d2l.Benchmark('torch'):
    for _ in range(10):
        a = torch.randn(size=(1000, 1000), device=device)
        b = torch.mm(a, a)
numpy: 1.4693 sec
torch: 0.0022 sec
The benchmark output via PyTorch is orders of magnitude faster. NumPy dot product is executed on the CPU processor while PyTorch matrix multiplication is executed on GPU and hence the latter is expected to be much faster. But the huge time difference suggests something else must be going on. By default, GPU operations are asynchronous in PyTorch. Forcing PyTorch to finish all computation prior to returning shows what happened previously: computation is being executed by the backend while the frontend returns control to Python.
英語のヒント
PyTorchによるベンチマークの出力は、桁違いに速い。NumPyの内積はCPUのプロセッサで実行され、PyTorchの行列の積はGPUで実行されるので、後者のほうがずっと速いことは予想される。しかし、この大きな時間の差は、何かほかのことが起きているに違いないことを示唆している。デフォルトでは、PyTorchではGPUの演算は非同期である。戻る前にすべての計算を終えるようPyTorchに強いると、前に何が起きていたかがわかる。フロントエンドがPythonに制御を返すあいだ、計算はバックエンドによって実行されているのである。
with d2l.Benchmark():
    for _ in range(10):
        a = torch.randn(size=(1000, 1000), device=device)
        b = torch.mm(a, a)
    torch.cuda.synchronize(device)
Done: 0.0058 sec
Broadly speaking, PyTorch has a frontend for direct interaction with the users, e.g., via Python, as well as a backend used by the system to perform the computation. As shown in Fig. 13.2.1, users can write PyTorch programs in various frontend languages, such as Python and C++. Regardless of the frontend programming language used, the execution of PyTorch programs occurs primarily in the backend of C++ implementations. Operations issued by the frontend language are passed on to the backend for execution. The backend manages its own threads that continuously collect and execute queued tasks. Note that for this to work the backend must be able to keep track of the dependencies between various steps in the computational graph. Hence, it is not possible to parallelize operations that depend on each other.
英語のヒント
大まかに言えば、PyTorchには、たとえばPythonを通じてユーザーと直接やりとりするためのフロントエンドと、システムが計算を行うのに使うバックエンドがある。図13.2.1に示すように、ユーザーはPythonやC++のようなさまざまなフロントエンドの言語でPyTorchのプログラムを書ける。使うフロントエンドのプログラミング言語にかかわらず、PyTorchのプログラムの実行は主にC++の実装のバックエンドで行われる。フロントエンドの言語が出した演算は、実行のためにバックエンドに渡される。バックエンドは自身のスレッドを管理し、それらはキューに入ったタスクを絶えず集めて実行する。これがうまくいくには、バックエンドが計算グラフのさまざまなステップのあいだの依存を追跡できなければならないことに注意してほしい。したがって、互いに依存する演算を並列化することはできない。
Image omitted because its reuse rights could not be verified.権利の確認ができないため画像を割愛。
Fig. 13.2.1図13.2.1
Programming language frontends and deep learning framework backends.
英語のヒント
プログラミング言語のフロントエンドと、深層学習のフレームワークのバックエンド。
Let’s look at another toy example to understand the dependency graph a bit better.
英語のヒント
依存のグラフをもう少しよく理解するため、別のおもちゃの例を見よう。
x = torch.ones((1, 2), device=device)
y = torch.ones((1, 2), device=device)
z = x * y + 2
z
tensor([[3., 3.]], device='cuda:0')
Fig. 13.2.2図13.2.2
The backend tracks dependencies between various steps in the computational graph.
英語のヒント
バックエンドは、計算グラフのさまざまなステップのあいだの依存を追跡する。
The code snippet above is also illustrated in Fig. 13.2.2. Whenever the Python frontend thread executes one of the first three statements, it simply returns the task to the backend queue. When the last statement’s results need to be printed, the Python frontend thread will wait for the C++ backend thread to finish computing the result of the variable z. One benefit of this design is that the Python frontend thread does not need to perform actual computations. Thus, there is little impact on the program’s overall performance, regardless of Python’s performance. Fig. 13.2.3 illustrates how frontend and backend interact.
英語のヒント
上のコード片は図13.2.2にも示されている。Pythonのフロントエンドのスレッドは、最初の三つの文のどれかを実行するたびに、単にタスクをバックエンドのキューに戻す。最後の文の結果を出力する必要があるとき、Pythonのフロントエンドのスレッドは、C++のバックエンドのスレッドが変数zの結果を計算し終えるのを待つ。この設計の利点の一つは、Pythonのフロントエンドのスレッドが実際の計算を行う必要がないことである。したがって、Pythonの性能にかかわらず、プログラム全体の性能への影響はほとんどない。図13.2.3は、フロントエンドとバックエンドがどうやりとりするかを示している。
Fig. 13.2.3図13.2.3
Interactions of the frontend and backend.
英語のヒント
フロントエンドとバックエンドのやりとり。