Skip to content

Commit e6aa3fa

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

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

exir/_serialize/data_serializer.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
from abc import ABC, abstractmethod
2+
from typing import Dict, List, Tuple, Union
3+
4+
from executorch.exir._serialize._cord import Cord
5+
6+
from executorch.exir.schema import Tensor
7+
8+
9+
# Abstract base class that data serializers should adhere to.
10+
class DataSerializer(ABC):
11+
@abstractmethod
12+
def __init__(self) -> None:
13+
"""
14+
This initializer may be overridden in derived classes to hold
15+
the data required for serialization, eg. configurations.
16+
"""
17+
pass
18+
19+
@abstractmethod
20+
def serialize_tensors(
21+
self,
22+
tensor_buffer: List[bytes],
23+
tensor_map: Dict[str, int],
24+
tensor_metadata: Dict[str, Tensor],
25+
) -> Union[Cord, bytes, bytearray]:
26+
"""
27+
Serializes a list of tensor metadata and tensors emitted by ExecuTorch
28+
into a binary blob.
29+
30+
Args:
31+
tensor_buffer: A list of deduplicated tensor data.
32+
tensor_map: A map from tensor name (fqn) to tensor index inside 'tensor_buffer'.
33+
tensor_metadata: A map from tensor name (fqn) to tensor metadata.
34+
35+
Returns:
36+
A binary blob that contains the serialized data.
37+
"""
38+
raise NotImplementedError("serialize_data")
39+
40+
@abstractmethod
41+
def deserialize_tensors(
42+
self, blob: Union[Cord, bytes, bytearray]
43+
) -> Tuple[List[bytes], Dict[str, int], Dict[str, Tensor]]:
44+
"""
45+
Deserializes a blob into a list of tensor metadata and tensors. Reverses the effect of serialize_tensors.
46+
47+
Args:
48+
blob: A binary blob that contains the serialized data.
49+
50+
Returns:
51+
A tuple of (tensor_buffer, tensor_map, tensor_metadata).
52+
"""
53+
raise NotImplementedError("deserialize_data")

0 commit comments

Comments
 (0)