Android build broken on main: TVM/mlc_llm version skew + tvm_runtime.h out of sync with upstream TVM reorg
Bug
Summary Building the Android target (mlc_llm package + android/mlc4j) is currently broken on a fresh clone of main (tested on Windows, but items 2 and 4 below are platform-independent). Root causes fall into two groups:
Version skew between the pinned 3rdparty/tvm submodule and what python/mlc_llm (and the mlc-ai-nightly/mlc-llm-nightly wheel pair) expect. android/mlc4j/src/cpp/tvm_runtime.h amalgamation header is out of sync with upstream TVM's internal reorganization (file moves/renames/removals), so the Android/OpenCL CMake build fails outright regardless of platform. Environment: Windows 11, conda env, Python 3.13, mlc-ai-nightly-cpu==0.26.dev246, mlc-llm-nightly-cpu==0.26.dev6, 3rdparty/tvm pinned at 837cb9de1 (mlc-ai/relax, branch mlc), Android NDK r30 (30.0.16248370).
- Native crash (access violation) during Dlight lowering Symptom: mlc_llm compile/package crashes with a native access violation (0xC0000005 on Windows) inside tvm.s_tir.dlight...compute_inline, while running with apache-tvm-ffi==0.1.14.post0 (the version mlc-ai-nightly-cpu==0.26.dev246 declares as Requires-Dist: apache-tvm-ffi>=0.1.13).
Root cause: ABI mismatch between the installed apache-tvm-ffi Python package and the compiled tvm_compiler.dll/tvm_runtime.dll shipped in the mlc-ai-nightly-cpu wheel for this snapshot.
Workaround: pinning apache-tvm-ffi==0.1.13 exactly avoids the crash. Note this version is currently yanked on PyPI (no reason given); the .post0–.post3 patch releases of 0.1.13 do not work (crash returns), only the base 0.1.13 release does.
Ask: should the mlc-ai-nightly-cpu wheel pin/declare a narrower/exact apache-tvm-ffi version instead of >=0.1.13? A yanked release being the only functional one is a packaging trap for anyone following the install docs literally.
- AttributeError: module 'tvm.tirx' has no attribute 'is_buffer_var' Symptom: compilation fails during the LowBatchGemvSpecialize pass with tirx.is_buffer_var not existing.
Root cause: commit 9fa644f5 ("[Fix] Adapt compiler passes to typed TIR buffer parameters (#3534)", 2026-08-17) changed python/mlc_llm/compiler_pass/{low_batch_specialization,fuse_dequantize_transpose,lift_global_buffer_alloc}.py to use a "typed TIR buffer parameters" TVM API (tirx.is_buffer_var, PrimFunc.params containing Buffer objects directly instead of Var + buffer_map). This API does not exist anywhere — not in Python, not in C++/FFI-registered — in the pinned 3rdparty/tvm submodule commit (837cb9de1), nor in the currently published mlc-ai-nightly-cpu==0.26.dev246 wheel (checked via dir(tvm.tirx) at runtime). origin/main of the mlc-llm repo has no newer commit fixing this; the tvm submodule's own mlc and mlc-staging branches on mlc-ai/relax also don't define is_buffer_var anywhere in their trees.
Workaround used locally: revert python/mlc_llm/compiler_pass/{low_batch_specialization,fuse_dequantize_transpose,lift_global_buffer_alloc}.py to their state immediately before commit 9fa644f5 (i.e. git checkout 9fa644f5~1 -- <those 3 files>). This restores the old func.buffer_map-based logic, which matches what the pinned TVM commit actually produces, and compilation succeeds end-to-end (tested with Qwen2.5-Coder-7B-Instruct-q4f16_1-MLC and Llama-3.1-8B-Instruct-q4f16_1-MLC, --device android).
Ask: this looks like commit 9fa644f5 landed ahead of the TVM submodule bump it depends on. Should 3rdparty/tvm be bumped to a commit that actually defines tirx.is_buffer_var, or should 9fa644f5 be reverted until that's ready?
- TypeError: Cannot convert from type 'ffi.SmallStr' to 'ffi.Object' (minor / edge case) Symptom: relax.build(..., system_lib=True) → auto_attach_system_lib_prefix → IRModule.get_attr("system_lib_prefix") raises this TypeError, but only when the --system-lib-prefix value is short (e.g. a manual test with "test"). With the long, hash-based prefix that mlc_llm package's JIT path actually generates (e.g. qwen2_q4f16_1_51f64c2c4ea9d28aba6bc1a710c4e0e6_), it does not reproduce — likely a small-string-optimization (SmallStr) code path in tvm_ffi's Any::cast that isn't handled for Map<String, ObjectRef> attribute retrieval in this exact tvm_ffi/TVM build pairing.
Impact: none in practice for mlc_llm package's real flow (prefixes are always long), but could surprise anyone calling mlc_llm compile --system-lib-prefix manually with a short value. Flagging in case it points to a real latent bug worth fixing at the source.
- android/mlc4j CMake/C++ build is broken (unrelated to items 1–3) android/mlc4j/src/cpp/tvm_runtime.h directly #includes a hand-maintained list of TVM .cc files (an "amalgamation" build for the JNI wrapper). Several of these have moved/renamed/disappeared upstream in TVM without this list being updated:
cmake/modules/Logging.cmake:26: target_compile_definitions(tvm_libinfo_objs PUBLIC TVM_LOG_CUSTOMIZE=1) references a target that is not defined anywhere in the CMake graph (confirmed via grep across the whole 3rdparty/tvm tree) — this makes cmake configure fail outright with USE_CUSTOM_LOGGING=ON (which android/mlc4j/prepare_libs.py always passes). logging.cc already gets the macro correctly via tvm_objs/tvm_runtime_objs on the two lines above, so this line appears to be dead/orphaned. runtime/nvtx.cc: no longer exists; nvtx.h was made header-only and relocated to tvm/support/cuda by commit 66a4bcd40 ([REFACTOR][RUNTIME] Relocate nvtx.h to tvm/support/cuda and make it header-only (#19621)). runtime/opencl/{opencl_device_api.cc,opencl_module.cc,opencl_wrapper/opencl_wrapper.cc}: the whole src/runtime/opencl/ directory was relocated to src/backend/opencl/runtime/. runtime/profiling.cc: renamed to runtime/timer.cc by commit 4e5b869c2 ([REFACTOR][RUNTIME] Phase out profiling.h heavy types, rename to timer.h (#19455)). runtime/source_utils.cc: no longer exists as a .cc; became backend/opencl/runtime/source_utils.h (header-only) per commit f42a279b5 ([REFACTOR] Move source_utils.h into runtime/opencl (#19456)) — already #included internally by opencl_device_api.cc, so no replacement line is needed, just removal. tvm::runtime::InternalError: this class no longer exists (error handling was migrated to tvm::ffi::Error, e.g. [REFACTOR] Migrate CHECK macros to tvm-ffi ones (#18803)). tvm_runtime.h's custom LogFatalImpl override (used to redirect fatal errors to __android_log_write) still does throw InternalError(file, lineno, message);, which no longer compiles. Missing entries: ffi/init_once.cc (defines TVMFFIHandleInitOnce) and ffi/custom_allocator.cc (defines TVMFFIGetCustomAllocator) are needed at link time but were never in the include list — linking libtvm4j_runtime_packed.so fails with undefined symbol: TVMFFIHandleInitOnce / TVMFFIGetCustomAllocator. Fixes applied locally (all verified — the Android build completes and links successfully end-to-end with these + the item-2 workaround above):
cmake/modules/Logging.cmake:
target_compile_definitions(tvm_objs PUBLIC TVM_LOG_CUSTOMIZE=1) target_compile_definitions(tvm_runtime_objs PUBLIC TVM_LOG_CUSTOMIZE=1)
- target_compile_definitions(tvm_libinfo_objs PUBLIC TVM_LOG_CUSTOMIZE=1)
- if(TARGET tvm_libinfo_objs)
- target_compile_definitions(tvm_libinfo_objs PUBLIC TVM_LOG_CUSTOMIZE=1)
- endif() target_compile_definitions(tvm_compiler PUBLIC TVM_LOG_CUSTOMIZE=1) android/mlc4j/src/cpp/tvm_runtime.h:
#include <ffi/container.cc> +#include <ffi/custom_allocator.cc> #include <ffi/dtype.cc> ... #include <ffi/function.cc> +#include <ffi/init_once.cc> #include <ffi/object.cc> ... #include <runtime/module.cc> -#include <runtime/nvtx.cc> -#include <runtime/opencl/opencl_device_api.cc> -#include <runtime/opencl/opencl_module.cc> -#include <runtime/opencl/opencl_wrapper/opencl_wrapper.cc> -#include <runtime/profiling.cc> -#include <runtime/source_utils.cc> +#include <backend/opencl/runtime/opencl_device_api.cc> +#include <backend/opencl/runtime/opencl_module.cc> +#include <backend/opencl/runtime/opencl_wrapper/opencl_wrapper.cc> +#include <runtime/timer.cc> ...
- throw InternalError(file, lineno, message);
- throw ::tvm::ffi::Error("InternalError", message,
::TVMFFIBacktrace(file.c_str(), lineno, "", 0));
(mirrors the equivalent pattern already used in tvm/runtime/logging.h's LogMessage::Entry::Finalize)
Reproduction git clone https://github.com/mlc-ai/mlc-llm.git cd mlc-llm && git submodule update --init --recursive conda create -n repro python=3.13 -y && conda activate repro python -m pip install --pre -U -f https://mlc.ai/wheels mlc-llm-nightly-cpu mlc-ai-nightly-cpu cd android/MLCChat export ANDROID_NDK=/path/to/Sdk/ndk/30.0.16248370 export TVM_NDK_CC=$ANDROID_NDK/toolchains/llvm/prebuilt//bin/aarch64-linux-android24-clang export TVM_SOURCE_DIR=/path/to/mlc-llm/3rdparty/tvm export MLC_LLM_SOURCE_DIR=/path/to/mlc-llm mlc_llm package Fails first at item 2 (is_buffer_var); after working around that, fails at item 4.
Happy to open a PR for the item 4 fixes (they're unambiguous and don't change behavior on platforms where the build currently happens to work). Items 1–3 feel like they need a maintainer call on whether to bump the 3rdparty/tvm submodule / adjust the nightly wheel pinning instead of reverting mlc_llm's own typed-buffer-params migration.
Source: mlc-ai/mlc-llm