mirror of
https://github.com/zebrajr/pytorch.git
synced 2025-12-07 00:21:07 +01:00
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
79 lines
2.6 KiB
C++
79 lines
2.6 KiB
C++
#include <torch/script.h>
|
|
#include <gtest/gtest.h>
|
|
#include <test/cpp/api/support.h>
|
|
|
|
using namespace torch::autograd;
|
|
using namespace torch::test;
|
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
|
|
TEST(GradModeTest, TestRequiresGradFunctionalOp) {
|
|
torch::AutoGradMode mode(false);
|
|
for (bool requires_grad : {true, false}) {
|
|
torch::Tensor c = torch::ones({1, 2, 3}).set_requires_grad(requires_grad);
|
|
|
|
torch::Tensor func_out = c * c;
|
|
ASSERT_FALSE(func_out.requires_grad());
|
|
ASSERT_TRUE(func_out.is_leaf());
|
|
}
|
|
}
|
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
|
|
TEST(GradModeTest, TestRequiresGradInplaceOp) {
|
|
torch::AutoGradMode mode(false);
|
|
for (bool requires_grad : {true, false}) {
|
|
torch::Tensor c = torch::ones({1, 2, 3}).set_requires_grad(requires_grad);
|
|
|
|
c.mul_(2);
|
|
ASSERT_EQ(c.requires_grad(), requires_grad);
|
|
}
|
|
}
|
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
|
|
TEST(GradModeTest, TestRequiresGradViewOp) {
|
|
torch::AutoGradMode mode(false);
|
|
for (bool requires_grad : {true, false}) {
|
|
torch::Tensor c = torch::ones({1, 2, 3}).set_requires_grad(requires_grad);
|
|
|
|
torch::Tensor view_out = c.view({2, 3});
|
|
ASSERT_EQ(view_out.requires_grad(), requires_grad);
|
|
ASSERT_TRUE(view_out.is_leaf());
|
|
}
|
|
}
|
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
|
|
TEST(GradModeTest, TestRequiresGradViewOpExiting) {
|
|
for (bool requires_grad: {true, false}) {
|
|
torch::Tensor s = torch::ones({1, 2, 3}).set_requires_grad(requires_grad);
|
|
torch::Tensor a = s.clone();
|
|
torch::Tensor view_out, tmp;
|
|
|
|
{
|
|
torch::AutoGradMode mode(false);
|
|
view_out = a.view({2, 3}); // go through kernels: VariableType, InplaceOrView, CPU
|
|
assert_tensor_creation_meta(view_out, torch::autograd::CreationMeta::NO_GRAD_MODE);
|
|
ASSERT_EQ(view_out.requires_grad(), requires_grad);
|
|
ASSERT_TRUE(view_out.is_leaf());
|
|
}
|
|
|
|
tmp = view_out * view_out;
|
|
ASSERT_EQ(tmp.requires_grad(), requires_grad);
|
|
if (requires_grad) {
|
|
tmp.backward(torch::ones_like(tmp));
|
|
// TODO: this behavior is a side effect of issue #11390.
|
|
ASSERT_FALSE(view_out.grad().defined());
|
|
}
|
|
|
|
if (requires_grad) {
|
|
ASSERT_THROWS_WITH(view_out.mul_(2), // go through kernels: VariableType, InplaceOrView, CPU
|
|
"a leaf Variable that requires grad is being used in an in-place operation")
|
|
} else {
|
|
view_out.mul_(2);
|
|
}
|
|
|
|
tmp = view_out.view({2, 3});
|
|
ASSERT_EQ(tmp.requires_grad(), requires_grad);
|
|
// TODO: update when above error is fixed
|
|
// assert_tensor_creation_meta(tmp, torch::autograd::CreationMeta::NO_GRAD_MODE);
|
|
}
|
|
}
|