pytorch/test/cpp/api/tensor_options_cuda.cpp
Nikita Shulga 4cb534f92e 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 14:10:25 -07:00

87 lines
3.3 KiB
C++

#include <gtest/gtest.h>
#include <torch/torch.h>
#include <torch/cuda.h>
// NB: This file is compiled even in CPU build (for some reason), so
// make sure you don't include any CUDA only headers.
using namespace at;
// TODO: This might be generally helpful aliases elsewhere.
at::Device CPUDevice() {
return at::Device(at::kCPU);
}
at::Device CUDADevice(DeviceIndex index) {
return at::Device(at::kCUDA, index);
}
// A macro so we don't lose location information when an assertion fails.
#define REQUIRE_OPTIONS(device_, index_, type_, layout_) \
ASSERT_EQ(options.device().type(), Device((device_), (index_)).type()); \
ASSERT_TRUE( \
options.device().index() == Device((device_), (index_)).index()); \
ASSERT_EQ(typeMetaToScalarType(options.dtype()), (type_)); \
ASSERT_TRUE(options.layout() == (layout_))
#define REQUIRE_TENSOR_OPTIONS(device_, index_, type_, layout_) \
ASSERT_EQ(tensor.device().type(), Device((device_), (index_)).type()); \
ASSERT_EQ(tensor.device().index(), Device((device_), (index_)).index()); \
ASSERT_EQ(tensor.scalar_type(), (type_)); \
ASSERT_TRUE(tensor.options().layout() == (layout_))
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
TEST(TensorOptionsTest, ConstructsWellFromCUDATypes_CUDA) {
auto options = CUDA(kFloat).options();
REQUIRE_OPTIONS(kCUDA, -1, kFloat, kStrided);
options = CUDA(kInt).options();
REQUIRE_OPTIONS(kCUDA, -1, kInt, kStrided);
options = getDeprecatedTypeProperties(Backend::SparseCUDA, kFloat).options();
REQUIRE_OPTIONS(kCUDA, -1, kFloat, kSparse);
options = getDeprecatedTypeProperties(Backend::SparseCUDA, kByte).options();
REQUIRE_OPTIONS(kCUDA, -1, kByte, kSparse);
// NOLINTNEXTLINE(bugprone-argument-comment,cppcoreguidelines-avoid-magic-numbers)
options = CUDA(kFloat).options(/*device=*/5);
REQUIRE_OPTIONS(kCUDA, 5, kFloat, kStrided);
options =
// NOLINTNEXTLINE(bugprone-argument-comment,cppcoreguidelines-avoid-magic-numbers)
getDeprecatedTypeProperties(Backend::SparseCUDA, kFloat).options(/*device=*/5);
REQUIRE_OPTIONS(kCUDA, 5, kFloat, kSparse);
}
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
TEST(TensorOptionsTest, ConstructsWellFromCUDATensors_MultiCUDA) {
// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers)
auto options = empty(5, device(kCUDA).dtype(kDouble)).options();
REQUIRE_OPTIONS(kCUDA, 0, kDouble, kStrided);
// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers)
options = empty(5, getDeprecatedTypeProperties(Backend::SparseCUDA, kByte)).options();
REQUIRE_OPTIONS(kCUDA, 0, kByte, kSparse);
if (torch::cuda::device_count() > 1) {
Tensor tensor;
{
DeviceGuard guard(CUDADevice(1));
// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers)
tensor = empty(5, device(kCUDA));
}
options = tensor.options();
REQUIRE_OPTIONS(kCUDA, 1, kFloat, kStrided);
{
DeviceGuard guard(CUDADevice(1));
// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers)
tensor = empty(5, device(kCUDA).layout(kSparse));
}
options = tensor.options();
REQUIRE_OPTIONS(kCUDA, 1, kFloat, kSparse);
}
}