Phone LLM 37x Too Slow: Check CMake Build Type
In this article
If your on-device LLM runs 37 times slower than expected, the most likely cause is a CMake build type that disables compiler optimizations, such as Debug or an unset CMAKE_BUILD_TYPE. Switching to Release (or MinSizeRel for smaller binaries) typically restores expected performance, often bringing inference speed back to within a small factor of the optimized baseline.
Why Build Type Matters for LLM Inference
Large language models (LLMs) on phones rely on heavy matrix multiplication, quantization, and memory access patterns that are extremely sensitive to compiler optimizations. The CMake build type controls compiler flags like -O0 (no optimization) versus -O3 (aggressive optimization) and also enables or disables architecture-specific features (e.g., NEON on ARM, AVX on x86). A Debug build also adds debug symbols and disables inlining, which can slow down tight loops by an order of magnitude or more. In practice, the difference between an unoptimized and an optimized build of a transformer model can easily reach 30–50x on mobile CPUs, which matches the “37x” figure you observed.
How CMake Build Types Work
CMake uses the CMAKE_BUILD_TYPE variable to select a set of compiler flags. Common types include:
Debug:-O0 -g, no optimizations, full debug info.Release:-O3 -DNDEBUG, high optimization, no debug info.RelWithDebInfo:-O2 -g, optimized but with debug symbols.MinSizeRel:-Os -DNDEBUG, optimized for small binary size.
If you don’t set CMAKE_BUILD_TYPE, CMake may default to an empty string, which often results in no optimization flags being passed to the compiler. On some multi-config generators (like Xcode or Visual Studio), the build type is chosen at build time, but for single-config generators (like Unix Makefiles or Ninja), you must specify it during configuration.
How to Check Your Current Build Type
To see what build type was used for your existing build, look at the CMake cache file (CMakeCache.txt) in your build directory. Search for CMAKE_BUILD_TYPE. Alternatively, run cmake -L in the build directory to list cache variables. If it shows an empty value, that’s your problem. For a fresh build, explicitly set it:
- Delete your build directory (or create a new one).
- Run
cmake -DCMAKE_BUILD_TYPE=Release ..(adjust the path to your source). - Build and re-run your benchmark.
For Android projects using CMake via Gradle, set -DCMAKE_BUILD_TYPE=Release in your build.gradle under cmake.arguments or use the externalNativeBuild block. For iOS, you typically choose “Release” in Xcode’s scheme settings, which passes the corresponding flags to CMake.
Other Causes of Extreme Slowness
While build type is the most common culprit, a 37x slowdown can also stem from other issues. Check these if the build type is already correct:
- Quantization not enabled: If your model uses float32 instead of int8 or int4, inference can be several times slower. Ensure your model file is quantized and your runtime supports it.
- Wrong CPU architecture: If the binary is compiled for a generic ARMv7 target instead of ARMv8.2+ with NEON, you lose significant performance. Set
CMAKE_SYSTEM_PROCESSORappropriately. - Threading issues: If the runtime is single-threaded or thread count is set to 1, you’ll miss out on multi-core speedups. Set the number of threads to match your device’s cores.
- Memory bandwidth: On phones, memory bandwidth is often the bottleneck. Ensure your model fits in cache or use memory-mapped files to avoid excessive copying.
- Debug logging: Some runtimes enable verbose logging in debug builds, which adds overhead. Disable it in release builds.
How to Verify the Fix
After rebuilding with Release, run the same benchmark you used to measure the 37x slowdown. You should see a dramatic improvement. To confirm the compiler flags are applied, inspect the build command with make VERBOSE=1 (or ninja -v) and look for -O3 and -DNDEBUG. You can also check the generated flags.make file in the build directory. If the speed is still poor, profile the code to identify hotspots—often the issue is in a custom kernel that isn’t optimized for the target CPU.
Common Misconceptions
One common misconception is that RelWithDebInfo is as fast as Release. While it uses -O2, the lack of -DNDEBUG means assertions are still active, which can add overhead in hot paths. For LLM inference, prefer Release unless you need to debug. Another misconception is that build type only affects compile time, not runtime. In reality, the generated machine code can differ by an order of magnitude in performance. Finally, some developers assume that enabling -march=native is always beneficial, but on phones this can produce binaries that crash on older devices—use the appropriate target architecture flags instead.
Frequently Asked Questions
How do I set CMake build type in Android Studio?
In your module’s build.gradle file, inside the android block, add: defaultConfig { externalNativeBuild { cmake { arguments "-DCMAKE_BUILD_TYPE=Release" } } }. Then rebuild your project. The build type will be applied to all CMake targets.
Why does my CMake build default to Debug?
CMake does not automatically default to Debug; if CMAKE_BUILD_TYPE is not set, it remains empty, which often results in no optimization flags. Some IDEs (like Visual Studio) set it to Debug by default when you create a new project. Always explicitly set the build type in your CMake command or in your IDE’s settings.
Can I change build type without rebuilding everything?
Yes, you can re-run CMake with a new -DCMAKE_BUILD_TYPE in the same build directory, and it will regenerate the build files. However, object files from the previous build type may be stale, so it’s safer to clean or delete the build directory to ensure all files are recompiled with the new flags.
Is MinSizeRel a good choice for mobile LLMs?
MinSizeRel uses -Os, which optimizes for size rather than speed. For LLM inference, where speed is critical, Release (-O3) is usually better. If binary size is a hard constraint, test both and measure the trade-off—sometimes the difference is small, but often -O3 gives a noticeable speedup.
What if I’m using a prebuilt library that’s slow?
If you’re linking against a prebuilt library (e.g., a third-party LLM runtime), you can’t change its build type. You can still ensure your own code is optimized, and you can try using a different library build (many projects provide release binaries). If the library is open-source, rebuild it yourself with the correct flags.
Does the build type affect GPU inference?
Yes, but to a lesser extent. GPU kernels are often compiled at runtime (e.g., OpenCL or Metal shaders), so the CPU-side code that launches them benefits from optimization, but the heavy computation happens on the GPU. Still, a Debug build can add CPU overhead that reduces throughput, so always use Release for production.