第13章計算の性能
13.5.5. Distributing Data13.5.5 データを配る
We need a simple utility function to distribute a minibatch evenly across multiple GPUs. For instance, on two GPUs we would like to have half of the data to be copied to either of the GPUs. Since it is more convenient and more concise, we use the built-in function from the deep learning framework to try it out on a matrix.
英語のヒント
ミニバッチを複数のGPUに均等に配るための単純なユーティリティ関数が要る。たとえば、二つのGPUでは、データの半分をそれぞれのGPUにコピーしたい。そのほうが便利で簡潔なので、深層学習のフレームワークの組み込み関数を使い、の行列で試してみる。
data = torch.arange(20).reshape(4, 5)
devices = [torch.device('cuda:0'), torch.device('cuda:1')]
split = nn.parallel.scatter(data, devices)
print('input :', data)
print('load into', devices)
print('output:', split)input : tensor([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19]])
load into [device(type='cuda', index=0), device(type='cuda', index=1)]
output: (tensor([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9]], device='cuda:0'), tensor([[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19]], device='cuda:1'))For later reuse we define a
split_batch function that splits both data and labels.英語のヒント
後で再利用するため、データとラベルの両方を分ける
split_batch関数を定義する。#@save
def split_batch(X, y, devices):
"""Split `X` and `y` into multiple devices."""
assert X.shape[0] == y.shape[0]
return (nn.parallel.scatter(X, devices),
nn.parallel.scatter(y, devices))