11# Copyright (c) Meta Platforms, Inc. and affiliates.
22# All rights reserved.
33#
4+ # pyre-strict
5+ #
46# This source code is licensed under the BSD-style license found in the
57# LICENSE file in the root directory of this source tree.
68
1921 VkBytes ,
2022 VkGraph ,
2123)
22- from executorch .exir ._serialize ._dataclass import _DataclassEncoder
24+ from executorch .exir ._serialize ._dataclass import _DataclassEncoder , _json_to_dataclass
2325
24- from executorch .exir ._serialize ._flatbuffer import _flatc_compile
26+ from executorch .exir ._serialize ._flatbuffer import _flatc_compile , _flatc_decompile
2527
2628
2729def convert_to_flatbuffer (vk_graph : VkGraph ) -> bytes :
@@ -40,6 +42,25 @@ def convert_to_flatbuffer(vk_graph: VkGraph) -> bytes:
4042 return output_file .read ()
4143
4244
45+ def flatbuffer_to_vk_graph (flatbuffers : bytes ) -> VkGraph :
46+ # Following similar (de)serialization logic on other backends:
47+ # https://github.com/pytorch/executorch/blob/main/backends/qualcomm/serialization/qc_schema_serialize.py#L33
48+ with tempfile .TemporaryDirectory () as d :
49+ schema_path = os .path .join (d , "schema.fbs" )
50+ with open (schema_path , "wb" ) as schema_file :
51+ schema_file .write (pkg_resources .resource_string (__name__ , "schema.fbs" ))
52+
53+ bin_path = os .path .join (d , "schema.bin" )
54+ with open (bin_path , "wb" ) as bin_file :
55+ bin_file .write (flatbuffers )
56+
57+ _flatc_decompile (d , schema_path , bin_path , ["--raw-binary" ])
58+
59+ json_path = os .path .join (d , "schema.json" )
60+ with open (json_path , "rb" ) as output_file :
61+ return _json_to_dataclass (json .load (output_file ), VkGraph )
62+
63+
4364@dataclass
4465class VulkanDelegateHeader :
4566 # Defines the byte region that each component of the header corresponds to
0 commit comments