Skip to content

Commit ee00caa

Browse files
authored
[xnnpack] Add debug XNNGraph printing (#7617)
Prints to a file, with increasing id. TODO: use actual delegate instance id in the filename. Take filepath from compile_spec.
1 parent a727b55 commit ee00caa

File tree

1 file changed

+33
-5
lines changed

1 file changed

+33
-5
lines changed

backends/xnnpack/serialization/xnnpack_graph_serialize.py

+33-5
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,23 @@
55
# LICENSE file in the root directory of this source tree.
66

77
import json
8+
9+
import logging
810
import os
911
import tempfile
1012

1113
from dataclasses import dataclass, fields, is_dataclass
12-
from typing import ClassVar, Literal
14+
from typing import ClassVar, Literal, Optional
1315

1416
import pkg_resources
1517
from executorch.backends.xnnpack.serialization.xnnpack_graph_schema import XNNGraph
1618
from executorch.exir._serialize._dataclass import _DataclassEncoder
1719

1820
from executorch.exir._serialize._flatbuffer import _flatc_compile
1921

22+
logger = logging.getLogger(__name__)
23+
logger.setLevel(logging.WARNING)
24+
2025
# Byte order of numbers written to program headers. Always little-endian
2126
# regardless of the host system, since all commonly-used modern CPUs are little
2227
# endian.
@@ -273,19 +278,42 @@ def _pad_to(data: bytes, length: int) -> bytes:
273278
return data
274279

275280

276-
def pretty_print_xnngraph(xnnpack_graph_json: str):
281+
def pretty_print_xnngraph(xnnpack_graph_json: str, filename: Optional[str] = None):
277282
"""
278-
Pretty print the XNNGraph
283+
Pretty print the XNNGraph, optionally writing to a file if filename is provided
279284
"""
280-
from pprint import pprint
285+
from pprint import pformat
281286

282287
d = json.loads(xnnpack_graph_json)
283-
pprint(d)
288+
pstr = pformat(d, indent=2, compact=True).replace("'", '"')
289+
if filename:
290+
with open(filename, "w") as f:
291+
if filename.endswith(".json"):
292+
pstr = pstr.replace("None", "null")
293+
f.write(pstr)
294+
else: # dump to stdout
295+
print("XNNGraph:")
296+
print(pstr)
297+
print("End of XNNGraph")
298+
299+
300+
# TODO: Replace this with an actual delegate id
301+
_delegate_instance_id = 0
284302

285303

286304
def convert_to_flatbuffer(xnnpack_graph: XNNGraph) -> bytes:
305+
global _delegate_instance_id
287306
sanity_check_xnngraph_dataclass(xnnpack_graph)
288307
xnnpack_graph_json = json.dumps(xnnpack_graph, cls=_DataclassEncoder)
308+
309+
# Log the XNNGraph if debugging
310+
if logger.getEffectiveLevel() == logging.DEBUG:
311+
filename: str = f"./xnnpack_delegate_graph_{_delegate_instance_id}.json"
312+
logger.debug(f"Writing XNNGraph to {filename}")
313+
pretty_print_xnngraph(xnnpack_graph_json, filename)
314+
315+
_delegate_instance_id += 1
316+
289317
with tempfile.TemporaryDirectory() as d:
290318
schema_path = os.path.join(d, "schema.fbs")
291319
with open(schema_path, "wb") as schema_file:

0 commit comments

Comments
 (0)