pytorch/test/cpp/tensorexpr/test_ir_printer.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

111 lines
3.1 KiB
C++

#include <gtest/gtest.h>
#include <stdexcept>
#include "test/cpp/tensorexpr/test_base.h"
#include <torch/csrc/jit/tensorexpr/expr.h>
#include <torch/csrc/jit/tensorexpr/ir.h>
#include <torch/csrc/jit/tensorexpr/ir_printer.h>
#include <torch/csrc/jit/tensorexpr/loopnest.h>
#include <torch/csrc/jit/tensorexpr/tensor.h>
#include <torch/csrc/jit/testing/file_check.h>
#include <sstream>
namespace torch {
namespace jit {
using namespace torch::jit::tensorexpr;
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
TEST(IRPrinter, BasicValueTest) {
KernelScope kernel_scope;
ExprHandle a = IntImm::make(2), b = IntImm::make(3);
ExprHandle c = Add::make(a, b);
std::stringstream ss;
ss << c;
ASSERT_EQ(ss.str(), "2 + 3");
}
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
TEST(IRPrinter, BasicValueTest02) {
KernelScope kernel_scope;
// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers)
ExprHandle a(2.0f);
// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers)
ExprHandle b(3.0f);
// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers)
ExprHandle c(4.0f);
// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers)
ExprHandle d(5.0f);
ExprHandle f = (a + b) - (c + d);
std::stringstream ss;
ss << f;
ASSERT_EQ(ss.str(), "(2.f + 3.f) - (4.f + 5.f)");
}
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
TEST(IRPrinter, CastTest) {
KernelScope kernel_scope;
VarHandle x("x", kHalf);
VarHandle y("y", kFloat);
// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers)
ExprHandle body = ExprHandle(2.f) +
// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers)
(Cast::make(kFloat, x) * ExprHandle(3.f) + ExprHandle(4.f) * y);
std::stringstream ss;
ss << body;
ASSERT_EQ(ss.str(), "2.f + (float(x) * 3.f + 4.f * y)");
}
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
TEST(IRPrinter, FunctionName) {
KernelScope kernel_scope;
int M = 4;
// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers)
int N = 20;
Tensor* producer = Compute(
"producer",
{{M, "m"}, {N, "n"}},
[&](const ExprHandle& m, const ExprHandle& n) { return m * n; });
Tensor* chunk_0 = Compute(
"chunk",
{{M, "m"}, {N / 2, "n"}},
[&](const ExprHandle& m, const ExprHandle& n) {
return producer->load(m, n);
});
Tensor* chunk_1 = Compute(
"chunk",
{{M, "m"}, {N / 2, "n"}},
[&](const ExprHandle& m, const ExprHandle& n) {
return producer->load(m, n + ExprHandle(N / 2));
});
Tensor* consumer = Compute(
"consumer",
{{M, "i"}, {N / 2, "j"}},
[&](const ExprHandle& i, const ExprHandle& j) {
return i * chunk_1->load(i, j);
});
LoopNest l({chunk_0, chunk_1, consumer});
auto* body = l.root_stmt();
std::stringstream ss;
ss << *body;
const std::string& verification_pattern =
R"IR(
# CHECK: for (int i
# CHECK: for (int j
# CHECK: consumer[i, j] = i * (chunk_1[i, j])IR";
torch::jit::testing::FileCheck().run(verification_pattern, ss.str());
}
} // namespace jit
} // namespace torch