|
5 | 5 | # The utility function cannot be placed in `vllm.utils` |
6 | 6 | # this needs to be a standalone script |
7 | 7 | import sys |
8 | | -from contextlib import nullcontext |
9 | | - |
10 | | -from vllm_test_utils import BlameResult, blame |
11 | 8 |
|
12 | 9 | # List of modules that should not be imported too early. |
13 | 10 | # Lazy import `torch._inductor.async_compile` to avoid creating |
|
16 | 13 | # `cv2` can easily mess up the environment. |
17 | 14 | module_names = ["torch._inductor.async_compile", "cv2"] |
18 | 15 |
|
| 16 | +# set all modules in `module_names` to be None. |
| 17 | +# if we import any modules during `import vllm`, there would be a |
| 18 | +# hard error and nice stacktrace on the first import. |
| 19 | +for module_name in module_names: |
| 20 | + sys.modules[module_name] = None # type: ignore[assignment] |
19 | 21 |
|
20 | | -def any_module_imported(): |
21 | | - return any(module_name in sys.modules for module_name in module_names) |
22 | | - |
23 | | - |
24 | | -# In CI, we only check finally if the module is imported. |
25 | | -# If it is indeed imported, we can rerun the test with `use_blame=True`, |
26 | | -# which will trace every function call to find the first import location, |
27 | | -# and help find the root cause. |
28 | | -# We don't run it in CI by default because it is slow. |
29 | | -use_blame = False |
30 | | -context = blame(any_module_imported) if use_blame else nullcontext() |
31 | | -with context as result: |
32 | | - import vllm # noqa |
33 | | - |
34 | | -if use_blame: |
35 | | - assert isinstance(result, BlameResult) |
36 | | - print(f"the first import location is:\n{result.trace_stack}") |
37 | | - |
38 | | -assert not any_module_imported(), ( |
39 | | - f"Some the modules in {module_names} are imported. To see the first" |
40 | | - f" import location, run the test with `use_blame=True`." |
41 | | -) |
| 22 | +import vllm # noqa |
0 commit comments