From 56db128697fd0e9d3f689d472d53e33bf4ea1bb8 Mon Sep 17 00:00:00 2001 From: "A. Unique TensorFlower" <gardener@tensorflow.org> Date: Wed, 17 Jun 2020 17:56:36 -0700 Subject: [PATCH] Qualify uses of std::string PiperOrigin-RevId: 317003622 Change-Id: Iae6a9a287ffd3b97dee8b9993c443db322936592 --- .../lite/toco/logging/conversion_log_util.cc | 67 ++++++++++--------- .../lite/toco/logging/conversion_log_util.h | 22 +++--- .../toco/logging/conversion_log_util_test.cc | 60 ++++++++--------- 3 files changed, 77 insertions(+), 72 deletions(-) diff --git a/tensorflow/lite/toco/logging/conversion_log_util.cc b/tensorflow/lite/toco/logging/conversion_log_util.cc index c23c305c750..55afa1370b3 100644 --- a/tensorflow/lite/toco/logging/conversion_log_util.cc +++ b/tensorflow/lite/toco/logging/conversion_log_util.cc @@ -34,8 +34,8 @@ namespace toco { namespace { -string TryGetOperatorName(const Operator& op) { - string op_name; +std::string TryGetOperatorName(const Operator& op) { + std::string op_name; if (!op.tensorflow_node_def.empty()) { // Parse op name from serialized NodeDef. tensorflow::NodeDef node_def; @@ -63,8 +63,8 @@ string TryGetOperatorName(const Operator& op) { return op_name; } -string GetOSVersion() { - string os_info; +std::string GetOSVersion() { + std::string os_info; #ifdef __linux__ utsname info; if (uname(&info)) { @@ -72,12 +72,13 @@ string GetOSVersion() { LOG(ERROR) << "Cannot get OS info."; return ""; } - os_info = string(info.sysname) + ";OSVer=" + string(info.release) + ";"; + os_info = + std::string(info.sysname) + ";OSVer=" + std::string(info.release) + ";"; #endif return os_info; } -string ShapeToStringNoSpace(const Shape& shape) { +std::string ShapeToStringNoSpace(const Shape& shape) { if (shape.dimensions_count() == 0) { return "[]"; } @@ -85,13 +86,13 @@ string ShapeToStringNoSpace(const Shape& shape) { return absl::StrCat("[", absl::StrJoin(shape.dims(), ","), "]"); } -string GetOperatorSignature( +std::string GetOperatorSignature( const Model& model, const Operator& op, const std::map<OperatorType, std::unique_ptr<tflite::BaseOperator>>& op_types_map) { // The signature of an op has the following schema: // INPUT:SHAPE::TYPE::OUTPUT:SHAPE::TYPE::NAME:VERSION: - string op_signature; + std::string op_signature; constexpr char delimiter[] = "::"; // Get input shapes and types. @@ -137,8 +138,8 @@ string GetOperatorSignature( } // namespace -std::vector<string> GetOperatorNames(const Model& model) { - std::vector<string> op_names; +std::vector<std::string> GetOperatorNames(const Model& model) { + std::vector<std::string> op_names; for (const auto& op : model.operators) { op_names.push_back(TryGetOperatorName(*op)); } @@ -146,9 +147,9 @@ std::vector<string> GetOperatorNames(const Model& model) { } void CountOperatorsByType(const Model& model, - std::map<string, int>* built_in_ops, - std::map<string, int>* custom_ops, - std::map<string, int>* select_ops) { + std::map<std::string, int>* built_in_ops, + std::map<std::string, int>* custom_ops, + std::map<std::string, int>* select_ops) { for (const auto& op : model.operators) { OperatorSignature op_signature = {op.get(), &model}; const auto ops_by_type = @@ -156,7 +157,7 @@ void CountOperatorsByType(const Model& model, tflite::details::OperatorKey op_key(op_signature, ops_by_type, true /*enable_select_tf_ops*/); - const string op_name = TryGetOperatorName(*op); + const std::string op_name = TryGetOperatorName(*op); if (op_key.is_custom_op()) { (*custom_ops)[op_name]++; } else if (op_key.is_flex_op()) { @@ -168,8 +169,9 @@ void CountOperatorsByType(const Model& model, } void GetInputAndOutputTypes( - const Model& model, TFLITE_PROTO_NS::RepeatedPtrField<string>* input_types, - TFLITE_PROTO_NS::RepeatedPtrField<string>* output_types) { + const Model& model, + TFLITE_PROTO_NS::RepeatedPtrField<std::string>* input_types, + TFLITE_PROTO_NS::RepeatedPtrField<std::string>* output_types) { for (const auto& input_array : model.flags.input_arrays()) { const Array& array = model.GetArray(input_array.name()); input_types->Add(ArrayDataTypeName(array.data_type)); @@ -180,15 +182,16 @@ void GetInputAndOutputTypes( } } -string GetTfLiteVersion() { return TFLITE_VERSION_STRING; } +std::string GetTfLiteVersion() { return TFLITE_VERSION_STRING; } -string GetCachedOSVersion() { - static string* version = new string(GetOSVersion()); +std::string GetCachedOSVersion() { + static std::string* version = new std::string(GetOSVersion()); return *version; } -void GetOpSignatures(const Model& model, - TFLITE_PROTO_NS::RepeatedPtrField<string>* op_signatures) { +void GetOpSignatures( + const Model& model, + TFLITE_PROTO_NS::RepeatedPtrField<std::string>* op_signatures) { const auto& op_types_map = tflite::BuildOperatorByTypeMap(true /*enable_select_tf_ops*/); for (const auto& op : model.operators) { @@ -196,7 +199,7 @@ void GetOpSignatures(const Model& model, } } -string GetModelHash(const Model& model) { +std::string GetModelHash(const Model& model) { // TODO(b/123519920): Implement the hash function for Model. // Need to consider different implementations for public/private models. return ""; @@ -204,18 +207,18 @@ string GetModelHash(const Model& model) { // This function scans through the error message string, extracts the part about // missing ops and prunes away all other information in the error info. -string SanitizeErrorMessage(const string& error_message) { - const string s1 = "Ops that can be supported by the flex runtime"; - const string s2 = "Ops that need custom implementation"; - string pruned_message; +std::string SanitizeErrorMessage(const std::string& error_message) { + const std::string s1 = "Ops that can be supported by the flex runtime"; + const std::string s2 = "Ops that need custom implementation"; + std::string pruned_message; size_t pos = error_message.find(s1); - if (pos != string::npos) { + if (pos != std::string::npos) { // Find the terminate point for flex op list. auto end = error_message.find(".", pos); pruned_message.append(error_message.substr(pos, end - pos + 1)); } pos = error_message.find(s2); - if (pos != string::npos) { + if (pos != std::string::npos) { // Find the terminate point for custom op list. auto end = error_message.find(".", pos); pruned_message.append(error_message.substr(pos, end - pos + 1)); @@ -225,18 +228,18 @@ string SanitizeErrorMessage(const string& error_message) { void PopulateConversionLog(const Model& model, TocoConversionLog* log) { // Get the list of ops after conversion. - const std::vector<string> op_names = GetOperatorNames(model); + const std::vector<std::string> op_names = GetOperatorNames(model); for (const auto& op_name : op_names) { log->add_op_list(op_name); } // Get op signatures. - TFLITE_PROTO_NS::RepeatedPtrField<string> op_signatures; + TFLITE_PROTO_NS::RepeatedPtrField<std::string> op_signatures; GetOpSignatures(model, &op_signatures); log->mutable_op_signatures()->CopyFrom(op_signatures); // Get op counts by category: custom, built-in or select. - std::map<string, int> custom_ops, select_ops, built_in_ops; + std::map<std::string, int> custom_ops, select_ops, built_in_ops; CountOperatorsByType(model, &built_in_ops, &custom_ops, &select_ops); log->mutable_custom_ops()->insert(custom_ops.cbegin(), custom_ops.cend()); log->mutable_built_in_ops()->insert(built_in_ops.cbegin(), @@ -244,7 +247,7 @@ void PopulateConversionLog(const Model& model, TocoConversionLog* log) { log->mutable_select_ops()->insert(select_ops.cbegin(), select_ops.cend()); // Get the model's input and output types. - TFLITE_PROTO_NS::RepeatedPtrField<string> input_types, output_types; + TFLITE_PROTO_NS::RepeatedPtrField<std::string> input_types, output_types; GetInputAndOutputTypes(model, &input_types, &output_types); log->mutable_input_tensor_types()->CopyFrom(input_types); log->mutable_output_tensor_types()->CopyFrom(output_types); diff --git a/tensorflow/lite/toco/logging/conversion_log_util.h b/tensorflow/lite/toco/logging/conversion_log_util.h index 2237615adbb..c21ec0792cc 100644 --- a/tensorflow/lite/toco/logging/conversion_log_util.h +++ b/tensorflow/lite/toco/logging/conversion_log_util.h @@ -25,37 +25,39 @@ namespace toco { // This function scans through the error message string, extracts the part about // missing ops and prunes away all other information in the error info. -string SanitizeErrorMessage(const string& error_message); +std::string SanitizeErrorMessage(const std::string& error_message); // Populates the TocoConversionLog proto after analyzing the model. void PopulateConversionLog(const Model& model, TocoConversionLog* log); // Returns the names of the operators in the model. -std::vector<string> GetOperatorNames(const Model& model); +std::vector<std::string> GetOperatorNames(const Model& model); // Counts the number of different types of operators in the model: // Built-in ops, custom ops and select ops. // Each map is mapping from the name of the operator (such as 'Conv') to its // total number of occurrences in the model. void CountOperatorsByType(const Model& model, - std::map<string, int>* built_in_ops, - std::map<string, int>* custom_ops, - std::map<string, int>* select_ops); + std::map<std::string, int>* built_in_ops, + std::map<std::string, int>* custom_ops, + std::map<std::string, int>* select_ops); // Gets the input and output types of the model. The input and output is // specified by model.flags.input_arrays and model.flags.output_arrays. void GetInputAndOutputTypes( - const Model& model, TFLITE_PROTO_NS::RepeatedPtrField<string>* input_types, - TFLITE_PROTO_NS::RepeatedPtrField<string>* output_types); + const Model& model, + TFLITE_PROTO_NS::RepeatedPtrField<std::string>* input_types, + TFLITE_PROTO_NS::RepeatedPtrField<std::string>* output_types); // Calculates signatures for all the ops in the model. An op signature is // defined by its input/output shapes and types, op name and its version. -void GetOpSignatures(const Model& model, - TFLITE_PROTO_NS::RepeatedPtrField<string>* op_signatures); +void GetOpSignatures( + const Model& model, + TFLITE_PROTO_NS::RepeatedPtrField<std::string>* op_signatures); // TODO(b/123519920): Implement this. // Calculates a unique hash for the model. -string GetModelHash(const Model& model); +std::string GetModelHash(const Model& model); } // namespace toco diff --git a/tensorflow/lite/toco/logging/conversion_log_util_test.cc b/tensorflow/lite/toco/logging/conversion_log_util_test.cc index c4960715f25..17111eca6d0 100644 --- a/tensorflow/lite/toco/logging/conversion_log_util_test.cc +++ b/tensorflow/lite/toco/logging/conversion_log_util_test.cc @@ -58,9 +58,9 @@ TEST(ConversionLogUtilTest, TestCountOperatorsByType) { Model model; // 1st Conv operator. std::unique_ptr<ConvOperator> conv1(new ConvOperator()); - const string conv1_input_name = "conv_input1"; - const string conv1_filter_name = "conv_filter1"; - const string conv1_output_name = "conv_output1"; + const std::string conv1_input_name = "conv_input1"; + const std::string conv1_filter_name = "conv_filter1"; + const std::string conv1_output_name = "conv_output1"; conv1->inputs.push_back(conv1_input_name); conv1->inputs.push_back(conv1_filter_name); conv1->outputs.push_back(conv1_output_name); @@ -71,9 +71,9 @@ TEST(ConversionLogUtilTest, TestCountOperatorsByType) { // 2nd Conv operator. std::unique_ptr<ConvOperator> conv2(new ConvOperator()); - const string conv2_input_name = "conv_input2"; - const string conv2_filter_name = "conv_filter2"; - const string conv2_output_name = "conv_output2"; + const std::string conv2_input_name = "conv_input2"; + const std::string conv2_filter_name = "conv_filter2"; + const std::string conv2_output_name = "conv_output2"; conv2->inputs.push_back(conv2_input_name); conv2->inputs.push_back(conv2_filter_name); conv2->outputs.push_back(conv2_output_name); @@ -83,7 +83,7 @@ TEST(ConversionLogUtilTest, TestCountOperatorsByType) { // Mean operator. std::unique_ptr<MeanOperator> mean(new MeanOperator()); - const string mean_input_name = "mean_input"; + const std::string mean_input_name = "mean_input"; mean->inputs.push_back(mean_input_name); array_map[mean_input_name] = std::unique_ptr<Array>(new Array); @@ -111,26 +111,26 @@ TEST(ConversionLogUtilTest, TestCountOperatorsByType) { model.operators.push_back(std::move(elu_grad)); model.operators.push_back(std::move(my_custom_op)); - std::map<string, int> built_in_ops, select_ops, custom_ops; + std::map<std::string, int> built_in_ops, select_ops, custom_ops; CountOperatorsByType(model, &built_in_ops, &custom_ops, &select_ops); EXPECT_THAT(built_in_ops, - UnorderedElementsAre(std::pair<string, int>("Conv", 2), - std::pair<string, int>("Mean", 1))); + UnorderedElementsAre(std::pair<std::string, int>("Conv", 2), + std::pair<std::string, int>("Mean", 1))); EXPECT_THAT(select_ops, - UnorderedElementsAre(std::pair<string, int>("AvgPool3D", 1), - std::pair<string, int>("EluGrad", 1))); - EXPECT_THAT(custom_ops, UnorderedElementsAre( - std::pair<string, int>("MyAwesomeCustomOp", 1))); + UnorderedElementsAre(std::pair<std::string, int>("AvgPool3D", 1), + std::pair<std::string, int>("EluGrad", 1))); + EXPECT_THAT(custom_ops, UnorderedElementsAre(std::pair<std::string, int>( + "MyAwesomeCustomOp", 1))); } TEST(ConversionLogUtilTest, TestGetInputAndOutputTypes) { Model model; auto& array_map = model.GetMutableArrayMap(); - const string input1 = "conv_input"; - const string input2 = "conv_filter"; - const string input3 = "feature"; - const string output = "softmax"; + const std::string input1 = "conv_input"; + const std::string input2 = "conv_filter"; + const std::string input3 = "feature"; + const std::string output = "softmax"; array_map[input1] = std::unique_ptr<Array>(new Array); array_map[input1]->data_type = ArrayDataType::kFloat; array_map[input2] = std::unique_ptr<Array>(new Array); @@ -149,7 +149,7 @@ TEST(ConversionLogUtilTest, TestGetInputAndOutputTypes) { *model.flags.add_input_arrays() = input_arrays[2]; model.flags.add_output_arrays(output); - TFLITE_PROTO_NS::RepeatedPtrField<string> input_types, output_types; + TFLITE_PROTO_NS::RepeatedPtrField<std::string> input_types, output_types; GetInputAndOutputTypes(model, &input_types, &output_types); EXPECT_THAT(input_types, ElementsAre("float", "float", "int16")); @@ -161,9 +161,9 @@ TEST(ConversionLogUtilTest, TestGetOpSignatures) { auto& array_map = model.GetMutableArrayMap(); std::unique_ptr<ConvOperator> conv(new ConvOperator()); - const string conv_input_name = "conv_input"; - const string conv_filter_name = "conv_filter"; - const string conv_output_name = "conv_output"; + const std::string conv_input_name = "conv_input"; + const std::string conv_filter_name = "conv_filter"; + const std::string conv_output_name = "conv_output"; conv->inputs.push_back(conv_input_name); conv->inputs.push_back(conv_filter_name); conv->outputs.push_back(conv_output_name); @@ -177,15 +177,15 @@ TEST(ConversionLogUtilTest, TestGetOpSignatures) { array_map[conv_output_name]->data_type = ArrayDataType::kFloat; array_map[conv_output_name]->copy_shape({4, 4, 2}); - const string mean_input_name = "mean_input"; - const string mean_output_name = "mean_output"; + const std::string mean_input_name = "mean_input"; + const std::string mean_output_name = "mean_output"; std::unique_ptr<MeanOperator> mean(new MeanOperator()); mean->inputs.push_back(mean_input_name); mean->outputs.push_back(mean_output_name); array_map[mean_input_name] = std::unique_ptr<Array>(new Array); array_map[mean_output_name] = std::unique_ptr<Array>(new Array); - const string avg_pool_3d_output_name = "avg_pool_output"; + const std::string avg_pool_3d_output_name = "avg_pool_output"; auto avg_pool_3d = absl::make_unique<TensorFlowUnsupportedOperator>(); avg_pool_3d->tensorflow_op = "AvgPool3D"; tensorflow::NodeDef node_def; @@ -197,7 +197,7 @@ TEST(ConversionLogUtilTest, TestGetOpSignatures) { array_map[avg_pool_3d_output_name]->data_type = ArrayDataType::kInt32; array_map[avg_pool_3d_output_name]->copy_shape({2, 2}); - const string custom_op_output_name = "custom_op_output"; + const std::string custom_op_output_name = "custom_op_output"; auto my_custom_op = absl::make_unique<TensorFlowUnsupportedOperator>(); my_custom_op->tensorflow_op = "MyAwesomeCustomOp"; my_custom_op->inputs.push_back(avg_pool_3d_output_name); @@ -211,7 +211,7 @@ TEST(ConversionLogUtilTest, TestGetOpSignatures) { model.operators.push_back(std::move(avg_pool_3d)); model.operators.push_back(std::move(my_custom_op)); - TFLITE_PROTO_NS::RepeatedPtrField<string> op_signatures; + TFLITE_PROTO_NS::RepeatedPtrField<std::string> op_signatures; GetOpSignatures(model, &op_signatures); EXPECT_THAT(op_signatures, UnorderedElementsAre( @@ -225,14 +225,14 @@ TEST(ConversionLogUtilTest, TestGetOpSignatures) { } TEST(ConversionLogUtilTest, TestSanitizeErrorMessage) { - const string error = + const std::string error = "error: failed while converting: 'main': Ops that can be supported by " "the flex runtime (enabled via setting the -emit-select-tf-ops flag): " "ResizeNearestNeighbor,ResizeNearestNeighbor. Ops that need custom " "implementation (enabled via setting the -emit-custom-ops flag): " "CombinedNonMaxSuppression.\nTraceback (most recent call last): File " "/usr/local/bin/toco_from_protos, line 8, in <module>"; - const string pruned_error = + const std::string pruned_error = "Ops that can be supported by " "the flex runtime (enabled via setting the -emit-select-tf-ops flag): " "ResizeNearestNeighbor,ResizeNearestNeighbor.Ops that need custom " @@ -242,7 +242,7 @@ TEST(ConversionLogUtilTest, TestSanitizeErrorMessage) { } TEST(ConversionLogUtilTest, TestSanitizeErrorMessageNoMatching) { - const string error = + const std::string error = "error: failed while converting: 'main': Traceback (most recent call " "last): File " "/usr/local/bin/toco_from_protos, line 8, in <module>";