Skip to content

Commit 95e8e18

Browse files
Varun Purifacebook-github-bot
authored andcommitted
Add schema changes for delegate profiling in etdump_flatcc
Summary: Make a few changes to the flatbuffer+python schema to enable delegate profiling - Remove `EventType` - we can determine the event type by simply checking which of the fields in Event is null. - Add `instruction_id`, `delegate_debug_id_int` and `delegate_debug_id_str` to `ProfileEvent` to replace `debug_handle` Differential Revision: D49076428
1 parent 71470a7 commit 95e8e18

File tree

6 files changed

+67
-30
lines changed

6 files changed

+67
-30
lines changed

exir/_serialize/_flatbuffer.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import tempfile
1414

1515
from dataclasses import dataclass
16-
from typing import Callable, Dict, Optional, Sequence
16+
from typing import Callable, Dict, List, Optional, Sequence
1717

1818

1919
def _is_valid_alignment(alignment: int) -> bool:
@@ -212,7 +212,12 @@ def _flatc_compile(output_dir: str, schema_path: str, json_path: str) -> None:
212212
)
213213

214214

215-
def _flatc_decompile(output_dir: str, schema_path: str, bin_path: str) -> None:
215+
def _flatc_decompile(
216+
output_dir: str,
217+
schema_path: str,
218+
bin_path: str,
219+
flatc_additional_args: Optional[List[str]] = None,
220+
) -> None:
216221
"""Deserializes binary flatbuffer data to a JSON file.
217222
218223
Args:
@@ -223,8 +228,10 @@ def _flatc_decompile(output_dir: str, schema_path: str, bin_path: str) -> None:
223228
bin_path: Path to the data to deserialize, as binary data compatible
224229
with the schema.
225230
"""
231+
flatc_additional_args = flatc_additional_args if flatc_additional_args else []
226232
_run_flatc(
227-
[
233+
flatc_additional_args
234+
+ [
228235
"--json",
229236
"--defaults-json",
230237
"--strict-json",

sdk/etdump/TARGETS

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ python_library(
3939
"fbsource//third-party/pypi/setuptools:setuptools",
4040
":schema",
4141
":schema_flatcc",
42-
"//executorch/exir/_serialize:_bindings",
4342
"//executorch/exir/_serialize:lib",
4443
],
4544
)

sdk/etdump/etdump_schema_flatcc.fbs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,20 @@ table ProfileEvent {
8282

8383
// The chain to which this instruction belongs to. For now it will always be 0,
8484
// as chains are not used, but left in place in case they are used in the future.
85-
chain_idx:int;
85+
chain_id:int;
8686

87-
// If this event corresponds to an operator execution then this is the debug
88-
// handle that was generated by the compiler that will help us map back this
89-
// operator to the source code.
90-
debug_handle:int;
87+
// Runtime instruction id to which this event corresponds to.
88+
instruction_id:int = -1;
89+
90+
// If this is a delegate event then these will be the corresponding delegate
91+
// debug identifiers of the event which occurred in the delegate backend. This
92+
// should be the same delegate debug identifier that was generated AOT.
93+
94+
// Integer based delegate debug identifier.
95+
delegate_debug_id_int:int = -1;
96+
97+
// String based delegate debug identifier.
98+
delegate_debug_id_str:string;
9199

92100
// Time at which this event started. Could be in units of time or CPU cycles.
93101
start_time:ulong;

sdk/etdump/schema_flatcc.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,10 @@ class PROFILE_EVENT_ENUM(Enum):
9191
@dataclass
9292
class ProfileEvent:
9393
name: str
94-
chain_idx: int
95-
debug_handle: int
94+
chain_id: int
95+
instruction_id: int
96+
delegate_debug_id_int: int
97+
delegate_debug_id_str: str
9698
start_time: int
9799
end_time: int
98100

@@ -118,8 +120,9 @@ class Event:
118120

119121
@dataclass
120122
class RunData:
121-
allocators: List[Allocator]
122-
events: List[Event]
123+
name: str
124+
allocators: Optional[List[Allocator]]
125+
events: Optional[List[Event]]
123126

124127

125128
@dataclass

sdk/etdump/serialize.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,11 @@
1111
import tempfile
1212
from typing import Union
1313

14-
# pyre-ignore[21]: Could not find module `executorch.exir._serialize._bindings`.
15-
import executorch.exir._serialize._bindings as bindings # @manual=//executorch/exir/_serialize:_bindings
16-
1714
import pkg_resources
1815

1916
from executorch.exir._serialize._dataclass import _DataclassEncoder, _json_to_dataclass
17+
18+
from executorch.exir._serialize._flatbuffer import _flatc_compile, _flatc_decompile
2019
from executorch.sdk.etdump.schema import ETDump
2120
from executorch.sdk.etdump.schema_flatcc import ETDumpFlatCC
2221

@@ -55,8 +54,7 @@ def _convert_to_flatbuffer(etdump_json: str) -> bytes:
5554
with open(json_path, "wb") as json_file:
5655
json_file.write(etdump_json.encode("ascii"))
5756

58-
# pyre-ignore
59-
bindings.flatc_compile(d, schema_path, json_path)
57+
_flatc_compile(d, schema_path, json_path)
6058
output_path = os.path.join(d, "{}.etdp".format(ETDUMP_SCHEMA_NAME))
6159
with open(output_path, "rb") as output_file:
6260
return output_file.read()
@@ -71,8 +69,7 @@ def _convert_from_flatbuffer(etdump_flatbuffer: bytes) -> bytes:
7169
bin_path = os.path.join(d, "schema.bin")
7270
with open(bin_path, "wb") as bin_file:
7371
bin_file.write(etdump_flatbuffer)
74-
# pyre-ignore
75-
bindings.flatc_decompile(d, schema_path, bin_path)
72+
_flatc_decompile(d, schema_path, bin_path)
7673
output_path = os.path.join(d, "schema.json")
7774
with open(output_path, "rb") as output_file:
7875
return output_file.read()
@@ -126,14 +123,13 @@ def _convert_to_flatcc(etdump_json: str) -> bytes:
126123
with open(json_path, "wb") as json_file:
127124
json_file.write(etdump_json.encode("ascii"))
128125

129-
# pyre-ignore
130-
bindings.flatc_compile(d, schema_path, json_path)
126+
_flatc_compile(d, schema_path, json_path)
131127
output_path = os.path.join(d, "{}.etdp".format(ETDUMP_FLATCC_SCHEMA_NAME))
132128
with open(output_path, "rb") as output_file:
133129
return output_file.read()
134130

135131

136-
def _convert_from_flatcc(etdump_flatbuffer: bytes) -> bytes:
132+
def _convert_from_flatcc(etdump_flatbuffer: bytes, size_prefixed: bool = True) -> bytes:
137133
with tempfile.TemporaryDirectory() as d:
138134
_write_schema(d, ETDUMP_FLATCC_SCHEMA_NAME)
139135
_write_schema(d, SCALAR_TYPE_SCHEMA_NAME)
@@ -142,8 +138,10 @@ def _convert_from_flatcc(etdump_flatbuffer: bytes) -> bytes:
142138
bin_path = os.path.join(d, "schema.bin")
143139
with open(bin_path, "wb") as bin_file:
144140
bin_file.write(etdump_flatbuffer)
145-
# pyre-ignore
146-
bindings.flatc_decompile(d, schema_path, bin_path)
141+
additional_args = []
142+
if size_prefixed:
143+
additional_args = ["--size-prefixed"]
144+
_flatc_decompile(d, schema_path, bin_path, additional_args)
147145
output_path = os.path.join(d, "schema.json")
148146
with open(output_path, "rb") as output_file:
149147
return output_file.read()
@@ -163,7 +161,9 @@ def serialize_to_etdump_flatcc(
163161
return _convert_to_flatcc(_serialize_from_etdump_to_json(etdump))
164162

165163

166-
def deserialize_from_etdump_flatcc(data: bytes) -> ETDumpFlatCC:
164+
def deserialize_from_etdump_flatcc(
165+
data: bytes, size_prefixed: bool = True
166+
) -> ETDumpFlatCC:
167167
"""
168168
Given an etdump binary blob (constructed using the FlatCC schema) this function will deserialize
169169
it and return the FlatCC python object representation of etdump.
@@ -172,4 +172,6 @@ def deserialize_from_etdump_flatcc(data: bytes) -> ETDumpFlatCC:
172172
Returns:
173173
Deserialized ETDump python object.
174174
"""
175-
return _deserialize_from_json_to_etdump_flatcc(_convert_from_flatcc(data))
175+
return _deserialize_from_json_to_etdump_flatcc(
176+
_convert_from_flatcc(data, size_prefixed)
177+
)

sdk/etdump/tests/serialize_test.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ def get_sample_etdump_flatcc() -> flatcc.ETDumpFlatCC:
100100
version=0,
101101
run_data=[
102102
flatcc.RunData(
103+
name="test_block",
103104
allocators=[
104105
flatcc.Allocator(
105106
name="test_allocator",
@@ -109,8 +110,23 @@ def get_sample_etdump_flatcc() -> flatcc.ETDumpFlatCC:
109110
flatcc.Event(
110111
profile_event=flatcc.ProfileEvent(
111112
name="test_profile_event",
112-
chain_idx=1,
113-
debug_handle=1,
113+
chain_id=1,
114+
instruction_id=1,
115+
delegate_debug_id_str="",
116+
delegate_debug_id_int=-1,
117+
start_time=1001,
118+
end_time=2002,
119+
),
120+
allocation_event=None,
121+
debug_event=None,
122+
),
123+
flatcc.Event(
124+
profile_event=flatcc.ProfileEvent(
125+
name="test_profile_event_delegated",
126+
chain_id=1,
127+
instruction_id=1,
128+
delegate_debug_id_str="",
129+
delegate_debug_id_int=13,
114130
start_time=1001,
115131
end_time=2002,
116132
),
@@ -169,7 +185,9 @@ def test_serialize(self) -> None:
169185
program = get_sample_etdump_flatcc()
170186

171187
flatcc_from_py = serialize_to_etdump_flatcc(program)
172-
deserialized_obj = deserialize_from_etdump_flatcc(flatcc_from_py)
188+
deserialized_obj = deserialize_from_etdump_flatcc(
189+
flatcc_from_py, size_prefixed=False
190+
)
173191
self.assertEqual(
174192
program,
175193
deserialized_obj,

0 commit comments

Comments
 (0)