Disable avoid-non-const-global-variables lint check (#62008)

Summary:
As GoogleTest `TEST` macro is non-compliant with it as well as `DEFINE_DISPATCH`

All changes but the ones to `.clang-tidy` are generated using following script:
```
for i in `find . -type f -iname "*.c*" -or -iname "*.h"|xargs grep cppcoreguidelines-avoid-non-const-global-variables|cut -f1 -d:|sort|uniq`;  do sed -i "/\/\/ NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)/d" $i; done
```

Pull Request resolved: https://github.com/pytorch/pytorch/pull/62008

Reviewed By: driazati, r-barnes

Differential Revision: D29838584

Pulled By: malfet

fbshipit-source-id: 1b2f8602c945bd4ce50a9bfdd204755556e31d13
This commit is contained in:
Nikita Shulga
2021-07-22 18:00:38 -07:00
committed by Facebook GitHub Bot
parent 260198d42c
commit a9b0a921d5
1011 changed files with 1 additions and 7626 deletions

View File

@ -29,7 +29,6 @@ bool almost_equal(at::Tensor left, T right, double tolerance = 1e-4) {
ASSERT_EQ(tensor.dtype(), (type_)); \
ASSERT_TRUE(tensor.layout() == (layout_))
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
TEST(TensorTest, ToDtype) {
auto tensor = at::empty({3, 4});
REQUIRE_TENSOR_OPTIONS(at::kCPU, -1, at::kFloat, at::kStrided);
@ -53,7 +52,6 @@ TEST(TensorTest, ToDtype) {
REQUIRE_TENSOR_OPTIONS(at::kCPU, -1, at::kDouble, at::kStrided);
}
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
TEST(TensorTest, ToTensorAndTensorAttributes) {
auto tensor = at::empty({3, 4});
REQUIRE_TENSOR_OPTIONS(at::kCPU, -1, at::kFloat, at::kStrided);
@ -89,7 +87,6 @@ TEST(TensorTest, ToTensorAndTensorAttributes) {
// REQUIRE_TENSOR_OPTIONS(at::kCPU, -1, at::kFloat, at::kStrided);
// }
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
TEST(TensorTest, ToOptionsWithRequiresGrad) {
{
// Respects requires_grad
@ -127,7 +124,6 @@ TEST(TensorTest, ToOptionsWithRequiresGrad) {
}
}
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
TEST(TensorTest, ToDoesNotCopyWhenOptionsAreAllTheSame) {
{
auto tensor = at::empty({3, 4}, at::kFloat);
@ -156,7 +152,6 @@ TEST(TensorTest, ToDoesNotCopyWhenOptionsAreAllTheSame) {
}
}
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
TEST(TensorTest, AtTensorCtorScalar) {
auto tensor = at::tensor(123);
ASSERT_EQ(tensor.numel(), 1);
@ -199,7 +194,6 @@ TEST(TensorTest, AtTensorCtorScalar) {
ASSERT_TRUE(almost_equal(tensor[0], c10::complex<double>(1.5, 2.0)));
}
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
TEST(TensorTest, AtTensorCtorSingleDim) {
auto tensor = at::tensor({1, 2, 3});
ASSERT_EQ(tensor.numel(), 3);
@ -293,7 +287,6 @@ TEST(TensorTest, AtTensorCtorSingleDim) {
}
}
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
TEST(TensorTest, AtTensorCastRealToComplex) {
auto tensor = at::tensor(std::vector<double>({1.5, 2.5, 3.5}), at::kComplexDouble);
ASSERT_EQ(tensor.numel(), 3);
@ -315,7 +308,6 @@ TEST(TensorTest, AtTensorCastRealToComplex) {
ASSERT_TRUE(almost_equal(tensor[0], c10::complex<double>(1.5)));
}
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
TEST(TensorTest, AtTensorCastComplexToRealErrorChecks) {
{
ASSERT_THROWS_WITH(at::tensor(c10::complex<float>(0.1, 0.2), at::kFloat),
@ -331,7 +323,6 @@ TEST(TensorTest, AtTensorCastComplexToRealErrorChecks) {
}
}
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
TEST(TensorTest, TorchTensorCtorScalarIntegralType) {
auto tensor = torch::tensor(123);
ASSERT_EQ(tensor.numel(), 1);
@ -362,13 +353,11 @@ void test_TorchTensorCtorScalarFloatingType_expected_dtype(c10::ScalarType defau
ASSERT_TRUE(almost_equal(tensor[0], 123.456));
}
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
TEST(TensorTest, TorchTensorCtorScalarFloatingType) {
test_TorchTensorCtorScalarFloatingType_expected_dtype(/*default_dtype=*/torch::kFloat);
test_TorchTensorCtorScalarFloatingType_expected_dtype(/*default_dtype=*/torch::kDouble);
}
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
TEST(TensorTest, TorchTensorCtorScalarBoolType) {
auto tensor = torch::tensor(true);
ASSERT_EQ(tensor.numel(), 1);
@ -383,7 +372,6 @@ TEST(TensorTest, TorchTensorCtorScalarBoolType) {
ASSERT_TRUE(exactly_equal(tensor[0], true));
}
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
TEST(TensorTest, TorchTensorCtorSingleDimIntegralType) {
auto tensor = torch::tensor({1, 2, 3});
ASSERT_EQ(tensor.numel(), 3);
@ -476,13 +464,11 @@ void test_TorchTensorCtorSingleDimFloatingType_expected_dtype(c10::ScalarType de
ASSERT_TRUE(almost_equal(tensor[2], 3.125));
}
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
TEST(TensorTest, TorchTensorCtorSingleDimFloatingType) {
test_TorchTensorCtorSingleDimFloatingType_expected_dtype(/*default_dtype=*/torch::kFloat);
test_TorchTensorCtorSingleDimFloatingType_expected_dtype(/*default_dtype=*/torch::kDouble);
}
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
TEST(TensorTest, TorchTensorCtorSingleDimBoolType) {
auto tensor = torch::tensor({true, false, true});
ASSERT_EQ(tensor.numel(), 3);
@ -501,7 +487,6 @@ TEST(TensorTest, TorchTensorCtorSingleDimBoolType) {
ASSERT_TRUE(exactly_equal(tensor[2], true));
}
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
TEST(TensorTest, TorchTensorCtorMultiDimIntegralType) {
{
auto tensor = torch::tensor({{1, 2}});
@ -572,13 +557,11 @@ void test_TorchTensorCtorMultiDimFloatingType_expected_dtype(c10::ScalarType def
}
}
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
TEST(TensorTest, TorchTensorCtorMultiDimFloatingType) {
test_TorchTensorCtorMultiDimFloatingType_expected_dtype(/*default_dtype=*/torch::kFloat);
test_TorchTensorCtorMultiDimFloatingType_expected_dtype(/*default_dtype=*/torch::kDouble);
}
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
TEST(TensorTest, TorchTensorCtorMultiDimBoolType) {
{
auto tensor = torch::tensor({{true, false}});
@ -602,7 +585,6 @@ TEST(TensorTest, TorchTensorCtorMultiDimBoolType) {
}
}
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
TEST(TensorTest, TorchTensorCtorMultiDimWithOptions) {
{
auto tensor = torch::tensor({{1, 2}}, torch::dtype(torch::kInt));
@ -620,7 +602,6 @@ TEST(TensorTest, TorchTensorCtorMultiDimWithOptions) {
}
}
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
TEST(TensorTest, TorchTensorCtorMultiDimErrorChecks) {
{
ASSERT_THROWS_WITH(torch::tensor({{{2, 3, 4}, {{5, 6}, {7}}}}),
@ -644,7 +625,6 @@ TEST(TensorTest, TorchTensorCtorMultiDimErrorChecks) {
}
}
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
TEST(TensorTest, TorchTensorCastRealToComplex) {
auto tensor = torch::tensor(std::vector<double>({1.5, 2.5, 3.5}), torch::kComplexDouble);
ASSERT_EQ(tensor.numel(), 3);
@ -666,7 +646,6 @@ TEST(TensorTest, TorchTensorCastRealToComplex) {
ASSERT_TRUE(almost_equal(tensor, c10::complex<double>(1.5)));
}
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
TEST(TensorTest, TorchTensorCastComplexToRealErrorChecks) {
{
ASSERT_THROWS_WITH(torch::tensor(c10::complex<float>(0.1, 0.2), torch::kFloat),
@ -697,7 +676,6 @@ void test_TorchTensorCtorMultiDim_CUDA_expected_dtype(c10::ScalarType default_dt
ASSERT_FALSE(tensor.requires_grad());
}
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
TEST(TensorTest, TorchTensorCtorMultiDim_CUDA) {
test_TorchTensorCtorMultiDim_CUDA_expected_dtype(/*default_dtype=*/torch::kFloat);
test_TorchTensorCtorMultiDim_CUDA_expected_dtype(/*default_dtype=*/torch::kDouble);
@ -756,7 +734,6 @@ void test_TorchTensorCtorZeroSizedDim_expected_dtype(c10::ScalarType default_dty
}
}
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
TEST(TensorTest, TorchTensorCtorZeroSizedDim) {
test_TorchTensorCtorZeroSizedDim_expected_dtype(/*default_dtype=*/torch::kFloat);
test_TorchTensorCtorZeroSizedDim_expected_dtype(/*default_dtype=*/torch::kDouble);
@ -771,7 +748,6 @@ void test_TorchTensorCtorWithoutSpecifyingDtype_expected_dtype(c10::ScalarType d
ASSERT_EQ(torch::tensor({{1., 2., 3.}}, torch::TensorOptions()).dtype(), default_dtype);
}
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
TEST(TensorTest, TorchTensorCtorWithoutSpecifyingDtype) {
ASSERT_EQ(torch::tensor({1, 2, 3}).dtype(), torch::kLong);
ASSERT_EQ(torch::tensor({{1, 2, 3}}).dtype(), torch::kLong);
@ -798,7 +774,6 @@ void test_TorchTensorCtorWithNonDtypeOptions_expected_dtype(c10::ScalarType defa
ASSERT_EQ(torch::tensor(std::vector<float>({1.f, 2.f, 3.f}), torch::TensorOptions()).dtype(), default_dtype);
}
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
TEST(TensorTest, TorchTensorCtorWithNonDtypeOptions) {
test_TorchTensorCtorWithNonDtypeOptions_expected_dtype(/*default_dtype=*/torch::kFloat);
test_TorchTensorCtorWithNonDtypeOptions_expected_dtype(/*default_dtype=*/torch::kDouble);
@ -810,7 +785,6 @@ void test_Arange_expected_dtype(c10::ScalarType default_dtype) {
ASSERT_EQ(torch::arange(0., 5).dtype(), default_dtype);
}
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
TEST(TensorTest, Arange) {
{
auto x = torch::arange(0, 5);
@ -820,7 +794,6 @@ TEST(TensorTest, Arange) {
test_Arange_expected_dtype(torch::kDouble);
}
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
TEST(TensorTest, PrettyPrintTensorDataContainer) {
{
ASSERT_EQ(
@ -869,7 +842,6 @@ TEST(TensorTest, PrettyPrintTensorDataContainer) {
}
}
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
TEST(TensorTest, TensorDataContainerCallingAccessorOfWrongType) {
{
ASSERT_THROWS_WITH(
@ -897,7 +869,6 @@ TEST(TensorTest, TensorDataContainerCallingAccessorOfWrongType) {
}
}
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
TEST(TensorTest, FromBlob) {
std::vector<double> v = {1.0, 2.0, 3.0};
auto tensor = torch::from_blob(
@ -912,7 +883,6 @@ TEST(TensorTest, FromBlob) {
ASSERT_EQ(tensor.storage().data_ptr().get_context(), nullptr);
}
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
TEST(TensorTest, FromBlobUsesDeleter) {
bool called = false;
{
@ -926,7 +896,6 @@ TEST(TensorTest, FromBlobUsesDeleter) {
ASSERT_TRUE(called);
}
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
TEST(TensorTest, FromBlobWithStrides) {
// clang-format off
std::vector<int32_t> v = {
@ -952,7 +921,6 @@ TEST(TensorTest, FromBlobWithStrides) {
}
}
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
TEST(TensorTest, Item) {
{
torch::Tensor tensor = torch::tensor(3.14);
@ -966,7 +934,6 @@ TEST(TensorTest, Item) {
}
}
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
TEST(TensorTest, Item_CUDA) {
{
torch::Tensor tensor = torch::tensor(3.14, torch::kCUDA);
@ -980,7 +947,6 @@ TEST(TensorTest, Item_CUDA) {
}
}
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
TEST(TensorTest, DataPtr) {
auto tensor = at::empty({3, 4}, at::kFloat);
auto tensor_not_copy = tensor.to(tensor.options());
@ -988,13 +954,11 @@ TEST(TensorTest, DataPtr) {
ASSERT_EQ(tensor_not_copy.data_ptr(), tensor.data_ptr());
}
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
TEST(TensorTest, Data) {
const auto tensor = torch::rand({3, 3});
ASSERT_TRUE(torch::equal(tensor, tensor.data()));
}
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
TEST(TensorTest, BackwardAndGrad) {
auto x = torch::tensor({5}, torch::dtype(torch::kFloat).requires_grad(true));
auto y = x * x;
@ -1002,7 +966,6 @@ TEST(TensorTest, BackwardAndGrad) {
ASSERT_EQ(x.grad().item<float>(), 10.0);
}
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
TEST(TensorTest, BackwardCreatesOnesGrad) {
const auto x = torch::tensor({5}, torch::dtype(torch::kFloat).requires_grad(true));
x.backward();
@ -1010,7 +973,6 @@ TEST(TensorTest, BackwardCreatesOnesGrad) {
torch::ones_like(x)));
}
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
TEST(TensorTest, BackwardNonScalarOutputs) {
auto x = torch::randn({5, 5}, torch::requires_grad());
auto y = x * x;
@ -1018,7 +980,6 @@ TEST(TensorTest, BackwardNonScalarOutputs) {
"grad can be implicitly created only for scalar outputs");
}
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
TEST(TensorTest, IsLeaf) {
auto x = torch::tensor({5}, torch::dtype(torch::kFloat).requires_grad(true));
auto y = x * x;
@ -1026,7 +987,6 @@ TEST(TensorTest, IsLeaf) {
ASSERT_FALSE(y.is_leaf());
}
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
TEST(TensorTest, OutputNr) {
auto x = torch::tensor({5}, torch::dtype(torch::kFloat).requires_grad(true));
auto y = x * x;
@ -1034,7 +994,6 @@ TEST(TensorTest, OutputNr) {
ASSERT_EQ(y.output_nr(), 0);
}
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
TEST(TensorTest, Version) {
auto x = torch::ones(3);
ASSERT_EQ(x._version(), 0);
@ -1044,7 +1003,6 @@ TEST(TensorTest, Version) {
ASSERT_EQ(x._version(), 2);
}
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
TEST(TensorTest, Detach) {
auto x = torch::tensor({5}, torch::dtype(torch::kFloat).requires_grad(true));
auto y = x * x;
@ -1054,7 +1012,6 @@ TEST(TensorTest, Detach) {
ASSERT_FALSE(y_detached.requires_grad());
}
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
TEST(TensorTest, DetachInplace) {
auto x = torch::tensor({5}, torch::dtype(torch::kFloat).requires_grad(true));
auto y = x * x;
@ -1065,7 +1022,6 @@ TEST(TensorTest, DetachInplace) {
ASSERT_FALSE(y_detached.requires_grad());
}
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
TEST(TensorTest, SetData) {
auto x = torch::randn({5});
auto y = torch::randn({5});
@ -1077,7 +1033,6 @@ TEST(TensorTest, SetData) {
ASSERT_EQ(x.data_ptr<float>(), y.data_ptr<float>());
}
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
TEST(TensorTest, RequiresGradInplace) {
auto x = torch::tensor({5.0});
x.requires_grad_(true);
@ -1095,7 +1050,6 @@ TEST(TensorTest, RequiresGradInplace) {
"Only Tensors of floating point and complex dtype can require gradients");
}
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
TEST(TensorTest, StdDimension) {
// Test that std(0) doesn't select the std(unbiased=False) overload (gh-40287)
auto x = torch::randn({4, 3});
@ -1118,7 +1072,6 @@ TEST(TensorTest, StdDimension) {
ASSERT_EQ(std::get<0>(torch::std_mean(x, 0, /*unbiased=*/true)).numel(), 3);
}
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
TEST(TensorTest, ReshapeAlias) {
// Tests the behavior of the _reshape_alias private operator so
// that it matches the behavior of as_strided and view.