-
Notifications
You must be signed in to change notification settings - Fork 2.1k
[Bug] Raise an actionable error when libmlc_llm shared dependencies are missing #3510
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pjdurden
wants to merge
2
commits into
mlc-ai:main
Choose a base branch
from
pjdurden:fix/3506
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+110
−10
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| # Issue #3506 — `OSError: libtvm.so: cannot open shared object file` | ||
|
|
||
| ## 1. Root cause | ||
|
|
||
| `libtvm.so` is **not** part of the `mlc-llm` wheel. It is shipped by the separate | ||
| `mlc-ai` / TVM package. This is deliberate: `ci/task/build_lib.sh` runs | ||
| `auditwheel repair` with `--exclude libtvm --exclude libtvm_runtime --exclude | ||
| libtvm_ffi ...`, so the mlc-llm wheel only bundles `libmlc_llm.so` / | ||
| `libmlc_llm_module.so`, both of which link against `libtvm.so` at load time. | ||
|
|
||
| At import, `python/mlc_llm/base.py` loads `libmlc_llm.so` via `ctypes.CDLL`. When the | ||
| matching `mlc-ai` package is missing, incomplete, or its CUDA variant does not match | ||
| (the exact situation reported for the CUDA 13.0 nightly wheels), the dynamic linker | ||
| cannot resolve the `libtvm.so` dependency and `ctypes.CDLL` raises a bare | ||
| `OSError: libtvm.so: cannot open shared object file: No such file or directory`. | ||
|
|
||
| The underlying missing-file problem lives in the `mlc-ai`/TVM wheel packaging, which is | ||
| built and published **outside this repository** — there is no `libtvm.so`-producing | ||
| build in mlc-llm to fix here. What *is* in this repo's control is the loader's behavior: | ||
| it turns a resolvable-diagnosis situation ("your `mlc-ai` install is missing/mismatched") | ||
| into an opaque `OSError` that gives the user no path forward. That unhelpful failure is | ||
| the fixable defect. | ||
|
|
||
| ## 2. The fix and why | ||
|
|
||
| Added `load_lib(path)` to `python/mlc_llm/libinfo.py` and routed `base.py`'s load | ||
| through it. It wraps `ctypes.CDLL` and, on `OSError`, re-raises a `RuntimeError` that: | ||
|
|
||
| - preserves the original loader error (kept as `__cause__` and in the message) for | ||
| debugging, and | ||
| - explains the actual cause and fix: a matching `mlc-ai` package must be installed, and | ||
| for pip wheels the CUDA variant must match (`mlc-ai-nightly-cuXYZ` alongside | ||
| `mlc-llm-nightly-cuXYZ`), with a link to the install docs. | ||
|
|
||
| This mirrors the existing convention in the same file: `find_lib_path(..., optional=False)` | ||
| already raises a descriptive `RuntimeError` (with candidate paths) when the mlc-llm | ||
| library itself is absent. The change extends the same "clear, actionable error" treatment | ||
| to the missing-**dependency** case. | ||
|
|
||
| The helper was placed in `libinfo.py` (not `base.py`) on purpose: `libinfo.py` is | ||
| standalone — it imports only `os`/`sys`/`ctypes`, has no `tvm` dependency, and is even | ||
| `exec`-ed directly by `setup.py`. That keeps the new logic unit-testable without a fully | ||
| built `mlc_llm`/`tvm` install. `base.py` no longer references `ctypes` directly, so that | ||
| now-unused import was removed. | ||
|
|
||
| ## 3. Files changed | ||
|
|
||
| - `python/mlc_llm/libinfo.py` — add `import ctypes`; add `load_lib(path)` helper. | ||
| - `python/mlc_llm/base.py` — call `libinfo.load_lib(...)` instead of `ctypes.CDLL(...)`; | ||
| drop the now-unused `import ctypes`. | ||
| - `tests/python/test_libinfo.py` — new focused unit test. | ||
|
|
||
| ## 4. Risk / uncertainty | ||
|
|
||
| - **Low behavioral risk.** On the success path `load_lib` returns exactly what | ||
| `ctypes.CDLL` returned; only the *error* path changes (a clearer `RuntimeError` in place | ||
| of the raw `OSError`). Callers of `_load_mlc_llm_lib` did not catch `OSError` | ||
| specifically, so no error-handling contract is broken. | ||
| - **Scope caveat (honest):** this does not make CUDA 13.0 wheels ship `libtvm.so` — that | ||
| requires a change in the `mlc-ai`/TVM wheel build, which is not in this repository. This | ||
| fix converts the confusing symptom into a self-service diagnostic that points users to | ||
| the real remedy; it is a robustness/UX fix, not a repackaging of the upstream wheel. | ||
| - I could not exercise the real dlopen failure locally because `tvm`/`mlc_llm` are not | ||
| installed in this environment; the test simulates the `OSError` from `ctypes.CDLL` | ||
| instead (see below). | ||
|
|
||
| ## 5. How I verified | ||
|
|
||
| - `python3 -m pytest tests/python/test_libinfo.py -v` → 2 passed. Covers both the | ||
| missing-dependency error path (asserts the message retains `libtvm.so`, mentions | ||
| `mlc-ai`, and chains the original `OSError` as `__cause__`) and the success path. | ||
| - Confirmed `libinfo.py` still loads two ways: via `importlib` and via the exact | ||
| `exec(compile(...))` pattern `setup.py` uses (so packaging is unaffected). | ||
| - `python3 -m py_compile` on all three files, and `ruff check` on them → all checks passed. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| """Unit tests for :mod:`mlc_llm.libinfo`. | ||
|
|
||
| ``libinfo.py`` is intentionally standalone (it is also ``exec``-ed by ``setup.py`` | ||
| before the package is importable), so it is loaded here directly from its file path. | ||
| This keeps the test independent of a fully built ``mlc_llm`` / ``tvm`` install. | ||
| """ | ||
|
|
||
| import importlib.util | ||
| import os | ||
|
|
||
| import pytest | ||
|
|
||
| pytestmark = [pytest.mark.unittest] | ||
|
|
||
| _LIBINFO_PATH = os.path.join( | ||
| os.path.dirname(__file__), "..", "..", "python", "mlc_llm", "libinfo.py" | ||
| ) | ||
|
|
||
|
|
||
| def _load_libinfo(): | ||
| spec = importlib.util.spec_from_file_location("mlc_llm_libinfo_standalone", _LIBINFO_PATH) | ||
| module = importlib.util.module_from_spec(spec) | ||
| spec.loader.exec_module(module) | ||
| return module | ||
|
|
||
|
|
||
| def test_load_lib_reports_missing_dependency(monkeypatch): | ||
| """A failed dependency resolution should surface an actionable RuntimeError.""" | ||
| libinfo = _load_libinfo() | ||
|
|
||
| def _raise_missing_dep(_path): | ||
| raise OSError("libtvm.so: cannot open shared object file: No such file or directory") | ||
|
|
||
| monkeypatch.setattr(libinfo.ctypes, "CDLL", _raise_missing_dep) | ||
|
|
||
| with pytest.raises(RuntimeError) as exc_info: | ||
| libinfo.load_lib("/some/path/libmlc_llm.so") | ||
|
|
||
| message = str(exc_info.value) | ||
| # The original loader error is preserved for debugging... | ||
| assert "libtvm.so" in message | ||
| # ...and the message points at the real fix: a matching mlc-ai install. | ||
| assert "mlc-ai" in message | ||
| assert isinstance(exc_info.value.__cause__, OSError) | ||
|
|
||
|
|
||
| def test_load_lib_success(monkeypatch): | ||
| """On success ``load_lib`` returns the loaded handle unchanged.""" | ||
| libinfo = _load_libinfo() | ||
| sentinel = object() | ||
| monkeypatch.setattr(libinfo.ctypes, "CDLL", lambda path: sentinel) | ||
|
|
||
| assert libinfo.load_lib("/some/path/libmlc_llm.so") is sentinel |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.