|
| 1 | +--- |
| 2 | +name: building-ort-genai |
| 3 | +description: > |
| 4 | + Use this skill when building OnnxRuntime and onnxruntime-genai from |
| 5 | + source with CUDA support. Covers CUDA toolkit and cuDNN installation, |
| 6 | + ORT build flags, GenAI build linked to custom ORT, verification, |
| 7 | + and common build issues. |
| 8 | +--- |
| 9 | + |
| 10 | +# Skill: Building ORT and GenAI from Source |
| 11 | + |
| 12 | +## When to use |
| 13 | + |
| 14 | +Use this skill when: |
| 15 | +- You need a custom ORT build (e.g. unreleased features, CUDA support, |
| 16 | + custom ops) |
| 17 | +- You need a custom GenAI build linked to your ORT build |
| 18 | +- Deploying to Foundry Local requires overriding bundled ORT/GenAI |
| 19 | +- The pip-released ORT/GenAI version doesn't support your model |
| 20 | + |
| 21 | +## Prerequisites |
| 22 | + |
| 23 | +- Linux (tested on Ubuntu) |
| 24 | +- NVIDIA GPU |
| 25 | +- conda or any Python 3.10+ environment |
| 26 | +- ~20 GB disk space for builds |
| 27 | +- CMake 3.26+, gcc/g++ 11+ |
| 28 | + |
| 29 | +## Step 1: Install CUDA toolkit |
| 30 | + |
| 31 | +```bash |
| 32 | +# CUDA 13.0 |
| 33 | +wget https://developer.download.nvidia.com/compute/cuda/12.8.1/local_installers/cuda_13.0.1_575.51.03_linux.run |
| 34 | +sudo sh cuda_13.0.1_575.51.03_linux.run \ |
| 35 | + --toolkit --toolkitpath=$HOME/cuda13.0 \ |
| 36 | + --silent --override --no-man-page |
| 37 | + |
| 38 | +# Or adjust the URL and path for a different CUDA version |
| 39 | +``` |
| 40 | + |
| 41 | +Verify: |
| 42 | + |
| 43 | +```bash |
| 44 | +$HOME/cuda13.0/bin/nvcc --version |
| 45 | +``` |
| 46 | + |
| 47 | +## Step 2: Install cuDNN 9.x |
| 48 | + |
| 49 | +```bash |
| 50 | +wget https://developer.download.nvidia.com/compute/cudnn/redist/cudnn/linux-x86_64/cudnn-linux-x86_64-9.8.0.87_cuda13-archive.tar.xz |
| 51 | +mkdir -p $HOME/cudnn9.8 |
| 52 | +tar -xf cudnn-linux-x86_64-9.8.0.87_cuda13-archive.tar.xz \ |
| 53 | + -C $HOME/cudnn9.8 --strip-components=1 |
| 54 | +``` |
| 55 | + |
| 56 | +Alternative — install cuDNN via pip and create symlinks: |
| 57 | + |
| 58 | +```bash |
| 59 | +pip install nvidia-cudnn-cu13 |
| 60 | +mkdir -p ~/cudnn9/{lib,include} |
| 61 | +CUDNN_PKG=$(python -c "import nvidia.cudnn; import pathlib; print(pathlib.Path(nvidia.cudnn.__file__).parent)") |
| 62 | +ln -sf $CUDNN_PKG/lib/* ~/cudnn9/lib/ |
| 63 | +ln -sf $CUDNN_PKG/include/* ~/cudnn9/include/ |
| 64 | +# Then use ~/cudnn9 as CUDNN_HOME below |
| 65 | +``` |
| 66 | + |
| 67 | +## Step 3: Set environment |
| 68 | + |
| 69 | +```bash |
| 70 | +export PATH=$HOME/cuda13.0/bin:$PATH |
| 71 | +export LD_LIBRARY_PATH=$HOME/cuda13.0/lib64:$HOME/cudnn9.8/lib:$LD_LIBRARY_PATH |
| 72 | +``` |
| 73 | + |
| 74 | +Add these to your shell profile (`~/.bashrc`) or conda |
| 75 | +`activate.d/env_vars.sh` for persistence. |
| 76 | + |
| 77 | +## Step 4: Build ORT from source |
| 78 | + |
| 79 | +```bash |
| 80 | +git clone https://github.com/microsoft/onnxruntime.git ~/dev/onnxruntime |
| 81 | +cd ~/dev/onnxruntime |
| 82 | + |
| 83 | +./build.sh \ |
| 84 | + --config Release \ |
| 85 | + --use_cuda \ |
| 86 | + --cuda_home $HOME/cuda13.0 \ |
| 87 | + --cudnn_home $HOME/cudnn9.8 \ |
| 88 | + --cmake_extra_defines \ |
| 89 | + CMAKE_CUDA_ARCHITECTURES=native \ |
| 90 | + onnxruntime_USE_FLASH_ATTENTION=ON \ |
| 91 | + --build_wheel \ |
| 92 | + --enable_pybind \ |
| 93 | + --parallel \ |
| 94 | + --skip_tests |
| 95 | +``` |
| 96 | + |
| 97 | +### Build flag reference |
| 98 | + |
| 99 | +| Flag | Description | |
| 100 | +|------|-------------| |
| 101 | +| `--use_cuda` | Enable CUDA execution provider | |
| 102 | +| `--cuda_home <path>` | Path to CUDA toolkit installation | |
| 103 | +| `--cudnn_home <path>` | Path to cuDNN directory (lib/ + include/) | |
| 104 | +| `CMAKE_CUDA_ARCHITECTURES=native` | Compile for the GPU on this machine | |
| 105 | +| `onnxruntime_USE_FLASH_ATTENTION=ON` | Enable Flash Attention kernels (requires compute ≥ 8.0) | |
| 106 | +| `--build_wheel` | Build a pip-installable wheel | |
| 107 | +| `--parallel` | Parallel compilation | |
| 108 | +| `--skip_tests` | Skip test targets (may fail on abseil linking) | |
| 109 | + |
| 110 | +### Install |
| 111 | + |
| 112 | +```bash |
| 113 | +pip install build/Linux/Release/dist/onnxruntime-*.whl \ |
| 114 | + --force-reinstall --no-deps |
| 115 | +``` |
| 116 | + |
| 117 | +> **Note:** If the build fails on test targets (`onnxruntime_perf_test`) |
| 118 | +> but the wheel was produced, you can still install it. Alternatively, |
| 119 | +> build the wheel manually: |
| 120 | +> ```bash |
| 121 | +> cd build/Linux/Release |
| 122 | +> python ~/dev/onnxruntime/setup.py bdist_wheel |
| 123 | +> pip install dist/onnxruntime-*.whl --force-reinstall --no-deps |
| 124 | +> ``` |
| 125 | +
|
| 126 | +## Step 5: Create ORT install layout for GenAI |
| 127 | +
|
| 128 | +GenAI needs ORT headers and libraries in a specific layout: |
| 129 | +
|
| 130 | +```bash |
| 131 | +mkdir -p ~/ort-install/{include,lib} |
| 132 | +
|
| 133 | +# Headers |
| 134 | +cp ~/dev/onnxruntime/include/onnxruntime/core/session/*.h \ |
| 135 | + ~/ort-install/include/ |
| 136 | +
|
| 137 | +# Libraries |
| 138 | +cp ~/dev/onnxruntime/build/Linux/Release/libonnxruntime.so \ |
| 139 | + ~/ort-install/lib/ |
| 140 | +cp ~/dev/onnxruntime/build/Linux/Release/libonnxruntime_providers_cuda.so \ |
| 141 | + ~/ort-install/lib/ |
| 142 | +cp ~/dev/onnxruntime/build/Linux/Release/libonnxruntime_providers_shared.so \ |
| 143 | + ~/ort-install/lib/ |
| 144 | +``` |
| 145 | +
|
| 146 | +## Step 6: Build GenAI from source |
| 147 | + |
| 148 | +```bash |
| 149 | +git clone https://github.com/microsoft/onnxruntime-genai.git \ |
| 150 | + ~/dev/onnxruntime-genai |
| 151 | +cd ~/dev/onnxruntime-genai |
| 152 | + |
| 153 | +python build.py \ |
| 154 | + --config Release \ |
| 155 | + --use_cuda \ |
| 156 | + --cuda_home $HOME/cuda13.0 \ |
| 157 | + --ort_home ~/ort-install \ |
| 158 | + --parallel \ |
| 159 | + --skip_tests \ |
| 160 | + --skip_examples \ |
| 161 | + --cmake_extra_defines CMAKE_CUDA_ARCHITECTURES=native \ |
| 162 | + --update --build |
| 163 | +``` |
| 164 | + |
| 165 | +### Install |
| 166 | + |
| 167 | +```bash |
| 168 | +pip install build/Linux/Release/wheel/onnxruntime_genai_cuda-*.whl \ |
| 169 | + --no-deps |
| 170 | +``` |
| 171 | + |
| 172 | +## Step 7: Verify |
| 173 | + |
| 174 | +```bash |
| 175 | +python -c 'import onnxruntime; print(onnxruntime.__version__, onnxruntime.get_device())' |
| 176 | +# Expected: 1.x.x GPU |
| 177 | + |
| 178 | +python -c 'import onnxruntime_genai as og; print(og.__version__, og.is_cuda_available())' |
| 179 | +# Expected: 0.x.x True |
| 180 | +``` |
| 181 | + |
| 182 | +If `get_device()` returns `CPU` or `is_cuda_available()` returns |
| 183 | +`False`, check `LD_LIBRARY_PATH` and that you installed the correct |
| 184 | +wheels (not pip overrides — see below). |
| 185 | + |
| 186 | +## Common issues |
| 187 | + |
| 188 | +### 1. cuDNN version mismatch |
| 189 | + |
| 190 | +**Symptom:** ORT build fails with cuDNN errors, or CUDA EP doesn't |
| 191 | +load at runtime. |
| 192 | + |
| 193 | +**Fix:** cuDNN 9.x works with CUDA 12.x and 13.x. Ensure the cuDNN |
| 194 | +version matches your CUDA major version: |
| 195 | + |
| 196 | +```bash |
| 197 | +# Check installed cuDNN version |
| 198 | +python -c "import nvidia.cudnn; print(nvidia.cudnn.__version__)" |
| 199 | +``` |
| 200 | + |
| 201 | +### 2. LD_LIBRARY_PATH not set |
| 202 | + |
| 203 | +**Symptom:** `import onnxruntime` fails with `.so` not found, or |
| 204 | +CUDA EP missing from providers. |
| 205 | + |
| 206 | +**Fix:** |
| 207 | + |
| 208 | +```bash |
| 209 | +export LD_LIBRARY_PATH=$HOME/cuda13.0/lib64:$HOME/cudnn9.8/lib:$LD_LIBRARY_PATH |
| 210 | +``` |
| 211 | + |
| 212 | +### 3. pip packages overriding custom builds |
| 213 | + |
| 214 | +**Symptom:** After `pip install foundry-local-sdk` or other packages, |
| 215 | +custom build is replaced. `ort.get_device()` returns `'CPU'`. |
| 216 | + |
| 217 | +**Fix:** Always reinstall custom wheels after installing packages that |
| 218 | +depend on ORT: |
| 219 | + |
| 220 | +```bash |
| 221 | +pip install foundry-local-sdk |
| 222 | +# THEN reinstall your builds: |
| 223 | +pip install --force-reinstall <your_ort_wheel>.whl |
| 224 | +pip install --force-reinstall <your_genai_wheel>.whl |
| 225 | +``` |
| 226 | + |
| 227 | +See the **foundry-local** skill for the full dependency override |
| 228 | +mechanism. |
| 229 | + |
| 230 | +### 4. ABI mismatch between ORT and GenAI |
| 231 | + |
| 232 | +**Symptom:** GenAI build fails with undefined symbols or linker errors. |
| 233 | + |
| 234 | +**Fix:** Both must be built with the same compiler, Python version, |
| 235 | +and C++ ABI. The ORT install layout (Step 5) must use the exact |
| 236 | +libraries from your ORT build. |
| 237 | + |
| 238 | +### 5. Flash Attention not available |
| 239 | + |
| 240 | +**Symptom:** Attention ops are slow or fall back to non-fused path. |
| 241 | + |
| 242 | +**Fix:** Ensure `onnxruntime_USE_FLASH_ATTENTION=ON` was set during |
| 243 | +the ORT build. Requires GPU compute capability ≥ 8.0 (Ampere+). |
| 244 | + |
| 245 | +## Reference |
| 246 | + |
| 247 | +- [Full Gemma4 + Foundry Local tutorial](https://github.com/onnxruntime/mobius/issues/245) |
| 248 | +- [ORT build documentation](https://onnxruntime.ai/docs/build/) |
| 249 | +- [GenAI build documentation](https://github.com/microsoft/onnxruntime-genai/blob/main/BUILD.md) |
| 250 | + |
| 251 | +## Cross-references |
| 252 | + |
| 253 | +- **Foundry Local deployment:** `.agents/skills/foundry-local/SKILL.md` |
| 254 | +- **ONNX export:** `.agents/skills/onnx-export-quantization/SKILL.md` |
| 255 | +- **ORT GenAI config:** `.agents/skills/ort-genai-config/SKILL.md` |
0 commit comments