プリンキピア

第6章構築者のための手引き

6.7.2. Tensors and GPUs6.7.2 テンソルとGPU

By default, tensors are created on the CPU. We can query the device where the tensor is located.
英語のヒント
既定では、テンソルはCPU上に作られる。テンソルが置かれているデバイスを問い合わせられる。
x = torch.tensor([1, 2, 3])
x.device
device(type='cpu')
It is important to note that whenever we want to operate on multiple terms, they need to be on the same device. For instance, if we sum two tensors, we need to make sure that both arguments live on the same device—otherwise the framework would not know where to store the result or even how to decide where to perform the computation.
英語のヒント
複数の項に演算を行いたいときには、それらが同じデバイス上にある必要があることに注意するのが大切である。たとえば、二つのテンソルの和をとるなら、両方の引数が同じデバイス上にあることを確かめる必要がある。さもないと、フレームワークは結果をどこに保存すればよいか、さらには計算をどこで行うかをどう決めればよいかもわからないだろう。

6.7.2.1. Storage on the GPU6.7.2.1 GPU上の保存

There are several ways to store a tensor on the GPU. For example, we can specify a storage device when creating a tensor. Next, we create the tensor variable X on the first gpu. The tensor created on a GPU only consumes the memory of this GPU. We can use the nvidia-smi command to view GPU memory usage. In general, we need to make sure that we do not create data that exceeds the GPU memory limit.
英語のヒント
GPU上にテンソルを保存する方法はいくつかある。たとえば、テンソルを作るときに保存先のデバイスを指定できる。次に、最初のgpu上にテンソル変数Xを作る。GPU上に作られたテンソルは、そのGPUのメモリだけを消費する。nvidia-smiコマンドを使って、GPUのメモリ使用量を見られる。一般に、GPUのメモリの上限を超えるデータを作らないようにする必要がある。
X = torch.ones(2, 3, device=try_gpu())
X
tensor([[1., 1., 1.],
        [1., 1., 1.]], device='cuda:0')
Assuming that you have at least two GPUs, the following code will create a random tensor, Y, on the second GPU.
英語のヒント
少なくとも二つのGPUがあるとすると、次のコードは2番目のGPU上に無作為なテンソルYを作る。
Y = torch.rand(2, 3, device=try_gpu(1))
Y
tensor([[0.0022, 0.5723, 0.2890],
        [0.1456, 0.3537, 0.7359]], device='cuda:1')

6.7.2.2. Copying6.7.2.2 コピー

If we want to compute X + Y, we need to decide where to perform this operation. For instance, as shown in Fig. 6.7.1, we can transfer X to the second GPU and perform the operation there. Do not simply add X and Y, since this will result in an exception. The runtime engine would not know what to do: it cannot find data on the same device and it fails. Since Y lives on the second GPU, we need to move X there before we can add the two.
英語のヒント
X + Yを計算したいなら、この演算をどこで行うかを決める必要がある。たとえば、図6.7.1に示すように、Xを2番目のGPUに転送し、そこで演算を行える。単純にXとYを足してはいけない。例外が起きるからである。実行時のエンジンは、どうすればよいかわからないだろう。同じデバイス上にデータが見つからず、失敗するのである。Yは2番目のGPU上にあるので、二つを足す前にXをそこへ移す必要がある。
Fig. 6.7.1図6.7.1
Copy data to perform an operation on the same device.
英語のヒント
同じデバイス上で演算を行うためにデータをコピーする。
Z = X.cuda(1)
print(X)
print(Z)
tensor([[1., 1., 1.],
        [1., 1., 1.]], device='cuda:0')
tensor([[1., 1., 1.],
        [1., 1., 1.]], device='cuda:1')
Now that the data (both Z and Y) are on the same GPU), we can add them up.
英語のヒント
データ(ZとYの両方)が同じGPU上にあるので、足し合わせられる。
Y + Z
tensor([[1.0022, 1.5723, 1.2890],
        [1.1456, 1.3537, 1.7359]], device='cuda:1')
But what if your variable Z already lived on your second GPU? What happens if we still call Z.cuda(1)? It will return Z instead of making a copy and allocating new memory.
英語のヒント
しかし、変数Zがすでに2番目のGPU上にあったらどうなるか。それでもZ.cuda(1)を呼ぶと何が起きるか。コピーを作って新しいメモリを割り当てる代わりに、Zを返すのである。
Z.cuda(1) is Z
True

6.7.2.3. Side Notes6.7.2.3 補足

People use GPUs to do machine learning because they expect them to be fast. But transferring variables between devices is slow: much slower than computation. So we want you to be 100% certain that you want to do something slow before we let you do it. If the deep learning framework just did the copy automatically without crashing then you might not realize that you had written some slow code.
英語のヒント
人々が機械学習にGPUを使うのは、速いことを期待するからである。しかし、デバイスのあいだで変数を転送するのは遅い。計算よりずっと遅いのである。そのため、遅いことをする前に、本当にそうしたいのだと100%確信してもらってから、それをさせたい。深層学習のフレームワークが、落ちることなく自動でコピーをしてしまったら、遅いコードを書いてしまったことに気づかないかもしれない。
Transferring data is not only slow, it also makes parallelization a lot more difficult, since we have to wait for data to be sent (or rather to be received) before we can proceed with more operations. This is why copy operations should be taken with great care. As a rule of thumb, many small operations are much worse than one big operation. Moreover, several operations at a time are much better than many single operations interspersed in the code unless you know what you are doing. This is the case since such operations can block if one device has to wait for the other before it can do something else. It is a bit like ordering your coffee in a queue rather than pre-ordering it by phone and finding out that it is ready when you are.
英語のヒント
データの転送は遅いだけでなく、並列化をずっと難しくもする。さらに演算を進める前に、データが送られる(というより受け取られる)のを待たなければならないからである。だからこそ、コピーの演算は細心の注意を払って行うべきなのである。経験則として、小さな演算がたくさんあるのは、大きな演算が一つあるよりずっと悪い。さらに、自分のしていることがわかっているのでないかぎり、一度にいくつかの演算をまとめて行うほうが、コードのあちこちに一つずつの演算をたくさんばらまくよりずっとよい。そうなるのは、一方のデバイスがほかのことをする前にもう一方を待たなければならないと、そうした演算が詰まってしまうことがあるからである。これは少し、電話で前もって注文しておき、着いたときにはできあがっているとわかる代わりに、列に並んでコーヒーを注文するようなものである。
Last, when we print tensors or convert tensors to the NumPy format, if the data is not in the main memory, the framework will copy it to the main memory first, resulting in additional transmission overhead. Even worse, it is now subject to the dreaded global interpreter lock that makes everything wait for Python to complete.
英語のヒント
最後に、テンソルを表示したり、テンソルをNumPy形式に変換したりするとき、データがメインメモリになければ、フレームワークはまずそれをメインメモリにコピーするので、余分な転送の負担が生じる。さらに悪いことに、それは恐ろしいグローバルインタプリタロックの影響を受け、すべてがPythonの処理が終わるのを待たされることになる。