-
Notifications
You must be signed in to change notification settings - Fork 838
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
95da7a8
commit 9c8e33a
Showing
36 changed files
with
3,382 additions
and
383 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
load("@flatbuffers//:build_defs.bzl", "flatbuffer_cc_library", "flatbuffer_py_library") | ||
load("@rules_python//python:defs.bzl", "py_test") | ||
load("@tflm_pip_deps//:requirements.bzl", "requirement") | ||
|
||
package( | ||
default_visibility = [ | ||
"//visibility:public", | ||
], | ||
) | ||
|
||
flatbuffer_cc_library( | ||
name = "metadata_flatbuffer_cc", | ||
srcs = ["metadata.fbs"], | ||
) | ||
|
||
flatbuffer_py_library( | ||
name = "metadata_flatbuffer_py", | ||
srcs = ["metadata.fbs"], | ||
) | ||
|
||
cc_test( | ||
name = "metadata_test_cc", | ||
srcs = ["metadata_test.cc"], | ||
deps = [ | ||
"metadata_flatbuffer_cc", | ||
"//tensorflow/lite/micro:hexdump", | ||
"@flatbuffers//:runtime_cc", | ||
], | ||
size = "small", | ||
) | ||
|
||
py_test( | ||
name = "metadata_test_py", | ||
main = "metadata_test.py", | ||
srcs = ["metadata_test.py"], | ||
deps = [ | ||
"metadata_flatbuffer_py", | ||
"@flatbuffers//:runtime_py", | ||
requirement("hexdump"), | ||
], | ||
size = "small", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright 2024 The TensorFlow Authors. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
namespace tflite.micro.compression; | ||
|
||
table Metadata { | ||
// 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. | ||
} | ||
|
||
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 | ||
} | ||
|
||
root_type Metadata; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,228 @@ | ||
// automatically generated by the FlatBuffers compiler, do not modify | ||
|
||
|
||
#ifndef FLATBUFFERS_GENERATED_METADATA_TFLITE_MICRO_COMPRESSION_H_ | ||
#define FLATBUFFERS_GENERATED_METADATA_TFLITE_MICRO_COMPRESSION_H_ | ||
|
||
#include "flatbuffers/flatbuffers.h" | ||
|
||
// Ensure the included flatbuffers.h is the same version as when this file was | ||
// generated, otherwise it may not be compatible. | ||
static_assert(FLATBUFFERS_VERSION_MAJOR == 23 && | ||
FLATBUFFERS_VERSION_MINOR == 5 && | ||
FLATBUFFERS_VERSION_REVISION == 26, | ||
"Non-compatible flatbuffers version included"); | ||
|
||
namespace tflite { | ||
namespace micro { | ||
namespace compression { | ||
|
||
struct Metadata; | ||
struct MetadataBuilder; | ||
|
||
struct Subgraph; | ||
struct SubgraphBuilder; | ||
|
||
struct LutTensor; | ||
struct LutTensorBuilder; | ||
|
||
struct Metadata FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { | ||
typedef MetadataBuilder Builder; | ||
enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { | ||
VT_SUBGRAPHS = 4 | ||
}; | ||
const ::flatbuffers::Vector<::flatbuffers::Offset<tflite::micro::compression::Subgraph>> *subgraphs() const { | ||
return GetPointer<const ::flatbuffers::Vector<::flatbuffers::Offset<tflite::micro::compression::Subgraph>> *>(VT_SUBGRAPHS); | ||
} | ||
bool Verify(::flatbuffers::Verifier &verifier) const { | ||
return VerifyTableStart(verifier) && | ||
VerifyOffset(verifier, VT_SUBGRAPHS) && | ||
verifier.VerifyVector(subgraphs()) && | ||
verifier.VerifyVectorOfTables(subgraphs()) && | ||
verifier.EndTable(); | ||
} | ||
}; | ||
|
||
struct MetadataBuilder { | ||
typedef Metadata Table; | ||
::flatbuffers::FlatBufferBuilder &fbb_; | ||
::flatbuffers::uoffset_t start_; | ||
void add_subgraphs(::flatbuffers::Offset<::flatbuffers::Vector<::flatbuffers::Offset<tflite::micro::compression::Subgraph>>> subgraphs) { | ||
fbb_.AddOffset(Metadata::VT_SUBGRAPHS, subgraphs); | ||
} | ||
explicit MetadataBuilder(::flatbuffers::FlatBufferBuilder &_fbb) | ||
: fbb_(_fbb) { | ||
start_ = fbb_.StartTable(); | ||
} | ||
::flatbuffers::Offset<Metadata> Finish() { | ||
const auto end = fbb_.EndTable(start_); | ||
auto o = ::flatbuffers::Offset<Metadata>(end); | ||
return o; | ||
} | ||
}; | ||
|
||
inline ::flatbuffers::Offset<Metadata> CreateMetadata( | ||
::flatbuffers::FlatBufferBuilder &_fbb, | ||
::flatbuffers::Offset<::flatbuffers::Vector<::flatbuffers::Offset<tflite::micro::compression::Subgraph>>> subgraphs = 0) { | ||
MetadataBuilder builder_(_fbb); | ||
builder_.add_subgraphs(subgraphs); | ||
return builder_.Finish(); | ||
} | ||
|
||
inline ::flatbuffers::Offset<Metadata> CreateMetadataDirect( | ||
::flatbuffers::FlatBufferBuilder &_fbb, | ||
const std::vector<::flatbuffers::Offset<tflite::micro::compression::Subgraph>> *subgraphs = nullptr) { | ||
auto subgraphs__ = subgraphs ? _fbb.CreateVector<::flatbuffers::Offset<tflite::micro::compression::Subgraph>>(*subgraphs) : 0; | ||
return tflite::micro::compression::CreateMetadata( | ||
_fbb, | ||
subgraphs__); | ||
} | ||
|
||
struct Subgraph FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { | ||
typedef SubgraphBuilder Builder; | ||
enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { | ||
VT_LUT_TENSORS = 4 | ||
}; | ||
const ::flatbuffers::Vector<::flatbuffers::Offset<tflite::micro::compression::LutTensor>> *lut_tensors() const { | ||
return GetPointer<const ::flatbuffers::Vector<::flatbuffers::Offset<tflite::micro::compression::LutTensor>> *>(VT_LUT_TENSORS); | ||
} | ||
bool Verify(::flatbuffers::Verifier &verifier) const { | ||
return VerifyTableStart(verifier) && | ||
VerifyOffset(verifier, VT_LUT_TENSORS) && | ||
verifier.VerifyVector(lut_tensors()) && | ||
verifier.VerifyVectorOfTables(lut_tensors()) && | ||
verifier.EndTable(); | ||
} | ||
}; | ||
|
||
struct SubgraphBuilder { | ||
typedef Subgraph Table; | ||
::flatbuffers::FlatBufferBuilder &fbb_; | ||
::flatbuffers::uoffset_t start_; | ||
void add_lut_tensors(::flatbuffers::Offset<::flatbuffers::Vector<::flatbuffers::Offset<tflite::micro::compression::LutTensor>>> lut_tensors) { | ||
fbb_.AddOffset(Subgraph::VT_LUT_TENSORS, lut_tensors); | ||
} | ||
explicit SubgraphBuilder(::flatbuffers::FlatBufferBuilder &_fbb) | ||
: fbb_(_fbb) { | ||
start_ = fbb_.StartTable(); | ||
} | ||
::flatbuffers::Offset<Subgraph> Finish() { | ||
const auto end = fbb_.EndTable(start_); | ||
auto o = ::flatbuffers::Offset<Subgraph>(end); | ||
return o; | ||
} | ||
}; | ||
|
||
inline ::flatbuffers::Offset<Subgraph> CreateSubgraph( | ||
::flatbuffers::FlatBufferBuilder &_fbb, | ||
::flatbuffers::Offset<::flatbuffers::Vector<::flatbuffers::Offset<tflite::micro::compression::LutTensor>>> lut_tensors = 0) { | ||
SubgraphBuilder builder_(_fbb); | ||
builder_.add_lut_tensors(lut_tensors); | ||
return builder_.Finish(); | ||
} | ||
|
||
inline ::flatbuffers::Offset<Subgraph> CreateSubgraphDirect( | ||
::flatbuffers::FlatBufferBuilder &_fbb, | ||
const std::vector<::flatbuffers::Offset<tflite::micro::compression::LutTensor>> *lut_tensors = nullptr) { | ||
auto lut_tensors__ = lut_tensors ? _fbb.CreateVector<::flatbuffers::Offset<tflite::micro::compression::LutTensor>>(*lut_tensors) : 0; | ||
return tflite::micro::compression::CreateSubgraph( | ||
_fbb, | ||
lut_tensors__); | ||
} | ||
|
||
struct LutTensor FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { | ||
typedef LutTensorBuilder Builder; | ||
enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { | ||
VT_TENSOR = 4, | ||
VT_VALUE_BUFFER = 6, | ||
VT_INDEX_BITWIDTH = 8 | ||
}; | ||
int32_t tensor() const { | ||
return GetField<int32_t>(VT_TENSOR, 0); | ||
} | ||
uint32_t value_buffer() const { | ||
return GetField<uint32_t>(VT_VALUE_BUFFER, 0); | ||
} | ||
uint8_t index_bitwidth() const { | ||
return GetField<uint8_t>(VT_INDEX_BITWIDTH, 0); | ||
} | ||
bool Verify(::flatbuffers::Verifier &verifier) const { | ||
return VerifyTableStart(verifier) && | ||
VerifyField<int32_t>(verifier, VT_TENSOR, 4) && | ||
VerifyField<uint32_t>(verifier, VT_VALUE_BUFFER, 4) && | ||
VerifyField<uint8_t>(verifier, VT_INDEX_BITWIDTH, 1) && | ||
verifier.EndTable(); | ||
} | ||
}; | ||
|
||
struct LutTensorBuilder { | ||
typedef LutTensor Table; | ||
::flatbuffers::FlatBufferBuilder &fbb_; | ||
::flatbuffers::uoffset_t start_; | ||
void add_tensor(int32_t tensor) { | ||
fbb_.AddElement<int32_t>(LutTensor::VT_TENSOR, tensor, 0); | ||
} | ||
void add_value_buffer(uint32_t value_buffer) { | ||
fbb_.AddElement<uint32_t>(LutTensor::VT_VALUE_BUFFER, value_buffer, 0); | ||
} | ||
void add_index_bitwidth(uint8_t index_bitwidth) { | ||
fbb_.AddElement<uint8_t>(LutTensor::VT_INDEX_BITWIDTH, index_bitwidth, 0); | ||
} | ||
explicit LutTensorBuilder(::flatbuffers::FlatBufferBuilder &_fbb) | ||
: fbb_(_fbb) { | ||
start_ = fbb_.StartTable(); | ||
} | ||
::flatbuffers::Offset<LutTensor> Finish() { | ||
const auto end = fbb_.EndTable(start_); | ||
auto o = ::flatbuffers::Offset<LutTensor>(end); | ||
return o; | ||
} | ||
}; | ||
|
||
inline ::flatbuffers::Offset<LutTensor> CreateLutTensor( | ||
::flatbuffers::FlatBufferBuilder &_fbb, | ||
int32_t tensor = 0, | ||
uint32_t value_buffer = 0, | ||
uint8_t index_bitwidth = 0) { | ||
LutTensorBuilder builder_(_fbb); | ||
builder_.add_value_buffer(value_buffer); | ||
builder_.add_tensor(tensor); | ||
builder_.add_index_bitwidth(index_bitwidth); | ||
return builder_.Finish(); | ||
} | ||
|
||
inline const tflite::micro::compression::Metadata *GetMetadata(const void *buf) { | ||
return ::flatbuffers::GetRoot<tflite::micro::compression::Metadata>(buf); | ||
} | ||
|
||
inline const tflite::micro::compression::Metadata *GetSizePrefixedMetadata(const void *buf) { | ||
return ::flatbuffers::GetSizePrefixedRoot<tflite::micro::compression::Metadata>(buf); | ||
} | ||
|
||
inline bool VerifyMetadataBuffer( | ||
::flatbuffers::Verifier &verifier) { | ||
return verifier.VerifyBuffer<tflite::micro::compression::Metadata>(nullptr); | ||
} | ||
|
||
inline bool VerifySizePrefixedMetadataBuffer( | ||
::flatbuffers::Verifier &verifier) { | ||
return verifier.VerifySizePrefixedBuffer<tflite::micro::compression::Metadata>(nullptr); | ||
} | ||
|
||
inline void FinishMetadataBuffer( | ||
::flatbuffers::FlatBufferBuilder &fbb, | ||
::flatbuffers::Offset<tflite::micro::compression::Metadata> root) { | ||
fbb.Finish(root); | ||
} | ||
|
||
inline void FinishSizePrefixedMetadataBuffer( | ||
::flatbuffers::FlatBufferBuilder &fbb, | ||
::flatbuffers::Offset<tflite::micro::compression::Metadata> root) { | ||
fbb.FinishSizePrefixed(root); | ||
} | ||
|
||
} // namespace compression | ||
} // namespace micro | ||
} // namespace tflite | ||
|
||
#endif // FLATBUFFERS_GENERATED_METADATA_TFLITE_MICRO_COMPRESSION_H_ |
Oops, something went wrong.