forked from tensorflow/tensorflow
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make some //tf/compiler/mlir/tensorflow/utils modules build with --co…
…nfig android_arm64. PiperOrigin-RevId: 291246582 Change-Id: Idf13e25462a9722ea70bcc48a9c7b9091bcba8a5
- Loading branch information
1 parent
b840bf5
commit 29c1d30
Showing
6 changed files
with
148 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
tensorflow/compiler/mlir/tensorflow/utils/parse_text_proto.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* 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/compiler/mlir/tensorflow/utils/parse_text_proto.h" | ||
|
||
#include "absl/strings/match.h" | ||
#include "absl/strings/string_view.h" | ||
#include "tensorflow/core/lib/core/errors.h" | ||
#include "tensorflow/core/lib/core/status.h" | ||
#include "tensorflow/core/platform/casts.h" | ||
#include "tensorflow/core/platform/protobuf.h" | ||
|
||
namespace tensorflow { | ||
|
||
#ifndef TENSORFLOW_LITE_PROTOS | ||
namespace { | ||
// Error collector that simply ignores errors reported. | ||
class NoOpErrorCollector : public protobuf::io::ErrorCollector { | ||
public: | ||
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) { | ||
if (absl::StartsWith(str, prefix)) { | ||
*output = str.substr(prefix.size()); | ||
return Status::OK(); | ||
} | ||
return errors::NotFound("No prefix \"", prefix, "\" in \"", str, "\""); | ||
} | ||
|
||
Status ParseTextProto(absl::string_view text_proto, | ||
absl::string_view prefix_to_strip, | ||
protobuf::MessageLite* parsed_proto) { | ||
#ifndef TENSORFLOW_LITE_PROTOS | ||
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. | ||
NoOpErrorCollector collector; | ||
parser.RecordErrorsTo(&collector); | ||
// Attempt to parse as text. | ||
absl::string_view text_proto_without_prefix = text_proto; | ||
if (!prefix_to_strip.empty()) { | ||
TF_RETURN_IF_ERROR( | ||
ConsumePrefix(text_proto, prefix_to_strip, &text_proto_without_prefix)); | ||
} | ||
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))) { | ||
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 |
39 changes: 39 additions & 0 deletions
39
tensorflow/compiler/mlir/tensorflow/utils/parse_text_proto.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* 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_COMPILER_MLIR_TENSORFLOW_UTILS_PARSE_TEXT_PROTO_H_ | ||
#define TENSORFLOW_COMPILER_MLIR_TENSORFLOW_UTILS_PARSE_TEXT_PROTO_H_ | ||
|
||
#include "absl/strings/string_view.h" | ||
#include "tensorflow/core/lib/core/errors.h" | ||
#include "tensorflow/core/lib/core/status.h" | ||
#include "tensorflow/core/platform/protobuf.h" | ||
|
||
namespace tensorflow { | ||
|
||
// Sets output to the given input with `prefix` stripped, or returns an error if | ||
// the prefix doesn't exist. | ||
Status ConsumePrefix(absl::string_view str, absl::string_view prefix, | ||
absl::string_view* output); | ||
|
||
// Strips `prefix_to_strip` from `text_proto`, parses, and returns the parsed | ||
// proto. | ||
Status ParseTextProto(absl::string_view text_proto, | ||
absl::string_view prefix_to_strip, | ||
protobuf::MessageLite* parsed_proto); | ||
|
||
} // namespace tensorflow | ||
|
||
#endif // TENSORFLOW_COMPILER_MLIR_TENSORFLOW_UTILS_PARSE_TEXT_PROTO_H_ |