【PyTorch】torch.utils.bottleneck実行時のPicklingErrorの対処法 - 加賀百万石ですが何か?

【PyTorch】torch.utils.bottleneck実行時のPicklingErrorの対処法

前回の記事に書きましたが,PyTorchではボトルネック解析の機能があります。

実行方法は↓こんな感じです。

>python -m torch.utils.bottleneck /PATH/TO/PyTorch_SCRIPT.py

これを実行したときに,よく分からないエラーに遭遇しました。

とりあえず暫定的な対処方法は分かりましたので,記事に残しておきたいと思います。

実行環境

  • Windows環境(コマンドプロンプト上で実行)
  • PyTorch==0.4.1

エラーの内容

ログにPathが含まれている箇所がありましたので,途中は削除していますがこういうPicklingErrorです。

>python -m torch.utils.bottleneck pytorch_sample.py
`bottleneck` is a tool that can be used as an initial step for debugging
bottlenecks in your program.

It summarizes runs of your script with the Python profiler and PyTorch's
autograd profiler. Because your script will be profiled, please ensure that it
exits in a finite amount of time.

For more complicated uses of the profilers, please see
https://docs.python.org/3/library/profile.html and
http://pytorch.org/docs/master/autograd.html#profiler for more information.
Running environment analysis...
Running your script with cProfile
Device: cuda
CNN(
  (block1): Sequential(
    (0): Conv2d(3, 16, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (1): ReLU(inplace)
    (2): BatchNorm2d(16, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    (3): MaxPool2d(kernel_size=2, stride=1, padding=0, dilation=1, ceil_mode=False)
  )
  (block2): Sequential(
    (0): Conv2d(16, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (1): ReLU(inplace)
    (2): BatchNorm2d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    (3): MaxPool2d(kernel_size=2, stride=1, padding=0, dilation=1, ceil_mode=False)
  )
  (full_connection): Sequential(
    (0): Linear(in_features=21632, out_features=128, bias=True)
    (1): ReLU()
    (2): Dropout(p=0.5)
    (3): Linear(in_features=128, out_features=784, bias=True)
  )
)
Begin train
  0%|                                                             | 0/21 [00:00<?, ?it/s]Traceback (most recent call last):

(中略)

ForkingPickler(file, protocol).dump(obj)EOFError: Ran out of input

_pickle.PicklingError: Can't pickle <class '__main__.BatchLoader'>: attribute lookup BatchLoader on __main__ failed

※最後の行の”BatchLoader”というのは自作したclassです。

対処方法

  • DataLoaderを作成するときに使用するtorch.utils.data.DataLoader()が原因
  • 引数のnum_workersを0にするとエラーが解消した

DataLoaderを作成している部分は以下のようにしています。

この中のnum_workersの値を”0″にすると,とりあえずエラーは消えてボトルネックの解析結果が出力されました。

# Error occurs (num_workers > 0)
torch.utils.data.DataLoader(dataset=train_dataset,
                            batch_size=batch_size,
                            shuffle=True,
                            num_workers=4)

# Correct (num_workers = 0)
torch.utils.data.DataLoader(dataset=train_dataset,
                            batch_size=batch_size,
                            shuffle=True,
                            num_workers=0)

原因は正直よく分かりません…。

スポンサーリンク