Qualify uses of std::string

PiperOrigin-RevId: 316789814
Change-Id: Ice83a74e70122008e090af3b818b9920abf7f5bc
This commit is contained in:
A. Unique TensorFlower 2020-06-16 17:25:15 -07:00 committed by TensorFlower Gardener
parent 7873e14cf9
commit cdb6e80b21
7 changed files with 53 additions and 52 deletions

View File

@ -47,7 +47,7 @@ class Cluster {
// 2- All the nodes in GraphDef which belong to this cluster. // 2- All the nodes in GraphDef which belong to this cluster.
void SetGraphDefInfo(const tensorflow::GraphDef* graph_def); void SetGraphDefInfo(const tensorflow::GraphDef* graph_def);
const string& GetName() const { return name_; } const std::string& GetName() const { return name_; }
const std::vector<std::unique_ptr<tensorflow::NodeDef>>& GetNewNodes() const { const std::vector<std::unique_ptr<tensorflow::NodeDef>>& GetNewNodes() const {
return new_nodes_; return new_nodes_;
@ -55,18 +55,18 @@ class Cluster {
const std::vector<const tensorflow::NodeDef*>& GetNodes() { return nodes_; } const std::vector<const tensorflow::NodeDef*>& GetNodes() { return nodes_; }
void SetName(const string& name) { name_ = name; } void SetName(const std::string& name) { name_ = name; }
void SetDevice(const string& device) { device_ = device; } void SetDevice(const std::string& device) { device_ = device; }
// Find the input(s) and output(s) of this Cluster. // Find the input(s) and output(s) of this Cluster.
bool FindClusterInputsAndOutputs(); bool FindClusterInputsAndOutputs();
protected: protected:
string name_; std::string name_;
string device_; std::string device_;
std::vector<string> inputs_; std::vector<std::string> inputs_;
std::vector<string> outputs_; std::vector<std::string> outputs_;
// Used to hold the pointers to nodes which are in this cluster. These nodes // Used to hold the pointers to nodes which are in this cluster. These nodes
// are pointing to the nodes in graph_def_. // are pointing to the nodes in graph_def_.

View File

@ -16,8 +16,8 @@ limitations under the License.
#include "tensorflow/lite/toco/toco_types.h" #include "tensorflow/lite/toco/toco_types.h"
namespace toco { namespace toco {
bool StrContains(const string& x, const string& search_pattern) { bool StrContains(const std::string& x, const std::string& search_pattern) {
return x.find(search_pattern) != string::npos; return x.find(search_pattern) != std::string::npos;
} }
void Transpose2DTensor(const float* tensor, int row, int col, void Transpose2DTensor(const float* tensor, int row, int col,

View File

@ -33,7 +33,8 @@ using tensorflow::GraphDef;
using tensorflow::NodeDef; using tensorflow::NodeDef;
void AddNodeToGraph(const NodeDef& node, void AddNodeToGraph(const NodeDef& node,
const std::vector<string>& cluster_names, GraphDef* graph) { const std::vector<std::string>& cluster_names,
GraphDef* graph) {
NodeDef* new_node = graph->add_node(); NodeDef* new_node = graph->add_node();
new_node->set_op(node.op()); new_node->set_op(node.op());
new_node->set_name(node.name()); new_node->set_name(node.name());
@ -41,9 +42,9 @@ void AddNodeToGraph(const NodeDef& node,
// If the inputs are coming from a node which belongs to another cluster, then // If the inputs are coming from a node which belongs to another cluster, then
// those inputs are renamed to the source cluster name. Otherwise the original // those inputs are renamed to the source cluster name. Otherwise the original
// input name is used. // input name is used.
for (const string& node_input : node.input()) { for (const std::string& node_input : node.input()) {
bool input_from_cluster = false; bool input_from_cluster = false;
for (const string& cluster_name : cluster_names) { for (const std::string& cluster_name : cluster_names) {
if (StrContains(node_input, cluster_name) && if (StrContains(node_input, cluster_name) &&
!StrContains(node.name(), cluster_name)) { !StrContains(node.name(), cluster_name)) {
new_node->add_input(cluster_name); new_node->add_input(cluster_name);
@ -62,7 +63,7 @@ void AddNodeToGraph(const NodeDef& node,
bool FindCluster(const ClusterFactoryInterface& cluster_factory, bool FindCluster(const ClusterFactoryInterface& cluster_factory,
const GraphDef& graph_def, const GraphDef& graph_def,
std::unordered_map<string, bool>* is_node_in_cluster, std::unordered_map<std::string, bool>* is_node_in_cluster,
std::vector<std::unique_ptr<Cluster>>* clusters) { std::vector<std::unique_ptr<Cluster>>* clusters) {
for (const NodeDef& node : graph_def.node()) { for (const NodeDef& node : graph_def.node()) {
// If the node is not assigned to any cluster, then we check if it belong to // If the node is not assigned to any cluster, then we check if it belong to
@ -90,12 +91,12 @@ std::unique_ptr<GraphDef> MaybeResolveClusters(
std::unique_ptr<GraphDef> pruned_graph(new GraphDef); std::unique_ptr<GraphDef> pruned_graph(new GraphDef);
// The structure to keep track of which cluster each node is assigned to, and // The structure to keep track of which cluster each node is assigned to, and
// to initialize them to all un-assigned, // to initialize them to all un-assigned,
std::unordered_map<string, bool> is_node_in_cluster; std::unordered_map<std::string, bool> is_node_in_cluster;
for (const NodeDef& node : graph_def.node()) { for (const NodeDef& node : graph_def.node()) {
is_node_in_cluster[node.name()] = false; is_node_in_cluster[node.name()] = false;
} }
std::vector<string> cluster_names; std::vector<std::string> cluster_names;
std::vector<std::unique_ptr<Cluster>> all_clusters; std::vector<std::unique_ptr<Cluster>> all_clusters;
// Find the clusters for all available cluster factories. // Find the clusters for all available cluster factories.
for (const ClusterFactoryInterface* cluster_factory : cluster_factories) { for (const ClusterFactoryInterface* cluster_factory : cluster_factories) {

View File

@ -40,7 +40,7 @@ std::unique_ptr<tensorflow::GraphDef> MaybeResolveClusters(
// belongs to another cluster, then those inputs are renamed to the source // belongs to another cluster, then those inputs are renamed to the source
// cluster name. // cluster name.
void AddNodeToGraph(const tensorflow::NodeDef& node, void AddNodeToGraph(const tensorflow::NodeDef& node,
const std::vector<string>& cluster_names, const std::vector<std::string>& cluster_names,
tensorflow::GraphDef* graph); tensorflow::GraphDef* graph);
// Given a graph and a cluster class, it finds all the nodes which belong to a // Given a graph and a cluster class, it finds all the nodes which belong to a
@ -49,7 +49,7 @@ void AddNodeToGraph(const tensorflow::NodeDef& node,
// they belong to the generated clusters. // they belong to the generated clusters.
bool FindCluster(const ClusterFactoryInterface& cluster_factory, bool FindCluster(const ClusterFactoryInterface& cluster_factory,
const tensorflow::GraphDef& graph_def, const tensorflow::GraphDef& graph_def,
std::unordered_map<string, bool>* is_node_in_cluster, std::unordered_map<std::string, bool>* is_node_in_cluster,
std::vector<std::unique_ptr<Cluster>>* clusters); std::vector<std::unique_ptr<Cluster>>* clusters);
// Receives a graph and generates another graph by replacing the cluster of // Receives a graph and generates another graph by replacing the cluster of

View File

@ -47,11 +47,11 @@ namespace {
// Since these nodes are connected to a Concatenate node, it makes sure the // Since these nodes are connected to a Concatenate node, it makes sure the
// axis value input of the Concatenate operator is 0. // axis value input of the Concatenate operator is 0.
void FilterPartitionedConstNodes( void FilterPartitionedConstNodes(
const string& const_pattern, const std::string& const_pattern,
const std::vector<const NodeDef*>& cluster_nodes, const std::vector<const NodeDef*>& cluster_nodes,
std::vector<const NodeDef*>* const_node_parts) { std::vector<const NodeDef*>* const_node_parts) {
for (const NodeDef* node : cluster_nodes) { for (const NodeDef* node : cluster_nodes) {
string node_name_to_upper = node->name(); std::string node_name_to_upper = node->name();
std::transform(node_name_to_upper.begin(), node_name_to_upper.end(), std::transform(node_name_to_upper.begin(), node_name_to_upper.end(),
node_name_to_upper.begin(), ::toupper); node_name_to_upper.begin(), ::toupper);
if (StrContains(node->name(), const_pattern) && node->op() == "Const") { if (StrContains(node->name(), const_pattern) && node->op() == "Const") {
@ -97,7 +97,7 @@ int SvdfCluster::InferFilterRank() {
} }
void SvdfCluster::CreateNodes() { void SvdfCluster::CreateNodes() {
for (const string& const_pattern : const_node_patterns_) { for (const std::string& const_pattern : const_node_patterns_) {
CreateConstNode(const_pattern); CreateConstNode(const_pattern);
} }
std::unique_ptr<tensorflow::NodeDef> svdf_node(new NodeDef); std::unique_ptr<tensorflow::NodeDef> svdf_node(new NodeDef);
@ -110,14 +110,14 @@ void SvdfCluster::CreateNodes() {
// Add the rest of the inputs to Svdf cell: weights and bias. // Add the rest of the inputs to Svdf cell: weights and bias.
CHECK(new_nodes_.size() == 3 || new_nodes_.size() == 2); CHECK(new_nodes_.size() == 3 || new_nodes_.size() == 2);
string* weights_feature_input = svdf_node->add_input(); std::string* weights_feature_input = svdf_node->add_input();
string* weights_time_input = svdf_node->add_input(); std::string* weights_time_input = svdf_node->add_input();
string* bias_input; std::string* bias_input;
if (new_nodes_.size() == 3) { if (new_nodes_.size() == 3) {
bias_input = svdf_node->add_input(); bias_input = svdf_node->add_input();
} }
for (const std::unique_ptr<tensorflow::NodeDef>& node : new_nodes_) { for (const std::unique_ptr<tensorflow::NodeDef>& node : new_nodes_) {
const string node_name = node->name(); const std::string node_name = node->name();
if (StrContains(node_name, "SVDF_weights_feature")) { if (StrContains(node_name, "SVDF_weights_feature")) {
*weights_feature_input = node_name; *weights_feature_input = node_name;
} else if (StrContains(node_name, "SVDF_weights_time")) { } else if (StrContains(node_name, "SVDF_weights_time")) {
@ -136,7 +136,7 @@ void SvdfCluster::CreateNodes() {
CHECK_GT(rank, 0); CHECK_GT(rank, 0);
// Add Svdf activation and rank. // Add Svdf activation and rank.
string activation_function = std::string activation_function =
StrContains(outputs_[0], "Relu") ? "Relu" : "None"; StrContains(outputs_[0], "Relu") ? "Relu" : "None";
(*svdf_node->mutable_attr())["ActivationFunction"].set_s(activation_function); (*svdf_node->mutable_attr())["ActivationFunction"].set_s(activation_function);
(*svdf_node->mutable_attr())["Rank"].set_i(rank); (*svdf_node->mutable_attr())["Rank"].set_i(rank);
@ -145,7 +145,7 @@ void SvdfCluster::CreateNodes() {
new_nodes_.push_back(std::move(svdf_node)); new_nodes_.push_back(std::move(svdf_node));
} }
void SvdfCluster::CreateConstNode(const string& const_pattern) { void SvdfCluster::CreateConstNode(const std::string& const_pattern) {
// Find the nodes with pattern like: "const_pattern"/part_xxx of type Const. // Find the nodes with pattern like: "const_pattern"/part_xxx of type Const.
std::vector<const NodeDef*> const_node_parts; std::vector<const NodeDef*> const_node_parts;
FilterPartitionedConstNodes(const_pattern, nodes_, &const_node_parts); FilterPartitionedConstNodes(const_pattern, nodes_, &const_node_parts);
@ -236,15 +236,15 @@ void SvdfCluster::MaybeMergeConstNodes(
// Set the tensor attributes. // Set the tensor attributes.
allocated_tensor->set_tensor_content( allocated_tensor->set_tensor_content(
string(reinterpret_cast<const char*>(transposed_tensor.get()), std::string(reinterpret_cast<const char*>(transposed_tensor.get()),
allocated_content_flat_size)); allocated_content_flat_size));
} else { } else {
tensor_shape_dim0->set_size(dim0_size); tensor_shape_dim0->set_size(dim0_size);
// Set the tensor attributes. // Set the tensor attributes.
allocated_tensor->set_tensor_content( allocated_tensor->set_tensor_content(
string(reinterpret_cast<const char*>(allocated_content.get()), std::string(reinterpret_cast<const char*>(allocated_content.get()),
allocated_content_flat_size)); allocated_content_flat_size));
} }
} }
@ -252,21 +252,21 @@ void SvdfCluster::MaybeMergeConstNodes(
std::unique_ptr<Cluster> SvdfClusterFactory::CreateCluster( std::unique_ptr<Cluster> SvdfClusterFactory::CreateCluster(
const NodeDef& node, const GraphDef& graph_def) const { const NodeDef& node, const GraphDef& graph_def) const {
std::vector<string> node_patterns = {"SVDF_weights_feature", std::vector<std::string> node_patterns = {"SVDF_weights_feature",
"SVDF_weights_time", "SVDF_bias"}; "SVDF_weights_time", "SVDF_bias"};
string node_name_to_upper = node.name(); std::string node_name_to_upper = node.name();
std::transform(node_name_to_upper.begin(), node_name_to_upper.end(), std::transform(node_name_to_upper.begin(), node_name_to_upper.end(),
node_name_to_upper.begin(), ::toupper); node_name_to_upper.begin(), ::toupper);
std::unique_ptr<SvdfCluster> cluster = nullptr; std::unique_ptr<SvdfCluster> cluster = nullptr;
if (node_name_to_upper.find("SVDF", 0) != string::npos) { if (node_name_to_upper.find("SVDF", 0) != std::string::npos) {
size_t weights_pos = node.name().find(node_patterns[0]); size_t weights_pos = node.name().find(node_patterns[0]);
if (weights_pos != string::npos) { if (weights_pos != std::string::npos) {
// Assuming the node name has a pattern like: // Assuming the node name has a pattern like:
// "SOMESTRING1/CELLNAME/SEARCH_PATTERN/SOMESTRING2", we use // "SOMESTRING1/CELLNAME/SEARCH_PATTERN/SOMESTRING2", we use
// CELLNAME as the cluster name. // CELLNAME as the cluster name.
size_t cell_pos = node.name().rfind("/", weights_pos - 2) + 1; size_t cell_pos = node.name().rfind("/", weights_pos - 2) + 1;
string cell_name = std::string cell_name =
node.name().substr(cell_pos, weights_pos - cell_pos - 1); node.name().substr(cell_pos, weights_pos - cell_pos - 1);
cluster = std::unique_ptr<SvdfCluster>(new SvdfCluster); cluster = std::unique_ptr<SvdfCluster>(new SvdfCluster);
cluster->SetName(cell_name); cluster->SetName(cell_name);
@ -274,7 +274,7 @@ std::unique_ptr<Cluster> SvdfClusterFactory::CreateCluster(
cluster->SetGraphDefInfo(&graph_def); cluster->SetGraphDefInfo(&graph_def);
CHECK(cluster->FindClusterInputsAndOutputs()); CHECK(cluster->FindClusterInputsAndOutputs());
for (const string& const_pattern : node_patterns) { for (const std::string& const_pattern : node_patterns) {
cluster->AddConstNodePattern(const_pattern); cluster->AddConstNodePattern(const_pattern);
} }
} }

View File

@ -36,7 +36,7 @@ class SvdfCluster : public Cluster {
// A helper function to set the pattern of Const nodes which CreateNodes() // A helper function to set the pattern of Const nodes which CreateNodes()
// should handle specially. // should handle specially.
void AddConstNodePattern(const string& const_pattern) { void AddConstNodePattern(const std::string& const_pattern) {
const_node_patterns_.push_back(const_pattern); const_node_patterns_.push_back(const_pattern);
} }
@ -46,7 +46,7 @@ class SvdfCluster : public Cluster {
// The main function which is used to create Const nodes for this cluster. // The main function which is used to create Const nodes for this cluster.
// These Const nodes are the inputs to the composite op generated for this // These Const nodes are the inputs to the composite op generated for this
// cluster. // cluster.
void CreateConstNode(const string& const_pattern); void CreateConstNode(const std::string& const_pattern);
// Receives a vector of Const nodes, merge them (if necessary) and returns // Receives a vector of Const nodes, merge them (if necessary) and returns
// only one Const node holding all the arrays contents. It transposes it if // only one Const node holding all the arrays contents. It transposes it if
@ -61,7 +61,7 @@ class SvdfCluster : public Cluster {
// shape to [num_units, rank, batch] shape. The 2nd shape element is rank. // shape to [num_units, rank, batch] shape. The 2nd shape element is rank.
int InferFilterRank(); int InferFilterRank();
std::vector<string> const_node_patterns_; std::vector<std::string> const_node_patterns_;
}; };
class SvdfClusterFactory : public ClusterFactoryInterface { class SvdfClusterFactory : public ClusterFactoryInterface {

View File

@ -77,8 +77,8 @@ class ResolveSvdfTest : public ::testing::Test {
~ResolveSvdfTest() override {} ~ResolveSvdfTest() override {}
protected: protected:
void AddNewNode(const string& name, const string& op, void AddNewNode(const std::string& name, const std::string& op,
const std::vector<string>& inputs) { const std::vector<std::string>& inputs) {
NodeDef* node = graph_.add_node(); NodeDef* node = graph_.add_node();
node->set_name(name); node->set_name(name);
node->set_op(op); node->set_op(op);
@ -89,8 +89,8 @@ class ResolveSvdfTest : public ::testing::Test {
} }
} }
void AddNewNode(const string& name, const string& op, void AddNewNode(const std::string& name, const std::string& op,
const std::vector<string>& inputs, const std::vector<std::string>& inputs,
const std::vector<float>& values) { const std::vector<float>& values) {
NodeDef* node = graph_.add_node(); NodeDef* node = graph_.add_node();
node->set_name(name); node->set_name(name);
@ -109,12 +109,12 @@ class ResolveSvdfTest : public ::testing::Test {
tensor_shape_dim0->set_size(values.size()); tensor_shape_dim0->set_size(values.size());
allocated_tensor->set_allocated_tensor_shape(allocated_tensor_shape); allocated_tensor->set_allocated_tensor_shape(allocated_tensor_shape);
allocated_tensor->set_tensor_content( allocated_tensor->set_tensor_content(
string(reinterpret_cast<const char*>(values.data()), std::string(reinterpret_cast<const char*>(values.data()),
values.size() * sizeof(float))); values.size() * sizeof(float)));
(*node->mutable_attr())["value"].set_allocated_tensor(allocated_tensor); (*node->mutable_attr())["value"].set_allocated_tensor(allocated_tensor);
} }
void AddShapeNode(const string& name, const std::vector<int>& values) { void AddShapeNode(const std::string& name, const std::vector<int>& values) {
NodeDef* node = graph_.add_node(); NodeDef* node = graph_.add_node();
node->set_name(name); node->set_name(name);
node->set_op("Const"); node->set_op("Const");
@ -128,8 +128,8 @@ class ResolveSvdfTest : public ::testing::Test {
tensor_shape_dim0->set_size(values.size()); tensor_shape_dim0->set_size(values.size());
allocated_tensor->set_allocated_tensor_shape(allocated_tensor_shape); allocated_tensor->set_allocated_tensor_shape(allocated_tensor_shape);
allocated_tensor->set_tensor_content( allocated_tensor->set_tensor_content(
string(reinterpret_cast<const char*>(values.data()), std::string(reinterpret_cast<const char*>(values.data()),
values.size() * sizeof(int))); values.size() * sizeof(int)));
(*node->mutable_attr())["value"].set_allocated_tensor(allocated_tensor); (*node->mutable_attr())["value"].set_allocated_tensor(allocated_tensor);
} }
@ -157,12 +157,12 @@ TEST_F(ResolveSvdfTest, TestTranspose2DTensor) {
} }
TEST_F(ResolveSvdfTest, TestResolveSvdfFlow) { TEST_F(ResolveSvdfTest, TestResolveSvdfFlow) {
std::unordered_map<string, bool> is_node_in_cluster; std::unordered_map<std::string, bool> is_node_in_cluster;
for (const NodeDef& node : graph_.node()) { for (const NodeDef& node : graph_.node()) {
is_node_in_cluster[node.name()] = false; is_node_in_cluster[node.name()] = false;
} }
std::vector<string> cluster_names; std::vector<std::string> cluster_names;
CHECK(FindCluster(svdf_cluster_factory_, graph_, &is_node_in_cluster, CHECK(FindCluster(svdf_cluster_factory_, graph_, &is_node_in_cluster,
&clusters_)); &clusters_));
@ -174,7 +174,7 @@ TEST_F(ResolveSvdfTest, TestResolveSvdfFlow) {
EXPECT_THAT(cluster_names, EXPECT_THAT(cluster_names,
testing::UnorderedElementsAreArray({"Svdf1", "Svdf2"})); testing::UnorderedElementsAreArray({"Svdf1", "Svdf2"}));
std::vector<string> new_node_names; std::vector<std::string> new_node_names;
std::vector<float> content_array(3); std::vector<float> content_array(3);
for (const std::unique_ptr<Cluster>& cluster : clusters_) { for (const std::unique_ptr<Cluster>& cluster : clusters_) {
// After CreateNodes in each cluster we have three nodes: Svdf, // After CreateNodes in each cluster we have three nodes: Svdf,