1 /* Copyright 2016 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 "tensorflow/core/profiler/internal/advisor/tfprof_advisor.h" 17 18 #include "tensorflow/core/lib/io/path.h" 19 #include "tensorflow/core/platform/env.h" 20 #include "tensorflow/core/platform/test.h" 21 22 namespace tensorflow { 23 namespace tfprof { 24 25 class TFProfAdvisorTest : public ::testing::Test { 26 protected: 27 TFProfAdvisorTest() { 28 stats_.reset(new TFStats(std::unique_ptr<GraphDef>(new GraphDef()), nullptr, 29 nullptr, nullptr)); 30 31 stats_->AddNodeForTest( 32 0, CreateNode("n1", "Conv2D", {{"data_format", "NHWC"}}, 0, 10, 2)); 33 stats_->AddNodeForTest(0, CreateNode("n2", "Conv2D", {}, 0, 20, 2)); 34 stats_->BuildAllViews(); 35 advisor_.reset(new Advisor(stats_.get())); 36 } 37 38 std::unique_ptr<TFGraphNode> CreateNode(const string& name, 39 const string& type, 40 std::map<string, string> attrs, 41 int64 step, int64 start_miros, 42 int64 end_rel_micros) { 43 node_defs_.push_back(std::unique_ptr<NodeDef>(new NodeDef())); 44 NodeDef* def = node_defs_.back().get(); 45 46 def->set_name(name); 47 def->set_op(type); 48 for (const auto& attr : attrs) { 49 (*def->mutable_attr())[attr.first].set_s(attr.second); 50 } 51 std::unique_ptr<TFGraphNode> node(new TFGraphNode(def, -1, nullptr)); 52 53 NodeExecStats node_stat; 54 node_stat.set_all_start_micros(start_miros); 55 node_stat.set_op_end_rel_micros(end_rel_micros); 56 node->AddStepStat(step, "/job:localhost/replica:0/task:0/device:GPU:0", 57 node_stat); 58 node->AddStepStat(step, 59 "/job:localhost/replica:0/task:0/device:GPU:0:stream:all", 60 node_stat); 61 node->AddStepStat(step, 62 "/job:localhost/replica:0/task:0/device:GPU:0:stream:0", 63 node_stat); 64 return node; 65 } 66 67 std::unique_ptr<TFStats> stats_; 68 std::unique_ptr<Advisor> advisor_; 69 std::vector<std::unique_ptr<NodeDef>> node_defs_; 70 }; 71 72 TEST_F(TFProfAdvisorTest, Basics) { 73 AdvisorOptionsProto options = Advisor::DefaultOptions(); 74 AdviceProto advice = advisor_->Advise(options); 75 EXPECT_TRUE(advice.checkers().find(kCheckers[0]) != advice.checkers().end()); 76 EXPECT_TRUE(advice.checkers().find(kCheckers[1]) != advice.checkers().end()); 77 EXPECT_TRUE(advice.checkers().find(kCheckers[2]) != advice.checkers().end()); 78 } 79 80 TEST_F(TFProfAdvisorTest, OperationChecker) { 81 AdvisorOptionsProto options; 82 (*options.mutable_checkers())[kCheckers[1]]; 83 AdviceProto advice = advisor_->Advise(options); 84 EXPECT_EQ(advice.checkers().at(kCheckers[1]).reports_size(), 1); 85 EXPECT_TRUE(StringPiece(advice.checkers().at(kCheckers[1]).reports(0)) 86 .contains("NCHW")); 87 } 88 89 TEST_F(TFProfAdvisorTest, UtilizationChecker) { 90 AdvisorOptionsProto options; 91 (*options.mutable_checkers())[kCheckers[0]]; 92 AdviceProto advice = advisor_->Advise(options); 93 EXPECT_EQ(advice.checkers().at(kCheckers[0]).reports_size(), 1); 94 EXPECT_TRUE(StringPiece(advice.checkers().at(kCheckers[0]).reports(0)) 95 .contains("low utilization")); 96 } 97 98 TEST_F(TFProfAdvisorTest, ExpensiveOperationChecker) { 99 AdvisorOptionsProto options; 100 (*options.mutable_checkers())[kCheckers[2]]; 101 AdviceProto advice = advisor_->Advise(options); 102 EXPECT_TRUE(StringPiece(advice.checkers().at(kCheckers[2]).reports(0)) 103 .contains("top 1 operation type: Conv2D")); 104 } 105 106 } // namespace tfprof 107 } // namespace tensorflow 108