diff options
author | 2017-11-13 11:41:38 -0800 | |
---|---|---|
committer | 2017-11-13 11:53:36 -0800 | |
commit | 0a79d4ab6ab961d7f36cbb3a14cfaff2152e70fd (patch) | |
tree | 07a87e026711ea484e0fdf740a78960d541ddce7 | |
parent | 73bc96ffc009283058c9d55b494745631a931814 (diff) |
Moved tensorflow::StringPiece::Hasher out of tensorflow::StringPiece and renamed it tensorflow::StringPieceHasher. This allows tensorflow::StringPiece to be more easily replaced with absl::string_view (which does not contain a Hasher struct).
PiperOrigin-RevId: 175563786
-rw-r--r-- | tensorflow/cc/framework/cc_op_gen.cc | 119 | ||||
-rw-r--r-- | tensorflow/core/common_runtime/device_mgr.h | 2 | ||||
-rw-r--r-- | tensorflow/core/common_runtime/direct_session.cc | 2 | ||||
-rw-r--r-- | tensorflow/core/common_runtime/direct_session.h | 3 | ||||
-rw-r--r-- | tensorflow/core/common_runtime/placer.cc | 4 | ||||
-rw-r--r-- | tensorflow/core/common_runtime/step_stats_collector.cc | 4 | ||||
-rw-r--r-- | tensorflow/core/distributed_runtime/master_session.cc | 4 | ||||
-rw-r--r-- | tensorflow/core/framework/variant_op_registry.h | 9 | ||||
-rw-r--r-- | tensorflow/core/graph/costmodel.h | 2 | ||||
-rw-r--r-- | tensorflow/core/graph/graph_constructor.cc | 6 | ||||
-rw-r--r-- | tensorflow/core/graph/quantize_training.cc | 4 | ||||
-rw-r--r-- | tensorflow/core/graph/subgraph.h | 2 | ||||
-rw-r--r-- | tensorflow/core/lib/core/stringpiece.cc | 2 | ||||
-rw-r--r-- | tensorflow/core/lib/core/stringpiece.h | 8 | ||||
-rw-r--r-- | tensorflow/tools/graph_transforms/fold_constants_lib.cc | 4 |
15 files changed, 128 insertions, 47 deletions
diff --git a/tensorflow/cc/framework/cc_op_gen.cc b/tensorflow/cc/framework/cc_op_gen.cc index 38a17598b8..7a1b2a012d 100644 --- a/tensorflow/cc/framework/cc_op_gen.cc +++ b/tensorflow/cc/framework/cc_op_gen.cc @@ -297,7 +297,7 @@ string ToCamelCase(const string& str) { // argument to a function. std::pair<const char*, bool> AttrTypeName(StringPiece attr_type) { static const std::unordered_map<StringPiece, std::pair<const char*, bool>, - StringPiece::Hasher> + StringPieceHasher> attr_type_map{ {"string", {"StringPiece", false}}, {"list(string)", {"gtl::ArraySlice<string>", true}}, @@ -325,29 +325,112 @@ std::pair<const char*, bool> AttrTypeName(StringPiece attr_type) { } bool IsCPPKeyword(StringPiece name) { - static const std::unordered_set<StringPiece, StringPiece::Hasher> + static const std::unordered_set<StringPiece, StringPieceHasher> // Keywords obtained from http://en.cppreference.com/w/cpp/keyword kCPPReserved{ - "alignas", "alignof", "and", "and_eq", "asm", "atomic_cancel", - "atomic_commit", "atomic_noexcept", "auto", "bitand", "bitor", "bool", - "break", "case", "catch", "char", "char16_t", "char32_t", "class", - "compl", "concept", "const", "const_cast", "constexpr", "continue", - "decltype", "default", "delete", "do", "double", "dynamic_cast", - "else", "enum", "explicit", "export", "extern", "false", "final", - "float", "for", "friend", "goto", "if", "import", "inline", "int", - "long", "module", "mutable", "namespace", "new", "noexcept", "not", - "not_eq", "nullptr", "operator", "or", "or_eq", "override", "private", - "protected", "public", "register", "reinterpret_cast", "requires", - "return", "short", "signed", "sizeof", "static", "static_assert", - "static_cast", "struct", "switch", "synchronized", "template", "this", - "thread_local", "throw", "true", "try", "typedef", "typeid", - "typename", "union", "unsigned", "using", "virtual", "void", - "volatile", "wchar_t", "while", "xor", "xor_eq", + "alignas", + "alignof", + "and", + "and_eq", + "asm", + "atomic_cancel", + "atomic_commit", + "atomic_noexcept", + "auto", + "bitand", + "bitor", + "bool", + "break", + "case", + "catch", + "char", + "char16_t", + "char32_t", + "class", + "compl", + "concept", + "const", + "const_cast", + "constexpr", + "continue", + "decltype", + "default", + "delete", + "do", + "double", + "dynamic_cast", + "else", + "enum", + "explicit", + "export", + "extern", + "false", + "final", + "float", + "for", + "friend", + "goto", + "if", + "import", + "inline", + "int", + "long", + "module", + "mutable", + "namespace", + "new", + "noexcept", + "not", + "not_eq", + "nullptr", + "operator", + "or", + "or_eq", + "override", + "private", + "protected", + "public", + "register", + "reinterpret_cast", + "requires", + "return", + "short", + "signed", + "sizeof", + "static", + "static_assert", + "static_cast", + "struct", + "switch", + "synchronized", + "template", + "this", + "thread_local", + "throw", + "true", + "try", + "typedef", + "typeid", + "typename", + "union", + "unsigned", + "using", + "virtual", + "void", + "volatile", + "wchar_t", + "while", + "xor", + "xor_eq", // The following are not C++ keywords, but names of local variables // and parameters used in the op constructor. Treating them as // keywords, so that other parameter names don't conflict with these. - "builder", "node", "ret", "scope", "unique_name", + "builder", + "node", + "ret", + "scope", + "unique_name", }; return kCPPReserved.count(name) > 0; } diff --git a/tensorflow/core/common_runtime/device_mgr.h b/tensorflow/core/common_runtime/device_mgr.h index d16681ac59..cd93f76324 100644 --- a/tensorflow/core/common_runtime/device_mgr.h +++ b/tensorflow/core/common_runtime/device_mgr.h @@ -68,7 +68,7 @@ class DeviceMgr { StringPiece CopyToBackingStore(StringPiece s); - std::unordered_map<StringPiece, Device*, StringPiece::Hasher> device_map_; + std::unordered_map<StringPiece, Device*, StringPieceHasher> device_map_; core::Arena name_backing_store_; // Storage for keys in device_map_ std::unordered_map<string, int> device_type_counts_; diff --git a/tensorflow/core/common_runtime/direct_session.cc b/tensorflow/core/common_runtime/direct_session.cc index 316fb0ac16..d1dc826a6e 100644 --- a/tensorflow/core/common_runtime/direct_session.cc +++ b/tensorflow/core/common_runtime/direct_session.cc @@ -1135,7 +1135,7 @@ Status DirectSession::GetOrCreateExecutors( if (run_state_args->is_partial_run) { ek->graph = std::move(run_state_args->graph); - std::unordered_set<StringPiece, StringPiece::Hasher> names; + std::unordered_set<StringPiece, StringPieceHasher> names; for (const string& input : inputs) { TensorId id(ParseTensorName(input)); names.emplace(id.first); diff --git a/tensorflow/core/common_runtime/direct_session.h b/tensorflow/core/common_runtime/direct_session.h index 7fbabf6d81..780d0b46a8 100644 --- a/tensorflow/core/common_runtime/direct_session.h +++ b/tensorflow/core/common_runtime/direct_session.h @@ -64,8 +64,7 @@ class DirectSession : public Session { ~DirectSession() override; typedef std::vector<std::pair<string, Tensor>> NamedTensorList; - typedef std::unordered_map<StringPiece, Node*, StringPiece::Hasher> - NameNodeMap; + typedef std::unordered_map<StringPiece, Node*, StringPieceHasher> NameNodeMap; ::tensorflow::Status Create(const GraphDef& graph) override; ::tensorflow::Status Extend(const GraphDef& graph) override; diff --git a/tensorflow/core/common_runtime/placer.cc b/tensorflow/core/common_runtime/placer.cc index 73fdf60fd5..54f082e823 100644 --- a/tensorflow/core/common_runtime/placer.cc +++ b/tensorflow/core/common_runtime/placer.cc @@ -129,7 +129,7 @@ class ColocationGraph { // 'string' values stored in NodeDef attribute lists, as well as StringPiece // values that refer to 'string' values from NodeDef::name(), without // performing any string allocations. - std::unordered_map<StringPiece, const Node*, StringPiece::Hasher> + std::unordered_map<StringPiece, const Node*, StringPieceHasher> colocation_group_root; for (Node* node : graph_->nodes()) { @@ -171,7 +171,7 @@ class ColocationGraph { } Status ColocateNodeToGroup( - std::unordered_map<StringPiece, const Node*, StringPiece::Hasher>* + std::unordered_map<StringPiece, const Node*, StringPieceHasher>* colocation_group_root, Node* node, StringPiece colocation_group) { const Node*& root_node = (*colocation_group_root)[colocation_group]; diff --git a/tensorflow/core/common_runtime/step_stats_collector.cc b/tensorflow/core/common_runtime/step_stats_collector.cc index e6403df97f..ba8e555f36 100644 --- a/tensorflow/core/common_runtime/step_stats_collector.cc +++ b/tensorflow/core/common_runtime/step_stats_collector.cc @@ -139,7 +139,7 @@ void StepStatsCollector::BuildCostModel( const DeviceStepStats* hardware_stats; }; - std::unordered_map<StringPiece, DeviceStats, StringPiece::Hasher> + std::unordered_map<StringPiece, DeviceStats, StringPieceHasher> per_device_stats; std::unordered_map<int, const DeviceStepStats*> gpu_hardware_stats; @@ -179,7 +179,7 @@ void StepStatsCollector::BuildCostModel( CostModel* cm = cost_model_manager->FindOrCreateCostModel(graph); cm->IncrementUpdateTimes(); - std::unordered_map<StringPiece, Node*, StringPiece::Hasher> name_to_node; + std::unordered_map<StringPiece, Node*, StringPieceHasher> name_to_node; for (Node* n : graph->nodes()) { name_to_node.emplace(n->name(), n); } diff --git a/tensorflow/core/distributed_runtime/master_session.cc b/tensorflow/core/distributed_runtime/master_session.cc index f7fce1d0ec..7617158d66 100644 --- a/tensorflow/core/distributed_runtime/master_session.cc +++ b/tensorflow/core/distributed_runtime/master_session.cc @@ -208,7 +208,7 @@ class MasterSession::ReffedClientGraph : public core::RefCounted { const bool is_partial_; const DebugOptions& debug_opts_; WorkerCacheInterface* const worker_cache_; // Not owned. - std::unordered_map<StringPiece, Node*, StringPiece::Hasher> name_to_node_; + std::unordered_map<StringPiece, Node*, StringPieceHasher> name_to_node_; // Graph partitioned into per-location subgraphs. struct Part { @@ -486,7 +486,7 @@ Status MasterSession::ReffedClientGraph::RunPartitions( VLOG(2) << "RunPartitions step_id " << step_id << " execution_count " << execution_count; // Maps the names of fed tensors to their index in `req`. - std::unordered_map<StringPiece, size_t, StringPiece::Hasher> feeds(3); + std::unordered_map<StringPiece, size_t, StringPieceHasher> feeds(3); for (size_t i = 0; i < req.num_feeds(); ++i) { if (!feeds.insert({req.feed_name(i), i}).second) { diff --git a/tensorflow/core/framework/variant_op_registry.h b/tensorflow/core/framework/variant_op_registry.h index 831dbd3dff..13f6908cae 100644 --- a/tensorflow/core/framework/variant_op_registry.h +++ b/tensorflow/core/framework/variant_op_registry.h @@ -145,9 +145,8 @@ class UnaryVariantOpRegistry { static std::unordered_set<string>* PersistentStringStorage(); private: - std::unordered_map<StringPiece, VariantShapeFn, StringPiece::Hasher> - shape_fns; - std::unordered_map<StringPiece, VariantDecodeFn, StringPiece::Hasher> + std::unordered_map<StringPiece, VariantShapeFn, StringPieceHasher> shape_fns; + std::unordered_map<StringPiece, VariantDecodeFn, StringPieceHasher> decode_fns; // Map std::pair<Direction, type_name> to function. @@ -159,7 +158,7 @@ class UnaryVariantOpRegistry { ret = Hash64Combine(ret, sp_hasher_(std::get<1>(x))); return ret; } - StringPiece::Hasher sp_hasher_; + StringPieceHasher sp_hasher_; }; std::unordered_map<std::pair<VariantDeviceCopyDirection, StringPiece>, @@ -177,7 +176,7 @@ class UnaryVariantOpRegistry { ret = Hash64Combine(ret, sp_hasher_(std::get<2>(x))); return ret; } - StringPiece::Hasher sp_hasher_; + StringPieceHasher sp_hasher_; }; std::unordered_map<std::tuple<VariantUnaryOp, StringPiece, StringPiece>, VariantUnaryOpFn, TupleHash> diff --git a/tensorflow/core/graph/costmodel.h b/tensorflow/core/graph/costmodel.h index a908a4843c..8afa4971ad 100644 --- a/tensorflow/core/graph/costmodel.h +++ b/tensorflow/core/graph/costmodel.h @@ -30,7 +30,7 @@ limitations under the License. #include "tensorflow/core/platform/protobuf.h" namespace tensorflow { -typedef std::unordered_map<StringPiece, int32, StringPiece::Hasher> +typedef std::unordered_map<StringPiece, int32, StringPieceHasher> NodeNameToCostIdMap; class StepStats; diff --git a/tensorflow/core/graph/graph_constructor.cc b/tensorflow/core/graph/graph_constructor.cc index 2ee409768b..ebaffeb50e 100644 --- a/tensorflow/core/graph/graph_constructor.cc +++ b/tensorflow/core/graph/graph_constructor.cc @@ -241,13 +241,13 @@ class GraphConstructor { }; // TODO(vrv): Profile this data structure to see if we should use an // alternative implementation of std::unordered_map. - std::unordered_map<StringPiece, NodeInfo, StringPiece::Hasher> gdef_nodes_; + std::unordered_map<StringPiece, NodeInfo, StringPieceHasher> gdef_nodes_; // Mapping from node name to the existing node in g_. - std::unordered_map<StringPiece, Node*, StringPiece::Hasher> existing_nodes_; + std::unordered_map<StringPiece, Node*, StringPieceHasher> existing_nodes_; // Prefixes already used in the graph. - std::unordered_set<StringPiece, StringPiece::Hasher> existing_prefixes_; + std::unordered_set<StringPiece, StringPieceHasher> existing_prefixes_; // Imported node names that have been uniquified. The key is the original // name, the value is the new unique name. diff --git a/tensorflow/core/graph/quantize_training.cc b/tensorflow/core/graph/quantize_training.cc index d9cb55f448..cb0fc8a154 100644 --- a/tensorflow/core/graph/quantize_training.cc +++ b/tensorflow/core/graph/quantize_training.cc @@ -42,7 +42,7 @@ const float kEMADecay = 0.999; // Node types to rewrite. Insert quantize_and_dequantize op for their inputs. const auto* nodes_to_rewrite = - new std::unordered_set<string, StringPiece::Hasher>{"MatMul", "Conv2D"}; + new std::unordered_set<string, StringPieceHasher>{"MatMul", "Conv2D"}; // Contains necessary parameters to convert an edge. struct EdgeToConvert { @@ -563,7 +563,7 @@ Status ProcessTargetEdges(Graph* graph, const string& quant_op_type, const std::vector<EdgeToConvert>& target_edges) { // Remember previously converted ops to avoid duplicated conversion on the // same input. - std::unordered_map<string, Node*, StringPiece::Hasher> name_index; + std::unordered_map<string, Node*, StringPieceHasher> name_index; std::vector<Node*> added_variables; for (const EdgeToConvert edge : target_edges) { Node* convert_node; diff --git a/tensorflow/core/graph/subgraph.h b/tensorflow/core/graph/subgraph.h index 8ccc27914b..3c1f8870f5 100644 --- a/tensorflow/core/graph/subgraph.h +++ b/tensorflow/core/graph/subgraph.h @@ -71,7 +71,7 @@ Status RewriteGraphForExecution( const DeviceAttributes& device_info, bool use_function_convention, RewriteGraphMetadata* out_metadata); -typedef std::unordered_map<StringPiece, Node*, StringPiece::Hasher> NameIndex; +typedef std::unordered_map<StringPiece, Node*, StringPieceHasher> NameIndex; // Augment "*g" by adding special "fetch" nodes that connect to the // tensor outputs specified in "fetch_outputs" to retrieve the output diff --git a/tensorflow/core/lib/core/stringpiece.cc b/tensorflow/core/lib/core/stringpiece.cc index 984f4404ce..29b727fc44 100644 --- a/tensorflow/core/lib/core/stringpiece.cc +++ b/tensorflow/core/lib/core/stringpiece.cc @@ -21,7 +21,7 @@ limitations under the License. namespace tensorflow { -size_t StringPiece::Hasher::operator()(StringPiece s) const { +size_t StringPieceHasher::operator()(StringPiece s) const { return Hash64(s.data(), s.size()); } diff --git a/tensorflow/core/lib/core/stringpiece.h b/tensorflow/core/lib/core/stringpiece.h index 94f4a377f1..53af116465 100644 --- a/tensorflow/core/lib/core/stringpiece.h +++ b/tensorflow/core/lib/core/stringpiece.h @@ -103,10 +103,6 @@ class StringPiece { StringPiece substr(size_t pos, size_t n = npos) const; - struct Hasher { - size_t operator()(StringPiece arg) const; - }; - // Return a string that contains the copy of the referenced data. std::string ToString() const { return std::string(data_, size_); } @@ -133,6 +129,10 @@ class StringPiece { // Intentionally copyable }; +struct StringPieceHasher { + size_t operator()(StringPiece s) const; +}; + inline bool operator==(StringPiece x, StringPiece y) { return ((x.size() == y.size()) && (memcmp(x.data(), y.data(), x.size()) == 0)); diff --git a/tensorflow/tools/graph_transforms/fold_constants_lib.cc b/tensorflow/tools/graph_transforms/fold_constants_lib.cc index f2934a79bd..250f54e20f 100644 --- a/tensorflow/tools/graph_transforms/fold_constants_lib.cc +++ b/tensorflow/tools/graph_transforms/fold_constants_lib.cc @@ -39,9 +39,9 @@ limitations under the License. namespace tensorflow { namespace graph_transforms { namespace { -using StringPieceSet = std::unordered_set<StringPiece, StringPiece::Hasher>; +using StringPieceSet = std::unordered_set<StringPiece, StringPieceHasher>; template <typename T> -using StringPieceMap = std::unordered_map<StringPiece, T, StringPiece::Hasher>; +using StringPieceMap = std::unordered_map<StringPiece, T, StringPieceHasher>; } // namespace Status ReplaceSendRecvs(const GraphDef& original_graph_def, |