Skip to content

Commit

Permalink
Initial commit for Text CC APIs - BertQuestionAnswerer and NLClassifier
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 318162063
  • Loading branch information
tensorflower-gardener authored and tflite-support-robot committed Jul 7, 2020
1 parent 210ef2d commit 3960378
Show file tree
Hide file tree
Showing 62 changed files with 5,751 additions and 17 deletions.
125 changes: 110 additions & 15 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,35 @@ http_archive(
],
)

# Apple and Swift rules.
# https://github.com/bazelbuild/rules_apple/releases
http_archive(
name = "build_bazel_rules_apple",
sha256 = "ee9e6073aeb5a65c100cb9c44b0017c937706a4ae03176e14a7e78620a198079",
strip_prefix = "rules_apple-5131f3d46794bf227d296c82f30c2499c9de3c5b",
urls = [
"https://storage.googleapis.com/mirror.tensorflow.org/github.com/bazelbuild/rules_apple/archive/5131f3d46794bf227d296c82f30c2499c9de3c5b.tar.gz",
"https://github.com/bazelbuild/rules_apple/archive/5131f3d46794bf227d296c82f30c2499c9de3c5b.tar.gz",
],
)

# https://github.com/bazelbuild/rules_swift/releases
http_archive(
name = "build_bazel_rules_swift",
sha256 = "d0833bc6dad817a367936a5f902a0c11318160b5e80a20ece35fb85a5675c886",
strip_prefix = "rules_swift-3eeeb53cebda55b349d64c9fc144e18c5f7c0eb8",
urls = [
"https://storage.googleapis.com/mirror.tensorflow.org/github.com/bazelbuild/rules_swift/archive/3eeeb53cebda55b349d64c9fc144e18c5f7c0eb8.tar.gz",
"https://github.com/bazelbuild/rules_swift/archive/3eeeb53cebda55b349d64c9fc144e18c5f7c0eb8.tar.gz",
],
)

http_archive(
name = "org_tensorflow",
sha256 = "bb8b10da8184ce747f0348ea5b0d0aaf9e9bbe63cf68363d0e1bcdb72b4d3315",
strip_prefix = "tensorflow-5d49dc5526324443931a33cc84d66c8bcae9cea2",
sha256 = "972ec45352161e4308a1b203956eedfb56e22cc6ce4f4ec95f7b087aeb00559e",
strip_prefix = "tensorflow-2.3.0-rc0",
urls = [
"https://github.com/tensorflow/tensorflow/archive/5d49dc5526324443931a33cc84d66c8bcae9cea2.zip", # 2020-06-13
"https://github.com/tensorflow/tensorflow/archive/v2.3.0-rc0.tar.gz",
],
)

Expand Down Expand Up @@ -46,33 +69,105 @@ http_archive(
],
)

http_archive(
name = "six_archive",
build_file = "//third_party:six.BUILD",
sha256 = "d16a0141ec1a18405cd4ce8b4613101da75da0e9a7aec5bdd4fa804d0e0eba73",
strip_prefix = "six-1.12.0",
urls = [
"https://storage.googleapis.com/mirror.tensorflow.org/pypi.python.org/packages/source/s/six/six-1.12.0.tar.gz",
"https://pypi.python.org/packages/source/s/six/six-1.12.0.tar.gz",
],
)

http_archive(
name = "com_google_sentencepiece",
strip_prefix = "sentencepiece-1.0.0",
sha256 = "c05901f30a1d0ed64cbcf40eba08e48894e1b0e985777217b7c9036cac631346",
urls = [
"https://github.com/google/sentencepiece/archive/1.0.0.zip",
],
)

http_archive(
name = "org_tensorflow_text",
sha256 = "f64647276f7288d1b1fe4c89581d51404d0ce4ae97f2bcc4c19bd667549adca8",
strip_prefix = "text-2.2.0",
urls = [
"https://github.com/tensorflow/text/archive/v2.2.0.zip",
],
patches = ["@//third_party:tensorflow_text_fix_local_config_tf.patch"],
patch_args = ["-p1"],
repo_mapping = {"@com_google_re2": "@com_googlesource_code_re2"},
)

http_archive(
name = "com_googlesource_code_re2",
sha256 = "d070e2ffc5476c496a6a872a6f246bfddce8e7797d6ba605a7c8d72866743bf9",
strip_prefix = "re2-506cfa4bffd060c06ec338ce50ea3468daa6c814",
urls = [
"https://github.com/google/re2/archive/506cfa4bffd060c06ec338ce50ea3468daa6c814.tar.gz",
],
)

# ABSL cpp library lts_2020_02_25
# Needed for absl/status
http_archive(
name = "com_google_absl",
build_file = "//third_party:com_google_absl.BUILD",
sha256 = "f368a8476f4e2e0eccf8a7318b98dafbe30b2600f4e3cf52636e5eb145aba06a",
strip_prefix = "abseil-cpp-df3ea785d8c30a9503321a3d35ee7d35808f190d",
urls = [
"https://storage.googleapis.com/mirror.tensorflow.org/github.com/abseil/abseil-cpp/archive/df3ea785d8c30a9503321a3d35ee7d35808f190d.tar.gz",
"https://github.com/abseil/abseil-cpp/archive/df3ea785d8c30a9503321a3d35ee7d35808f190d.tar.gz",
"https://github.com/abseil/abseil-cpp/archive/20200225.tar.gz",
],
# Remove after https://github.com/abseil/abseil-cpp/issues/326 is solved.
patches = [
"@//third_party:com_google_absl_f863b622fe13612433fdf43f76547d5edda0c93001.diff"
],
patch_args = [
"-p1",
],
strip_prefix = "abseil-cpp-20200225",
sha256 = "728a813291bdec2aa46eab8356ace9f75ac2ed9dfe2df5ab603c4e6c09f1c353"
)

http_archive(
name = "six_archive",
build_file = "//third_party:six.BUILD",
sha256 = "d16a0141ec1a18405cd4ce8b4613101da75da0e9a7aec5bdd4fa804d0e0eba73",
strip_prefix = "six-1.12.0",
name = "com_google_glog",
sha256 = "1ee310e5d0a19b9d584a855000434bb724aa744745d5b8ab1855c85bff8a8e21",
strip_prefix = "glog-028d37889a1e80e8a07da1b8945ac706259e5fd8",
urls = [
"https://storage.googleapis.com/mirror.tensorflow.org/pypi.python.org/packages/source/s/six/six-1.12.0.tar.gz",
"https://pypi.python.org/packages/source/s/six/six-1.12.0.tar.gz",
"https://mirror.bazel.build/github.com/google/glog/archive/028d37889a1e80e8a07da1b8945ac706259e5fd8.tar.gz",
"https://github.com/google/glog/archive/028d37889a1e80e8a07da1b8945ac706259e5fd8.tar.gz",
],
)

load("//third_party/flatbuffers:workspace.bzl", flatbuffers = "repo")

flatbuffers()
http_archive(
name = "zlib",
build_file = "//third_party:zlib.BUILD",
sha256 = "c3e5e9fdd5004dcb542feda5ee4f0ff0744628baf8ed2dd5d66f8ca1197cb1a1",
strip_prefix = "zlib-1.2.11",
urls = [
"http://mirror.bazel.build/zlib.net/fossils/zlib-1.2.11.tar.gz",
"http://zlib.net/fossils/zlib-1.2.11.tar.gz", # 2017-01-15
],
)

http_archive(
name = "org_libzip",
build_file = "//third_party:libzip.BUILD",
sha256 = "a5d22f0c87a2625450eaa5e10db18b8ee4ef17042102d04c62e311993a2ba363",
strip_prefix = "libzip-rel-1-5-1",
urls = [
# Bazel does not like the official download link at libzip.org,
# so use the GitHub release tag.
"https://mirror.bazel.build/github.com/nih-at/libzip/archive/rel-1-5-1.zip",
"https://github.com/nih-at/libzip/archive/rel-1-5-1.zip",
],
)


load("//third_party/flatbuffers:workspace.bzl", flatbuffers = "repo")

flatbuffers()
# Set up TF.
load("@org_tensorflow//tensorflow:workspace.bzl", "tf_workspace")
tf_workspace(tf_repo_name="@org_tensorflow")
Expand Down
17 changes: 17 additions & 0 deletions tensorflow_lite_support/cc/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package(
default_visibility = ["//tensorflow_lite_support:users"],
licenses = ["notice"], # Apache 2.0
)

cc_library(
name = "common",
srcs = [
"common.cc",
],
hdrs = ["common.h"],
deps = [
"@com_google_absl//absl/status",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:cord",
],
)
35 changes: 35 additions & 0 deletions tensorflow_lite_support/cc/common.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/* 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_support/cc/common.h"

#include "absl/strings/cord.h"
#include "absl/strings/str_cat.h"

namespace tflite {
namespace support {

absl::Status CreateStatusWithPayload(absl::StatusCode canonical_code,
absl::string_view message,
TfLiteSupportStatus tfls_code) {
// NOTE: Ignores `message` if the canonical code is ok.
absl::Status status = absl::Status(canonical_code, message);
// NOTE: Does nothing if the canonical code is ok.
status.SetPayload(kTfLiteSupportPayload, absl::Cord(absl::StrCat(tfls_code)));
return status;
}

} // namespace support
} // namespace tflite
163 changes: 163 additions & 0 deletions tensorflow_lite_support/cc/common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/* 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_SUPPORT_CC_COMMON_H_
#define TENSORFLOW_LITE_SUPPORT_CC_COMMON_H_

#include "absl/status/status.h"
#include "absl/strings/string_view.h"

namespace tflite {
namespace support {

// Name (aka type URL key) of the `absl::Status` payload which contains a
// stringified `TfLiteSupportStatus` code (see below).
inline constexpr absl::string_view kTfLiteSupportPayload =
"tflite::support::TfLiteSupportStatus";

// Error codes for TensorFlow Lite Support (TFLS) C++ APIs.
//
// Such codes capture errors encountered in the TFLS layer. They complement all
// the other type of errors that occur in the lower-level TF Lite codebase (see
// `TfLiteStatus` codes).
//
// At runtime, such codes are meant to be attached (where applicable) to a
// `absl::Status` in a key-value manner with `kTfLiteSupportPayload` as key and
// stringifed error code as value (aka payload). This logic is encapsulated in
// the `CreateStatusWithPayload` helper below for convenience.
//
// The returned status includes:
// 1. The canonical error code (INVALID_ARGUMENT)
// 2. The fine-grained error message ("Invalid metadata ...")
// 3. The specific TFLS code as a payload (kMetadataInvalidSchemaVersionError)
enum class TfLiteSupportStatus {
// Generic error codes.

// Success.
kOk = 0,
// Unspecified error.
kError = 1,
// Invalid argument specified.
kInvalidArgumentError = 2,
// Invalid FlatBuffer file or buffer specified.
kInvalidFlatBufferError = 3,

// File I/O error codes.

// No such file.
kFileNotFoundError = 100,
// Permission issue.
kFilePermissionDeniedError,
// I/O error when reading file.
kFileReadError,
// I/O error when mmap-ing file.
kFileMmapError,

// TensorFlow Lite metadata error codes.

// Unexpected schema version (aka file_identifier) in the Metadata FlatBuffer.
kMetadataInvalidSchemaVersionError = 200,
// No such associated file within metadata, or file has not been packed.
kMetadataAssociatedFileNotFoundError,
// ZIP I/O error when unpacking an associated file.
kMetadataAssociatedFileZipError,
// Inconsistency error between the metadata and actual TF Lite model.
// E.g.: number of labels and output tensor values differ.
kMetadataInconsistencyError,
// Invalid process units specified.
// E.g.: multiple ProcessUnits with the same type for a given tensor.
kMetadataInvalidProcessUnitsError,
// Inconsistency error with the number of labels.
// E.g.: label files for different locales have a different number of labels.
kMetadataNumLabelsMismatchError,
// Score calibration parameters parsing error.
// E.g.: too many parameters provided in the corresponding associated file.
kMetadataMalformedScoreCalibrationError,
// Unexpected number of subgraphs for the current task.
// E.g.: image classification expects a single subgraph.
kMetadataInvalidNumSubgraphsError,
// A given tensor requires NormalizationOptions but none were found.
// E.g.: float input tensor requires normalization to preprocess input images.
kMetadataMissingNormalizationOptionsError,
// Invalid ContentProperties specified.
// E.g. expected ImageProperties, got BoundingBoxProperties.
kMetadataInvalidContentPropertiesError,
// Metadata is mandatory but was not found.
// E.g. current task requires TFLite Model Metadata but none was found.
kMetadataNotFoundError,
// Associated TENSOR_AXIS_LABELS or TENSOR_VALUE_LABELS file is mandatory but
// none was found or it was empty.
// E.g. current task requires labels but none were found.
kMetadataMissingLabelsError,

// Input tensor(s) error codes.

// Unexpected number of input tensors for the current task.
// E.g. current task expects a single input tensor.
kInvalidNumInputTensorsError = 300,
// Unexpected input tensor dimensions for the current task.
// E.g.: only 4D input tensors supported.
kInvalidInputTensorDimensionsError,
// Unexpected input tensor type for the current task.
// E.g.: current task expects a uint8 pixel image as input.
kInvalidInputTensorTypeError,
// Unexpected input tensor bytes size.
// E.g.: size in bytes does not correspond to the expected number of pixels.
kInvalidInputTensorSizeError,
// No correct input tensor found for the model.
// E.g.: input tensor name is not part of the text model's input tensors.
kInputTensorNotFoundError,

// Output tensor(s) error codes.

// Unexpected output tensor dimensions for the current task.
// E.g.: only a batch size of 1 is supported.
kInvalidOutputTensorDimensionsError = 400,
// Unexpected input tensor type for the current task.
// E.g.: multi-head model with different output tensor types.
kInvalidOutputTensorTypeError,
// No correct output tensor found for the model.
// E.g.: output tensor name is not part of the text model's output tensors.
kOutputTensorNotFoundError,
// Unexpected number of output tensors for the current task.
// E.g.: current task expects a single output tensor.
kInvalidNumOutputTensorsError,

// Image processing error codes.

// Unspecified image processing failures.
kImageProcessingError = 500,
// Unexpected input or output buffer metadata.
// E.g.: rotate RGBA buffer to Grayscale buffer by 90 degrees.
kImageProcessingInvalidArgumentError,
// Image processing operation failures.
// E.g. libyuv rotation failed for an unknown reason.
kImageProcessingBackendError,
};

// Convenience helper to create an `absl::Status` augmented with the
// fine-grained `tfls_code` attached as payload under the
// `kTfLiteSupportPayload` type URL key.
//
// This should only be used for non-ok codes since otherwise it does nothing
// more than returning an object identical to an OK status. See `absl::Status`
// for more details.
absl::Status CreateStatusWithPayload(
absl::StatusCode canonical_code, absl::string_view message,
TfLiteSupportStatus tfls_code = TfLiteSupportStatus::kError);

} // namespace support
} // namespace tflite
#endif // TENSORFLOW_LITE_SUPPORT_CC_COMMON_H_

0 comments on commit 3960378

Please sign in to comment.