2018-09-22 04:12:37 +00:00
|
|
|
#include <gtest/gtest.h>
|
2018-05-01 01:36:35 +00:00
|
|
|
|
2019-10-10 16:44:55 +00:00
|
|
|
#include <torch/torch.h>
|
2018-05-01 01:36:35 +00:00
|
|
|
|
2018-09-22 04:12:37 +00:00
|
|
|
#include <test/cpp/api/support.h>
|
2018-05-24 19:46:51 +00:00
|
|
|
|
2019-08-28 04:41:15 +00:00
|
|
|
#include <functional>
|
|
|
|
|
|
|
|
|
|
using namespace torch::test;
|
|
|
|
|
|
|
|
|
|
void torch_warn_once_A() {
|
|
|
|
|
TORCH_WARN_ONCE("warn once");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void torch_warn_once_B() {
|
|
|
|
|
TORCH_WARN_ONCE("warn something else once");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void torch_warn() {
|
|
|
|
|
TORCH_WARN("warn multiple times");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(UtilsTest, WarnOnce) {
|
|
|
|
|
{
|
2020-04-23 08:05:31 +00:00
|
|
|
WarningCapture warnings;
|
2019-08-28 04:41:15 +00:00
|
|
|
|
|
|
|
|
torch_warn_once_A();
|
|
|
|
|
torch_warn_once_A();
|
|
|
|
|
torch_warn_once_B();
|
|
|
|
|
torch_warn_once_B();
|
|
|
|
|
|
2020-04-23 08:05:31 +00:00
|
|
|
ASSERT_EQ(count_substr_occurrences(warnings.str(), "warn once"), 1);
|
|
|
|
|
ASSERT_EQ(
|
|
|
|
|
count_substr_occurrences(warnings.str(), "warn something else once"),
|
|
|
|
|
1);
|
2019-08-28 04:41:15 +00:00
|
|
|
}
|
|
|
|
|
{
|
2020-04-23 08:05:31 +00:00
|
|
|
WarningCapture warnings;
|
2019-08-28 04:41:15 +00:00
|
|
|
|
|
|
|
|
torch_warn();
|
|
|
|
|
torch_warn();
|
|
|
|
|
torch_warn();
|
|
|
|
|
|
2020-04-23 08:05:31 +00:00
|
|
|
ASSERT_EQ(
|
|
|
|
|
count_substr_occurrences(warnings.str(), "warn multiple times"), 3);
|
2019-08-28 04:41:15 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-22 04:12:37 +00:00
|
|
|
TEST(NoGradTest, SetsGradModeCorrectly) {
|
2018-06-28 03:00:53 +00:00
|
|
|
torch::manual_seed(0);
|
|
|
|
|
torch::NoGradGuard guard;
|
2018-09-22 04:12:37 +00:00
|
|
|
torch::nn::Linear model(5, 2);
|
2018-06-28 03:00:53 +00:00
|
|
|
auto x = torch::randn({10, 5}, torch::requires_grad());
|
|
|
|
|
auto y = model->forward(x);
|
|
|
|
|
torch::Tensor s = y.sum();
|
|
|
|
|
|
2019-09-18 16:19:00 +00:00
|
|
|
// Mimicking python API behavior:
|
|
|
|
|
ASSERT_THROWS_WITH(s.backward(),
|
|
|
|
|
"element 0 of tensors does not require grad and does not have a grad_fn")
|
2018-05-01 01:36:35 +00:00
|
|
|
}
|
2018-05-17 21:10:15 +00:00
|
|
|
|
2018-09-22 04:12:37 +00:00
|
|
|
struct AutogradTest : torch::test::SeedingFixture {
|
|
|
|
|
AutogradTest() {
|
|
|
|
|
x = torch::randn({3, 3}, torch::requires_grad());
|
|
|
|
|
y = torch::randn({3, 3});
|
|
|
|
|
z = x * y;
|
2018-05-25 00:31:41 +00:00
|
|
|
}
|
2018-09-22 04:12:37 +00:00
|
|
|
torch::Tensor x, y, z;
|
|
|
|
|
};
|
2018-05-25 00:31:41 +00:00
|
|
|
|
2018-09-22 04:12:37 +00:00
|
|
|
TEST_F(AutogradTest, CanTakeDerivatives) {
|
2019-09-18 16:19:00 +00:00
|
|
|
z.backward(torch::ones_like(z));
|
2018-09-22 04:12:37 +00:00
|
|
|
ASSERT_TRUE(x.grad().allclose(y));
|
2018-07-13 01:45:48 +00:00
|
|
|
}
|
|
|
|
|
|
2018-09-22 04:12:37 +00:00
|
|
|
TEST_F(AutogradTest, CanTakeDerivativesOfZeroDimTensors) {
|
|
|
|
|
z.sum().backward();
|
|
|
|
|
ASSERT_TRUE(x.grad().allclose(y));
|
2018-05-17 21:10:15 +00:00
|
|
|
}
|
2018-05-24 19:46:51 +00:00
|
|
|
|
2018-09-22 04:12:37 +00:00
|
|
|
TEST_F(AutogradTest, CanPassCustomGradientInputs) {
|
|
|
|
|
z.sum().backward(torch::ones({}) * 2);
|
|
|
|
|
ASSERT_TRUE(x.grad().allclose(y * 2));
|
2019-08-28 04:41:15 +00:00
|
|
|
}
|
2020-06-12 15:41:35 +00:00
|
|
|
|
|
|
|
|
TEST(DeterministicTest, CanSetDeterministic) {
|
|
|
|
|
auto context = &at::globalContext();
|
|
|
|
|
for (bool deterministic : {true, false}) {
|
|
|
|
|
context->setDeterministic(deterministic);
|
|
|
|
|
ASSERT_TRUE(context->deterministic() == deterministic);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(DeterministicTest, CanAlertNotDeterministic) {
|
|
|
|
|
auto context = &at::globalContext();
|
|
|
|
|
context->setDeterministic(true);
|
|
|
|
|
ASSERT_ANY_THROW(context->alertNotDeterministic("test"));
|
|
|
|
|
context->setDeterministic(false);
|
|
|
|
|
// Should not throw error if deterministic setting is turned off
|
|
|
|
|
context->alertNotDeterministic("test");
|
|
|
|
|
}
|