-
Notifications
You must be signed in to change notification settings - Fork 217
Expand file tree
/
Copy pathvalidation.py
More file actions
606 lines (485 loc) · 21 KB
/
Copy pathvalidation.py
File metadata and controls
606 lines (485 loc) · 21 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
from pydantic import BaseModel, Field, ValidationError, ConfigDict, model_validator
from typing import List, Optional, Union, Literal
from enum import Enum
import pprint
import yaml
"""
The below class defines the field names expected to be present in the JSON entries
for both single-node and multi-node configurations.
"""
class Fields(Enum):
# Field name constants
# Top-level config fields
IMAGE = 'image'
MODEL = 'model'
MODEL_PREFIX = 'model-prefix'
PRECISION = 'precision'
FRAMEWORK = 'framework'
RUNNER = 'runner'
SCENARIOS = 'scenarios'
MULTINODE = 'multinode'
# Scenario type keys
FIXED_SEQ_LEN = 'fixed-seq-len'
AGENTIC_CODING = 'agentic-coding'
# Seq-len-config fields
ISL = 'isl'
OSL = 'osl'
SEARCH_SPACE = 'search-space'
# Search-space/benchmark fields
TP = 'tp'
CONC_START = 'conc-start'
CONC_END = 'conc-end'
CONC_LIST = 'conc-list'
EP = 'ep'
DP_ATTN = 'dp-attn'
# Multinode-specific fields (when MULTINODE = true)
SPEC_DECODING = 'spec-decoding'
PREFILL = 'prefill'
DECODE = 'decode'
NUM_WORKER = 'num-worker'
BATCH_SIZE = 'batch-size'
MAX_NUM_TOKENS = 'max-num-tokens'
ADDITIONAL_SETTINGS = 'additional-settings'
# Agentic coding fields
OFFLOADING = 'offloading'
DURATION = 'duration'
# Matrix entry fields
CONC = 'conc'
MAX_MODEL_LEN = 'max-model-len'
EXP_NAME = 'exp-name'
DISAGG = 'disagg'
SCENARIO_TYPE = 'scenario-type'
# Eval
RUN_EVAL = 'run-eval'
EVAL_ONLY = 'eval-only'
EVAL_CONC = 'eval-conc'
"""
Below is the validation logic for the OUTPUT of utils/matrix_logic/generate_sweep_configs.py, i.e.,
the input to the actual workflow files. The validation enforces a strict set of rules on the structure
of the generated matrix entries to ensure correctness before proceeding with benchmarking. This ensures
that no validation has to happen in the workflow itself, i.e., at runtime, it is assumed that all inputs
are valid. Threfore, there should not be any default values set in these Pydantic models. Any missing value
should raise a validation error.
"""
class SingleNodeMatrixEntry(BaseModel):
"""Pydantic model for validating single node matrix entry structure.
This validates the input that should be expected to .github/workflows/benchmark-tmpl.yml"""
model_config = ConfigDict(extra='forbid', populate_by_name=True)
image: str
model: str
model_prefix: str = Field(alias=Fields.MODEL_PREFIX.value)
precision: str
framework: str
spec_decoding: Literal["mtp", "draft_model", "none"] = Field(
alias=Fields.SPEC_DECODING.value
)
runner: str
isl: int
osl: int
tp: int
ep: int
dp_attn: bool = Field(alias=Fields.DP_ATTN.value)
conc: Union[int, List[int]]
max_model_len: int = Field(alias=Fields.MAX_MODEL_LEN.value)
exp_name: str = Field(alias=Fields.EXP_NAME.value)
disagg: bool
run_eval: bool = Field(alias=Fields.RUN_EVAL.value)
eval_only: bool = Field(alias=Fields.EVAL_ONLY.value, default=False)
class WorkerConfig(BaseModel):
"""Pydantic model for validating worker configuration in multinode entries."""
model_config = ConfigDict(extra='forbid', populate_by_name=True)
num_worker: int = Field(alias=Fields.NUM_WORKER.value)
tp: int
ep: int
dp_attn: bool = Field(alias=Fields.DP_ATTN.value)
additional_settings: Optional[List[str]] = Field(
default=[], alias=Fields.ADDITIONAL_SETTINGS.value)
class MultiNodeMatrixEntry(BaseModel):
"""Pydantic model for validating multinode matrix entry structure.
This validates the input that should be expected to .github/workflows/benchmark-multinode-tmpl.yml"""
model_config = ConfigDict(extra='forbid', populate_by_name=True)
image: str
model: str
model_prefix: str = Field(alias=Fields.MODEL_PREFIX.value)
precision: str
framework: str
spec_decoding: Literal["mtp", "draft_model", "none"] = Field(
alias=Fields.SPEC_DECODING.value
)
runner: str
isl: int
osl: int
prefill: WorkerConfig
decode: WorkerConfig
conc: List[int]
max_model_len: int = Field(alias=Fields.MAX_MODEL_LEN.value)
exp_name: str = Field(alias=Fields.EXP_NAME.value)
disagg: bool
run_eval: bool = Field(alias=Fields.RUN_EVAL.value)
eval_only: bool = Field(alias=Fields.EVAL_ONLY.value, default=False)
eval_conc: Optional[int] = Field(default=None, alias=Fields.EVAL_CONC.value)
class SingleNodeAgenticMatrixEntry(BaseModel):
"""Pydantic model for validating single-node agentic coding matrix entries."""
model_config = ConfigDict(extra='forbid', populate_by_name=True)
image: str
model: str
model_prefix: str = Field(alias=Fields.MODEL_PREFIX.value)
precision: str
framework: str
runner: str
tp: int
ep: int
dp_attn: bool = Field(alias=Fields.DP_ATTN.value)
conc: int
offloading: Literal["none", "cpu", "ssd", "lmcache", "lmcache-mp", "hicache"] = Field(
alias=Fields.OFFLOADING.value
)
duration: int = Field(default=1800, alias=Fields.DURATION.value)
exp_name: str = Field(alias=Fields.EXP_NAME.value)
scenario_type: str = Field(alias=Fields.SCENARIO_TYPE.value)
class MultiNodeAgenticMatrixEntry(BaseModel):
"""Pydantic model for validating multinode agentic coding matrix entries."""
model_config = ConfigDict(extra='forbid', populate_by_name=True)
image: str
model: str
model_prefix: str = Field(alias=Fields.MODEL_PREFIX.value)
precision: str
framework: str
spec_decoding: Literal["mtp", "draft_model", "none"] = Field(
alias=Fields.SPEC_DECODING.value
)
runner: str
prefill: WorkerConfig
decode: WorkerConfig
conc: int
duration: int = Field(default=1800, alias=Fields.DURATION.value)
exp_name: str = Field(alias=Fields.EXP_NAME.value)
disagg: bool
scenario_type: str = Field(alias=Fields.SCENARIO_TYPE.value)
AgenticMatrixEntry = Union[SingleNodeAgenticMatrixEntry, MultiNodeAgenticMatrixEntry]
def validate_agentic_matrix_entry(entry: dict) -> dict:
"""Validate that an agentic matrix entry matches the expected structure."""
try:
if Fields.PREFILL.value in entry:
MultiNodeAgenticMatrixEntry(**entry)
else:
SingleNodeAgenticMatrixEntry(**entry)
except ValidationError as e:
raise ValueError(
f"The following parsed agentic matrix entry failed validation:\n{pprint.pformat(entry)}\n{e}")
return entry
def validate_matrix_entry(entry: dict, is_multinode: bool) -> dict:
"""Validate that matrix_values entries match the expected structure.
Raises ValueError if any entry fails validation.
Returns the original list if all entries are valid.
"""
try:
if is_multinode:
MultiNodeMatrixEntry(**entry)
else:
SingleNodeMatrixEntry(**entry)
except ValidationError as e:
raise ValueError(
f"The following parsed matrix entry failed validation:\n{pprint.pformat(entry)}\n{e}")
return entry
"""
Below is the validation logic for the INPUT to utils/matrix_logic/generate_sweep_configs.py, i.e.,
the master configuration files found in .github/configs. The validation enforces a strict set of
rules on the structure of the master configuration files to ensure correctness before proceeding
with matrix generation.
"""
def _validate_conc_fields(self):
"""Ensure either (conc_start AND conc_end) OR conc_list is provided, but not both."""
has_range = self.conc_start is not None and self.conc_end is not None
has_list = self.conc_list is not None and len(self.conc_list) > 0
if has_range and has_list:
raise ValueError(
f"Cannot specify both '{Fields.CONC_LIST.value}' list and "
f"'{Fields.CONC_START.value}'/'{Fields.CONC_END.value}'. "
"Use either a list or a range, not both."
)
if not has_range and not has_list:
raise ValueError(
f"Must specify either '{Fields.CONC_LIST.value}' list or both "
f"'{Fields.CONC_START.value}' and '{Fields.CONC_END.value}'."
)
if has_range:
if self.conc_start is None or self.conc_end is None:
raise ValueError(
f"Both '{Fields.CONC_START.value}' and '{Fields.CONC_END.value}' "
"must be provided together."
)
if self.conc_start > self.conc_end:
raise ValueError(
f"'{Fields.CONC_START.value}' ({self.conc_start}) must be <= "
f"'{Fields.CONC_END.value}' ({self.conc_end})."
)
if has_list:
if not all(x > 0 for x in self.conc_list):
raise ValueError(
f"Input '{Fields.CONC_LIST.value}' entries must be greater than 0."
)
return self
class SingleNodeSearchSpaceEntry(BaseModel):
"""Single node search space configuration."""
model_config = ConfigDict(extra='forbid', populate_by_name=True)
tp: int
ep: Optional[int] = None
spec_decoding: Literal["mtp", "draft_model", "none"] = Field(
default="none", alias=Fields.SPEC_DECODING.value)
dp_attn: Optional[bool] = Field(
default=None, alias=Fields.DP_ATTN.value)
conc_start: Optional[int] = Field(
default=None, alias=Fields.CONC_START.value)
conc_end: Optional[int] = Field(
default=None, alias=Fields.CONC_END.value)
conc_list: Optional[List[int]] = Field(
default=None, alias=Fields.CONC_LIST.value)
@model_validator(mode='after')
def validate_conc_fields(self):
return _validate_conc_fields(self)
class MultiNodeSearchSpaceEntry(BaseModel):
"""Multinode search space configuration."""
model_config = ConfigDict(extra='forbid', populate_by_name=True)
spec_decoding: Literal["mtp", "draft_model", "none"] = Field(
default="none", alias=Fields.SPEC_DECODING.value)
prefill: WorkerConfig
decode: WorkerConfig
conc_start: Optional[int] = Field(
default=None, alias=Fields.CONC_START.value)
conc_end: Optional[int] = Field(
default=None, alias=Fields.CONC_END.value)
conc_list: Optional[List[int]] = Field(
default=None, alias=Fields.CONC_LIST.value)
@model_validator(mode='after')
def validate_conc_fields(self):
return _validate_conc_fields(self)
class SingleNodeSeqLenConfig(BaseModel):
"""Single node sequence length configuration."""
model_config = ConfigDict(extra='forbid', populate_by_name=True)
isl: int
osl: int
search_space: List[SingleNodeSearchSpaceEntry] = Field(
alias=Fields.SEARCH_SPACE.value)
class MultiNodeSeqLenConfig(BaseModel):
"""Multinode sequence length configuration."""
model_config = ConfigDict(extra='forbid', populate_by_name=True)
isl: int
osl: int
search_space: List[MultiNodeSearchSpaceEntry] = Field(
alias=Fields.SEARCH_SPACE.value)
class AgenticCodingSearchSpaceEntry(BaseModel):
"""Agentic coding search space configuration."""
model_config = ConfigDict(extra='forbid', populate_by_name=True)
tp: Optional[int] = None
ep: Optional[int] = None
dp_attn: Optional[bool] = Field(default=None, alias=Fields.DP_ATTN.value)
spec_decoding: Literal["mtp", "draft_model", "none"] = Field(
default="none", alias=Fields.SPEC_DECODING.value)
prefill: Optional[WorkerConfig] = None
decode: Optional[WorkerConfig] = None
offloading: Literal["none", "cpu", "ssd", "lmcache", "lmcache-mp", "hicache"] = Field(
default="none", alias=Fields.OFFLOADING.value
)
conc_start: Optional[int] = Field(default=None, alias=Fields.CONC_START.value)
conc_end: Optional[int] = Field(default=None, alias=Fields.CONC_END.value)
conc_list: Optional[List[int]] = Field(default=None, alias=Fields.CONC_LIST.value)
@model_validator(mode='after')
def validate_conc_fields(self):
return _validate_conc_fields(self)
@model_validator(mode='after')
def validate_topology_fields(self):
has_single_node = self.tp is not None
has_any_multinode_field = self.prefill is not None or self.decode is not None
has_complete_multinode = self.prefill is not None and self.decode is not None
if has_single_node:
valid = not has_any_multinode_field
else:
valid = has_complete_multinode
if not valid:
raise ValueError("Agentic search-space entries must specify either tp or both prefill and decode")
return self
class AgenticCodingConfig(BaseModel):
"""Agentic coding scenario configuration for trace replay benchmarks."""
model_config = ConfigDict(extra='forbid', populate_by_name=True)
search_space: List[AgenticCodingSearchSpaceEntry] = Field(alias=Fields.SEARCH_SPACE.value)
duration: int = Field(default=1800, alias=Fields.DURATION.value)
class SingleNodeScenarios(BaseModel):
"""Scenarios wrapper for single-node configs."""
model_config = ConfigDict(extra='forbid', populate_by_name=True)
fixed_seq_len: Optional[List[SingleNodeSeqLenConfig]] = Field(
default=None, alias=Fields.FIXED_SEQ_LEN.value)
agentic_coding: Optional[List[AgenticCodingConfig]] = Field(
default=None, alias=Fields.AGENTIC_CODING.value)
@model_validator(mode='after')
def at_least_one_scenario(self):
if not self.fixed_seq_len and not self.agentic_coding:
raise ValueError("At least one scenario type must be specified")
return self
class MultiNodeScenarios(BaseModel):
"""Scenarios wrapper for multinode configs."""
model_config = ConfigDict(extra='forbid', populate_by_name=True)
fixed_seq_len: Optional[List[MultiNodeSeqLenConfig]] = Field(
default=None, alias=Fields.FIXED_SEQ_LEN.value)
agentic_coding: Optional[List[AgenticCodingConfig]] = Field(
default=None, alias=Fields.AGENTIC_CODING.value)
@model_validator(mode='after')
def at_least_one_scenario(self):
if not self.fixed_seq_len and not self.agentic_coding:
raise ValueError("At least one scenario type must be specified")
return self
class SingleNodeMasterConfigEntry(BaseModel):
"""Top-level single node master configuration entry."""
model_config = ConfigDict(extra='forbid', populate_by_name=True)
image: str
model: str
model_prefix: str = Field(alias=Fields.MODEL_PREFIX.value)
precision: str
framework: str
runner: str
multinode: Literal[False]
disagg: bool = Field(default=False)
scenarios: SingleNodeScenarios
class MultiNodeMasterConfigEntry(BaseModel):
"""Top-level multinode master configuration entry."""
model_config = ConfigDict(extra='forbid', populate_by_name=True)
image: str
model: str
model_prefix: str = Field(alias=Fields.MODEL_PREFIX.value)
precision: str
framework: str
runner: str
multinode: Literal[True]
disagg: bool = Field(default=False)
scenarios: MultiNodeScenarios
def validate_master_config(master_configs: dict) -> List[dict]:
"""Validate input master configuration structure."""
for key, entry in master_configs.items():
is_multinode = entry.get('multinode', False)
try:
if is_multinode:
MultiNodeMasterConfigEntry(**entry)
else:
SingleNodeMasterConfigEntry(**entry)
except ValidationError as e:
raise ValueError(
f"Master config entry '{key}' failed validation:\n{e}")
return master_configs
# Runner Config Validation
def validate_runner_config(runner_configs: dict) -> List[dict]:
"""Validate input master configuration structure."""
for key, value in runner_configs.items():
if not isinstance(value, list):
raise ValueError(
f"Runner config entry '{key}' must be a list, got {type(value).__name__}")
if not all(isinstance(item, str) for item in value):
raise ValueError(
f"Runner config entry '{key}' must contain only strings")
if not value:
raise ValueError(
f"Runner config entry '{key}' cannot be an empty list")
return runner_configs
"""
Below is the validation logic for the changelog entries found in perf-changelog.yaml.
This ensures that the changelog entries conform to the expected structure before
proceeding with processing.
"""
class ChangelogEntry(BaseModel):
"""Pydantic model for validating changelog entry structure."""
model_config = ConfigDict(extra="forbid", populate_by_name=True)
config_keys: list[str] = Field(alias="config-keys", min_length=1)
description: list[str] = Field(min_length=1)
pr_link: str = Field(alias="pr-link")
evals_only: bool = Field(alias="evals-only", default=False)
benchmarks_only: bool = Field(
alias="benchmarks-only", default=False,
description="Skip the eval pass; generate benchmarks only (e.g. power-only re-runs)."
)
scenario_type: Optional[List[str]] = Field(
alias="scenario-type", default=None,
description="Restrict to specific scenario types (e.g., ['fixed-seq-len', 'agentic-coding'])"
)
@model_validator(mode='after')
def check_evals_benchmarks_exclusive(self) -> "ChangelogEntry":
if self.evals_only and self.benchmarks_only:
raise ValueError(
"'evals-only' and 'benchmarks-only' are mutually exclusive; set at most one."
)
return self
class ChangelogMetadata(BaseModel):
"""Pydantic model for validating changelog metadata structure."""
model_config = ConfigDict(extra="forbid")
base_ref: str
head_ref: str
entries: list[ChangelogEntry]
class ChangelogMatrixEntry(BaseModel):
"""Pydantic model for validating final changelog matrix entry structure.
This imposes a strict contract on the output of process_changelog.py, dictated by
the expected input to the run-sweep.yml workflow file.
"""
model_config = ConfigDict(extra="forbid", populate_by_name=True)
single_node: dict[str, list[Union[SingleNodeMatrixEntry, SingleNodeAgenticMatrixEntry]]
] = Field(default_factory=dict)
multi_node: dict[str, list[Union[MultiNodeMatrixEntry, MultiNodeAgenticMatrixEntry]]
] = Field(default_factory=dict)
evals: list[SingleNodeMatrixEntry] = Field(default_factory=list)
multinode_evals: list[MultiNodeMatrixEntry] = Field(default_factory=list)
changelog_metadata: ChangelogMetadata
# =============================================================================
# File Loading Functions
# =============================================================================
def load_config_files(config_files: List[str], validate: bool = True) -> dict:
"""Load and merge configuration files.
Args:
config_files: List of paths to YAML configuration files.
validate: If True, run validate_master_config on loaded data. Defaults to True.
Returns:
Merged configuration dictionary.
Raises:
ValueError: If file doesn't exist, isn't a dict, or has duplicate keys.
"""
all_config_data = {}
for config_file in config_files:
try:
with open(config_file, 'r') as f:
config_data = yaml.safe_load(f)
assert isinstance(
config_data, dict), f"Config file '{config_file}' must contain a dictionary"
# Don't allow '*' wildcard in master config keys as we need to reserve these
# for expansion in process_changelog.py
for key in config_data.keys():
if "*" in key:
raise ValueError(
f" Wildcard '*' is not allowed in master config keys: '{key}'")
# Check for duplicate keys
duplicate_keys = set(all_config_data.keys()) & set(
config_data.keys())
if duplicate_keys:
raise ValueError(
f"Duplicate configuration keys found in '{config_file}': {', '.join(sorted(duplicate_keys))}"
)
all_config_data.update(config_data)
except FileNotFoundError:
raise ValueError(f"Input file '{config_file}' does not exist.")
if validate:
validate_master_config(all_config_data)
return all_config_data
def load_runner_file(runner_file: str, validate: bool = True) -> dict:
"""Load runner configuration file.
Args:
runner_file: Path to the runner YAML configuration file.
validate: If True, run validate_runner_config on loaded data. Defaults to True.
Returns:
Runner configuration dictionary.
Raises:
ValueError: If file doesn't exist or fails validation.
"""
try:
with open(runner_file, 'r') as f:
runner_config = yaml.safe_load(f)
except FileNotFoundError:
raise ValueError(
f"Runner config file '{runner_file}' does not exist.")
if validate:
validate_runner_config(runner_config)
return runner_config