|
| 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 | + """ |
| 13 | + Tensor layout information for externally-serialized tensors. |
| 14 | + """ |
| 15 | + scalar_type: ScalarType |
| 16 | + dim_sizes: List[int] |
| 17 | + dim_order: List[bytes] |
| 18 | + |
| 19 | + |
| 20 | +@dataclass |
| 21 | +class SerializationInfo: |
| 22 | + # A sequence of tensor data buffers. |
| 23 | + tensor_buffers: Sequence[bytes] |
| 24 | + |
| 25 | + # A map from tensor name (fqn) to tensor index inside `tensor_buffers`. |
| 26 | + fqn_to_buffer_index: Dict[str, int] |
| 27 | + |
| 28 | + # A map from tensor name (fqn) to TensorLayout. |
| 29 | + fqn_to_tensor_layout: Dict[str, TensorLayout] |
| 30 | + |
| 31 | + |
| 32 | +class DataSerializer(ABC): |
| 33 | + """Serializes and deserializes FQN-tagged tensor data. |
| 34 | +
|
| 35 | + This base class enables serialization into different formats. See |
| 36 | + executorch/extension/flat_tensor/ for an example. |
| 37 | + """ |
| 38 | + |
| 39 | + @abstractmethod |
| 40 | + def __init__(self) -> None: |
| 41 | + """ |
| 42 | + This initializer may be overridden in derived classes to hold |
| 43 | + the data required for serialization, eg. configurations. |
| 44 | + """ |
| 45 | + pass |
| 46 | + |
| 47 | + @abstractmethod |
| 48 | + def serialize_tensors( |
| 49 | + self, |
| 50 | + serialization_info: SerializationInfo, |
| 51 | + ) -> Cord: |
| 52 | + """ |
| 53 | + Serializes a list of tensors emitted by ExecuTorch into a binary blob. |
| 54 | +
|
| 55 | + Args: |
| 56 | + serialization_info: the tensor buffers and tensor layout |
| 57 | + information required for serialization. |
| 58 | +
|
| 59 | + Returns: |
| 60 | + A binary blob that contains the serialized data. |
| 61 | + """ |
| 62 | + raise NotImplementedError("serialize_data") |
| 63 | + |
| 64 | + @abstractmethod |
| 65 | + def deserialize_tensors(self, blob: Cord) -> SerializationInfo: |
| 66 | + """ |
| 67 | + Deserializes a blob into a list of tensors. Reverses the effect of |
| 68 | + serialize_tensors. |
| 69 | +
|
| 70 | + Args: |
| 71 | + blob: A binary blob that contains the serialized data. |
| 72 | +
|
| 73 | + Returns: |
| 74 | + SerializationInfo: tensor buffers and tensor layout information |
| 75 | + deserialized from `blob`. |
| 76 | + """ |
| 77 | + raise NotImplementedError("deserialize_data") |
0 commit comments