-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathtest_lepton_optional_dependency.py
More file actions
93 lines (73 loc) · 2.92 KB
/
test_lepton_optional_dependency.py
File metadata and controls
93 lines (73 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import subprocess
import sys
import textwrap
from pathlib import Path
REPO_ROOT = Path(__file__).parents[3]
def _run_with_blocked_leptonai(code: str) -> subprocess.CompletedProcess[str]:
blocker = """
import importlib.abc
import sys
class BlockLeptonai(importlib.abc.MetaPathFinder):
def find_spec(self, fullname, path=None, target=None):
if fullname == "leptonai" or fullname.startswith("leptonai."):
raise ModuleNotFoundError("No module named 'leptonai'")
return None
sys.meta_path.insert(0, BlockLeptonai())
"""
script = blocker + "\n" + textwrap.dedent(code)
return subprocess.run(
[sys.executable, "-c", script],
cwd=REPO_ROOT,
text=True,
capture_output=True,
check=False,
)
def test_nemo_run_import_without_leptonai() -> None:
result = _run_with_blocked_leptonai(
"""
import sys
import nemo_run as run
from nemo_run import LeptonExecutor as PublicLeptonExecutor
from nemo_run.core.execution import LeptonExecutor as ExecutionLeptonExecutor
assert run.LocalExecutor.__name__ == "LocalExecutor"
assert run.LeptonExecutor.__name__ == "LeptonExecutor"
assert PublicLeptonExecutor is run.LeptonExecutor
assert ExecutionLeptonExecutor is run.LeptonExecutor
assert "leptonai" not in sys.modules
try:
run.LeptonExecutor(container_image="image", nemo_run_dir="/nemo")
except ImportError as e:
assert "nemo_run[lepton]" in str(e)
else:
raise AssertionError("LeptonExecutor should require the lepton extra")
"""
)
assert result.returncode == 0, result.stderr
def test_scheduler_and_ray_modules_import_without_leptonai() -> None:
result = _run_with_blocked_leptonai(
"""
import sys
from nemo_run.core.execution.lepton import LeptonExecutor
from nemo_run.run.torchx_backend.schedulers.api import REVERSE_EXECUTOR_MAPPING
import nemo_run.run.ray.cluster
import nemo_run.run.ray.job
import nemo_run.run.ray.lepton
assert REVERSE_EXECUTOR_MAPPING["lepton"] is LeptonExecutor
assert "leptonai" not in sys.modules
"""
)
assert result.returncode == 0, result.stderr