-
Notifications
You must be signed in to change notification settings - Fork 524
/
Copy pathserialize.py
110 lines (85 loc) · 3.72 KB
/
serialize.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
# pyre-strict
import json
import os
import tempfile
import pkg_resources
from executorch.devtools.etdump.schema_flatcc import ETDumpFlatCC
from executorch.exir._serialize._dataclass import _DataclassEncoder, _json_to_dataclass
from executorch.exir._serialize._flatbuffer import _flatc_compile, _flatc_decompile
# The prefix of schema files used for etdump
ETDUMP_FLATCC_SCHEMA_NAME = "etdump_schema_flatcc"
SCALAR_TYPE_SCHEMA_NAME = "scalar_type"
def _write_schema(d: str, schema_name: str) -> None:
schema_path = os.path.join(d, "{}.fbs".format(schema_name))
with open(schema_path, "wb") as schema_file:
schema_file.write(
pkg_resources.resource_string(__name__, "{}.fbs".format(schema_name))
)
def _serialize_from_etdump_to_json(etdump: ETDumpFlatCC) -> str:
return json.dumps(etdump, cls=_DataclassEncoder, indent=4)
"""
ETDump FlatCC Schema Implementations
"""
# from json to etdump
def _deserialize_from_json_to_etdump_flatcc(etdump_json: bytes) -> ETDumpFlatCC:
etdump_json = json.loads(etdump_json)
return _json_to_dataclass(etdump_json, ETDumpFlatCC)
def _convert_to_flatcc(etdump_json: str) -> bytes:
with tempfile.TemporaryDirectory() as d:
# load given and common schema
_write_schema(d, ETDUMP_FLATCC_SCHEMA_NAME)
_write_schema(d, SCALAR_TYPE_SCHEMA_NAME)
schema_path = os.path.join(d, "{}.fbs".format(ETDUMP_FLATCC_SCHEMA_NAME))
json_path = os.path.join(d, "{}.json".format(ETDUMP_FLATCC_SCHEMA_NAME))
with open(json_path, "wb") as json_file:
json_file.write(etdump_json.encode("ascii"))
_flatc_compile(d, schema_path, json_path)
output_path = os.path.join(d, "{}.etdp".format(ETDUMP_FLATCC_SCHEMA_NAME))
with open(output_path, "rb") as output_file:
return output_file.read()
def _convert_from_flatcc(etdump_flatbuffer: bytes, size_prefixed: bool = True) -> bytes:
with tempfile.TemporaryDirectory() as d:
_write_schema(d, ETDUMP_FLATCC_SCHEMA_NAME)
_write_schema(d, SCALAR_TYPE_SCHEMA_NAME)
schema_path = os.path.join(d, "{}.fbs".format(ETDUMP_FLATCC_SCHEMA_NAME))
bin_path = os.path.join(d, "schema.bin")
with open(bin_path, "wb") as bin_file:
bin_file.write(etdump_flatbuffer)
additional_args = []
if size_prefixed:
additional_args = ["--size-prefixed"]
_flatc_decompile(d, schema_path, bin_path, additional_args)
output_path = os.path.join(d, "schema.json")
with open(output_path, "rb") as output_file:
return output_file.read()
def serialize_to_etdump_flatcc(
etdump: ETDumpFlatCC,
) -> bytes:
"""
Given an ETdump python object this function will return a serialized object
that can then be written to a file using the FlatCC schema.
Args:
etdump: ETDump python object that the user wants to serialize.
Returns:
Serialized etdump binary blob using the FlatCC schema
"""
return _convert_to_flatcc(_serialize_from_etdump_to_json(etdump))
def deserialize_from_etdump_flatcc(
data: bytes, size_prefixed: bool = True
) -> ETDumpFlatCC:
"""
Given an etdump binary blob (constructed using the FlatCC schema) this function will deserialize
it and return the FlatCC python object representation of etdump.
Args:
data: Serialized etdump binary blob.
Returns:
Deserialized ETDump python object.
"""
return _deserialize_from_json_to_etdump_flatcc(
_convert_from_flatcc(data, size_prefixed)
)