Numerical Computing

Numerical computing은 finite-precision computation의 수학입니다. AI formula는 real number 위에서 쓰이지만, 실제 training과 inference는 floating-point tensor, limited memory, finite accumulation order 위에서 실행됩니다.

이 페이지는 algebraically equivalent한 formula가 hardware 위에서 왜 다르게 동작할 수 있는지 설명하므로 Math에 둡니다. System-specific tradeoff는 GPU infra, Training run, Memory-compute tradeoff와 연결됩니다.

Route Map

RouteUse forStart
Stable probabilitiessoftmax, log-sum-exp, likelihood, attention logitSoftmax, Information and likelihood
Stable activationsnormalization, residual scaling, exploding/vanishing valueNormalization, Training stability
Stable gradientsclipping, accumulation, underflow, optimizer sensitivityGradient clipping, Gradient accumulation, Calculus and gradients
Precision and memorymixed precision, reduction, tensor layout, memory-compute tradeoffMemory-compute tradeoff, GPU infra, Training run
Debugging boundary문제가 mathematical, numerical, model, optimizer, system behavior 중 어디에 있는가Infra, Evaluation

Floating Point

Floating-point number는 real number를 finite precision으로 근사합니다.

여기서 은 machine-dependent precision scale입니다. 더 작은 precision은 memory와 bandwidth를 아끼지만 rounding sensitivity를 키웁니다.

Overflow and Underflow

Exponential은 큰 positive input에서 overflow하고, 큰 negative input에서 underflow할 수 있습니다.

이는 softmax, likelihood, attention, contrastive learning, energy-based scoring에서 중요합니다.

Log-Sum-Exp

Stable log-sum-exp trick은 exponentiation 전에 maximum을 빼는 방식입니다.

이렇게 하면 가장 큰 exponent가 로 유지됩니다.

Stable Softmax

Softmax는 보통 아래처럼 계산하는 것이 안전합니다.

이는 ordinary softmax와 수학적으로 같지만 numerically safer합니다.

Conditioning

작은 input change가 큰 output change를 만들 수 있으면 problem이 ill-conditioned라고 봅니다. Matrix 의 condition number는 아래와 같습니다.

큰 는 system solving, matrix inversion, gradient propagation이 noise와 rounding에 민감할 수 있음을 뜻합니다.

Precision in Training

Mixed precision은 memory와 throughput을 바꾸지만 numerical behavior도 함께 바꿉니다. 흔한 risk point는 아래와 같습니다.

  • tiny gradients underflowing to zero
  • large activations or logits overflowing
  • reductions accumulating in a different order
  • normalization statistics losing precision
  • optimizer state requiring more precision than activations

Loss scaling은 gradient underflow를 줄이는 방법 중 하나입니다.

Scaled gradient는 optimizer update 전에 다시 unscale됩니다.

Reduction Order

Floating-point addition은 완전히 associative하지 않습니다.

따라서 mathematical expression이 같아도 parallel reduction, distributed training, 다른 kernel은 조금 다른 결과를 만들 수 있습니다.

Checks

  • logit, loss, probability가 stable formula로 계산되는가?
  • reduction이 dynamic range에 맞는 precision에서 수행되는가?
  • instability가 overflow, underflow, NaN propagation, gradient explosion 중 무엇에서 오는가?
  • 문제가 mathematical, numerical, architectural, optimizer-related, hardware-related 중 어디에 있는가?
  • deterministic expectation이 해당 kernel과 distributed setup에서 현실적인가?