aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/tools/tfprof/internal/advisor/tfprof_advisor_test.cc
blob: b41d0770dc7045d84973b4f94c47bc51d3266847 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/* Copyright 2016 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/tools/tfprof/internal/advisor/tfprof_advisor.h"

#include "tensorflow/core/lib/io/path.h"
#include "tensorflow/core/platform/env.h"
#include "tensorflow/core/platform/test.h"

namespace tensorflow {
namespace tfprof {

class TFProfAdvisorTest : public ::testing::Test {
 protected:
  TFProfAdvisorTest() {
    stats_.reset(new TFStats(std::unique_ptr<GraphDef>(new GraphDef()), nullptr,
                             nullptr, nullptr));

    stats_->AddNodeForTest(
        "n1", CreateNode("n1", "Conv2D", {{"data_format", "NHWC"}}, 10, 2));
    stats_->AddNodeForTest("n2", CreateNode("n2", "Conv2D", {}, 20, 2));
    advisor_.reset(new Advisor(stats_.get()));
  }

  std::unique_ptr<TFGraphNode> CreateNode(const string& name,
                                          const string& type,
                                          std::map<string, string> attrs,
                                          int64 start_miros,
                                          int64 end_rel_micros) {
    node_defs_.push_back(std::unique_ptr<NodeDef>(new NodeDef()));
    NodeDef* def = node_defs_.back().get();

    def->set_name(name);
    def->set_op(type);
    for (const auto& attr : attrs) {
      (*def->mutable_attr())[attr.first].set_s(attr.second);
    }
    std::unique_ptr<TFGraphNode> node(new TFGraphNode(def));

    NodeExecStats node_stat;
    node_stat.set_all_start_micros(start_miros);
    node_stat.set_op_end_rel_micros(end_rel_micros);
    node->AddStepStat(0, "/job:localhost/replica:0/task:0/gpu:0", node_stat);
    node->AddStepStat(0, "/job:localhost/replica:0/task:0/gpu:0:stream:all",
                      node_stat);
    node->AddStepStat(0, "/job:localhost/replica:0/task:0/gpu:0:stream:0",
                      node_stat);
    return node;
  }

  std::unique_ptr<TFStats> stats_;
  std::unique_ptr<Advisor> advisor_;
  std::vector<std::unique_ptr<NodeDef>> node_defs_;
};

TEST_F(TFProfAdvisorTest, Basics) {
  std::map<string, std::vector<string>> reports = advisor_->Advise();
  EXPECT_TRUE(reports.find("AcceleratorUtilizationChecker") != reports.end());
  EXPECT_TRUE(reports.find("OperationChecker") != reports.end());
}

TEST_F(TFProfAdvisorTest, OperationChecker) {
  std::map<string, std::vector<string>> reports = advisor_->Advise();
  EXPECT_EQ(reports["OperationChecker"].size(), 1);
  EXPECT_TRUE(StringPiece(reports["OperationChecker"][0]).contains("NCHW"));
}

TEST_F(TFProfAdvisorTest, UtilizationChecker) {
  std::map<string, std::vector<string>> reports = advisor_->Advise();
  EXPECT_EQ(reports["AcceleratorUtilizationChecker"].size(), 1);
  EXPECT_TRUE(StringPiece(reports["AcceleratorUtilizationChecker"][0])
                  .contains("low utilization"));
}

}  // namespace tfprof
}  // namespace tensorflow