Skip to content

Commit

Permalink
update to latest metadata schema
Browse files Browse the repository at this point in the history
  • Loading branch information
ddavis-2015 committed Aug 10, 2024
1 parent 26a153e commit 28e4f4f
Show file tree
Hide file tree
Showing 12 changed files with 319 additions and 825 deletions.
3 changes: 2 additions & 1 deletion tensorflow/lite/micro/compression.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ namespace tflite {
// Compressed tensors
//

static constexpr const char* kCompressionMetadataString = "TFLM_COMPRESSION";
static constexpr const char* kCompressionMetadataString =
"COMPRESSION_METADATA";

enum class CompressionScheme : uint8_t {
kBinQuant,
Expand Down
52 changes: 0 additions & 52 deletions tensorflow/lite/micro/compression/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ flatbuffer_cc_library(
srcs = ["metadata.fbs"],
)

flatbuffer_py_library(
name = "original_flatbuffer_py",
srcs = ["original.fbs"],
)

flatbuffer_py_library(
name = "metadata_flatbuffer_py",
srcs = ["metadata.fbs"],
Expand All @@ -34,33 +29,6 @@ cc_test(
size = "small",
)

py_binary(
name = "compress",
srcs = ["compress.py"],
deps = [
"@absl_py//absl:app",
"@absl_py//absl/flags",
"@absl_py//absl/logging",
"@flatbuffers//:runtime_py",
"metadata_flatbuffer_py",
"//tensorflow/lite/python:schema_py",
requirement("bitarray"),
requirement("numpy"),
requirement("scikit-learn"),
],
)

py_binary(
name = "view",
srcs = [
"view.py",
],
deps = [
"metadata_flatbuffer_py",
"//tensorflow/lite/python:schema_py",
],
)

py_test(
name = "metadata_test_py",
main = "metadata_test.py",
Expand All @@ -72,23 +40,3 @@ py_test(
],
size = "small",
)

py_test(
name = "original_test_py",
main = "original_test.py",
srcs = ["original_test.py"],
deps = [
"original_flatbuffer_py",
"@flatbuffers//:runtime_py",
requirement("hexdump"),
],
size = "small",
)

genrule(
name = "hello_world_int8.compressed",
srcs = ["//tensorflow/lite/micro/examples/hello_world/models:hello_world_int8.tflite"],
outs = ["hello_world_int8.compressed.tflite"],
cmd = "$(location :compress) --input_model_path $< --output_model_path $@",
tools = [":compress"],
)
244 changes: 0 additions & 244 deletions tensorflow/lite/micro/compression/compress.py

This file was deleted.

43 changes: 27 additions & 16 deletions tensorflow/lite/micro/compression/metadata.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,38 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// Flatbuffer schema describing a TFLM compressed model. Use as the value for
// the key "TFLM_COMPRESSION" in the metadata table in a .tflite flatbuffer.

namespace tflite.micro.compression;

table Metadata {
lut_tensors:[LutTensor]; // list of tensors that are compressed by LUT
// Compression data root, to be used in a tflite.Model.metadata field with
// the key "COMPRESSION_METADATA".

subgraphs:[Subgraph]; // compression data indexed by subgraph index
}

table Subgraph {
// Per-subgraph compression metadata.

lut_tensors:[LutTensor];
// ^ A list of tensors which are compressed using the
// (L)ook-(U)p-(T)able method. The indices of this vector are not
// significant.
}

struct LutTensor {
subgraph:uint16; // the index of the subgraph
tensor:uint16; // the index of the tensor in its subgraph
index_bitwidth:uint8; // the bit-width of LUT indexes
index_buffer:uint16; // the index of the buffer containing LUT indexes
value_buffer:uint16; // the index of the buffer containing LUT values
table LutTensor {
// Look-Up-Table Tensor: a tensor representation where elements are
// compressed into indices into a table of values. The indices are unsigned
// integers, index_bitwidth-wide, in big-endian bit order, packed into the
// buffer identified by the corresponding tflite.Tensor's buffer field. The
// values are located in a newly-created buffer, encoded according to the
// tflite.Tensor.type. Tensors with multiple channels have distinct values
// tables for each channel, concatenated one after another in the buffer.
// An element's LUT index must be looked up in the value table for its
// channel.

tensor:int; // index of the corresponding tflite.Tensor
value_buffer:uint; // index of the buffer containing LUT values
index_bitwidth:uint8; // bit-width of LUT indexes
}
// Look-Up-Table tensors are encoded in two buffers: an index buffer and a
// value buffer. The indexes are unsigned integers packed into the index buffer
// in bitwidth-wide bit fields with a big-endian bit order. The data in the
// value buffer is encoded as usual according to the type of the tensor.
// Tensors with multiple channels have distinct values tables for each channel,
// concatinated into one value buffer. (Will elaborate this comment.)

root_type Metadata;
Loading

0 comments on commit 28e4f4f

Please sign in to comment.