Skip to content

Commit 514cc22

Browse files
committed
Introduce data schema to store raw tensors
ghstack-source-id: 99f32da Pull Request resolved: #6540
1 parent 3b25b05 commit 514cc22

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed

exir/schema_data.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
# pyre-strict
8+
9+
from dataclasses import dataclass
10+
from typing import List
11+
12+
from executorch.exir.scalar_type import ScalarType
13+
14+
# Note: check executorch/schema/data.fbs for explanations of these fields.
15+
16+
17+
@dataclass
18+
class TensorMetadata:
19+
fully_qualified_name: str
20+
scalar_type: ScalarType
21+
dim_sizes: List[int]
22+
dim_order: List[bytes]
23+
storage_offset: int
24+
layout: int
25+
26+
offset: int
27+
size: int
28+
29+
30+
@dataclass
31+
class TensorSegment:
32+
segment_index: int
33+
tensor_metadata: List[TensorMetadata]
34+
35+
36+
@dataclass
37+
class DataSegment:
38+
offset: int
39+
size: int
40+
41+
42+
@dataclass
43+
class Data:
44+
version: int
45+
tensor_alignment: int
46+
tensor_segments: List[TensorSegment]
47+
data_segments: List[DataSegment]

schema/data.fbs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
include "scalar_type.fbs";
2+
namespace executorch_flatbuffer;
3+
4+
// Update after BC breaking changes.
5+
file_identifier "DT01";
6+
file_extension "data";
7+
8+
table TensorMetadata {
9+
// The unique id used to connect the data and program.
10+
fully_qualified_name:string;
11+
scalar_type:ScalarType;
12+
13+
// Size of each dimension.
14+
dim_sizes:[int];
15+
16+
// Specifies in what order the dimensions are laid out in memory (from outer
17+
// to inner).
18+
//
19+
// For example, given a rank 3 Tensor of size (3, 5, 2). If we name
20+
// dimensions: [row, column, batch], then a dim_order of:
21+
// - (2, 0, 1) represents a [batch, row, column] ordering where "column" is
22+
// the innermost dimension, then comes "row", and the outermost dimension is
23+
// "batch".
24+
// - (0, 2, 1) represents a [row, batch, column] ordering where "column" is
25+
// the innermost dimension, then comes "batch", and the outermost dimension
26+
// is "row".
27+
dim_order:[ubyte];
28+
29+
// Offset in scalar_type elements (e.g., multiples of 4 bytes for an int
30+
// scalar type) from the beginning of the tensor buffer to the beginning of
31+
// the actual data. Currently, the runtime only supports a value of zero.
32+
storage_offset:int;
33+
34+
// May not be needed.
35+
layout:byte;
36+
37+
// Tensor offsets are relative to each TensorSegment.
38+
// To retrieve a given tensor:
39+
// 1. segment_base_offset: from the file header.
40+
// 2. segment offset: segments[tensor_segments[i].segment_index].offset
41+
// This is likely to be 0 (all the tensors in one segment).
42+
// 3. tensor offset: tensor_segments[i].tensor_metadata[j].offset
43+
// May need to binary search over tensor_metadata to find the matching
44+
// tensor using fqn.
45+
offset: uint64;
46+
size: uint64;
47+
}
48+
49+
table TensorSegment {
50+
// Index of the segment in Data.segments.
51+
segment_index: uint;
52+
53+
// Tensor information, including the offset and size.
54+
tensor_metadata:[TensorMetadata];
55+
}
56+
57+
table DataSegment {
58+
// Segment offsets are relative to the segment base offset provided in
59+
// the extended file header. Segments will typically be aligned in a
60+
// way to make it possible to use mmap() to load them.
61+
offset: uint64;
62+
63+
// The size in bytes of valid data starting at the offset. The segment
64+
// data may be followed by padding before the segment that follows it,
65+
// to make it easier to use mmap().
66+
size: uint64;
67+
}
68+
69+
table Data {
70+
// Schema version.
71+
version:uint;
72+
73+
// Alignment for each tensor.
74+
tensor_alignment: uint32;
75+
76+
// Tensor information.
77+
tensor_segments:[TensorSegment];
78+
79+
// Data segments.
80+
segments:[DataSegment];
81+
}
82+
83+
root_type Data;

0 commit comments

Comments
 (0)