-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathtest_kernel_attribution.py
More file actions
385 lines (341 loc) · 14.1 KB
/
test_kernel_attribution.py
File metadata and controls
385 lines (341 loc) · 14.1 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
# Copyright (c) Meta Platforms, Inc. and affiliates.
"""Tests for kernel attribution via compile mapping in trace_processor."""
import json
import os
import tempfile
import unittest
from tritonparse.parse.torch_trace_parser import CompileInfo
from tritonparse.parse.trace_processor import (
_determine_output_fname,
_resolve_compile_info,
parse_single_file,
)
class TestResolveCompileInfo(unittest.TestCase):
"""Tests for _resolve_compile_info."""
def _make_mapping(self):
return {
"/tmp/torchinductor_user/ab/kernel1.py": CompileInfo(
frame_id=0, frame_compile_id=0
),
"/tmp/torchinductor_user/cd/kernel2.py": CompileInfo(
frame_id=1, frame_compile_id=0, attempt=1
),
}
def test_resolve_via_python_source(self):
"""Test resolution via python_source.file_path."""
event = {
"payload": {
"python_source": {"file_path": "/tmp/torchinductor_user/ab/kernel1.py"}
},
"stack": [],
}
result = _resolve_compile_info(event, self._make_mapping())
self.assertIsNotNone(result)
self.assertEqual(result.frame_id, 0)
self.assertEqual(result.frame_compile_id, 0)
def test_resolve_via_stack_trace(self):
"""Test resolution via stack trace when python_source is missing."""
event = {
"payload": {},
"stack": [
{"filename": "/user/code.py", "line": 10, "name": "main"},
{
"filename": "/tmp/torchinductor_user/cd/kernel2.py",
"line": 1,
"name": "kernel",
},
{"filename": "triton/jit.py", "line": 50, "name": "run"},
],
}
result = _resolve_compile_info(event, self._make_mapping())
self.assertIsNotNone(result)
self.assertEqual(result.frame_id, 1)
self.assertEqual(result.frame_compile_id, 0)
self.assertEqual(result.attempt, 1)
def test_no_match(self):
"""Test that None is returned when no match is found."""
event = {
"payload": {"python_source": {"file_path": "/tmp/unknown/path.py"}},
"stack": [{"filename": "/user/code.py", "line": 10, "name": "main"}],
}
result = _resolve_compile_info(event, self._make_mapping())
self.assertIsNone(result)
def test_empty_event(self):
"""Test with minimal event data."""
event = {}
result = _resolve_compile_info(event, self._make_mapping())
self.assertIsNone(result)
def test_python_source_takes_priority(self):
"""Test that python_source.file_path is preferred over stack trace."""
event = {
"payload": {
"python_source": {"file_path": "/tmp/torchinductor_user/ab/kernel1.py"}
},
"stack": [
{
"filename": "/tmp/torchinductor_user/cd/kernel2.py",
"line": 1,
"name": "kernel",
}
],
}
result = _resolve_compile_info(event, self._make_mapping())
# Should use python_source path (kernel1 -> frame_id=0), not stack (kernel2 -> frame_id=1)
self.assertEqual(result.frame_id, 0)
class TestDetermineOutputFname(unittest.TestCase):
"""Tests for _determine_output_fname."""
def test_with_pt_info(self):
"""Test normal case where pt_info has frame_id/compile_id."""
fname = _determine_output_fname(
pt_info={"frame_id": 0, "frame_compile_id": 1, "attempt_id": 0},
file_name_without_extension="trace",
split_inductor_compilations=True,
)
self.assertEqual(fname, "f0_fc1_a0_cai-.ndjson")
def test_without_pt_info_no_mapping(self):
"""Test fallback to mapped file when pt_info is missing and no mapping."""
fname = _determine_output_fname(
pt_info={},
file_name_without_extension="trace",
split_inductor_compilations=True,
)
self.assertEqual(fname, "trace_mapped.ndjson")
def test_without_pt_info_with_mapping(self):
"""Test resolution via mapping when pt_info is missing."""
mapping = {
"/tmp/torchinductor_user/ab/kernel.py": CompileInfo(
frame_id=3, frame_compile_id=2, attempt=1, compiled_autograd_id=5
)
}
event = {
"payload": {
"python_source": {"file_path": "/tmp/torchinductor_user/ab/kernel.py"}
},
"stack": [],
}
fname = _determine_output_fname(
pt_info={},
file_name_without_extension="trace",
split_inductor_compilations=True,
event=event,
kernel_compile_mapping=mapping,
)
self.assertEqual(fname, "f3_fc2_a1_cai5.ndjson")
def test_split_disabled(self):
"""Test that splitting disabled always returns mapped filename."""
fname = _determine_output_fname(
pt_info={"frame_id": 0, "frame_compile_id": 0},
file_name_without_extension="trace",
split_inductor_compilations=False,
)
self.assertEqual(fname, "trace_mapped.ndjson")
def test_compiled_autograd_id_none(self):
"""Test that compiled_autograd_id defaults to '-' when not set."""
fname = _determine_output_fname(
pt_info={"frame_id": 0, "frame_compile_id": 0},
file_name_without_extension="trace",
split_inductor_compilations=True,
)
self.assertEqual(fname, "f0_fc0_a0_cai-.ndjson")
class TestParseSingleFileWithMapping(unittest.TestCase):
"""Integration tests for parse_single_file with kernel_compile_mapping."""
def test_mapping_redirects_compilation_to_frame_file(self):
"""Test that a compilation without pt_info is redirected when mapping is provided."""
kernel_path = "/tmp/torchinductor_user/ab/cabcdef1234.py"
mapping = {
kernel_path: CompileInfo(frame_id=2, frame_compile_id=1),
}
trace_lines = [
json.dumps(
{
"event_type": "compilation",
"pid": 1000,
"stack": [],
"payload": {
"metadata": {"hash": "kernel_hash_1", "name": "kernel_1"},
"file_content": {},
"file_path": {},
"python_source": {"file_path": kernel_path},
# No pt_info — this is the multi-process scenario
},
}
),
json.dumps(
{
"event_type": "launch",
"name": "kernel_1",
"pid": 1000,
"stack": [],
"compilation_metadata": {"hash": "kernel_hash_1"},
}
),
]
with tempfile.TemporaryDirectory() as temp_dir:
input_file = os.path.join(temp_dir, "test_trace.ndjson")
with open(input_file, "w") as f:
for line in trace_lines:
f.write(line + "\n")
output_dir = os.path.join(temp_dir, "output")
os.makedirs(output_dir)
parse_single_file(input_file, output_dir, kernel_compile_mapping=mapping)
output_files = os.listdir(output_dir)
# Should produce a frame-specific file, not _mapped
frame_files = [f for f in output_files if f.startswith("f")]
mapped_files = [f for f in output_files if "mapped" in f]
self.assertEqual(len(frame_files), 1)
self.assertEqual(len(mapped_files), 0)
self.assertEqual(frame_files[0], "f2_fc1_a0_cai-.ndjson")
def test_no_mapping_falls_back_to_mapped(self):
"""Test that without mapping, compilations without pt_info go to _mapped."""
trace_lines = [
json.dumps(
{
"event_type": "compilation",
"pid": 1000,
"stack": [],
"payload": {
"metadata": {"hash": "kernel_hash_1", "name": "kernel_1"},
"file_content": {},
"file_path": {},
# No pt_info, no python_source
},
}
),
json.dumps(
{
"event_type": "launch",
"name": "kernel_1",
"pid": 1000,
"stack": [],
"compilation_metadata": {"hash": "kernel_hash_1"},
}
),
]
with tempfile.TemporaryDirectory() as temp_dir:
input_file = os.path.join(temp_dir, "test_trace.ndjson")
with open(input_file, "w") as f:
for line in trace_lines:
f.write(line + "\n")
output_dir = os.path.join(temp_dir, "output")
os.makedirs(output_dir)
parse_single_file(input_file, output_dir)
output_files = os.listdir(output_dir)
mapped_files = [f for f in output_files if "mapped" in f]
self.assertGreater(len(mapped_files), 0)
def test_mixed_with_and_without_pt_info(self):
"""Test a mix of events: some with pt_info, some resolved via mapping."""
kernel_path_a = "/tmp/torchinductor_user/ab/kernel_a.py"
mapping = {
kernel_path_a: CompileInfo(frame_id=0, frame_compile_id=0),
}
trace_lines = [
# Compilation WITH pt_info (should be split normally)
json.dumps(
{
"event_type": "compilation",
"pid": 1000,
"stack": [],
"payload": {
"metadata": {"hash": "hash_with_pt", "name": "kernel_with_pt"},
"file_content": {},
"file_path": {},
"pt_info": {
"frame_id": 1,
"frame_compile_id": 0,
},
},
}
),
json.dumps(
{
"event_type": "launch",
"name": "kernel_with_pt",
"pid": 1000,
"stack": [],
"compilation_metadata": {"hash": "hash_with_pt"},
}
),
# Compilation WITHOUT pt_info (should be resolved via mapping)
json.dumps(
{
"event_type": "compilation",
"pid": 1000,
"stack": [],
"payload": {
"metadata": {
"hash": "hash_without_pt",
"name": "kernel_without_pt",
},
"file_content": {},
"file_path": {},
"python_source": {"file_path": kernel_path_a},
},
}
),
json.dumps(
{
"event_type": "launch",
"name": "kernel_without_pt",
"pid": 1000,
"stack": [],
"compilation_metadata": {"hash": "hash_without_pt"},
}
),
]
with tempfile.TemporaryDirectory() as temp_dir:
input_file = os.path.join(temp_dir, "test_trace.ndjson")
with open(input_file, "w") as f:
for line in trace_lines:
f.write(line + "\n")
output_dir = os.path.join(temp_dir, "output")
os.makedirs(output_dir)
parse_single_file(input_file, output_dir, kernel_compile_mapping=mapping)
output_files = sorted(os.listdir(output_dir))
# Should have two frame files: f0_fc0 and f1_fc0
frame_files = sorted([f for f in output_files if f.startswith("f")])
self.assertEqual(len(frame_files), 2)
self.assertIn("f0_fc0_a0_cai-.ndjson", frame_files)
self.assertIn("f1_fc0_a0_cai-.ndjson", frame_files)
def test_fake_compilation_with_mapping(self):
"""Test that fake compilations can also be attributed via stack trace mapping."""
kernel_path = "/tmp/torchinductor_user/ab/kernel_fake.py"
mapping = {
kernel_path: CompileInfo(frame_id=5, frame_compile_id=0),
}
trace_lines = [
# Only launch event (will trigger fake compilation)
json.dumps(
{
"event_type": "launch",
"name": "fake_kernel",
"pid": 1000,
"stack": [
{"filename": "/user/code.py", "line": 10, "name": "main"},
{
"filename": kernel_path,
"line": 1,
"name": "kernel_fn",
},
],
"compilation_metadata": {
"hash": "fake_hash",
"name": "fake_kernel",
"num_warps": 4,
},
}
),
]
with tempfile.TemporaryDirectory() as temp_dir:
input_file = os.path.join(temp_dir, "test_trace.ndjson")
with open(input_file, "w") as f:
for line in trace_lines:
f.write(line + "\n")
output_dir = os.path.join(temp_dir, "output")
os.makedirs(output_dir)
parse_single_file(input_file, output_dir, kernel_compile_mapping=mapping)
output_files = os.listdir(output_dir)
frame_files = [f for f in output_files if f.startswith("f")]
self.assertEqual(len(frame_files), 1)
self.assertEqual(frame_files[0], "f5_fc0_a0_cai-.ndjson")
if __name__ == "__main__":
unittest.main()