From ead36c50c1865dc0fc8fdc1b4b20eb37e4879420 Mon Sep 17 00:00:00 2001 From: "A. Unique TensorFlower" Date: Sat, 9 Feb 2019 19:33:32 -0800 Subject: [PATCH] Use "empty" instead of "size"-based checks, and replace C-style casts by static_cast. PiperOrigin-RevId: 233247794 --- .../compiler/tf2tensorrt/convert/convert_graph.cc | 6 +++--- .../compiler/tf2tensorrt/convert/convert_nodes.cc | 6 +++--- .../tf2tensorrt/convert/trt_optimization_pass.cc | 14 +++++++------- .../tf2tensorrt/utils/trt_int8_calibrator.cc | 2 +- .../compiler/tf2tensorrt/utils/trt_resources.cc | 2 +- tensorflow/compiler/xla/tests/hlo_test_base.cc | 8 +++++--- 6 files changed, 20 insertions(+), 18 deletions(-) diff --git a/tensorflow/compiler/tf2tensorrt/convert/convert_graph.cc b/tensorflow/compiler/tf2tensorrt/convert/convert_graph.cc index f3db42509ec..d8298952395 100644 --- a/tensorflow/compiler/tf2tensorrt/convert/convert_graph.cc +++ b/tensorflow/compiler/tf2tensorrt/convert/convert_graph.cc @@ -222,7 +222,7 @@ tensorflow::Status ConvertCalibGraphToInferGraph( cres->thr_->join(); const auto& calibration_table = cres->calibrator_->getCalibrationTableAsString(); - if (!calibration_table.size()) { + if (calibration_table.empty()) { LOG(ERROR) << "Calibration table is empty"; return tensorflow::errors::Unknown( "Calibration table is missing. This shouldn't have happened!"); @@ -662,8 +662,8 @@ tensorflow::Status CreateTRTNode(const std::vector& infos, int pos, info.use_calibration, /*convert_successfully=*/nullptr)); TrtUniquePtrType engine_data(engine->serialize()); - segment_string = - string((const char*)engine_data->data(), engine_data->size()); + segment_string = string(static_cast(engine_data->data()), + engine_data->size()); if (calibrate_int8) { // See above comment about why not putting this inside the 'else' branch. segment_string = info.segment_graph_def.SerializeAsString(); diff --git a/tensorflow/compiler/tf2tensorrt/convert/convert_nodes.cc b/tensorflow/compiler/tf2tensorrt/convert/convert_nodes.cc index 21683703914..ce8a69b0d32 100644 --- a/tensorflow/compiler/tf2tensorrt/convert/convert_nodes.cc +++ b/tensorflow/compiler/tf2tensorrt/convert/convert_nodes.cc @@ -2106,7 +2106,7 @@ tensorflow::Status ConvertSqueeze(OpConverterParams* params) { // Mark axes to remove by setting them to 0. TFAttrs attrs(node_def); auto squeeze_dims = attrs.get>("squeeze_dims"); - if (squeeze_dims.size() == 0) { + if (squeeze_dims.empty()) { return tensorflow::errors::Unimplemented( "Squeeze is only implemented for explicit dims, at ", node_def.name()); } @@ -2274,7 +2274,7 @@ tensorflow::Status ConvertStridedSlice(OpConverterParams* params) { pad_dims.push_back(i); } } - if (pad_dims.size() == 0) { + if (pad_dims.empty()) { // No dimensions are changed. We could create a padding layer anyway with // values of 0. if (params->validation_only) return Status::OK(); @@ -3138,7 +3138,7 @@ tensorflow::Status ConvertPad(OpConverterParams* params) { } // No padding at all, we should exit - if (pad_index.size() == 0) { + if (pad_index.empty()) { params->outputs->push_back(inputs.at(0)); return tensorflow::Status::OK(); } diff --git a/tensorflow/compiler/tf2tensorrt/convert/trt_optimization_pass.cc b/tensorflow/compiler/tf2tensorrt/convert/trt_optimization_pass.cc index f36aa558ea2..0eedfcacb4c 100644 --- a/tensorflow/compiler/tf2tensorrt/convert/trt_optimization_pass.cc +++ b/tensorflow/compiler/tf2tensorrt/convert/trt_optimization_pass.cc @@ -87,7 +87,7 @@ void TRTOptimizationPass::PrintDebugInfo( LOG(INFO) << offset << "type = " << cluster->type(); LOG(INFO) << offset << "num warmup steps = " << cluster->NumWarmupSteps(); const auto dev_names = cluster->GetDeviceNames(); - if (dev_names.size()) { + if (!dev_names.empty()) { LOG(INFO) << offset << " Device names:"; for (const auto s : dev_names) { LOG(INFO) << offset2 << s; @@ -103,7 +103,7 @@ void TRTOptimizationPass::PrintDebugInfo( } const auto dev_props = cluster->GetDevices(); - if (dev_props.size()) { + if (!dev_props.empty()) { LOG(INFO) << offset << "Device properties:"; for (auto k : dev_props) { LOG(INFO) << offset2 << k.first; @@ -131,7 +131,7 @@ void TRTOptimizationPass::PrintDebugInfo( } } LOG(INFO) << "item: " << item.id; - if (item.feed.size()) { + if (!item.feed.empty()) { LOG(INFO) << offset << "Feeds :"; for (const auto& f : item.feed) { const auto& shape = f.second.shape(); @@ -140,7 +140,7 @@ void TRTOptimizationPass::PrintDebugInfo( } else { LOG(INFO) << offset << "No Feeds"; } - if (item.fetch.size()) { + if (!item.fetch.empty()) { LOG(INFO) << offset << "Fetches :"; for (const auto& f : item.fetch) { LOG(INFO) << offset2 << f; @@ -149,7 +149,7 @@ void TRTOptimizationPass::PrintDebugInfo( LOG(INFO) << offset << "No Fetches"; } - if (item.init_ops.size()) { + if (!item.init_ops.empty()) { LOG(INFO) << offset << "init ops :"; for (const auto& f : item.init_ops) { LOG(INFO) << offset2 << f; @@ -160,7 +160,7 @@ void TRTOptimizationPass::PrintDebugInfo( LOG(INFO) << "Save Op = " << item.save_op; LOG(INFO) << "Restore Op = " << item.restore_op; LOG(INFO) << "save_restore_loc_tensor = " << item.save_restore_loc_tensor; - if (item.keep_ops.size()) { + if (!item.keep_ops.empty()) { LOG(INFO) << offset << "keep ops :"; for (const auto& f : item.keep_ops) { LOG(INFO) << offset2 << f; @@ -197,7 +197,7 @@ tensorflow::Status TRTOptimizationPass::Optimize( PrintDebugInfo(cluster, item); } int max_dim = -1; - if (item.feed.size()) { + if (!item.feed.empty()) { for (const auto& f : item.feed) { const auto& shape = f.second.shape(); if (shape.dims() > 0) { diff --git a/tensorflow/compiler/tf2tensorrt/utils/trt_int8_calibrator.cc b/tensorflow/compiler/tf2tensorrt/utils/trt_int8_calibrator.cc index bf111d3a2ee..5213fced1ea 100644 --- a/tensorflow/compiler/tf2tensorrt/utils/trt_int8_calibrator.cc +++ b/tensorflow/compiler/tf2tensorrt/utils/trt_int8_calibrator.cc @@ -135,7 +135,7 @@ void TRTInt8Calibrator::setDone() { void TRTInt8Calibrator::writeCalibrationCache(const void* ptr, std::size_t length) { - calibration_table_ = string((const char*)ptr, length); + calibration_table_ = string(static_cast(ptr), length); VLOG(1) << "Got calibration data for " << engine_name_ << " @" << ptr << " length=" << length; } diff --git a/tensorflow/compiler/tf2tensorrt/utils/trt_resources.cc b/tensorflow/compiler/tf2tensorrt/utils/trt_resources.cc index 37f7fe99fbb..2e553079b19 100644 --- a/tensorflow/compiler/tf2tensorrt/utils/trt_resources.cc +++ b/tensorflow/compiler/tf2tensorrt/utils/trt_resources.cc @@ -48,7 +48,7 @@ Status TRTCalibrationResource::SerializeToString(string* serialized) { calibrator_->waitAndSetDone(); thr_->join(); *serialized = calibrator_->getCalibrationTableAsString(); - if (!serialized->size()) { + if (serialized->empty()) { return tensorflow::errors::Unknown("Calibration table is empty."); } return Status::OK(); diff --git a/tensorflow/compiler/xla/tests/hlo_test_base.cc b/tensorflow/compiler/xla/tests/hlo_test_base.cc index 38466b175e9..d9d54fd2556 100644 --- a/tensorflow/compiler/xla/tests/hlo_test_base.cc +++ b/tensorflow/compiler/xla/tests/hlo_test_base.cc @@ -400,7 +400,7 @@ StatusOr<::testing::AssertionResult> HloTestBase::RunAndCompareInternal( module->set_config(config); } - if (backend_config != "") { + if (!backend_config.empty()) { // Set backend configuration if it is given. HloInstruction* instruction = module->entry_computation()->root_instruction(); @@ -409,9 +409,10 @@ StatusOr<::testing::AssertionResult> HloTestBase::RunAndCompareInternal( auto executable = test_runner_.CreateExecutable(std::move(module), run_hlo_passes); - if (!executable.ok()) + if (!executable.ok()) { return ::testing::AssertionFailure() << executable.status().error_message(); + } executables[i] = std::move(executable.ValueOrDie()); } @@ -419,8 +420,9 @@ StatusOr<::testing::AssertionResult> HloTestBase::RunAndCompareInternal( auto output = test_runner_.Execute(std::move(executables[i]), fake_argument_ptrs[i], /*profile=*/&((*profiles)[i])); - if (!output.ok()) + if (!output.ok()) { return ::testing::AssertionFailure() << output.status().error_message(); + } } return ::testing::AssertionSuccess();