M1 MacBookProにPyTorchをインストール

ついでなので、M1 MBPにPyTorchをインストールする。

https://towardsdatascience.com/installing-pytorch-on-apple-m1-chip-with-gpu-acceleration-3351dc44d67c

$conda create -n torch-gpu python=3.9
$ conda activate torch-gpu
$ conda install pytorch torchvision torchaudio -c pytorch-nightly

注意点としては、deviceに"mps"を設定すること。

device = torch.device("mps")

それと、まだM1でサポートしていない機能があること。

$ python test.py
[ 6 10 22 17 17]
torch.Size([64, 30, 27])
torch.Size([64, 5])
Traceback (most recent call last):
  File "/Users/hironobu/tmp/CNN+RNN/tmp/test.py", line 175, in <module>
    loss = criterion(out, target, input_lengths, target_lengths)
  File "/Users/hironobu/miniforge3/envs/torch-gpu/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1482, in _call_impl
    return forward_call(*args, **kwargs)
  File "/Users/hironobu/miniforge3/envs/torch-gpu/lib/python3.9/site-packages/torch/nn/modules/loss.py", line 1756, in forward
    return F.ctc_loss(log_probs, targets, input_lengths, target_lengths, self.blank, self.reduction,
  File "/Users/hironobu/miniforge3/envs/torch-gpu/lib/python3.9/site-packages/torch/nn/functional.py", line 2630, in ctc_loss
    return torch.ctc_loss(
NotImplementedError: The operator 'aten::_ctc_loss' is not currently implemented for the MPS device. If you want this op to be added in priority during the prototype phase of this feature, please comment on https://github.com/pytorch/pytorch/issues/77764. As a temporary fix, you can set the environment variable `PYTORCH_ENABLE_MPS_FALLBACK=1` to use the CPU as a fallback for this op. WARNING: this will be slower than running natively on MPS.

M1でpytorchを使うにはいくつもの未知のトラップを回避しないといけないらしい。困った。