|
| 1 | +// Schema for flatbuffer-serialized tensors. |
| 2 | + |
| 3 | +include "common.fbs"; |
| 4 | +namespace flat_tensor; |
| 5 | + |
| 6 | +// Update after BC breaking changes. |
| 7 | +file_identifier "FT01"; |
| 8 | +file_extension "ptd"; |
| 9 | + |
| 10 | +table TensorMetadata { |
| 11 | + // The unique id used to connect the data and program. |
| 12 | + fully_qualified_name: string; |
| 13 | + scalar_type: executorch_flatbuffer.ScalarType; |
| 14 | + |
| 15 | + // Size of each dimension. |
| 16 | + dim_sizes: [int32]; |
| 17 | + |
| 18 | + // Specifies in what order the dimensions are laid out in memory (from outer |
| 19 | + // to inner). |
| 20 | + // |
| 21 | + // For example, given a rank 3 Tensor of size (3, 5, 2). If we name |
| 22 | + // dimensions: [row, column, batch], then a dim_order of: |
| 23 | + // - (2, 0, 1) represents a [batch, row, column] ordering where "column" is |
| 24 | + // the innermost dimension, then comes "row", and the outermost dimension is |
| 25 | + // "batch". |
| 26 | + // - (0, 2, 1) represents a [row, batch, column] ordering where "column" is |
| 27 | + // the innermost dimension, then comes "batch", and the outermost dimension |
| 28 | + // is "row". |
| 29 | + dim_order: [uint8]; |
| 30 | + |
| 31 | + // FlatTensor.segments index that the tensor data is stored in. |
| 32 | + segment_index: uint32; |
| 33 | + |
| 34 | + // Tensor offsets are relative to each TensorSegment. |
| 35 | + // To retrieve a given tensor: |
| 36 | + // 1. segment_base_offset: from the file header. |
| 37 | + // 2. segment_offset: segments[segment_index].offset |
| 38 | + // 3. tensor_offset: segments[segment_offset].tensor_metadata[j].offset |
| 39 | + // Find the relevant index j by matching on tensor fqn. |
| 40 | + offset: uint64; |
| 41 | +} |
| 42 | + |
| 43 | +// Describes a contiguous piece of data that lives outside of the flatbuffer data, |
| 44 | +// typically appended afterwards in the file. |
| 45 | +// For .ptd files, the "extended header" in the file points to the segment base offset. |
| 46 | +table DataSegment { |
| 47 | + // Segment offsets are relative to the segment base offset provided in the |
| 48 | + // extended file header. Segments will typically be aligned in a way to make |
| 49 | + // it possible to use mmap() to load them. |
| 50 | + offset: uint64; |
| 51 | + |
| 52 | + // The size in bytes of valid data starting at the offset. The segment |
| 53 | + // data may be followed by padding before the segment that follows it, |
| 54 | + // to make it easier to use mmap(). |
| 55 | + size: uint64; |
| 56 | +} |
| 57 | + |
| 58 | +// FlatTensor is a flatbuffer-based format for storing and loading tensors. |
| 59 | +// FlatTensor interoperates with the ExecuTorch PTE file, providing the option |
| 60 | +// to store tensors outside of the PTE file. |
| 61 | +table FlatTensor { |
| 62 | + // Schema version. |
| 63 | + version: uint32; |
| 64 | + |
| 65 | + // Alignment for each tensor in bytes. Offsets of the tensor provided |
| 66 | + // in TensorMetadata.offset are aligned to tensor_alignment. |
| 67 | + tensor_alignment: uint32; |
| 68 | + |
| 69 | + // Tensor information, including metadata and offsets to the raw tensor data. |
| 70 | + tensors: [TensorMetadata]; |
| 71 | + |
| 72 | + // List of data segments that follow the FlatTensor data in this file, sorted by |
| 73 | + // offset. Elements in this schema can refer to these segments by index. |
| 74 | + segments: [DataSegment]; |
| 75 | +} |
| 76 | + |
| 77 | +root_type FlatTensor; |
0 commit comments