pytorch/test/cpp/lazy/test_cache.cpp
Will Constable 69389fb542 Sync lazy_tensor_staging back to master (#72875)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/72875

This diff contains changes from several PRs landed to lazy_tensor_staging branch.
* generating 'fallback' overrides for each codegenned op, useful for debugging
* supports operators which are missing aten:: symbols for op names, instead using their string counterpart
* makes the IR class a base class instead of hardcoding the assumption of TS

It also resolves lint issues and in particular cleans up the following:
* {Type}s shouldn't be passed into isValueType, and using the catch-all base class of CType is nicer than specifying a list of types.

Fixes #72852

Test Plan: test manually on lazy_tensor_staging branch

Reviewed By: shunting314

Differential Revision: D34250357

fbshipit-source-id: aa7d589f605055d5d02bc77c77fa6f1182ff7497
(cherry picked from commit 2f8f5e4971)
2022-02-18 03:49:46 +00:00

64 lines
1.9 KiB
C++

#include <gtest/gtest.h>
#include <c10/util/Exception.h>
#include <torch/csrc/lazy/core/cache.h>
#include <torch/csrc/lazy/core/hash.h>
#include <torch/csrc/lazy/core/ir.h>
namespace torch {
namespace lazy {
class CacheNode : public Node {
public:
explicit CacheNode(const std::string& str)
: Node(OpKind(), /* num_outputs */ 1, /* hash_func */ [&](bool /*bakeInSizes*/) -> hash_t { return Hash(str); }),
str_(str) {}
~CacheNode() override = default;
const std::vector<Output>& operands() const override {
TORCH_INTERNAL_ASSERT(false, "Can't access operands of test node");
}
const Output& operand(size_t i) const override {
TORCH_INTERNAL_ASSERT(false, "Can't access operand[i] of test node");
}
private:
std::string str_;
};
TEST(CacheTest, BasicTest) {
std::shared_ptr<CacheNode> a = std::make_shared<CacheNode>("a");
std::shared_ptr<CacheNode> b = std::make_shared<CacheNode>("b");
std::shared_ptr<CacheNode> c = std::make_shared<CacheNode>("c");
Cache<hash_t, CacheNode, HashReducer> cache(2);
cache.Add(a->node_hash(), a);
EXPECT_EQ(cache.Get(a->node_hash()), a);
EXPECT_EQ(cache.Get(b->node_hash()), nullptr);
EXPECT_EQ(cache.Get(c->node_hash()), nullptr);
cache.Add(b->node_hash(), b);
EXPECT_EQ(cache.Get(a->node_hash()), a);
EXPECT_EQ(cache.Get(b->node_hash()), b);
EXPECT_EQ(cache.Get(c->node_hash()), nullptr);
cache.Add(c->node_hash(), c);
EXPECT_EQ(cache.Get(a->node_hash()), nullptr); // a has been evicted
EXPECT_EQ(cache.Get(b->node_hash()), b);
EXPECT_EQ(cache.Get(c->node_hash()), c);
cache.Erase(c->node_hash());
EXPECT_EQ(cache.Get(a->node_hash()), nullptr);
EXPECT_EQ(cache.Get(b->node_hash()), b);
EXPECT_EQ(cache.Get(c->node_hash()), nullptr); // c has been removed
cache.Clear();
EXPECT_EQ(cache.Get(a->node_hash()), nullptr);
EXPECT_EQ(cache.Get(b->node_hash()), nullptr);
EXPECT_EQ(cache.Get(c->node_hash()), nullptr);
}
} // namespace lazy
} // namespace torch