File: test_trie_cache.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 (97 lines) | stat: -rw-r--r-- 2,650 bytes parent folder | download | duplicates (3)
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
89
90
91
92
93
94
95
96
97
#include <gtest/gtest.h>

#include <c10/util/Exception.h>
#include <torch/csrc/lazy/core/config.h>
#include <torch/csrc/lazy/core/ir.h>
#include <torch/csrc/lazy/core/ir_builder.h>
#include <torch/csrc/lazy/core/ir_metadata.h>
#include <torch/csrc/lazy/core/ir_util.h>
#include <memory>

namespace torch {
namespace lazy {

class TrieCacheNode : public Node {
 public:
  static OpKind ClassOpKind() {
    return OpKind();
  }

  explicit TrieCacheNode(size_t id)
      : Node(ClassOpKind(), /* num_outputs */ 1), id_(id), hash_(Hash(id_)) {}
  ~TrieCacheNode() override = default;

  bool CanBeReused(size_t id) const {
    return (id_ == id);
  }

  void AddOperand(Value v) {
    if (!v.node) {
      return;
    }
    operands_as_outputs_.emplace_back(v.node.get(), v.index);
    operands_.push_back(std::move(v.node));
  }

  hash_t hash() const override {
    return hash_;
  }
  hash_t shapeHash() const override {
    return hash_;
  }

 private:
  size_t id_;
  hash_t hash_;
};

TEST(TrieCacheTest, TestSinglePath) {
  FLAGS_torch_lazy_reuse_ir = true;
  TrieCache::Get()->Clear();

  NodePtr a = ReuseOrMakeNode<TrieCacheNode>(0);
  NodePtr b = ReuseOrMakeNode<TrieCacheNode>(1);
  NodePtr c = ReuseOrMakeNode<TrieCacheNode>(2);
  TrieCache::Get()->ResetCurrent(); // MarkStep

  EXPECT_EQ(ReuseOrMakeNode<TrieCacheNode>(0).get(), a.get());
  EXPECT_EQ(ReuseOrMakeNode<TrieCacheNode>(1).get(), b.get());
  EXPECT_EQ(ReuseOrMakeNode<TrieCacheNode>(2).get(), c.get());
  TrieCache::Get()->ResetCurrent(); // MarkStep
}

/*
 *    0
 *    |
 *    1
 *   / \
 *  2   3
 */
TEST(TrieCacheTest, TestTwoPaths) {
  FLAGS_torch_lazy_reuse_ir = true;
  TrieCache::Get()->Clear();

  NodePtr a = ReuseOrMakeNode<TrieCacheNode>(0);
  NodePtr b = ReuseOrMakeNode<TrieCacheNode>(1);
  NodePtr c = ReuseOrMakeNode<TrieCacheNode>(2);
  TrieCache::Get()->ResetCurrent(); // MarkStep

  EXPECT_EQ(ReuseOrMakeNode<TrieCacheNode>(0).get(), a.get());
  EXPECT_EQ(ReuseOrMakeNode<TrieCacheNode>(1).get(), b.get());
  NodePtr d = ReuseOrMakeNode<TrieCacheNode>(3);
  EXPECT_NE(d.get(), c.get());
  TrieCache::Get()->ResetCurrent(); // MarkStep

  EXPECT_EQ(ReuseOrMakeNode<TrieCacheNode>(0).get(), a.get());
  EXPECT_EQ(ReuseOrMakeNode<TrieCacheNode>(1).get(), b.get());
  EXPECT_EQ(ReuseOrMakeNode<TrieCacheNode>(3).get(), d.get());
  TrieCache::Get()->ResetCurrent(); // MarkStep

  EXPECT_EQ(ReuseOrMakeNode<TrieCacheNode>(0).get(), a.get());
  EXPECT_EQ(ReuseOrMakeNode<TrieCacheNode>(1).get(), b.get());
  EXPECT_EQ(ReuseOrMakeNode<TrieCacheNode>(2).get(), c.get());
  TrieCache::Get()->ResetCurrent(); // MarkStep
}

} // namespace lazy
} // namespace torch