|
| 1 | +# Build System (`CMakeLists.txt`) |
| 2 | + |
| 3 | +LEAP uses **CMake 4.1+** with C++20, targeting multi-platform builds (Linux, macOS, potentially Windows). The build produces three executables, one static library, and an optional Linux kernel module. |
| 4 | + |
| 5 | +## Build Targets |
| 6 | + |
| 7 | +| Target | Type | Description | |
| 8 | +|--------|------|-------------| |
| 9 | +| `export` | Executable | Model conversion & quantization tool | |
| 10 | +| `inference` | Executable | High-performance inference runtime | |
| 11 | +| `tokenizer` | Executable | Tokenizer model exporter | |
| 12 | +| `model` | Static Library | LibTorch-based Llama model definitions | |
| 13 | +| `leap_kmod` | Kernel Module | Zero-copy networking (Linux only, optional) | |
| 14 | + |
| 15 | +--- |
| 16 | + |
| 17 | +## Dependencies |
| 18 | + |
| 19 | +| Dependency | Version | Targets | Source | |
| 20 | +|-----------|---------|---------|--------| |
| 21 | +| **LibTorch** | 2.4+ | `export`, `model`, `tokenizer` | Manual download or pip | |
| 22 | +| **nlohmann/json** | 3.12+ | `export` | FetchContent (auto-downloaded) | |
| 23 | +| **safetensors-cpp** | latest | `export` | FetchContent (Git) | |
| 24 | +| **CLI11** | 2.6.1 | `export`, `inference`, `tokenizer` | FetchContent (Git) | |
| 25 | +| **tokenizers (tiktoken-cpp)** | — | `tokenizer` | Git submodule (`third-party/tokenizers`) | |
| 26 | +| **OpenMP** | — | All targets | System (Linux) or Homebrew (macOS) | |
| 27 | + |
| 28 | +### LibTorch |
| 29 | + |
| 30 | +The most significant dependency. Required for the export pipeline but **not** for inference. |
| 31 | + |
| 32 | +**Option A: Direct Download (x86\_64)** |
| 33 | +```bash |
| 34 | +wget https://download.pytorch.org/libtorch/cpu/libtorch-cxx11-abi-shared-with-deps-2.4.0%2Bcpu.zip |
| 35 | +unzip libtorch-*.zip -d third-party/ |
| 36 | +``` |
| 37 | + |
| 38 | +**Option B: Via Python pip** |
| 39 | +```bash |
| 40 | +pip install torch --index-url https://download.pytorch.org/whl/cpu |
| 41 | +TORCH_PATH=$(python3 -c 'import torch; print(torch.utils.cmake_prefix_path)') |
| 42 | +cmake -DCMAKE_PREFIX_PATH=$TORCH_PATH ... |
| 43 | +``` |
| 44 | + |
| 45 | +> **Note:** Option B is recommended for ARM platforms (Raspberry Pi, Jetson) where pre-built LibTorch binaries aren't available. |
| 46 | +
|
| 47 | +--- |
| 48 | + |
| 49 | +## Compiler Flags |
| 50 | + |
| 51 | +### Release Optimization Flags |
| 52 | + |
| 53 | +| Platform | Flags | |
| 54 | +|----------|-------| |
| 55 | +| GCC/Clang | `-O3 -march=native -mtune=native -ffast-math -funroll-loops -fomit-frame-pointer` | |
| 56 | +| MSVC | `/O2 /arch:AVX2 /fp:fast` | |
| 57 | + |
| 58 | +Key flags: |
| 59 | +- **`-march=native`**: Enables all SIMD instructions supported by the build machine (AVX2, NEON, etc.). |
| 60 | +- **`-ffast-math`**: Allows aggressive FP optimizations (breaks strict IEEE compliance — acceptable for inference workloads). |
| 61 | +- **`-funroll-loops`**: Aggressive loop unrolling for better pipeline utilization. |
| 62 | + |
| 63 | +### Link-Time Optimization (LTO) |
| 64 | + |
| 65 | +LTO/IPO is automatically detected and enabled for Release builds via `check_ipo_supported()`. This allows cross-translation-unit inlining and dead code elimination. |
| 66 | + |
| 67 | +### Exception Handling |
| 68 | + |
| 69 | +LibTorch's default build flags disable exceptions (`-fno-exceptions`). LEAP's CMake configuration explicitly strips these flags and forces: |
| 70 | +``` |
| 71 | +-fexceptions -frtti -fvisibility=default |
| 72 | +``` |
| 73 | + |
| 74 | +This ensures C++ exception handling works correctly throughout all targets. |
| 75 | + |
| 76 | +--- |
| 77 | + |
| 78 | +## OpenMP Configuration |
| 79 | + |
| 80 | +### Linux |
| 81 | +Standard `find_package(OpenMP)` — links `OpenMP::OpenMP_CXX`. |
| 82 | + |
| 83 | +### macOS (Homebrew) |
| 84 | +macOS requires special handling because Apple Clang doesn't ship OpenMP: |
| 85 | +1. Detects Homebrew's `libomp` prefix via `brew --prefix libomp`. |
| 86 | +2. Falls back to hardcoded paths (`/opt/homebrew/opt/libomp` or `/usr/local/opt/libomp`). |
| 87 | +3. Adds compile flags: `-Xpreprocessor -fopenmp`. |
| 88 | +4. Links the `omp` library directly. |
| 89 | + |
| 90 | +```bash |
| 91 | +# Required on macOS |
| 92 | +brew install libomp |
| 93 | +``` |
| 94 | + |
| 95 | +--- |
| 96 | + |
| 97 | +## Kernel Module Build |
| 98 | + |
| 99 | +Enabled via the `BUILD_KERNEL_MODULE` CMake option (Linux only): |
| 100 | + |
| 101 | +```bash |
| 102 | +cmake -DBUILD_KERNEL_MODULE=ON ... |
| 103 | +``` |
| 104 | + |
| 105 | +This creates a custom target that invokes the kernel's build system: |
| 106 | +```cmake |
| 107 | +add_custom_target(leap_kmod ALL |
| 108 | + COMMAND make -C ${CMAKE_CURRENT_SOURCE_DIR}/src/kernel |
| 109 | +) |
| 110 | +``` |
| 111 | + |
| 112 | +**Requirements:** |
| 113 | +- Linux kernel headers: `sudo apt install linux-headers-$(uname -r)` |
| 114 | + |
| 115 | +--- |
| 116 | + |
| 117 | +## Build Instructions |
| 118 | + |
| 119 | +### Quick Start |
| 120 | + |
| 121 | +```bash |
| 122 | +# Clone with submodules |
| 123 | +git clone --recursive https://github.com/Harikeshav-R/LEAP.git |
| 124 | +cd LEAP |
| 125 | + |
| 126 | +# Configure (choose one LibTorch method) |
| 127 | +cmake -S . -B build -DCMAKE_BUILD_TYPE=Release \ |
| 128 | + -DCMAKE_PREFIX_PATH=$(pwd)/third-party/libtorch |
| 129 | + |
| 130 | +# Build all targets |
| 131 | +cmake --build build --config Release -- -j$(nproc) |
| 132 | +``` |
| 133 | + |
| 134 | +### Build with Kernel Module (Linux) |
| 135 | + |
| 136 | +```bash |
| 137 | +cmake -S . -B build -DCMAKE_BUILD_TYPE=Release \ |
| 138 | + -DBUILD_KERNEL_MODULE=ON \ |
| 139 | + -DCMAKE_PREFIX_PATH=$(pwd)/third-party/libtorch |
| 140 | + |
| 141 | +cmake --build build --config Release -- -j$(nproc) |
| 142 | +``` |
| 143 | + |
| 144 | +### macOS |
| 145 | + |
| 146 | +```bash |
| 147 | +brew install libomp cmake |
| 148 | + |
| 149 | +# Using pip-installed PyTorch |
| 150 | +pip install torch --index-url https://download.pytorch.org/whl/cpu |
| 151 | +TORCH_PATH=$(python3 -c 'import torch; print(torch.utils.cmake_prefix_path)') |
| 152 | + |
| 153 | +cmake -S . -B build -DCMAKE_BUILD_TYPE=Release \ |
| 154 | + -DCMAKE_PREFIX_PATH=$TORCH_PATH |
| 155 | + |
| 156 | +cmake --build build --config Release -- -j$(sysctl -n hw.ncpu) |
| 157 | +``` |
| 158 | + |
| 159 | +--- |
| 160 | + |
| 161 | +## Dependency Graph |
| 162 | + |
| 163 | +```mermaid |
| 164 | +graph TD |
| 165 | + EXPORT["export"] --> MODEL["model (lib)"] |
| 166 | + EXPORT --> TORCH["LibTorch"] |
| 167 | + EXPORT --> JSON["nlohmann/json"] |
| 168 | + EXPORT --> SAFETENSORS["safetensors-cpp"] |
| 169 | + EXPORT --> CLI11["CLI11"] |
| 170 | + EXPORT --> OMP["OpenMP"] |
| 171 | +
|
| 172 | + MODEL --> TORCH |
| 173 | +
|
| 174 | + INF["inference"] --> CLI11 |
| 175 | + INF --> OMP |
| 176 | +
|
| 177 | + TOK["tokenizer"] --> TORCH |
| 178 | + TOK --> TIKTOK["tiktoken-cpp"] |
| 179 | + TOK --> CLI11 |
| 180 | + TOK --> OMP |
| 181 | +
|
| 182 | + KMOD["leap_kmod"] --> KHEADERS["Linux Kernel Headers"] |
| 183 | +``` |
| 184 | + |
| 185 | +--- |
| 186 | + |
| 187 | +## Troubleshooting |
| 188 | + |
| 189 | +### LibTorch ABI Mismatch |
| 190 | +**Symptom:** Linker errors with `std::__cxx11::basic_string` |
| 191 | +**Fix:** Use the **cxx11 ABI** version of LibTorch. If using pip, ensure GCC versions match. |
| 192 | + |
| 193 | +### Kernel Module Load Failure |
| 194 | +**Symptom:** `Operation not permitted` on `insmod` |
| 195 | +**Fix:** Disable Secure Boot or sign the module. |
| 196 | + |
| 197 | +**Symptom:** `Exec format error` |
| 198 | +**Fix:** Module compiled for a different kernel. Rebuild after `sudo apt install linux-headers-$(uname -r)`. |
| 199 | + |
| 200 | +### OpenMP Not Found (macOS) |
| 201 | +**Symptom:** CMake error about libomp |
| 202 | +**Fix:** `brew install libomp` |
0 commit comments