Skip to content

Commit 19268de

Browse files
authored
Introduce data schema to store raw tensors (#6919)
[executorch][serialization] Introduce data schema to store raw tensors Pull Request resolved: #6540 @imported-using-ghimport Differential Revision: [D65156641](https://our.internmc.facebook.com/intern/diff/D65156641/) ghstack-source-id: 253910001
1 parent 793a988 commit 19268de

File tree

5 files changed

+158
-2
lines changed

5 files changed

+158
-2
lines changed

extension/flat_tensor/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
## FlatTensor
2+
3+
> [!IMPORTANT]
4+
> FlatTensor is still under development, and not ready to use.
5+
6+
FlatTensor is a flatbuffer-based format for storing and loading tensors. The format provides a way to store tensors keyed by string.

extension/flat_tensor/flat_tensor.fbs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Schema for flatbuffer-serialized tensors.
2+
3+
include "scalar_type.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+
table FlatTensor {
60+
// Schema version.
61+
version: uint32;
62+
63+
// Alignment for each tensor in bytes. Offsets of the tensor provided
64+
// in TensorMetadata.offset are aligned to tensor_alignment.
65+
tensor_alignment: uint32;
66+
67+
// Tensor information, including metadata and offsets to the raw tensor data.
68+
tensors: [TensorMetadata];
69+
70+
// List of data segments that follow the FlatTensor data in this file, sorted by
71+
// offset. Elements in this schema can refer to these segments by index.
72+
segments: [DataSegment];
73+
}
74+
75+
root_type FlatTensor;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
# All rights reserved.
3+
#
4+
# This source code is licensed under the BSD-style license found in the
5+
# LICENSE file in the root directory of this source tree.
6+
7+
8+
from dataclasses import dataclass
9+
from typing import List
10+
11+
from executorch.exir.scalar_type import ScalarType
12+
13+
# Note: check executorch/extension/data_format/flat_tensor.fbs for explanations of these fields.
14+
15+
16+
@dataclass
17+
class TensorMetadata:
18+
fully_qualified_name: str
19+
scalar_type: ScalarType
20+
dim_sizes: List[int]
21+
dim_order: List[bytes]
22+
23+
segment_index: int
24+
offset: int
25+
26+
27+
@dataclass
28+
class DataSegment:
29+
offset: int
30+
size: int
31+
32+
33+
@dataclass
34+
class FlatTensor:
35+
version: int
36+
tensor_alignment: int
37+
tensors: List[TensorMetadata]
38+
segments: List[DataSegment]

extension/flat_tensor/scalar_type.fbs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (c) Meta Platforms, Inc. and affiliates.
2+
3+
//
4+
// See executorch/schema/README.md before modifying this file.
5+
//
6+
7+
// TODO(T207893511): sync scalar_type.fbs copies across ExecuTorch repo.
8+
namespace executorch_flatbuffer;
9+
10+
// The scalar data type.
11+
// Must match executorch/runtime/core/portable_type/tensor_impl.h
12+
enum ScalarType : byte {
13+
BYTE = 0,
14+
CHAR = 1,
15+
SHORT = 2,
16+
INT = 3,
17+
LONG = 4,
18+
HALF = 5,
19+
FLOAT = 6,
20+
DOUBLE = 7,
21+
BOOL = 11,
22+
// TODO(jakeszwe): Verify these are unused and then remove support
23+
QINT8 = 12,
24+
QUINT8 = 13,
25+
QINT32 = 14,
26+
QUINT4X2 = 16,
27+
QUINT2X4 = 17,
28+
BITS16 = 22,
29+
// Types currently not implemented.
30+
// COMPLEXHALF = 8,
31+
// COMPLEXFLOAT = 9,
32+
// COMPLEXDOUBLE = 10,
33+
// BFLOAT16 = 15,
34+
// BITS1x8 = 18,
35+
// BITS2x4 = 19,
36+
// BITS4x2 = 20,
37+
// BITS8 = 21,
38+
}

schema/README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ The `schema.fbs` file in this directory describes the
22
[Flatbuffers](https://google.github.io/flatbuffers/) schema used to serialize
33
ExecuTorch programs.
44

5-
The `scalar_type.fbs` file contains schema for scalar types, used in both
6-
`schema.fbs` and `bundled_program_schema.fbs`.
5+
The `scalar_type.fbs` file contains schema for scalar types.
76

87
## Rules to ensure forward/backward compatibility
98

0 commit comments

Comments
 (0)