-
Notifications
You must be signed in to change notification settings - Fork 413
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[executorch][serialization] Data serialization interface (#7487)
* [executorch][serialization] Data serialization interface Pull Request resolved: #7194 Introduce data serialization interface. ghstack-source-id: 260014193 @exported-using-ghexport Differential Revision: [D65947145](https://our.internmc.facebook.com/intern/diff/D65947145/) * [executorch][serialization] Refactor flatbuffer utils into separate file (#7488) Pull Request resolved: #7254 Todo: let xnnpack and vulkan serialization use these utils instead of redefining the same functions. For usage in extension/flat_tensor/serialize. ghstack-source-id: 260036856 @exported-using-ghexport Differential Revision: [D66854756](https://our.internmc.facebook.com/intern/diff/D66854756/) Co-authored-by: lucylq <lfq@meta.com> --------- Co-authored-by: lucylq <lfq@meta.com>
- Loading branch information
1 parent
fab1463
commit 443ba3b
Showing
4 changed files
with
140 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
from abc import ABC, abstractmethod | ||
from dataclasses import dataclass | ||
from typing import Dict, List, Sequence | ||
|
||
from executorch.exir._serialize._cord import Cord | ||
|
||
from executorch.exir.schema import ScalarType | ||
|
||
|
||
@dataclass | ||
class TensorLayout: | ||
"""Tensor layout information for externally-serialized tensors. | ||
Attributes: | ||
scalar_type: type of the elements in the tensor. | ||
sizes: size of each dim in the tensor. | ||
dim_order: specifies the order the dimensions are laid out in memory, | ||
from outer to inner. | ||
""" | ||
|
||
scalar_type: ScalarType | ||
sizes: List[int] | ||
dim_order: List[int] | ||
|
||
|
||
@dataclass | ||
class TensorEntry: | ||
"""Represents a single tensor in `DataPayload`, specifying its location | ||
and metadata. | ||
Attributes: | ||
buffer_index: The index inside `DataPayload.buffers` that this | ||
TensorEntry refers to. | ||
layout: Metadata about the tensor. | ||
""" | ||
|
||
buffer_index: int | ||
layout: TensorLayout | ||
|
||
|
||
@dataclass | ||
class DataPayload: | ||
"""Contains the data and metadata required for serialization. | ||
Having an index-based arrangement instead of embedding the buffers in | ||
TensorEntry allows the caller to deduplicate buffers and point multiple | ||
fully qualified names (FQNs) to the same entry. | ||
Attributes: | ||
buffers: a sequence of tensor buffers. | ||
fqn_to_tensor: a map from fully qualified names to serializable tensors. | ||
""" | ||
|
||
buffers: Sequence[bytes] | ||
fqn_to_tensor: Dict[str, TensorEntry] | ||
|
||
|
||
class DataSerializer(ABC): | ||
"""Serializes and deserializes FQN-tagged tensor data. | ||
This base class enables serialization into different formats. See | ||
executorch/extension/flat_tensor/ for an example. | ||
""" | ||
|
||
@abstractmethod | ||
def serialize( | ||
self, | ||
data: DataPayload, | ||
) -> Cord: | ||
""" | ||
Serializes a list of tensors emitted by ExecuTorch into a binary blob. | ||
Args: | ||
data: the tensor buffers and tensor layout information required for | ||
serialization. | ||
Returns: | ||
A binary blob that contains the serialized data. | ||
""" | ||
raise NotImplementedError("serialize_data") | ||
|
||
@abstractmethod | ||
def deserialize(self, blob: Cord) -> DataPayload: | ||
""" | ||
Deserializes a blob into a list of tensors. Reverses the effect of | ||
serialize. | ||
Args: | ||
blob: A binary blob that contains the serialized data. | ||
Returns: | ||
DataPayload: tensor buffers and tensor layout information | ||
deserialized from `blob`. | ||
""" | ||
raise NotImplementedError("deserialize_data") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary. | ||
|
||
# pyre-strict | ||
|
||
|
||
def pad_to(data: bytes, length: int) -> bytes: | ||
"""Returns the input followed by enough zero bytes to become the requested length. | ||
Args: | ||
data: The data to pad. | ||
length: The length of the returned data. | ||
Returns: | ||
The padded data. | ||
Raises: | ||
ValueError: If the requested length is less than the input length. | ||
""" | ||
if length < len(data): | ||
raise ValueError(f"Data length {len(data)} > padded length {length}") | ||
if length > len(data): | ||
data = data + b"\x00" * (length - len(data)) | ||
assert len(data) == length | ||
return data | ||
|
||
|
||
def padding_required(offset: int, alignment: int) -> int: | ||
"""Returns the padding required to align `offset` to `alignment`.""" | ||
remainder: int = offset % alignment | ||
if remainder != 0: | ||
return alignment - remainder | ||
return 0 | ||
|
||
|
||
def aligned_size(input_size: int, alignment: int) -> int: | ||
"""Returns input_size padded up to the next whole multiple of alignment.""" | ||
return input_size + padding_required(input_size, alignment) |