diff --git a/tensorflow/core/profiler/convert/BUILD b/tensorflow/core/profiler/convert/BUILD index e08a2c543f7..2754773f333 100644 --- a/tensorflow/core/profiler/convert/BUILD +++ b/tensorflow/core/profiler/convert/BUILD @@ -203,7 +203,6 @@ cc_library( "//tensorflow/core/profiler/protobuf:steps_db_proto_cc", "//tensorflow/core/profiler/protobuf:xplane_proto_cc", "//tensorflow/core/profiler/utils:event_span", - "//tensorflow/core/profiler/utils:metadata_matcher", "//tensorflow/core/profiler/utils:tf_xplane_visitor", "//tensorflow/core/profiler/utils:trace_utils", "//tensorflow/core/profiler/utils:xplane_schema", diff --git a/tensorflow/core/profiler/convert/xplane_to_step_events.cc b/tensorflow/core/profiler/convert/xplane_to_step_events.cc index 72696afb80e..a47c9223835 100644 --- a/tensorflow/core/profiler/convert/xplane_to_step_events.cc +++ b/tensorflow/core/profiler/convert/xplane_to_step_events.cc @@ -16,7 +16,6 @@ limitations under the License. #include "tensorflow/core/profiler/convert/xplane_to_step_events.h" #include "tensorflow/core/lib/strings/str_util.h" -#include "tensorflow/core/profiler/utils/metadata_matcher.h" #include "tensorflow/core/profiler/utils/tf_xplane_visitor.h" #include "tensorflow/core/profiler/utils/trace_utils.h" #include "tensorflow/core/profiler/utils/xplane_schema.h" diff --git a/tensorflow/core/profiler/utils/BUILD b/tensorflow/core/profiler/utils/BUILD index 8886a444a4c..f79c5a5353f 100644 --- a/tensorflow/core/profiler/utils/BUILD +++ b/tensorflow/core/profiler/utils/BUILD @@ -188,32 +188,3 @@ cc_library( ":xplane_visitor", ], ) - -cc_library( - name = "metadata_matcher", - srcs = ["metadata_matcher.cc"], - hdrs = ["metadata_matcher.h"], - deps = [ - "//tensorflow/core:lib", - "//tensorflow/core:lib_internal", - "//tensorflow/core/profiler/protobuf:xplane_proto_cc", - "@com_google_absl//absl/container:flat_hash_map", - "@com_google_absl//absl/strings", - "@com_google_absl//absl/types:optional", - "@com_google_absl//absl/types:span", - ], -) - -tf_cc_test( - name = "metadata_matcher_test", - size = "small", - srcs = ["metadata_matcher_test.cc"], - deps = [ - ":metadata_matcher", - ":xplane_schema", - "//tensorflow/core:test", - "//tensorflow/core:test_main", - "@com_google_absl//absl/strings", - "@com_google_absl//absl/types:span", - ], -) diff --git a/tensorflow/core/profiler/utils/metadata_matcher.cc b/tensorflow/core/profiler/utils/metadata_matcher.cc deleted file mode 100644 index 7abdd77941a..00000000000 --- a/tensorflow/core/profiler/utils/metadata_matcher.cc +++ /dev/null @@ -1,145 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/core/profiler/utils/metadata_matcher.h" - -#include "absl/strings/string_view.h" - -namespace tensorflow { -namespace profiler { -namespace { - -using ::tensorflow::profiler::XEvent; -using ::tensorflow::profiler::XPlane; -using ::tensorflow::profiler::XStat; - -absl::flat_hash_map CreateEventMetadataMap( - const XPlane& xplane, - const std::vector, - /*first_event_type*/ int>>& - event_type_metadata_maps) { - absl::flat_hash_map id_to_event_type_map; - for (const auto& id_and_event_metadata : xplane.event_metadata()) { - int64 id = id_and_event_metadata.first; - absl::string_view event_name = id_and_event_metadata.second.name(); - for (const auto& event_type_metadata_map_and_first_event_type : - event_type_metadata_maps) { - auto event_type_metadata_map = - event_type_metadata_map_and_first_event_type.first; - int first_event_type = - event_type_metadata_map_and_first_event_type.second; - for (int i = 0; i < event_type_metadata_map.size(); ++i) { - if (event_type_metadata_map[i] == event_name) { - id_to_event_type_map[id] = first_event_type + i; - break; - } - } - } - } - return id_to_event_type_map; -} - -absl::flat_hash_map CreateStatMetadataMap( - const XPlane& xplane, - const absl::Span stat_type_str_map) { - absl::flat_hash_map id_to_stat_type_map; - for (const auto& id_and_stat_metadata : xplane.stat_metadata()) { - int64 id = id_and_stat_metadata.first; - absl::string_view stat_name = id_and_stat_metadata.second.name(); - for (int stat_type = 0; stat_type < stat_type_str_map.size(); ++stat_type) { - if (stat_type_str_map[stat_type] == stat_name) { - id_to_stat_type_map[id] = stat_type; - break; - } - } - } - return id_to_stat_type_map; -} - -} // namespace - -MetadataMatcher::MetadataMatcher( - const XPlane& xplane, - const std::vector, - /*first_event_type*/ int>>& - event_type_metadata_maps, - const absl::Span stat_type_str_map) - : id_to_event_type_map_( - CreateEventMetadataMap(xplane, event_type_metadata_maps)), - id_to_stat_type_map_(CreateStatMetadataMap(xplane, stat_type_str_map)), - event_type_to_id_map_(gtl::ReverseMap( - id_to_event_type_map_)), - stat_type_to_id_map_(gtl::ReverseMap( - id_to_stat_type_map_)) {} - -const XStat* MetadataMatcher::GetStat(const XEvent& event, - int stat_type) const { - for (const auto& stat : event.stats()) { - if (GetStatType(stat) == stat_type) { - return &stat; - } - } - return nullptr; -} - -absl::optional> -MetadataMatcher::GetStats(const XEvent& event, int first_stat_type, - int second_stat_type) const { - const XStat* first_stat = nullptr; - const XStat* second_stat = nullptr; - for (const auto& stat : event.stats()) { - if (GetStatType(stat) == first_stat_type) { - first_stat = &stat; - } else if (GetStatType(stat) == second_stat_type) { - second_stat = &stat; - } - } - if (first_stat && second_stat) { - return std::make_tuple(first_stat, second_stat); - } - return absl::nullopt; -} - -absl::optional> -MetadataMatcher::GetStats(const XEvent& event, int first_stat_type, - int second_stat_type, int third_stat_type) const { - const XStat* first_stat = nullptr; - const XStat* second_stat = nullptr; - const XStat* third_stat = nullptr; - for (const auto& stat : event.stats()) { - if (GetStatType(stat) == first_stat_type) { - first_stat = &stat; - } else if (GetStatType(stat) == second_stat_type) { - second_stat = &stat; - } else if (GetStatType(stat) == third_stat_type) { - third_stat = &stat; - } - } - if (first_stat && second_stat && third_stat) { - return std::make_tuple(first_stat, second_stat, third_stat); - } - return absl::nullopt; -} - -absl::optional MetadataMatcher::GetIntStatValue(const XEvent& event, - int stat_type) const { - if (const XStat* stat = GetStat(event, stat_type)) { - return stat->int64_value(); - } - return absl::nullopt; -} - -} // namespace profiler -} // namespace tensorflow diff --git a/tensorflow/core/profiler/utils/metadata_matcher.h b/tensorflow/core/profiler/utils/metadata_matcher.h deleted file mode 100644 index beaba5ecd70..00000000000 --- a/tensorflow/core/profiler/utils/metadata_matcher.h +++ /dev/null @@ -1,108 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#ifndef TENSORFLOW_CORE_PROFILER_UTILS_METADATA_MATCHER_H_ -#define TENSORFLOW_CORE_PROFILER_UTILS_METADATA_MATCHER_H_ - -#include "absl/container/flat_hash_map.h" -#include "absl/strings/string_view.h" -#include "absl/types/optional.h" -#include "absl/types/span.h" -#include "tensorflow/core/lib/gtl/map_util.h" -#include "tensorflow/core/platform/types.h" -#include "tensorflow/core/profiler/protobuf/xplane.pb.h" - -namespace tensorflow { -namespace profiler { - -// Builds mapping between metadata ids and interesting event and stat types. -// Event and stat types are represented in integer ids. Multiple spans of event -// types can be passed with offset values (i.e., first_event_type) to be -// used to calculate integer ids for event types. Spans and offset values are -// expected to result in a unique integer id for each event type. -class MetadataMatcher { - public: - explicit MetadataMatcher( - const XPlane& xplane, - const std::vector, - /*first_event_type*/ int>>& - event_type_metadata_maps, - const absl::Span stat_type_str_map); - - // Returns EventType if input is one of interesting event types. - // Otherwise, it returns kUnknownEventType. - int GetEventType(const XEvent& xevent) const { - return gtl::FindWithDefault(id_to_event_type_map_, xevent.metadata_id(), - /*kUnknownEventType*/ 0); - } - - // Overload of GetEventType function. - // Returns EventType if input is one of interesting event types. - // Otherwise, it returns kUnknownEventType. - int GetEventType(int64 metadata_id) const { - return gtl::FindWithDefault(id_to_event_type_map_, metadata_id, - /*kUnknownEventType*/ 0); - } - - // Returns metadata id if xplane has the input event type. - absl::optional GetEventMetadataId(int event_type) const { - if (const int64* id = gtl::FindOrNull(event_type_to_id_map_, event_type)) { - return *id; - } - return absl::nullopt; - } - - // Returns StatType if input is one of interesting stat types. - // Otherwise, it returns kUnknownStatType. - int GetStatType(const XStat& xstat) const { - return gtl::FindWithDefault(id_to_stat_type_map_, xstat.metadata_id(), - /*kUnknownStatType*/ 0); - } - - // Returns metadata id if xplane has the input stat type. - absl::optional GetStatMetadataId(int stat_type) const { - if (const int64* id = gtl::FindOrNull(stat_type_to_id_map_, stat_type)) { - return *id; - } - return absl::nullopt; - } - - const XStat* GetStat(const XEvent& event, int stat_type) const; - - absl::optional> GetStats( - const XEvent& event, int first_stat_type, int second_stat_type) const; - - absl::optional> GetStats( - const XEvent& event, int first_stat_type, int second_stat_type, - int third_stat_type) const; - - absl::optional GetIntStatValue(const XEvent& event, - int stat_type) const; - - private: - // Maps from metada ids to interesting event and stat types. - // Uninteresting event and stat types are not cached in these maps and - // considered to be kUnknown*. - const absl::flat_hash_map id_to_event_type_map_; - const absl::flat_hash_map id_to_stat_type_map_; - // Reverse of the above. - const absl::flat_hash_map event_type_to_id_map_; - const absl::flat_hash_map stat_type_to_id_map_; -}; - -} // namespace profiler -} // namespace tensorflow - -#endif // TENSORFLOW_CORE_PROFILER_UTILS_METADATA_MATCHER_H_ diff --git a/tensorflow/core/profiler/utils/metadata_matcher_test.cc b/tensorflow/core/profiler/utils/metadata_matcher_test.cc deleted file mode 100644 index d430b44fc64..00000000000 --- a/tensorflow/core/profiler/utils/metadata_matcher_test.cc +++ /dev/null @@ -1,69 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "tensorflow/core/profiler/utils/metadata_matcher.h" - -#include "absl/strings/string_view.h" -#include "absl/types/span.h" -#include "tensorflow/core/platform/test.h" -#include "tensorflow/core/profiler/utils/xplane_schema.h" - -namespace tensorflow { -namespace profiler { -namespace { - -using ::tensorflow::profiler::XEventMetadata; -using ::tensorflow::profiler::XPlane; -using ::tensorflow::profiler::XStatMetadata; - -TEST(MetadataMatcherTest, GetHostEventTypeTest) { - for (int event_type = HostEventType::kFirstHostEventType; - event_type <= HostEventType::kLastHostEventType; ++event_type) { - XPlane xplane; - XEventMetadata& metadata = (*xplane.mutable_event_metadata())[0]; - metadata.set_id(0); - metadata.set_name(std::string( - GetHostEventTypeStr(static_cast(event_type)))); - MetadataMatcher metadata_matcher( - xplane, - {{GetHostEventTypeStrMap(), HostEventType::kFirstHostEventType}}, - GetStatTypeStrMap()); - XEvent event; - event.set_metadata_id(0); - EXPECT_EQ(metadata_matcher.GetEventType(event), event_type); - } -} - -TEST(MetadataMatcherTest, GetStatTypeTest) { - for (int stat_type = StatType::kFirstStatType; - stat_type <= StatType::kLastStatType; ++stat_type) { - XPlane xplane; - XStatMetadata& metadata = (*xplane.mutable_stat_metadata())[0]; - metadata.set_id(0); - metadata.set_name( - std::string(GetStatTypeStr(static_cast(stat_type)))); - MetadataMatcher metadata_matcher( - xplane, - {{GetHostEventTypeStrMap(), HostEventType::kFirstHostEventType}}, - GetStatTypeStrMap()); - XStat stat; - stat.set_metadata_id(0); - EXPECT_EQ(metadata_matcher.GetStatType(stat), stat_type); - } -} - -} // namespace -} // namespace profiler -} // namespace tensorflow diff --git a/tensorflow/core/profiler/utils/xplane_visitor.h b/tensorflow/core/profiler/utils/xplane_visitor.h index ca218d836fe..8b7064b0ade 100644 --- a/tensorflow/core/profiler/utils/xplane_visitor.h +++ b/tensorflow/core/profiler/utils/xplane_visitor.h @@ -34,6 +34,7 @@ class XPlaneVisitor; class XStatVisitor { public: + // REQUIRED: plane and stat cannot be nullptr. XStatVisitor(const XPlaneVisitor* plane, const XStat* stat); int64 Id() const { return stat_->metadata_id(); } @@ -67,6 +68,7 @@ class XStatVisitor { template class XStatsOwner { public: + // REQUIRED: metadata and stats_owner cannot be nullptr. XStatsOwner(const XPlaneVisitor* metadata, const T* stats_owner) : stats_owner_(stats_owner), metadata_(metadata) {} @@ -88,6 +90,7 @@ class XStatsOwner { class XEventVisitor : public XStatsOwner { public: + // REQUIRED: plane, line and event cannot be nullptr. XEventVisitor(const XPlaneVisitor* plane, const XLine* line, const XEvent* event); int64 Id() const { return event_->metadata_id(); } @@ -141,6 +144,7 @@ class XEventVisitor : public XStatsOwner { class XLineVisitor { public: + // REQUIRED: plane and line cannot be nullptr. XLineVisitor(const XPlaneVisitor* plane, const XLine* line) : plane_(plane), line_(line) {} @@ -180,6 +184,7 @@ using TypeGetterList = std::vector; class XPlaneVisitor : public XStatsOwner { public: + // REQUIRED: plane cannot be nullptr. explicit XPlaneVisitor( const XPlane* plane, const TypeGetterList& event_type_getter_list = TypeGetterList(), @@ -201,9 +206,15 @@ class XPlaneVisitor : public XStatsOwner { // TODO(jiesun): use single map look up for both StatMetadata and StatType. const XStatMetadata* GetStatMetadata(int64 stat_metadata_id) const; absl::optional GetStatType(int64 stat_metadata_id) const; + absl::optional GetStatType(const XStat& stat) const { + return GetStatType(stat.metadata_id()); + } absl::optional GetStatMetadataId(int64 stat_type) const; const XEventMetadata* GetEventMetadata(int64 event_metadata_id) const; absl::optional GetEventType(int64 event_metadata_id) const; + absl::optional GetEventType(const XEvent& event) const { + return GetEventType(event.metadata_id()); + } private: void BuildEventTypeMap(const XPlane* plane,