Skip to content

Commit abc2a99

Browse files
Olivia-liufacebook-github-bot
authored andcommitted
Debug event populates event name (#5142)
Summary: Pull Request resolved: #5142 Intermediate debugging in delegate doesn't work without also doing intermediate latency profiling in delegates. This diff is to fix this issue. It's currently blocking modai and htp side of work. Differential Revision: D60947913
1 parent b69ae0c commit abc2a99

File tree

7 files changed

+116
-19
lines changed

7 files changed

+116
-19
lines changed

devtools/etdump/etdump_schema_flatcc.fbs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ table DebugEvent {
7676

7777
// String based delegate debug identifier.
7878
delegate_debug_id_str:string;
79+
80+
// Name assigned to this debug event by the runtime. If it is an operator
81+
// call this will just be the name of the operator that was executed.
82+
name:string;
7983
}
8084

8185
// All the details pertaining to an allocation done in the runtime. The main

devtools/etdump/schema_flatcc.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ class Value:
9393

9494
@dataclass
9595
class DebugEvent:
96+
name: Optional[str]
9697
chain_index: int
9798
instruction_id: int
9899
delegate_debug_id_int: Optional[int]

devtools/etdump/tests/serialize_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ def get_sample_etdump_flatcc() -> flatcc.ETDumpFlatCC:
8383
profile_event=None,
8484
allocation_event=None,
8585
debug_event=flatcc.DebugEvent(
86+
name="test_debug_event",
8687
chain_index=1,
8788
instruction_id=0,
8889
delegate_debug_id_str="56",

devtools/inspector/_inspector.py

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ def _gen_from_event(event: ProfileEvent) -> "ProfileEventSignature":
150150
# Signature of a DebugEvent
151151
@dataclass(frozen=True, order=True)
152152
class DebugEventSignature:
153+
name: str
153154
instruction_id: Optional[int] = -1
154155
delegate_id: Optional[int] = None
155156
delegate_id_str: Optional[str] = None
@@ -163,6 +164,7 @@ def _gen_from_event(event: DebugEvent) -> "DebugEventSignature":
163164
The Signature will convert these back to the intended None value
164165
"""
165166
return DebugEventSignature(
167+
event.name or "",
166168
event.instruction_id if event.instruction_id != -1 else None,
167169
event.delegate_debug_id_int if event.delegate_debug_id_int != -1 else None,
168170
event.delegate_debug_id_str if event.delegate_debug_id_str != "" else None,
@@ -468,46 +470,62 @@ def _calculate_elapsed_time(start_time, end_time):
468470
return elapsed_time
469471

470472
@staticmethod
471-
def _populate_profiling_related_fields(
473+
def _populate_event_signature_fields(
472474
ret_event: "Event",
473-
profile_event_signature: Optional[ProfileEventSignature],
474-
events: List[InstructionEvent],
475-
scale_factor: float,
475+
event_signature: Optional[Union[ProfileEventSignature, DebugEventSignature]],
476476
) -> None:
477477
"""
478478
Given a partially constructed Event, populate the fields related to
479-
the profile events
479+
the profile event signature or debug event signature
480480
481481
Fields Updated:
482482
name
483483
delegate_debug_identifier
484484
is_delegated_op
485-
perf_data
486-
delegate_debug_metadatas
487485
"""
488-
489-
# Fill out fields from profile event signature
490-
if profile_event_signature is not None:
491-
if profile_event_signature.delegate_id is not None: # 0 is a valid value
492-
delegate_debug_identifier = profile_event_signature.delegate_id
486+
if event_signature is not None:
487+
if event_signature.delegate_id is not None: # 0 is a valid value
488+
delegate_debug_identifier = event_signature.delegate_id
493489
else:
494-
delegate_debug_identifier = (
495-
profile_event_signature.delegate_id_str or None
496-
)
490+
delegate_debug_identifier = event_signature.delegate_id_str or None
497491

498492
# Use the delegate identifier as the event name if delegated
499493
is_delegated_op = delegate_debug_identifier is not None
500494
name = (
501-
profile_event_signature.name
495+
event_signature.name
502496
if not is_delegated_op
503497
else str(delegate_debug_identifier)
504498
)
505499

506500
# Update fields
507-
ret_event.name = name
501+
# This is for older version of etdump that doesn't have the name field for debug events, we don't update the name field
502+
if name:
503+
ret_event.name = name
508504
ret_event.delegate_debug_identifier = delegate_debug_identifier
509505
ret_event.is_delegated_op = is_delegated_op
510506

507+
@staticmethod
508+
def _populate_profiling_related_fields(
509+
ret_event: "Event",
510+
profile_event_signature: Optional[ProfileEventSignature],
511+
events: List[InstructionEvent],
512+
scale_factor: float,
513+
) -> None:
514+
"""
515+
Given a partially constructed Event, populate the fields related to
516+
the profile events
517+
518+
Fields Updated:
519+
name
520+
delegate_debug_identifier
521+
is_delegated_op
522+
perf_data
523+
delegate_debug_metadatas
524+
"""
525+
526+
# Fill out fields from profile event signature
527+
Event._populate_event_signature_fields(ret_event, profile_event_signature)
528+
511529
# Fill out fields from profile event
512530
data = []
513531
delegate_debug_metadatas = []
@@ -575,9 +593,15 @@ def _populate_debugging_related_fields(
575593
the debug events
576594
577595
Fields Updated:
596+
name
597+
delegate_debug_identifier
598+
is_delegated_op
578599
debug_data
579600
"""
580601

602+
# Fill out fields from debug event signature
603+
Event._populate_event_signature_fields(ret_event, debug_event_signature)
604+
581605
debug_data: List[flatcc.Value] = []
582606
for event in events:
583607
if (debug_events := event.debug_events) is None:

devtools/inspector/tests/event_blocks_test.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ def _gen_sample_profile_event(
6262
def _gen_sample_debug_event(
6363
instruction_id: int,
6464
delegate_debug_id: Optional[Union[int, str]] = None,
65+
name: str = "test_debug_event",
6566
) -> flatcc.DebugEvent:
6667
"""
6768
Helper for generating test DebugEvents
@@ -77,6 +78,7 @@ def _gen_sample_debug_event(
7778
)
7879

7980
return flatcc.DebugEvent(
81+
name=name,
8082
chain_index=0,
8183
instruction_id=instruction_id,
8284
delegate_debug_id_int=delegate_debug_id_int,
@@ -299,6 +301,42 @@ def _get_sample_etdump_flatcc_profiling_and_debugging() -> flatcc.ETDumpFlatCC:
299301

300302
return ETDumpFlatCC(version=0, run_data=[run_data_1, run_data_2, run_data_3])
301303

304+
@staticmethod
305+
def _get_sample_etdump_flatcc_debugging_only(
306+
event_name: str,
307+
delegate_debug_id: str,
308+
) -> flatcc.ETDumpFlatCC:
309+
"""
310+
Helper for getting a sample ETDumpFlatCC object with RunData signature_a
311+
and (debug_event_delegated, debug_event_non_delegated, no profile event)
312+
"""
313+
314+
debug_event_delegated = TestEventBlock._gen_sample_debug_event(
315+
instruction_id=1, delegate_debug_id=delegate_debug_id, name=event_name
316+
)
317+
debug_event_non_delegated = TestEventBlock._gen_sample_debug_event(
318+
instruction_id=1, name=event_name
319+
)
320+
run_data_1 = flatcc.RunData(
321+
name="signature_a",
322+
bundled_input_index=-1,
323+
allocators=[],
324+
events=[
325+
flatcc.Event(
326+
allocation_event=None,
327+
debug_event=debug_event_delegated,
328+
profile_event=None,
329+
),
330+
flatcc.Event(
331+
allocation_event=None,
332+
debug_event=debug_event_non_delegated,
333+
profile_event=None,
334+
),
335+
],
336+
)
337+
338+
return ETDumpFlatCC(version=0, run_data=[run_data_1])
339+
302340
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Tests ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
303341

304342
def test_gen_from_etdump(self) -> None:
@@ -370,6 +408,28 @@ def test_gen_from_etdump_inconsistent_debug_data(self) -> None:
370408
with self.assertRaises(AssertionError):
371409
EventBlock._gen_from_etdump(etdump)
372410

411+
def test_gen_from_etdump_debugging_only(self) -> None:
412+
"""
413+
Test generation of EventBlocks given an ETDump with only debugging events
414+
415+
Specifically it tests:
416+
- Correct number of EventBlocks and Events
417+
- Correct name of each Event
418+
"""
419+
event_name = "test_debug_event_only"
420+
delegate_debug_id = "debug_id"
421+
etdump: ETDumpFlatCC = TestEventBlock._get_sample_etdump_flatcc_debugging_only(
422+
event_name=event_name,
423+
delegate_debug_id=delegate_debug_id,
424+
)
425+
event_blocks = EventBlock._gen_from_etdump(etdump)
426+
self.assertEqual(len(event_blocks), 1)
427+
self.assertEqual(len(event_blocks[0].events), 2)
428+
# Delegated event uses delegate_debug_id as event name
429+
self.assertEqual(event_blocks[0].events[0].name, delegate_debug_id)
430+
# Non delegated event uses event_name as event name
431+
self.assertEqual(event_blocks[0].events[1].name, event_name)
432+
373433
def test_inspector_event_generation(self) -> None:
374434
"""
375435
Test Inspector.Event derivation from various ProfileEvent cases

devtools/inspector/tests/inspector_test.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ def test_populate_debugging_related_fields_raises_for_inconsistent_events(self):
288288
)
289289

290290
debug_event_0 = flatcc.DebugEvent(
291+
name="event",
291292
chain_index=1,
292293
instruction_id=0,
293294
delegate_debug_id_int=1,
@@ -311,6 +312,7 @@ def test_populate_debugging_related_fields_raises_for_inconsistent_events(self):
311312

312313
# Note the sizes of this tensor are different from the previous one
313314
debug_event_1 = flatcc.DebugEvent(
315+
name="event",
314316
chain_index=1,
315317
instruction_id=0,
316318
delegate_debug_id_int=1,
@@ -345,7 +347,7 @@ def test_populate_debugging_related_fields_raises_for_inconsistent_events(self):
345347
with self.assertRaises(AssertionError):
346348
Event._populate_debugging_related_fields(
347349
ret_event=ret_event,
348-
debug_event_signature=DebugEventSignature(instruction_id=1),
350+
debug_event_signature=DebugEventSignature(name="", instruction_id=1),
349351
events=events,
350352
)
351353

@@ -355,6 +357,7 @@ def test_populate_debugging_related_fields_passes_for_consistent_events(self):
355357
)
356358

357359
debug_event_0 = flatcc.DebugEvent(
360+
name="event",
358361
chain_index=1,
359362
instruction_id=0,
360363
delegate_debug_id_int=1,
@@ -378,6 +381,7 @@ def test_populate_debugging_related_fields_passes_for_consistent_events(self):
378381

379382
# Same as the event above except for offset
380383
debug_event_1 = flatcc.DebugEvent(
384+
name="event",
381385
chain_index=1,
382386
instruction_id=0,
383387
delegate_debug_id_int=1,
@@ -412,7 +416,9 @@ def test_populate_debugging_related_fields_passes_for_consistent_events(self):
412416
# Expect it runs with no error because is_inference_output_equal() is mocked to return True
413417
Event._populate_debugging_related_fields(
414418
ret_event=ret_event,
415-
debug_event_signature=DebugEventSignature(instruction_id=1),
419+
debug_event_signature=DebugEventSignature(
420+
name="name", instruction_id=1
421+
),
416422
events=events,
417423
)
418424

devtools/inspector/tests/inspector_utils_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ def test_find_populated_event(self):
7474
end_time=2002,
7575
)
7676
debug_event = flatcc.DebugEvent(
77+
name="test_debug_event",
7778
chain_index=1,
7879
instruction_id=0,
7980
delegate_debug_id_str="56",

0 commit comments

Comments
 (0)