#include "tensorflow/core/common_runtime/gpu/gpu_device.h"
#include "tensorflow/core/common_runtime/gpu/gpu_init.h"
+#include "tensorflow/core/common_runtime/gpu/process_state.h"
#include "tensorflow/core/lib/core/errors.h"
#include "tensorflow/core/lib/core/status.h"
#include "tensorflow/core/lib/core/status_test_util.h"
#include "tensorflow/core/platform/test.h"
namespace tensorflow {
-namespace {
const char* kDeviceNamePrefix = "/job:localhost/replica:0/task:0";
-static SessionOptions MakeSessionOptions(
- const string& visible_device_list = "",
- double per_process_gpu_memory_fraction = 0, int gpu_device_count = 1,
- const std::vector<std::vector<float>>& memory_limit_mb = {}) {
- SessionOptions options;
- ConfigProto* config = &options.config;
- (*config->mutable_device_count())["GPU"] = gpu_device_count;
- GPUOptions* gpu_options = config->mutable_gpu_options();
- gpu_options->set_visible_device_list(visible_device_list);
- gpu_options->set_per_process_gpu_memory_fraction(
- per_process_gpu_memory_fraction);
- for (const auto& v : memory_limit_mb) {
- auto virtual_devices =
- gpu_options->mutable_experimental()->add_virtual_devices();
- for (float mb : v) {
- virtual_devices->add_memory_limit_mb(mb);
+class GPUDeviceTest : public ::testing::Test {
+ public:
+ void TearDown() { ProcessState::singleton()->TestOnlyReset(); }
+
+ protected:
+ static SessionOptions MakeSessionOptions(
+ const string& visible_device_list = "",
+ double per_process_gpu_memory_fraction = 0, int gpu_device_count = 1,
+ const std::vector<std::vector<float>>& memory_limit_mb = {}) {
+ SessionOptions options;
+ ConfigProto* config = &options.config;
+ (*config->mutable_device_count())["GPU"] = gpu_device_count;
+ GPUOptions* gpu_options = config->mutable_gpu_options();
+ gpu_options->set_visible_device_list(visible_device_list);
+ gpu_options->set_per_process_gpu_memory_fraction(
+ per_process_gpu_memory_fraction);
+ for (const auto& v : memory_limit_mb) {
+ auto virtual_devices =
+ gpu_options->mutable_experimental()->add_virtual_devices();
+ for (float mb : v) {
+ virtual_devices->add_memory_limit_mb(mb);
+ }
}
+ return options;
}
- return options;
-}
-static bool StartsWith(const string& lhs, const string& rhs) {
- if (rhs.length() > lhs.length()) return false;
- return lhs.substr(0, rhs.length()) == rhs;
-}
+ static bool StartsWith(const string& lhs, const string& rhs) {
+ if (rhs.length() > lhs.length()) return false;
+ return lhs.substr(0, rhs.length()) == rhs;
+ }
+};
-TEST(GPUDeviceTest, FailedToParseVisibleDeviceList) {
+TEST_F(GPUDeviceTest, FailedToParseVisibleDeviceList) {
SessionOptions opts = MakeSessionOptions("0,abc");
std::vector<tensorflow::Device*> devices;
Status status = DeviceFactory::GetFactory("GPU")->CreateDevices(
<< status;
}
-TEST(GPUDeviceTest, InvalidGpuId) {
+TEST_F(GPUDeviceTest, InvalidGpuId) {
SessionOptions opts = MakeSessionOptions("100");
std::vector<tensorflow::Device*> devices;
Status status = DeviceFactory::GetFactory("GPU")->CreateDevices(
<< status;
}
-TEST(GPUDeviceTest, DuplicateEntryInVisibleDeviceList) {
+TEST_F(GPUDeviceTest, DuplicateEntryInVisibleDeviceList) {
SessionOptions opts = MakeSessionOptions("0,0");
std::vector<tensorflow::Device*> devices;
Status status = DeviceFactory::GetFactory("GPU")->CreateDevices(
<< status;
}
-TEST(GPUDeviceTest, VirtualDeviceConfigConflictsWithMemoryFractionSettings) {
+TEST_F(GPUDeviceTest, VirtualDeviceConfigConflictsWithMemoryFractionSettings) {
SessionOptions opts = MakeSessionOptions("0", 0.1, 1, {{}});
std::vector<tensorflow::Device*> devices;
Status status = DeviceFactory::GetFactory("GPU")->CreateDevices(
<< status;
}
-TEST(GPUDeviceTest, GpuDeviceCountTooSmall) {
+TEST_F(GPUDeviceTest, GpuDeviceCountTooSmall) {
// device_count is 0, but with one entry in visible_device_list and one
// (empty) VirtualDevices messages.
SessionOptions opts = MakeSessionOptions("0", 0, 0, {{}});
<< status;
}
-TEST(GPUDeviceTest, NotEnoughGpuInVisibleDeviceList) {
+TEST_F(GPUDeviceTest, NotEnoughGpuInVisibleDeviceList) {
// Single entry in visible_device_list with two (empty) VirtualDevices
// messages.
SessionOptions opts = MakeSessionOptions("0", 0, 8, {{}, {}});
<< status;
}
-TEST(GPUDeviceTest, VirtualDeviceConfigConflictsWithVisibleDeviceList) {
+TEST_F(GPUDeviceTest, VirtualDeviceConfigConflictsWithVisibleDeviceList) {
// This test requires at least two visible GPU hardware.
if (GPUMachineManager()->VisibleDeviceCount() < 2) return;
// Three entries in visible_device_list with two (empty) VirtualDevices
<< status;
}
-TEST(GPUDeviceTest, EmptyVirtualDeviceConfig) {
+TEST_F(GPUDeviceTest, EmptyVirtualDeviceConfig) {
// It'll create single virtual device when the virtual device config is empty.
SessionOptions opts = MakeSessionOptions("0");
std::vector<tensorflow::Device*> devices;
for (auto d : devices) delete d;
}
-TEST(GPUDeviceTest, SingleVirtualDeviceWithNoMemoryLimit) {
+TEST_F(GPUDeviceTest, SingleVirtualDeviceWithNoMemoryLimit) {
// It'll create single virtual device for the gpu in question when
// memory_limit_mb is unset.
SessionOptions opts = MakeSessionOptions("0", 0, 1, {{}});
for (auto d : devices) delete d;
}
-TEST(GPUDeviceTest, SingleVirtualDeviceWithMemoryLimit) {
+TEST_F(GPUDeviceTest, SingleVirtualDeviceWithMemoryLimit) {
SessionOptions opts = MakeSessionOptions("0", 0, 1, {{123}});
std::vector<tensorflow::Device*> devices;
TF_CHECK_OK(DeviceFactory::GetFactory("GPU")->CreateDevices(
for (auto d : devices) delete d;
}
-TEST(GPUDeviceTest, MultipleVirtualDevices) {
+TEST_F(GPUDeviceTest, MultipleVirtualDevices) {
SessionOptions opts = MakeSessionOptions("0", 0, 1, {{123, 456}});
std::vector<tensorflow::Device*> devices;
TF_CHECK_OK(DeviceFactory::GetFactory("GPU")->CreateDevices(
for (auto d : devices) delete d;
}
-} // namespace
} // namespace tensorflow
#endif