GPU Data Science

NumPy vs PyTorch

import numpy as np
import torch

x = np.random.random((10000, 10000))  # 1억
y = torch.from_numpy(x).float().to(torch.device("cuda:0"))
z = torch.from_numpy(x).float().to(torch.device("cpu"))

'''
>>> %timeit (np.sin(x) ** 2 / np.cos(x) ** 2).sum()
4.12 s ± 23.1 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

>>> %timeit (torch.sin(y) ** 2 / torch.cos(y) ** 2).sum()
18.9 ms ± 30 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

>>> %timeit (torch.sin(z) ** 2 / torch.cos(z) ** 2).sum()
738 ms ± 5.64 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
'''

GPU에서 계산할때 40배 더 빠르다.1 GCP n1-standard-4/Tesla T4 기준으로, V100이었으면 훨씬 더 빠를 것이다.

메모리

Tesla T4의 available memory는 14.726G로 나온다. 메모리가 꽉 차서 더 이상 할당이 안되는 경우 다음과 같은 오류가 발생한다.

CudaAPIError: [1] Call to cuMemcpyHtoD results in CUDA_ERROR_INVALID_VALUE

python REPL을 나가거나 하면 모든 메모리를 반환한다. T4도 14.7G의 메모리를 data manipluation 작업 등으로 초과할 경우 방법이 없기 때문에 Dask를 활용해야 한다. dask_cudf.compute()가 매우 불안하다. 계산 도중 실패하면 메모리를 반환하지 못하고 점유하고 있으며, 아무런 task 없이 compute() 할때도 저절로 메모리가 증가한다. 결국 메모리가 꽉 차서 오류를 발생한다.

예전에는 클러스터도 구동하지 않고 돌려서 그랬던거 같다(2022/11).

아래는 XGBoost를 gpu_hist로 학습하는 모습이다.

nvtop

htop과 유사한 nvtop 설치2 apt에는 없어서 예전에는 소스 컴파일했다.

$ sudo apt install cmake libncurses5-dev libncursesw5-dev
$ git clone https://github.com/Syllo/nvtop.git
$ mkdir -p nvtop/build && cd nvtop/build
$ cmake .. -DNVIDIA_SUPPORT=ON -DAMDGPU_SUPPORT=OFF -DINTEL_SUPPORT=OFF
$ make
$ sudo make install

빌드 오류가 발생하면 $ conda deactivate 후 다시 cmake를 실행한다. 요즘은 apt로 설치 가능하다고 가이드에 나와 있다.

$ sudo apt install nvtop

Low Precision Inference on GPU

T4 GPU에서 Quantized 연산으로 8.1 TFLOPS를 130 TOPS, 16배 개선할 수 있다. 그러나, BERT SQuAD 1.1(F1) 점수가 Int8에서 6.43%나 낮다. Activation으로 GeLU10을 사용해 0.67% 이내로 줄일 수 있다. 3 MRPC는 두 문장의 유사 문장 판별 태스크로 마찬가지로 성능 차이를 0.80% 이내로 줄일 수 있다.

Last Modified: 2022/11/03 23:55:17

is a collection of Papers I have written.
© 2000 - Sang-Kil Park Except where otherwise noted, content on this site is licensed under a CC BY 4.0.
This site design was brought from Distill.