-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathrun.py
More file actions
354 lines (297 loc) · 12.6 KB
/
run.py
File metadata and controls
354 lines (297 loc) · 12.6 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
import sys
import argparse
import json
import os
from pathlib import Path
from framework import (
get_hardware_args_group,
add_common_test_args,
InfiniDeviceEnum,
InfiniDeviceNames,
)
from framework.test_manager import TestCollector, TestManager
def generate_help_epilog(ops_dir=None):
"""
Generate dynamic help epilog containing available operators and hardware platforms.
Maintains the original output format for backward compatibility.
"""
# === Adapter: Use TestCollector to get operator list ===
# Temporarily instantiate a Collector just to fetch the list
collector = TestCollector(ops_dir)
operators = collector.get_available_operators()
# Build epilog text (fully replicating original logic)
epilog_parts = []
# Examples section
epilog_parts.append("Examples:")
epilog_parts.append(" # Run common operator tests on CPU")
epilog_parts.append(" python run.py --cpu")
epilog_parts.append("")
epilog_parts.append(" # Run specific operators")
epilog_parts.append(" python run.py --ops add matmul --nvidia")
epilog_parts.append("")
epilog_parts.append(" # Run with debug mode on multiple devices")
epilog_parts.append(" python run.py --cpu --nvidia --debug")
epilog_parts.append("")
epilog_parts.append(
" # Run with verbose mode to stop on first error with full traceback"
)
epilog_parts.append(" python run.py --cpu --nvidia --verbose")
epilog_parts.append("")
epilog_parts.append(" # Run with benchmarking (both host and device timing)")
epilog_parts.append(" python run.py --cpu --bench")
epilog_parts.append("")
epilog_parts.append(" # Run with host timing only")
epilog_parts.append(" python run.py --nvidia --bench host")
epilog_parts.append("")
epilog_parts.append(" # Run with device timing only")
epilog_parts.append(" python run.py --nvidia --bench device")
epilog_parts.append("")
epilog_parts.append(" # List available tests without running")
epilog_parts.append(" python run.py --list")
epilog_parts.append("")
# Available operators section
if operators:
epilog_parts.append("Available Operators:")
# Group operators for better display
operators_per_line = 4
for i in range(0, len(operators), operators_per_line):
line_ops = operators[i : i + operators_per_line]
epilog_parts.append(f" {', '.join(line_ops)}")
epilog_parts.append("")
else:
epilog_parts.append("Available Operators: (none detected)")
epilog_parts.append("")
# Additional notes
epilog_parts.append("Note:")
epilog_parts.append(
" - Use '--' to pass additional arguments to individual test scripts"
)
epilog_parts.append(
" - Operators are automatically discovered from the ops directory"
)
epilog_parts.append(
" - --bench mode now shows cumulative timing across all operators"
)
epilog_parts.append(
" - --bench host/device/both controls host/device timing measurement"
)
epilog_parts.append(
" - --verbose mode stops execution on first error and shows full traceback"
)
return "\n".join(epilog_parts)
def fill_defaults_for_local_mode(args):
"""
Helper function specifically for Local Scan mode to fill default arguments.
Since parser defaults are set to None (to handle override logic in load mode),
we need to manually fill None with default values in local mode.
"""
# 1. Copy args to avoid modifying the original object and affecting other logic
# argparse.Namespace can be converted to dict and back, or copied directly
local_args = argparse.Namespace(**vars(args))
# 2. Fill default values for numeric arguments
if local_args.num_prerun is None:
local_args.num_prerun = 10
if local_args.num_iterations is None:
local_args.num_iterations = 1000
return local_args
def load_and_override_cases(load_paths, args):
"""
Load JSON, apply CLI overrides, and handle all argument logic.
"""
cases = []
files_to_read = []
# 1. Scan
for p_str in load_paths:
p = Path(p_str)
if p.is_dir():
files_to_read.extend(p.glob("*.json"))
elif p.is_file():
files_to_read.append(p)
# 2. Read and Validate
loaded_count = 0
skipped_count = 0
for f_path in files_to_read:
try:
with open(f_path, "r", encoding="utf-8") as f:
data = json.load(f)
# Unify as a list to handle both single dict and list of dicts
current_batch = data if isinstance(data, list) else [data]
valid_batch = []
for item in current_batch:
# We only require the 'operator' field to identify the test case.
if isinstance(item, dict) and "operator" in item:
valid_batch.append(item)
else:
skipped_count += 1
if valid_batch:
cases.extend(valid_batch)
loaded_count += 1
except Exception as e:
# Log warning only; do not crash the program on bad files to ensure flow continuity.
print(f"❌ Error loading {f_path.name}: {e}")
if skipped_count > 0:
print(f"ℹ️ Ignored {skipped_count} items/files (invalid format).")
# ==================================================
# Device Logic using InfiniDeviceEnum
# ==================================================
# 1. Identify active devices from CLI arguments
cli_active_devices = []
# Iterate through the Enum to check corresponding CLI args
# Logic: Enum name (e.g., CAMBRICON) -> lower() -> arg name (cambricon)
# Value: InfiniDeviceNames mapping (e.g., "Cambricon")
for device_enum, device_name in InfiniDeviceNames.items():
# device_name is like "CPU", "NVIDIA", "Cambricon"
# arg_name becomes "cpu", "nvidia", "cambricon"
arg_name = device_name.lower()
if getattr(args, arg_name, False):
cli_active_devices.append(device_name)
print(f"\n[Config Processing]")
for case in cases:
if "args" not in case or case["args"] is None:
case["args"] = {}
case_args = case["args"]
# 2. Apply Device Overrides (CLI > JSON)
if cli_active_devices:
case["device"] = ",".join(cli_active_devices)
final_dev_str = case.get("device", "").upper() # Uppercase for easier matching
# 3. Set Boolean flags in case_args based on final device string
for device_enum, device_name in InfiniDeviceNames.items():
arg_name = device_name.lower()
# Check if the standard name (e.g., "Cambricon" or "NVIDIA") is in the device string
# We convert both to upper to ensure case-insensitive matching
is_active = device_name.upper() in final_dev_str
case_args[arg_name] = is_active
case_args["save"] = getattr(args, "save", None)
# Standard arguments (CLI > JSON > Default)
case_args["bench"] = (
args.bench if args.bench is not None else case_args.get("bench")
)
# Boolean Flags
case_args["verbose"] = args.verbose or case_args.get("verbose", False)
case_args["debug"] = args.debug or case_args.get("debug", False)
case_args["eq_nan"] = args.eq_nan or case_args.get("eq_nan", False)
case_args["num_prerun"] = (
args.num_prerun
if args.num_prerun is not None
else (case_args.get("num_prerun") or 10)
)
case_args["num_iterations"] = (
args.num_iterations
if args.num_iterations is not None
else (case_args.get("num_iterations") or 1000)
)
print(f"📂 Processed {len(cases)} cases ready for execution.\n")
return cases
def main():
"""Main entry point for the InfiniCore Operator Test Runner."""
parser = argparse.ArgumentParser(
description="Run InfiniCore operator tests across multiple hardware platforms",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=generate_help_epilog(),
)
parser.add_argument(
"--ops-dir", type=str, help="Path to the ops directory (default: auto-detect)"
)
parser.add_argument(
"--ops", nargs="+", help="Run specific operators only (e.g., --ops add matmul)"
)
parser.add_argument(
"--list",
action="store_true",
help="List all available test files without running them",
)
parser.add_argument(
"--load",
nargs="+",
help="Load test cases from JSON",
)
# Default value is None to determine if user provided input
parser.add_argument("--num_prerun", type=lambda x: max(0, int(x)), default=None)
parser.add_argument("--num_iterations", type=lambda x: max(0, int(x)), default=None)
# Add common test arguments (including --save, --bench, etc.)
add_common_test_args(parser)
get_hardware_args_group(parser)
args, unknown_args = parser.parse_known_args()
# Show what extra arguments will be passed
if unknown_args:
print(f"Passing extra arguments to test scripts: {unknown_args}")
# 1. Discovery
collector = TestCollector(args.ops_dir)
if args.list:
print("Available operators:", collector.get_available_operators())
return
# ==========================================================================
# Branch 1: Load Mode (JSON Data Driven)
# ==========================================================================
if args.load:
# 1. Load and override arguments
json_cases = load_and_override_cases(args.load, args)
if not json_cases:
sys.exit(1)
# 2. Determine global Bench status (for Summary display)
bench = json_cases[0]["args"].get("bench")
verbose = json_cases[0]["args"].get("verbose")
if verbose:
print(
f"Verbose mode: ENABLED (will stop on first error with full traceback)"
)
if bench:
print(f"Benchmark mode: {args.bench.upper()} timing")
# 3. Initialize and Execute
test_manager = TestManager(
ops_dir=args.ops_dir, verbose=verbose, bench_mode=bench
)
success, _ = test_manager.test(json_cases_list=json_cases)
# ==========================================================================
# Branch 2: Local Scan Mode
# ==========================================================================
else:
if args.verbose:
print(
f"Verbose mode: ENABLED (will stop on first error with full traceback)"
)
if args.bench:
print(f"Benchmark mode: {args.bench.upper()} timing")
# 2. Filtering
target_ops = None
if args.ops:
available_ops = set(collector.get_available_operators())
requested_ops = set(args.ops)
valid_ops = list(requested_ops & available_ops)
invalid_ops = list(requested_ops - available_ops)
if invalid_ops:
print(f"⚠️ Warning: The following requested operators were not found:")
print(f" {', '.join(invalid_ops)}")
print(f" (Use --list to see available operators)")
if not valid_ops:
# Case A: User input provided, but ALL were invalid.
print(f"⚠️ No valid operators remained from your list.")
print(f"🔄 Fallback: Proceeding to run common tests...")
else:
# Case B: At least some valid operators found.
print(f"🎯 Targeted operators: {', '.join(valid_ops)}")
target_ops = valid_ops
# 3. Execution Preparation
# Fill defaults for local mode (since parser default is None)
global_exec_args = fill_defaults_for_local_mode(args)
# 4. Initialize API & Execute
test_manager = TestManager(
ops_dir=args.ops_dir, verbose=args.verbose, bench_mode=args.bench
)
# default to common operators if none specified
if target_ops is None:
target_ops = [
"add",
"causal_softmax",
"matmul",
"random_sample",
"rms_norm",
"rope",
"swiglu",
]
success, _ = test_manager.test(
target_ops=target_ops, global_exec_args=global_exec_args
)
sys.exit(0 if success else 1)
if __name__ == "__main__":
main()