File: test_dce.cpp

package info (click to toggle)
pytorch 1.13.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 139,252 kB
  • sloc: cpp: 1,100,274; python: 706,454; ansic: 83,052; asm: 7,618; java: 3,273; sh: 2,841; javascript: 612; makefile: 323; xml: 269; ruby: 185; yacc: 144; objc: 68; lex: 44
file content (52 lines) | stat: -rw-r--r-- 1,612 bytes parent folder | download | duplicates (4)
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
#include <gtest/gtest.h>

#include <torch/csrc/jit/ir/irparser.h>
#include <torch/csrc/jit/passes/dead_code_elimination.h>
#include <torch/csrc/jit/testing/file_check.h>

namespace torch {
namespace jit {
TEST(EliminateDeadCodeTest, Basic) {
  auto graph = std::make_shared<Graph>();

  // Consider the following loop:
  //   for i in range(3):
  //     tot += a[0][0]
  //     b = a[0]
  //     b[0] += 1
  //   print(tot)
  // We want to check that b[0] and b are properly marked as live and thus not
  // DCE'd.
  const std::string input =
      R"IR(
graph():
  %48 : None = prim::Constant()
  %50 : bool = prim::Constant[value=1]()
  %0 : int = prim::Constant[value=2]()
  %12 : int = prim::Constant[value=1]()
  %24 : int = prim::Constant[value=3]()
  %31 : int = prim::Constant[value=0]()
  %2 : int[] = prim::ListConstruct(%0, %0)
  %a.1 : Tensor = prim::MakeTestTensor()
  %14 : int[] = prim::ListConstruct(%12)
  %tot.1 : Tensor = prim::MakeTestTensor()
  %tot : Tensor = prim::Loop(%24, %50, %tot.1)
    block0(%i : int, %tot.6 : Tensor):
      %33 : Tensor = aten::select(%a.1, %31, %31)
      %35 : Tensor = aten::select(%33, %31, %31)
      # CHECK: add_
      %tot.3 : Tensor = aten::add_(%tot.6, %35, %12)
      %b.1 : Tensor = aten::select(%a.1, %31, %31)
      %44 : Tensor = aten::select(%b.1, %31, %31)
      # CHECK: add_
      %46 : Tensor = aten::add_(%44, %12, %12)
      -> (%50, %tot.3)
  return (%tot)
)IR";
  parseIR(input, graph.get());
  EliminateDeadCode(graph);
  // Check that dead code elimin
  testing::FileCheck().run(input, *graph);
}
} // namespace jit
} // namespace torch