Files
pytorch/test/cpp/api/misc.cpp

333 lines
10 KiB
C++
Raw Normal View History

#include "catch_utils.hpp"
#include <torch/detail/ordered_dict.h>
#include <torch/expanding_array.h>
#include <torch/nn/init.h>
2018-05-30 08:55:34 -07:00
#include <torch/nn/modules/linear.h>
#include <torch/tensor.h>
#include <torch/utils.h>
#include <torch/csrc/utils/memory.h>
#include <ATen/core/optional.h>
using namespace torch::nn;
template <typename T>
using OrderedDict = torch::detail::OrderedDict<std::string, T>;
using Catch::StartsWith;
CATCH_TEST_CASE("NoGrad") {
torch::manual_seed(0);
torch::NoGradGuard guard;
Linear model(5, 2);
auto x = torch::randn({10, 5}, torch::requires_grad());
auto y = model->forward(x);
torch::Tensor s = y.sum();
s.backward();
CATCH_REQUIRE(!model->parameters()["weight"].grad().defined());
}
CATCH_TEST_CASE("autograd") {
torch::manual_seed(0);
auto x = torch::randn({3, 3}, torch::requires_grad());
Create ATen tensors via TensorOptions (#7869) * Created TensorOptions Storing the type in TensorOptions to solve the Variable problem Created convenience creation functions for TensorOptions and added tests Converted zeros to TensorOptions Converted rand to TensorOptions Fix codegen for TensorOptions and multiple arguments Put TensorOptions convenience functions into torch namespace too All factory functions except *_like support TensorOptions Integrated with recent JIT changes Support *_like functions Fix in place modification Some cleanups and fixes Support sparse_coo_tensor Fix bug in Type.cpp Fix .empty calls in C++ API Fix bug in Type.cpp Trying to fix device placement Make AutoGPU CPU compatible Remove some auto_gpu.h uses Fixing some headers Fix some remaining CUDA/AutoGPU issues Fix some AutoGPU uses Fixes to dispatch_tensor_conversion Reset version of new variables to zero Implemented parsing device strings Random fixes to tests Self review cleanups flake8 Undo changes to variable.{h,cpp} because they fail on gcc7.2 Add [cuda] tag to tensor_options_cuda.cpp Move AutoGPU::set_index_from into .cpp file because Windows is stupid and sucks Fix linker error in AutoGPU.cpp Fix bad merge conflict in native_functions.yaml Fixed caffe2/contrib/aten Fix new window functions added to TensorFactories.cpp * Removed torch::TensorOptions Added code to generate wrapper functions for factory methods Add implicit constructor from Backend to TensorOptions Remove Var() from C++ API and use torch:: functions Use torch:: functions more subtly in C++ API Make AutoGPU::set_device more exception safe Check status directly in DynamicCUDAHooksInterface Rename AutoGPU to DeviceGuard Removed set_requires_grad from python_variables.h and warn appropriately in Variable::set_requires_grad remove python_default_init: self.type() Add back original factory functions, but with deprecation warnings Disable DeviceGuard for a couple functions in ATen Remove print statement Fix DeviceGuard construction from undefined tensor Fixing CUDA device compiler issues Moved as many methods as possible into header files Dont generate python functions for deprecated factories Remove merge conflict artefact Fix tensor_options_cuda.cpp Fix set_requires_grad not being checked Fix tensor_new.h TEMPORARILY put some methods in .cpp files to see if it solves issues on windows and mac Fix bug in DeviceGuard.h Missing includes TEMPORARILY moving a few more methods into .cpp to see if it fixes windows Fixing linker errors * Fix up SummaryOps to use new factories Undo device agnostic behavior of DeviceGuard Use -1 instead of optional for default device index Also move DeviceGuard methods into header Fixes around device index after optional -> int32_t switch Fix use of DeviceGuard in new_with_tensor_copy Fix tensor_options.cpp * Fix Type::copy( * Remove test_non_float_params from ONNX tests * Set requires_grad=False in ONNX tests that use ints * Put layout/dtype/device on Tensor * Post merge fixes * Change behavior of DeviceGuard to match AutoGPU * Fix C++ API integration tests * Fix flip functions
2018-06-16 00:40:35 -07:00
auto y = torch::randn({3, 3});
auto z = x * y;
CATCH_SECTION("derivatives of zero-dim tensors") {
z.sum().backward();
CATCH_REQUIRE(x.grad().allclose(y));
}
CATCH_SECTION("derivatives of tensors") {
z.backward();
CATCH_REQUIRE(x.grad().allclose(y));
}
CATCH_SECTION("custom gradient inputs") {
Create ATen tensors via TensorOptions (#7869) * Created TensorOptions Storing the type in TensorOptions to solve the Variable problem Created convenience creation functions for TensorOptions and added tests Converted zeros to TensorOptions Converted rand to TensorOptions Fix codegen for TensorOptions and multiple arguments Put TensorOptions convenience functions into torch namespace too All factory functions except *_like support TensorOptions Integrated with recent JIT changes Support *_like functions Fix in place modification Some cleanups and fixes Support sparse_coo_tensor Fix bug in Type.cpp Fix .empty calls in C++ API Fix bug in Type.cpp Trying to fix device placement Make AutoGPU CPU compatible Remove some auto_gpu.h uses Fixing some headers Fix some remaining CUDA/AutoGPU issues Fix some AutoGPU uses Fixes to dispatch_tensor_conversion Reset version of new variables to zero Implemented parsing device strings Random fixes to tests Self review cleanups flake8 Undo changes to variable.{h,cpp} because they fail on gcc7.2 Add [cuda] tag to tensor_options_cuda.cpp Move AutoGPU::set_index_from into .cpp file because Windows is stupid and sucks Fix linker error in AutoGPU.cpp Fix bad merge conflict in native_functions.yaml Fixed caffe2/contrib/aten Fix new window functions added to TensorFactories.cpp * Removed torch::TensorOptions Added code to generate wrapper functions for factory methods Add implicit constructor from Backend to TensorOptions Remove Var() from C++ API and use torch:: functions Use torch:: functions more subtly in C++ API Make AutoGPU::set_device more exception safe Check status directly in DynamicCUDAHooksInterface Rename AutoGPU to DeviceGuard Removed set_requires_grad from python_variables.h and warn appropriately in Variable::set_requires_grad remove python_default_init: self.type() Add back original factory functions, but with deprecation warnings Disable DeviceGuard for a couple functions in ATen Remove print statement Fix DeviceGuard construction from undefined tensor Fixing CUDA device compiler issues Moved as many methods as possible into header files Dont generate python functions for deprecated factories Remove merge conflict artefact Fix tensor_options_cuda.cpp Fix set_requires_grad not being checked Fix tensor_new.h TEMPORARILY put some methods in .cpp files to see if it solves issues on windows and mac Fix bug in DeviceGuard.h Missing includes TEMPORARILY moving a few more methods into .cpp to see if it fixes windows Fixing linker errors * Fix up SummaryOps to use new factories Undo device agnostic behavior of DeviceGuard Use -1 instead of optional for default device index Also move DeviceGuard methods into header Fixes around device index after optional -> int32_t switch Fix use of DeviceGuard in new_with_tensor_copy Fix tensor_options.cpp * Fix Type::copy( * Remove test_non_float_params from ONNX tests * Set requires_grad=False in ONNX tests that use ints * Put layout/dtype/device on Tensor * Post merge fixes * Change behavior of DeviceGuard to match AutoGPU * Fix C++ API integration tests * Fix flip functions
2018-06-16 00:40:35 -07:00
z.sum().backward(torch::ones({}) * 2);
CATCH_REQUIRE(x.grad().allclose(y * 2));
}
// Assume everything else is safe from PyTorch tests.
}
CATCH_TEST_CASE("nn::init") {
auto tensor = torch::empty({3, 4}, torch::requires_grad());
CATCH_REQUIRE_THROWS_WITH(
tensor.fill_(1),
StartsWith("a leaf Variable that requires grad "
"has been used in an in-place operation"));
CATCH_REQUIRE(torch::nn::init::ones_(tensor).sum().toCInt() == 12);
}
CATCH_TEST_CASE("expanding-array") {
torch::manual_seed(0);
CATCH_SECTION("successful construction") {
CATCH_SECTION("initializer_list") {
torch::ExpandingArray<5> e({1, 2, 3, 4, 5});
CATCH_REQUIRE(e.size() == 5);
for (size_t i = 0; i < e.size(); ++i) {
CATCH_REQUIRE((*e)[i] == i + 1);
}
}
CATCH_SECTION("vector") {
torch::ExpandingArray<5> e(std::vector<int64_t>{1, 2, 3, 4, 5});
CATCH_REQUIRE(e.size() == 5);
for (size_t i = 0; i < e.size(); ++i) {
CATCH_REQUIRE((*e)[i] == i + 1);
}
}
CATCH_SECTION("array") {
torch::ExpandingArray<5> e(std::array<int64_t, 5>({1, 2, 3, 4, 5}));
CATCH_REQUIRE(e.size() == 5);
for (size_t i = 0; i < e.size(); ++i) {
CATCH_REQUIRE((*e)[i] == i + 1);
}
}
CATCH_SECTION("single value") {
torch::ExpandingArray<5> e(5);
CATCH_REQUIRE(e.size() == 5);
for (size_t i = 0; i < e.size(); ++i) {
CATCH_REQUIRE((*e)[i] == 5);
}
}
}
CATCH_SECTION("throws for incorrect size on construction") {
CATCH_SECTION("initializer_list") {
CATCH_REQUIRE_THROWS_WITH(
torch::ExpandingArray<5>({1, 2, 3, 4, 5, 6, 7}),
StartsWith("Expected 5 values, but instead got 7"));
}
CATCH_SECTION("vector") {
CATCH_REQUIRE_THROWS_WITH(
torch::ExpandingArray<5>(std::vector<int64_t>({1, 2, 3, 4, 5, 6, 7})),
StartsWith("Expected 5 values, but instead got 7"));
}
}
}
CATCH_TEST_CASE("make_unique") {
struct Test {
explicit Test(const int& x) : lvalue_(x) {}
explicit Test(int&& x) : rvalue_(x) {}
at::optional<int> lvalue_;
at::optional<int> rvalue_;
};
CATCH_SECTION("forwards rvalues correctly") {
auto ptr = torch::make_unique<Test>(123);
CATCH_REQUIRE(!ptr->lvalue_.has_value());
CATCH_REQUIRE(ptr->rvalue_.has_value());
CATCH_REQUIRE(*ptr->rvalue_ == 123);
}
CATCH_SECTION("forwards lvalues correctly") {
int x = 5;
auto ptr = torch::make_unique<Test>(x);
CATCH_REQUIRE(ptr->lvalue_.has_value());
CATCH_REQUIRE(*ptr->lvalue_ == 5);
CATCH_REQUIRE(!ptr->rvalue_.has_value());
}
CATCH_SECTION("Can construct unique_ptr of array") {
auto ptr = torch::make_unique<int[]>(3);
// Value initialization is required by the standard.
CATCH_REQUIRE(ptr[0] == 0);
CATCH_REQUIRE(ptr[1] == 0);
CATCH_REQUIRE(ptr[2] == 0);
}
}
CATCH_TEST_CASE("ordered-dict") {
CATCH_SECTION("is empty after default construction") {
OrderedDict<int> dict;
CATCH_REQUIRE(dict.subject() == "Key");
CATCH_REQUIRE(dict.is_empty());
CATCH_REQUIRE(dict.size() == 0);
}
CATCH_SECTION("insert inserts elements when they are not yet present") {
OrderedDict<int> dict;
dict.insert("a", 1);
dict.insert("b", 2);
CATCH_REQUIRE(dict.size() == 2);
}
CATCH_SECTION("get returns values when present") {
OrderedDict<int> dict;
dict.insert("a", 1);
dict.insert("b", 2);
CATCH_REQUIRE(dict.get("a") == 1);
CATCH_REQUIRE(dict.get("b") == 2);
}
CATCH_SECTION("get throws when passed keys that are not present") {
OrderedDict<int> dict;
dict.insert("a", 1);
dict.insert("b", 2);
CATCH_REQUIRE_THROWS_WITH(
dict.get("foo"), StartsWith("Key 'foo' is not defined"));
CATCH_REQUIRE_THROWS_WITH(dict.get(""), StartsWith("Key '' is not defined"));
}
CATCH_SECTION("can initialize from list") {
OrderedDict<int> dict = {{"a", 1}, {"b", 2}};
CATCH_REQUIRE(dict.size() == 2);
CATCH_REQUIRE(dict.get("a") == 1);
CATCH_REQUIRE(dict.get("b") == 2);
}
CATCH_SECTION("insert throws when passed elements that are present") {
OrderedDict<int> dict = {{"a", 1}, {"b", 2}};
CATCH_REQUIRE_THROWS_WITH(
dict.insert("a", 1), StartsWith("Key 'a' already defined"));
CATCH_REQUIRE_THROWS_WITH(
dict.insert("b", 1), StartsWith("Key 'b' already defined"));
}
CATCH_SECTION("front() returns the first item") {
OrderedDict<int> dict = {{"a", 1}, {"b", 2}};
CATCH_REQUIRE(dict.front().key == "a");
CATCH_REQUIRE(dict.front().value == 1);
}
CATCH_SECTION("back() returns the last item") {
OrderedDict<int> dict = {{"a", 1}, {"b", 2}};
CATCH_REQUIRE(dict.back().key == "b");
CATCH_REQUIRE(dict.back().value == 2);
}
CATCH_SECTION("find returns pointers to values when present") {
OrderedDict<int> dict = {{"a", 1}, {"b", 2}};
CATCH_REQUIRE(dict.find("a") != nullptr);
CATCH_REQUIRE(*dict.find("a") == 1);
CATCH_REQUIRE(dict.find("b") != nullptr);
CATCH_REQUIRE(*dict.find("b") == 2);
}
CATCH_SECTION("find returns null pointers when passed keys that are not present") {
OrderedDict<int> dict = {{"a", 1}, {"b", 2}};
CATCH_REQUIRE(dict.find("bar") == nullptr);
CATCH_REQUIRE(dict.find("") == nullptr);
}
CATCH_SECTION("operator[] returns values when passed keys that are present") {
OrderedDict<int> dict = {{"a", 1}, {"b", 2}};
CATCH_REQUIRE(dict["a"] == 1);
CATCH_REQUIRE(dict["b"] == 2);
}
CATCH_SECTION("operator[] returns items positionally when passed integers") {
OrderedDict<int> dict = {{"a", 1}, {"b", 2}};
CATCH_REQUIRE(dict[0].key == "a");
CATCH_REQUIRE(dict[0].value == 1);
CATCH_REQUIRE(dict[1].key == "b");
CATCH_REQUIRE(dict[1].value == 2);
}
CATCH_SECTION("operator[] throws when passed keys that are not present") {
OrderedDict<int> dict = {{"a", 1}, {"b", 2}};
CATCH_REQUIRE_THROWS_WITH(
dict.get("foo"), StartsWith("Key 'foo' is not defined"));
CATCH_REQUIRE_THROWS_WITH(dict.get(""), StartsWith("Key '' is not defined"));
}
CATCH_SECTION("update inserts all items from another OrderedDict") {
OrderedDict<int> dict = {{"a", 1}, {"b", 2}};
OrderedDict<int> dict2 = {{"c", 3}};
dict2.update(dict);
CATCH_REQUIRE(dict2.size() == 3);
CATCH_REQUIRE(dict2.find("a") != nullptr);
CATCH_REQUIRE(dict2.find("b") != nullptr);
CATCH_REQUIRE(dict2.find("c") != nullptr);
}
CATCH_SECTION("update also checks for duplicates") {
OrderedDict<int> dict = {{"a", 1}, {"b", 2}};
OrderedDict<int> dict2 = {{"a", 1}};
CATCH_REQUIRE_THROWS_WITH(
dict2.update(dict), StartsWith("Key 'a' already defined"));
}
CATCH_SECTION("Can iterate items") {
OrderedDict<int> dict = {{"a", 1}, {"b", 2}};
auto iterator = dict.begin();
CATCH_REQUIRE(iterator != dict.end());
CATCH_REQUIRE(iterator->key == "a");
CATCH_REQUIRE(iterator->value == 1);
++iterator;
CATCH_REQUIRE(iterator != dict.end());
CATCH_REQUIRE(iterator->key == "b");
CATCH_REQUIRE(iterator->value == 2);
++iterator;
CATCH_REQUIRE(iterator == dict.end());
}
CATCH_SECTION("clear makes the dict empty") {
OrderedDict<int> dict = {{"a", 1}, {"b", 2}};
CATCH_REQUIRE(!dict.is_empty());
dict.clear();
CATCH_REQUIRE(dict.is_empty());
}
CATCH_SECTION("can copy construct") {
OrderedDict<int> dict = {{"a", 1}, {"b", 2}};
OrderedDict<int> copy = dict;
CATCH_REQUIRE(copy.size() == 2);
CATCH_REQUIRE(*copy[0] == 1);
CATCH_REQUIRE(*copy[1] == 2);
}
CATCH_SECTION("can copy assign") {
OrderedDict<int> dict = {{"a", 1}, {"b", 2}};
OrderedDict<int> copy = {{"c", 1}};
CATCH_REQUIRE(copy.find("c") != nullptr);
copy = dict;
CATCH_REQUIRE(copy.size() == 2);
CATCH_REQUIRE(*copy[0] == 1);
CATCH_REQUIRE(*copy[1] == 2);
CATCH_REQUIRE(copy.find("c") == nullptr);
}
CATCH_SECTION("can move construct") {
OrderedDict<int> dict = {{"a", 1}, {"b", 2}};
OrderedDict<int> copy = std::move(dict);
CATCH_REQUIRE(copy.size() == 2);
CATCH_REQUIRE(*copy[0] == 1);
CATCH_REQUIRE(*copy[1] == 2);
}
CATCH_SECTION("can move assign") {
OrderedDict<int> dict = {{"a", 1}, {"b", 2}};
OrderedDict<int> copy = {{"c", 1}};
CATCH_REQUIRE(copy.find("c") != nullptr);
copy = std::move(dict);
CATCH_REQUIRE(copy.size() == 2);
CATCH_REQUIRE(*copy[0] == 1);
CATCH_REQUIRE(*copy[1] == 2);
CATCH_REQUIRE(copy.find("c") == nullptr);
}
CATCH_SECTION("can insert with braces") {
OrderedDict<std::pair<int, int>> dict;
dict.insert("a", {1, 2});
CATCH_REQUIRE(!dict.is_empty());
CATCH_REQUIRE(dict["a"].first == 1);
CATCH_REQUIRE(dict["a"].second == 2);
}
CATCH_SECTION("Error messages include the what") {
OrderedDict<int> dict("Penguin");
CATCH_REQUIRE(dict.subject() == "Penguin");
dict.insert("a", 1);
CATCH_REQUIRE(!dict.is_empty());
CATCH_REQUIRE_THROWS_WITH(
dict.get("b"), StartsWith("Penguin 'b' is not defined"));
CATCH_REQUIRE_THROWS_WITH(
dict.insert("a", 1), StartsWith("Penguin 'a' already defined"));
}
}