This guide provides step-by-step instructions for setting up a Python environment with PyTorch, NVIDIA Apex, Flash Attention, and Megatron-LLM. Explanatory notes are included to clarify each step and highlight important caveats.
Use Python 3.10.
- PyTorch does not currently support Python 3.12.
- Ensure your environment uses Python 3.10 (e.g., via
condaorvenv).
Install PyTorch, torchvision, and torchaudio with CUDA 11.8 support:
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118This command ensures you get pre-built CUDA-enabled wheels directly from the official PyTorch repository.
If your system uses environment modules (common on clusters), load GCC 11.2.0:
module load compiler/gcc/11.2.0This ensures compatibility with CUDA and C++ extensions required by some packages.
Apex is used for mixed-precision training and requires a custom install.
- Clone the Apex repository (22.04-dev branch):
git clone -b 22.04-dev https://github.com/NVIDIA/apex.git
cd apex- Install Apex with CUDA and C++ extensions:
pip install -v --disable-pip-version-check --no-cache-dir --global-option="--cpp_ext" --global-option="--cuda_ext" ./- Patch for Python 3.10 compatibility:
- Open:
/path/to/env/lib/python3.10/site-packages/apex/amp/_initialize.py - Replace line 2 with:
- Open:
string_classes = strThis patch fixes a compatibility issue with Python 3.10.
Flash Attention accelerates transformer models but requires special installation steps.
- Clone the repository:
git clone https://github.com/Dao-AILab/flash-attn.git
cd flash-attn- Install
ninjafor faster compilation:
pip install ninjaninja enables parallel builds, greatly speeding up compilation[^1].
3. Build and install Flash Attention:
python setup.py install- Use at least 10 CPU cores for faster installation.
- If your machine has limited RAM, consider limiting parallel jobs:
MAX_JOBS=4 python setup.py installIf you encounter SSL errors at the end of installation, run:
pip install --trusted-host pypi.org --trusted-host pypi.python.org --trusted-host files.pythonhosted.org flash-attn- Pre-built wheels are available on the GitHub releases page for Flash Attention.
- Download the wheel matching your Python, CUDA, and PyTorch versions, then install:
pip install --no-dependencies --upgrade <wheel-file>.whlBuilding from source is more flexible, but pre-built wheels install much faster if available for your configuration[^2].
Megatron is a large-scale language model training framework.
- Clone the repository:
git clone https://github.com/epfLLM/Megatron-LLM.git- Install build dependency:
pip install pybind11- Compile C++ helpers:
cd Megatron-LLM/megatron/data
makeThis compiles helpers.cpp into helpers.so, which is required by the Megatron codebase.
- Check CUDA compatibility: Ensure your CUDA toolkit matches the versions required by the packages.
- Linux is recommended: Most of these libraries are best supported on Linux. Windows support for Flash Attention is experimental[^1][^3].
- RAM and CPU: Building Flash Attention from source can be RAM- and CPU-intensive; adjust
MAX_JOBSif you encounter memory issues[^1].
| Component | Install Method | Notes |
|---|---|---|
| Python | 3.10 | PyTorch not compatible with 3.12 |
| PyTorch | pip, CUDA 11.8 wheel | Use official index URL |
| Apex | Source, patch required | Patch _initialize.py for Python 3.10 |
| Flash Attention | Source or pre-built wheel | Use ninja for speed; pre-built wheels are fastest |
| Megatron-LLM | Source, compile helpers | Requires pybind11 and make in megatron/data |
- Torch install fails: Double-check Python version and CUDA compatibility.
- Apex issues: Ensure the patch is applied for Python 3.10.
- Flash Attention build slow: Confirm
ninjais installed and working; limitMAX_JOBSif low on RAM. - SSL errors: Use the
--trusted-hostpip flags as shown above.