pytorch/test/cpp/jit/torch_python_test.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
2.2 KiB
C++

#include <ATen/core/ivalue.h>
#include <c10/util/Exception.h>
#include <torch/csrc/WindowsTorchApiMacro.h>
#include <torch/csrc/jit/api/module.h>
#include <torch/script.h>
namespace torch {
namespace jit {
#ifdef _MSC_VER
#define JIT_TEST_API
#else
#define JIT_TEST_API TORCH_API
#endif
namespace {
bool isSandcastle() {
return (
(std::getenv("SANDCASTLE")) ||
(std::getenv("TW_JOB_USER") &&
std::string(std::getenv("TW_JOB_USER")) == "sandcastle"));
}
void testEvalModeForLoadedModule() {
if (isSandcastle())
return; // The module file to load is not generated in Sandcastle
std::string module_path = "dropout_model.pt";
torch::jit::Module module = torch::jit::load(module_path);
AT_ASSERT(module.attr("dropout").toModule().is_training());
module.eval();
AT_ASSERT(!module.attr("dropout").toModule().is_training());
module.train();
AT_ASSERT(module.attr("dropout").toModule().is_training());
}
void testSerializationInterop() {
if (isSandcastle()) {
// The module file to load is not generated in Sandcastle
return;
}
// This should be generated by `test/cpp/jit/tests_setup.py`
std::ifstream input_stream("ivalue.pt");
std::vector<char> input;
input.insert(
input.begin(),
std::istream_iterator<char>(input_stream),
std::istream_iterator<char>());
IValue ivalue = pickle_load(input);
auto elements = ivalue.toTuple()->elements();
auto ones = torch::ones({2, 2});
AT_ASSERT(ones.equal(elements.at(0).toTensor()));
// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers)
auto twos = torch::ones({3, 5}) * 2;
AT_ASSERT(twos.equal(elements.at(1).toTensor()));
}
void testTorchSaveError() {
if (isSandcastle()) {
// The file to load is not generated in Sandcastle
return;
}
// This should be generated by `test/cpp/jit/tests_setup.py`
bool passed = true;
try {
torch::jit::load("eager_value.pt");
passed = false;
} catch (const std::exception& c) {
}
// Ensure torch::jit::load did not run
AT_ASSERT(passed);
}
} // namespace
JIT_TEST_API void runJITCPPTests() {
// TODO: this test never ran before and is broken.
// testSerializationInterop();
testEvalModeForLoadedModule();
testTorchSaveError();
}
} // namespace jit
} // namespace torch