5
5
# LICENSE file in the root directory of this source tree.
6
6
7
7
import dataclasses
8
+ import logging
8
9
from collections import defaultdict , OrderedDict
9
10
from dataclasses import dataclass
10
- from typing import Dict , List , Mapping , NewType , Optional , Tuple , Union
11
+ from typing import (
12
+ Dict ,
13
+ List ,
14
+ Mapping ,
15
+ Optional ,
16
+ Sequence ,
17
+ Tuple ,
18
+ TypeAlias ,
19
+ TypedDict ,
20
+ Union ,
21
+ )
11
22
12
23
import numpy as np
13
24
import pandas as pd
20
31
from executorch .sdk .etrecord import parse_etrecord
21
32
from tabulate import tabulate
22
33
34
+ log : logging .Logger = logging .getLogger (__name__ )
23
35
24
36
# Signature of a ProfileEvent
25
37
@dataclass (frozen = True , order = True )
@@ -46,7 +58,19 @@ def _gen_from_event(event: ProfileEvent) -> "ProfileEventSignature":
46
58
47
59
48
60
# Signature of a RunData as defined by its ProfileEvents
49
- RunSignature = NewType ("RunSignature" , Tuple [ProfileEventSignature ])
61
+ RunSignature : TypeAlias = Tuple [ProfileEventSignature ]
62
+
63
+
64
+ # Typing for mapping Event.delegate_debug_identifiers to debug_handle(s)
65
+ DelegateIdentifierDebugHandleMap : TypeAlias = Union [
66
+ Mapping [int , Tuple [int , ...]], Mapping [str , Tuple [int , ...]]
67
+ ]
68
+
69
+ # Typing for Dict containig delegate metadata
70
+ DelegateMetadata = TypedDict (
71
+ "DelegateMetadata" ,
72
+ {"name" : str , "delegate_map" : DelegateIdentifierDebugHandleMap },
73
+ )
50
74
51
75
52
76
@dataclass
@@ -97,7 +121,7 @@ class Event:
97
121
delegate_debug_identifier : Optional [Union [int , str ]] = None
98
122
99
123
# Debug Handles in the model graph to which this event is correlated
100
- debug_handles : Optional [Union [int , List [int ]]] = None
124
+ debug_handles : Optional [Union [int , Sequence [int ]]] = None
101
125
102
126
stack_trace : Dict [str , str ] = dataclasses .field (default_factory = dict )
103
127
module_hierarchy : Dict [str , Dict ] = dataclasses .field (default_factory = dict )
@@ -203,7 +227,7 @@ def _gen_from_etdump(etdump: ETDumpFlatCC) -> List["EventBlock"]:
203
227
profile_events [signature ] = profile_event
204
228
205
229
# Create a RunSignature from the ProfileEventSignature found
206
- run_signature : RunSignature = RunSignature ( tuple (profile_events .keys () ))
230
+ run_signature : RunSignature = tuple (profile_events .keys ())
207
231
208
232
# Update the Profile Run Groups, indexed on the RunSignature
209
233
run_signature_events : OrderedDict [
@@ -224,6 +248,47 @@ def _gen_from_etdump(etdump: ETDumpFlatCC) -> List["EventBlock"]:
224
248
for index , profile_events in enumerate (profile_run_groups .values ())
225
249
]
226
250
251
+ def _gen_resolve_debug_handles (
252
+ self ,
253
+ handle_map : Dict [int , List [int ]],
254
+ delegate_map : Optional [Dict [int , DelegateMetadata ]] = None ,
255
+ ):
256
+ """
257
+ Given mappings from instruction id to debug handles, populate the
258
+ debug_handles field of all underlying events
259
+
260
+ If the event is delegated, index with the instruction_id and delegate_debug_identifier
261
+ to obtain the debug_handle via the delegate map
262
+ """
263
+ for event in self .events :
264
+ # Check for the instruction_id in handle map
265
+ if (
266
+ instruction_id := event .instruction_id
267
+ ) is None or instruction_id not in handle_map :
268
+ continue
269
+
270
+ # For non-delegated event, handles are found in handle_map
271
+ if (delegate_debug_id := event .delegate_debug_identifier ) is None :
272
+ event .debug_handles = handle_map [instruction_id ]
273
+ continue
274
+
275
+ # Check that the delegated event has a corresponding mapping
276
+ if (
277
+ delegate_map is None
278
+ or (delegate_metadata := delegate_map .get (instruction_id )) is None
279
+ ):
280
+ event .debug_handles = handle_map [instruction_id ]
281
+ log .warning (
282
+ f" No delegate mapping found for delegate with instruction id { event .instruction_id } "
283
+ )
284
+ continue
285
+
286
+ # For delegated events, handles are found via delegateMetadata
287
+ event .delegate_backend_name = delegate_metadata .get ("name" , "" )
288
+ event .debug_handles = delegate_metadata .get ("delegate_map" , {}).get (
289
+ delegate_debug_id # pyre-ignore
290
+ )
291
+
227
292
228
293
class Inspector :
229
294
"""
0 commit comments