#include #include #include "test/cpp/cpp_test_util.h" #include "test/cpp/torch_xla_test.h" #include "torch_xla/csrc/ir.h" #include "torch_xla/csrc/lowering_context.h" #include "torch_xla/csrc/ops/arithmetic_ir_ops.h" #include "torch_xla/csrc/ops/dynamic_ir.h" #include "torch_xla/csrc/ops/expand.h" #include "torch_xla/csrc/ops/nonzero.h" #include "torch_xla/csrc/ops/ops.h" #include "torch_xla/csrc/ops/scalar.h" #include "torch_xla/csrc/ops/select.h" #include "torch_xla/csrc/ops/unselect.h" #include "torch_xla/csrc/ops/update_slice.h" namespace torch_xla { namespace cpp_test { class IrTest : public TorchXlaTest {}; TEST_F(IrTest, TestScalarCreate) { torch::lazy::NodePtr scalar = ScalarOp(1.0, xla::F32); ASSERT_TRUE(scalar != nullptr); } TEST_F(IrTest, TestHash) { torch::lazy::NodePtr scalar1 = ScalarOp(1.0, xla::F32); torch::lazy::NodePtr scalar2 = ScalarOp(2.0, xla::F32); torch::lazy::Value add1 = torch::lazy::Value(scalar1, 0) + torch::lazy::Value(scalar2, 0); torch::lazy::NodePtr scalar3 = ScalarOp(1.0, xla::F32); torch::lazy::NodePtr scalar4 = ScalarOp(2.0, xla::F32); torch::lazy::Value add2 = torch::lazy::Value(scalar3, 0) + torch::lazy::Value(scalar4, 0); torch::lazy::NodePtr scalar5 = ScalarOp(11.0, xla::F32); torch::lazy::NodePtr scalar6 = ScalarOp(22.0, xla::F32); torch::lazy::Value add3 = torch::lazy::Value(scalar5, 0) + torch::lazy::Value(scalar6, 0); EXPECT_EQ(add1->hash(), add2->hash()); EXPECT_NE(add1->hash(), add3->hash()); torch::lazy::Value sub = torch::lazy::Value(scalar1, 0) - torch::lazy::Value(scalar2, 0); EXPECT_NE(add1->hash(), sub->hash()); } TEST_F(IrTest, TestSelectUnselect) { ForEachDevice([&](const torch::lazy::BackendDevice& device) { at::Tensor a = at::rand({4, 16, 3}, at::TensorOptions(at::kFloat)).abs() + 1.0; torch::lazy::Value v_a = GetTensorIrValue(a, device); torch::lazy::Value v_s = torch_xla::MakeNode