diff --git a/tensorflow/compiler/mlir/tensorflow/BUILD b/tensorflow/compiler/mlir/tensorflow/BUILD index f75e45c8b1f..5f1aaa01514 100644 --- a/tensorflow/compiler/mlir/tensorflow/BUILD +++ b/tensorflow/compiler/mlir/tensorflow/BUILD @@ -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", ], diff --git a/tensorflow/compiler/mlir/tensorflow/utils/import_utils.cc b/tensorflow/compiler/mlir/tensorflow/utils/import_utils.cc index 47c5d27767d..3d16352f78e 100644 --- a/tensorflow/compiler/mlir/tensorflow/utils/import_utils.cc +++ b/tensorflow/compiler/mlir/tensorflow/utils/import_utils.cc @@ -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 diff --git a/tensorflow/compiler/mlir/tensorflow/utils/import_utils.h b/tensorflow/compiler/mlir/tensorflow/utils/import_utils.h index 56cd188f393..ad1531dd449 100644 --- a/tensorflow/compiler/mlir/tensorflow/utils/import_utils.h +++ b/tensorflow/compiler/mlir/tensorflow/utils/import_utils.h @@ -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 diff --git a/tensorflow/compiler/mlir/tensorflow/utils/parse_text_proto.cc b/tensorflow/compiler/mlir/tensorflow/utils/parse_text_proto.cc index b616d34fdd8..1bf615de8c4 100644 --- a/tensorflow/compiler/mlir/tensorflow/utils/parse_text_proto.cc +++ b/tensorflow/compiler/mlir/tensorflow/utils/parse_text_proto.cc @@ -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 diff --git a/tensorflow/compiler/mlir/tensorflow/utils/parse_text_proto.h b/tensorflow/compiler/mlir/tensorflow/utils/parse_text_proto.h index 5646f1378af..c1f1e3b111d 100644 --- a/tensorflow/compiler/mlir/tensorflow/utils/parse_text_proto.h +++ b/tensorflow/compiler/mlir/tensorflow/utils/parse_text_proto.h @@ -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 diff --git a/tensorflow/core/BUILD b/tensorflow/core/BUILD index d08a5734e22..b4bec2a6907 100644 --- a/tensorflow/core/BUILD +++ b/tensorflow/core/BUILD @@ -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", diff --git a/tensorflow/core/framework/graph_to_functiondef.cc b/tensorflow/core/framework/graph_to_functiondef.cc index 4e965e7b5bb..bbd70151849 100644 --- a/tensorflow/core/framework/graph_to_functiondef.cc +++ b/tensorflow/core/framework/graph_to_functiondef.cc @@ -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); diff --git a/tensorflow/core/framework/op_def_util.cc b/tensorflow/core/framework/op_def_util.cc index 0ebc4bf2483..115c24e1968 100644 --- a/tensorflow/core/framework/op_def_util.cc +++ b/tensorflow/core/framework/op_def_util.cc @@ -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; diff --git a/tensorflow/core/grappler/grappler_item_builder.cc b/tensorflow/core/grappler/grappler_item_builder.cc index 26e8f61ab5a..cffe7df8186 100644 --- a/tensorflow/core/grappler/grappler_item_builder.cc +++ b/tensorflow/core/grappler/grappler_item_builder.cc @@ -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) { diff --git a/tensorflow/core/kernels/constant_op.cc b/tensorflow/core/kernels/constant_op.cc index f787d879ed6..4bcbc076446 100644 --- a/tensorflow/core/kernels/constant_op.cc +++ b/tensorflow/core/kernels/constant_op.cc @@ -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()); diff --git a/tensorflow/core/kernels/example_parsing_ops.cc b/tensorflow/core/kernels/example_parsing_ops.cc index 583ab5eb3b7..0a940e52eb7 100644 --- a/tensorflow/core/kernels/example_parsing_ops.cc +++ b/tensorflow/core/kernels/example_parsing_ops.cc @@ -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: '", diff --git a/tensorflow/core/platform/BUILD b/tensorflow/core/platform/BUILD index 59cc6d68732..819f8fcdadb 100644 --- a/tensorflow/core/platform/BUILD +++ b/tensorflow/core/platform/BUILD @@ -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", diff --git a/tensorflow/core/platform/build_config.bzl b/tensorflow/core/platform/build_config.bzl index d1fb5f829ea..f0613cdc069 100644 --- a/tensorflow/core/platform/build_config.bzl +++ b/tensorflow/core/platform/build_config.bzl @@ -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 diff --git a/tensorflow/core/platform/default/build_config.bzl b/tensorflow/core/platform/default/build_config.bzl index fa3bd466d8e..e3ccf84fe91 100644 --- a/tensorflow/core/platform/default/build_config.bzl +++ b/tensorflow/core/platform/default/build_config.bzl @@ -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( [ diff --git a/tensorflow/core/platform/default/human_readable_json.cc b/tensorflow/core/platform/default/human_readable_json.cc index a8a79a44b47..acc8d223619 100644 --- a/tensorflow/core/platform/default/human_readable_json.cc +++ b/tensorflow/core/platform/default/human_readable_json.cc @@ -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 diff --git a/tensorflow/core/platform/env.cc b/tensorflow/core/platform/env.cc index 74da5b9429d..b29cad05459 100644 --- a/tensorflow/core/platform/env.cc +++ b/tensorflow/core/platform/env.cc @@ -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); } diff --git a/tensorflow/core/platform/env.h b/tensorflow/core/platform/env.h index 7b617c0231f..99924ec1143 100644 --- a/tensorflow/core/platform/env.h +++ b/tensorflow/core/platform/env.h @@ -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 diff --git a/tensorflow/core/platform/human_readable_json.h b/tensorflow/core/platform/human_readable_json.h index f6830e20207..12a7bd042ee 100644 --- a/tensorflow/core/platform/human_readable_json.h +++ b/tensorflow/core/platform/human_readable_json.h @@ -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 diff --git a/tensorflow/core/platform/protobuf.h b/tensorflow/core/platform/protobuf.h index 0075241ee6c..6b4db77ea3f 100644 --- a/tensorflow/core/platform/protobuf.h +++ b/tensorflow/core/platform/protobuf.h @@ -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 { diff --git a/tensorflow/core/platform/protobuf_internal.h b/tensorflow/core/platform/protobuf_internal.h index d41ee5a468a..0d303197d45 100644 --- a/tensorflow/core/platform/protobuf_internal.h +++ b/tensorflow/core/platform/protobuf_internal.h @@ -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(); } diff --git a/tensorflow/core/util/dump_graph.cc b/tensorflow/core/util/dump_graph.cc index dd273af2a00..0e16f9d3fb3 100644 --- a/tensorflow/core/util/dump_graph.cc +++ b/tensorflow/core/util/dump_graph.cc @@ -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(); }