Repository navigation
Expand file tree
/
Copy pathbatch_test_torch.py
More file actions
executable file
·353 lines (291 loc) · 10.5 KB
/
Copy pathbatch_test_torch.py
File metadata and controls
executable file
·353 lines (291 loc) · 10.5 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
#!/usr/bin/env python3
"""
ATTest batch testing script for PyTorch modules
Read all modules from the PynguinML XML definition file and run tests automatically
"""
import sys
import subprocess
import xml.etree.ElementTree as ET
from pathlib import Path
from datetime import datetime
import json
import time
from typing import List, Dict, Optional
class BatchTester:
def __init__(
self,
xml_file: str,
workspace_base: str,
python_path: str,
epochs: int = 5,
mode: str = "full-auto"
):
self.xml_file = Path(xml_file)
self.workspace_base = Path(workspace_base)
self.python_path = python_path
self.epochs = epochs
self.mode = mode
# State file
self.state_file = self.workspace_base / "batch_test_state.json"
self.log_file = self.workspace_base / "batch_test.log"
# Ensure the workspace exists
self.workspace_base.mkdir(parents=True, exist_ok=True)
# Load state
self.state = self.load_state()
def load_state(self) -> Dict:
"""Load testing state"""
if self.state_file.exists():
with open(self.state_file, 'r') as f:
return json.load(f)
return {
"completed": [],
"failed": [],
"current_index": 0,
"start_time": None,
"last_update": None
}
def save_state(self):
"""Save testing state"""
self.state["last_update"] = datetime.now().isoformat()
with open(self.state_file, 'w') as f:
json.dump(self.state, f, indent=2)
def log(self, message: str, level: str = "INFO"):
"""Record logs"""
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
log_msg = f"[{timestamp}] [{level}] {message}"
print(log_msg)
with open(self.log_file, 'a') as f:
f.write(log_msg + "\n")
def extract_modules(self) -> List[str]:
"""Extract the module list from the XML file"""
try:
tree = ET.parse(self.xml_file)
root = tree.getroot()
modules = []
for module in root.findall('.//module'):
if module.text:
modules.append(module.text.strip())
return modules
except Exception as e:
self.log(f"Failed to parse the XML file: {e}", "ERROR")
return []
def module_to_workspace(self, module: str) -> Path:
"""Convert a module name to a workspace path."""
# Replace dots with slashes, for example `torch.nn.modules.batchnorm` -> `torch/nn.modules.batchnorm`
workspace_name = module.replace(".", "/")
return self.workspace_base / workspace_name
def run_test(self, module: str) -> bool:
"""Run tests for a single module"""
workspace = self.module_to_workspace(module)
self.log(f"Start testing module: {module}")
self.log(f"Workspace: {workspace}")
# Build command
cmd = [
self.python_path,
"-m", "attest_cli.cli",
"run",
"--func", module,
"--workspace", str(workspace),
"--mode", self.mode,
"--epoch", str(self.epochs)
]
self.log(f"Run command: {' '.join(cmd)}")
try:
# Run tests
start_time = time.time()
result = subprocess.run(
cmd,
capture_output=True,
text=True,
timeout=3600 # 1-hour timeout
)
elapsed = time.time() - start_time
if result.returncode == 0:
self.log(f"✓ Module {module} tests passed (elapsed: {elapsed:.1f}s)", "SUCCESS")
return True
else:
self.log(f"✗ Module {module} tests failed (exit code: {result.returncode})", "ERROR")
self.log(f"STDOUT: {result.stdout[-500:]}", "DEBUG")
self.log(f"STDERR: {result.stderr[-500:]}", "DEBUG")
return False
except subprocess.TimeoutExpired:
self.log(f"✗ Module {module} test timed out", "ERROR")
return False
except Exception as e:
self.log(f"✗ Module {module} execution exception: {e}", "ERROR")
return False
def run_batch(self, start_index: Optional[int] = None):
"""Run tests in batch."""
modules = self.extract_modules()
if not modules:
self.log("No modules were found", "ERROR")
return
total = len(modules)
self.log(f"Found {total} modules to test")
# Determine the starting position
if start_index is not None:
current = start_index
else:
current = self.state.get("current_index", 0)
# Record the start time on the first run
if not self.state.get("start_time"):
self.state["start_time"] = datetime.now().isoformat()
self.log(f"Start from module {current + 1}/{total}")
# Test modules one by one
for i in range(current, total):
module = modules[i]
# Check whether the module is already completed
if module in self.state["completed"]:
self.log(f"Skip completed module: {module}", "INFO")
continue
self.log(f"\n{'='*60}")
self.log(f"Progress: {i+1}/{total} ({(i+1)/total*100:.1f}%)")
self.log(f"Module: {module}")
self.log(f"{'='*60}")
# Run tests
success = self.run_test(module)
# Update state
if success:
self.state["completed"].append(module)
else:
self.state["failed"].append(module)
self.state["current_index"] = i + 1
self.save_state()
# Short pause
time.sleep(2)
# Generate report
self.generate_report(modules)
def generate_report(self, modules: List[str]):
"""Generate the test report"""
total = len(modules)
completed = len(self.state["completed"])
failed = len(self.state["failed"])
remaining = total - completed - failed
report_file = self.workspace_base / "batch_test_report.md"
report = f"""# ATTest Batch Test Report - PyTorch Modules
## Test Overview
- **Start time**: {self.state.get('start_time', 'N/A')}
- **End time**: {datetime.now().isoformat()}
- **Total modules**: {total}
- **Completed**: {completed} ({completed/total*100:.1f}%)
- **Failed**: {failed} ({failed/total*100:.1f}%)
- **Not tested**: {remaining} ({remaining/total*100:.1f}%)
## Test Configuration
- **XML file**: {self.xml_file}
- **Workspace**: {self.workspace_base}
- **Mode**: {self.mode}
- **Iteration rounds**: {self.epochs}
## Successful Modules ({completed})
"""
for module in self.state["completed"]:
workspace = self.module_to_workspace(module)
report += f"- ✓ `{module}` → `{workspace}`\n"
if self.state["failed"]:
report += f"\n## Failed Modules ({failed})\n\n"
for module in self.state["failed"]:
workspace = self.module_to_workspace(module)
report += f"- ✗ `{module}` → `{workspace}`\n"
if remaining > 0:
report += f"\n## Not Tested Modules ({remaining})\n\n"
untested = [m for m in modules if m not in self.state["completed"] and m not in self.state["failed"]]
for module in untested:
report += f"- ⊙ `{module}`\n"
report += "\n## Detailed Log\n\n"
report += f"See the full log: `{self.log_file}`\n"
with open(report_file, 'w') as f:
f.write(report)
self.log(f"\n{'='*60}")
self.log("Testing completed!")
self.log(f"Report generated: {report_file}")
self.log(f"{'='*60}\n")
# Print summary
print(report)
def main():
import argparse
parser = argparse.ArgumentParser(
description="ATTest batch testing tool",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
# Test all PyTorch modules
python batch_test_torch.py
# Start from the 10th module
python batch_test_torch.py --start 9
# Specify a different epoch count
python batch_test_torch.py --epoch 3
# Use interactive mode
python batch_test_torch.py --mode interactive
"""
)
parser.add_argument(
"--xml",
default="artifact/rundefinitions/pynguinml-torch.xml",
help="Path to the XML definition file (default: `artifact/rundefinitions/pynguinml-torch.xml`)"
)
parser.add_argument(
"--workspace",
default="./exam/torch",
help="Base workspace path (default: ./exam/torch)"
)
parser.add_argument(
"--python",
default="/opt/anaconda3/envs/attest-experiment/bin/python",
help="Python interpreter path"
)
parser.add_argument(
"--epoch",
type=int,
default=5,
help="Iteration rounds (default: 5)"
)
parser.add_argument(
"--mode",
choices=["full-auto", "interactive"],
default="full-auto",
help="Run mode (default: full-auto)"
)
parser.add_argument(
"--start",
type=int,
help="Start from the specified index (0-based)"
)
parser.add_argument(
"--reset",
action="store_true",
help="Reset state and start from scratch"
)
args = parser.parse_args()
# Create the tester
tester = BatchTester(
xml_file=args.xml,
workspace_base=args.workspace,
python_path=args.python,
epochs=args.epoch,
mode=args.mode
)
# Reset the state
if args.reset:
tester.log("Reset test state", "INFO")
tester.state = {
"completed": [],
"failed": [],
"current_index": 0,
"start_time": None,
"last_update": None
}
tester.save_state()
# Run batch testing
try:
tester.run_batch(start_index=args.start)
except KeyboardInterrupt:
tester.log("\n\nTesting interrupted by the user", "WARNING")
tester.save_state()
tester.log("State has been saved; you can continue with the current configuration", "INFO")
sys.exit(1)
except Exception as e:
tester.log(f"Batch testing exception: {e}", "ERROR")
import traceback
tester.log(traceback.format_exc(), "ERROR")
sys.exit(1)
if __name__ == "__main__":
main()