-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathdeploy_ray_inframework.py
More file actions
265 lines (249 loc) · 7.98 KB
/
Copy pathdeploy_ray_inframework.py
File metadata and controls
265 lines (249 loc) · 7.98 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved.
#
# 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 argparse
import logging
import multiprocessing
from nemo_deploy.deploy_ray import DeployRay
LOGGER = logging.getLogger("NeMo")
def get_available_cpus():
"""Get the total number of available CPUs in the system."""
return multiprocessing.cpu_count()
def parse_args():
"""Parse command-line arguments for the Ray deployment script."""
parser = argparse.ArgumentParser(description="Deploy a Megatron model using Ray")
parser.add_argument(
"--nemo_checkpoint",
type=str,
default=None,
help="Path to the .nemo checkpoint file",
)
parser.add_argument(
"--num_gpus",
type=int,
default=1,
help="Number of GPUs per node in case of single node. In case of multinode total number of GPUs across all nodes",
)
parser.add_argument(
"--tensor_model_parallel_size",
type=int,
default=1,
help="Size of the tensor model parallelism",
)
parser.add_argument(
"--pipeline_model_parallel_size",
type=int,
default=1,
help="Size of the pipeline model parallelism",
)
parser.add_argument(
"-nlfps",
"--num_layers_in_first_pipeline_stage",
default=None,
type=int,
help="Number of layers in the first pipeline stage",
)
parser.add_argument(
"-nllps",
"--num_layers_in_last_pipeline_stage",
default=None,
type=int,
help="Number of layers in the last pipeline stage",
)
parser.add_argument(
"--expert_model_parallel_size",
type=int,
default=1,
help="Size of the expert model parallelism",
)
parser.add_argument(
"--context_parallel_size",
type=int,
default=1,
help="Size of the context parallelism",
)
parser.add_argument(
"-eps",
"--account_for_embedding_in_pipeline_split",
default=False,
action="store_true",
help="Account for embedding in the pipeline split",
)
parser.add_argument(
"-lps",
"--account_for_loss_in_pipeline_split",
default=False,
action="store_true",
help="Account for loss in the pipeline split",
)
parser.add_argument(
"--model_id",
type=str,
default="nemo-model",
help="Identifier for the model in the API responses",
)
parser.add_argument(
"--host",
type=str,
default="0.0.0.0",
help="Host address to bind the Ray Serve server to",
)
parser.add_argument(
"--port",
type=int,
default=1024,
help="Port number to use for the Ray Serve server",
)
parser.add_argument(
"--num_cpus",
type=int,
default=None,
help="Number of CPUs to allocate for the Ray cluster. If None, will use all available CPUs.",
)
parser.add_argument(
"--num_cpus_per_replica",
type=float,
default=8,
help="Number of CPUs per model replica",
)
parser.add_argument(
"--include_dashboard",
action="store_true",
help="Whether to include the Ray dashboard",
)
parser.add_argument(
"--cuda_visible_devices",
type=str,
default=None,
help="Comma-separated list of CUDA visible devices",
)
parser.add_argument(
"--enable_cuda_graphs",
action="store_true",
help="Whether to enable CUDA graphs for faster inference",
)
parser.add_argument(
"--enable_flash_decode",
action="store_true",
help="Whether to enable Flash Attention decode",
)
parser.add_argument(
"--num_replicas",
type=int,
default=1,
help="Number of replicas for the deployment",
)
parser.add_argument(
"--legacy_ckpt",
action="store_true",
help="Whether to use legacy checkpoint format",
)
parser.add_argument(
"--max_batch_size",
type=int,
default=32,
help="Maximum batch size for inference",
)
parser.add_argument(
"--random_seed",
type=int,
default=None,
help="Random seed for reproducible inference",
)
parser.add_argument(
"--megatron_checkpoint",
type=str,
default=None,
help="Path to the Megatron checkpoint file",
)
parser.add_argument(
"--model_type",
type=str,
default="gpt",
help="Type of model to load",
)
parser.add_argument(
"--micro_batch_size",
type=int,
default=None,
help="Micro batch size for model execution",
)
parser.add_argument(
"--tokenizer_path",
type=str,
default=None,
help="Path to the tokenizer model file (optional, overrides checkpoint tokenizer)",
)
parser.add_argument(
"--runtime_env",
type=dict,
default={},
help="Runtime environment for the deployment",
)
return parser.parse_args()
def main():
"""Main function to deploy a Megatron model using Ray."""
args = parse_args()
# Initialize Ray deployment with updated DeployRay class
runtime_env = args.runtime_env
if args.cuda_visible_devices is not None:
runtime_env["env_vars"] = {
"CUDA_VISIBLE_DEVICES": args.cuda_visible_devices,
}
ray_deployer = DeployRay(
num_cpus=args.num_cpus,
num_gpus=args.num_gpus,
include_dashboard=args.include_dashboard,
host=args.host,
port=args.port,
runtime_env=runtime_env,
)
if args.nemo_checkpoint:
model_format = "nemo"
elif args.megatron_checkpoint:
model_format = "megatron"
else:
raise ValueError("Either --nemo_checkpoint or --megatron_checkpoint must be provided")
model_config_kwargs = {
"account_for_embedding_in_pipeline_split": args.account_for_embedding_in_pipeline_split,
"account_for_loss_in_pipeline_split": args.account_for_loss_in_pipeline_split,
}
if args.num_layers_in_first_pipeline_stage is not None:
model_config_kwargs["num_layers_in_first_pipeline_stage"] = args.num_layers_in_first_pipeline_stage
if args.num_layers_in_last_pipeline_stage is not None:
model_config_kwargs["num_layers_in_last_pipeline_stage"] = args.num_layers_in_last_pipeline_stage
# Deploy the inframework model using the updated API
ray_deployer.deploy_inframework_model(
nemo_checkpoint=args.nemo_checkpoint,
num_gpus=args.num_gpus,
tensor_model_parallel_size=args.tensor_model_parallel_size,
pipeline_model_parallel_size=args.pipeline_model_parallel_size,
expert_model_parallel_size=args.expert_model_parallel_size,
context_parallel_size=args.context_parallel_size,
model_id=args.model_id,
num_cpus_per_replica=args.num_cpus_per_replica,
num_replicas=args.num_replicas,
enable_cuda_graphs=args.enable_cuda_graphs,
enable_flash_decode=args.enable_flash_decode,
legacy_ckpt=args.legacy_ckpt,
max_batch_size=args.max_batch_size,
random_seed=args.random_seed,
megatron_checkpoint_filepath=args.megatron_checkpoint,
model_type=args.model_type,
model_format=model_format,
micro_batch_size=args.micro_batch_size,
tokenizer_path=args.tokenizer_path,
**model_config_kwargs,
)
if __name__ == "__main__":
main()