pytorch/test/cpp/lazy/test_ir_util.cpp
Antonio Kim 2c2c13d21b Decouple Lazy Node Shape Cache (#75324)
Summary:
Next stage of breaking up https://github.com/pytorch/pytorch/pull/74710

Move shape cache implementation to the backend interface. Also, clean up some of the hashing logic in the base node class.

CC: wconstab JackCaoG henrytwo

Partially Fixes https://github.com/pytorch/pytorch/issues/74628

Pull Request resolved: https://github.com/pytorch/pytorch/pull/75324

Reviewed By: anjali411

Differential Revision: D35730823

Pulled By: wconstab

fbshipit-source-id: cf6fa326319b9324e5f422a78817b6fb5bf7e9b8
(cherry picked from commit faec5043df56639e2fd23de2d91ae796e4f3df70)
2022-04-21 17:27:05 -07:00

76 lines
1.9 KiB
C++

#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_metadata.h>
#include <torch/csrc/lazy/core/ir_util.h>
namespace torch {
namespace lazy {
class IrUtilNode : public Node {
public:
explicit IrUtilNode()
: Node(OpKind(), /* num_outputs */ 1), hash_(Hash(0)) {}
~IrUtilNode() override = default;
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:
hash_t hash_;
};
/* a
* / \
*b c
* \ /
* d
* Post-order: d c b a
*/
TEST(IrUtilTest, BasicTest) {
NodePtr a = MakeNode<IrUtilNode>();
NodePtr b = MakeNode<IrUtilNode>();
NodePtr c = MakeNode<IrUtilNode>();
NodePtr d = MakeNode<IrUtilNode>();
dynamic_cast<IrUtilNode*>(a.get())->AddOperand(Value(b, 0));
dynamic_cast<IrUtilNode*>(a.get())->AddOperand(Value(c, 1));
dynamic_cast<IrUtilNode*>(b.get())->AddOperand(Value(d, 0));
dynamic_cast<IrUtilNode*>(c.get())->AddOperand(Value(d, 0));
std::vector<Node*> postorder = Util::ComputePostOrder({a.get()});
EXPECT_EQ(postorder.size(), 4);
EXPECT_EQ(postorder.at(0), d.get());
EXPECT_EQ(postorder.at(1), c.get());
EXPECT_EQ(postorder.at(2), b.get());
EXPECT_EQ(postorder.at(3), a.get());
}
/* a
* / \
*b---c
* Post-order: not valid
*/
TEST(IrUtilTest, TestCircle) {
NodePtr a = MakeNode<IrUtilNode>();
NodePtr b = MakeNode<IrUtilNode>();
NodePtr c = MakeNode<IrUtilNode>();
dynamic_cast<IrUtilNode*>(a.get())->AddOperand(Value(b, 0));
dynamic_cast<IrUtilNode*>(b.get())->AddOperand(Value(c, 0));
dynamic_cast<IrUtilNode*>(c.get())->AddOperand(Value(a, 0));
EXPECT_THROW(Util::ComputePostOrder({a.get()}), c10::Error);
}
} // namespace lazy
} // namespace torch