Repository navigation
[None][feat] Add a link mode to the precompiled editable-install path #18752
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
efd44e5
[None][feat] Add a link mode to the precompiled editable-install path
brnguyen2 cf21ed3
Address trivial review comments
brnguyen2 5112832
Address trivial review comments
brnguyen2 573f209
[None][fix] Guard link mode against self-link and stale fmha_sm100 link
brnguyen2 9e0e1f9
[None][test] Mark test_precompiled_link_mode as cpu_only so CI collec…
brnguyen2 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
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,208 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| """``TRTLLM_PRECOMPILED_LINK`` in ``setup.py``'s ``extract_from_precompiled``. | ||
|
|
||
| The local-directory source normally copies the compiled artifacts into the | ||
| checkout. ``TRTLLM_PRECOMPILED_LINK=1`` symlinks them instead so several | ||
| checkouts can share one build tree. | ||
|
|
||
| ``setup.py`` cannot be imported (module scope runs ``setup()``), so the two | ||
| functions under test are pulled out of its AST and executed on their own. They | ||
| are self-contained: every import they need is inside the function body, apart | ||
| from ``os``. | ||
| """ | ||
|
|
||
| import ast | ||
| import os | ||
| from pathlib import Path | ||
|
|
||
| import pytest | ||
|
|
||
| _SETUP_PY = Path(__file__).resolve().parents[3] / "setup.py" | ||
| _WANTED = ("should_skip_precompiled_package_data", "warn_on_build_skew", "extract_from_precompiled") | ||
|
|
||
|
|
||
| def _load(name): | ||
| assert _SETUP_PY.is_file(), _SETUP_PY | ||
| tree = ast.parse(_SETUP_PY.read_text(encoding="utf-8")) | ||
| wanted = [ | ||
| node for node in tree.body if isinstance(node, ast.FunctionDef) and node.name in _WANTED | ||
| ] | ||
| assert {node.name for node in wanted} == set(_WANTED) | ||
| namespace = {"os": os} | ||
| exec(compile(ast.Module(body=wanted, type_ignores=[]), str(_SETUP_PY), "exec"), namespace) | ||
| return namespace[name] | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def extract_from_precompiled(): | ||
| return _load("extract_from_precompiled") | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def warn_on_build_skew(): | ||
| return _load("warn_on_build_skew") | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def build_tree(tmp_path, monkeypatch): | ||
| """A source checkout holding compiled artifacts, and an empty destination.""" | ||
| source = tmp_path / "built-checkout" | ||
| (source / "tensorrt_llm" / "libs").mkdir(parents=True) | ||
| (source / "tensorrt_llm" / "libs" / "libtensorrt_llm.so").write_text("so") | ||
| (source / "3rdparty" / "fmha_sm100").mkdir(parents=True) | ||
| (source / "3rdparty" / "fmha_sm100" / "__init__.py").write_text("") | ||
|
|
||
| destination = tmp_path / "checkout" | ||
| destination.mkdir() | ||
| monkeypatch.chdir(destination) | ||
| return source | ||
|
|
||
|
|
||
| PACKAGE_DATA = ["libs/*.so"] | ||
|
|
||
|
|
||
| def _run(extract, source, workspace, link): | ||
| previous = os.environ.get("TRTLLM_PRECOMPILED_LINK") | ||
| os.environ["TRTLLM_PRECOMPILED_LINK"] = "1" if link else "0" | ||
| try: | ||
| extract(str(source), PACKAGE_DATA, str(workspace)) | ||
| finally: | ||
| if previous is None: | ||
| os.environ.pop("TRTLLM_PRECOMPILED_LINK", None) | ||
| else: | ||
| os.environ["TRTLLM_PRECOMPILED_LINK"] = previous | ||
|
|
||
|
|
||
| def test_link_mode_symlinks_the_artifacts(extract_from_precompiled, build_tree, tmp_path): | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| _run(extract_from_precompiled, build_tree, tmp_path, link=True) | ||
|
|
||
| lib = Path("tensorrt_llm/libs/libtensorrt_llm.so") | ||
| assert lib.is_symlink() | ||
| assert Path(os.readlink(lib)) == (build_tree / "tensorrt_llm" / "libs" / "libtensorrt_llm.so") | ||
| fmha = Path("3rdparty/fmha_sm100") | ||
| assert fmha.is_symlink() | ||
| assert Path(os.readlink(fmha)) == build_tree / "3rdparty" / "fmha_sm100" | ||
|
|
||
|
|
||
| def test_link_mode_keeps_an_existing_fmha_symlink(extract_from_precompiled, build_tree, tmp_path): | ||
| """A checkout already sharing a build tree keeps the link it has.""" | ||
| other = tmp_path / "other-fmha" | ||
| other.mkdir() | ||
| Path("3rdparty").mkdir() | ||
| os.symlink(other, "3rdparty/fmha_sm100") | ||
|
|
||
| _run(extract_from_precompiled, build_tree, tmp_path, link=True) | ||
|
|
||
| assert Path(os.readlink("3rdparty/fmha_sm100")) == other | ||
|
|
||
|
|
||
| def test_link_mode_replaces_a_stale_artifact(extract_from_precompiled, build_tree, tmp_path): | ||
| """A real file left by an earlier copy-mode install is not kept.""" | ||
| Path("tensorrt_llm/libs").mkdir(parents=True) | ||
| Path("tensorrt_llm/libs/libtensorrt_llm.so").write_text("stale") | ||
|
|
||
| _run(extract_from_precompiled, build_tree, tmp_path, link=True) | ||
|
|
||
| assert Path("tensorrt_llm/libs/libtensorrt_llm.so").is_symlink() | ||
|
|
||
|
|
||
| def test_copy_mode_is_unchanged(extract_from_precompiled, build_tree, tmp_path): | ||
| _run(extract_from_precompiled, build_tree, tmp_path, link=False) | ||
|
|
||
| lib = Path("tensorrt_llm/libs/libtensorrt_llm.so") | ||
| assert lib.is_file() and not lib.is_symlink() | ||
| assert lib.read_text() == "so" | ||
| fmha = Path("3rdparty/fmha_sm100") | ||
| assert fmha.is_dir() and not fmha.is_symlink() | ||
| assert (fmha / "__init__.py").is_file() | ||
|
|
||
|
|
||
| def test_link_mode_rejects_a_wheel(extract_from_precompiled, build_tree, tmp_path): | ||
| """There is no build tree to point at, so fail instead of copying.""" | ||
| from setuptools.errors import SetupError | ||
|
|
||
| wheel = tmp_path / "tensorrt_llm-0.0.0.whl" | ||
| wheel.write_text("") | ||
|
|
||
| with pytest.raises(SetupError, match="TRTLLM_PRECOMPILED_LINK"): | ||
| _run(extract_from_precompiled, wheel, tmp_path, link=True) | ||
|
|
||
|
|
||
| # -------------------------------------------------------------------------- | ||
| # Build-skew warning | ||
| # -------------------------------------------------------------------------- | ||
| def _fake_git(monkeypatch, heads, diff_output="", diff_fails=False): | ||
| """Stub ``git rev-parse`` / ``git diff`` with canned output.""" | ||
| import subprocess | ||
|
|
||
| class Done: | ||
| def __init__(self, stdout): | ||
| self.stdout = stdout | ||
|
|
||
| def run(argv, **kwargs): | ||
| if "rev-parse" in argv: | ||
| return Done(heads[argv[argv.index("-C") + 1]] + "\n") | ||
| assert argv[1] == "diff", argv | ||
| if diff_fails: | ||
| raise subprocess.CalledProcessError(1, argv) | ||
| return Done(diff_output) | ||
|
|
||
| monkeypatch.setattr(subprocess, "run", run) | ||
|
|
||
|
|
||
| def test_no_warning_when_the_checkouts_match(warn_on_build_skew, monkeypatch, capfd): | ||
| _fake_git(monkeypatch, {"/src": "a" * 40, ".": "a" * 40}) | ||
|
|
||
| warn_on_build_skew("/src") | ||
|
|
||
| assert capfd.readouterr().out == "" | ||
|
|
||
|
|
||
| def test_no_warning_when_nothing_native_differs(warn_on_build_skew, monkeypatch, capfd): | ||
| """The two checkouts are meant to differ; only the native inputs matter.""" | ||
| _fake_git(monkeypatch, {"/src": "a" * 40, ".": "b" * 40}, diff_output="") | ||
|
|
||
| warn_on_build_skew("/src") | ||
|
|
||
| assert capfd.readouterr().out == "" | ||
|
|
||
|
|
||
| def test_warns_when_native_inputs_differ(warn_on_build_skew, monkeypatch, capfd): | ||
| _fake_git( | ||
| monkeypatch, | ||
| {"/src": "a" * 40, ".": "b" * 40}, | ||
| diff_output="cpp/one.cu\ncpp/two.cu\nsetup.py\n3rdparty/x\n", | ||
| ) | ||
|
|
||
| warn_on_build_skew("/src") | ||
|
|
||
| out = capfd.readouterr().out | ||
| assert "WARNING" in out | ||
| assert "aaaaaaaaaaaa" in out and "bbbbbbbbbbbb" in out | ||
| assert "4 files feeding" in out | ||
| assert "cpp/one.cu" in out and "..." in out | ||
| assert "ABI skew" in out | ||
|
|
||
|
|
||
| def test_warns_generically_when_the_diff_cannot_be_taken(warn_on_build_skew, monkeypatch, capfd): | ||
| """Two unrelated clones do not share an object store.""" | ||
| _fake_git(monkeypatch, {"/src": "a" * 40, ".": "b" * 40}, diff_fails=True) | ||
|
|
||
| warn_on_build_skew("/src") | ||
|
|
||
| out = capfd.readouterr().out | ||
| assert "WARNING" in out and "could not be inspected" in out | ||
|
|
||
|
|
||
| def test_skew_check_is_skipped_outside_a_repo(warn_on_build_skew, monkeypatch, capfd): | ||
| import subprocess | ||
|
|
||
| def run(argv, **kwargs): | ||
| raise subprocess.CalledProcessError(128, argv) | ||
|
|
||
| monkeypatch.setattr(subprocess, "run", run) | ||
|
|
||
| warn_on_build_skew("/src") | ||
|
|
||
| assert "Cannot check for build skew" in capfd.readouterr().out | ||
Oops, something went wrong.
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.