Skip to content

Commit fc1bd20

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 7650667 commit fc1bd20

File tree

7 files changed

+117
-20
lines changed

7 files changed

+117
-20
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 & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def gen_from_events(run_events: List[flatcc.Event]) -> List["InstructionEvent"]:
128128
# Signature of a ProfileEvent
129129
@dataclass(frozen=True, order=True)
130130
class ProfileEventSignature:
131-
name: str
131+
name: str = ""
132132
instruction_id: Optional[int]
133133
delegate_id: Optional[int] = None
134134
delegate_id_str: Optional[str] = None
@@ -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,62 @@ 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+
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
495491
else:
496-
delegate_debug_identifier = (
497-
profile_event_signature.delegate_id_str or None
498-
)
492+
delegate_debug_identifier = event_signature.delegate_id_str or None
499493

500494
# Use the delegate identifier as the event name if delegated
501495
is_delegated_op = delegate_debug_identifier is not None
502496
name = (
503-
profile_event_signature.name
497+
event_signature.name
504498
if not is_delegated_op
505499
else str(delegate_debug_identifier)
506500
)
507501

508502
# Update fields
509-
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
510506
ret_event.delegate_debug_identifier = delegate_debug_identifier
511507
ret_event.is_delegated_op = is_delegated_op
512508

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+
513531
# Fill out fields from profile event
514532
data = []
515533
delegate_debug_metadatas = []
@@ -577,9 +595,15 @@ def _populate_debugging_related_fields(
577595
the debug events
578596
579597
Fields Updated:
598+
name
599+
delegate_debug_identifier
600+
is_delegated_op
580601
debug_data
581602
"""
582603

604+
# Fill out fields from debug event signature
605+
Event._populate_event_signature_fields(ret_event, debug_event_signature)
606+
583607
debug_data: List[flatcc.Value] = []
584608
for event in events:
585609
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
@@ -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,
@@ -375,7 +377,7 @@ def test_populate_debugging_related_fields_raises_for_inconsistent_events(self):
375377
with self.assertRaises(AssertionError):
376378
Event._populate_debugging_related_fields(
377379
ret_event=ret_event,
378-
debug_event_signature=DebugEventSignature(instruction_id=1),
380+
debug_event_signature=DebugEventSignature(name="", instruction_id=1),
379381
events=events,
380382
)
381383

@@ -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,
@@ -442,7 +446,9 @@ def test_populate_debugging_related_fields_passes_for_consistent_events(self):
442446
# Expect it runs with no error because is_inference_output_equal() is mocked to return True
443447
Event._populate_debugging_related_fields(
444448
ret_event=ret_event,
445-
debug_event_signature=DebugEventSignature(instruction_id=1),
449+
debug_event_signature=DebugEventSignature(
450+
name="name", instruction_id=1
451+
),
446452
events=events,
447453
)
448454

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)