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");
|
|
|
|
|
}
|
|
|
|
|
|
Make PyTorch code-base clang-tidy compliant (#56892)
Summary:
This is an automatic change generated by the following script:
```
#!/usr/bin/env python3
from subprocess import check_output, check_call
import os
def get_compiled_files_list():
import json
with open("build/compile_commands.json") as f:
data = json.load(f)
files = [os.path.relpath(node['file']) for node in data]
for idx, fname in enumerate(files):
if fname.startswith('build/') and fname.endswith('.DEFAULT.cpp'):
files[idx] = fname[len('build/'):-len('.DEFAULT.cpp')]
return files
def run_clang_tidy(fname):
check_call(["python3", "tools/clang_tidy.py", "-c", "build", "-x", fname,"-s"])
changes = check_output(["git", "ls-files", "-m"])
if len(changes) == 0:
return
check_call(["git", "commit","--all", "-m", f"NOLINT stubs for {fname}"])
def main():
git_files = check_output(["git", "ls-files"]).decode("ascii").split("\n")
compiled_files = get_compiled_files_list()
for idx, fname in enumerate(git_files):
if fname not in compiled_files:
continue
if fname.startswith("caffe2/contrib/aten/"):
continue
print(f"[{idx}/{len(git_files)}] Processing {fname}")
run_clang_tidy(fname)
if __name__ == "__main__":
main()
```
Pull Request resolved: https://github.com/pytorch/pytorch/pull/56892
Reviewed By: H-Huang
Differential Revision: D27991944
Pulled By: malfet
fbshipit-source-id: 5415e1eb2c1b34319a4f03024bfaa087007d7179
2021-04-28 21:09:06 +00:00
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
|
2019-08-28 04:41:15 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
Make PyTorch code-base clang-tidy compliant (#56892)
Summary:
This is an automatic change generated by the following script:
```
#!/usr/bin/env python3
from subprocess import check_output, check_call
import os
def get_compiled_files_list():
import json
with open("build/compile_commands.json") as f:
data = json.load(f)
files = [os.path.relpath(node['file']) for node in data]
for idx, fname in enumerate(files):
if fname.startswith('build/') and fname.endswith('.DEFAULT.cpp'):
files[idx] = fname[len('build/'):-len('.DEFAULT.cpp')]
return files
def run_clang_tidy(fname):
check_call(["python3", "tools/clang_tidy.py", "-c", "build", "-x", fname,"-s"])
changes = check_output(["git", "ls-files", "-m"])
if len(changes) == 0:
return
check_call(["git", "commit","--all", "-m", f"NOLINT stubs for {fname}"])
def main():
git_files = check_output(["git", "ls-files"]).decode("ascii").split("\n")
compiled_files = get_compiled_files_list()
for idx, fname in enumerate(git_files):
if fname not in compiled_files:
continue
if fname.startswith("caffe2/contrib/aten/"):
continue
print(f"[{idx}/{len(git_files)}] Processing {fname}")
run_clang_tidy(fname)
if __name__ == "__main__":
main()
```
Pull Request resolved: https://github.com/pytorch/pytorch/pull/56892
Reviewed By: H-Huang
Differential Revision: D27991944
Pulled By: malfet
fbshipit-source-id: 5415e1eb2c1b34319a4f03024bfaa087007d7179
2021-04-28 21:09:06 +00:00
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
|
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
|
|
|
|
Make PyTorch code-base clang-tidy compliant (#56892)
Summary:
This is an automatic change generated by the following script:
```
#!/usr/bin/env python3
from subprocess import check_output, check_call
import os
def get_compiled_files_list():
import json
with open("build/compile_commands.json") as f:
data = json.load(f)
files = [os.path.relpath(node['file']) for node in data]
for idx, fname in enumerate(files):
if fname.startswith('build/') and fname.endswith('.DEFAULT.cpp'):
files[idx] = fname[len('build/'):-len('.DEFAULT.cpp')]
return files
def run_clang_tidy(fname):
check_call(["python3", "tools/clang_tidy.py", "-c", "build", "-x", fname,"-s"])
changes = check_output(["git", "ls-files", "-m"])
if len(changes) == 0:
return
check_call(["git", "commit","--all", "-m", f"NOLINT stubs for {fname}"])
def main():
git_files = check_output(["git", "ls-files"]).decode("ascii").split("\n")
compiled_files = get_compiled_files_list()
for idx, fname in enumerate(git_files):
if fname not in compiled_files:
continue
if fname.startswith("caffe2/contrib/aten/"):
continue
print(f"[{idx}/{len(git_files)}] Processing {fname}")
run_clang_tidy(fname)
if __name__ == "__main__":
main()
```
Pull Request resolved: https://github.com/pytorch/pytorch/pull/56892
Reviewed By: H-Huang
Differential Revision: D27991944
Pulled By: malfet
fbshipit-source-id: 5415e1eb2c1b34319a4f03024bfaa087007d7179
2021-04-28 21:09:06 +00:00
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
|
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
|
|
|
}
|
|
|
|
|
|
Make PyTorch code-base clang-tidy compliant (#56892)
Summary:
This is an automatic change generated by the following script:
```
#!/usr/bin/env python3
from subprocess import check_output, check_call
import os
def get_compiled_files_list():
import json
with open("build/compile_commands.json") as f:
data = json.load(f)
files = [os.path.relpath(node['file']) for node in data]
for idx, fname in enumerate(files):
if fname.startswith('build/') and fname.endswith('.DEFAULT.cpp'):
files[idx] = fname[len('build/'):-len('.DEFAULT.cpp')]
return files
def run_clang_tidy(fname):
check_call(["python3", "tools/clang_tidy.py", "-c", "build", "-x", fname,"-s"])
changes = check_output(["git", "ls-files", "-m"])
if len(changes) == 0:
return
check_call(["git", "commit","--all", "-m", f"NOLINT stubs for {fname}"])
def main():
git_files = check_output(["git", "ls-files"]).decode("ascii").split("\n")
compiled_files = get_compiled_files_list()
for idx, fname in enumerate(git_files):
if fname not in compiled_files:
continue
if fname.startswith("caffe2/contrib/aten/"):
continue
print(f"[{idx}/{len(git_files)}] Processing {fname}")
run_clang_tidy(fname)
if __name__ == "__main__":
main()
```
Pull Request resolved: https://github.com/pytorch/pytorch/pull/56892
Reviewed By: H-Huang
Differential Revision: D27991944
Pulled By: malfet
fbshipit-source-id: 5415e1eb2c1b34319a4f03024bfaa087007d7179
2021-04-28 21:09:06 +00:00
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
|
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
|
|
|
|
Make PyTorch code-base clang-tidy compliant (#56892)
Summary:
This is an automatic change generated by the following script:
```
#!/usr/bin/env python3
from subprocess import check_output, check_call
import os
def get_compiled_files_list():
import json
with open("build/compile_commands.json") as f:
data = json.load(f)
files = [os.path.relpath(node['file']) for node in data]
for idx, fname in enumerate(files):
if fname.startswith('build/') and fname.endswith('.DEFAULT.cpp'):
files[idx] = fname[len('build/'):-len('.DEFAULT.cpp')]
return files
def run_clang_tidy(fname):
check_call(["python3", "tools/clang_tidy.py", "-c", "build", "-x", fname,"-s"])
changes = check_output(["git", "ls-files", "-m"])
if len(changes) == 0:
return
check_call(["git", "commit","--all", "-m", f"NOLINT stubs for {fname}"])
def main():
git_files = check_output(["git", "ls-files"]).decode("ascii").split("\n")
compiled_files = get_compiled_files_list()
for idx, fname in enumerate(git_files):
if fname not in compiled_files:
continue
if fname.startswith("caffe2/contrib/aten/"):
continue
print(f"[{idx}/{len(git_files)}] Processing {fname}")
run_clang_tidy(fname)
if __name__ == "__main__":
main()
```
Pull Request resolved: https://github.com/pytorch/pytorch/pull/56892
Reviewed By: H-Huang
Differential Revision: D27991944
Pulled By: malfet
fbshipit-source-id: 5415e1eb2c1b34319a4f03024bfaa087007d7179
2021-04-28 21:09:06 +00:00
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
|
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
|
|
|
}
|
2021-01-04 19:51:28 +00:00
|
|
|
|
Make PyTorch code-base clang-tidy compliant (#56892)
Summary:
This is an automatic change generated by the following script:
```
#!/usr/bin/env python3
from subprocess import check_output, check_call
import os
def get_compiled_files_list():
import json
with open("build/compile_commands.json") as f:
data = json.load(f)
files = [os.path.relpath(node['file']) for node in data]
for idx, fname in enumerate(files):
if fname.startswith('build/') and fname.endswith('.DEFAULT.cpp'):
files[idx] = fname[len('build/'):-len('.DEFAULT.cpp')]
return files
def run_clang_tidy(fname):
check_call(["python3", "tools/clang_tidy.py", "-c", "build", "-x", fname,"-s"])
changes = check_output(["git", "ls-files", "-m"])
if len(changes) == 0:
return
check_call(["git", "commit","--all", "-m", f"NOLINT stubs for {fname}"])
def main():
git_files = check_output(["git", "ls-files"]).decode("ascii").split("\n")
compiled_files = get_compiled_files_list()
for idx, fname in enumerate(git_files):
if fname not in compiled_files:
continue
if fname.startswith("caffe2/contrib/aten/"):
continue
print(f"[{idx}/{len(git_files)}] Processing {fname}")
run_clang_tidy(fname)
if __name__ == "__main__":
main()
```
Pull Request resolved: https://github.com/pytorch/pytorch/pull/56892
Reviewed By: H-Huang
Differential Revision: D27991944
Pulled By: malfet
fbshipit-source-id: 5415e1eb2c1b34319a4f03024bfaa087007d7179
2021-04-28 21:09:06 +00:00
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
|
2021-01-04 19:51:28 +00:00
|
|
|
TEST(UtilsTest, AmbiguousOperatorDefaults) {
|
|
|
|
|
auto tmp = at::empty({}, at::kCPU);
|
|
|
|
|
at::_test_ambiguous_defaults(tmp);
|
|
|
|
|
at::_test_ambiguous_defaults(tmp, 1);
|
|
|
|
|
at::_test_ambiguous_defaults(tmp, 1, 1);
|
|
|
|
|
at::_test_ambiguous_defaults(tmp, 2, "2");
|
|
|
|
|
}
|