Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions backends/vulkan/serialization/vulkan_graph_serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
VkBytes,
VkGraph,
)
from executorch.exir._serialize._dataclass import _DataclassEncoder
from executorch.exir._serialize._dataclass import _DataclassEncoder, _json_to_dataclass

from executorch.exir._serialize._flatbuffer import _flatc_compile
from executorch.exir._serialize._flatbuffer import _flatc_compile, _flatc_decompile


def convert_to_flatbuffer(vk_graph: VkGraph) -> bytes:
Expand All @@ -40,6 +40,23 @@ def convert_to_flatbuffer(vk_graph: VkGraph) -> bytes:
return output_file.read()


def flatbuffer_to_vk_graph(flatbuffers: bytes) -> VkGraph:
with tempfile.TemporaryDirectory() as d:
schema_path = os.path.join(d, "schema.fbs")
with open(schema_path, "wb") as schema_file:
schema_file.write(pkg_resources.resource_string(__name__, "schema.fbs"))

bin_path = os.path.join(d, "schema.bin")
with open(bin_path, "wb") as bin_file:
bin_file.write(flatbuffers)

_flatc_decompile(d, schema_path, bin_path, ["--raw-binary"])

json_path = os.path.join(d, "schema.json")
with open(json_path, "rb") as output_file:
return _json_to_dataclass(json.load(output_file), VkGraph)


@dataclass
class VulkanDelegateHeader:
# Defines the byte region that each component of the header corresponds to
Expand Down
40 changes: 39 additions & 1 deletion backends/vulkan/test/test_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,17 @@

import torch

from executorch.backends.vulkan.serialization.vulkan_graph_schema import VkGraph
from executorch.backends.vulkan.serialization.vulkan_graph_schema import (
IntList,
OperatorCall,
String,
VkGraph,
VkValue,
)

from executorch.backends.vulkan.serialization.vulkan_graph_serialize import (
convert_to_flatbuffer,
flatbuffer_to_vk_graph,
serialize_vulkan_graph,
VulkanDelegateHeader,
)
Expand Down Expand Up @@ -93,3 +101,33 @@ def test_serialize_vulkan_binary(self):

tensor_bytes = bytes(array)
self.assertEqual(constant_data_bytes, tensor_bytes)

def test_serialize_deserialize_vkgraph(self):
in_vk_graph = VkGraph(
version="1",
chain=[
OperatorCall(node_id=1, name="foo", args=[1, 2, 3]),
OperatorCall(node_id=2, name="bar", args=[]),
],
values=[
VkValue(
value=String(
string_val="abc",
),
),
VkValue(
value=IntList(
items=[-1, -4, 2],
),
),
],
input_ids=[],
output_ids=[],
constants=[],
shaders=[],
)

bs = convert_to_flatbuffer(in_vk_graph)
out_vk_graph = flatbuffer_to_vk_graph(bs)

self.assertEqual(in_vk_graph, out_vk_graph)
Loading