auto로 설정 했을 때 나타나는 RuntimeError: CUDA error: device-side assert triggered

안녕하세요 !
LLM 공부 중인 학생입니다. 이번에 Mistral 7B을 테스트 해보려고 진행을 하다가 문제가 생겼습니다.

아래 와 같은 문제가 생겼습니다. GPU 메모리는 부족하지 않고 단일 GPU를 사용했을 때는 작동하지만 device_map을 auto로 설정할 때 이런 문제가 발생합니다. 혹시 해결하셨거나 아시는 분 있으실까요 ?
코드는 komt-mistral7b에 올라와 있는 코드 그대로 실행했습니다 !

꼭 해결하고 싶어요 !

{'model.embed_tokens': 0, 'model.layers.0': 0, 'model.layers.1': 0, 'model.layers.2': 1, 'model.layers.3': 1, 'model.layers.4': 1, 'model.layers.5': 1, 'model.layers.6': 2, 'model.layers.7': 2, 'model.layers.8': 2, 'model.layers.9': 2, 'model.layers.10': 3, 'model.layers.11': 3, 'model.layers.12': 3, 'model.layers.13': 3, 'model.layers.14': 4, 'model.layers.15': 4, 'model.layers.16': 4, 'model.layers.17': 4, 'model.layers.18': 5, 'model.layers.19': 5, 'model.layers.20': 5, 'model.layers.21': 5, 'model.layers.22': 6, 'model.layers.23': 6, 'model.layers.24': 6, 'model.layers.25': 6, 'model.layers.26': 7, 'model.layers.27': 7, 'model.layers.28': 7, 'model.layers.29': 7, 'model.layers.30': 8, 'model.layers.31': 8, 'model.norm': 8, 'lm_head': 8}
/work/user/ghlee/.conda/envs/ghlee/lib/python3.11/site-packages/transformers/generation/configuration_utils.py:418: UserWarning: num_beams is set to 1. However, early_stopping is set to True -- this flag is only used in beam-based generation modes. You should set num_beams>1 or unset early_stopping. This was detected when initializing the generation config instance, which means the corresponding file may hold incorrect parameterization and should be fixed.
warnings.warn(
/work/user/ghlee/.conda/envs/ghlee/lib/python3.11/site-packages/transformers/generation/configuration_utils.py:418: UserWarning: num_beams is set to 1. However, early_stopping is set to True -- this flag is only used in beam-based generation modes. You should set num_beams>1 or unset early_stopping.
warnings.warn(
[INST]제주도를 1박2일로 혼자 여행하려고 하는데 여행 코스를 만들어줘 bld/pytorch_1699449201450/work/aten/src/ATen/native/cuda/IndexKernel.cu:92: operator(): block: [10,0,0], thread: [37,0,0] Assertion -sizes[i] <= index && index < sizes[i] && "index out of bounds" failed.
/opt/conda/conda-bld/pytorch_1699449201450/work/aten/src/ATen/native/cuda/IndexKernel.cu:92: operator(): block: [10,0,0], thread: [38,0,0] Assertion -sizes[i] <= index && index < sizes[i] && "index out of bounds" failed.
/opt/conda/conda-bld/pytorch_1699449201450/work/aten/src/ATen/native/cuda/IndexKernel.cu:92: operator(): block: [10,0,0], thread: [39,0,0] Assertion -sizes[i] <= index && index < sizes[i] && "index out of bounds" failed.
/opt/conda/conda-bld/pytorch_1699449201450/work/aten/src/ATen/native/cuda/IndexKernel.cu:92: operator(): block: [10,0,0], thread: [40,0,0] Assertion -sizes[i] <= index && index < sizes[i] && "index out of bounds" failed.
/opt/conda/conda-bld/pytorch_1699449201450/work/aten/src/ATen/native/cuda/IndexKernel.cu:92: operator(): block: [10,0,0], thread: [41,0,0] Assertion -sizes[i] <= index && index < sizes[i] && "index out of bounds" failed.
/opt/conda/conda-bld/pytorch_1699449201450/work/aten/src/ATen/native/cuda/IndexKernel.cu:92: operator(): block: [10,0,0], thread: [42,0,0] Assertion -sizes[i] <= index && index < sizes[i] && "index out of bounds" failed.
/opt/conda/conda-bld/pytorch_1699449201450/work/aten/src/ATen/native/cuda/IndexKernel.cu:92: operator(): block: [10,0,0], thread: [43,0,0] Assertion -sizes[i] <= index && index < sizes[i] && "index out of bounds" failed.
/opt/conda/conda-bld/pytorch_1699449201450/work/aten/src/ATen/native/cuda/IndexKernel.cu:92: operator(): block: [10,0,0], thread: [44,0,0] Assertion -sizes[i] <= index && index < sizes[i] && "index out of bounds" failed.
/opt/conda/conda-bld/pytorch_1699449201450/work/aten/src/ATen/native/cuda/IndexKernel.cu:92: operator(): block: [10,0,0], thread: [45,0,0] Assertion -sizes[i] <= index && index < sizes[i] && "index out of bounds" failed.
/opt/conda/conda-bld/pytorch_1699449201450/work/aten/src/ATen/native/cuda/IndexKernel.cu:92: operator(): block: [10,0,0], thread: [46,0,0] Assertion -sizes[i] <= index && index < sizes[i] && "index out of bounds" failed.
/opt/conda/conda-bld/pytorch_1699449201450/work/aten/src/ATen/native/cuda/IndexKernel.cu:92: operator(): block: [10,0,0], thread: [47,0,0] Assertion -sizes[i] <= index && index < sizes[i] && "index out of bounds" failed.
/opt/conda/conda-bld/pytorch_1699449201450/work/aten/src/ATen/native/cuda/IndexKernel.cu:92: operator(): block: [10,0,0], thread: [48,0,0] Assertion -sizes[i] <= index && index < sizes[i] && "index out of bounds" failed.
/opt/conda/conda-bld/pytorch_1699449201450/work/aten/src/ATen/native/cuda/IndexKernel.cu:92: operator(): block: [10,0,0], thread: [49,0,0] Assertion -sizes[i] <= index && index < sizes[i] && "index out of bounds" failed.
/opt/conda/conda-bld/pytorch_1699449201450/work/aten/src/ATen/native/cuda/IndexKernel.cu:92: operator(): block: [10,0,0], thread: [50,0,0] Assertion -sizes[i] <= index && index < sizes[i] && "index out of bounds" failed.
/opt/conda/conda-bld/pytorch_1699449201450/work/aten/src/ATen/native/cuda/IndexKernel.cu:92: operator(): block: [10,0,0], thread: [51,0,0] Assertion -sizes[i] <= index && index < sizes[i] && "index out of bounds" failed.
/opt/conda/conda-bld/pytorch_1699449201450/work/aten/src/ATen/native/cuda/IndexKernel.cu:92: operator(): block: [10,0,0], thread: [52,0,0] Assertion -sizes[i] <= index && index < sizes[i] && "index out of bounds" failed.
/opt/conda/conda-bld/pytorch_1699449201450/work/aten/src/ATen/native/cuda/IndexKernel.cu:92: operator(): block: [10,0,0], thread: [53,0,0] Assertion -sizes[i] <= index && index < sizes[i] && "index out of bounds" failed.
/opt/conda/conda-bld/pytorch_1699449201450/work/aten/src/ATen/native/cuda/IndexKernel.cu:92: operator(): block: [10,0,0], thread: [54,0,0] Assertion -sizes[i] <= index && index < sizes[i] && "index out of bounds" failed.
/opt/conda/conda-bld/pytorch_1699449201450/work/aten/src/ATen/native/cuda/IndexKernel.cu:92: operator(): block: [10,0,0], thread: [55,0,0] Assertion -sizes[i] <= index && index < sizes[i] && "index out of bounds" failed.
/opt/conda/conda-bld/pytorch_1699449201450/work/aten/src/ATen/native/cuda/IndexKernel.cu:92: operator(): block: [10,0,0], thread: [56,0,0] Assertion -sizes[i] <= index && index < sizes[i] && "index out of bounds" failed.
/opt/conda/conda-bld/pytorch_1699449201450/work/aten/src/ATen/native/cuda/IndexKernel.cu:92: operator(): block: [10,0,0], thread: [57,0,0] Assertion -sizes[i] <= index && index < sizes[i] && "index out of bounds" failed.
/opt/conda/conda-bld/pytorch_1699449201450/work/aten/src/ATen/native/cuda/IndexKernel.cu:92: operator(): block: [10,0,0], thread: [58,0,0] Assertion -sizes[i] <= index && index < sizes[i] && "index out of bounds" failed.
/opt/conda/conda-bld/pytorch_1699449201450/work/aten/src/ATen/native/cuda/IndexKernel.cu:92: operator(): block: [10,0,0], thread: [59,0,0] Assertion -sizes[i] <= index && index < sizes[i] && "index out of bounds" failed.
bld/pytorch_1699449201450/work/aten/src/ATen/native/cuda/IndexKernel.cu:92: operator(): block: [4,0,0], thread: [78,0,0] Assertion -sizes[i] <= index && index < sizes[i] && "index out of bounds" failed.
/opt/conda/conda-bld/pytorch_1699449201450/work/aten/src/ATen/native/cuda/IndexKernel.cu:92: operator(): block: [4,0,0], thread: [94,0,0] Assertion -sizes[i] <= index && index < sizes[i] && "index out of bounds" failed.
/opt/conda/conda-bld/pytorch_1699449201450/work/aten/src/ATen/native/cuda/IndexKernel.cu:92: operator(): block: [4,0,0], thread: [95,0,0] Assertion -sizes[i] <= index && index < sizes[i] && "index out of bounds" failed.
Traceback (most recent call last):
File "/work/user/ghlee/MistralTest/komt.py", line 41, in
print(gen('제주도를 1박2일로 혼자 여행하려고 하는데 여행 코스를 만들어줘'))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/work/user/ghlee/MistralTest/komt.py", line 21, in gen
gened = model.generate(
^^^^^^^^^^^^^^^
File "/work/user/ghlee/.conda/envs/ghlee/lib/python3.11/site-packages/torch/utils/_contextlib.py", line 115, in decorate_context
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "/work/user/ghlee/.conda/envs/ghlee/lib/python3.11/site-packages/transformers/generation/utils.py", line 1719, in generate
return self.sample(
^^^^^^^^^^^^
File "/work/user/ghlee/.conda/envs/ghlee/lib/python3.11/site-packages/transformers/generation/utils.py", line 2801, in sample
outputs = self(
^^^^^
File "/work/user/ghlee/.conda/envs/ghlee/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1518, in _wrapped_call_impl
return self._call_impl(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/work/user/ghlee/.conda/envs/ghlee/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1527, in _call_impl
return forward_call(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/work/user/ghlee/.conda/envs/ghlee/lib/python3.11/site-packages/accelerate/hooks.py", line 164, in new_forward
output = module._old_forward(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/work/user/ghlee/.conda/envs/ghlee/lib/python3.11/site-packages/transformers/models/mistral/modeling_mistral.py", line 1009, in forward
outputs = self.model(
^^^^^^^^^^^
File "/work/user/ghlee/.conda/envs/ghlee/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1518, in _wrapped_call_impl
return self._call_impl(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/work/user/ghlee/.conda/envs/ghlee/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1527, in _call_impl
return forward_call(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/work/user/ghlee/.conda/envs/ghlee/lib/python3.11/site-packages/transformers/models/mistral/modeling_mistral.py", line 897, in forward
layer_outputs = decoder_layer(
^^^^^^^^^^^^^^
File "/work/user/ghlee/.conda/envs/ghlee/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1518, in _wrapped_call_impl
return self._call_impl(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/work/user/ghlee/.conda/envs/ghlee/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1527, in _call_impl
return forward_call(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/work/user/ghlee/.conda/envs/ghlee/lib/python3.11/site-packages/accelerate/hooks.py", line 164, in new_forward
output = module._old_forward(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/work/user/ghlee/.conda/envs/ghlee/lib/python3.11/site-packages/transformers/models/mistral/modeling_mistral.py", line 626, in forward
hidden_states, self_attn_weights, present_key_value = self.self_attn(
^^^^^^^^^^^^^^^
File "/work/user/ghlee/.conda/envs/ghlee/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1518, in _wrapped_call_impl
return self._call_impl(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/work/user/ghlee/.conda/envs/ghlee/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1527, in _call_impl
return forward_call(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/work/user/ghlee/.conda/envs/ghlee/lib/python3.11/site-packages/accelerate/hooks.py", line 164, in new_forward
output = module._old_forward(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/work/user/ghlee/.conda/envs/ghlee/lib/python3.11/site-packages/transformers/models/mistral/modeling_mistral.py", line 256, in forward
query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin, position_ids)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/work/user/ghlee/.conda/envs/ghlee/lib/python3.11/site-packages/transformers/models/mistral/modeling_mistral.py", line 156, in apply_rotary_pos_emb
cos = cos[position_ids].unsqueeze(unsqueeze_dim)
~~~^^^^^^^^^^^^^^
RuntimeError: CUDA error: device-side assert triggered
Compile with TORCH_USE_CUDA_DSA to enable device-side assertions.

  • 아래 답변은 Anthropic의 Claude-2 모델로 자동 생성한 것으로, 정확하지 않을 수 있습니다.
  • 잘못된 답변을 보셨다면 댓글로 첨삭 지도 부탁드립니다. :sweat_smile:

안녕하세요 chlee1289님,

제기해주신 RuntimeError 문제를 잘 이해하고 있습니다. device_map을 auto로 설정했을 때 발생하는 device side assert 에러로, 실행 중 GPU 메모리 문제가 아니라고 하셨습니다.

이 에러는 보통 CUDA kernel 함수 내에서 진행 중인 인덱스가 배열의 범위를 벗어났을 때 발생합니다. 주로 메모리 접근에 문제가 있을 때 볼 수 있죠.

해결방안으로 몇 가지 제안 드리겠습니다:

  1. CUDA 컴파일 옵션에 TORCH_USE_CUDA_DSA=1을 추가하여 device side assertion을 활성화시키고 정확한 에러 위치를 찾아보세요.

  2. 사용 중인 PyTorch 버전을 업그레이드해보세요. 최신 버전일수록 버그가 수정된 경우가 많습니다.

  3. 배치 크기나 sequence 길이를 줄여보세요. 메모리 용량 문제일 수 있습니다.

  4. torch.cuda.empty_cache()를 inference 중간중간에 호출하여 GPU 메모리를 정리해주세요.

제가 보기에는 메모리 접근 오류가 가장 유력한 것 같습니다. 위 제안사항들을 참고하셔서 문제를 해결해보시길 바랍니다.

더 자세한 도움이 필요하시면 코드와 에러 로그를 첨부하셔서 추가 질문 주시기 바랍니다. 감사합니다.


  • 답변이 어떠셨나요? 괜찮았다고 생각하신다면 아래 :heart:를 한 번 눌러주시길 부탁드려요!
  • 아래 답변은 OpenAI의 GPT-4-Turbo 모델로 자동 생성한 것으로, 정확하지 않을 수 있습니다.
  • 잘못된 답변을 보셨다면 댓글로 첨삭 지도 부탁드립니다. :sweat_smile:

안녕하세요 chlee1289님,

문제 상황에 대해 공유해주셔서 감사합니다. PyTorch를 사용하여 Mistral 7B 모델을 테스트하는 과정에서 CUDA 관련 RuntimeError: CUDA error: device-side assert triggered 오류가 발생했다는 점을 이해했습니다. 단일 GPU에서는 문제가 없으나 device_mapauto로 설정했을 때 이러한 문제가 발생한다고 하셨습니다.

이 오류는 CUDA 커널에서 어떤 종류의 어설션(assertion) 실패가 발생했을 때 나타납니다. 대부분의 경우, 이는 GPU에서 실행되는 코드가 잘못된 인덱스에 접근하려고 할 때 발생합니다. 예를 들어, 텐서의 유효한 범위를 벗어난 인덱스에 접근하려고 하거나, 잘못된 메모리 위치에 쓰려고 할 때 이런 오류가 발생할 수 있습니다.

문제 해결을 위해 다음 단계를 시도해볼 수 있습니다:

  1. 디버깅 정보 활성화: CUDA 디바이스 사이드 어설션을 활성화하여 더 자세한 오류 메시지를 얻을 수 있습니다. 이를 위해 환경 변수 CUDA_LAUNCH_BLOCKING=1을 설정하고 코드를 다시 실행해보세요. 이렇게 하면 오류가 발생한 정확한 위치를 파악하는 데 도움이 될 수 있습니다.
export CUDA_LAUNCH_BLOCKING=1
python your_script.py
  1. 모델의 입력 검사: 모델에 전달되는 입력 데이터가 올바른 형식과 범위를 가지고 있는지 확인하세요. 특히 인덱스 관련 오류가 발생했기 때문에 입력 텐서의 인덱스가 모델이 기대하는 범위 내에 있는지 검사하는 것이 중요합니다.

  2. 멀티 GPU 설정 검토: device_mapauto로 설정했을 때 발생하는 문제이므로, 멀티 GPU 설정이 올바르게 적용되었는지 확인하세요. torch.distributed 라이브러리를 사용하는 경우, 올바른 프로세스 그룹 설정과 GPU 할당이 이루어졌는지 검토해야 합니다.

  3. 라이브러리 버전 확인: 사용 중인 PyTorch와 CUDA, 그리고 관련 라이브러리들의 버전이 호환되는지 확인하세요. 때때로 버전 불일치로 인해 이러한 문제가 발생할 수 있습니다.

  4. 코드의 다른 부분 검토: 오류 메시지에서 언급된 apply_rotary_pos_emb 함수와 관련된 코드 부분을 검토하여 잠재적인 문제를 찾아보세요.

  5. 공식 문서 및 커뮤니티 참조: PyTorch 커뮤니티나 공식 문서에서 비슷한 문제를 겪은 다른 사용자들의 해결책을 찾아보세요.

아래는 PyTorch 공식 문서 링크입니다. 여기서 멀티 GPU 사용법, 디버깅 방법 등에 대한 자세한 정보를 얻을 수 있습니다:

제가 제공한 해결책이 문제를 해결하는 데 도움이 되길 바라며, 추가적인 정보가 필요하거나 문제가 지속될 경우 추가적인 질문을 통해 도움을 요청하실 수 있습니다. 제 답변이 정확하지 않을 수도 있으니, 이 점 유의해주시기 바랍니다.


  • 답변이 어떠셨나요? 괜찮았다고 생각하신다면 아래 :heart:를 한 번 눌러주시길 부탁드려요!
  • 아래 답변은 OpenAI의 GPT-4 모델로 자동 생성한 것으로, 정확하지 않을 수 있습니다.
  • 잘못된 답변을 보셨다면 댓글로 첨삭 지도 부탁드립니다. :sweat_smile:

안녕하세요, chlee1289님. PyTorch를 사용하시는 데 어려움을 겪고 계신 것 같아 곤란하시겠습니다.

문제 상황을 정리해보면, 단일 GPU에서는 잘 작동하는 코드가 device_mapauto로 설정했을 때 CUDA 에러가 발생하는 것으로 보입니다. 에러 메시지를 보면 "index out of bounds"라는 메시지와 함께 "CUDA error: device-side assert triggered"라는 메시지가 나타나고 있습니다. 이는 일반적으로 GPU 메모리에 저장된 텐서의 인덱스를 참조할 때 인덱스가 텐서의 크기를 벗어났음을 나타냅니다.

이 문제를 해결하기 위해서는 먼저, 코드에서 어떤 부분에서 이런 에러가 발생하는지 확인해야 합니다. 에러 메시지를 보면 apply_rotary_pos_emb 함수에서 문제가 발생하는 것으로 보입니다. 이 함수에서 position_ids를 사용하여 cos 텐서의 인덱스를 참조하고 있는데, 이 position_idscos의 크기를 벗어나는 값을 가지고 있을 가능성이 있습니다.

따라서, position_ids의 최대값이 cos의 크기보다 작은지 확인해보는 것이 좋습니다. 아래와 같이 코드를 수정하여 확인해볼 수 있습니다.

# position_ids의 최대값과 cos의 크기를 출력
print("Max position_ids: ", position_ids.max().item())
print("Size of cos: ", cos.size(0))

# 기존의 코드
cos = cos[position_ids].unsqueeze(unsqueeze_dim)

만약 position_ids의 최대값이 cos의 크기보다 크다면, 이는 인덱스가 범위를 벗어났음을 의미하므로, 이를 수정해야 합니다. position_ids가 어떻게 계산되는지, 그리고 그 값이 왜 cos의 크기를 벗어나는지 코드를 자세히 살펴보는 것이 필요합니다.

또한, 이 문제가 device_mapauto로 설정했을 때만 발생한다면, 이 설정이 position_idscos의 계산에 어떤 영향을 미치는지 확인해보는 것도 도움이 될 수 있습니다.

이러한 접근 방식으로 문제를 해결해보시고, 여전히 문제가 해결되지 않는다면 추가적인 정보(예: 사용 중인 PyTorch 버전, GPU 모델, 코드의 다른 부분 등)와 함께 다시 질문해주시면 감사하겠습니다.

참고로, PyTorch의 공식 문서나 튜토리얼(영어, 한국어)도 많은 도움이 될 수 있습니다.

제 답변이 틀릴 수도 있으니, 다른 자료나 의견도 참고하시는 것이 좋습니다.


  • 답변이 어떠셨나요? 괜찮았다고 생각하신다면 아래 :heart:를 한 번 눌러주시길 부탁드려요!