diff --git a/tensorflow/core/grappler/costs/graph_memory.cc b/tensorflow/core/grappler/costs/graph_memory.cc index 020e8cf1d1f..db252d16c49 100644 --- a/tensorflow/core/grappler/costs/graph_memory.cc +++ b/tensorflow/core/grappler/costs/graph_memory.cc @@ -255,7 +255,8 @@ void GraphMemory::InferFromTrace(const StepStats& timeline) { std::unordered_set live_at_peak; size_t current = 0; std::unordered_set currently_live; - for (int i = 0; i < events.size(); ++i) { + int events_size = events.size(); + for (int i = 0; i < events_size; ++i) { const auto& event = events[i]; if (event.allocated) { @@ -271,8 +272,7 @@ void GraphMemory::InferFromTrace(const StepStats& timeline) { current -= event.tensor->memory_used; currently_live.erase(event.tensor); } - if (i + 1 == events.size() || - event.timestamp != events[i + 1].timestamp) { + if (i + 1 == events_size || event.timestamp != events[i + 1].timestamp) { if (current > peak) { peak = current; live_at_peak = currently_live; diff --git a/tensorflow/core/grappler/costs/graph_properties.cc b/tensorflow/core/grappler/costs/graph_properties.cc index ee691e7a081..36e530916a3 100644 --- a/tensorflow/core/grappler/costs/graph_properties.cc +++ b/tensorflow/core/grappler/costs/graph_properties.cc @@ -363,7 +363,7 @@ void VerboseLogUnknownDimensionSources( std::vector ReplaceUnknownDimFromConstWithUnknownDim( InferenceContext* ic, const std::vector& shapes) { std::vector converted_shapes(shapes.size()); - for (int i = 0; i < shapes.size(); i++) { + for (int i = 0, shapes_size = shapes.size(); i < shapes_size; i++) { const auto& shape = shapes[i]; if (!ic->RankKnown(shape)) { converted_shapes[i] = shape; @@ -502,7 +502,8 @@ class TopoQueue { const std::vector& topo_order) const { absl::flat_hash_map map; map.reserve(topo_order.size()); - for (int i = 0; i < topo_order.size(); ++i) { + for (int i = 0, topo_order_size = topo_order.size(); i < topo_order_size; + ++i) { map.emplace(topo_order[i], i); } return map; @@ -680,14 +681,17 @@ class SymbolicShapeRefiner { ", shape: ", ic->DebugString(ic->input(i)), ", tensor: "); Tensor t1; - if (input_tensor_protos.size() > i && + int input_tensor_protos_size = input_tensor_protos.size(); + if (input_tensor_protos_size > i && input_tensor_protos.at(i) != nullptr && t1.FromProto(*input_tensor_protos.at(i))) { absl::StrAppend(&output, t1.DebugString(), ", tensor_as_shape: "); } else { absl::StrAppend(&output, " null, tensor_as_shape: "); } - if (input_tensors_as_shapes_to_propagate.size() > i) { + int input_tensors_as_shapes_to_propagate_size = + input_tensors_as_shapes_to_propagate.size(); + if (input_tensors_as_shapes_to_propagate_size > i) { absl::StrAppend( &output, StringifyShapeHandle(input_tensors_as_shapes_to_propagate.at(i)), @@ -702,14 +706,16 @@ class SymbolicShapeRefiner { ", shape: ", ic->DebugString(ic->output(i)), ", tensor: "); Tensor t2; - if (output_tensor_protos.size() > i && + int output_tensor_protos_size = output_tensor_protos.size(); + if (output_tensor_protos_size > i && output_tensor_protos.at(i) != nullptr && t2.FromProto(*output_tensor_protos.at(i))) { absl::StrAppend(&output, t2.DebugString(), ", tensor_as_shape: "); } else { absl::StrAppend(&output, " null, tensor_as_shape: "); } - if (output_tensors_as_shapes.size() > i) { + int output_tensors_as_shapes_size = output_tensors_as_shapes.size(); + if (output_tensors_as_shapes_size > i) { absl::StrAppend(&output, StringifyShapeHandle(output_tensors_as_shapes.at(i)), "\n"); @@ -779,7 +785,8 @@ class SymbolicShapeRefiner { MutableGraphView gv(&grappler_function_item.graph); // Forward shapes from function input nodes to argument nodes. - for (int i = 0; i < grappler_function_item.inputs().size(); ++i) { + for (int i = 0, iter_limit = grappler_function_item.inputs().size(); + i < iter_limit; ++i) { auto& fun_input = grappler_function_item.input(i); NodeDef* fun_node = gv.GetNode(fun_input.node_name); const TensorId input_tensor = ParseTensorName(function_node->input(i)); @@ -858,13 +865,13 @@ class SymbolicShapeRefiner { if (IsConstant(*input_node)) { TF_CHECK_OK( ReplaceInputWithConst(*input_node, i, &grappler_function_item)); - } else if (ctx->input_tensor_protos.size() > i && + } else if (static_cast(ctx->input_tensor_protos.size()) > i && ctx->input_tensor_protos[i] != nullptr) { NodeDef const_input_node = MakeConstNodeDefFromTensorProto( ic, *ctx->input_tensor_protos[i], ctx->input_types[i]); TF_CHECK_OK(ReplaceInputWithConst(const_input_node, i, &grappler_function_item)); - } else if (ic->input_tensors_as_shapes().size() > i && + } else if (static_cast(ic->input_tensors_as_shapes().size()) > i && IsShapeFullyDefinedIntegerVectorOrScalar( ic, ic->input(i), ic->input_tensors_as_shapes()[i], ctx->input_types[i])) { @@ -912,7 +919,8 @@ class SymbolicShapeRefiner { } auto output_properties = gp.GetOutputProperties(retnode->name()); - if (out_tensor.index() >= output_properties.size()) { + int output_properties_size = output_properties.size(); + if (out_tensor.index() >= output_properties_size) { return errors::InvalidArgument( out_tensor.ToString(), " has invalid position ", out_tensor.index(), " (output_properties.size() = ", output_properties.size(), ")."); @@ -975,12 +983,13 @@ class SymbolicShapeRefiner { // NodeContext: // output_tensor_protos to input_tensor_protos and input_tensors, and // output_tensors_as_shapes to input_tensors_as_shapes. - if (src_ctx->output_tensors_as_shapes.size() > src_output) { + if (static_cast(src_ctx->output_tensors_as_shapes.size()) > + src_output) { ctx->input_tensors_as_shapes_to_propagate[dst_input] = src_ctx->output_tensors_as_shapes[src_output]; } - if (src_ctx->output_tensor_protos.size() > src_output) { + if (static_cast(src_ctx->output_tensor_protos.size()) > src_output) { const auto* tensor_proto = src_ctx->output_tensor_protos[src_output]; if (tensor_proto != nullptr) { ctx->input_tensor_protos[dst_input] = tensor_proto; @@ -1233,7 +1242,7 @@ class SymbolicShapeRefiner { if (st1.size() != st2.size()) { return false; } - for (int i = 0; i < st1.size(); ++i) { + for (int i = 0, st1_size = st1.size(); i < st1_size; ++i) { const ShapeAndType& s1 = st1[i]; const ShapeAndType& s2 = st2[i]; if (s1.dtype != s2.dtype) { @@ -1268,13 +1277,15 @@ class SymbolicShapeRefiner { return Status::OK(); } - if (grappler_function_item.inputs().size() > function_node->input_size()) { + if (static_cast(grappler_function_item.inputs().size()) > + function_node->input_size()) { return errors::FailedPrecondition( "Function input size should be smaller than node input size."); } - for (int i = grappler_function_item.inputs().size(); - i < function_node->input_size(); ++i) { + for (int i = grappler_function_item.inputs().size(), + iter_limit = function_node->input_size(); + i < iter_limit; ++i) { const string& input = function_node->input(i); if (!IsControlInput(input)) { return errors::FailedPrecondition( @@ -1357,18 +1368,20 @@ class SymbolicShapeRefiner { // Returns true if all the output tensors have known values. bool AllOutputValuesKnown(NodeContext* c) { InferenceContext* ic = c->inference_context.get(); - if (c->output_tensors_as_shapes.size() < ic->num_outputs() && - c->output_tensor_protos.size() < ic->num_outputs()) { + int c_output_tensors_as_shapes_size = c->output_tensors_as_shapes.size(); + int c_output_tensor_protos_size = c->output_tensor_protos.size(); + if (c_output_tensors_as_shapes_size < ic->num_outputs() && + c_output_tensor_protos_size < ic->num_outputs()) { return false; } else { // Checks if we can get output value via either output_tensor_proto or // output_tensors_as_shapes. for (int i = 0; i < ic->num_outputs(); i++) { - if (c->output_tensor_protos.size() > i && + if (c_output_tensor_protos_size > i && c->output_tensor_protos[i] != nullptr) { continue; } - if (c->output_tensors_as_shapes.size() > i && + if (c_output_tensors_as_shapes_size > i && ic->FullyDefined(c->output_tensors_as_shapes[i])) { bool no_unknown_dim_from_const = true; for (int32 j = 0; j < ic->Rank(c->output_tensors_as_shapes[i]); ++j) { @@ -1539,7 +1552,7 @@ class SymbolicShapeRefiner { &resource_mgr_, &outputs)); c->output_tensors_as_shapes.resize(outputs.size()); c->output_tensor_protos.resize(outputs.size(), nullptr); - for (int k = 0; k < outputs.size(); k++) { + for (int k = 0, outputs_size = outputs.size(); k < outputs_size; k++) { const auto& t = outputs[k]; // Override output shape. ShapeHandle output_shape; @@ -2297,7 +2310,7 @@ Status GraphProperties::UpdateEnqueue( // TODO(bsteiner): handle EnqueueMany as well. std::vector shapes_and_types; - for (int i = 1; i < ctx->input_types.size(); ++i) { + for (int i = 1, iter_limit = ctx->input_types.size(); i < iter_limit; ++i) { GraphView::InputPort inp(enqueue_node, i); GraphView::OutputPort fanin = shape_refiner->graph().GetRegularFanin(inp); InferenceContext* in = shape_refiner->GetContext(fanin.node); @@ -2490,10 +2503,11 @@ Status GraphProperties::InferStatically(bool assume_valid_feeds, const TensorProto& raw_val = fanin.node->attr().at("value").tensor(); *input_properties[i].mutable_value() = raw_val; - } else if (ctx->input_tensor_protos.size() > i && + } else if (static_cast(ctx->input_tensor_protos.size()) > i && ctx->input_tensor_protos[i] != nullptr) { *input_properties[i].mutable_value() = *ctx->input_tensor_protos[i]; - } else if (ic->input_tensors_as_shapes().size() > i && + } else if (static_cast(ic->input_tensors_as_shapes().size()) > + i && IsShapeFullyDefinedIntegerVectorOrScalar( ic, ic->input(i), ic->input_tensors_as_shapes()[i], ctx->input_types[i])) { @@ -2525,11 +2539,12 @@ Status GraphProperties::InferStatically(bool assume_valid_feeds, // TODO(rmlarsen): Eliminate this copy. const TensorProto& raw_val = node.attr().at("value").tensor(); *output_properties[i].mutable_value() = raw_val; - } else if (ctx->output_tensor_protos.size() > i && + } else if (static_cast(ctx->output_tensor_protos.size()) > i && ctx->output_tensor_protos[i] != nullptr) { *output_properties[i].mutable_value() = *ctx->output_tensor_protos[i]; - } else if (converted_output_tensors_as_shapes.size() > i && + } else if (static_cast( + converted_output_tensors_as_shapes.size()) > i && IsShapeFullyDefinedIntegerVectorOrScalar( ic, ic->output(i), converted_output_tensors_as_shapes[i], diff --git a/tensorflow/core/grappler/costs/op_level_cost_estimator.cc b/tensorflow/core/grappler/costs/op_level_cost_estimator.cc index b8b62cbd6e5..6f57708a780 100644 --- a/tensorflow/core/grappler/costs/op_level_cost_estimator.cc +++ b/tensorflow/core/grappler/costs/op_level_cost_estimator.cc @@ -1470,8 +1470,8 @@ Costs OpLevelCostEstimator::PredictEinsum(const OpContext& op_context) const { (a_input.shape().dim_size() < matrix_rank) || (b_input.shape().dim_size() < matrix_rank); - if (a_input_str.size() != a_input_shape.dim_size() || - b_input_str.size() != b_input_shape.dim_size()) { + if (a_input_str.size() != static_cast(a_input_shape.dim_size()) || + b_input_str.size() != static_cast(b_input_shape.dim_size())) { VLOG(1) << "Missing accurate estimator for op: " << op_info.op() << ", equation subscripts don't match tensor rank."; return PredictCostOfAnUnknownOp(op_context); @@ -1513,7 +1513,8 @@ Costs OpLevelCostEstimator::PredictEinsum(const OpContext& op_context) const { n_dim.set_size(1); k_dim.set_size(1); - for (int i_idx = 0; i_idx < a_input_str.size(); ++i_idx) { + for (int i_idx = 0, a_input_str_size = a_input_str.size(); + i_idx < a_input_str_size; ++i_idx) { if (b_input_str.find(a_input_str[i_idx]) == std::string::npos) { if (rhs_str.find(a_input_str[i_idx]) == std::string::npos) { VLOG(1) << "Missing accurate estimator for op: " << op_info.op(); @@ -1533,7 +1534,8 @@ Costs OpLevelCostEstimator::PredictEinsum(const OpContext& op_context) const { *(a_matrix_shape->add_dim()) = a_input_shape.dim(i_idx); *(b_matrix_shape->add_dim()) = a_input_shape.dim(i_idx); } - for (int i_idx = 0; i_idx < b_input_str.size(); ++i_idx) { + for (int i_idx = 0, b_input_str_size = b_input_str.size(); + i_idx < b_input_str_size; ++i_idx) { if (a_input_str.find(b_input_str[i_idx]) == std::string::npos) { if (rhs_str.find(b_input_str[i_idx]) == std::string::npos) { VLOG(1) << "Missing accurate estimator for op: " << op_info.op(); diff --git a/tensorflow/core/grappler/costs/virtual_scheduler.cc b/tensorflow/core/grappler/costs/virtual_scheduler.cc index 5339b00627e..67af304b081 100644 --- a/tensorflow/core/grappler/costs/virtual_scheduler.cc +++ b/tensorflow/core/grappler/costs/virtual_scheduler.cc @@ -522,8 +522,9 @@ Status SchedulerState::Init(const GrapplerItem* item, if (IsPersistent(*curr_node)) { auto& device_state = device_[curr_node_device]; - for (int port_num = 0; - port_num < curr_node_state.output_properties.size(); ++port_num) { + for (int port_num = 0, + port_num_iter_limit = curr_node_state.output_properties.size(); + port_num < port_num_iter_limit; ++port_num) { device_state.persistent_nodes.insert( std::make_pair(curr_node, port_num)); } @@ -795,7 +796,8 @@ void SchedulerState::GetOutputNodes(const NodeDef* node, // Execute a node as soon as all its inputs are ready. Merge nodes are // special since they run as soon as one of their inputs becomes // available. - if (output_state.num_inputs_ready == output_state.inputs.size() || + int output_state_inputs_size = output_state.inputs.size(); + if (output_state.num_inputs_ready == output_state_inputs_size || IsMerge(*output_node)) { // This output node is now ready. output_state.time_ready = curr_time; @@ -900,8 +902,8 @@ std::vector SchedulerState::MarkNodeExecuted( auto port = input_port.second; auto& input_state = node_map_[input]; input_state.num_outputs_executed[port]++; - if (input_state.num_outputs_executed[port] == - input_state.outputs[port].size() && + int input_state_outputs_size_ = input_state.outputs[port].size(); + if (input_state.num_outputs_executed[port] == input_state_outputs_size_ && !IsPersistent(*input)) { // All the outputs are executed; no reference to this output port of // input node. @@ -1119,7 +1121,8 @@ void SchedulerState::GenerateRunMetadata(RunMetadata* metadata) { const NodeState& nodestate = node_map_.at(node_def); NodeExecStats* node_stats = device_stepstats->add_node_stats(); uint64 total_output_size = 0; - for (int slot = 0; slot < nodestate.output_properties.size(); slot++) { + for (int slot = 0, slot_iter_limit = nodestate.output_properties.size(); + slot < slot_iter_limit; slot++) { const auto& properties = nodestate.output_properties[slot]; NodeOutput* no = node_stats->add_output(); no->set_slot(slot); diff --git a/tensorflow/core/grappler/optimizers/common_subgraph_elimination.cc b/tensorflow/core/grappler/optimizers/common_subgraph_elimination.cc index af323e913a7..2489cf93e78 100644 --- a/tensorflow/core/grappler/optimizers/common_subgraph_elimination.cc +++ b/tensorflow/core/grappler/optimizers/common_subgraph_elimination.cc @@ -73,7 +73,8 @@ class UniqueNodes { if (it == memoized_signatures_.end()) return; std::vector& candidates = rep_[it->second]; - for (int i = 0; i < candidates.size(); ++i) { + for (int i = 0, candidates_size = candidates.size(); i < candidates_size; + ++i) { if (candidates[i] == node) { std::swap(candidates[i], candidates[candidates.size() - 1]); candidates.resize(candidates.size() - 1); diff --git a/tensorflow/core/grappler/optimizers/debug_stripper.cc b/tensorflow/core/grappler/optimizers/debug_stripper.cc index d4b3bf395c3..95c8f816338 100644 --- a/tensorflow/core/grappler/optimizers/debug_stripper.cc +++ b/tensorflow/core/grappler/optimizers/debug_stripper.cc @@ -63,7 +63,8 @@ Status DebugStripper::Optimize(Cluster* cluster, const GrapplerItem& item, node.mutable_attr()->swap(new_attr); // As Identity op only takes one input, mark redundant inputs as control // input. - for (size_t i = 1; i < node.input_size(); ++i) { + for (int i = 1, node_input_size = node.input_size(); i < node_input_size; + ++i) { if (!IsControlInput(node.input(i))) { *node.mutable_input(i) = AsControlDependency(NodeName(node.input(i))); } diff --git a/tensorflow/core/grappler/optimizers/function_optimizer.cc b/tensorflow/core/grappler/optimizers/function_optimizer.cc index ed3af955c13..a66e645e04b 100644 --- a/tensorflow/core/grappler/optimizers/function_optimizer.cc +++ b/tensorflow/core/grappler/optimizers/function_optimizer.cc @@ -438,8 +438,8 @@ bool HasUnusedOutputs(const NodeDef& func_node, const FunctionDef& func, int num_outputs = func.signature().output_arg_size(); const absl::flat_hash_set active_outputs = GetActiveOutputs(func_node, ctx, /*size_hind*/ num_outputs); - - return active_outputs.size() != num_outputs; + int active_outputs_size = active_outputs.size(); + return active_outputs_size != num_outputs; } // Return pruned FunctionDefLibrary with functions that are reachable from @@ -563,7 +563,8 @@ void RemoveUnusedOutputsTypes(const FunctionSpecialization& specialization, if (tout == nullptr || !tout->has_list()) return; // Nothing to do if all outputs are active. - if (specialization.active_outputs.size() == tout->list().type_size()) return; + int specialization_active_outputs_size = specialization.active_outputs.size(); + if (specialization_active_outputs_size == tout->list().type_size()) return; // Clear input types for the specialized node. auto* attr = specialized_func_node->mutable_attr(); @@ -1142,7 +1143,8 @@ void AddFrameForwardingControlEdge(const std::vector& info, Node* caller, Graph* g) { // All nodes added to the graph by v2 control flow lowering and function // inlining are guaranteed to have control edges to nested function calls. - if (caller->id() >= info.size()) return; + int info_size = info.size(); + if (caller->id() >= info_size) return; // Check if a lowered node is executing inside a while loop. const Node* frame = info[caller->id()].frame; diff --git a/tensorflow/core/grappler/optimizers/model_pruner.cc b/tensorflow/core/grappler/optimizers/model_pruner.cc index 20db4360f73..3f5e3a8ea3a 100644 --- a/tensorflow/core/grappler/optimizers/model_pruner.cc +++ b/tensorflow/core/grappler/optimizers/model_pruner.cc @@ -401,9 +401,10 @@ Status SplitIdentityNInputs(GraphDef* graph, } const int num_non_control_inputs = NumNonControlInputs(*node); + int terminal_second_size = terminal.second.size(); if (node->attr().count("T") == 0 || node->attr().at("T").list().type_size() != num_non_control_inputs || - terminal.second.size() >= num_non_control_inputs) { + terminal_second_size >= num_non_control_inputs) { continue; } diff --git a/tensorflow/core/grappler/optimizers/pin_to_host_optimizer.cc b/tensorflow/core/grappler/optimizers/pin_to_host_optimizer.cc index ec16de1294b..35d0c5b0e40 100644 --- a/tensorflow/core/grappler/optimizers/pin_to_host_optimizer.cc +++ b/tensorflow/core/grappler/optimizers/pin_to_host_optimizer.cc @@ -107,7 +107,8 @@ Status IsNodeOutputPortHostFriendly(const GraphView& graph, /*include_tensor_values=*/false)); } const auto& output_properties = properties->GetOutputProperties(node.name()); - if (port_id >= output_properties.size()) { + int output_properties_size = output_properties.size(); + if (port_id >= output_properties_size) { LOG(WARNING) << "port_id=" << port_id << " but output_properties.size()=" << output_properties.size() << "\n" diff --git a/tensorflow/core/grappler/optimizers/shape_optimizer.cc b/tensorflow/core/grappler/optimizers/shape_optimizer.cc index 69de1cde4ca..656c1a1db1c 100644 --- a/tensorflow/core/grappler/optimizers/shape_optimizer.cc +++ b/tensorflow/core/grappler/optimizers/shape_optimizer.cc @@ -99,7 +99,8 @@ Status ShapeOptimizer::Optimize(Cluster* cluster, const GrapplerItem& item, } const auto& prop = properties.GetOutputProperties(reduce_indices.node->name()); - if (prop.size() <= reduce_indices.port_id) { + int prop_size = prop.size(); + if (prop_size <= reduce_indices.port_id) { continue; } const TensorShapeProto& reduction_indices_shape = diff --git a/tensorflow/core/grappler/utils.cc b/tensorflow/core/grappler/utils.cc index cd6b4855583..deed2591001 100644 --- a/tensorflow/core/grappler/utils.cc +++ b/tensorflow/core/grappler/utils.cc @@ -357,7 +357,8 @@ void PermuteNodesInPlace(GraphDef* graph, std::vector* permutation, } permutation->swap(inv_perm); } - for (std::size_t n = 0; n + 1 < permutation->size(); ++n) { + for (int n = 0, permutation_size = permutation->size(); + n + 1 < permutation_size; ++n) { while (n != (*permutation)[n]) { std::size_t r = (*permutation)[n]; graph->mutable_node()->SwapElements(n, r); diff --git a/tensorflow/core/grappler/utils/graph_view.cc b/tensorflow/core/grappler/utils/graph_view.cc index 5a9a1cd2abb..891c57d1e86 100644 --- a/tensorflow/core/grappler/utils/graph_view.cc +++ b/tensorflow/core/grappler/utils/graph_view.cc @@ -63,7 +63,7 @@ bool NodeView::HasFanout(const FaninView& fanout) const { return false; } else if (fanout.index() == Graph::kControlSlot) { return view->fanins_set_.contains({this->node(), Graph::kControlSlot}); - } else if (fanout.index() >= view->regular_fanins_.size()) { + } else if (fanout.index() >= static_cast(view->regular_fanins_.size())) { return false; } return view->regular_fanins_[fanout.index()].node_index_ == node_index_; @@ -152,8 +152,9 @@ Status GraphView::CheckAndAddFaninsInternal(NodeView* node_view) { Graph::kControlSlot); has_observed_control = true; } else { - if (fanin_node_view.regular_fanouts_by_port_.size() < - fanin_id.index() + 1) { + int fanin_node_view_regular_fanouts_by_port_size = + fanin_node_view.regular_fanouts_by_port_.size(); + if (fanin_node_view_regular_fanouts_by_port_size < fanin_id.index() + 1) { fanin_node_view.regular_fanouts_by_port_.resize(fanin_id.index() + 1); } fanin_node_view.regular_fanouts_by_port_[fanin_id.index()].emplace_back( @@ -197,7 +198,7 @@ bool MutableNodeView::HasFanout(const MutableFaninView& fanout) const { return false; } else if (fanout.index() == Graph::kControlSlot) { return view->fanins_count_.contains({this->node(), Graph::kControlSlot}); - } else if (fanout.index() >= view->regular_fanins_.size()) { + } else if (fanout.index() >= static_cast(view->regular_fanins_.size())) { return false; } return view->regular_fanins_[fanout.index()].node_index_ == node_index_; @@ -279,7 +280,8 @@ void Mutation::AddMutation( void Mutation::RemoveNode(MutableNodeView* node) { auto& update_index = node->update_index_; if (update_index != internal::kMissingIndex) { - if (update_index < updated_nodes_.size() - 1) { + int updated_nodes_size = updated_nodes_.size(); + if (update_index < updated_nodes_size - 1) { graph_view_->nodes_[updated_nodes_.back().node_index].update_index_ = update_index; std::swap(updated_nodes_[update_index], updated_nodes_.back()); @@ -574,7 +576,9 @@ void MutableGraphView::AddFaninsInternal( --last_pos; } } else { - if (fanin_node_view.regular_fanouts_by_port_.size() < + int fanin_node_view_regular_fanouts_by_port_size = + fanin_node_view.regular_fanouts_by_port_.size(); + if (fanin_node_view_regular_fanouts_by_port_size < fanin_id.index() + 1) { fanin_node_view.regular_fanouts_by_port_.resize(fanin_id.index() + 1); } @@ -852,8 +856,10 @@ template void MutableGraphView::ReplaceNodeFanouts(MutableNodeView* node, T* fanouts) { node->num_regular_fanouts_ = fanouts->num_regular_fanouts_; node->regular_fanouts_by_port_ = std::move(fanouts->regular_fanouts_by_port_); - for (int i = 0; i < node->regular_fanouts_by_port_.size(); ++i) { - for (int j = 0; j < node->regular_fanouts_by_port_[i].size(); ++j) { + for (int i = 0, i_max = node->regular_fanouts_by_port_.size(); i < i_max; + ++i) { + for (int j = 0, j_max = node->regular_fanouts_by_port_[i].size(); j < j_max; + ++j) { auto& fanout = node->regular_fanouts_by_port_[i][j]; auto* fanout_node_view = fanout.node_view(); auto& fanout_fanin = fanout_node_view->regular_fanins_[fanout.index()]; @@ -868,7 +874,7 @@ void MutableGraphView::ReplaceNodeFanouts(MutableNodeView* node, T* fanouts) { } } node->controlled_fanouts_ = std::move(fanouts->controlled_fanouts_); - for (int i = 0; i < node->controlled_fanouts_.size(); ++i) { + for (int i = 0, i_max = node->controlled_fanouts_.size(); i < i_max; ++i) { auto& fanout = node->controlled_fanouts_[i]; auto* fanout_node_view = fanout.node_view(); auto& fanout_fanin = @@ -1017,7 +1023,8 @@ inline void MutableGraphView::RemoveRegularFaninFanoutInternal( {&graph_->node(fanin.node_index_), fanin.index()}); auto* fanin_node_view = fanin.node_view(); auto& fanouts = fanin_node_view->regular_fanouts_by_port_[fanin.index()]; - if (fanin.fanout_index_ < fanouts.size() - 1) { + int fanouts_size = fanouts.size(); + if (fanin.fanout_index_ < fanouts_size - 1) { // Swap fanout with last fanout in vector, and update it's associated fanin // index. MutableFaninView& last_fanout = fanouts.back(); @@ -1043,7 +1050,9 @@ inline void MutableGraphView::RemoveRegularFaninFanoutInternal( break; } } - if (last_fanout_index < fanin_node_view->regular_fanouts_by_port_.size()) { + int fanin_node_view_regular_fanouts_by_port_size = + fanin_node_view->regular_fanouts_by_port_.size(); + if (last_fanout_index < fanin_node_view_regular_fanouts_by_port_size) { fanin_node_view->regular_fanouts_by_port_.resize(last_fanout_index); } } @@ -1052,7 +1061,9 @@ inline void MutableGraphView::AddRegularFaninInternal( MutableNodeView* node_view, const SafeTensorId& fanin_id) { MutableNodeView* fanin_node_view = GetNode(fanin_id.node()); // Resize fanouts to include new output port index. - if (fanin_node_view->regular_fanouts_by_port_.size() < fanin_id.index() + 1) { + int fanin_node_view_regular_fanouts_by_port_size = + fanin_node_view->regular_fanouts_by_port_.size(); + if (fanin_node_view_regular_fanouts_by_port_size < fanin_id.index() + 1) { fanin_node_view->regular_fanouts_by_port_.resize(fanin_id.index() + 1); } @@ -1078,7 +1089,9 @@ inline void MutableGraphView::UpdateRegularFaninInternal( MutableNodeView* fanin_node_view = GetNode(fanin_id.node()); // Resize fanouts to include new output port index. - if (fanin_node_view->regular_fanouts_by_port_.size() < fanin_id.index() + 1) { + int fanin_node_view_regular_fanouts_by_port_size = + fanin_node_view->regular_fanouts_by_port_.size(); + if (fanin_node_view_regular_fanouts_by_port_size < fanin_id.index() + 1) { fanin_node_view->regular_fanouts_by_port_.resize(fanin_id.index() + 1); } @@ -1110,8 +1123,10 @@ inline void MutableGraphView::RemoveControllingFaninFanoutInternal( // controlled fanout in controlling fanin with controlled fanout to be // removed. auto* control_to_remove_view = control_to_remove.node_view(); + int control_to_remove_view_controlled_fanouts_size = + control_to_remove_view->controlled_fanouts_.size(); if (control_to_remove.fanout_index_ < - control_to_remove_view->controlled_fanouts_.size() - 1) { + control_to_remove_view_controlled_fanouts_size - 1) { auto& control_to_remove_view_last_control = control_to_remove_view->controlled_fanouts_.back(); control_to_remove_view_last_control.node_view() @@ -1137,7 +1152,9 @@ inline void MutableGraphView::RemoveControllingFaninInternal( RemoveControllingFaninFanoutInternal(node_view, control_index); // Swap last controlling fanin in node with controlling fanin to be removed. - if (control_index < node_view->controlling_fanins_.size() - 1) { + int node_view_controlling_fanins_size = + node_view->controlling_fanins_.size(); + if (control_index < node_view_controlling_fanins_size - 1) { auto& last_control = node_view->controlling_fanins_.back(); auto* last_control_view = last_control.node_view(); last_control_view->controlled_fanouts_[last_control.fanout_index_] diff --git a/tensorflow/core/grappler/utils/graph_view_internal.h b/tensorflow/core/grappler/utils/graph_view_internal.h index d07f9f71640..d66b1ca0452 100644 --- a/tensorflow/core/grappler/utils/graph_view_internal.h +++ b/tensorflow/core/grappler/utils/graph_view_internal.h @@ -172,7 +172,8 @@ class NodeViewInternal { // Returns a regular fanin based on input index. If no such fanin exist, a // missing fanin is returned, with no NodeView set and an index of -2. const FanoutViewT& GetRegularFanin(int i) const { - if (i < 0 || i >= regular_fanins_.size()) { + int regular_fanins_size = regular_fanins_.size(); + if (i < 0 || i >= regular_fanins_size) { return GetMissingFanin(); } return regular_fanins_[i]; @@ -191,7 +192,8 @@ class NodeViewInternal { // Returns a regular fanout(s) based on output index. If no such output index // exists, no fanouts will be returned. const std::vector& GetRegularFanout(int i) const { - if (i < 0 || i >= regular_fanouts_by_port_.size()) { + int regular_fanouts_by_port_size = regular_fanouts_by_port_.size(); + if (i < 0 || i >= regular_fanouts_by_port_size) { return GetMissingFanout(); } return regular_fanouts_by_port_[i]; @@ -289,14 +291,16 @@ class GraphViewInternal { // Finds node by index in the graph. If no such node exists in the graph, a // `nullptr` is returned. const NodeViewT* GetNode(int node_index) const { - if (node_index < 0 || node_index >= nodes_.size()) { + int nodes_size = nodes_.size(); + if (node_index < 0 || node_index >= nodes_size) { return nullptr; } return &nodes_[node_index]; } NodeViewT* GetNode(int node_index) { - if (node_index < 0 || node_index >= nodes_.size()) { + int nodes_size = nodes_.size(); + if (node_index < 0 || node_index >= nodes_size) { return nullptr; } return &nodes_[node_index]; @@ -444,13 +448,14 @@ inline bool UpdateDevice(NodeViewDiff* diff, template inline bool AddOrUpdateAtIndex(std::vector* v, int i, const U& value, const T& default_value) { - if (i > v->size()) { + int v_size = v->size(); + if (i > v_size) { // Resize to include `value`, filling the newly introduced gap with // `default_value` for later checks of validity (gaps in vector). v->reserve(i + 1); v->resize(i, default_value); v->push_back({value}); - } else if (i == v->size()) { + } else if (i == v_size) { // Vector is large enough, simply append `value` to the end. v->push_back({value}); } else { @@ -494,7 +499,9 @@ inline bool AddOrUpdateRegularFanin(NodeViewDiff* diff, int index, // index from beginning of regular fanins. const int relative_removal_index = num_regular_fanins - index - 1; // Check if at relative index fanin was already marked for removal. - if (relative_removal_index < diff->regular_inputs_to_remove.size() && + int diff_regular_inputs_to_remove_size = + diff->regular_inputs_to_remove.size(); + if (relative_removal_index < diff_regular_inputs_to_remove_size && diff->regular_inputs_to_remove[relative_removal_index]) { // Unmark fanin for removal. diff->regular_inputs_to_remove[relative_removal_index] = false; @@ -543,7 +550,8 @@ inline bool RemoveRegularFanin(NodeViewDiff* diff, int index) { } else { // Relative index from end of regular fanins. const int relative_add_index = index - num_regular_fanins; - if (relative_add_index >= diff->regular_inputs_to_add.size() || + int diff_regular_inputs_to_add_size = diff->regular_inputs_to_add.size(); + if (relative_add_index >= diff_regular_inputs_to_add_size || IsEmptyTensorId(diff->regular_inputs_to_add[relative_add_index])) { // At relative index, appended regular fanin was already marked for // removal. @@ -671,7 +679,8 @@ inline bool IsWellFormed( const absl::flat_hash_map& updated_node_names) { ResizeByTrimmingEndForValue(&diff->regular_inputs_to_remove, false); ResizeByTrimmingEndForValue(&diff->regular_inputs_to_add, EmptyTensorId()); - if (diff->regular_inputs_to_add.size() != diff->num_regular_inputs_to_add) { + int diff_regular_inputs_to_add_size = diff->regular_inputs_to_add.size(); + if (diff_regular_inputs_to_add_size != diff->num_regular_inputs_to_add) { // Missing regular fanins in between appended fanins. return false; } else if (diff->num_regular_inputs_to_add > 0 && @@ -679,7 +688,7 @@ inline bool IsWellFormed( // Appending new fanins while removing existing fanins, resulting in missing // regular fanins in between. return false; - } else if (diff->regular_inputs_to_remove.size() != + } else if (static_cast(diff->regular_inputs_to_remove.size()) != diff->num_regular_inputs_to_remove) { // Regular fanins exist in between removed fanins. return false; @@ -830,7 +839,8 @@ inline void AddOrUpdateRegularFanin(NewNode* new_node, int index, // remove existing fanins and updated/added fanins via AddOrUpdateRegularFanins. template inline void RemoveRegularFanin(NewNode* new_node, int index) { - if (index < 0 || index >= new_node->regular_fanins.size() || + int new_node_regular_fanins_size = new_node->regular_fanins.size(); + if (index < 0 || index >= new_node_regular_fanins_size || IsEmptyTensorId(new_node->regular_fanins[index])) { return; } @@ -874,7 +884,8 @@ inline bool IsWellFormed( NewNode* new_node, const absl::flat_hash_map& updated_node_names) { ResizeByTrimmingEndForValue(&new_node->regular_fanins, EmptyTensorId()); - if (new_node->regular_fanins.size() != new_node->num_regular_fanins) { + int new_node_regular_fanins_size = new_node->regular_fanins.size(); + if (new_node_regular_fanins_size != new_node->num_regular_fanins) { return false; } diff --git a/tensorflow/core/grappler/utils/topological_sort.cc b/tensorflow/core/grappler/utils/topological_sort.cc index e24a457593a..a7bef1c7014 100644 --- a/tensorflow/core/grappler/utils/topological_sort.cc +++ b/tensorflow/core/grappler/utils/topological_sort.cc @@ -81,7 +81,8 @@ Status ComputeTopologicalOrder( int ready_node = (*ready_nodes)[front]; for (int fanout : graph_view.GetFanout(ready_node)) { ++num_ready_inputs[fanout]; - if (num_ready_inputs[fanout] == graph_view.GetFanin(fanout).size()) { + if (num_ready_inputs[fanout] == + static_cast(graph_view.GetFanin(fanout).size())) { ready_nodes->push_back(fanout); ++back; } @@ -95,7 +96,8 @@ Status ComputeTopologicalOrder( "at node = " << graph.node(back).DebugString(); for (int i = 0; i < graph_view.num_nodes(); ++i) { - if (num_ready_inputs[i] != graph_view.GetFanin(i).size()) { + if (num_ready_inputs[i] != + static_cast(graph_view.GetFanin(i).size())) { VLOG(1) << "Node not ready: " << graph.node(i).DebugString(); } } diff --git a/tensorflow/core/kernels/data/experimental/sql/sqlite_query_connection.cc b/tensorflow/core/kernels/data/experimental/sql/sqlite_query_connection.cc index e86cbc7684c..9a7eb125f95 100644 --- a/tensorflow/core/kernels/data/experimental/sql/sqlite_query_connection.cc +++ b/tensorflow/core/kernels/data/experimental/sql/sqlite_query_connection.cc @@ -69,7 +69,7 @@ Status SqliteQueryConnection::GetNext(IteratorContext* ctx, Status SqliteQueryConnection::PrepareQuery() { TF_RETURN_IF_ERROR(db_->Prepare(query_, &stmt_)); int column_count = stmt_.ColumnCount(); - if (column_count != output_types_.size()) { + if (column_count != static_cast(output_types_.size())) { stmt_ = SqliteStatement(); return errors::InvalidArgument(tensorflow::strings::Printf( "The number of columns in query (%d) must match the number of " diff --git a/tensorflow/python/grappler/model_analyzer.cc b/tensorflow/python/grappler/model_analyzer.cc index 5a76cdd8fb2..250010c0fed 100644 --- a/tensorflow/python/grappler/model_analyzer.cc +++ b/tensorflow/python/grappler/model_analyzer.cc @@ -48,7 +48,7 @@ void ModelAnalyzer::PrintNodeInfo(const NodeDef* node, if (properties.HasOutputProperties(node->name())) { const std::vector& props = properties.GetOutputProperties(node->name()); - for (int i = 0; i < props.size(); ++i) { + for (int i = 0, props_size = props.size(); i < props_size; ++i) { const OpInfo::TensorProperties& prop = props[i]; os << "\t" << "output " << i << " (" << DataTypeString(prop.dtype()) @@ -88,7 +88,7 @@ void ModelAnalyzer::PrintNodeInfo(const NodeDef* node, } else if (properties.HasInputProperties(node->name())) { const std::vector& props = properties.GetInputProperties(node->name()); - for (int i = 0; i < props.size(); ++i) { + for (int i = 0, props_size = props.size(); i < props_size; ++i) { const OpInfo::TensorProperties& prop = props[i]; if (prop.has_value()) { os << "\t"