| 93 | } |
| 94 | |
| 95 | TEST_F(ProfilerTest, Basics) { |
| 96 | SessionOptions options; |
| 97 | options.config.set_allow_soft_placement(true); |
| 98 | std::unique_ptr<Session> session(NewSession(options)); |
| 99 | GraphDef def = CreateGraphDef(); |
| 100 | if (options.target.empty()) { |
| 101 | graph::SetDefaultDevice("/gpu:0", &def); |
| 102 | } |
| 103 | |
| 104 | TF_CHECK_OK(session->Create(def)); |
| 105 | |
| 106 | Tensor x(DT_FLOAT, TensorShape({2, 1})); |
| 107 | auto x_flat = x.flat<float>(); |
| 108 | x_flat.setRandom(); |
| 109 | Eigen::Tensor<float, 0, Eigen::RowMajor> inv_norm = |
| 110 | x_flat.square().sum().sqrt().inverse(); |
| 111 | x_flat = x_flat * inv_norm(); |
| 112 | |
| 113 | std::vector<Tensor> outputs; |
| 114 | RunOptions run_options; |
| 115 | run_options.set_trace_level(RunOptions::FULL_TRACE); |
| 116 | RunMetadata run_metadata; |
| 117 | outputs.clear(); |
| 118 | |
| 119 | Profiler profiler(def); |
| 120 | for (int i = 0; i < 2; ++i) { |
| 121 | TF_CHECK_OK(session->Run(run_options, {{"x", x}}, {"y:0", "y_normalized:0"}, |
| 122 | {}, &outputs, &run_metadata)); |
| 123 | profiler.AddStep(i, run_metadata); |
| 124 | CHECK_EQ(size_t{2}, outputs.size()); |
| 125 | } |
| 126 | |
| 127 | std::vector<DeviceAttributes> resp; |
| 128 | TF_CHECK_OK(session->ListDevices(&resp)); |
| 129 | bool has_gpu = false; |
| 130 | for (const auto& dev : resp) { |
| 131 | if (dev.device_type() == "GPU") { |
| 132 | has_gpu = true; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | GraphNodeProto ret = profiler.ProfileNameScope(Default()); |
| 137 | const GraphNodeProto* matmul = ExtractNode(ret, "y"); |
| 138 | EXPECT_TRUE(matmul); |
| 139 | EXPECT_GT(matmul->exec_micros(), 0); |
| 140 | if (has_gpu) { |
| 141 | EXPECT_GT(matmul->accelerator_exec_micros(), 0); |
| 142 | } else { |
| 143 | EXPECT_EQ(matmul->accelerator_exec_micros(), 0); |
| 144 | } |
| 145 | const GraphNodeProto* square = ExtractNode(ret, "Square"); |
| 146 | EXPECT_TRUE(square); |
| 147 | EXPECT_GT(square->exec_micros(), 0); |
| 148 | if (has_gpu) { |
| 149 | EXPECT_GT(square->accelerator_exec_micros(), 0); |
| 150 | } else { |
| 151 | EXPECT_EQ(square->accelerator_exec_micros(), 0); |
| 152 | } |
nothing calls this directly
no test coverage detected