FasterTransformer
FasterTransformer
FasterTransformer는 NVIDIA가 제공하는 고성능 Transformer 추론(inference) 엔진이에요. BERT, GPT, T5 같은 Transformer 기반의 encoder와 decoder를 매우 최적화된 형태로 구현해서 GPU에서 빠르게 추론할 수 있게 도와줘요. Volta, Turing, Ampere GPU에서 FP16 정밀도로 실행하면 Tensor Core의 연산 능력을 자동으로 활용할 수 있어요.
FasterTransformer는 CUDA, cuBLAS, cuBLASLt, C++ 위에 만들어졌고, TensorFlow, PyTorch, Triton backend 같은 여러 프레임워크에서 사용할 수 있는 API를 제공해요. 원하는 프레임워크에 그대로 통합해서 쓸 수 있고, 각 프레임워크별 사용 예시 코드와 성능 결과도 함께 제공돼요.
최근에는 FasterTransformer의 개발이 TensorRT-LLM로 이전되었어요. 모든 개발자들은 최신 LLM 추론 개선을 위해 TensorRT-LLM을 활용하도록 권장되고 있으며, NVIDIA/FasterTransformer 저장소는 유지되지만 더 이상의 개발은 이루어지지 않아요.
출처: 문서
본문
이 저장소는 고도로 최적화된 Transformer 기반의 encoder와 decoder 컴포넌트를 실행하기 위한 스크립트와 레시피를 제공하고, NVIDIA가 테스트하고 유지보수해요.
NLP에서 encoder와 decoder는 두 가지 중요한 컴포넌트이고, Transformer 레이어는 두 컴포넌트에서 모두 널리 쓰이는 아키텍처가 되었어요. FasterTransformer는 encoder와 decoder 모두를 위한 고도로 최적화된 Transformer 레이어를 구현해요. Volta, Turing, Ampere GPU에서는 데이터와 가중치의 정밀도가 FP16일 때 Tensor Core의 연산 능력이 자동으로 사용돼요.
지원 모델 (Support matrix)
FasterTransformer는 다음 모델들을 다양한 프레임워크, 정밀도, 병렬화 방식으로 지원해요.
| Models | Framework | FP16 | INT8 (after Turing) | Sparsity (after Ampere) | Tensor parallel | Pipeline parallel | FP8 (after Hopper) |
|---|---|---|---|---|---|---|---|
| BERT | TensorFlow | Yes | Yes | - | - | - | - |
| BERT | PyTorch | Yes | Yes | Yes | Yes | Yes | - |
| BERT | Triton backend | Yes | - | - | Yes | Yes | - |
| BERT | C++ | Yes | Yes | - | - | - | Yes |
| XLNet | C++ | Yes | - | - | - | - | - |
| Encoder | TensorFlow | Yes | Yes | - | - | - | - |
| Encoder | PyTorch | Yes | Yes | Yes | - | - | - |
| Decoder | TensorFlow | Yes | - | - | - | - | - |
| Decoder | PyTorch | Yes | - | - | - | - | - |
| Decoding | TensorFlow | Yes | - | - | - | - | - |
| Decoding | PyTorch | Yes | - | - | - | - | - |
| GPT | TensorFlow | Yes | - | - | - | - | - |
| GPT/OPT | PyTorch | Yes | - | - | Yes | Yes | Yes |
| GPT/OPT | Triton backend | Yes | - | - | Yes | Yes | - |
| GPT-MoE | PyTorch | Yes | - | - | Yes | Yes | - |
| BLOOM | PyTorch | Yes | - | - | Yes | Yes | - |
| BLOOM | Triton backend | Yes | - | - | Yes | Yes | - |
| GPT-J | Triton backend | Yes | - | - | Yes | Yes | - |
| Longformer | PyTorch | Yes | - | - | - | - | - |
| T5/UL2 | PyTorch | Yes | - | - | Yes | Yes | - |
| T5 | TensorFlow 2 | Yes | - | - | - | - | - |
| T5/UL2 | Triton backend | Yes | - | - | Yes | Yes | - |
| T5 | TensorRT | Yes | - | - | Yes | Yes | - |
| T5-MoE | PyTorch | Yes | - | - | Yes | Yes | - |
| Swin Transformer | PyTorch | Yes | Yes | - | - | - | - |
| Swin Transformer | TensorRT | Yes | Yes | - | - | - | - |
| ViT | PyTorch | Yes | Yes | - | - | - | - |
| ViT | TensorRT | Yes | Yes | - | - | - | - |
| GPT-NeoX | PyTorch | Yes | - | - | Yes | Yes | - |
| GPT-NeoX | Triton backend | Yes | - | - | Yes | Yes | - |
| BART/mBART | PyTorch | Yes | - | - | Yes | Yes | - |
| WeNet | C++ | Yes | - | - | - | - | - |
| DeBERTa | TensorFlow 2 | Yes | - | - | On-going | On-going | - |
| DeBERTa | PyTorch | Yes | - | - | On-going | On-going | - |
- FasterTransformer는 모든 소스 코드가 C++로 만들어졌기 때문에 위 모델들을 C++에서도 지원해요.
특정 모델의 자세한 내용은 docs/ 디렉토리의 xxx_guide.md 파일에 정리되어 있어요. 여기서 xxx는 모델 이름이에요. 자주 묻는 질문과 답변은 docs/QAList.md에 정리되어 있어요. Encoder와 BERT 모델은 비슷해서 bert_guide.md에 함께 설명이 들어 있어요.
디렉토리 구조
FasterTransformer의 디렉토리 구조는 다음과 같아요.
/src/fastertransformer: source code of FasterTransformer
|--/cutlass_extensions: Implementation of cutlass gemm/kernels.
|--/kernels: CUDA kernels for different models/layers and operations, like addBiasResiual.
|--/layers: Implementation of layer modules, like attention layer, ffn layer.
|--/models: Implementation of different models, like BERT, GPT.
|--/tensorrt_plugin: encapluate FasterTransformer into TensorRT plugin.
|--/tf_op: custom Tensorflow OP implementation
|--/th_op: custom PyTorch OP implementation
|--/triton_backend: custom triton backend implementation
|--/utils: Contains common cuda utils, like cublasMMWrapper, memory_utils
/examples: C++, tensorflow and pytorch interface examples
|--/cpp: C++ interface examples
|--/pytorch: PyTorch OP examples
|--/tensorflow: TensorFlow OP examples
|--/tensorrt: TensorRT examples
/docs: Documents to explain the details of implementation of different models, and show the benchmark
/benchmark: Contains the scripts to run the benchmarks of different models
/tests: Unit tests
/templates: Documents to explain how to add a new model/example into FasterTransformer repo
양자화(quantization) 도구는 examples로 이동되었어요. 예를 들어 examples/tensorflow/bert/bert-quantization/와 examples/pytorch/bert/bert-quantization-sparsity/에서 찾을 수 있어요.
환경 변수 (Global Environment)
FasterTransformer는 디버깅과 테스트를 위한 편리한 환경 변수를 제공해요.
FT_LOG_LEVEL: 디버그 메시지의 로그 레벨을 제어하는 환경 변수예요. 자세한 내용은src/fastertransformer/utils/logger.h에 있어요. 레벨이DEBUG보다 낮으면 프로그램이 많은 메시지를 출력해서 매우 느려질 수 있어요.FT_NVTX:FT_NVTX=ON ./bin/gpt_example처럼ON으로 설정하면 프로그램에 nvtx 태그를 삽입해서 프로파일링을 도와줘요.FT_DEBUG_LEVEL:DEBUG로 설정하면 프로그램이 모든 커널 다음에cudaDeviceSynchronize()를 실행해요. 그렇지 않으면 커널은 기본적으로 비동기로 실행돼요. 디버깅 중 오류 지점을 찾는 데 유용하지만, 이 플래그는 성능에 큰 영향을 주기 때문에 디버깅에만 사용해야 해요.
성능 (Performance)
벤치마크 하드웨어 설정은 다음과 같아요.
- 8xA100-80GBs (with mclk 1593MHz, pclk 1410MHz) with AMD EPYC 7742 64-Core Processor
- T4 (with mclk 5000MHz, pclk 1590MHz) with Intel(R) Xeon(R) CPU E5-2670 0 @ 2.60GHz
아래 벤치마크를 실행하려면 먼저 Unix 계산 도구 "bc"를 설치해야 해요.
apt-get install bc
주요 성능 결과를 정리하면:
- BERT base (FP16, T4): 작은 batch size와 sequence length에서는 TensorFlow XLA 대비 약 3배, PyTorch TorchScript 대비 CustomExt로 약 4~6배 속도 향상을 보여줘요. 큰 경우에는 Effective FasterTransformer와 INT8-v2 양자화를 조합하면 약 5배 향상을 보여줘요.
- Decoder/Decoding: TensorFlow 대비 FT-Decoder는 1.5x ~ 3x, FT-Decoding은 4x ~ 18x의 속도 향상을 보여줘요. PyTorch 대비로는 각각 1.2x ~ 3x, 3.8x ~ 13x의 속도 향상을 보여줘요.
- GPT: A100에서 Megatron과 FasterTransformer의 FP16 성능을 비교해요. GPT-89B는 num_layers 48, GPT-175B는 num_layers 96, tensor parallel size 8을 사용해요.
빌드/설치
FasterTransformer의 모든 소스 코드는 C++로 빌드되고, 예시 코드가 examples 디렉토리에 C++, TensorFlow, PyTorch, TensorRT 인터페이스별로 정리되어 있어요. 각 모델별 빌드 및 사용 방법은 docs/ 디렉토리의 모델 가이드 문서(xxx_guide.md)에 자세히 설명되어 있어요.
사용 예시로, GPT 추론 예시를 환경 변수와 함께 실행하면 다음과 같은 형태로 사용할 수 있어요.
FT_NVTX=ON ./bin/gpt_example
알려진 이슈 (Known issues)
- TensorFlow 2.10에서는 undefined symbol 문제로 컴파일되지 않아요.
- 확장을 import할 때 undefined symbol 오류가 나면:
- 먼저
import torch를 실행해 보세요. 그래도 문제가 있다면 컴파일과 실행 시 사용한 PyTorch가 같은지, PyTorch가 어떻게 컴파일됐는지, GCC 버전 등을 확인해야 해요 (C++ ABI 불일치 때문일 수 있어요).
- 먼저
- 디코딩에서 TensorFlow와 OP의 결과가 달라질 수 있지만, 이는 누적 log probability 때문에 생기는 문제로 회피하지 않아요.
- 커스텀 환경에서 문제가 생기면, 특히 TensorFlow 1.14의 경우 gcc/g++ 4.8로 TensorFlow op 프로젝트를 빌드해 보세요.