Remove all uses of TENSORFLOW_LITE_PROTOS.

PiperOrigin-RevId: 310116123
Change-Id: I5fa28cc61644efc2fd202e80997a5c4c0a227572
This commit is contained in:
A. Unique TensorFlower 2020-05-06 02:40:04 -07:00 committed by TensorFlower Gardener
parent 2323679d9e
commit fedc6d951f
21 changed files with 136 additions and 175 deletions

View File

@ -600,7 +600,6 @@ cc_library(
":error_util",
":parse_text_proto",
"//tensorflow/core:lib",
"//tensorflow/core:protos_all_cc",
"@com_google_absl//absl/strings",
"@llvm-project//llvm:support",
],

View File

@ -31,12 +31,17 @@ inline llvm::StringRef StringViewToRef(absl::string_view view) {
}
} // namespace
Status LoadProtoFromBuffer(absl::string_view input,
protobuf::MessageLite* proto) {
Status LoadProtoFromBuffer(absl::string_view input, protobuf::Message* proto) {
// Attempt to parse as text.
if (ParseTextProto(input, "", proto).ok()) return Status::OK();
// Else attempt to parse as binary.
return LoadProtoFromBuffer(input, static_cast<protobuf::MessageLite*>(proto));
}
Status LoadProtoFromBuffer(absl::string_view input,
protobuf::MessageLite* proto) {
// Attempt to parse as binary.
protobuf::io::ArrayInputStream binary_stream(input.data(), input.size());
if (proto->ParseFromZeroCopyStream(&binary_stream)) return Status::OK();
@ -44,8 +49,8 @@ Status LoadProtoFromBuffer(absl::string_view input,
return errors::InvalidArgument("Could not parse input proto");
}
Status LoadProtoFromFile(absl::string_view input_filename,
protobuf::MessageLite* proto) {
template <class T>
Status LoadProtoFromFileImpl(absl::string_view input_filename, T* proto) {
const auto file_or_err =
llvm::MemoryBuffer::getFileOrSTDIN(StringViewToRef(input_filename));
if (std::error_code error = file_or_err.getError()) {
@ -60,4 +65,14 @@ Status LoadProtoFromFile(absl::string_view input_filename,
return LoadProtoFromBuffer(content, proto);
}
Status LoadProtoFromFile(absl::string_view input_filename,
protobuf::Message* proto) {
return LoadProtoFromFileImpl(input_filename, proto);
}
Status LoadProtoFromFile(absl::string_view input_filename,
protobuf::MessageLite* proto) {
return LoadProtoFromFileImpl(input_filename, proto);
}
} // namespace tensorflow

View File

@ -24,13 +24,20 @@ namespace tensorflow {
// Reads text (.pbtext) or binary (.pb) format of a proto message from the given
// buffer. Returns error status of the file is not found or malformed proto.
// Note that text protos can only be parsed when full protobuf::Message protos
// are used, and will fail for protobuf::MessageLite protos.
Status LoadProtoFromBuffer(absl::string_view input, protobuf::Message* proto);
Status LoadProtoFromBuffer(absl::string_view input,
tensorflow::protobuf::MessageLite* proto);
protobuf::MessageLite* proto);
// Reads text (.pbtext) or binary (.pb) format of a proto message from the given
// file path. Returns error status of the file is not found or malformed proto.
// Note that text protos can only be parsed when full protobuf::Message protos
// are used, and will fail for protobuf::MessageLite protos.
Status LoadProtoFromFile(absl::string_view input_filename,
tensorflow::protobuf::MessageLite* proto);
protobuf::Message* proto);
Status LoadProtoFromFile(absl::string_view input_filename,
protobuf::MessageLite* proto);
} // namespace tensorflow

View File

@ -24,7 +24,6 @@ limitations under the License.
namespace tensorflow {
#ifndef TENSORFLOW_LITE_PROTOS
namespace {
// Error collector that simply ignores errors reported.
class NoOpErrorCollector : public protobuf::io::ErrorCollector {
@ -32,7 +31,6 @@ class NoOpErrorCollector : public protobuf::io::ErrorCollector {
void AddError(int line, int column, const std::string& message) override {}
};
} // namespace
#endif // TENSORFLOW_LITE_PROTOS
Status ConsumePrefix(absl::string_view str, absl::string_view prefix,
absl::string_view* output) {
@ -45,8 +43,7 @@ Status ConsumePrefix(absl::string_view str, absl::string_view prefix,
Status ParseTextProto(absl::string_view text_proto,
absl::string_view prefix_to_strip,
protobuf::MessageLite* parsed_proto) {
#ifndef TENSORFLOW_LITE_PROTOS
protobuf::Message* parsed_proto) {
protobuf::TextFormat::Parser parser;
// Don't produce errors when attempting to parse text format as it would fail
// when the input is actually a binary file.
@ -60,15 +57,11 @@ Status ParseTextProto(absl::string_view text_proto,
}
protobuf::io::ArrayInputStream input_stream(text_proto_without_prefix.data(),
text_proto_without_prefix.size());
if (parser.Parse(&input_stream,
tensorflow::down_cast<protobuf::Message*>(parsed_proto))) {
if (parser.Parse(&input_stream, parsed_proto)) {
return Status::OK();
}
parsed_proto->Clear();
return errors::InvalidArgument("Could not parse text proto: ", text_proto);
#else
return errors::Unavailable("Cannot parse text protos on mobile.");
#endif // TENSORFLOW_LITE_PROTOS
}
} // namespace tensorflow

View File

@ -32,7 +32,12 @@ Status ConsumePrefix(absl::string_view str, absl::string_view prefix,
// proto.
Status ParseTextProto(absl::string_view text_proto,
absl::string_view prefix_to_strip,
protobuf::MessageLite* parsed_proto);
protobuf::Message* parsed_proto);
inline Status ParseTextProto(absl::string_view /* text_proto */,
absl::string_view /* prefix_to_strip */,
protobuf::MessageLite* /* parsed_proto */) {
return errors::Unavailable("Cannot parse text protos on mobile.");
}
} // namespace tensorflow

View File

@ -1374,10 +1374,7 @@ cc_library(
name = "portable_tensorflow_lib_lite",
srcs = if_mobile([":mobile_srcs"]),
copts = tf_copts(android_optimization_level_override = None) + tf_opts_nortti_if_lite_protos() + if_ios(["-Os"]),
defines = ["SUPPORT_SELECTIVE_REGISTRATION"] + tf_portable_full_lite_protos(
full = [],
lite = ["TENSORFLOW_LITE_PROTOS"],
) + if_chromiumos(["IS_MOBILE_PLATFORM"]) + tf_defines_nortti_if_lite_protos(),
defines = ["SUPPORT_SELECTIVE_REGISTRATION"] + if_chromiumos(["IS_MOBILE_PLATFORM"]) + tf_defines_nortti_if_lite_protos(),
linkopts = if_android(["-lz"]) + if_ios(["-lz"]),
tags = [
"manual",

View File

@ -276,17 +276,10 @@ Status FillFunctionBody(
}
}
if (!node_attr_def) {
#ifdef TENSORFLOW_LITE_PROTOS
return errors::Unimplemented(
"Placeholder value is not supported for attributes not in OpDef. "
"Attribute: ",
node_attr_name);
#else
return errors::Unimplemented(
"Placeholder value is not supported for attributes not in OpDef. "
"Attribute: ",
node_attr_name, ", OpDef: ", node->op_def().DebugString());
#endif
}
OpDef::AttrDef* attr_def = fdef->mutable_signature()->add_attr();
attr_def->set_name(func_attr_name);

View File

@ -783,11 +783,13 @@ void RemoveDescriptionsFromOpList(OpList* op_list) {
}
bool AttrDefEqual(const OpDef::AttrDef& a1, const OpDef::AttrDef& a2) {
#ifndef TENSORFLOW_LITE_PROTOS
DCHECK_EQ(7, a1.GetDescriptor()->field_count())
<< "Please modify these equality and hash functions to reflect the "
"changes to the AttrDef protobuf";
#endif // TENSORFLOW_LITE_PROTOS
if (std::is_base_of<protobuf::Message, OpDef::AttrDef>()) {
DCHECK_EQ(7, reinterpret_cast<const protobuf::Message*>(&a1)
->GetDescriptor()
->field_count())
<< "Please modify these equality and hash functions to reflect the "
"changes to the AttrDef protobuf";
}
if (a1.name() != a2.name()) return false;
if (a1.type() != a2.type()) return false;

View File

@ -14,6 +14,7 @@ limitations under the License.
==============================================================================*/
#include "tensorflow/core/grappler/grappler_item_builder.h"
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <vector>
@ -480,28 +481,28 @@ std::unique_ptr<GrapplerItem> GrapplerItemFromMetaGraphDef(
meta_graph.collection_def().at("saved_model_assets");
const auto& any_assets = collection.any_list().value();
if (!any_assets.empty()) {
#ifndef TENSORFLOW_LITE_PROTOS
for (const auto& any_asset : any_assets) {
AssetFileDef asset_file_def;
if (!ParseAny(any_asset, &asset_file_def, "tensorflow.AssetFileDef")
.ok()) {
LOG(ERROR) << "Failed to parse AssetFile.";
continue;
if (std::is_base_of<protobuf::Message, AssetFileDef>()) {
for (const auto& any_asset : any_assets) {
AssetFileDef asset_file_def;
if (!ParseAny(any_asset, &asset_file_def, "tensorflow.AssetFileDef")
.ok()) {
LOG(ERROR) << "Failed to parse AssetFile.";
continue;
}
string asset_filepath = io::JoinPath(cfg.assets_directory_override,
asset_file_def.filename());
if (!FilesExist({asset_filepath}, nullptr)) {
LOG(ERROR) << "Can't access one or more of the asset files "
<< asset_filepath << ", skipping this input";
return nullptr;
}
asset_node_to_value[NodeName(asset_file_def.tensor_info().name())] =
asset_filepath;
}
string asset_filepath = io::JoinPath(cfg.assets_directory_override,
asset_file_def.filename());
if (!FilesExist({asset_filepath}, nullptr)) {
LOG(ERROR) << "Can't access one or more of the asset files "
<< asset_filepath << ", skipping this input";
return nullptr;
}
asset_node_to_value[NodeName(asset_file_def.tensor_info().name())] =
asset_filepath;
} else {
LOG(ERROR) << "Can't parse AssetFileDef when using lite protos.";
return nullptr;
}
#else
LOG(ERROR) << "Can't parse AssetFileDef on mobile.";
return nullptr;
#endif // TENSORFLOW_LITE_PROTOS
}
}
} else if (meta_graph.collection_def().count("asset_filepaths") > 0) {

View File

@ -48,12 +48,15 @@ namespace tensorflow {
namespace {
NodeDef StripTensorDataFromNodeDef(OpKernelConstruction* ctx) {
#ifndef TENSORFLOW_LITE_PROTOS
DCHECK_EQ(NodeDef::descriptor()->field_count(), 6)
<< "The NodeDef format has changed, and the attr-stripping code may need "
<< "to be updated.";
#endif
const NodeDef& original = ctx->def();
if (std::is_base_of<protobuf::Message, NodeDef>()) {
DCHECK_EQ(reinterpret_cast<const protobuf::Message*>(&original)
->GetDescriptor()
->field_count(),
6)
<< "The NodeDef format has changed, and the attr-stripping code may "
"need to be updated.";
}
NodeDef ret;
ret.set_name(original.name());
ret.set_op(original.op());

View File

@ -950,9 +950,6 @@ class ParseSingleSequenceExampleOp : public OpKernel {
OP_REQUIRES_OK(ctx, ctx->output_list("feature_list_dense_values",
&feature_list_dense_values));
#ifdef TENSORFLOW_LITE_PROTOS
SequenceExample ex;
#else
// Allocate the SequenceExample on an arena. Provides better memory locality
// and greatly speeds up destruction.
protobuf::ArenaOptions options;
@ -965,7 +962,7 @@ class ParseSingleSequenceExampleOp : public OpKernel {
options.max_block_size = std::max(options.max_block_size, block_size);
protobuf::Arena arena(options);
auto& ex = *protobuf::Arena::CreateMessage<SequenceExample>(&arena);
#endif
OP_REQUIRES(
ctx, ParseProtoUnlimited(&ex, serialized_t()),
errors::InvalidArgument("Could not parse example input, value: '",

View File

@ -44,7 +44,6 @@ load(
"tf_cc_tests",
"tf_copts", # @unused
"tf_cuda_library",
"tf_portable_full_lite_protos",
)
load(
"@local_config_rocm//rocm:build_defs.bzl",
@ -472,10 +471,6 @@ cc_library(
"protobuf_util.cc",
],
hdrs = ["protobuf.h"],
defines = tf_portable_full_lite_protos(
full = [],
lite = ["TENSORFLOW_LITE_PROTOS"],
),
deps = [
":platform",
":types",

View File

@ -31,8 +31,6 @@ load(
_tf_proto_library_py = "tf_proto_library_py",
_tf_protobuf_compiler_deps = "tf_protobuf_compiler_deps",
_tf_protobuf_deps = "tf_protobuf_deps",
_tf_protobuf_full_deps = "tf_protobuf_full_deps",
_tf_protobuf_lite_deps = "tf_protobuf_lite_deps",
_tf_protos_all = "tf_protos_all",
_tf_protos_all_impl = "tf_protos_all_impl",
_tf_protos_grappler = "tf_protos_grappler",
@ -73,8 +71,6 @@ tf_proto_library_cc = _tf_proto_library_cc
tf_proto_library_py = _tf_proto_library_py
tf_protobuf_compiler_deps = _tf_protobuf_compiler_deps
tf_protobuf_deps = _tf_protobuf_deps
tf_protobuf_full_deps = _tf_protobuf_full_deps
tf_protobuf_lite_deps = _tf_protobuf_lite_deps
tf_protos_all = _tf_protos_all
tf_protos_all_impl = _tf_protos_all_impl
tf_protos_grappler = _tf_protos_grappler

View File

@ -717,12 +717,6 @@ def tf_fingerprint_deps():
"@farmhash_archive//:farmhash",
]
def tf_protobuf_full_deps():
return tf_protobuf_deps()
def tf_protobuf_lite_deps():
return tf_protobuf_deps()
def tf_protobuf_deps():
return if_static(
[

View File

@ -23,10 +23,6 @@ namespace tensorflow {
Status ProtoToHumanReadableJson(const protobuf::Message& proto, string* result,
bool ignore_accuracy_loss) {
#ifdef TENSORFLOW_LITE_PROTOS
*result = "[human readable output not available on Android]";
return Status::OK();
#else
result->clear();
protobuf::util::JsonPrintOptions json_options;
@ -37,31 +33,37 @@ Status ProtoToHumanReadableJson(const protobuf::Message& proto, string* result,
if (!status.ok()) {
// Convert error_msg google::protobuf::StringPiece to
// tensorflow::StringPiece.
auto error_msg = status.error_message();
auto error_msg = status.message();
return errors::Internal(
strings::StrCat("Could not convert proto to JSON string: ",
StringPiece(error_msg.data(), error_msg.length())));
}
return Status::OK();
#endif
}
Status ProtoToHumanReadableJson(const protobuf::MessageLite& proto,
string* result, bool ignore_accuracy_loss) {
*result = "[human readable output not available for lite protos]";
return Status::OK();
}
Status HumanReadableJsonToProto(const string& str, protobuf::Message* proto) {
#ifdef TENSORFLOW_LITE_PROTOS
return errors::Internal("Cannot parse JSON protos on Android");
#else
proto->Clear();
auto status = protobuf::util::JsonStringToMessage(str, proto);
if (!status.ok()) {
// Convert error_msg google::protobuf::StringPiece to
// tensorflow::StringPiece.
auto error_msg = status.error_message();
auto error_msg = status.message();
return errors::Internal(
strings::StrCat("Could not convert JSON string to proto: ",
StringPiece(error_msg.data(), error_msg.length())));
}
return Status::OK();
#endif
}
Status HumanReadableJsonToProto(const string& str,
protobuf::MessageLite* proto) {
return errors::Internal("Cannot parse JSON protos on Android");
}
} // namespace tensorflow

View File

@ -496,7 +496,7 @@ Status FileSystemCopyFile(FileSystem* src_fs, const string& src,
// A ZeroCopyInputStream on a RandomAccessFile.
namespace {
class FileStream : public ::tensorflow::protobuf::io::ZeroCopyInputStream {
class FileStream : public protobuf::io::ZeroCopyInputStream {
public:
explicit FileStream(RandomAccessFile* file) : file_(file), pos_(0) {}
@ -533,14 +533,14 @@ class FileStream : public ::tensorflow::protobuf::io::ZeroCopyInputStream {
} // namespace
Status WriteBinaryProto(Env* env, const string& fname,
const ::tensorflow::protobuf::MessageLite& proto) {
const protobuf::MessageLite& proto) {
string serialized;
proto.AppendToString(&serialized);
return WriteStringToFile(env, fname, serialized);
}
Status ReadBinaryProto(Env* env, const string& fname,
::tensorflow::protobuf::MessageLite* proto) {
protobuf::MessageLite* proto) {
std::unique_ptr<RandomAccessFile> file;
TF_RETURN_IF_ERROR(env->NewRandomAccessFile(fname, &file));
std::unique_ptr<FileStream> stream(new FileStream(file.get()));
@ -549,7 +549,7 @@ Status ReadBinaryProto(Env* env, const string& fname,
// one to parse arbitrarily large messages for MessageLite. One most likely
// doesn't want to put protobufs larger than 64MB on Android, so we should
// eventually remove this and quit loud when a large protobuf is passed in.
::tensorflow::protobuf::io::CodedInputStream coded_stream(stream.get());
protobuf::io::CodedInputStream coded_stream(stream.get());
// Total bytes hard limit / warning limit are set to 1GB and 512MB
// respectively.
coded_stream.SetTotalBytesLimit(1024LL << 20, 512LL << 20);
@ -563,47 +563,36 @@ Status ReadBinaryProto(Env* env, const string& fname,
}
Status WriteTextProto(Env* env, const string& fname,
const ::tensorflow::protobuf::Message& proto) {
#if !defined(TENSORFLOW_LITE_PROTOS)
const protobuf::Message& proto) {
string serialized;
if (!::tensorflow::protobuf::TextFormat::PrintToString(proto, &serialized)) {
if (!protobuf::TextFormat::PrintToString(proto, &serialized)) {
return errors::FailedPrecondition("Unable to convert proto to text.");
}
return WriteStringToFile(env, fname, serialized);
#else
return errors::Unimplemented("Can't write text protos with protolite.");
#endif
}
Status ReadTextProto(Env* env, const string& fname,
::tensorflow::protobuf::Message* proto) {
#if !defined(TENSORFLOW_LITE_PROTOS)
Status ReadTextProto(Env* env, const string& fname, protobuf::Message* proto) {
std::unique_ptr<RandomAccessFile> file;
TF_RETURN_IF_ERROR(env->NewRandomAccessFile(fname, &file));
std::unique_ptr<FileStream> stream(new FileStream(file.get()));
if (!::tensorflow::protobuf::TextFormat::Parse(stream.get(), proto)) {
if (!protobuf::TextFormat::Parse(stream.get(), proto)) {
TF_RETURN_IF_ERROR(stream->status());
return errors::DataLoss("Can't parse ", fname, " as text proto");
}
return Status::OK();
#else
return errors::Unimplemented("Can't parse text protos with protolite.");
#endif
}
Status ReadTextOrBinaryProto(Env* env, const string& fname,
#if !defined(TENSORFLOW_LITE_PROTOS)
::tensorflow::protobuf::Message* proto
#else
::tensorflow::protobuf::MessageLite* proto
#endif
) {
#if !defined(TENSORFLOW_LITE_PROTOS)
protobuf::Message* proto) {
if (ReadTextProto(env, fname, proto).ok()) {
return Status::OK();
}
#endif
return ReadBinaryProto(env, fname, proto);
}
Status ReadTextOrBinaryProto(Env* env, const string& fname,
protobuf::MessageLite* proto) {
return ReadBinaryProto(env, fname, proto);
}

View File

@ -481,37 +481,31 @@ Status WriteStringToFile(Env* env, const string& fname,
/// Write binary representation of "proto" to the named file.
Status WriteBinaryProto(Env* env, const string& fname,
const ::tensorflow::protobuf::MessageLite& proto);
const protobuf::MessageLite& proto);
/// Reads contents of named file and parse as binary encoded proto data
/// and store into `*proto`.
Status ReadBinaryProto(Env* env, const string& fname,
::tensorflow::protobuf::MessageLite* proto);
protobuf::MessageLite* proto);
/// Write the text representation of "proto" to the named file.
Status WriteTextProto(Env* env, const string& fname,
const ::tensorflow::protobuf::Message& proto);
const protobuf::Message& proto);
/// Read contents of named file and parse as text encoded proto data
/// and store into `*proto`.
template <typename T, typename std::enable_if<!std::is_base_of<
protobuf::Message, T>::value>::type* = nullptr>
Status ReadTextProto(Env* env, const string& fname, T* proto) {
inline Status ReadTextProto(Env* /* env */, const string& /* fname */,
protobuf::MessageLite* /* proto */) {
return errors::Unimplemented("Can't parse text protos with protolite.");
}
Status ReadTextProto(Env* env, const string& fname,
::tensorflow::protobuf::Message* proto);
Status ReadTextProto(Env* env, const string& fname, protobuf::Message* proto);
/// Read contents of named file and parse as either text or binary encoded proto
/// data and store into `*proto`.
Status ReadTextOrBinaryProto(Env* env, const string& fname,
#if !defined(TENSORFLOW_LITE_PROTOS)
::tensorflow::protobuf::Message* proto
#else
::tensorflow::protobuf::MessageLite* proto
#endif
);
protobuf::Message* proto);
Status ReadTextOrBinaryProto(Env* env, const string& fname,
protobuf::MessageLite* proto);
// START_SKIP_DOXYGEN

View File

@ -31,10 +31,14 @@ namespace tensorflow {
// accuracy loss with large integers.
Status ProtoToHumanReadableJson(const protobuf::Message& proto, string* result,
bool ignore_accuracy_loss);
Status ProtoToHumanReadableJson(const protobuf::MessageLite& proto,
string* result, bool ignore_accuracy_loss);
// Converts a string produced by ProtoToHumanReadableJSON to a protobuf. Not
// guaranteed to work for general JSON.
Status HumanReadableJsonToProto(const string& str, protobuf::Message* proto);
Status HumanReadableJsonToProto(const string& str,
protobuf::MessageLite* proto);
} // namespace tensorflow

View File

@ -25,22 +25,20 @@ limitations under the License.
// TensorFlow code should use the ::tensorflow::protobuf namespace to
// refer to all protobuf APIs.
#ifndef TENSORFLOW_LITE_PROTOS
#include "google/protobuf/io/coded_stream.h"
#include "google/protobuf/io/tokenizer.h"
#include "google/protobuf/io/zero_copy_stream.h"
#include "google/protobuf/io/zero_copy_stream_impl_lite.h"
#include "google/protobuf/descriptor.pb.h"
#include "google/protobuf/arena.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/dynamic_message.h"
#include "google/protobuf/map.h"
#include "google/protobuf/message.h"
#include "google/protobuf/repeated_field.h"
#include "google/protobuf/text_format.h"
#include "google/protobuf/util/json_util.h"
#include "google/protobuf/util/type_resolver_util.h"
#endif
#include "google/protobuf/io/coded_stream.h"
#include "google/protobuf/io/zero_copy_stream.h"
#include "google/protobuf/io/zero_copy_stream_impl_lite.h"
#include "google/protobuf/arena.h"
#include "google/protobuf/map.h"
#include "google/protobuf/repeated_field.h"
namespace tensorflow {

View File

@ -24,46 +24,19 @@ limitations under the License.
namespace tensorflow {
// Returns the DebugString when available, or a stub message otherwise. Useful
// for messages that are incompatible with proto_text (e.g. those using Any).
#ifdef TENSORFLOW_LITE_PROTOS
template <class T>
string DebugStringIfAvailable(T proto) {
return "[DebugString not available with lite protos]";
}
#else
template <class T>
auto DebugStringIfAvailable(T proto) -> decltype(proto.DebugString()) {
return proto.DebugString();
}
#endif // defined(TENSORFLOW_LITE_PROTOS)
// Utility for parsing an Any value with full or lite protos.
template <class T>
Status ParseAny(const google::protobuf::Any& any, T* message,
const string& type_name) {
#ifdef TENSORFLOW_LITE_PROTOS
if (any.type_url() != strings::StrCat("type.googleapis.com/", type_name)) {
return errors::FailedPrecondition(
"Expected Any type_url for: ", type_name,
". Got: ", string(any.type_url().data(), any.type_url().size()), ".");
}
if (!message->ParseFromString(ProtobufStringToString(any.value()))) {
return errors::FailedPrecondition("Failed to unpack: ",
DebugStringIfAvailable(any));
}
#else
CHECK_EQ(type_name, message->descriptor()->full_name());
CHECK_EQ(type_name, message->GetTypeName());
if (!any.Is<T>()) {
return errors::FailedPrecondition(
"Expected Any type_url for: ", message->descriptor()->full_name(),
"Expected Any type_url for: ", message->GetTypeName(),
". Got: ", string(any.type_url().data(), any.type_url().size()), ".");
}
if (!any.UnpackTo(message)) {
return errors::FailedPrecondition("Failed to unpack: ",
DebugStringIfAvailable(any));
return errors::FailedPrecondition("Failed to unpack: ", any.DebugString());
}
#endif
return Status::OK();
}

View File

@ -156,18 +156,22 @@ Status CreateWritableFile(Env* env, const string& dirname, const string& name,
return env->NewWritableFile(*filepath, file);
}
template <class T>
Status WriteTextProtoToUniqueFile(T& proto, WritableFile* file) {
Status WriteTextProtoToUniqueFile(const tensorflow::protobuf::Message& proto,
WritableFile* file) {
string s;
#if defined(TENSORFLOW_LITE_PROTOS)
if (!SerializeToStringDeterministic(proto, &s)) {
return errors::Internal("Failed to serialize proto to string.");
}
#else
if (!::tensorflow::protobuf::TextFormat::PrintToString(proto, &s)) {
return errors::FailedPrecondition("Unable to convert proto to text.");
}
#endif
TF_RETURN_IF_ERROR(file->Append(s));
return file->Close();
}
Status WriteTextProtoToUniqueFile(
const tensorflow::protobuf::MessageLite& proto, WritableFile* file) {
string s;
if (!SerializeToStringDeterministic(proto, &s)) {
return errors::Internal("Failed to serialize proto to string.");
}
TF_RETURN_IF_ERROR(file->Append(s));
return file->Close();
}