Skip to content

Commit 1bad84f

Browse files
committed
add more tests
Signed-off-by: Superjomn <[email protected]>
1 parent 9b1987c commit 1bad84f

File tree

3 files changed

+119
-15
lines changed

3 files changed

+119
-15
lines changed

tests/integration/defs/llmapi/test_llm_examples.py

Lines changed: 106 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@
1414
# limitations under the License.
1515

1616
import os
17+
import subprocess
18+
import sys
19+
import threading
1720
from pathlib import Path
21+
from subprocess import PIPE, Popen
1822

1923
import pytest
2024
from defs.common import venv_check_call
@@ -40,16 +44,8 @@ def test_llmapi_server_example(llm_root, llm_venv):
4044

4145

4246
### LLMAPI examples
43-
def _run_llmapi_example(llm_root, engine_dir, llm_venv, script_name: str,
44-
*args):
45-
example_root = Path(llm_root) / "examples" / "llm-api"
46-
engine_dir = Path(engine_dir) / "llmapi"
47-
if not engine_dir.exists():
48-
engine_dir.mkdir(parents=True)
49-
examples_script = example_root / script_name
50-
51-
run_command = [str(examples_script)] + list(args)
52-
47+
def _setup_llmapi_example_softlinks(llm_venv):
48+
"""Create softlinks for LLM models to avoid duplicated downloading for llm api examples"""
5349
# Create llm models softlink to avoid duplicated downloading for llm api example
5450
src_dst_dict = {
5551
# TinyLlama-1.1B-Chat-v1.0
@@ -87,9 +83,96 @@ def _run_llmapi_example(llm_root, engine_dir, llm_venv, script_name: str,
8783
cnn_dailymail_dst,
8884
target_is_directory=True)
8985

86+
87+
def _run_llmapi_example(llm_root, engine_dir, llm_venv, script_name: str,
88+
*args):
89+
example_root = Path(llm_root) / "examples" / "llm-api"
90+
engine_dir = Path(engine_dir) / "llmapi"
91+
if not engine_dir.exists():
92+
engine_dir.mkdir(parents=True)
93+
examples_script = example_root / script_name
94+
95+
run_command = [str(examples_script)] + list(args)
96+
97+
_setup_llmapi_example_softlinks(llm_venv)
98+
9099
venv_check_call(llm_venv, run_command)
91100

92101

102+
def _mpirun_llmapi_example(llm_root,
103+
llm_venv,
104+
script_name: str,
105+
tp_size: int,
106+
spawn_extra_main_process: bool = True,
107+
*args):
108+
"""Run an llmapi example script with mpirun.
109+
110+
Args:
111+
llm_root: Root directory of the LLM project
112+
llm_venv: Virtual environment object
113+
script_name: Name of the example script to run
114+
tp_size: Tensor parallelism size (number of MPI processes)
115+
spawn_extra_main_process: Whether to spawn extra main process (default: True)
116+
*args: Additional arguments to pass to the example script
117+
"""
118+
example_root = Path(llm_root) / "examples" / "llm-api"
119+
examples_script = example_root / script_name
120+
121+
# Set environment variable for spawn_extra_main_process
122+
env_vars = os.environ.copy()
123+
env_vars[
124+
'TLLM_SPAWN_EXTRA_MAIN_PROCESS'] = "1" if spawn_extra_main_process else "0"
125+
126+
run_command = [
127+
"mpirun", "-n",
128+
str(tp_size), "--oversubscribe", "--allow-run-as-root"
129+
]
130+
# Pass environment variables through mpirun
131+
for key, value in [('TLLM_SPAWN_EXTRA_MAIN_PROCESS',
132+
env_vars['TLLM_SPAWN_EXTRA_MAIN_PROCESS'])]:
133+
run_command.extend(["-x", f"{key}={value}"])
134+
run_command.extend(["python", str(examples_script)] + list(args))
135+
136+
_setup_llmapi_example_softlinks(llm_venv)
137+
138+
print(' '.join(run_command))
139+
140+
with Popen(run_command,
141+
env=env_vars,
142+
stdout=PIPE,
143+
stderr=PIPE,
144+
bufsize=1,
145+
start_new_session=True,
146+
universal_newlines=True,
147+
cwd=llm_venv.get_working_directory()) as process:
148+
149+
# Function to read from a stream and write to output
150+
def read_stream(stream, output_stream):
151+
for line in stream:
152+
output_stream.write(line)
153+
output_stream.flush()
154+
155+
# Create threads to read stdout and stderr concurrently
156+
stdout_thread = threading.Thread(target=read_stream,
157+
args=(process.stdout, sys.stdout))
158+
stderr_thread = threading.Thread(target=read_stream,
159+
args=(process.stderr, sys.stderr))
160+
161+
# Start both threads
162+
stdout_thread.start()
163+
stderr_thread.start()
164+
165+
# Wait for the process to complete
166+
return_code = process.wait()
167+
168+
# Wait for both threads to finish reading
169+
stdout_thread.join()
170+
stderr_thread.join()
171+
172+
if return_code != 0:
173+
raise subprocess.CalledProcessError(return_code, run_command)
174+
175+
93176
def test_llmapi_quickstart(llm_root, engine_dir, llm_venv):
94177
_run_llmapi_example(llm_root, engine_dir, llm_venv, "quickstart_example.py")
95178

@@ -133,6 +216,19 @@ def test_llmapi_example_distributed_tp2(llm_root, engine_dir, llm_venv):
133216
"llm_inference_distributed.py")
134217

135218

219+
@pytest.mark.skip_less_device(2)
220+
@pytest.mark.parametrize(
221+
"spawn_extra_main_process", [True, False],
222+
ids=["spawn_extra_main_process", "no_spawn_extra_main_process"])
223+
def test_llmapi_example_launch_distributed_tp2(llm_root, llm_venv,
224+
spawn_extra_main_process: bool):
225+
_mpirun_llmapi_example(llm_root,
226+
llm_venv,
227+
"llm_inference_distributed.py",
228+
tp_size=2,
229+
spawn_extra_main_process=spawn_extra_main_process)
230+
231+
136232
def test_llmapi_example_logits_processor(llm_root, engine_dir, llm_venv):
137233
_run_llmapi_example(llm_root, engine_dir, llm_venv,
138234
"llm_logits_processor.py")

tests/integration/test_lists/test-db/l0_dgx_h200.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ l0_dgx_h200:
163163
- test_e2e.py::test_trtllm_bench_llmapi_launch[trt_backend-llama-v3-llama3-8b]
164164
- examples/test_nemotron_nas.py::test_nemotron_nas_summary_2gpu[DeciLM-7B]
165165
- llmapi/test_llm_examples.py::test_llmapi_example_distributed_tp2
166+
- llmapi/test_llm_examples.py::test_llmapi_example_launch_distributed_tp2[spawn_extra_main_process]
167+
- llmapi/test_llm_examples.py::test_llmapi_example_launch_distributed_tp2[no_spawn_extra_main_process]
166168
- unittest/trt/functional/test_allreduce_norm.py
167169
- examples/test_multimodal.py::test_llm_multimodal_general[Llama-3.2-11B-Vision-pp:1-tp:2-bfloat16-bs:1-cpp_e2e:False-nb:1]
168170
- examples/test_multimodal.py::test_llm_multimodal_general[llava-v1.6-mistral-7b-hf-vision-trtllm-pp:1-tp:2-float16-bs:1-cpp_e2e:False-nb:1]

tests/unittest/llmapi/_run_mpi_comm_task.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33

44
import click
55

6-
from tensorrt_llm.llmapi.mpi_session import RemoteMpiCommSessionClient
6+
from tensorrt_llm.llmapi.mpi_session import (MpiCommSession,
7+
RemoteMpiCommSessionClient)
78
from tensorrt_llm.llmapi.utils import print_colored
89

910

@@ -13,10 +14,15 @@
1314
default="submit")
1415
def main(task_type: Literal["submit", "submit_sync"]):
1516
tasks = [0]
16-
assert os.environ[
17-
'TLLM_SPAWN_PROXY_PROCESS_IPC_ADDR'] is not None, "TLLM_SPAWN_PROXY_PROCESS_IPC_ADDR is not set"
18-
client = RemoteMpiCommSessionClient(
19-
os.environ['TLLM_SPAWN_PROXY_PROCESS_IPC_ADDR'])
17+
18+
if os.environ.get('TLLM_SPAWN_EXTRA_MAIN_PROCESS', '0') == '1':
19+
assert os.environ[
20+
'TLLM_SPAWN_PROXY_PROCESS_IPC_ADDR'] is not None, "TLLM_SPAWN_PROXY_PROCESS_IPC_ADDR is not set"
21+
client = RemoteMpiCommSessionClient(
22+
os.environ['TLLM_SPAWN_PROXY_PROCESS_IPC_ADDR'])
23+
else:
24+
client = MpiCommSession(n_workers=2)
25+
2026
for task in tasks:
2127
if task_type == "submit":
2228
client.submit(print_colored, f"{task}\n", "green")

0 commit comments

Comments
 (0)