Skip to content

Commit 043bbe4

Browse files
Olivia-liufacebook-github-bot
authored andcommitted
Debug event populates event name
Summary: 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 2ce4ad1 commit 043bbe4

File tree

7 files changed

+120
-19
lines changed

7 files changed

+120
-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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ class Value:
9393

9494
@dataclass
9595
class DebugEvent:
96+
name: Optional[
97+
str
98+
] # I think this is BC-breaking for old etdump files. Need to give a default value.
9699
chain_index: int
97100
instruction_id: int
98101
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: 43 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,
@@ -428,6 +430,8 @@ def _gen_from_inference_events(
428430
profile_event_signature = signature.profile_event_signature
429431
debug_event_signature = signature.debug_event_signature
430432

433+
# TODO: Assert the signatures don't conflict
434+
431435
# Event is gradually populated in this function
432436
ret_event: Event = Event(
433437
name="",
@@ -468,46 +472,62 @@ def _calculate_elapsed_time(start_time, end_time):
468472
return elapsed_time
469473

470474
@staticmethod
471-
def _populate_profiling_related_fields(
475+
def _populate_event_signature_fields(
472476
ret_event: "Event",
473-
profile_event_signature: Optional[ProfileEventSignature],
474-
events: List[InstructionEvent],
475-
scale_factor: float,
477+
event_signature: Optional[Union[ProfileEventSignature, DebugEventSignature]],
476478
) -> None:
477479
"""
478480
Given a partially constructed Event, populate the fields related to
479-
the profile events
481+
the profile event signature or debug event signature
480482
481483
Fields Updated:
482484
name
483485
delegate_debug_identifier
484486
is_delegated_op
485-
perf_data
486-
delegate_debug_metadatas
487487
"""
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
488+
if event_signature is not None:
489+
if event_signature.delegate_id is not None: # 0 is a valid value
490+
delegate_debug_identifier = event_signature.delegate_id
493491
else:
494-
delegate_debug_identifier = (
495-
profile_event_signature.delegate_id_str or None
496-
)
492+
delegate_debug_identifier = event_signature.delegate_id_str or None
497493

498494
# Use the delegate identifier as the event name if delegated
499495
is_delegated_op = delegate_debug_identifier is not None
500496
name = (
501-
profile_event_signature.name
497+
event_signature.name
502498
if not is_delegated_op
503499
else str(delegate_debug_identifier)
504500
)
505501

506502
# Update fields
507-
ret_event.name = name
503+
# This is for older version of etdump that doesn't have the name field for debug events, we don't update the name field
504+
if name:
505+
ret_event.name = name
508506
ret_event.delegate_debug_identifier = delegate_debug_identifier
509507
ret_event.is_delegated_op = is_delegated_op
510508

509+
@staticmethod
510+
def _populate_profiling_related_fields(
511+
ret_event: "Event",
512+
profile_event_signature: Optional[ProfileEventSignature],
513+
events: List[InstructionEvent],
514+
scale_factor: float,
515+
) -> None:
516+
"""
517+
Given a partially constructed Event, populate the fields related to
518+
the profile events
519+
520+
Fields Updated:
521+
name
522+
delegate_debug_identifier
523+
is_delegated_op
524+
perf_data
525+
delegate_debug_metadatas
526+
"""
527+
528+
# Fill out fields from profile event signature
529+
Event._populate_event_signature_fields(ret_event, profile_event_signature)
530+
511531
# Fill out fields from profile event
512532
data = []
513533
delegate_debug_metadatas = []
@@ -575,9 +595,15 @@ def _populate_debugging_related_fields(
575595
the debug events
576596
577597
Fields Updated:
598+
name
599+
delegate_debug_identifier
600+
is_delegated_op
578601
debug_data
579602
"""
580603

604+
# Fill out fields from debug event signature
605+
Event._populate_event_signature_fields(ret_event, debug_event_signature)
606+
581607
debug_data: List[flatcc.Value] = []
582608
for event in events:
583609
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)