pytorch/test/cpp/api/tensor_options_cuda.cpp
Peter Goldsborough b770156a7a Functional DataParallel (#9234)
Summary:
This PR adds the functional version of `DataParallel` (i.e. `data_parallel`) to the C++ frontend.

For this, I had to:
1. Add "differentiable" versions of scatter and gather, which perform their inverse operation in the backward pass, to C++. I've added them under `torch/csrc/autograd/functions/comm.{h,cpp}`. I had to move some utilities from `VariableType.cpp` into `torch/csrc/autograd/functions/utils.h`, and changed them a bit to fix the `const_cast`s for which there were `TODO`s,
2. Implement the `replicate`, `parallel_apply` and the combining `data_parallel` functions in C++.

`replicate` is implemented based on our existing `clone()` interface, along with the ability to set the current device via `at::OptionsGuard` (so nice).

`parallel_apply` is implemented using `at::parallel_for` (CC cpuhrsch) and [follows the code from PyTorch](https://github.com/pytorch/pytorch/blob/master/torch/nn/parallel/parallel_apply.py).

Added lots of tests for these things.

apaszke ezyang ebetica colesbury
Pull Request resolved: https://github.com/pytorch/pytorch/pull/9234

Differential Revision: D8865182

Pulled By: goldsborough

fbshipit-source-id: 4f1fecf2b3f3bc1540c071dfb2d23dd45de433e4
2018-07-19 16:12:04 -07:00

129 lines
4.0 KiB
C++

#include "catch.hpp"
#include <ATen/Context.h>
#include <ATen/DeviceGuard.h>
#include <ATen/Functions.h>
#include <ATen/OptionsGuard.h>
#include <ATen/TensorOptions.h>
using namespace at;
// A macro so we don't lose location information when an assertion fails.
#define REQUIRE_OPTIONS(device_, index_, type_, layout_) \
REQUIRE(options.device().type() == Device((device_), (index_)).type()); \
REQUIRE(options.device().index() == Device((device_), (index_)).index()); \
REQUIRE(options.dtype() == (type_)); \
REQUIRE(options.layout() == (layout_))
#define REQUIRE_TENSOR_OPTIONS(device_, index_, type_, layout_) \
REQUIRE(tensor.device().type() == Device((device_), (index_)).type()); \
REQUIRE(tensor.device().index() == Device((device_), (index_)).index()); \
REQUIRE(tensor.type().scalarType() == (type_)); \
REQUIRE(tensor.type().layout() == (layout_))
TEST_CASE("TensorOptions/ConstructsWellFromCUDATypes", "[cuda]") {
auto options = TensorOptions(CUDA(kFloat));
REQUIRE_OPTIONS(kCUDA, -1, kFloat, kStrided);
options = TensorOptions(CUDA(kInt));
REQUIRE_OPTIONS(kCUDA, -1, kInt, kStrided);
options = TensorOptions(getType(kSparseCUDA, kFloat));
REQUIRE_OPTIONS(kCUDA, -1, kFloat, kSparse);
options = TensorOptions(getType(kSparseCUDA, kByte));
REQUIRE_OPTIONS(kCUDA, -1, kByte, kSparse);
options = TensorOptions(CUDA(kFloat), /*device=*/5);
REQUIRE_OPTIONS(kCUDA, 5, kFloat, kStrided);
options = TensorOptions(getType(kSparseCUDA, kFloat), /*device=*/5);
REQUIRE_OPTIONS(kCUDA, 5, kFloat, kSparse);
}
TEST_CASE("TensorOptions/ConstructsWellFromCUDATensors", "[multi-cuda]") {
auto options = TensorOptions(empty(5, device(kCUDA).dtype(kDouble)));
REQUIRE_OPTIONS(kCUDA, 0, kDouble, kStrided);
options = TensorOptions(empty(5, getType(kSparseCUDA, kByte)));
REQUIRE_OPTIONS(kCUDA, 0, kByte, kSparse);
if (at::globalContext().getNumGPUs() > 1) {
Tensor tensor;
{
DeviceGuard guard(1);
tensor = empty(5, device(kCUDA));
}
options = TensorOptions(tensor);
REQUIRE_OPTIONS(kCUDA, 1, kFloat, kStrided);
{
DeviceGuard guard(1);
tensor = empty(5, device(kCUDA).layout(kSparse));
}
options = TensorOptions(tensor);
REQUIRE_OPTIONS(kCUDA, 1, kFloat, kSparse);
}
}
TEST_CASE("OptionsGuardCUDA", "[multi-cuda]") {
Tensor tensor;
{
OptionsGuard guard(device(kCUDA));
tensor = at::empty({10});
}
REQUIRE_TENSOR_OPTIONS(kCUDA, 0, kFloat, kStrided);
{
OptionsGuard guard(device({kCUDA, 1}));
tensor = at::empty({10});
}
REQUIRE_TENSOR_OPTIONS(kCUDA, 1, kFloat, kStrided);
{
OptionsGuard guard(device(kCUDA).dtype(kInt));
tensor = at::empty({10});
}
REQUIRE_TENSOR_OPTIONS(kCUDA, 0, kInt, kStrided);
}
TEST_CASE("DeviceGuardOptionsGuardInteraction", "[multi-cuda]") {
Tensor tensor;
{
// Check that OptionsGuard respects any active device before construction.
DeviceGuard guard(1);
{
OptionsGuard guard(device(kCUDA));
tensor = at::empty({10});
REQUIRE_TENSOR_OPTIONS(kCUDA, 1, kFloat, kStrided);
{
// Check that OptionsGuard respects any active device after
// construction.
DeviceGuard guard(0);
tensor = at::empty({10});
REQUIRE_TENSOR_OPTIONS(kCUDA, 0, kFloat, kStrided);
{
OptionsGuard guard(device({kCUDA, 1}));
tensor = at::empty({10});
REQUIRE_TENSOR_OPTIONS(kCUDA, 1, kFloat, kStrided);
}
}
}
}
}
TEST_CASE("DeviceGuardIsMovable", "[cuda]") {
DeviceGuard first(1);
REQUIRE(first.original_index() == 0);
REQUIRE(first.last_index() == 1);
DeviceGuard second(std::move(first));
REQUIRE(second.original_index() == 0);
REQUIRE(second.last_index() == 1);
REQUIRE(first.original_index() == -1);
DeviceGuard third;
third = std::move(second);
REQUIRE(third.original_index() == 0);
REQUIRE(third.last_index() == 1);
REQUIRE(second.original_index() == -1);
}