Methods and tools for efficient training on a single GPU
<aside>
To achieve optimal performance, start by identifying the appropriate batch size.
→ 최적의 성능 얻으려면 배치 사이즈 중요
It is recommended to use batch sizes and input/output neuron counts that are of size 2^N.
→ 배치 크기와 입출력 뉴런 개수 = 2의 거듭 제곱 형태를 추천
→ 메모리(gpu, ram)는 2의 거듭 제곱을 기반으로 설계
Often it’s a multiple of 8, but it can be higher depending on the hardware being used and the model’s dtype.
→ 일반적으로 8의 배수가 많이 사용됨
→ 딥러닝에서 gpu를 사용할 때, 병렬 연산이 많이 일어남
→ but, 사용 중인 하드웨어 및 모델의 데이터 타입에 따라 더 큰 값 필요할 수 있음
</aside>
<aside>
3.1. Input Features And Output Neuron Counts
As fully-connected layers directly correspond to GEMMs, their performance trends are identical
→ 완전 연결 층은 일반 행렬 곱셈과 1:1로 대응되며, 그 성능도 마찬가지
to those described in Typical Tile Dimensions In NVIDIA cuBLAS And Performance.
Larger parameters tend to allow better parallelization(병렬 연산) and efficiency;
→ 더 큰 매개변수(파라미터)를 사용할수록 병렬 연산과 연산 효율이 증가하는 경향 있음
a GEMM that is twice the size often takes less than twice the time to calculate.
→ 두 배 크기의 GEMM 연산은, 계산 시간이 두 배보다 적게 걸리는 경우 있음
Figure 3. Larger fully-connected layers are equivalent to larger GEMMs, which perform better. NVIDIA A100-SXM4-80GB, CUDA 11.2, cuBLAS 11.4. → 뉴런 개수가 많아져서 완전 연결 층이 커지면, 결국 더 큰 행렬 곱셈 연산이 필요
→ gpu 병렬 처리를 더 잘 활용 가능 → 결과적으로 성능이 좋아짐(무조건은 아님)
</aside>
<aside>
The batch size directly contributes to the tiling strategy for two out of three training phases - forward pass and activation gradient computation.
→ 배치 크기는 학습 과정에서 타일링 전략에 직접적으로 영향
For these phases, the output matrix dimension includes batch size,
→ 출력 행렬의 크기에 배치 크기 포함
so larger batch sizes result in more tiles.
Training with larger batch sizes is one option to extract more performance when model size is too small to fully utilize a GPU.
→ 모델 크기가 너무 작아서 gpu를 완전히 활용하지 못할 때, 배치 크기를 키우면 성능을 향상시킬 수 있음
For weight gradient computation, the output matrix has the same dimensions as the weights,
→ 가중치 그래디언트 계산 단계: 출력 행렬 = 가중치와 동일한 크기
thus batch size does not affect the tile count directly.
→ 배치 크기가 타일 개수에 직접적인 영향을 주지 않음
Instead, batch size here maps to the K dimension of the GEMM;
→ 배치 크기 ↔ 행렬 곱셈의 k 차원과 연관
larger batch size enables more efficient computation per tile of weight gradients.
→ 배치 크기가 크면, 타일 당 연산량이 증가하여 더 효율적인 계산 가능
Figure 15 shows the performance impact of varying batch size on forward, activation gradient, and weight gradient computations for a fully-connected layer with 4096 inputs and 1024 outputs. The larger batch sizes yield roughly 250 TFLOPS delivered performance.
Figure 4. Performance data for (a) forward propagation, (b) activation gradient computation, and (c) weight gradient computation for a fully-connected layer with 4096 inputs, 1024 outputs, and varying batch size. NVIDIA A100-SXM4-80GB, CUDA 11.2, cuBLAS 11.4.
Of particular interest are GEMMs where one dimension is very small. For example, on NVIDIA A100-SXM4-80GB and for a fully-connected layer with 4096 inputs and 4096 outputs, forward propagation, activation gradient computation, and weight gradient computation are estimated to be memory-bound for batch sizes 128 and below (see Figure 5).
→ 배치 크기가 너무 작으면, 연산이 gpu의 메모리 대역폭에 제한 받음
Figure 5. Arithmetic intensity for a fully-connected layer with 4096 inputs and 4096 outputs. Batch sizes 128 and below are bandwidth limited on NVIDIA A100 accelerators. → 입출력 뉴런이 각각 4096개인 완전 연결 층에서, 배치 크기가 128 이하일 경우 GPU 메모리 대역폭에 의해 성능이 제한됨
**Larger numbers of inputs and outputs improve performance somewhat, but the computation will always be bandwidth-limited for very small batch sizes, for example, 8 and below.
→ 입력과 출력 뉴런 개수를 늘리면 성능이 약간 향상될 수 있음
→ 하지만, 배치 크기 너무 작으면 연산이 항상 메모리 대역폭 제한을 받게 됨
For a discussion of math- and bandwidth-limited computations, refer to Math And Memory Bounds.
📌 정리
배치 크기를 키우면 GPU 병렬 연산을 최적화할 수 있지만, 너무 작으면 메모리 대역폭에 제한받아 성능이 떨어진다!
✅ 배치 크기는 타일링 전략(Tiling Strategy)에 영향을 줌 (Forward Pass & Activation Gradient 계산에서 중요!)
✅ 배치 크기가 크면, GPU를 더 효율적으로 사용할 수 있음!
✅ Weight Gradient 단계에서는 배치 크기가 "K 차원(K Dimension)"에 영향을 줘서 연산 효율이 증가!
✅ 배치 크기가 128 이하일 경우, 연산이 메모리 대역폭(Bandwidth)에 제한 받아 성능이 떨어질 수 있음.
✅ 배치 크기가 너무 작으면 (예: 8 이하), 성능이 항상 메모리 제한에 걸려서 느려질 수 있음.
</aside>
<aside>
For reference, check out NVIDIA’s recommendation for input/output neuron counts and batch size for fully connected layers
(which are involved in GEMMs (General Matrix Multiplications)).
→ 완전 연결 층에서는 일반 행렬 곱셈이 사용됨
→ 배치 크기와 입출력 뉴런 개수를 적절히 설정하면, gpu에서 수행하는 행렬 곱셈의 연산 속도(gpu 연산속도)를 최적화할 수 있음
</aside>
<aside>
Tensor Core Requirements define the multiplier based on the dtype and the hardware.
For instance, for fp16 data type a multiple of 8 is recommended, unless it’s an A100 GPU, in which case use multiples of 64.
→ 데이터 타입에 따라 배치 크기 및 행렬 크기 조정하면 성능 극대화 가능
</aside>
<aside>
As we discussed in GPU Architecture Fundamentals, the latest NVIDIA GPUs have introduced Tensor Cores to maximize the speed of tensor multiplies.
→ 텐서 코어는 행렬 곱셈을 매우 빠르게 처리하는 NVIDIA GPU의 특수 연산 장치!
Requirements to use Tensor Cores depend on NVIDIA library versions.
Performance is better when equivalent matrix dimensions M, N, and K are aligned to multiples of 16 bytes (or 128 bytes on A100).
With NVIDIA cuBLAS versions before 11.0 or NVIDIA cuDNN versions before 7.6.3, this is a requirement to use Tensor Cores;
as of cuBLAS 11.0 and cuDNN 7.6.3, Tensor Cores may be used regardless, but efficiency is better when matrix dimensions are multiples of 16 bytes.
For example, when using FP16 data, each FP16 element is represented by 2 bytes, so matrix dimensions would need to be multiples of 8 elements for best efficiency (or 64 elements on A100).
The requirement is in fact more relaxed - only the fastest varying dimensions in memory are required to obey this rule - but it is easiest to just think of all three dimensions the same way.
Following these alignments for all dimensions ensures Tensor Cores will be enabled and run efficiently.
This effect can be seen in Figure 5 - calculations are fastest (durations are lowest) when K is divisible by 8.
When K is not divisible by 8, switching from cuBLAS 10.2 to cuBLAS 11.0 allows Tensor Cores to be used and results in 2-4x speedup.
It is also worth noting that with cuBLAS 11.0, among values of K that are not divisible by 8, even values still result in faster calculation than odd values. → cuBLAS 11.0에서는 K 차원이 8의 배수일 때 가장 빠르며, 그렇지 않다면 짝수로 맞추는 것이 유리함
We recommend choosing matrix dimensions to be multiples of 16 bytes (8 for FP16 as in Table 1); if this is not possible, choosing multiples of a smaller power of two (such as 8 or 4 bytes) often still helps performance with cuBLAS 11.0 and higher.
On A100, choosing multiples of larger powers of two up to 128 bytes (64 for FP16) can further improve efficiency.
Figure 2. Comparison of GEMM execution times with (a) cuBLAS 10.1 and (b) cuBLAS 11.0, both with FP16 data. Calculation is fastest (duration is lowest) when K is divisible by 8. “NN” means A and B matrices are both accessed non-transposed. NVIDIA V100-DGXS-16GB GPU.
📌 정리
최신 cuBLAS에서는 K가 8의 배수면 가장 빠르고, 8의 배수가 아니면 짝수라도 맞추는 것이 성능 최적화에 도움됨. A100에서는 128바이트(또는 FP16에서는 64배수)로 맞추면 추가적인 속도 향상 가능!
✅ 최적 성능을 위해 행렬 차원(M, N, K)은 16바이트(또는 FP16에서는 8바이트) 배수로 맞추는 것이 좋음!
</aside>
<aside>
</aside>
<aside>
</aside>