chore: import upstream snapshot with attribution
cffconvert / validate (push) Has been skipped
License Check / license-check (push) Failing after 2s

This commit is contained in:
wehub-resource-sync
2026-07-13 12:14:16 +08:00
commit 8a852e4b4e
36502 changed files with 9277225 additions and 0 deletions
+162
View File
@@ -0,0 +1,162 @@
load("@flatbuffers//:build_defs.bzl", "flatbuffer_cc_library")
load("@rules_cc//cc:cc_library.bzl", "cc_library")
# copybara:uncomment load("@flatbuffers//:flatbuffers.bzl", "flatbuffers_library", "ts_flatbuffers_library")
load("//tensorflow:tensorflow.bzl", "tf_cc_test")
load("//tensorflow:tensorflow.default.bzl", "get_compatible_with_portable")
# copybara:uncomment load("//tools/build_defs/cc:cc_embed_data.bzl", "cc_embed_data")
package(
# copybara:uncomment default_applicable_licenses = ["//tensorflow:LICENSE"],
default_visibility = [
"//visibility:public",
],
licenses = ["notice"],
)
exports_files(
srcs = [
"conversion_metadata.fbs",
"debug_metadata.fbs",
"schema.fbs",
"schema_v0.fbs",
"schema_v1.fbs",
"schema_v2.fbs",
"schema_v3.fbs",
"schema_v3a.fbs",
"schema_v3b.fbs",
"schema_v3c.fbs",
],
)
filegroup(
name = "tflite_internal_cc_3p_api_deps_src",
srcs = [
":debug_metadata_fbs_srcs",
":schema_fbs_srcs",
":schema_utils.h",
],
visibility = ["//tensorflow/lite:__pkg__"],
)
flatbuffer_cc_library(
name = "schema_fbs",
srcs = ["schema.fbs"],
compatible_with = get_compatible_with_portable(),
gen_reflections = True,
)
# copybara:uncomment_begin(google-only)
# cc_embed_data(
# name = "schema_reflection_data",
# srcs = ["schema.bfbs"],
# outs = [
# "schema_reflection_data.cc",
# "schema_reflection_data.h",
# "schema_reflection_data.o",
# ],
# embedopts = ["--namespace=tflite"],
# )
# copybara:uncomment_end
# Generic schema for flatbuffer converter (but with mutable makes bigger).
flatbuffer_cc_library(
name = "schema_fbs_with_mutable",
srcs = ["schema.fbs"],
compatible_with = get_compatible_with_portable(),
flatc_args = [
"--gen-mutable",
"--gen-object-api",
],
out_prefix = "mutable/",
)
flatbuffer_cc_library(
name = "debug_metadata_fbs",
srcs = ["debug_metadata.fbs"],
compatible_with = get_compatible_with_portable(),
)
flatbuffer_cc_library(
name = "debug_metadata_fbs_with_mutable",
srcs = ["debug_metadata.fbs"],
compatible_with = get_compatible_with_portable(),
flatc_args = [
"--gen-mutable",
"--gen-object-api",
],
out_prefix = "mutable/",
)
# Generic schema for inference on device (but with reflections makes bigger).
flatbuffer_cc_library(
name = "schema_fbs_with_reflection",
srcs = ["schema.fbs"],
compatible_with = get_compatible_with_portable(),
flatc_args = [
"--reflect-types",
"--reflect-names",
"--no-union-value-namespacing",
"--gen-object-api",
],
out_prefix = "reflection/",
)
cc_library(
name = "schema_utils",
srcs = ["schema_utils.cc"],
hdrs = ["schema_utils.h"],
compatible_with = get_compatible_with_portable(),
# visibility = [":utils_friends"],
deps = [
":schema_fbs",
"//tensorflow/compiler/mlir/lite/kernels/internal:compatibility_macros",
"@flatbuffers//:runtime_cc",
],
)
cc_library(
name = "schema_conversion_utils",
srcs = ["schema_conversion_utils.cc"],
hdrs = ["schema_conversion_utils.h"],
compatible_with = get_compatible_with_portable(),
# visibility = [":utils_friends"],
deps = [
":schema_fbs",
"@flatbuffers",
],
)
# Schema test to make sure we don't introduce backward incompatible changes
# to schemas.
tf_cc_test(
name = "flatbuffer_compatibility_test",
size = "small",
srcs = ["flatbuffer_compatibility_test.cc"],
data = [
"schema.fbs",
"schema_v3c.fbs",
],
tags = [
"no_oss",
"tflite_not_portable_android",
"tflite_not_portable_ios",
],
deps = [
"//tensorflow/core/platform",
"@com_google_googletest//:gtest_main",
"@flatbuffers//:flatc_library",
],
)
# copybara:uncomment_begin(google-only)
# flatbuffers_library(
# name = "schema_fbslib",
# srcs = ["schema.fbs"],
# )
#
# ts_flatbuffers_library(
# name = "schema_ts_fbs",
# deps = [":schema_fbslib"],
# )
# copybara:uncomment_end
@@ -0,0 +1,2 @@
This directory contains schema related files and targets that are used by both
the TFL converter (tf/compiler/mlir/lite/) and the runtime (tf/lite/).
@@ -0,0 +1,94 @@
// Copyright 2021 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;
// The original model format.
enum ModelType : int32 {
NONE = 0,
TF_SAVED_MODEL = 1,
KERAS_MODEL = 2,
TF_CONCRETE_FUNCTIONS = 3,
TF_GRAPH_DEF = 4,
TF_SESSION = 5,
JAX = 6,
PYTORCH = 7
}
// The environment during conversion.
table Environment {
// TensorFlow version string, e.g., "2.4.1".
tensorflow_version:string;
// TensorFlow Lite Converter API used.
api_version:uint;
// Original model format.
model_type:ModelType;
// Model identification hash.
model_hash:uint64;
}
// The model optimization modes.
// When adding a new optimization mode, it must be canonical with the existing
// ones. The thousand part of the value represents the optimization technique
// and the later part is to distinguish different modes of that technique.
enum ModelOptimizationMode : int32 {
// 0 is reserved as a placeholder
PTQ_FLOAT16 = 1001,
PTQ_DYNAMIC_RANGE = 1002,
PTQ_FULL_INTEGER = 1003,
PTQ_INT16 = 1004,
QUANTIZATION_AWARE_TRAINING = 2000,
RANDOM_SPARSITY = 3001,
BLOCK_SPARSITY = 3002,
STRUCTURED_SPARSITY = 3003,
}
// The block size used for sparsity.
table SparsityBlockSize {
values:[uint];
}
table ConversionOptions {
// Applied the optimization techniques.
model_optimization_modes:[ModelOptimizationMode];
// Boolean indicating whether to allow custom ops.
allow_custom_ops:bool;
// Boolean indicating whether to convert unsupported ops to select
// TensorFlow ops.
enable_select_tf_ops:bool;
// Boolean indicating whether to convert all TensorFlow ops to select
// TensorFlow ops.
force_select_tf_ops:bool;
// The block size used for sparsity.
sparsity_block_sizes:[SparsityBlockSize];
}
table ConversionMetadata {
// Metadata about the environment during conversion.
environment:Environment;
// Conversion options used.
options:ConversionOptions;
}
root_type ConversionMetadata;
@@ -0,0 +1,672 @@
/* Copyright 2021 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.
==============================================================================*/
// automatically generated by the FlatBuffers compiler, do not modify
#ifndef FLATBUFFERS_GENERATED_CONVERSIONMETADATA_TFLITE_H_
#define FLATBUFFERS_GENERATED_CONVERSIONMETADATA_TFLITE_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 == 25 &&
FLATBUFFERS_VERSION_MINOR == 9 &&
FLATBUFFERS_VERSION_REVISION == 23,
"Non-compatible flatbuffers version included");
namespace tflite {
struct Environment;
struct EnvironmentBuilder;
struct EnvironmentT;
struct SparsityBlockSize;
struct SparsityBlockSizeBuilder;
struct SparsityBlockSizeT;
struct ConversionOptions;
struct ConversionOptionsBuilder;
struct ConversionOptionsT;
struct ConversionMetadata;
struct ConversionMetadataBuilder;
struct ConversionMetadataT;
enum ModelType : int32_t {
ModelType_NONE = 0,
ModelType_TF_SAVED_MODEL = 1,
ModelType_KERAS_MODEL = 2,
ModelType_TF_CONCRETE_FUNCTIONS = 3,
ModelType_TF_GRAPH_DEF = 4,
ModelType_TF_SESSION = 5,
ModelType_JAX = 6,
ModelType_MIN = ModelType_NONE,
ModelType_MAX = ModelType_JAX
};
inline const ModelType (&EnumValuesModelType())[7] {
static const ModelType values[] = {
ModelType_NONE,
ModelType_TF_SAVED_MODEL,
ModelType_KERAS_MODEL,
ModelType_TF_CONCRETE_FUNCTIONS,
ModelType_TF_GRAPH_DEF,
ModelType_TF_SESSION,
ModelType_JAX
};
return values;
}
inline const char * const *EnumNamesModelType() {
static const char * const names[8] = {
"NONE",
"TF_SAVED_MODEL",
"KERAS_MODEL",
"TF_CONCRETE_FUNCTIONS",
"TF_GRAPH_DEF",
"TF_SESSION",
"JAX",
nullptr
};
return names;
}
inline const char *EnumNameModelType(ModelType e) {
if (::flatbuffers::IsOutRange(e, ModelType_NONE, ModelType_JAX)) return "";
const size_t index = static_cast<size_t>(e);
return EnumNamesModelType()[index];
}
enum ModelOptimizationMode : int32_t {
ModelOptimizationMode_PTQ_FLOAT16 = 1001,
ModelOptimizationMode_PTQ_DYNAMIC_RANGE = 1002,
ModelOptimizationMode_PTQ_FULL_INTEGER = 1003,
ModelOptimizationMode_PTQ_INT16 = 1004,
ModelOptimizationMode_QUANTIZATION_AWARE_TRAINING = 2000,
ModelOptimizationMode_RANDOM_SPARSITY = 3001,
ModelOptimizationMode_BLOCK_SPARSITY = 3002,
ModelOptimizationMode_STRUCTURED_SPARSITY = 3003,
ModelOptimizationMode_MIN = ModelOptimizationMode_PTQ_FLOAT16,
ModelOptimizationMode_MAX = ModelOptimizationMode_STRUCTURED_SPARSITY
};
inline const ModelOptimizationMode (&EnumValuesModelOptimizationMode())[8] {
static const ModelOptimizationMode values[] = {
ModelOptimizationMode_PTQ_FLOAT16,
ModelOptimizationMode_PTQ_DYNAMIC_RANGE,
ModelOptimizationMode_PTQ_FULL_INTEGER,
ModelOptimizationMode_PTQ_INT16,
ModelOptimizationMode_QUANTIZATION_AWARE_TRAINING,
ModelOptimizationMode_RANDOM_SPARSITY,
ModelOptimizationMode_BLOCK_SPARSITY,
ModelOptimizationMode_STRUCTURED_SPARSITY
};
return values;
}
inline const char *EnumNameModelOptimizationMode(ModelOptimizationMode e) {
switch (e) {
case ModelOptimizationMode_PTQ_FLOAT16: return "PTQ_FLOAT16";
case ModelOptimizationMode_PTQ_DYNAMIC_RANGE: return "PTQ_DYNAMIC_RANGE";
case ModelOptimizationMode_PTQ_FULL_INTEGER: return "PTQ_FULL_INTEGER";
case ModelOptimizationMode_PTQ_INT16: return "PTQ_INT16";
case ModelOptimizationMode_QUANTIZATION_AWARE_TRAINING: return "QUANTIZATION_AWARE_TRAINING";
case ModelOptimizationMode_RANDOM_SPARSITY: return "RANDOM_SPARSITY";
case ModelOptimizationMode_BLOCK_SPARSITY: return "BLOCK_SPARSITY";
case ModelOptimizationMode_STRUCTURED_SPARSITY: return "STRUCTURED_SPARSITY";
default: return "";
}
}
struct EnvironmentT : public ::flatbuffers::NativeTable {
typedef Environment TableType;
std::string tensorflow_version{};
uint32_t api_version = 0;
tflite::ModelType model_type = tflite::ModelType_NONE;
};
struct Environment FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table {
typedef EnvironmentT NativeTableType;
typedef EnvironmentBuilder Builder;
enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE {
VT_TENSORFLOW_VERSION = 4,
VT_API_VERSION = 6,
VT_MODEL_TYPE = 8
};
const ::flatbuffers::String *tensorflow_version() const {
return GetPointer<const ::flatbuffers::String *>(VT_TENSORFLOW_VERSION);
}
uint32_t api_version() const {
return GetField<uint32_t>(VT_API_VERSION, 0);
}
tflite::ModelType model_type() const {
return static_cast<tflite::ModelType>(GetField<int32_t>(VT_MODEL_TYPE, 0));
}
bool Verify(::flatbuffers::Verifier &verifier) const {
return VerifyTableStart(verifier) &&
VerifyOffset(verifier, VT_TENSORFLOW_VERSION) &&
verifier.VerifyString(tensorflow_version()) &&
VerifyField<uint32_t>(verifier, VT_API_VERSION, 4) &&
VerifyField<int32_t>(verifier, VT_MODEL_TYPE, 4) &&
verifier.EndTable();
}
EnvironmentT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const;
void UnPackTo(EnvironmentT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const;
static ::flatbuffers::Offset<Environment> Pack(::flatbuffers::FlatBufferBuilder &_fbb, const EnvironmentT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr);
};
struct EnvironmentBuilder {
typedef Environment Table;
::flatbuffers::FlatBufferBuilder &fbb_;
::flatbuffers::uoffset_t start_;
void add_tensorflow_version(::flatbuffers::Offset<::flatbuffers::String> tensorflow_version) {
fbb_.AddOffset(Environment::VT_TENSORFLOW_VERSION, tensorflow_version);
}
void add_api_version(uint32_t api_version) {
fbb_.AddElement<uint32_t>(Environment::VT_API_VERSION, api_version, 0);
}
void add_model_type(tflite::ModelType model_type) {
fbb_.AddElement<int32_t>(Environment::VT_MODEL_TYPE, static_cast<int32_t>(model_type), 0);
}
explicit EnvironmentBuilder(::flatbuffers::FlatBufferBuilder &_fbb)
: fbb_(_fbb) {
start_ = fbb_.StartTable();
}
::flatbuffers::Offset<Environment> Finish() {
const auto end = fbb_.EndTable(start_);
auto o = ::flatbuffers::Offset<Environment>(end);
return o;
}
};
inline ::flatbuffers::Offset<Environment> CreateEnvironment(
::flatbuffers::FlatBufferBuilder &_fbb,
::flatbuffers::Offset<::flatbuffers::String> tensorflow_version = 0,
uint32_t api_version = 0,
tflite::ModelType model_type = tflite::ModelType_NONE) {
EnvironmentBuilder builder_(_fbb);
builder_.add_model_type(model_type);
builder_.add_api_version(api_version);
builder_.add_tensorflow_version(tensorflow_version);
return builder_.Finish();
}
inline ::flatbuffers::Offset<Environment> CreateEnvironmentDirect(
::flatbuffers::FlatBufferBuilder &_fbb,
const char *tensorflow_version = nullptr,
uint32_t api_version = 0,
tflite::ModelType model_type = tflite::ModelType_NONE) {
auto tensorflow_version__ = tensorflow_version ? _fbb.CreateString(tensorflow_version) : 0;
return tflite::CreateEnvironment(
_fbb,
tensorflow_version__,
api_version,
model_type);
}
::flatbuffers::Offset<Environment> CreateEnvironment(::flatbuffers::FlatBufferBuilder &_fbb, const EnvironmentT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr);
struct SparsityBlockSizeT : public ::flatbuffers::NativeTable {
typedef SparsityBlockSize TableType;
std::vector<uint32_t> values{};
};
struct SparsityBlockSize FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table {
typedef SparsityBlockSizeT NativeTableType;
typedef SparsityBlockSizeBuilder Builder;
enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE {
VT_VALUES = 4
};
const ::flatbuffers::Vector<uint32_t> *values() const {
return GetPointer<const ::flatbuffers::Vector<uint32_t> *>(VT_VALUES);
}
bool Verify(::flatbuffers::Verifier &verifier) const {
return VerifyTableStart(verifier) &&
VerifyOffset(verifier, VT_VALUES) &&
verifier.VerifyVector(values()) &&
verifier.EndTable();
}
SparsityBlockSizeT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const;
void UnPackTo(SparsityBlockSizeT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const;
static ::flatbuffers::Offset<SparsityBlockSize> Pack(::flatbuffers::FlatBufferBuilder &_fbb, const SparsityBlockSizeT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr);
};
struct SparsityBlockSizeBuilder {
typedef SparsityBlockSize Table;
::flatbuffers::FlatBufferBuilder &fbb_;
::flatbuffers::uoffset_t start_;
void add_values(::flatbuffers::Offset<::flatbuffers::Vector<uint32_t>> values) {
fbb_.AddOffset(SparsityBlockSize::VT_VALUES, values);
}
explicit SparsityBlockSizeBuilder(::flatbuffers::FlatBufferBuilder &_fbb)
: fbb_(_fbb) {
start_ = fbb_.StartTable();
}
::flatbuffers::Offset<SparsityBlockSize> Finish() {
const auto end = fbb_.EndTable(start_);
auto o = ::flatbuffers::Offset<SparsityBlockSize>(end);
return o;
}
};
inline ::flatbuffers::Offset<SparsityBlockSize> CreateSparsityBlockSize(
::flatbuffers::FlatBufferBuilder &_fbb,
::flatbuffers::Offset<::flatbuffers::Vector<uint32_t>> values = 0) {
SparsityBlockSizeBuilder builder_(_fbb);
builder_.add_values(values);
return builder_.Finish();
}
inline ::flatbuffers::Offset<SparsityBlockSize> CreateSparsityBlockSizeDirect(
::flatbuffers::FlatBufferBuilder &_fbb,
const std::vector<uint32_t> *values = nullptr) {
auto values__ = values ? _fbb.CreateVector<uint32_t>(*values) : 0;
return tflite::CreateSparsityBlockSize(
_fbb,
values__);
}
::flatbuffers::Offset<SparsityBlockSize> CreateSparsityBlockSize(::flatbuffers::FlatBufferBuilder &_fbb, const SparsityBlockSizeT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr);
struct ConversionOptionsT : public ::flatbuffers::NativeTable {
typedef ConversionOptions TableType;
std::vector<tflite::ModelOptimizationMode> model_optimization_modes{};
bool allow_custom_ops = false;
bool enable_select_tf_ops = false;
bool force_select_tf_ops = false;
std::vector<std::unique_ptr<tflite::SparsityBlockSizeT>> sparsity_block_sizes{};
ConversionOptionsT() = default;
ConversionOptionsT(const ConversionOptionsT &o);
ConversionOptionsT(ConversionOptionsT&&) FLATBUFFERS_NOEXCEPT = default;
ConversionOptionsT &operator=(ConversionOptionsT o) FLATBUFFERS_NOEXCEPT;
};
struct ConversionOptions FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table {
typedef ConversionOptionsT NativeTableType;
typedef ConversionOptionsBuilder Builder;
enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE {
VT_MODEL_OPTIMIZATION_MODES = 4,
VT_ALLOW_CUSTOM_OPS = 6,
VT_ENABLE_SELECT_TF_OPS = 8,
VT_FORCE_SELECT_TF_OPS = 10,
VT_SPARSITY_BLOCK_SIZES = 12
};
const ::flatbuffers::Vector<int32_t> *model_optimization_modes() const {
return GetPointer<const ::flatbuffers::Vector<int32_t> *>(VT_MODEL_OPTIMIZATION_MODES);
}
bool allow_custom_ops() const {
return GetField<uint8_t>(VT_ALLOW_CUSTOM_OPS, 0) != 0;
}
bool enable_select_tf_ops() const {
return GetField<uint8_t>(VT_ENABLE_SELECT_TF_OPS, 0) != 0;
}
bool force_select_tf_ops() const {
return GetField<uint8_t>(VT_FORCE_SELECT_TF_OPS, 0) != 0;
}
const ::flatbuffers::Vector<::flatbuffers::Offset<tflite::SparsityBlockSize>> *sparsity_block_sizes() const {
return GetPointer<const ::flatbuffers::Vector<::flatbuffers::Offset<tflite::SparsityBlockSize>> *>(VT_SPARSITY_BLOCK_SIZES);
}
bool Verify(::flatbuffers::Verifier &verifier) const {
return VerifyTableStart(verifier) &&
VerifyOffset(verifier, VT_MODEL_OPTIMIZATION_MODES) &&
verifier.VerifyVector(model_optimization_modes()) &&
VerifyField<uint8_t>(verifier, VT_ALLOW_CUSTOM_OPS, 1) &&
VerifyField<uint8_t>(verifier, VT_ENABLE_SELECT_TF_OPS, 1) &&
VerifyField<uint8_t>(verifier, VT_FORCE_SELECT_TF_OPS, 1) &&
VerifyOffset(verifier, VT_SPARSITY_BLOCK_SIZES) &&
verifier.VerifyVector(sparsity_block_sizes()) &&
verifier.VerifyVectorOfTables(sparsity_block_sizes()) &&
verifier.EndTable();
}
ConversionOptionsT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const;
void UnPackTo(ConversionOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const;
static ::flatbuffers::Offset<ConversionOptions> Pack(::flatbuffers::FlatBufferBuilder &_fbb, const ConversionOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr);
};
struct ConversionOptionsBuilder {
typedef ConversionOptions Table;
::flatbuffers::FlatBufferBuilder &fbb_;
::flatbuffers::uoffset_t start_;
void add_model_optimization_modes(::flatbuffers::Offset<::flatbuffers::Vector<int32_t>> model_optimization_modes) {
fbb_.AddOffset(ConversionOptions::VT_MODEL_OPTIMIZATION_MODES, model_optimization_modes);
}
void add_allow_custom_ops(bool allow_custom_ops) {
fbb_.AddElement<uint8_t>(ConversionOptions::VT_ALLOW_CUSTOM_OPS, static_cast<uint8_t>(allow_custom_ops), 0);
}
void add_enable_select_tf_ops(bool enable_select_tf_ops) {
fbb_.AddElement<uint8_t>(ConversionOptions::VT_ENABLE_SELECT_TF_OPS, static_cast<uint8_t>(enable_select_tf_ops), 0);
}
void add_force_select_tf_ops(bool force_select_tf_ops) {
fbb_.AddElement<uint8_t>(ConversionOptions::VT_FORCE_SELECT_TF_OPS, static_cast<uint8_t>(force_select_tf_ops), 0);
}
void add_sparsity_block_sizes(::flatbuffers::Offset<::flatbuffers::Vector<::flatbuffers::Offset<tflite::SparsityBlockSize>>> sparsity_block_sizes) {
fbb_.AddOffset(ConversionOptions::VT_SPARSITY_BLOCK_SIZES, sparsity_block_sizes);
}
explicit ConversionOptionsBuilder(::flatbuffers::FlatBufferBuilder &_fbb)
: fbb_(_fbb) {
start_ = fbb_.StartTable();
}
::flatbuffers::Offset<ConversionOptions> Finish() {
const auto end = fbb_.EndTable(start_);
auto o = ::flatbuffers::Offset<ConversionOptions>(end);
return o;
}
};
inline ::flatbuffers::Offset<ConversionOptions> CreateConversionOptions(
::flatbuffers::FlatBufferBuilder &_fbb,
::flatbuffers::Offset<::flatbuffers::Vector<int32_t>> model_optimization_modes = 0,
bool allow_custom_ops = false,
bool enable_select_tf_ops = false,
bool force_select_tf_ops = false,
::flatbuffers::Offset<::flatbuffers::Vector<::flatbuffers::Offset<tflite::SparsityBlockSize>>> sparsity_block_sizes = 0) {
ConversionOptionsBuilder builder_(_fbb);
builder_.add_sparsity_block_sizes(sparsity_block_sizes);
builder_.add_model_optimization_modes(model_optimization_modes);
builder_.add_force_select_tf_ops(force_select_tf_ops);
builder_.add_enable_select_tf_ops(enable_select_tf_ops);
builder_.add_allow_custom_ops(allow_custom_ops);
return builder_.Finish();
}
inline ::flatbuffers::Offset<ConversionOptions> CreateConversionOptionsDirect(
::flatbuffers::FlatBufferBuilder &_fbb,
const std::vector<int32_t> *model_optimization_modes = nullptr,
bool allow_custom_ops = false,
bool enable_select_tf_ops = false,
bool force_select_tf_ops = false,
const std::vector<::flatbuffers::Offset<tflite::SparsityBlockSize>> *sparsity_block_sizes = nullptr) {
auto model_optimization_modes__ = model_optimization_modes ? _fbb.CreateVector<int32_t>(*model_optimization_modes) : 0;
auto sparsity_block_sizes__ = sparsity_block_sizes ? _fbb.CreateVector<::flatbuffers::Offset<tflite::SparsityBlockSize>>(*sparsity_block_sizes) : 0;
return tflite::CreateConversionOptions(
_fbb,
model_optimization_modes__,
allow_custom_ops,
enable_select_tf_ops,
force_select_tf_ops,
sparsity_block_sizes__);
}
::flatbuffers::Offset<ConversionOptions> CreateConversionOptions(::flatbuffers::FlatBufferBuilder &_fbb, const ConversionOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr);
struct ConversionMetadataT : public ::flatbuffers::NativeTable {
typedef ConversionMetadata TableType;
std::unique_ptr<tflite::EnvironmentT> environment{};
std::unique_ptr<tflite::ConversionOptionsT> options{};
ConversionMetadataT() = default;
ConversionMetadataT(const ConversionMetadataT &o);
ConversionMetadataT(ConversionMetadataT&&) FLATBUFFERS_NOEXCEPT = default;
ConversionMetadataT &operator=(ConversionMetadataT o) FLATBUFFERS_NOEXCEPT;
};
struct ConversionMetadata FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table {
typedef ConversionMetadataT NativeTableType;
typedef ConversionMetadataBuilder Builder;
enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE {
VT_ENVIRONMENT = 4,
VT_OPTIONS = 6
};
const tflite::Environment *environment() const {
return GetPointer<const tflite::Environment *>(VT_ENVIRONMENT);
}
const tflite::ConversionOptions *options() const {
return GetPointer<const tflite::ConversionOptions *>(VT_OPTIONS);
}
bool Verify(::flatbuffers::Verifier &verifier) const {
return VerifyTableStart(verifier) &&
VerifyOffset(verifier, VT_ENVIRONMENT) &&
verifier.VerifyTable(environment()) &&
VerifyOffset(verifier, VT_OPTIONS) &&
verifier.VerifyTable(options()) &&
verifier.EndTable();
}
ConversionMetadataT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const;
void UnPackTo(ConversionMetadataT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const;
static ::flatbuffers::Offset<ConversionMetadata> Pack(::flatbuffers::FlatBufferBuilder &_fbb, const ConversionMetadataT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr);
};
struct ConversionMetadataBuilder {
typedef ConversionMetadata Table;
::flatbuffers::FlatBufferBuilder &fbb_;
::flatbuffers::uoffset_t start_;
void add_environment(::flatbuffers::Offset<tflite::Environment> environment) {
fbb_.AddOffset(ConversionMetadata::VT_ENVIRONMENT, environment);
}
void add_options(::flatbuffers::Offset<tflite::ConversionOptions> options) {
fbb_.AddOffset(ConversionMetadata::VT_OPTIONS, options);
}
explicit ConversionMetadataBuilder(::flatbuffers::FlatBufferBuilder &_fbb)
: fbb_(_fbb) {
start_ = fbb_.StartTable();
}
::flatbuffers::Offset<ConversionMetadata> Finish() {
const auto end = fbb_.EndTable(start_);
auto o = ::flatbuffers::Offset<ConversionMetadata>(end);
return o;
}
};
inline ::flatbuffers::Offset<ConversionMetadata> CreateConversionMetadata(
::flatbuffers::FlatBufferBuilder &_fbb,
::flatbuffers::Offset<tflite::Environment> environment = 0,
::flatbuffers::Offset<tflite::ConversionOptions> options = 0) {
ConversionMetadataBuilder builder_(_fbb);
builder_.add_options(options);
builder_.add_environment(environment);
return builder_.Finish();
}
::flatbuffers::Offset<ConversionMetadata> CreateConversionMetadata(::flatbuffers::FlatBufferBuilder &_fbb, const ConversionMetadataT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr);
inline EnvironmentT *Environment::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const {
auto _o = std::unique_ptr<EnvironmentT>(new EnvironmentT());
UnPackTo(_o.get(), _resolver);
return _o.release();
}
inline void Environment::UnPackTo(EnvironmentT *_o, const ::flatbuffers::resolver_function_t *_resolver) const {
(void)_o;
(void)_resolver;
{ auto _e = tensorflow_version(); if (_e) _o->tensorflow_version = _e->str(); }
{ auto _e = api_version(); _o->api_version = _e; }
{ auto _e = model_type(); _o->model_type = _e; }
}
inline ::flatbuffers::Offset<Environment> Environment::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const EnvironmentT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) {
return CreateEnvironment(_fbb, _o, _rehasher);
}
inline ::flatbuffers::Offset<Environment> CreateEnvironment(::flatbuffers::FlatBufferBuilder &_fbb, const EnvironmentT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) {
(void)_rehasher;
(void)_o;
struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const EnvironmentT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va;
auto _tensorflow_version = _o->tensorflow_version.empty() ? 0 : _fbb.CreateString(_o->tensorflow_version);
auto _api_version = _o->api_version;
auto _model_type = _o->model_type;
return tflite::CreateEnvironment(
_fbb,
_tensorflow_version,
_api_version,
_model_type);
}
inline SparsityBlockSizeT *SparsityBlockSize::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const {
auto _o = std::unique_ptr<SparsityBlockSizeT>(new SparsityBlockSizeT());
UnPackTo(_o.get(), _resolver);
return _o.release();
}
inline void SparsityBlockSize::UnPackTo(SparsityBlockSizeT *_o, const ::flatbuffers::resolver_function_t *_resolver) const {
(void)_o;
(void)_resolver;
{ auto _e = values(); if (_e) { _o->values.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->values[_i] = _e->Get(_i); } } else { _o->values.resize(0); } }
}
inline ::flatbuffers::Offset<SparsityBlockSize> SparsityBlockSize::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const SparsityBlockSizeT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) {
return CreateSparsityBlockSize(_fbb, _o, _rehasher);
}
inline ::flatbuffers::Offset<SparsityBlockSize> CreateSparsityBlockSize(::flatbuffers::FlatBufferBuilder &_fbb, const SparsityBlockSizeT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) {
(void)_rehasher;
(void)_o;
struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const SparsityBlockSizeT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va;
auto _values = _o->values.size() ? _fbb.CreateVector(_o->values) : 0;
return tflite::CreateSparsityBlockSize(
_fbb,
_values);
}
inline ConversionOptionsT::ConversionOptionsT(const ConversionOptionsT &o)
: model_optimization_modes(o.model_optimization_modes),
allow_custom_ops(o.allow_custom_ops),
enable_select_tf_ops(o.enable_select_tf_ops),
force_select_tf_ops(o.force_select_tf_ops) {
sparsity_block_sizes.reserve(o.sparsity_block_sizes.size());
for (const auto &sparsity_block_sizes_ : o.sparsity_block_sizes) { sparsity_block_sizes.emplace_back((sparsity_block_sizes_) ? new tflite::SparsityBlockSizeT(*sparsity_block_sizes_) : nullptr); }
}
inline ConversionOptionsT &ConversionOptionsT::operator=(ConversionOptionsT o) FLATBUFFERS_NOEXCEPT {
std::swap(model_optimization_modes, o.model_optimization_modes);
std::swap(allow_custom_ops, o.allow_custom_ops);
std::swap(enable_select_tf_ops, o.enable_select_tf_ops);
std::swap(force_select_tf_ops, o.force_select_tf_ops);
std::swap(sparsity_block_sizes, o.sparsity_block_sizes);
return *this;
}
inline ConversionOptionsT *ConversionOptions::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const {
auto _o = std::unique_ptr<ConversionOptionsT>(new ConversionOptionsT());
UnPackTo(_o.get(), _resolver);
return _o.release();
}
inline void ConversionOptions::UnPackTo(ConversionOptionsT *_o, const ::flatbuffers::resolver_function_t *_resolver) const {
(void)_o;
(void)_resolver;
{ auto _e = model_optimization_modes(); if (_e) { _o->model_optimization_modes.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->model_optimization_modes[_i] = static_cast<tflite::ModelOptimizationMode>(_e->Get(_i)); } } else { _o->model_optimization_modes.resize(0); } }
{ auto _e = allow_custom_ops(); _o->allow_custom_ops = _e; }
{ auto _e = enable_select_tf_ops(); _o->enable_select_tf_ops = _e; }
{ auto _e = force_select_tf_ops(); _o->force_select_tf_ops = _e; }
{ auto _e = sparsity_block_sizes(); if (_e) { _o->sparsity_block_sizes.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { if(_o->sparsity_block_sizes[_i]) { _e->Get(_i)->UnPackTo(_o->sparsity_block_sizes[_i].get(), _resolver); } else { _o->sparsity_block_sizes[_i] = std::unique_ptr<tflite::SparsityBlockSizeT>(_e->Get(_i)->UnPack(_resolver)); }; } } else { _o->sparsity_block_sizes.resize(0); } }
}
inline ::flatbuffers::Offset<ConversionOptions> ConversionOptions::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const ConversionOptionsT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) {
return CreateConversionOptions(_fbb, _o, _rehasher);
}
inline ::flatbuffers::Offset<ConversionOptions> CreateConversionOptions(::flatbuffers::FlatBufferBuilder &_fbb, const ConversionOptionsT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) {
(void)_rehasher;
(void)_o;
struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const ConversionOptionsT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va;
auto _model_optimization_modes = _o->model_optimization_modes.size() ? _fbb.CreateVectorScalarCast<int32_t>(::flatbuffers::data(_o->model_optimization_modes), _o->model_optimization_modes.size()) : 0;
auto _allow_custom_ops = _o->allow_custom_ops;
auto _enable_select_tf_ops = _o->enable_select_tf_ops;
auto _force_select_tf_ops = _o->force_select_tf_ops;
auto _sparsity_block_sizes = _o->sparsity_block_sizes.size() ? _fbb.CreateVector<::flatbuffers::Offset<tflite::SparsityBlockSize>> (_o->sparsity_block_sizes.size(), [](size_t i, _VectorArgs *__va) { return CreateSparsityBlockSize(*__va->__fbb, __va->__o->sparsity_block_sizes[i].get(), __va->__rehasher); }, &_va ) : 0;
return tflite::CreateConversionOptions(
_fbb,
_model_optimization_modes,
_allow_custom_ops,
_enable_select_tf_ops,
_force_select_tf_ops,
_sparsity_block_sizes);
}
inline ConversionMetadataT::ConversionMetadataT(const ConversionMetadataT &o)
: environment((o.environment) ? new tflite::EnvironmentT(*o.environment) : nullptr),
options((o.options) ? new tflite::ConversionOptionsT(*o.options) : nullptr) {
}
inline ConversionMetadataT &ConversionMetadataT::operator=(ConversionMetadataT o) FLATBUFFERS_NOEXCEPT {
std::swap(environment, o.environment);
std::swap(options, o.options);
return *this;
}
inline ConversionMetadataT *ConversionMetadata::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const {
auto _o = std::unique_ptr<ConversionMetadataT>(new ConversionMetadataT());
UnPackTo(_o.get(), _resolver);
return _o.release();
}
inline void ConversionMetadata::UnPackTo(ConversionMetadataT *_o, const ::flatbuffers::resolver_function_t *_resolver) const {
(void)_o;
(void)_resolver;
{ auto _e = environment(); if (_e) { if(_o->environment) { _e->UnPackTo(_o->environment.get(), _resolver); } else { _o->environment = std::unique_ptr<tflite::EnvironmentT>(_e->UnPack(_resolver)); } } else if (_o->environment) { _o->environment.reset(); } }
{ auto _e = options(); if (_e) { if(_o->options) { _e->UnPackTo(_o->options.get(), _resolver); } else { _o->options = std::unique_ptr<tflite::ConversionOptionsT>(_e->UnPack(_resolver)); } } else if (_o->options) { _o->options.reset(); } }
}
inline ::flatbuffers::Offset<ConversionMetadata> ConversionMetadata::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const ConversionMetadataT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) {
return CreateConversionMetadata(_fbb, _o, _rehasher);
}
inline ::flatbuffers::Offset<ConversionMetadata> CreateConversionMetadata(::flatbuffers::FlatBufferBuilder &_fbb, const ConversionMetadataT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) {
(void)_rehasher;
(void)_o;
struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const ConversionMetadataT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va;
auto _environment = _o->environment ? CreateEnvironment(_fbb, _o->environment.get(), _rehasher) : 0;
auto _options = _o->options ? CreateConversionOptions(_fbb, _o->options.get(), _rehasher) : 0;
return tflite::CreateConversionMetadata(
_fbb,
_environment,
_options);
}
inline const tflite::ConversionMetadata *GetConversionMetadata(const void *buf) {
return ::flatbuffers::GetRoot<tflite::ConversionMetadata>(buf);
}
inline const tflite::ConversionMetadata *GetSizePrefixedConversionMetadata(const void *buf) {
return ::flatbuffers::GetSizePrefixedRoot<tflite::ConversionMetadata>(buf);
}
inline bool VerifyConversionMetadataBuffer(
::flatbuffers::Verifier &verifier) {
return verifier.VerifyBuffer<tflite::ConversionMetadata>(nullptr);
}
inline bool VerifySizePrefixedConversionMetadataBuffer(
::flatbuffers::Verifier &verifier) {
return verifier.VerifySizePrefixedBuffer<tflite::ConversionMetadata>(nullptr);
}
inline void FinishConversionMetadataBuffer(
::flatbuffers::FlatBufferBuilder &fbb,
::flatbuffers::Offset<tflite::ConversionMetadata> root) {
fbb.Finish(root);
}
inline void FinishSizePrefixedConversionMetadataBuffer(
::flatbuffers::FlatBufferBuilder &fbb,
::flatbuffers::Offset<tflite::ConversionMetadata> root) {
fbb.FinishSizePrefixed(root);
}
inline std::unique_ptr<tflite::ConversionMetadataT> UnPackConversionMetadata(
const void *buf,
const ::flatbuffers::resolver_function_t *res = nullptr) {
return std::unique_ptr<tflite::ConversionMetadataT>(GetConversionMetadata(buf)->UnPack(res));
}
inline std::unique_ptr<tflite::ConversionMetadataT> UnPackSizePrefixedConversionMetadata(
const void *buf,
const ::flatbuffers::resolver_function_t *res = nullptr) {
return std::unique_ptr<tflite::ConversionMetadataT>(GetSizePrefixedConversionMetadata(buf)->UnPack(res));
}
} // namespace tflite
#endif // FLATBUFFERS_GENERATED_CONVERSIONMETADATA_TFLITE_H_
@@ -0,0 +1,98 @@
// 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 debug_metadata;
table DictItem {
key: string;
val: string;
}
table CallSiteLoc {
callee_index: uint32;
caller_index: uint32;
}
table FileLineColLoc {
filename: string;
line: uint32;
column: uint32;
}
table FusedLoc {
// List of locations.
location_indexes: [uint32];
}
table NameLoc {
name: string;
// Nested location for hierarchical naming.
child_index: uint32;
}
table OpaqueLoc {
identifier: string;
fallback_location_index: uint32;
}
table UnknownLoc {
// No fields needed.
}
union LocationType {
CallSiteLoc,
FileLineColLoc,
FusedLoc,
NameLoc,
OpaqueLoc,
UnknownLoc
}
table Location {
location: LocationType;
}
union Attribute {
Location,
DictItem,
}
table OperatorDebugMetadata {
// Top-level location metadata index of this operator.
attribute_metadata_indexes: [uint32];
}
table SubgraphDebugMetadata {
// Debug metadata of all the operators.
// This list should be append-only.
operators_debug_metadata:[OperatorDebugMetadata];
}
table ConversionDebugMetadata {
// Debug metadata of all the subgraphs.
// This list should be append-only.
subgraphs_debug_metadata:[SubgraphDebugMetadata];
attributes: [Attribute];
}
union DebugMetadataType {
ConversionDebugMetadata,
}
table DebugMetadata {
debug_metadata: [DebugMetadataType];
}
root_type DebugMetadata;
@@ -0,0 +1,88 @@
/* Copyright 2017 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.
==============================================================================*/
#include <fstream>
#include <string>
#include <gtest/gtest.h>
#include "flatbuffers/flatc.h" // from @flatbuffers
#include "tensorflow/core/platform/platform.h"
#ifdef PLATFORM_GOOGLE
#define TFLITE_TF_PREFIX "third_party/tensorflow/"
#else
#define TFLITE_TF_PREFIX "tensorflow/"
#endif
/// Load filename `name`
bool LoadFileRaw(const char *name, std::string *buf) {
std::ifstream fp(name, std::ios::binary);
if (!fp) {
fprintf(stderr, "Failed to read '%s'\n", name);
return false;
}
std::string s((std::istreambuf_iterator<char>(fp)),
std::istreambuf_iterator<char>());
if (s.empty()) {
fprintf(stderr, "Read '%s' resulted in empty\n", name);
return false;
}
*buf = s;
return true;
}
bool ParseFile(flatbuffers::Parser *parser, const std::string &filename,
const std::string &contents) {
std::vector<const char *> include_directories;
auto local_include_directory = flatbuffers::StripFileName(filename);
include_directories.push_back(local_include_directory.c_str());
include_directories.push_back(nullptr);
if (!parser->Parse(contents.c_str(), include_directories.data(),
filename.c_str())) {
fprintf(stderr, "Failed to parse flatbuffer schema '%s'\n",
contents.c_str());
return false;
}
return true;
}
// Checks to make sure current schema in current code does not cause an
// incompatibility.
TEST(SchemaTest, TestCompatibility) {
// Read file contents of schemas into strings
// TODO(aselle): Need a reliable way to load files.
std::string base_contents, current_contents;
const char* base_filename =
TFLITE_TF_PREFIX "compiler/mlir/lite/schema/schema_v3c.fbs";
const char *current_filename =
TFLITE_TF_PREFIX "compiler/mlir/lite/schema/schema.fbs";
ASSERT_TRUE(LoadFileRaw(base_filename, &base_contents));
ASSERT_TRUE(LoadFileRaw(current_filename, &current_contents));
// Parse the schemas
flatbuffers::Parser base_parser, current_parser;
std::vector<const char *> include_directories;
ASSERT_TRUE(ParseFile(&base_parser, base_filename, base_contents));
ASSERT_TRUE(ParseFile(&current_parser, current_filename, current_contents));
// Check that the schemas conform and fail if they don't
auto err = current_parser.ConformTo(base_parser);
if (!err.empty()) {
fprintf(stderr,
"Schemas don't conform:\n%s\n"
"In other words some change you made means that new parsers can't"
"parse old files.\n",
err.c_str());
FAIL();
}
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,68 @@
/* Copyright 2020 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.
==============================================================================*/
#include "tensorflow/compiler/mlir/lite/schema/schema_conversion_utils.h"
#include <cstdint>
namespace tflite {
int8_t ConvertBuiltinCodeToDeprecatedBuiltinCode(
const BuiltinOperator builtin_code) {
return (builtin_code < BuiltinOperator_PLACEHOLDER_FOR_GREATER_OP_CODES)
? static_cast<int8_t>(builtin_code)
: static_cast<int8_t>(
BuiltinOperator_PLACEHOLDER_FOR_GREATER_OP_CODES);
}
// The following methods are the following `OperatorCode` table object creation
// methods for backward compatibility. These are manually copied from the
// flatbuffer generated code from schema v3. They serve as overloads for the
// v3a's CreateOperatorCode functions in schema_generated.h and enable code that
// still assumes flatbuffer schema v3 to be unchanged with the inclusion of the
// schema_utils header.
// TODO(b/162392898): remove once all callers are updated to use schema v3a
// functions.
flatbuffers::Offset<OperatorCode> CreateOperatorCode(
flatbuffers::FlatBufferBuilder &_fbb, BuiltinOperator builtin_code,
flatbuffers::Offset<flatbuffers::String> custom_code, int32_t version) {
OperatorCodeBuilder builder_(_fbb);
builder_.add_version(version);
int8_t deprecated_builtin_code =
static_cast<int8_t>(BuiltinOperator_PLACEHOLDER_FOR_GREATER_OP_CODES);
if (builtin_code < BuiltinOperator_PLACEHOLDER_FOR_GREATER_OP_CODES) {
deprecated_builtin_code = static_cast<int8_t>(builtin_code);
}
builder_.add_deprecated_builtin_code(deprecated_builtin_code);
builder_.add_custom_code(custom_code);
builder_.add_builtin_code(builtin_code);
return builder_.Finish();
}
flatbuffers::Offset<OperatorCode> CreateOperatorCodeDirect(
flatbuffers::FlatBufferBuilder &_fbb, BuiltinOperator builtin_code,
const char *custom_code, int32_t version) {
auto custom_code__ = custom_code ? _fbb.CreateString(custom_code) : 0;
int8_t deprecated_builtin_code =
static_cast<int8_t>(BuiltinOperator_PLACEHOLDER_FOR_GREATER_OP_CODES);
if (builtin_code < BuiltinOperator_PLACEHOLDER_FOR_GREATER_OP_CODES) {
deprecated_builtin_code = static_cast<int8_t>(builtin_code);
}
return CreateOperatorCode(_fbb, deprecated_builtin_code, custom_code__,
version, builtin_code);
}
} // namespace tflite
@@ -0,0 +1,40 @@
/* Copyright 2020 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.
==============================================================================*/
#ifndef TENSORFLOW_COMPILER_MLIR_LITE_SCHEMA_SCHEMA_CONVERSION_UTILS_H_
#define TENSORFLOW_COMPILER_MLIR_LITE_SCHEMA_SCHEMA_CONVERSION_UTILS_H_
#include "flatbuffers/flatbuffers.h"
#include "tensorflow/compiler/mlir/lite/schema/schema_generated.h"
namespace tflite {
int8_t ConvertBuiltinCodeToDeprecatedBuiltinCode(BuiltinOperator builtin_code);
// The following methods are for backward compatibility for the early version
// three, which does not have an extended builtin code.
flatbuffers::Offset<OperatorCode> CreateOperatorCode(
flatbuffers::FlatBufferBuilder &_fbb,
BuiltinOperator builtin_code = BuiltinOperator_ADD,
flatbuffers::Offset<flatbuffers::String> custom_code = 0,
int32_t version = 1);
flatbuffers::Offset<OperatorCode> CreateOperatorCodeDirect(
flatbuffers::FlatBufferBuilder &_fbb,
BuiltinOperator builtin_code = BuiltinOperator_ADD,
const char *custom_code = nullptr, int32_t version = 1);
} // namespace tflite
#endif // TENSORFLOW_COMPILER_MLIR_LITE_SCHEMA_SCHEMA_CONVERSION_UTILS_H_
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,113 @@
/* Copyright 2020 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.
==============================================================================*/
#include "tensorflow/compiler/mlir/lite/schema/schema_utils.h"
#include <algorithm>
#include <complex>
#include <cstddef>
#include <cstdint>
#include "tensorflow/compiler/mlir/lite/kernels/internal/compatibility_macros.h"
#include "tensorflow/compiler/mlir/lite/schema/schema_generated.h"
namespace tflite {
// The following GetBuiltinCode methods are the utility methods for reading
// builtin operator code, ensuring compatibility issues between v3 and v3a
// schema. Always the maximum value of the two fields always will be the correct
// value as follows:
//
// - Supporting schema version v3 models
//
// The `builtin_code` field is not available in the v3 models. Flatbuffer
// library will feed zero value, which is the default value in the v3a schema.
// The actual builtin operator code value will exist in the
// `deprecated_builtin_code` field. At the same time, it implies that
// `deprecated_builtin_code` >= `builtin_code` and the maximum value of the two
// fields will be same with `deprecated_builtin_code'.
//
// - Supporting builtin operator codes beyonds 127
//
// New builtin operators, whose operator code is larger than 127, can not be
// assigned to the `deprecated_builtin_code` field. In such cases, the
// value of the `builtin_code` field should be used for the builtin operator
// code. In the case, the maximum value of the two fields will be the value of
// the `builtin_code` as the right value.
BuiltinOperator GetBuiltinCode(const OperatorCode* op_code) {
// Caller should guarantee that the given argument value is not a nullptr.
TFLITE_DCHECK(op_code != nullptr);
return std::max(
op_code->builtin_code(),
static_cast<BuiltinOperator>(op_code->deprecated_builtin_code()));
}
BuiltinOperator GetBuiltinCode(const OperatorCodeT* op_code) {
// Caller should guarantee that the given argument value is not a nullptr.
TFLITE_DCHECK(op_code != nullptr);
return std::max(op_code->builtin_code, static_cast<BuiltinOperator>(
op_code->deprecated_builtin_code));
}
size_t TensorTypeGetSize(::tflite::TensorType data_type) {
switch (data_type) {
case ::tflite::TensorType_FLOAT32:
static_assert(sizeof(float) == 4, "");
return 4;
case ::tflite::TensorType_FLOAT16:
static_assert(sizeof(int16_t) == 2, "");
return 2;
case ::tflite::TensorType_INT32:
static_assert(sizeof(int32_t) == 4, "");
return 4;
case ::tflite::TensorType_UINT8:
static_assert(sizeof(uint8_t) == 1, "");
return 1;
case ::tflite::TensorType_INT64:
static_assert(sizeof(int64_t) == 8, "");
return 8;
case ::tflite::TensorType_BOOL:
return sizeof(bool);
case ::tflite::TensorType_INT16:
static_assert(sizeof(int16_t) == 2, "");
return 2;
case ::tflite::TensorType_COMPLEX64:
static_assert(sizeof(std::complex<float>) == 8, "");
return 8;
case ::tflite::TensorType_INT8:
static_assert(sizeof(int8_t) == 1, "");
return 1;
case ::tflite::TensorType_FLOAT64:
static_assert(sizeof(double) == 8, "");
return 8;
case ::tflite::TensorType_COMPLEX128:
static_assert(sizeof(std::complex<double>) == 16, "");
return 16;
case ::tflite::TensorType_UINT64:
static_assert(sizeof(uint64_t) == 8, "");
return 8;
case ::tflite::TensorType_UINT32:
static_assert(sizeof(uint32_t) == 4, "");
return 4;
case ::tflite::TensorType_UINT16:
static_assert(sizeof(uint16_t) == 2, "");
return 2;
default:
return 0;
}
}
} // namespace tflite
@@ -0,0 +1,40 @@
/* Copyright 2020 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.
==============================================================================*/
#ifndef TENSORFLOW_COMPILER_MLIR_LITE_SCHEMA_SCHEMA_UTILS_H_
#define TENSORFLOW_COMPILER_MLIR_LITE_SCHEMA_SCHEMA_UTILS_H_
#include <cstddef>
#include "flatbuffers/flatbuffers.h"
#include "tensorflow/compiler/mlir/lite/schema/schema_generated.h"
namespace tflite {
// The following methods are introduced to resolve op builtin code shortage
// problem. The new builtin operator will be assigned to the extended builtin
// code field in the flatbuffer schema. Those methods helps to hide builtin code
// details.
BuiltinOperator GetBuiltinCode(const OperatorCode *op_code);
BuiltinOperator GetBuiltinCode(const OperatorCodeT *op_code);
// Returns the size of the given TensorType in bytes, or 0 if the TensorType is
// not supported, this function should be aligned with TfLiteTypeGetSize in
// lite/kernels/kernel_util.h.
size_t TensorTypeGetSize(::tflite::TensorType data_type);
} // namespace tflite
#endif // TENSORFLOW_COMPILER_MLIR_LITE_SCHEMA_SCHEMA_UTILS_H_
@@ -0,0 +1,247 @@
// Copyright 2017 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;
// The type of data stored in a tensor.
enum TensorType : byte {
FLOAT32 = 0,
FLOAT16 = 1,
INT32 = 2,
UINT8 = 3,
INT64 = 4,
}
// Parameters for converting a quantized tensor back to float. Given a
// quantized value q, the corresponding float value f should be:
// f = scale * (q - zero_point)
table QuantizationParameters {
min:[float]; // For importing back into tensorflow.
max:[float]; // For importing back into tensorflow.
scale:[float];
zero_point:[long];
}
table Tensor {
// The tensor shape. The meaning of each entry is operator-specific but
// builtin ops use: [batch size, height, width, number of channels] (That's
// Tensorflow's NHWC).
shape:[int];
type:TensorType;
// The data_buffer is an opaque container, with the assumption that the
// target device is little-endian. In addition, all builtin operators assume
// the memory is ordered such that if `shape` is [4, 3, 2], then index
// [i, j, k] maps to data_buffer[i*4*3 + j*3 + k].
data_buffer:[ubyte];
name:string; // For debugging and importing back into tensorflow.
quantization:QuantizationParameters; // Optional.
}
// A list of builtin operators. Builtin operators are slightly faster than custom
// ones, but not by much. Moreover, while custom operators accept an opaque
// object containing configuration parameters, builtins have a predetermined
// set of acceptable options.
enum BuiltinOperator : byte {
CUSTOM = 0,
CONVOLUTION = 1,
DEPTHWISE_CONVOLUTION = 2,
CONCAT_EMBEDDINGS = 3,
LSH_PROJECTION = 4,
TANH = 5,
RELU = 6,
AVERAGE_POOL = 7,
MAX_POOL = 8,
L2_POOL = 9,
SIGMOID = 10,
SVDF = 11,
BasicRNN = 12,
RELU6 = 13,
EMBEDDING_LOOKUP = 14,
FULLY_CONNECTED = 15,
HASHTABLE_LOOKUP = 16,
SOFTMAX = 17,
CONCATENATION = 18,
LSTM = 19,
ADD = 20,
L2NORM = 21,
LOCAL_RESPONSE_NORM = 22,
RESIZE_BILINEAR = 23,
}
// Options for the builtin operators.
union BuiltinOptions {
ConvolutionOptions,
DepthwiseConvolutionOptions,
ConcatEmbeddingsOptions,
LSHProjectionOptions,
PoolOptions,
SVDFOptions,
BasicRNNOptions,
FullyConnectedOptions,
SoftmaxOptions,
ConcatenationOptions,
AddOptions,
L2NormOptions,
LocalResponseNormOptions,
LSTMOptions,
ResizeBilinearOptions,
}
enum Padding : byte { SAME, VALID }
enum ActivationFunctionType : byte {
NONE = 0,
RELU = 1,
RELU1 = 2,
RELU6 = 3,
TANH = 4,
SIGN_BIT = 5,
}
table ConvolutionOptions {
padding:Padding;
stride_w:int;
stride_h:int;
fused_activation_function:ActivationFunctionType;
}
table PoolOptions {
padding:Padding;
stride_w:int;
stride_h:int;
filter_width:int;
filter_height:int;
fused_activation_function:ActivationFunctionType;
}
table DepthwiseConvolutionOptions {
padding:Padding;
stride_w:int;
stride_h:int;
depth_multiplier:int;
fused_activation_function:ActivationFunctionType;
}
table ConcatEmbeddingsOptions {
num_channels:int;
num_columns_per_channel:[int];
embedding_dim_per_channel:[int]; // This could be inferred from parameters.
}
enum LSHProjectionType: byte {
UNKNOWN = 0,
SPARSE = 1,
DENSE = 2,
}
table LSHProjectionOptions {
type: LSHProjectionType;
}
table SVDFOptions {
rank:int;
fused_activation_function:ActivationFunctionType;
}
// An implementation of TensorFlow BasicRNNCell.
table BasicRNNOptions {
fused_activation_function:ActivationFunctionType;
}
// An implementation of TensorFlow fully_connected (a.k.a Dense) layer.
table FullyConnectedOptions {
fused_activation_function:ActivationFunctionType;
}
table SoftmaxOptions {
beta: float;
}
// An implementation of TensorFlow concat.
table ConcatenationOptions {
axis:int;
fused_activation_function:ActivationFunctionType;
}
table AddOptions {
fused_activation_function:ActivationFunctionType;
}
table L2NormOptions {
fused_activation_function:ActivationFunctionType;
}
table LocalResponseNormOptions {
radius:int;
bias:float;
alpha:float;
beta:float;
}
// An implementation of TensorFlow LSTMCell and CoupledInputForgetGateLSTMCell
table LSTMOptions {
fused_activation_function:ActivationFunctionType;
cell_clip: float; // Optional, 0.0 means no clipping
proj_clip: float; // Optional, 0.0 means no clipping
}
table ResizeBilinearOptions {
new_height:int;
new_width:int;
}
// An OperatorCode can be an enum value (BuiltinOperator) if the operator is a
// builtin, or a string if the operator is custom.
table OperatorCode {
builtin_code:BuiltinOperator;
custom_code:string;
}
// An operator takes tensors as inputs and outputs. The type of operation being
// performed is determined by an index into the list of valid OperatorCodes,
// while the specifics of each operations is configured using builtin_options
// or custom_options.
table Operator {
// Index into the operator_codes array. Using an integer here avoids
// complicate map lookups.
opcode_index:int;
inputs:[int];
outputs:[int];
builtin_options:BuiltinOptions;
custom_options:[ubyte];
}
// The root type, defining a model.
table Model {
// A list of all tensors used in this model.
tensors:[Tensor];
// Indices of the input tensors.
inputs:[int];
// Indices of the output tensors.
outputs:[int];
// A list of all operator codes used in this model. This is
// kept in order because operators carry an index into this
// vector.
operator_codes:[OperatorCode];
// All operators, in execution order.
operators:[Operator];
}
root_type Model;
@@ -0,0 +1,295 @@
// Copyright 2017 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.
// Revision History
// Version 0: Initial version.
// Version 1: Add subgraphs to schema.
namespace tflite;
// The type of data stored in a tensor.
enum TensorType : byte {
FLOAT32 = 0,
FLOAT16 = 1,
INT32 = 2,
UINT8 = 3,
INT64 = 4,
STRING = 5,
}
// Parameters for converting a quantized tensor back to float. Given a
// quantized value q, the corresponding float value f should be:
// f = scale * (q - zero_point)
table QuantizationParameters {
min:[float]; // For importing back into tensorflow.
max:[float]; // For importing back into tensorflow.
scale:[float];
zero_point:[long];
}
table Tensor {
// The tensor shape. The meaning of each entry is operator-specific but
// builtin ops use: [batch size, height, width, number of channels] (That's
// Tensorflow's NHWC).
shape:[int];
type:TensorType;
// The data_buffer is an opaque container, with the assumption that the
// target device is little-endian. In addition, all builtin operators assume
// the memory is ordered such that if `shape` is [4, 3, 2], then index
// [i, j, k] maps to data_buffer[i*3*2 + j*3 + k].
data_buffer:[ubyte];
name:string; // For debugging and importing back into tensorflow.
quantization:QuantizationParameters; // Optional.
}
// A list of builtin operators. Builtin operators are slightly faster than custom
// ones, but not by much. Moreover, while custom operators accept an opaque
// object containing configuration parameters, builtins have a predetermined
// set of acceptable options.
enum BuiltinOperator : byte {
CUSTOM = 0,
CONVOLUTION = 1,
DEPTHWISE_CONVOLUTION = 2,
CONCAT_EMBEDDINGS = 3,
LSH_PROJECTION = 4,
TANH = 5,
RELU = 6,
AVERAGE_POOL = 7,
MAX_POOL = 8,
L2_POOL = 9,
SIGMOID = 10,
SVDF = 11,
BasicRNN = 12,
RELU6 = 13,
EMBEDDING_LOOKUP = 14,
FULLY_CONNECTED = 15,
HASHTABLE_LOOKUP = 16,
SOFTMAX = 17,
CONCATENATION = 18,
LSTM = 19,
ADD = 20,
L2NORM = 21,
LOCAL_RESPONSE_NORM = 22,
RESIZE_BILINEAR = 23,
CALL = 24,
RESHAPE = 25,
SKIP_GRAM = 26,
SPACE_TO_DEPTH = 27,
}
// Options for the builtin operators.
union BuiltinOptions {
ConvolutionOptions,
DepthwiseConvolutionOptions,
ConcatEmbeddingsOptions,
LSHProjectionOptions,
PoolOptions,
SVDFOptions,
BasicRNNOptions,
FullyConnectedOptions,
SoftmaxOptions,
ConcatenationOptions,
AddOptions,
L2NormOptions,
LocalResponseNormOptions,
LSTMOptions,
ResizeBilinearOptions,
CallOptions,
ReshapeOptions,
SkipGramOptions,
SpaceToDepthOptions,
}
enum Padding : byte { SAME, VALID }
enum ActivationFunctionType : byte {
NONE = 0,
RELU = 1,
RELU1 = 2,
RELU6 = 3,
TANH = 4,
SIGN_BIT = 5,
}
table ConvolutionOptions {
padding:Padding;
stride_w:int;
stride_h:int;
fused_activation_function:ActivationFunctionType;
}
table PoolOptions {
padding:Padding;
stride_w:int;
stride_h:int;
filter_width:int;
filter_height:int;
fused_activation_function:ActivationFunctionType;
}
table DepthwiseConvolutionOptions {
padding:Padding;
stride_w:int;
stride_h:int;
depth_multiplier:int;
fused_activation_function:ActivationFunctionType;
}
table ConcatEmbeddingsOptions {
num_channels:int;
num_columns_per_channel:[int];
embedding_dim_per_channel:[int]; // This could be inferred from parameters.
}
enum LSHProjectionType: byte {
UNKNOWN = 0,
SPARSE = 1,
DENSE = 2,
}
table LSHProjectionOptions {
type: LSHProjectionType;
}
table SVDFOptions {
rank:int;
fused_activation_function:ActivationFunctionType;
}
// An implementation of TensorFlow BasicRNNCell.
table BasicRNNOptions {
fused_activation_function:ActivationFunctionType;
}
// An implementation of TensorFlow fully_connected (a.k.a Dense) layer.
table FullyConnectedOptions {
fused_activation_function:ActivationFunctionType;
}
table SoftmaxOptions {
beta: float;
}
// An implementation of TensorFlow concat.
table ConcatenationOptions {
axis:int;
fused_activation_function:ActivationFunctionType;
}
table AddOptions {
fused_activation_function:ActivationFunctionType;
}
table L2NormOptions {
fused_activation_function:ActivationFunctionType;
}
table LocalResponseNormOptions {
radius:int;
bias:float;
alpha:float;
beta:float;
}
// An implementation of TensorFlow LSTMCell and CoupledInputForgetGateLSTMCell
table LSTMOptions {
fused_activation_function:ActivationFunctionType;
cell_clip: float; // Optional, 0.0 means no clipping
proj_clip: float; // Optional, 0.0 means no clipping
}
table ResizeBilinearOptions {
new_height:int;
new_width:int;
}
// A call operation options
table CallOptions {
// The subgraph index that needs to be called.
subgraph:int;
}
table ReshapeOptions {
new_shape:[int];
}
table SkipGramOptions {
ngram_size: int;
max_skip_size: int;
include_all_ngrams: bool;
}
table SpaceToDepthOptions {
block_size: int;
}
// An OperatorCode can be an enum value (BuiltinOperator) if the operator is a
// builtin, or a string if the operator is custom.
table OperatorCode {
builtin_code:BuiltinOperator;
custom_code:string;
}
// An operator takes tensors as inputs and outputs. The type of operation being
// performed is determined by an index into the list of valid OperatorCodes,
// while the specifics of each operations is configured using builtin_options
// or custom_options.
table Operator {
// Index into the operator_codes array. Using an integer here avoids
// complicate map lookups.
opcode_index:int;
inputs:[int];
outputs:[int];
builtin_options:BuiltinOptions;
custom_options:[ubyte];
}
// The root type, defining a model.
table SubGraph {
// A list of all tensors used in this model.
tensors:[Tensor];
// Indices of the input tensors.
inputs:[int];
// Indices of the output tensors.
outputs:[int];
// All operators, in execution order.
operators:[Operator];
// Name of subgraph (used for debugging).
name:string;
}
table Model {
// Version of the schema.
version:int;
// A list of all operator codes used in this model. This is
// kept in order because operators carry an index into this
// vector.
operator_codes:[OperatorCode];
// All the subgraphs of the model. The 0th is assumed to be the main
// model.
subgraphs:[SubGraph];
// A description of the model.
description:string;
}
root_type Model;
@@ -0,0 +1,302 @@
// Copyright 2017 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.
// Revision History
// Version 0: Initial version.
// Version 1: Add subgraphs to schema.
// Version 2: Rename operators to conform to NN API.
namespace tflite;
// The type of data stored in a tensor.
enum TensorType : byte {
FLOAT32 = 0,
FLOAT16 = 1,
INT32 = 2,
UINT8 = 3,
INT64 = 4,
STRING = 5,
}
// Parameters for converting a quantized tensor back to float. Given a
// quantized value q, the corresponding float value f should be:
// f = scale * (q - zero_point)
table QuantizationParameters {
min:[float]; // For importing back into tensorflow.
max:[float]; // For importing back into tensorflow.
scale:[float];
zero_point:[long];
}
table Tensor {
// The tensor shape. The meaning of each entry is operator-specific but
// builtin ops use: [batch size, height, width, number of channels] (That's
// Tensorflow's NHWC).
shape:[int];
type:TensorType;
// The data_buffer is an opaque container, with the assumption that the
// target device is little-endian. In addition, all builtin operators assume
// the memory is ordered such that if `shape` is [4, 3, 2], then index
// [i, j, k] maps to data_buffer[i*3*2 + j*3 + k].
data_buffer:[ubyte];
name:string; // For debugging and importing back into tensorflow.
quantization:QuantizationParameters; // Optional.
}
// A list of builtin operators. Builtin operators are slightly faster than custom
// ones, but not by much. Moreover, while custom operators accept an opaque
// object containing configuration parameters, builtins have a predetermined
// set of acceptable options.
enum BuiltinOperator : byte {
ADD = 0,
AVERAGE_POOL_2D = 1,
CONCATENATION = 2,
CONV_2D = 3,
DEPTHWISE_CONV_2D = 4,
// DEPTH_TO_SPACE = 5,
// DEQUANTIZE = 6,
EMBEDDING_LOOKUP = 7,
// FLOOR = 8,
FULLY_CONNECTED = 9,
HASHTABLE_LOOKUP = 10,
L2_NORMALIZATION = 11,
L2_POOL_2D = 12,
LOCAL_RESPONSE_NORMALIZATION = 13,
LOGISTIC = 14,
LSH_PROJECTION = 15,
LSTM = 16,
MAX_POOL_2D = 17,
// MUL = 18,
RELU = 19,
// RELU1=20,
RELU6 = 21,
RESHAPE = 22,
RESIZE_BILINEAR = 23,
RNN = 24,
SOFTMAX = 25,
SPACE_TO_DEPTH = 26,
SVDF = 27,
TANH = 28,
CONCAT_EMBEDDINGS = 29,
SKIP_GRAM = 30,
CALL = 31,
CUSTOM = 32,
}
// Options for the builtin operators.
union BuiltinOptions {
Conv2DOptions,
DepthwiseConv2DOptions,
ConcatEmbeddingsOptions,
LSHProjectionOptions,
Pool2DOptions,
SVDFOptions,
RNNOptions,
FullyConnectedOptions,
SoftmaxOptions,
ConcatenationOptions,
AddOptions,
L2NormOptions,
LocalResponseNormalizationOptions,
LSTMOptions,
ResizeBilinearOptions,
CallOptions,
ReshapeOptions,
SkipGramOptions,
SpaceToDepthOptions,
}
enum Padding : byte { SAME, VALID }
enum ActivationFunctionType : byte {
NONE = 0,
RELU = 1,
RELU1 = 2,
RELU6 = 3,
TANH = 4,
SIGN_BIT = 5,
}
table Conv2DOptions {
padding:Padding;
stride_w:int;
stride_h:int;
fused_activation_function:ActivationFunctionType;
}
table Pool2DOptions {
padding:Padding;
stride_w:int;
stride_h:int;
filter_width:int;
filter_height:int;
fused_activation_function:ActivationFunctionType;
}
table DepthwiseConv2DOptions {
padding:Padding;
stride_w:int;
stride_h:int;
depth_multiplier:int;
fused_activation_function:ActivationFunctionType;
}
table ConcatEmbeddingsOptions {
num_channels:int;
num_columns_per_channel:[int];
embedding_dim_per_channel:[int]; // This could be inferred from parameters.
}
enum LSHProjectionType: byte {
UNKNOWN = 0,
SPARSE = 1,
DENSE = 2,
}
table LSHProjectionOptions {
type: LSHProjectionType;
}
table SVDFOptions {
rank:int;
fused_activation_function:ActivationFunctionType;
}
// An implementation of TensorFlow RNNCell.
table RNNOptions {
fused_activation_function:ActivationFunctionType;
}
// An implementation of TensorFlow fully_connected (a.k.a Dense) layer.
table FullyConnectedOptions {
fused_activation_function:ActivationFunctionType;
}
table SoftmaxOptions {
beta: float;
}
// An implementation of TensorFlow concat.
table ConcatenationOptions {
axis:int;
fused_activation_function:ActivationFunctionType;
}
table AddOptions {
fused_activation_function:ActivationFunctionType;
}
table L2NormOptions {
fused_activation_function:ActivationFunctionType;
}
table LocalResponseNormalizationOptions {
radius:int;
bias:float;
alpha:float;
beta:float;
}
// An implementation of TensorFlow LSTMCell and CoupledInputForgetGateLSTMCell
table LSTMOptions {
fused_activation_function:ActivationFunctionType;
cell_clip: float; // Optional, 0.0 means no clipping
proj_clip: float; // Optional, 0.0 means no clipping
}
table ResizeBilinearOptions {
new_height:int;
new_width:int;
}
// A call operation options
table CallOptions {
// The subgraph index that needs to be called.
subgraph:int;
}
table ReshapeOptions {
new_shape:[int];
}
table SkipGramOptions {
ngram_size: int;
max_skip_size: int;
include_all_ngrams: bool;
}
table SpaceToDepthOptions {
block_size: int;
}
// An OperatorCode can be an enum value (BuiltinOperator) if the operator is a
// builtin, or a string if the operator is custom.
table OperatorCode {
builtin_code:BuiltinOperator;
custom_code:string;
}
// An operator takes tensors as inputs and outputs. The type of operation being
// performed is determined by an index into the list of valid OperatorCodes,
// while the specifics of each operations is configured using builtin_options
// or custom_options.
table Operator {
// Index into the operator_codes array. Using an integer here avoids
// complicate map lookups.
opcode_index:int;
inputs:[int];
outputs:[int];
builtin_options:BuiltinOptions;
custom_options:[ubyte];
}
// The root type, defining a model.
table SubGraph {
// A list of all tensors used in this model.
tensors:[Tensor];
// Indices of the input tensors.
inputs:[int];
// Indices of the output tensors.
outputs:[int];
// All operators, in execution order.
operators:[Operator];
// Name of subgraph (used for debugging).
name:string;
}
table Model {
// Version of the schema.
version:int;
// A list of all operator codes used in this model. This is
// kept in order because operators carry an index into this
// vector.
operator_codes:[OperatorCode];
// All the subgraphs of the model. The 0th is assumed to be the main
// model.
subgraphs:[SubGraph];
// A description of the model.
description:string;
}
root_type Model;
@@ -0,0 +1,326 @@
// Copyright 2017 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.
// Revision History
// Version 0: Initial version.
// Version 1: Add subgraphs to schema.
// Version 2: Rename operators to conform to NN API.
// Version 3: Move buffer data from Model.Subgraph.Tensors to Model.Buffers.
namespace tflite;
// This corresponds to the version (4).
file_identifier "TFL3";
// File extension of any written files.
file_extension "tflite";
// The type of data stored in a tensor.
enum TensorType : byte {
FLOAT32 = 0,
FLOAT16 = 1,
INT32 = 2,
UINT8 = 3,
INT64 = 4,
STRING = 5,
}
// Parameters for converting a quantized tensor back to float. Given a
// quantized value q, the corresponding float value f should be:
// f = scale * (q - zero_point)
table QuantizationParameters {
min:[float]; // For importing back into tensorflow.
max:[float]; // For importing back into tensorflow.
scale:[float];
zero_point:[long];
}
table Tensor {
// The tensor shape. The meaning of each entry is operator-specific but
// builtin ops use: [batch size, height, width, number of channels] (That's
// Tensorflow's NHWC).
shape:[int];
type:TensorType;
// An index that refers to the buffers table at the root of the model. Or,
// if there is no data buffer associated (i.e. intermediate results), then
// this is 0 (which refers to an always existent empty buffer).
//
// The data_buffer itself is an opaque container, with the assumption that the
// target device is little-endian. In addition, all builtin operators assume
// the memory is ordered such that if `shape` is [4, 3, 2], then index
// [i, j, k] maps to data_buffer[i*3*2 + j*3 + k].
buffer:uint;
name:string; // For debugging and importing back into tensorflow.
quantization:QuantizationParameters; // Optional.
}
// A list of builtin operators. Builtin operators are slightly faster than custom
// ones, but not by much. Moreover, while custom operators accept an opaque
// object containing configuration parameters, builtins have a predetermined
// set of acceptable options.
enum BuiltinOperator : byte {
ADD = 0,
AVERAGE_POOL_2D = 1,
CONCATENATION = 2,
CONV_2D = 3,
DEPTHWISE_CONV_2D = 4,
// DEPTH_TO_SPACE = 5,
// DEQUANTIZE = 6,
EMBEDDING_LOOKUP = 7,
// FLOOR = 8,
FULLY_CONNECTED = 9,
HASHTABLE_LOOKUP = 10,
L2_NORMALIZATION = 11,
L2_POOL_2D = 12,
LOCAL_RESPONSE_NORMALIZATION = 13,
LOGISTIC = 14,
LSH_PROJECTION = 15,
LSTM = 16,
MAX_POOL_2D = 17,
// MUL = 18,
RELU = 19,
// RELU1=20,
RELU6 = 21,
RESHAPE = 22,
RESIZE_BILINEAR = 23,
RNN = 24,
SOFTMAX = 25,
SPACE_TO_DEPTH = 26,
SVDF = 27,
TANH = 28,
// TODO(aselle): Consider rename to CONCATENATE_EMBEDDINGS
CONCAT_EMBEDDINGS = 29,
SKIP_GRAM = 30,
CALL = 31,
CUSTOM = 32,
}
// Options for the builtin operators.
union BuiltinOptions {
Conv2DOptions,
DepthwiseConv2DOptions,
ConcatEmbeddingsOptions,
LSHProjectionOptions,
Pool2DOptions,
SVDFOptions,
RNNOptions,
FullyConnectedOptions,
SoftmaxOptions,
ConcatenationOptions,
AddOptions,
L2NormOptions,
LocalResponseNormalizationOptions,
LSTMOptions,
ResizeBilinearOptions,
CallOptions,
ReshapeOptions,
SkipGramOptions,
SpaceToDepthOptions,
}
enum Padding : byte { SAME, VALID }
enum ActivationFunctionType : byte {
NONE = 0,
RELU = 1,
RELU1 = 2,
RELU6 = 3,
TANH = 4,
SIGN_BIT = 5,
}
table Conv2DOptions {
padding:Padding;
stride_w:int;
stride_h:int;
fused_activation_function:ActivationFunctionType;
}
table Pool2DOptions {
padding:Padding;
stride_w:int;
stride_h:int;
filter_width:int;
filter_height:int;
fused_activation_function:ActivationFunctionType;
}
table DepthwiseConv2DOptions {
padding:Padding;
stride_w:int;
stride_h:int;
depth_multiplier:int;
fused_activation_function:ActivationFunctionType;
}
table ConcatEmbeddingsOptions {
num_channels:int;
num_columns_per_channel:[int];
embedding_dim_per_channel:[int]; // This could be inferred from parameters.
}
enum LSHProjectionType: byte {
UNKNOWN = 0,
SPARSE = 1,
DENSE = 2,
}
table LSHProjectionOptions {
type: LSHProjectionType;
}
table SVDFOptions {
rank:int;
fused_activation_function:ActivationFunctionType;
}
// An implementation of TensorFlow RNNCell.
table RNNOptions {
fused_activation_function:ActivationFunctionType;
}
// An implementation of TensorFlow fully_connected (a.k.a Dense) layer.
table FullyConnectedOptions {
fused_activation_function:ActivationFunctionType;
}
table SoftmaxOptions {
beta: float;
}
// An implementation of TensorFlow concat.
table ConcatenationOptions {
axis:int;
fused_activation_function:ActivationFunctionType;
}
table AddOptions {
fused_activation_function:ActivationFunctionType;
}
table L2NormOptions {
fused_activation_function:ActivationFunctionType;
}
table LocalResponseNormalizationOptions {
radius:int;
bias:float;
alpha:float;
beta:float;
}
// An implementation of TensorFlow LSTMCell and CoupledInputForgetGateLSTMCell
table LSTMOptions {
fused_activation_function:ActivationFunctionType;
cell_clip: float; // Optional, 0.0 means no clipping
proj_clip: float; // Optional, 0.0 means no clipping
}
table ResizeBilinearOptions {
new_height:int;
new_width:int;
}
// A call operation options
table CallOptions {
// The subgraph index that needs to be called.
subgraph:uint;
}
table ReshapeOptions {
new_shape:[int];
}
table SkipGramOptions {
ngram_size: int;
max_skip_size: int;
include_all_ngrams: bool;
}
table SpaceToDepthOptions {
block_size: int;
}
// An OperatorCode can be an enum value (BuiltinOperator) if the operator is a
// builtin, or a string if the operator is custom.
table OperatorCode {
builtin_code:BuiltinOperator;
custom_code:string;
}
// An operator takes tensors as inputs and outputs. The type of operation being
// performed is determined by an index into the list of valid OperatorCodes,
// while the specifics of each operations is configured using builtin_options
// or custom_options.
table Operator {
// Index into the operator_codes array. Using an integer here avoids
// complicate map lookups.
opcode_index:uint;
inputs:[int];
outputs:[int];
builtin_options:BuiltinOptions;
custom_options:[ubyte];
}
// The root type, defining a model.
table SubGraph {
// A list of all tensors used in this model.
tensors:[Tensor];
// Indices of the input tensors.
inputs:[int];
// Indices of the output tensors.
outputs:[int];
// All operators, in execution order.
operators:[Operator];
// Name of subgraph (used for debugging).
name:string;
}
// Table of raw data buffers (used for constant tensors). Referenced by tensors
// by index.
table Buffer {
data:[ubyte];
}
table Model {
// Version of the schema.
version:uint;
// A list of all operator codes used in this model. This is
// kept in order because operators carry an index into this
// vector.
operator_codes:[OperatorCode];
// All the subgraphs of the model. The 0th is assumed to be the main
// model.
subgraphs:[SubGraph];
// A description of the model.
description:string;
// Buffers of the model.
// NOTE: It is required that the first entry in here is always an empty
// buffer. This is so that the default buffer index of zero in Tensor
// will always refer to a valid empty buffer.
buffers:[Buffer];
}
root_type Model;
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff