test_event_node.cc 18.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// Copyright (c) 2022 PaddlePaddle 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 "gtest/gtest.h"
16
#include "paddle/fluid/framework/type_defs.h"
17 18 19
#include "paddle/fluid/platform/profiler/chrometracing_logger.h"
#include "paddle/fluid/platform/profiler/event_node.h"

20
using paddle::framework::AttributeMap;
21 22
using paddle::platform::ChromeTracingLogger;
using paddle::platform::CudaRuntimeTraceEventNode;
23
using paddle::platform::DeviceTraceEvent;
24 25
using paddle::platform::DeviceTraceEventNode;
using paddle::platform::HostTraceEvent;
26
using paddle::platform::HostTraceEventNode;
27 28 29
using paddle::platform::KernelEventInfo;
using paddle::platform::MemcpyEventInfo;
using paddle::platform::MemsetEventInfo;
C
chenjian 已提交
30 31
using paddle::platform::MemTraceEvent;
using paddle::platform::MemTraceEventNode;
32
using paddle::platform::NodeTrees;
C
chenjian 已提交
33 34
using paddle::platform::OperatorSupplementEvent;
using paddle::platform::OperatorSupplementEventNode;
35 36
using paddle::platform::RuntimeTraceEvent;
using paddle::platform::TracerEventType;
C
chenjian 已提交
37
using paddle::platform::TracerMemEventType;
38

39 40 41 42
TEST(NodeTreesTest, LogMe_case0) {
  std::list<HostTraceEvent> host_events;
  std::list<RuntimeTraceEvent> runtime_events;
  std::list<DeviceTraceEvent> device_events;
C
chenjian 已提交
43 44
  std::list<MemTraceEvent> mem_events;
  std::list<OperatorSupplementEvent> op_supplement_events;
45
  host_events.push_back(HostTraceEvent(std::string("dataloader#1"),
46 47 48 49 50
                                       TracerEventType::Dataloader,
                                       1000,
                                       10000,
                                       10,
                                       10));
51 52 53 54 55 56
  host_events.push_back(HostTraceEvent(
      std::string("op1"), TracerEventType::Operator, 11000, 20000, 10, 10));
  host_events.push_back(HostTraceEvent(
      std::string("op2"), TracerEventType::Operator, 21000, 30000, 10, 10));
  host_events.push_back(HostTraceEvent(
      std::string("op3"), TracerEventType::Operator, 31000, 40000, 10, 11));
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
  mem_events.push_back(MemTraceEvent(11500,
                                     0x1000,
                                     TracerMemEventType::Allocate,
                                     10,
                                     10,
                                     50,
                                     "GPU:0",
                                     50,
                                     50,
                                     100,
                                     100));
  mem_events.push_back(MemTraceEvent(11900,
                                     0x1000,
                                     TracerMemEventType::Free,
                                     10,
                                     10,
                                     -50,
                                     "GPU:0",
                                     0,
                                     50,
                                     100,
                                     100));
C
chenjian 已提交
79 80 81 82 83 84
  std::map<std::string, std::vector<std::vector<int64_t>>> input_shapes;
  std::map<std::string, std::vector<std::string>> dtypes;
  input_shapes[std::string("X")].push_back(std::vector<int64_t>{1, 2, 3});
  input_shapes[std::string("X")].push_back(std::vector<int64_t>{4, 5, 6, 7});
  dtypes[std::string("X")].push_back(std::string("int8"));
  dtypes[std::string("X")].push_back(std::string("float32"));
85
  AttributeMap attrs;
C
chenjian 已提交
86
  op_supplement_events.push_back(OperatorSupplementEvent(
87
      11600, "op1", input_shapes, dtypes, "op1()", attrs, 0, 10, 10));
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
  runtime_events.push_back(RuntimeTraceEvent(
      std::string("cudalaunch1"), 15000, 17000, 10, 10, 1, 0));
  runtime_events.push_back(RuntimeTraceEvent(
      std::string("cudalaunch2"), 25000, 35000, 10, 10, 2, 0));
  runtime_events.push_back(RuntimeTraceEvent(
      std::string("cudalaunch3"), 33000, 37000, 10, 11, 3, 0));
  runtime_events.push_back(RuntimeTraceEvent(
      std::string("cudaMemcpy1"), 18000, 19000, 10, 10, 4, 0));
  runtime_events.push_back(RuntimeTraceEvent(
      std::string("cudaMemset1"), 38000, 39000, 10, 11, 5, 0));
  device_events.push_back(DeviceTraceEvent(std::string("kernel1"),
                                           TracerEventType::Kernel,
                                           40000,
                                           55000,
                                           0,
                                           10,
                                           10,
                                           1,
                                           KernelEventInfo()));
  device_events.push_back(DeviceTraceEvent(std::string("kernel2"),
                                           TracerEventType::Kernel,
                                           70000,
                                           95000,
                                           0,
                                           10,
                                           10,
                                           2,
                                           KernelEventInfo()));
  device_events.push_back(DeviceTraceEvent(std::string("kernel3"),
                                           TracerEventType::Kernel,
                                           60000,
                                           65000,
                                           0,
                                           10,
                                           11,
                                           3,
                                           KernelEventInfo()));
  device_events.push_back(DeviceTraceEvent(std::string("memcpy1"),
                                           TracerEventType::Memcpy,
                                           56000,
                                           59000,
                                           0,
                                           10,
                                           10,
                                           4,
                                           MemcpyEventInfo()));
  device_events.push_back(DeviceTraceEvent(std::string("memset1"),
                                           TracerEventType::Memset,
                                           66000,
                                           69000,
                                           0,
                                           10,
                                           11,
                                           5,
                                           MemsetEventInfo()));
143
  ChromeTracingLogger logger("test_nodetrees_logme_case0.json");
144
  logger.LogMetaInfo(std::string("1.0.2"), 0);
145 146 147 148
  NodeTrees tree(host_events,
                 runtime_events,
                 device_events,
                 mem_events,
C
chenjian 已提交
149
                 op_supplement_events);
150 151 152 153 154 155
  std::map<uint64_t, std::vector<HostTraceEventNode*>> nodes =
      tree.Traverse(true);
  EXPECT_EQ(nodes[10].size(), 4u);
  EXPECT_EQ(nodes[11].size(), 2u);
  std::vector<HostTraceEventNode*> thread1_nodes = nodes[10];
  std::vector<HostTraceEventNode*> thread2_nodes = nodes[11];
156 157 158
  for (auto& thread1_node : thread1_nodes) {
    if (thread1_node->Name() == "root node") {
      EXPECT_EQ(thread1_node->GetChildren().size(), 3u);
159
    }
160 161 162 163 164
    if (thread1_node->Name() == "op1") {
      EXPECT_EQ(thread1_node->GetChildren().size(), 0u);
      EXPECT_EQ(thread1_node->GetRuntimeTraceEventNodes().size(), 2u);
      EXPECT_EQ(thread1_node->GetMemTraceEventNodes().size(), 2u);
      EXPECT_NE(thread1_node->GetOperatorSupplementEventNode(), nullptr);
165 166
    }
  }
167 168 169 170
  for (auto& thread2_node : thread2_nodes) {
    if (thread2_node->Name() == "op3") {
      EXPECT_EQ(thread2_node->GetChildren().size(), 0u);
      EXPECT_EQ(thread2_node->GetRuntimeTraceEventNodes().size(), 2u);
171 172 173
    }
  }
  tree.LogMe(&logger);
174
  logger.LogExtraInfo(std::unordered_map<std::string, std::string>());
175 176 177 178 179 180
}

TEST(NodeTreesTest, LogMe_case1) {
  std::list<HostTraceEvent> host_events;
  std::list<RuntimeTraceEvent> runtime_events;
  std::list<DeviceTraceEvent> device_events;
C
chenjian 已提交
181 182
  std::list<MemTraceEvent> mem_events;
  std::list<OperatorSupplementEvent> op_supplement_events;
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
  runtime_events.push_back(RuntimeTraceEvent(
      std::string("cudalaunch1"), 15000, 17000, 10, 10, 1, 0));
  runtime_events.push_back(RuntimeTraceEvent(
      std::string("cudalaunch2"), 25000, 35000, 10, 10, 2, 0));
  runtime_events.push_back(RuntimeTraceEvent(
      std::string("cudalaunch3"), 33000, 37000, 10, 11, 3, 0));
  runtime_events.push_back(RuntimeTraceEvent(
      std::string("cudaMemcpy1"), 18000, 19000, 10, 10, 4, 0));
  runtime_events.push_back(RuntimeTraceEvent(
      std::string("cudaMemset1"), 38000, 39000, 10, 11, 5, 0));
  device_events.push_back(DeviceTraceEvent(std::string("kernel1"),
                                           TracerEventType::Kernel,
                                           40000,
                                           55000,
                                           0,
                                           10,
                                           10,
                                           1,
                                           KernelEventInfo()));
  device_events.push_back(DeviceTraceEvent(std::string("kernel2"),
                                           TracerEventType::Kernel,
                                           70000,
                                           95000,
                                           0,
                                           10,
                                           10,
                                           2,
                                           KernelEventInfo()));
  device_events.push_back(DeviceTraceEvent(std::string("kernel3"),
                                           TracerEventType::Kernel,
                                           60000,
                                           65000,
                                           0,
                                           10,
                                           11,
                                           3,
                                           KernelEventInfo()));
  device_events.push_back(DeviceTraceEvent(std::string("memcpy1"),
                                           TracerEventType::Memcpy,
                                           56000,
                                           59000,
                                           0,
                                           10,
                                           10,
                                           4,
                                           MemcpyEventInfo()));
  device_events.push_back(DeviceTraceEvent(std::string("memset1"),
                                           TracerEventType::Memset,
                                           66000,
                                           69000,
                                           0,
                                           10,
                                           11,
                                           5,
                                           MemsetEventInfo()));
238
  ChromeTracingLogger logger("test_nodetrees_logme_case1.json");
239
  logger.LogMetaInfo(std::string("1.0.2"), 0);
240 241 242 243
  NodeTrees tree(host_events,
                 runtime_events,
                 device_events,
                 mem_events,
C
chenjian 已提交
244
                 op_supplement_events);
245 246 247 248 249 250
  std::map<uint64_t, std::vector<HostTraceEventNode*>> nodes =
      tree.Traverse(true);
  EXPECT_EQ(nodes[10].size(), 1u);
  EXPECT_EQ(nodes[11].size(), 1u);
  std::vector<HostTraceEventNode*> thread1_nodes = nodes[10];
  std::vector<HostTraceEventNode*> thread2_nodes = nodes[11];
251 252 253
  for (auto& thread1_node : thread1_nodes) {
    if (thread1_node->Name() == "root node") {
      EXPECT_EQ(thread1_node->GetRuntimeTraceEventNodes().size(), 3u);
254 255
    }
  }
256 257 258 259
  for (auto& thread2_node : thread2_nodes) {
    if (thread2_node->Name() == "root node") {
      EXPECT_EQ(thread2_node->GetChildren().size(), 0u);
      EXPECT_EQ(thread2_node->GetRuntimeTraceEventNodes().size(), 2u);
260 261 262
    }
  }
  tree.LogMe(&logger);
263
  logger.LogExtraInfo(std::unordered_map<std::string, std::string>());
264 265 266 267 268 269
}

TEST(NodeTreesTest, HandleTrees_case0) {
  std::list<HostTraceEvent> host_events;
  std::list<RuntimeTraceEvent> runtime_events;
  std::list<DeviceTraceEvent> device_events;
C
chenjian 已提交
270 271
  std::list<MemTraceEvent> mem_events;
  std::list<OperatorSupplementEvent> op_supplement_events;
272 273 274 275 276 277
  host_events.push_back(HostTraceEvent(
      std::string("op1"), TracerEventType::Operator, 10000, 100000, 10, 10));
  host_events.push_back(HostTraceEvent(
      std::string("op2"), TracerEventType::Operator, 30000, 70000, 10, 10));
  host_events.push_back(HostTraceEvent(
      std::string("op3"), TracerEventType::Operator, 2000, 120000, 10, 11));
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
  mem_events.push_back(MemTraceEvent(11500,
                                     0x1000,
                                     TracerMemEventType::Allocate,
                                     10,
                                     10,
                                     50,
                                     "GPU:0",
                                     50,
                                     50,
                                     100,
                                     100));
  mem_events.push_back(MemTraceEvent(11900,
                                     0x1000,
                                     TracerMemEventType::Free,
                                     10,
                                     10,
                                     -50,
                                     "GPU:0",
                                     0,
                                     50,
                                     100,
                                     100));
300
  AttributeMap attrs;
C
chenjian 已提交
301
  op_supplement_events.push_back(OperatorSupplementEvent(
302 303 304 305 306
      11600,
      "op1",
      std::map<std::string, std::vector<std::vector<int64_t>>>(),
      std::map<std::string, std::vector<std::string>>(),
      "op1()",
307 308
      attrs,
      0,
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
      10,
      10));
  runtime_events.push_back(RuntimeTraceEvent(
      std::string("cudalaunch1"), 15000, 25000, 10, 10, 1, 0));
  runtime_events.push_back(RuntimeTraceEvent(
      std::string("cudalaunch2"), 35000, 45000, 10, 10, 2, 0));
  runtime_events.push_back(RuntimeTraceEvent(
      std::string("cudalaunch3"), 10000, 55000, 10, 11, 3, 0));
  device_events.push_back(DeviceTraceEvent(std::string("kernel1"),
                                           TracerEventType::Kernel,
                                           40000,
                                           55000,
                                           0,
                                           10,
                                           10,
                                           1,
                                           KernelEventInfo()));
  device_events.push_back(DeviceTraceEvent(std::string("kernel2"),
                                           TracerEventType::Kernel,
                                           70000,
                                           95000,
                                           0,
                                           10,
                                           10,
                                           2,
                                           KernelEventInfo()));
  device_events.push_back(DeviceTraceEvent(std::string("kernel3"),
                                           TracerEventType::Kernel,
                                           60000,
                                           75000,
                                           0,
                                           10,
                                           11,
                                           3,
                                           KernelEventInfo()));
344
  ChromeTracingLogger logger("test_nodetrees_handletrees_case0.json");
345
  logger.LogMetaInfo(std::string("1.0.2"), 0);
346 347 348 349
  NodeTrees tree(host_events,
                 runtime_events,
                 device_events,
                 mem_events,
C
chenjian 已提交
350
                 op_supplement_events);
351 352 353 354 355 356
  std::map<uint64_t, std::vector<HostTraceEventNode*>> nodes =
      tree.Traverse(true);
  EXPECT_EQ(nodes[10].size(), 3u);
  EXPECT_EQ(nodes[11].size(), 2u);
  std::vector<HostTraceEventNode*> thread1_nodes = nodes[10];
  std::vector<HostTraceEventNode*> thread2_nodes = nodes[11];
357 358 359
  for (auto& thread1_node : thread1_nodes) {
    if (thread1_node->Name() == "root node") {
      EXPECT_EQ(thread1_node->GetChildren().size(), 1u);
360
    }
361 362 363
    if (thread1_node->Name() == "op1") {
      EXPECT_EQ(thread1_node->GetChildren().size(), 1u);
      EXPECT_EQ(thread1_node->GetRuntimeTraceEventNodes().size(), 1u);
364 365
    }
  }
366 367 368 369
  for (auto& thread2_node : thread2_nodes) {
    if (thread2_node->Name() == "op3") {
      EXPECT_EQ(thread2_node->GetChildren().size(), 0u);
      EXPECT_EQ(thread2_node->GetRuntimeTraceEventNodes().size(), 1u);
370 371 372 373
    }
  }
  std::function<void(HostTraceEventNode*)> host_event_node_handle(
      [&](HostTraceEventNode* a) { logger.LogHostTraceEventNode(*a); });
374 375 376 377
  std::function<void(CudaRuntimeTraceEventNode*)> runtime_event_node_handle(
      [&](CudaRuntimeTraceEventNode* a) {
        logger.LogRuntimeTraceEventNode(*a);
      });
378 379
  std::function<void(DeviceTraceEventNode*)> device_event_node_handle(
      [&](DeviceTraceEventNode* a) { logger.LogDeviceTraceEventNode(*a); });
C
chenjian 已提交
380 381 382 383
  std::function<void(MemTraceEventNode*)> mem_event_node_handle(
      [&](MemTraceEventNode* a) { logger.LogMemTraceEventNode(*a); });
  std::function<void(OperatorSupplementEventNode*)>
      op_supplement_event_node_handle([&](OperatorSupplementEventNode* a) {});
384 385 386 387
  tree.HandleTrees(host_event_node_handle,
                   runtime_event_node_handle,
                   device_event_node_handle,
                   mem_event_node_handle,
C
chenjian 已提交
388
                   op_supplement_event_node_handle);
389
  logger.LogExtraInfo(std::unordered_map<std::string, std::string>());
390
}