pytorch/test/cpp/lazy/test_backend_device.cpp
Jiewen Tan 6011c35a79 [LTC] Upstream class BackendDevice (#68027)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/68027

This commit upstreams class BackendDevice to the master, which is a backend
specific representation of the actual hardware, for instances, CPU, GPU, or
TPU.

This concept is important for backend like XLA where it needs to tell the
actual hardware type from the c10::DeviceType::Lazy virtual device during
both IR constructions and lowerings.

Test Plan: ./build/bin/test_lazy --gtest_filter=BackendDeviceTest.*

Reviewed By: wconstab

Differential Revision: D32261838

Pulled By: alanwaketan

fbshipit-source-id: 579c3fc5f9da7847c887a383c6047e8ecb9cc5bc
2021-11-10 07:05:43 -08:00

73 lines
1.8 KiB
C++

#include <gtest/gtest.h>
#include <sstream>
#include <torch/csrc/lazy/backend/backend_device.h>
namespace torch {
namespace lazy {
TEST(BackendDeviceTest, BackendDeviceType) {
auto type = BackendDeviceType();
EXPECT_EQ(type.type, 0);
EXPECT_STREQ(type.toString().c_str(), "Unknown");
}
TEST(BackendDeviceTest, Basic1) {
auto device = BackendDevice();
EXPECT_EQ(device.type(), 0);
EXPECT_EQ(device.ordinal(), 0);
EXPECT_STREQ(device.toString().c_str(), "Unknown0");
}
TEST(BackendDeviceTest, Basic2) {
auto type = std::make_shared<BackendDeviceType>();
type->type = 1;
auto device = BackendDevice(std::move(type), 1);
EXPECT_EQ(device.type(), 1);
EXPECT_EQ(device.ordinal(), 1);
EXPECT_STREQ(device.toString().c_str(), "Unknown1");
}
TEST(BackendDeviceTest, Basic3) {
struct TestType : public BackendDeviceType {
std::string toString() const override { return "Test"; }
};
auto device = BackendDevice(std::make_shared<TestType>(), 1);
EXPECT_EQ(device.type(), 0);
EXPECT_EQ(device.ordinal(), 1);
EXPECT_STREQ(device.toString().c_str(), "Test1");
}
TEST(BackendDeviceTest, Compare) {
auto type = std::make_shared<BackendDeviceType>();
type->type = 1;
auto device1 = BackendDevice(std::make_shared<BackendDeviceType>(), 1);
auto device2 = BackendDevice(std::move(type), 0);
auto device3 = BackendDevice(std::make_shared<BackendDeviceType>(), 2);
auto device4 = BackendDevice(std::make_shared<BackendDeviceType>(), 1);
EXPECT_NE(device1, device2);
EXPECT_NE(device1, device3);
EXPECT_EQ(device1, device4);
EXPECT_LT(device1, device2);
EXPECT_LT(device1, device3);
}
TEST(BackendDeviceTest, Ostream) {
auto device = BackendDevice();
std::stringstream ss;
ss << device;
EXPECT_EQ(device.toString(), ss.str());
}
} // namespace lazy
} // namespace torch