Skip to content

Commit c1ff368

Browse files
committed
[executorch][serialization] Data serialization interface
Pull Request resolved: #7194 Introduce data serialization interface. ghstack-source-id: 257299296 @exported-using-ghexport Differential Revision: [D65947145](https://our.internmc.facebook.com/intern/diff/D65947145/)
1 parent f3cebb4 commit c1ff368

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

exir/_serialize/data_serializer.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
from abc import ABC, abstractmethod
2+
from dataclasses import dataclass
3+
from typing import Dict, Sequence
4+
5+
from executorch.exir._serialize._cord import Cord
6+
7+
from executorch.exir.schema import Tensor
8+
9+
10+
@dataclass
11+
class SerializationInfo:
12+
# A sequence of deduplicated tensor data.
13+
tensor_buffers: Sequence[bytes]
14+
15+
# A map from tensor name (fqn) to tensor index inside `tensor_buffers`.
16+
fqn_to_buffer_index: Dict[str, int]
17+
18+
# A map from tensor name (fqn) to tensor metadata. The `Tensor` type contains information related to how tensor data is stored (eg. `data_buffer_idx`, `allocation_info`, `extra_tensor_info`). These are not used; only the tensor metadata (eg. `scalar_type`, `dim_order`, `sizes`, etc.) are used.
19+
fqn_to_metadata: Dict[str, Tensor]
20+
21+
22+
"""
23+
Abstract base class for custom serialization and deserialization of tensor data from ExecuTorch.
24+
Tensor data can be serialized into different formats. See executorch/extension/flat_tensor/ for an example.
25+
"""
26+
27+
28+
class DataSerializer(ABC):
29+
@abstractmethod
30+
def __init__(self) -> None:
31+
"""
32+
This initializer may be overridden in derived classes to hold
33+
the data required for serialization, eg. configurations.
34+
"""
35+
pass
36+
37+
@abstractmethod
38+
def serialize_tensors(
39+
self,
40+
serialization_info: SerializationInfo,
41+
) -> Cord:
42+
"""
43+
Serializes a list of tensor metadata and tensors emitted by ExecuTorch
44+
into a binary blob.
45+
46+
Args:
47+
serialization_info: the tensor buffers and metadata required for serialization.
48+
49+
Returns:
50+
A binary blob that contains the serialized data.
51+
"""
52+
raise NotImplementedError("serialize_data")
53+
54+
@abstractmethod
55+
def deserialize_tensors(self, blob: Cord) -> SerializationInfo:
56+
"""
57+
Deserializes a blob into a list of tensor metadata and tensors. Reverses the effect of serialize_tensors, which serializes tensor metadata and tensors emitted by ExecuTorch into a binary blob.
58+
59+
Args:
60+
blob: A binary blob that contains the serialized data.
61+
62+
Returns:
63+
SerializationInfo: tensors and metadata that can be used to serialize data.
64+
"""
65+
raise NotImplementedError("deserialize_data")

0 commit comments

Comments
 (0)