|
| 1 | +# SPDX-FileCopyrightText: NVIDIA CORPORATION & AFFILIATES |
| 2 | +# Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +from pathlib import Path |
| 18 | +from typing import Any, Dict, List, Tuple, Union, cast |
| 19 | + |
| 20 | +from cloudai import TestRun |
| 21 | +from cloudai.systems.slurm.strategy import SlurmCommandGenStrategy |
| 22 | + |
| 23 | +from .triton_inference import TritonInferenceTestDefinition |
| 24 | + |
| 25 | + |
| 26 | +class TritonInferenceSlurmCommandGenStrategy(SlurmCommandGenStrategy): |
| 27 | + """Command generation strategy for TritonInference server and client.""" |
| 28 | + |
| 29 | + def _container_mounts(self, tr: TestRun) -> list[str]: |
| 30 | + td = cast(TritonInferenceTestDefinition, tr.test.test_definition) |
| 31 | + mounts = [ |
| 32 | + f"{td.nim_model_path}:{td.nim_model_path}:ro", |
| 33 | + f"{td.nim_cache_path}:{td.nim_cache_path}:rw", |
| 34 | + ] |
| 35 | + |
| 36 | + wrapper_host = (tr.output_path / "start_server_wrapper.sh").resolve() |
| 37 | + wrapper_container = "/opt/nim/start_server_wrapper.sh" |
| 38 | + self._generate_start_wrapper_script(wrapper_host, td.extra_env_vars) |
| 39 | + mounts.append(f"{wrapper_host}:{wrapper_container}:ro") |
| 40 | + |
| 41 | + return mounts |
| 42 | + |
| 43 | + def _append_sbatch_directives( |
| 44 | + self, |
| 45 | + batch_script_content: List[str], |
| 46 | + args: Dict[str, Any], |
| 47 | + tr: TestRun, |
| 48 | + ) -> None: |
| 49 | + super()._append_sbatch_directives(batch_script_content, args, tr) |
| 50 | + batch_script_content.append("export HEAD_NODE=$SLURM_JOB_MASTER_NODE") |
| 51 | + batch_script_content.append("export NIM_LEADER_IP_ADDRESS=$SLURM_JOB_MASTER_NODE") |
| 52 | + batch_script_content.append(f"export NIM_NUM_COMPUTE_NODES={args['num_nodes'] - 1}") |
| 53 | + batch_script_content.append("export NIM_MODEL_TOKENIZER='deepseek-ai/DeepSeek-R1'") |
| 54 | + |
| 55 | + def _generate_start_wrapper_script(self, script_path: Path, env_vars: Dict[str, Any]) -> None: |
| 56 | + lines = ["#!/bin/bash", ""] |
| 57 | + lines.append("export NIM_LEADER_IP_ADDRESS=${SLURM_JOB_MASTER_NODE}") |
| 58 | + lines.append("export NIM_NODE_RANK=${SLURM_NODEID}") |
| 59 | + lines.append("") |
| 60 | + for key, val in env_vars.items(): |
| 61 | + if key in {"NIM_LEADER_IP_ADDRESS", "NIM_NODE_RANK"}: |
| 62 | + continue |
| 63 | + if isinstance(val, str): |
| 64 | + lines.append(f"export {key}='{val}'") |
| 65 | + lines.append("") |
| 66 | + lines.append('if [ "$NIM_NODE_RANK" -eq 0 ]; then') |
| 67 | + lines.append(" export NIM_LEADER_ROLE=1") |
| 68 | + lines.append("else") |
| 69 | + lines.append(" export NIM_LEADER_ROLE=0") |
| 70 | + lines.append("fi") |
| 71 | + lines.append("") |
| 72 | + lines.append('echo "Starting NIM server on node rank ${NIM_NODE_RANK} with leader role ${NIM_LEADER_ROLE}"') |
| 73 | + lines.append("exec /opt/nim/start_server.sh") |
| 74 | + script_path.parent.mkdir(parents=True, exist_ok=True) |
| 75 | + with script_path.open("w", encoding="utf-8") as f: |
| 76 | + f.write("\n".join(lines)) |
| 77 | + script_path.chmod(0o755) |
| 78 | + |
| 79 | + def _gen_srun_command( |
| 80 | + self, |
| 81 | + slurm_args: Dict[str, Any], |
| 82 | + env_vars: Dict[str, Union[str, List[str]]], |
| 83 | + cmd_args: Dict[str, Union[str, List[str]]], |
| 84 | + tr: TestRun, |
| 85 | + ) -> str: |
| 86 | + num_server_nodes, num_client_nodes = self._get_server_client_split(tr) |
| 87 | + server_line = self._build_server_srun(slurm_args, tr, num_server_nodes) |
| 88 | + client_line = self._build_client_srun(slurm_args, tr, num_client_nodes) |
| 89 | + sleep_sec = cast(TritonInferenceTestDefinition, tr.test.test_definition).cmd_args.sleep_seconds |
| 90 | + return f"{server_line} &\n\nsleep {sleep_sec}\n\n{client_line}" |
| 91 | + |
| 92 | + def _get_server_client_split(self, tr: TestRun) -> Tuple[int, int]: |
| 93 | + num_nodes, _ = self.system.get_nodes_by_spec(tr.num_nodes, tr.nodes) |
| 94 | + if num_nodes < 3: |
| 95 | + raise ValueError("DeepSeekR1 requires at least 3 nodes: 2 server and 1 client.") |
| 96 | + return num_nodes - 1, 1 |
| 97 | + |
| 98 | + def _build_server_srun(self, slurm_args: Dict[str, Any], tr: TestRun, num_server_nodes: int) -> str: |
| 99 | + test_definition = cast(TritonInferenceTestDefinition, tr.test.test_definition) |
| 100 | + server_slurm_args = { |
| 101 | + **slurm_args, |
| 102 | + "image_path": test_definition.server_docker_image.installed_path, |
| 103 | + } |
| 104 | + srun_prefix = self.gen_srun_prefix(server_slurm_args, tr) |
| 105 | + srun_prefix.append(f"--nodes={num_server_nodes}") |
| 106 | + srun_prefix.append(f"--ntasks={num_server_nodes}") |
| 107 | + srun_prefix.append("--ntasks-per-node=1") |
| 108 | + nsys_command = self.gen_nsys_command(tr) |
| 109 | + server_launch_command = ["/opt/nim/start_server_wrapper.sh"] |
| 110 | + return " ".join(srun_prefix + nsys_command + server_launch_command) |
| 111 | + |
| 112 | + def _build_client_srun(self, slurm_args: Dict[str, Any], tr: TestRun, num_client_nodes: int) -> str: |
| 113 | + test_definition = cast(TritonInferenceTestDefinition, tr.test.test_definition) |
| 114 | + client_slurm_args = { |
| 115 | + **slurm_args, |
| 116 | + "image_path": test_definition.client_docker_image.installed_path, |
| 117 | + } |
| 118 | + srun_prefix = self.gen_srun_prefix(client_slurm_args, tr) |
| 119 | + srun_prefix.append(f"--nodes={num_client_nodes}") |
| 120 | + srun_prefix.append(f"--ntasks={num_client_nodes}") |
| 121 | + |
| 122 | + args = test_definition.cmd_args |
| 123 | + client_command = [ |
| 124 | + "genai-perf", |
| 125 | + "profile", |
| 126 | + "-m", |
| 127 | + args.served_model_name, |
| 128 | + f"--endpoint-type {args.endpoint_type}", |
| 129 | + f"--service-kind {args.service_kind}", |
| 130 | + ] |
| 131 | + if args.streaming: |
| 132 | + client_command.append("--streaming") |
| 133 | + client_command += [ |
| 134 | + "-u", |
| 135 | + f"$SLURM_JOB_MASTER_NODE:{args.port}", |
| 136 | + "--num-prompts", |
| 137 | + str(args.num_prompts), |
| 138 | + "--synthetic-input-tokens-mean", |
| 139 | + str(args.input_sequence_length), |
| 140 | + "--synthetic-input-tokens-stddev", |
| 141 | + "0", |
| 142 | + "--concurrency", |
| 143 | + str(args.concurrency), |
| 144 | + "--output-tokens-mean", |
| 145 | + str(args.output_sequence_length), |
| 146 | + "--extra-inputs", |
| 147 | + f"max_tokens:{args.output_sequence_length}", |
| 148 | + "--extra-inputs", |
| 149 | + f"min_tokens:{args.output_sequence_length}", |
| 150 | + "--extra-inputs", |
| 151 | + "ignore_eos:true", |
| 152 | + "--artifact-dir", |
| 153 | + "/cloudai_run_results", |
| 154 | + "--tokenizer", |
| 155 | + args.tokenizer, |
| 156 | + "--", |
| 157 | + "-v", |
| 158 | + f"--max-threads {args.concurrency}", |
| 159 | + f"--request-count {args.num_prompts}", |
| 160 | + ] |
| 161 | + return " ".join(srun_prefix + client_command) |
0 commit comments