|
| 1 | +/* Copyright 2025 The TensorFlow Authors. All Rights Reserved. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. |
| 14 | +==============================================================================*/ |
| 15 | + |
| 16 | +#include "xprof/convert/smart_suggestion/sparse_core_bound_rule.h" |
| 17 | + |
| 18 | +#include <memory> |
| 19 | +#include <optional> |
| 20 | +#include <utility> |
| 21 | +#include <vector> |
| 22 | +#include <string> |
| 23 | + |
| 24 | +#include "testing/base/public/gmock.h" |
| 25 | +#include "<gtest/gtest.h>" |
| 26 | +#include "absl/status/statusor.h" |
| 27 | +#include "xprof/convert/smart_suggestion/signal_provider.h" |
| 28 | +#include "xprof/convert/smart_suggestion/tool_data_provider.h" |
| 29 | +#include "plugin/xprof/protobuf/input_pipeline.pb.h" |
| 30 | +#include "plugin/xprof/protobuf/overview_page.pb.h" |
| 31 | +#include "plugin/xprof/protobuf/smart_suggestion.pb.h" |
| 32 | +#include "plugin/xprof/protobuf/tpu_input_pipeline.pb.h" |
| 33 | + |
| 34 | +namespace tensorflow { |
| 35 | +namespace profiler { |
| 36 | +namespace { |
| 37 | + |
| 38 | +using ::testing::Eq; |
| 39 | +using ::testing::Return; |
| 40 | +using ::testing::status::IsOkAndHolds; |
| 41 | + |
| 42 | +// Mock ToolDataProvider |
| 43 | +class MockToolDataProvider : public ToolDataProvider { |
| 44 | + public: |
| 45 | + MOCK_METHOD(absl::StatusOr<const OverviewPage*>, GetOverviewPage, (), |
| 46 | + (override)); |
| 47 | + MOCK_METHOD(absl::StatusOr<const InputPipelineAnalysisResult*>, |
| 48 | + GetInputPipelineAnalysisResult, (), (override)); |
| 49 | + MOCK_METHOD(absl::StatusOr<std::vector<float>>, |
| 50 | + GetEventTimeFractionEachStep, (const std::string&), |
| 51 | + (override)); |
| 52 | +}; |
| 53 | + |
| 54 | +TEST(SparseCoreBoundRuleTest, MeetsConditions) { |
| 55 | + auto mock_tool_data_provider = std::make_unique<MockToolDataProvider>(); |
| 56 | + InputPipelineAnalysisResult input_pipeline_analysis; |
| 57 | + input_pipeline_analysis.mutable_step_time_summary()->set_average(100.0); |
| 58 | + TpuStepTimeBreakdown step_time_breakdown; |
| 59 | + step_time_breakdown.mutable_sparse_core_step_summary() |
| 60 | + ->mutable_sc_step_time_ms_summary() |
| 61 | + ->set_average(11.0); |
| 62 | + input_pipeline_analysis.mutable_step_time_breakdown()->PackFrom( |
| 63 | + step_time_breakdown); |
| 64 | + |
| 65 | + EXPECT_CALL(*mock_tool_data_provider, GetInputPipelineAnalysisResult()) |
| 66 | + .WillRepeatedly(Return(&input_pipeline_analysis)); |
| 67 | + |
| 68 | + OverviewPage overview_page; |
| 69 | + overview_page.mutable_analysis()->set_mxu_utilization_percent(49.0); |
| 70 | + overview_page.mutable_analysis() |
| 71 | + ->set_memory_bw_utilization_relative_to_hw_limit_percent(49.0); |
| 72 | + EXPECT_CALL(*mock_tool_data_provider, GetOverviewPage()) |
| 73 | + .WillRepeatedly(Return(&overview_page)); |
| 74 | + |
| 75 | + SignalProvider signal_provider(std::move(mock_tool_data_provider)); |
| 76 | + SparseCoreBoundRule rule; |
| 77 | + |
| 78 | + absl::StatusOr<std::optional<SmartSuggestion>> suggestion = |
| 79 | + rule.Apply(signal_provider); |
| 80 | + EXPECT_THAT(suggestion, IsOkAndHolds(testing::Not(Eq(std::nullopt)))); |
| 81 | + EXPECT_EQ((*suggestion)->rule_name(), "SparseCoreBoundRule"); |
| 82 | + EXPECT_THAT((*suggestion)->suggestion_text(), |
| 83 | + testing::HasSubstr( |
| 84 | + "11.0% of the total step time </b> is spent on SparseCore")); |
| 85 | +} |
| 86 | + |
| 87 | +TEST(SparseCoreBoundRuleTest, IdleTimeTooLow) { |
| 88 | + auto mock_tool_data_provider = std::make_unique<MockToolDataProvider>(); |
| 89 | + InputPipelineAnalysisResult input_pipeline_analysis; |
| 90 | + input_pipeline_analysis.mutable_step_time_summary()->set_average(100.0); |
| 91 | + TpuStepTimeBreakdown step_time_breakdown; |
| 92 | + step_time_breakdown.mutable_sparse_core_step_summary() |
| 93 | + ->mutable_sc_step_time_ms_summary() |
| 94 | + ->set_average(9.0); |
| 95 | + input_pipeline_analysis.mutable_step_time_breakdown()->PackFrom( |
| 96 | + step_time_breakdown); |
| 97 | + |
| 98 | + EXPECT_CALL(*mock_tool_data_provider, GetInputPipelineAnalysisResult()) |
| 99 | + .WillRepeatedly(Return(&input_pipeline_analysis)); |
| 100 | + |
| 101 | + OverviewPage overview_page; |
| 102 | + overview_page.mutable_analysis()->set_mxu_utilization_percent(49.0); |
| 103 | + overview_page.mutable_analysis() |
| 104 | + ->set_memory_bw_utilization_relative_to_hw_limit_percent(49.0); |
| 105 | + EXPECT_CALL(*mock_tool_data_provider, GetOverviewPage()) |
| 106 | + .WillRepeatedly(Return(&overview_page)); |
| 107 | + |
| 108 | + SignalProvider signal_provider(std::move(mock_tool_data_provider)); |
| 109 | + SparseCoreBoundRule rule; |
| 110 | + |
| 111 | + absl::StatusOr<std::optional<SmartSuggestion>> suggestion = |
| 112 | + rule.Apply(signal_provider); |
| 113 | + EXPECT_THAT(suggestion, IsOkAndHolds(Eq(std::nullopt))); |
| 114 | +} |
| 115 | + |
| 116 | +TEST(SparseCoreBoundRuleTest, NoTpuStepTimeBreakdownField) { |
| 117 | + auto mock_tool_data_provider = std::make_unique<MockToolDataProvider>(); |
| 118 | + InputPipelineAnalysisResult input_pipeline_analysis; |
| 119 | + input_pipeline_analysis.mutable_step_time_summary()->set_average(100.0); |
| 120 | + GenericStepTimeBreakdown step_time_breakdown; |
| 121 | + step_time_breakdown.mutable_input_ms_summary()->set_average(9.0); |
| 122 | + input_pipeline_analysis.mutable_step_time_breakdown()->PackFrom( |
| 123 | + step_time_breakdown); |
| 124 | + |
| 125 | + EXPECT_CALL(*mock_tool_data_provider, GetInputPipelineAnalysisResult()) |
| 126 | + .WillRepeatedly(Return(&input_pipeline_analysis)); |
| 127 | + |
| 128 | + SignalProvider signal_provider(std::move(mock_tool_data_provider)); |
| 129 | + SparseCoreBoundRule rule; |
| 130 | + |
| 131 | + absl::StatusOr<std::optional<SmartSuggestion>> suggestion = |
| 132 | + rule.Apply(signal_provider); |
| 133 | + EXPECT_THAT(suggestion, IsOkAndHolds(Eq(std::nullopt))); |
| 134 | +} |
| 135 | + |
| 136 | +TEST(SparseCoreBoundRuleTest, HbmAndMxuUtilizationTooHigh) { |
| 137 | + auto mock_tool_data_provider = std::make_unique<MockToolDataProvider>(); |
| 138 | + InputPipelineAnalysisResult input_pipeline_analysis; |
| 139 | + input_pipeline_analysis.mutable_step_time_summary()->set_average(100.0); |
| 140 | + TpuStepTimeBreakdown step_time_breakdown; |
| 141 | + step_time_breakdown.mutable_sparse_core_step_summary() |
| 142 | + ->mutable_sc_step_time_ms_summary() |
| 143 | + ->set_average(11.0); |
| 144 | + input_pipeline_analysis.mutable_step_time_breakdown()->PackFrom( |
| 145 | + step_time_breakdown); |
| 146 | + |
| 147 | + EXPECT_CALL(*mock_tool_data_provider, GetInputPipelineAnalysisResult()) |
| 148 | + .WillRepeatedly(Return(&input_pipeline_analysis)); |
| 149 | + |
| 150 | + OverviewPage overview_page; |
| 151 | + overview_page.mutable_analysis()->set_mxu_utilization_percent(51.0); |
| 152 | + overview_page.mutable_analysis() |
| 153 | + ->set_memory_bw_utilization_relative_to_hw_limit_percent(51.0); |
| 154 | + EXPECT_CALL(*mock_tool_data_provider, GetOverviewPage()) |
| 155 | + .WillRepeatedly(Return(&overview_page)); |
| 156 | + |
| 157 | + SignalProvider signal_provider(std::move(mock_tool_data_provider)); |
| 158 | + SparseCoreBoundRule rule; |
| 159 | + |
| 160 | + absl::StatusOr<std::optional<SmartSuggestion>> suggestion = |
| 161 | + rule.Apply(signal_provider); |
| 162 | + EXPECT_THAT(suggestion, IsOkAndHolds(Eq(std::nullopt))); |
| 163 | +} |
| 164 | + |
| 165 | +} // namespace |
| 166 | +} // namespace profiler |
| 167 | +} // namespace tensorflow |
0 commit comments