Stamp the version number for all metadata components simultaneously
PiperOrigin-RevId: 317349859 Change-Id: Ica912c3fb310889185c026e6d73ce4c69a9f0505
This commit is contained in:
parent
f805153a25
commit
f53e1aac65
@ -1,5 +1,6 @@
|
|||||||
load("//tensorflow:tensorflow.bzl", "py_test")
|
load("//tensorflow:tensorflow.bzl", "py_test")
|
||||||
load("@flatbuffers//:build_defs.bzl", "flatbuffer_android_library", "flatbuffer_cc_library", "flatbuffer_java_library", "flatbuffer_py_library")
|
load("@flatbuffers//:build_defs.bzl", "flatbuffer_android_library", "flatbuffer_cc_library", "flatbuffer_java_library", "flatbuffer_py_library")
|
||||||
|
load("//tensorflow/lite/experimental/support/metadata:build_defs.bzl", "stamp_metadata_parser_version")
|
||||||
|
|
||||||
package(
|
package(
|
||||||
default_visibility = [
|
default_visibility = [
|
||||||
@ -51,9 +52,19 @@ flatbuffer_android_library(
|
|||||||
custom_package = "org.tensorflow.lite.support.metadata.schema",
|
custom_package = "org.tensorflow.lite.support.metadata.schema",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# TODO(b/157813075): move the metadata python library to metadata/python/ when migrating to the new repo.
|
||||||
|
stamp_metadata_parser_version(
|
||||||
|
name = "metadata_parser_py",
|
||||||
|
srcs = ["metadata_parser.py.template"],
|
||||||
|
outs = ["metadata_parser.py"],
|
||||||
|
)
|
||||||
|
|
||||||
py_library(
|
py_library(
|
||||||
name = "metadata",
|
name = "metadata",
|
||||||
srcs = ["metadata.py"],
|
srcs = [
|
||||||
|
"metadata.py",
|
||||||
|
":metadata_parser_py",
|
||||||
|
],
|
||||||
data = [
|
data = [
|
||||||
"//tensorflow/lite/experimental/support/metadata:metadata_schema.fbs",
|
"//tensorflow/lite/experimental/support/metadata:metadata_schema.fbs",
|
||||||
],
|
],
|
||||||
@ -89,3 +100,14 @@ py_test(
|
|||||||
"@six_archive//:six",
|
"@six_archive//:six",
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
py_test(
|
||||||
|
name = "metadata_parser_test",
|
||||||
|
srcs = ["metadata_parser_test.py"],
|
||||||
|
python_version = "PY3",
|
||||||
|
srcs_version = "PY2AND3",
|
||||||
|
deps = [
|
||||||
|
":metadata",
|
||||||
|
"//tensorflow/python:client_testlib",
|
||||||
|
],
|
||||||
|
)
|
||||||
|
43
tensorflow/lite/experimental/support/metadata/build_defs.bzl
Normal file
43
tensorflow/lite/experimental/support/metadata/build_defs.bzl
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
"""Build rules to generate metadata schema versions."""
|
||||||
|
|
||||||
|
METADATA_SCHEMA_FILE = "//tensorflow/lite/experimental/support/metadata:metadata_schema.fbs"
|
||||||
|
|
||||||
|
def stamp_metadata_parser_version(
|
||||||
|
name,
|
||||||
|
srcs,
|
||||||
|
outs):
|
||||||
|
"""Stamps the latest metadata parser version into the srcs files.
|
||||||
|
|
||||||
|
Replaces all the occurrences of "{LATEST_METADATA_PARSER_VERSION}" in the
|
||||||
|
srcs files with the metadata schema version extracted from
|
||||||
|
METADATA_SCHEMA_FILE and then outputs the generated file into outs,
|
||||||
|
respectively. The number of srcs files needs to match the number of outs
|
||||||
|
files.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
name: Rule name. (required)
|
||||||
|
srcs: List of source files. (required)
|
||||||
|
outs: List of output files. (required)
|
||||||
|
"""
|
||||||
|
if len(srcs) != len(outs):
|
||||||
|
fail(("The number of srcs files (%d) does not match that of the outs" +
|
||||||
|
" files (%d).") %
|
||||||
|
(len(srcs), len(outs)))
|
||||||
|
|
||||||
|
for i in range(0, len(srcs)):
|
||||||
|
native.genrule(
|
||||||
|
name = "%s_file%d" % (name, i),
|
||||||
|
srcs = [srcs[i]],
|
||||||
|
outs = [outs[i]],
|
||||||
|
tools = [METADATA_SCHEMA_FILE],
|
||||||
|
# Gets the metadata schema version from the file, and stamps it
|
||||||
|
# into the srcs file.
|
||||||
|
cmd = "version=$$(sed -n -e '/Schema Semantic version/ s/.*\\: *//p' $(location %s));" %
|
||||||
|
METADATA_SCHEMA_FILE +
|
||||||
|
'sed "s/{LATEST_METADATA_PARSER_VERSION}/$$version/" $< > $@',
|
||||||
|
)
|
||||||
|
|
||||||
|
native.filegroup(
|
||||||
|
name = name,
|
||||||
|
srcs = outs,
|
||||||
|
)
|
@ -1,12 +1,23 @@
|
|||||||
|
load("//tensorflow/lite/experimental/support/metadata:build_defs.bzl", "stamp_metadata_parser_version")
|
||||||
|
|
||||||
package(
|
package(
|
||||||
default_visibility = ["//tensorflow/lite/experimental/support:users"],
|
default_visibility = ["//tensorflow/lite/experimental/support:users"],
|
||||||
licenses = ["notice"], # Apache 2.0
|
licenses = ["notice"], # Apache 2.0
|
||||||
)
|
)
|
||||||
|
|
||||||
|
stamp_metadata_parser_version(
|
||||||
|
name = "metadata_parser_h",
|
||||||
|
srcs = ["metadata_parser.h.template"],
|
||||||
|
outs = ["metadata_parser.h"],
|
||||||
|
)
|
||||||
|
|
||||||
cc_library(
|
cc_library(
|
||||||
name = "metadata_version",
|
name = "metadata_version",
|
||||||
srcs = ["metadata_version.cc"],
|
srcs = ["metadata_version.cc"],
|
||||||
hdrs = ["metadata_version.h"],
|
hdrs = [
|
||||||
|
"metadata_version.h",
|
||||||
|
":metadata_parser_h",
|
||||||
|
],
|
||||||
deps = [
|
deps = [
|
||||||
"//tensorflow/lite/c:common",
|
"//tensorflow/lite/c:common",
|
||||||
"//tensorflow/lite/experimental/support/metadata:metadata_schema_cc",
|
"//tensorflow/lite/experimental/support/metadata:metadata_schema_cc",
|
||||||
|
@ -0,0 +1,28 @@
|
|||||||
|
/* 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_LITE_EXPERIMENTAL_SUPPORT_METADATA_CC_METADATA_PARSER_H_
|
||||||
|
#define TENSORFLOW_LITE_EXPERIMENTAL_SUPPORT_METADATA_CC_METADATA_PARSER_H_
|
||||||
|
|
||||||
|
namespace tflite {
|
||||||
|
namespace metadata {
|
||||||
|
|
||||||
|
// The version of the metadata parser that this metadata versioning library is
|
||||||
|
// depending on.
|
||||||
|
inline constexpr char kMatadataParserVersion[] = "{LATEST_METADATA_PARSER_VERSION}";
|
||||||
|
|
||||||
|
} // namespace metadata
|
||||||
|
} // namespace tflite
|
||||||
|
|
||||||
|
#endif // TENSORFLOW_LITE_EXPERIMENTAL_SUPPORT_METADATA_CC_METADATA_PARSER_H_
|
@ -13,3 +13,12 @@ cc_test(
|
|||||||
"@flatbuffers",
|
"@flatbuffers",
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
cc_test(
|
||||||
|
name = "metadata_parser_test",
|
||||||
|
srcs = ["metadata_parser_test.cc"],
|
||||||
|
deps = [
|
||||||
|
"//tensorflow/lite/experimental/support/metadata/cc:metadata_version",
|
||||||
|
"@com_google_googletest//:gtest_main",
|
||||||
|
],
|
||||||
|
)
|
||||||
|
@ -0,0 +1,33 @@
|
|||||||
|
/* 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/lite/experimental/support/metadata/cc/metadata_parser.h"
|
||||||
|
|
||||||
|
#include <gmock/gmock.h>
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
|
namespace tflite {
|
||||||
|
namespace metadata {
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
using ::testing::MatchesRegex;
|
||||||
|
|
||||||
|
TEST(MetadataParserTest, MatadataParserVersionIsWellFormed) {
|
||||||
|
// Validates that the version is well-formed (x.y.z).
|
||||||
|
EXPECT_THAT(kMatadataParserVersion, MatchesRegex("[0-9]+\\.[0-9]+\\.[0-9]+"));
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
} // namespace metadata
|
||||||
|
} // namespace tflite
|
@ -44,7 +44,7 @@ TEST(MetadataVersionTest,
|
|||||||
builder.GetSize(), &min_version),
|
builder.GetSize(), &min_version),
|
||||||
kTfLiteOk);
|
kTfLiteOk);
|
||||||
// Validates that the version is well-formed (x.y.z).
|
// Validates that the version is well-formed (x.y.z).
|
||||||
EXPECT_THAT(min_version, MatchesRegex("[0-9]*\\.[0-9]*\\.[0-9]"));
|
EXPECT_THAT(min_version, MatchesRegex("[0-9]+\\.[0-9]+\\.[0-9]+"));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(MetadataVersionTest,
|
TEST(MetadataVersionTest,
|
||||||
|
@ -9,9 +9,13 @@ package(
|
|||||||
licenses = ["notice"], # Apache 2.0
|
licenses = ["notice"], # Apache 2.0
|
||||||
)
|
)
|
||||||
|
|
||||||
|
METADATA_SRCS = glob(
|
||||||
|
["src/java/org/tensorflow/lite/support/metadata/**/*.java"],
|
||||||
|
)
|
||||||
|
|
||||||
android_library(
|
android_library(
|
||||||
name = "tensorflow-lite-support-metadata",
|
name = "tensorflow-lite-support-metadata",
|
||||||
srcs = glob(["src/java/org/tensorflow/lite/support/metadata/**/*.java"]),
|
srcs = METADATA_SRCS,
|
||||||
manifest = "AndroidManifest.xml",
|
manifest = "AndroidManifest.xml",
|
||||||
deps = [
|
deps = [
|
||||||
"//tensorflow/lite/experimental/support/metadata:metadata_schema_fbs_android",
|
"//tensorflow/lite/experimental/support/metadata:metadata_schema_fbs_android",
|
||||||
@ -22,7 +26,7 @@ android_library(
|
|||||||
|
|
||||||
java_library(
|
java_library(
|
||||||
name = "tensorflow-lite-support-metadata-lib",
|
name = "tensorflow-lite-support-metadata-lib",
|
||||||
srcs = glob(["src/java/org/tensorflow/lite/support/metadata/**/*.java"]),
|
srcs = METADATA_SRCS,
|
||||||
javacopts = JAVACOPTS,
|
javacopts = JAVACOPTS,
|
||||||
resource_jars = [
|
resource_jars = [
|
||||||
"//tensorflow/lite/experimental/support/metadata:libmetadata_schema_java.jar",
|
"//tensorflow/lite/experimental/support/metadata:libmetadata_schema_java.jar",
|
||||||
|
@ -52,10 +52,6 @@ import org.tensorflow.lite.support.metadata.schema.TensorMetadata;
|
|||||||
* MetadataExtractor} omits subgraph index as an input in its methods.
|
* MetadataExtractor} omits subgraph index as an input in its methods.
|
||||||
*/
|
*/
|
||||||
public class MetadataExtractor {
|
public class MetadataExtractor {
|
||||||
// TODO(b/156539454): remove the hardcode versioning number and populate the version through
|
|
||||||
// genrule.
|
|
||||||
/** The version of the metadata parser that this {@link MetadataExtractor} library depends on. */
|
|
||||||
public static final String METADATA_PARSER_VERSION = "1.0.1";
|
|
||||||
|
|
||||||
/** The helper class to load metadata from TFLite model FlatBuffer. */
|
/** The helper class to load metadata from TFLite model FlatBuffer. */
|
||||||
private final ModelInfo modelInfo;
|
private final ModelInfo modelInfo;
|
||||||
@ -85,7 +81,7 @@ public class MetadataExtractor {
|
|||||||
System.err.printf(
|
System.err.printf(
|
||||||
"<Warning> Some fields in the metadata belong to a future schema. The minimum parser"
|
"<Warning> Some fields in the metadata belong to a future schema. The minimum parser"
|
||||||
+ " version required is %s, but the version of the current metadata parser is %s",
|
+ " version required is %s, but the version of the current metadata parser is %s",
|
||||||
metadataInfo.getMininumParserVersion(), METADATA_PARSER_VERSION);
|
metadataInfo.getMininumParserVersion(), MetadataParser.VERSION);
|
||||||
}
|
}
|
||||||
|
|
||||||
checkArgument(
|
checkArgument(
|
||||||
@ -290,7 +286,7 @@ public class MetadataExtractor {
|
|||||||
if (minVersion == null) {
|
if (minVersion == null) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return compareVersions(minVersion, METADATA_PARSER_VERSION) <= 0;
|
return compareVersions(minVersion, MetadataParser.VERSION) <= 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -0,0 +1,27 @@
|
|||||||
|
/* 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.
|
||||||
|
==============================================================================*/
|
||||||
|
|
||||||
|
package org.tensorflow.lite.support.metadata;
|
||||||
|
|
||||||
|
/** Information about the metadata parser that this metadata extractor library is depending on. */
|
||||||
|
public final class MetadataParser {
|
||||||
|
/**
|
||||||
|
* The version of the metadata parser that this metadata extractor library is depending on. The
|
||||||
|
* value should match the value of "Schema Semantic version" in metadata_schema.fbs.
|
||||||
|
*/
|
||||||
|
public static final String VERSION = "1.0.1";
|
||||||
|
|
||||||
|
private MetadataParser() {}
|
||||||
|
}
|
@ -0,0 +1,26 @@
|
|||||||
|
# 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.
|
||||||
|
# ==============================================================================
|
||||||
|
"""Information about the metadata parser that this python library depends on."""
|
||||||
|
|
||||||
|
from __future__ import absolute_import
|
||||||
|
from __future__ import division
|
||||||
|
from __future__ import print_function
|
||||||
|
|
||||||
|
|
||||||
|
class MetadataParser(object):
|
||||||
|
"""Information about the metadata parser."""
|
||||||
|
|
||||||
|
# The version of the metadata parser.
|
||||||
|
VERSION = "{LATEST_METADATA_PARSER_VERSION}"
|
@ -0,0 +1,38 @@
|
|||||||
|
# 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.
|
||||||
|
# ==============================================================================
|
||||||
|
"""Tests for tensorflow.lite.experimental.support.metadata.metadata_parser."""
|
||||||
|
|
||||||
|
from __future__ import absolute_import
|
||||||
|
from __future__ import division
|
||||||
|
from __future__ import print_function
|
||||||
|
|
||||||
|
import re
|
||||||
|
|
||||||
|
from tensorflow.lite.experimental.support.metadata import metadata_parser
|
||||||
|
from tensorflow.python.framework import test_util
|
||||||
|
from tensorflow.python.platform import test
|
||||||
|
|
||||||
|
|
||||||
|
class MetadataParserTest(test_util.TensorFlowTestCase):
|
||||||
|
|
||||||
|
def test_version_wellFormedSemanticVersion(self):
|
||||||
|
# Validates that the version is well-formed (x.y.z).
|
||||||
|
self.assertTrue(
|
||||||
|
re.match('[0-9]+\\.[0-9]+\\.[0-9]+',
|
||||||
|
metadata_parser.MetadataParser.VERSION))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
test.main()
|
@ -29,18 +29,31 @@ namespace tflite;
|
|||||||
// generate the model interface. It is recommended to fill in at least those
|
// generate the model interface. It is recommended to fill in at least those
|
||||||
// enties to boost the codegen performance.
|
// enties to boost the codegen performance.
|
||||||
|
|
||||||
// LINT.IfChange
|
// The Metadata schema is versioned by the Semantic versioning number, such as
|
||||||
|
// MAJOR.MINOR.PATCH. It tracks the schema changes according to the rules below:
|
||||||
// The Metadata schema is versioned by the Semantic versioning number, which
|
// * Bump up the MAJOR number when making potentially backwards incompatible
|
||||||
// tracks the schema changes according to the Semantic versioning rules.
|
// changes. It must be incremented if the new changes break the backwards
|
||||||
|
// compatibility. It may also include minor and patch level changes as
|
||||||
|
// needed. The true backwards compatibility is indicated by the file
|
||||||
|
// identifier.
|
||||||
|
// * Bump up the MINOR number when making backwards compatible updates for
|
||||||
|
// major features, such as supporting new content types or adding new
|
||||||
|
// processing units.
|
||||||
|
// * Bump up the PATCH number when making small backwards compatible changes,
|
||||||
|
// such as adding a new fields or deprecating certain fields (not deleting
|
||||||
|
// them).
|
||||||
//
|
//
|
||||||
// ModelMetadata.min_parser_version indicates the minimum necessary metadata
|
// ModelMetadata.min_parser_version indicates the minimum necessary metadata
|
||||||
// parser version to fully understand all fields in a given metadata flatbuffer.
|
// parser version to fully understand all fields in a given metadata flatbuffer.
|
||||||
//
|
//
|
||||||
// New fields and types will have associated comments with the schema version for
|
// New fields and types will have associated comments with the schema version
|
||||||
// which they were added.
|
// for which they were added.
|
||||||
//
|
//
|
||||||
|
// LINT.IfChange
|
||||||
// Schema Semantic version: 1.0.1
|
// Schema Semantic version: 1.0.1
|
||||||
|
// LINT.ThenChange(//tensorflow/lite/experimental/\
|
||||||
|
//. support/metadata/java/src/java/org/tensorflow/lite/support/metadata/\
|
||||||
|
//. MetadataParser.java)
|
||||||
|
|
||||||
// This indicates the flatbuffer compatibility. The number will bump up when a
|
// This indicates the flatbuffer compatibility. The number will bump up when a
|
||||||
// break change is applied to the schema, such as removing fields or adding new
|
// break change is applied to the schema, such as removing fields or adding new
|
||||||
@ -53,10 +66,6 @@ file_identifier "M001";
|
|||||||
// File extension of any written files.
|
// File extension of any written files.
|
||||||
file_extension "tflitemeta";
|
file_extension "tflitemeta";
|
||||||
|
|
||||||
// LINT.ThenChange(//tensorflow/lite/experimental/\
|
|
||||||
// /support/metadata/java/src/java/org/tensorflow/lite/support/metadata/\
|
|
||||||
// MetadataExtractor.java)
|
|
||||||
|
|
||||||
// LINT.IfChange
|
// LINT.IfChange
|
||||||
enum AssociatedFileType : byte {
|
enum AssociatedFileType : byte {
|
||||||
UNKNOWN = 0,
|
UNKNOWN = 0,
|
||||||
|
Loading…
Reference in New Issue
Block a user