Skip to content

Commit 16e101e

Browse files
committed
feat: add compression metadata flatbuffer schema and tests
Add a flatbuffer schema for describing compressed models. Flatbuffers with this schema are to be used as the value in a .tflite model flatbuffer metadata field, and contain the extra information necessary to describe a compressed model. Include tests to ensure basic functionality and demonstrate integration with C++, Python, and Bazel. BUG=#2636
1 parent 8ff28fa commit 16e101e

File tree

8 files changed

+782
-0
lines changed

8 files changed

+782
-0
lines changed
+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
load(
2+
"//tensorflow/lite/micro:build_def.bzl",
3+
"tflm_cc_library",
4+
"tflm_cc_test",
5+
)
6+
load(
7+
"@flatbuffers//:build_defs.bzl",
8+
"flatbuffer_cc_library",
9+
"flatbuffer_py_library",
10+
)
11+
load("@rules_python//python:defs.bzl", "py_test")
12+
load("@tflm_pip_deps//:requirements.bzl", "requirement")
13+
14+
package(
15+
default_visibility = [
16+
"//visibility:public",
17+
],
18+
)
19+
20+
flatbuffer_cc_library(
21+
# Generates the header-only library "metadata_generated.h", used to read
22+
# the metadata flatbuffer.
23+
name = "metadata_cc",
24+
srcs = ["metadata.fbs"],
25+
)
26+
27+
tflm_cc_library(
28+
# The header-only library generated by flatc in ":metadata_cc" is saved to
29+
# the source tree and comitted to git as "metadata_saved.h", which is used
30+
# by code which builds via the Make build system, which has no means of
31+
# generating the header on the fly. Code which builds via both bazel and
32+
# Make should #include the saved header and use this target in its bazel
33+
# BUILD deps. Code built exclusively via bazel would typically depend
34+
# directly on ":metadata_cc", which would generate a header from the schema
35+
# on the fly, during the build.
36+
#
37+
# When the schema definition "metadata.fbs" is changed, this saved header
38+
# should be updated by running the script "./metadata_saved_update.sh",
39+
# outside of bazel (because bazel cannot modify the source tree). The
40+
# script regenerates the header from the schema and copies it to the source
41+
# tree as "metadata_saved.h".
42+
#
43+
# Comitting the generated file risks inconsistency between the schema and
44+
# the saved header, so consistency ensured by the unit test
45+
# ":metadata_saved_test".
46+
#
47+
name = "metadata_saved",
48+
hdrs = ["metadata_saved.h"],
49+
)
50+
51+
sh_test(
52+
# Ensures consistency bewteen the schema and the saved generated header.
53+
# Fails if they mismatch, in which case, ./metadata_saved_update.sh should
54+
# be run. See :metadata_saved above.
55+
name = "metadata_saved_test",
56+
size = "small",
57+
srcs = ["metadata_saved_test.sh"],
58+
args = [
59+
"$(location metadata_saved.h)",
60+
"$(location :metadata_cc_srcs)",
61+
],
62+
data = [
63+
"metadata_saved.h",
64+
":metadata_cc_srcs",
65+
],
66+
)
67+
68+
tflm_cc_test(
69+
name = "metadata_test_cc",
70+
size = "small",
71+
srcs = ["metadata_test.cc"],
72+
deps = [
73+
":metadata_saved",
74+
"//tensorflow/lite/micro:hexdump",
75+
"//tensorflow/lite/micro/testing:micro_test",
76+
"@flatbuffers//:runtime_cc",
77+
],
78+
)
79+
80+
flatbuffer_py_library(
81+
# Generates the Python module "metadata_py_generated", used to read the
82+
# metadata flatbuffer.
83+
name = "metadata_py",
84+
srcs = ["metadata.fbs"],
85+
)
86+
87+
py_test(
88+
name = "metadata_test_py",
89+
size = "small",
90+
srcs = ["metadata_test.py"],
91+
main = "metadata_test.py",
92+
deps = [
93+
"metadata_py",
94+
"@flatbuffers//:runtime_py",
95+
requirement("hexdump"),
96+
],
97+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2024 The TensorFlow Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
namespace tflite.micro.compression;
16+
17+
table Metadata {
18+
// Compression data root, to be used in a tflite.Model.metadata field with
19+
// the key "COMPRESSION_METADATA".
20+
21+
schema_version:int = 1;
22+
// ^ Incremented whenever there are backward-incompatible changes. Code
23+
// should accept models with versions less than or equal the version
24+
// for which the code is built. I.e., code should accept older models,
25+
// but not necessarily newer ones.
26+
27+
subgraphs:[Subgraph];
28+
// ^ Compression data indexed by subgraph index.
29+
}
30+
31+
table Subgraph {
32+
// Per-subgraph compression metadata.
33+
34+
lut_tensors:[LutTensor];
35+
// ^ A list of tensors which are compressed using the
36+
// (L)ook-(U)p-(T)able method. The indices of this vector are not
37+
// significant.
38+
}
39+
40+
table LutTensor {
41+
// Look-Up-Table Tensor: a tensor representation where elements are
42+
// compressed into indices into a table of values. The indices are unsigned
43+
// integers, index_bitwidth-wide, in big-endian bit order, packed into the
44+
// buffer identified by the corresponding tflite.Tensor's buffer field. The
45+
// values are located in a newly-created buffer, encoded according to the
46+
// tflite.Tensor.type. Tensors with multiple channels have distinct value
47+
// tables for each channel, typically along their quantization axis,
48+
// concatenated one after another. An element's index must be looked up in
49+
// the value table corresponding to its channel.
50+
51+
tensor:int; // index of the corresponding tflite.Tensor
52+
value_buffer:uint; // index of the buffer containing LUT values
53+
index_bitwidth:uint8; // bit-width of LUT indexes
54+
}
55+
56+
root_type Metadata;

0 commit comments

Comments
 (0)