|
| 1 | +from abc import ABC, abstractmethod |
| 2 | +from dataclasses import dataclass |
| 3 | +from typing import Dict, List, Sequence |
| 4 | + |
| 5 | +from executorch.exir._serialize._cord import Cord |
| 6 | + |
| 7 | +from executorch.exir.schema import ScalarType |
| 8 | + |
| 9 | + |
| 10 | +@dataclass |
| 11 | +class TensorLayout: |
| 12 | + """Tensor layout information for externally-serialized tensors. |
| 13 | +
|
| 14 | + Attributes: |
| 15 | + scalar_type: type of the elements in the tensor. |
| 16 | + sizes: size of each dim in the tensor. |
| 17 | + dim_order: specifies the order the dimensions are laid out in memory, |
| 18 | + from outer to inner. |
| 19 | + """ |
| 20 | + |
| 21 | + scalar_type: ScalarType |
| 22 | + sizes: List[int] |
| 23 | + dim_order: List[int] |
| 24 | + |
| 25 | + |
| 26 | +@dataclass |
| 27 | +class TensorEntry: |
| 28 | + """Represents a single tensor in `DataPayload`, specifying its location |
| 29 | + and metadata. |
| 30 | +
|
| 31 | + Attributes: |
| 32 | + buffer_index: The index inside `DataPayload.buffers` that this |
| 33 | + TensorEntry refers to. |
| 34 | + layout: Metadata about the tensor. |
| 35 | + """ |
| 36 | + |
| 37 | + buffer_index: int |
| 38 | + layout: TensorLayout |
| 39 | + |
| 40 | + |
| 41 | +@dataclass |
| 42 | +class DataPayload: |
| 43 | + """Contains the data and metadata required for serialization. |
| 44 | +
|
| 45 | + Having an index-based arrangement instead of embedding the buffers in |
| 46 | + TensorEntry allows the caller to deduplicate buffers and point multiple |
| 47 | + fully qualified names (FQNs) to the same entry. |
| 48 | +
|
| 49 | + Attributes: |
| 50 | + buffers: a sequence of tensor buffers. |
| 51 | + fqn_to_tensor: a map from fully qualified names to serializable tensors. |
| 52 | + """ |
| 53 | + |
| 54 | + buffers: Sequence[bytes] |
| 55 | + fqn_to_tensor: Dict[str, TensorEntry] |
| 56 | + |
| 57 | + |
| 58 | +class DataSerializer(ABC): |
| 59 | + """Serializes and deserializes FQN-tagged tensor data. |
| 60 | +
|
| 61 | + This base class enables serialization into different formats. See |
| 62 | + executorch/extension/flat_tensor/ for an example. |
| 63 | + """ |
| 64 | + |
| 65 | + @abstractmethod |
| 66 | + def serialize( |
| 67 | + self, |
| 68 | + data: DataPayload, |
| 69 | + ) -> Cord: |
| 70 | + """ |
| 71 | + Serializes a list of tensors emitted by ExecuTorch into a binary blob. |
| 72 | +
|
| 73 | + Args: |
| 74 | + data: the tensor buffers and tensor layout information required for |
| 75 | + serialization. |
| 76 | +
|
| 77 | + Returns: |
| 78 | + A binary blob that contains the serialized data. |
| 79 | + """ |
| 80 | + raise NotImplementedError("serialize_data") |
| 81 | + |
| 82 | + @abstractmethod |
| 83 | + def deserialize(self, blob: Cord) -> DataPayload: |
| 84 | + """ |
| 85 | + Deserializes a blob into a list of tensors. Reverses the effect of |
| 86 | + serialize. |
| 87 | +
|
| 88 | + Args: |
| 89 | + blob: A binary blob that contains the serialized data. |
| 90 | +
|
| 91 | + Returns: |
| 92 | + DataPayload: tensor buffers and tensor layout information |
| 93 | + deserialized from `blob`. |
| 94 | + """ |
| 95 | + raise NotImplementedError("deserialize_data") |
0 commit comments