皆様PyTorchは使っていますか?
普段は便利ですが、たまに(ピー)したい時、ありますよね?
そう、エラーが出た時です。
そのエラーメッセージで検索するのが通例ですが、解決しない場合も往々にしてあります。
そんな不一致!(時のオカリナ風)を紹介、原因(のひとつ)を紹介したいと思います。
RuntimeError: CUDA error: device-side assert triggered
たまに見ませんか?
githubからコードを持ってきて、別のデータセットとかで試してみると出る時があります。
「CUDAのエラーなんか直せない」?
違う原因の場合もあります。
このエラーメッセージより前に、なんか変なメッセージが大量に出てきませんか?
int, DstDim = 2, SrcDim = 2, IdxDim = -2, IndexIsMajor = true]: block: [117,0,0], thread: [92,0,0] Assertion `srcIndex < srcSelectDimSize` failed.
/pytorch/aten/src/THC/THCTensorIndex.cu:361: void indexSelectLargeIndex(TensorInfo<T, IndexType>, TensorInfo<T, IndexType>, TensorInfo<long, IndexType>, int, int, IndexType, IndexType, long) [with T = float, IndexType = unsigned int, DstDim = 2, SrcDim = 2, IdxDim = -2, IndexIsMajor = true]: block: [117,0,0], thread: [93,0,0] Assertion `srcIndex < srcSelectDimSize` failed.
/pytorch/aten/src/THC/THCTensorIndex.cu:361: void indexSelectLargeIndex(TensorInfo<T, IndexType>, TensorInfo<T, IndexType>, TensorInfo<long, IndexType>, int, int, IndexType, IndexType, long) [with T = float, IndexType = unsigned int, DstDim = 2, SrcDim = 2, IdxDim = -2, IndexIsMajor = true]: block: [117,0,0], thread: [94,0,0] Assertion `srcIndex < srcSelectDimSize` failed.
/pytorch/aten/src/THC/THCTensorIndex.cu:361: void indexSelectLargeIndex(TensorInfo<T, IndexType>, TensorInfo<T, IndexType>, TensorInfo<long, IndexType>, int, int, IndexType, IndexType, long) [with T = float, IndexType = unsigned int, DstDim = 2, SrcDim = 2, IdxDim = -2, IndexIsMajor = true]: block: [117,0,0], thread: [95,0,0] Assertion `srcIndex < srcSelectDimSize` failed.
このようなエラーが見えたら、何かしらの上限値、あるいは下限値を調べてみましょう。
例えば、cross_entropyのクラス数よりも多い出力を用意していないでしょうか(1敗)?
入力、あるいは出力が想定したデータより長い事はないですか(1敗)?
nn.Embeddingの入力で想定したクラス数よりも大きい数字が入ってませんか(1敗)?
test_error.py
# 注意! このコードはエラーが起こります。
# ご使用は自己責任でお願いいたします。
import torch
import torch.nn as nn
emb = nn.Embedding(2,1).cuda()
input_var = torch.tensor([4]).cuda().long() # 4 > (2+1)
output = emb(input_var)
/pytorch/aten/src/THC/THCTensorIndex.cu:308: void indexSelectSmallIndex(TensorInfo<T, IndexType>, TensorInfo<T, IndexType>, TensorInfo<long, IndexType>, int, int, IndexType, long) [with T = float, IndexType = unsigned int, DstDim = 1, SrcDim = 1, IdxDim = -2]: block: [0,0,0], thread: [0,0,0] Assertion `srcIndex < srcSelectDimSize` failed.
順次更新予定