Skip to content

Commit af80804

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. Reviewed By: Jack-Khuu Differential Revision: D60947913 fbshipit-source-id: 78cb252dc4f0088c2af3a27f467f8cb6182cc785
1 parent 7e374d7 commit af80804

File tree

7 files changed

+115
-17
lines changed

7 files changed

+115
-17
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: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ def _gen_from_event(event: ProfileEvent) -> "ProfileEventSignature":
152152
# Signature of a DebugEvent
153153
@dataclass(frozen=True, order=True)
154154
class DebugEventSignature:
155+
name: str = ""
155156
instruction_id: Optional[int] = -1
156157
delegate_id: Optional[int] = None
157158
delegate_id_str: Optional[str] = None
@@ -165,6 +166,7 @@ def _gen_from_event(event: DebugEvent) -> "DebugEventSignature":
165166
The Signature will convert these back to the intended None value
166167
"""
167168
return DebugEventSignature(
169+
event.name or "",
168170
event.instruction_id if event.instruction_id != -1 else None,
169171
event.delegate_debug_id_int if event.delegate_debug_id_int != -1 else None,
170172
event.delegate_debug_id_str if event.delegate_debug_id_str != "" else None,
@@ -470,46 +472,63 @@ def _calculate_elapsed_time(start_time, end_time):
470472
return elapsed_time
471473

472474
@staticmethod
473-
def _populate_profiling_related_fields(
475+
def _populate_event_signature_fields(
474476
ret_event: "Event",
475-
profile_event_signature: Optional[ProfileEventSignature],
476-
events: List[InstructionEvent],
477-
scale_factor: float,
477+
event_signature: Optional[Union[ProfileEventSignature, DebugEventSignature]],
478478
) -> None:
479479
"""
480480
Given a partially constructed Event, populate the fields related to
481-
the profile events
481+
the profile event signature or debug event signature
482482
483483
Fields Updated:
484484
name
485485
delegate_debug_identifier
486486
is_delegated_op
487-
perf_data
488-
delegate_debug_metadatas
489487
"""
490-
491-
# Fill out fields from profile event signature
492-
if profile_event_signature is not None:
493-
if profile_event_signature.delegate_id is not None: # 0 is a valid value
494-
delegate_debug_identifier = profile_event_signature.delegate_id
488+
# TODO: T201347372 Push the None check to ealier in the stack.
489+
if event_signature is not None:
490+
if event_signature.delegate_id is not None: # 0 is a valid value
491+
delegate_debug_identifier = event_signature.delegate_id
495492
else:
496-
delegate_debug_identifier = (
497-
profile_event_signature.delegate_id_str or None
498-
)
493+
delegate_debug_identifier = event_signature.delegate_id_str or None
499494

500495
# Use the delegate identifier as the event name if delegated
501496
is_delegated_op = delegate_debug_identifier is not None
502497
name = (
503-
profile_event_signature.name
498+
event_signature.name
504499
if not is_delegated_op
505500
else str(delegate_debug_identifier)
506501
)
507502

508503
# Update fields
509-
ret_event.name = name
504+
# This is for older version of etdump that doesn't have the name field for debug events, we don't update the name field
505+
if name:
506+
ret_event.name = name
510507
ret_event.delegate_debug_identifier = delegate_debug_identifier
511508
ret_event.is_delegated_op = is_delegated_op
512509

510+
@staticmethod
511+
def _populate_profiling_related_fields(
512+
ret_event: "Event",
513+
profile_event_signature: Optional[ProfileEventSignature],
514+
events: List[InstructionEvent],
515+
scale_factor: float,
516+
) -> None:
517+
"""
518+
Given a partially constructed Event, populate the fields related to
519+
the profile events
520+
521+
Fields Updated:
522+
name
523+
delegate_debug_identifier
524+
is_delegated_op
525+
perf_data
526+
delegate_debug_metadatas
527+
"""
528+
529+
# Fill out fields from profile event signature
530+
Event._populate_event_signature_fields(ret_event, profile_event_signature)
531+
513532
# Fill out fields from profile event
514533
data = []
515534
delegate_debug_metadatas = []
@@ -577,9 +596,15 @@ def _populate_debugging_related_fields(
577596
the debug events
578597
579598
Fields Updated:
599+
name
600+
delegate_debug_identifier
601+
is_delegated_op
580602
debug_data
581603
"""
582604

605+
# Fill out fields from debug event signature
606+
Event._populate_event_signature_fields(ret_event, debug_event_signature)
607+
583608
debug_data: List[flatcc.Value] = []
584609
for event in events:
585610
if (debug_events := event.debug_events) is None:

devtools/inspector/tests/event_blocks_test.py

Lines changed: 62 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_debug_events_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,30 @@ 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_debug_events_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 = (
422+
TestEventBlock._get_sample_etdump_flatcc_debug_events_only(
423+
event_name=event_name,
424+
delegate_debug_id=delegate_debug_id,
425+
)
426+
)
427+
event_blocks = EventBlock._gen_from_etdump(etdump)
428+
self.assertEqual(len(event_blocks), 1)
429+
self.assertEqual(len(event_blocks[0].events), 2)
430+
# Delegated event uses delegate_debug_id as event name
431+
self.assertEqual(event_blocks[0].events[0].name, delegate_debug_id)
432+
# Non delegated event uses event_name as event name
433+
self.assertEqual(event_blocks[0].events[1].name, event_name)
434+
373435
def test_inspector_event_generation(self) -> None:
374436
"""
375437
Test Inspector.Event derivation from various ProfileEvent cases

devtools/inspector/tests/inspector_test.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ def test_populate_debugging_related_fields_raises_for_inconsistent_events(self):
318318
)
319319

320320
debug_event_0 = flatcc.DebugEvent(
321+
name="event",
321322
chain_index=1,
322323
instruction_id=0,
323324
delegate_debug_id_int=1,
@@ -341,6 +342,7 @@ def test_populate_debugging_related_fields_raises_for_inconsistent_events(self):
341342

342343
# Note the sizes of this tensor are different from the previous one
343344
debug_event_1 = flatcc.DebugEvent(
345+
name="event",
344346
chain_index=1,
345347
instruction_id=0,
346348
delegate_debug_id_int=1,
@@ -385,6 +387,7 @@ def test_populate_debugging_related_fields_passes_for_consistent_events(self):
385387
)
386388

387389
debug_event_0 = flatcc.DebugEvent(
390+
name="event",
388391
chain_index=1,
389392
instruction_id=0,
390393
delegate_debug_id_int=1,
@@ -408,6 +411,7 @@ def test_populate_debugging_related_fields_passes_for_consistent_events(self):
408411

409412
# Same as the event above except for offset
410413
debug_event_1 = flatcc.DebugEvent(
414+
name="event",
411415
chain_index=1,
412416
instruction_id=0,
413417
delegate_debug_id_int=1,

devtools/inspector/tests/inspector_utils_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ def test_find_populated_event(self):
7878
end_time=2002,
7979
)
8080
debug_event = flatcc.DebugEvent(
81+
name="test_debug_event",
8182
chain_index=1,
8283
instruction_id=0,
8384
delegate_debug_id_str="56",

0 commit comments

Comments
 (0)