Stamp the version number for all metadata components simultaneously

PiperOrigin-RevId: 317349859
Change-Id: Ica912c3fb310889185c026e6d73ce4c69a9f0505
This commit is contained in:
Lu Wang 2020-06-19 11:50:15 -07:00 committed by TensorFlower Gardener
parent f805153a25
commit f53e1aac65
13 changed files with 267 additions and 21 deletions

View File

@ -1,5 +1,6 @@
load("//tensorflow:tensorflow.bzl", "py_test")
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(
default_visibility = [
@ -51,9 +52,19 @@ flatbuffer_android_library(
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(
name = "metadata",
srcs = ["metadata.py"],
srcs = [
"metadata.py",
":metadata_parser_py",
],
data = [
"//tensorflow/lite/experimental/support/metadata:metadata_schema.fbs",
],
@ -89,3 +100,14 @@ py_test(
"@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",
],
)

View 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,
)

View File

@ -1,12 +1,23 @@
load("//tensorflow/lite/experimental/support/metadata:build_defs.bzl", "stamp_metadata_parser_version")
package(
default_visibility = ["//tensorflow/lite/experimental/support:users"],
licenses = ["notice"], # Apache 2.0
)
stamp_metadata_parser_version(
name = "metadata_parser_h",
srcs = ["metadata_parser.h.template"],
outs = ["metadata_parser.h"],
)
cc_library(
name = "metadata_version",
srcs = ["metadata_version.cc"],
hdrs = ["metadata_version.h"],
hdrs = [
"metadata_version.h",
":metadata_parser_h",
],
deps = [
"//tensorflow/lite/c:common",
"//tensorflow/lite/experimental/support/metadata:metadata_schema_cc",

View File

@ -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_

View File

@ -13,3 +13,12 @@ cc_test(
"@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",
],
)

View File

@ -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

View File

@ -44,7 +44,7 @@ TEST(MetadataVersionTest,
builder.GetSize(), &min_version),
kTfLiteOk);
// 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,

View File

@ -9,9 +9,13 @@ package(
licenses = ["notice"], # Apache 2.0
)
METADATA_SRCS = glob(
["src/java/org/tensorflow/lite/support/metadata/**/*.java"],
)
android_library(
name = "tensorflow-lite-support-metadata",
srcs = glob(["src/java/org/tensorflow/lite/support/metadata/**/*.java"]),
srcs = METADATA_SRCS,
manifest = "AndroidManifest.xml",
deps = [
"//tensorflow/lite/experimental/support/metadata:metadata_schema_fbs_android",
@ -22,7 +26,7 @@ android_library(
java_library(
name = "tensorflow-lite-support-metadata-lib",
srcs = glob(["src/java/org/tensorflow/lite/support/metadata/**/*.java"]),
srcs = METADATA_SRCS,
javacopts = JAVACOPTS,
resource_jars = [
"//tensorflow/lite/experimental/support/metadata:libmetadata_schema_java.jar",

View File

@ -52,10 +52,6 @@ import org.tensorflow.lite.support.metadata.schema.TensorMetadata;
* MetadataExtractor} omits subgraph index as an input in its methods.
*/
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. */
private final ModelInfo modelInfo;
@ -85,7 +81,7 @@ public class MetadataExtractor {
System.err.printf(
"<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",
metadataInfo.getMininumParserVersion(), METADATA_PARSER_VERSION);
metadataInfo.getMininumParserVersion(), MetadataParser.VERSION);
}
checkArgument(
@ -290,7 +286,7 @@ public class MetadataExtractor {
if (minVersion == null) {
return true;
}
return compareVersions(minVersion, METADATA_PARSER_VERSION) <= 0;
return compareVersions(minVersion, MetadataParser.VERSION) <= 0;
}
/**

View File

@ -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() {}
}

View File

@ -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}"

View File

@ -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()

View File

@ -29,18 +29,31 @@ namespace tflite;
// generate the model interface. It is recommended to fill in at least those
// enties to boost the codegen performance.
// LINT.IfChange
// The Metadata schema is versioned by the Semantic versioning number, which
// tracks the schema changes according to the Semantic versioning rules.
// 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:
// * Bump up the MAJOR number when making potentially backwards incompatible
// 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
// 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
// which they were added.
// New fields and types will have associated comments with the schema version
// for which they were added.
//
// LINT.IfChange
// 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
// 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 "tflitemeta";
// LINT.ThenChange(//tensorflow/lite/experimental/\
// /support/metadata/java/src/java/org/tensorflow/lite/support/metadata/\
// MetadataExtractor.java)
// LINT.IfChange
enum AssociatedFileType : byte {
UNKNOWN = 0,