mirror of
https://github.com/zebrajr/pytorch.git
synced 2025-12-08 07:39:33 +01:00
Summary:
Anywhere we used #include "foo.h", we now say #include <foo.h>
Paths are adjusted to be rooted out of aten/src, torch/lib, or
the root level directory.
I modified CMakeLists.txt by hand to remove TH and THC from
the include paths.
I used the following script to do the canonicalization:
```
import subprocess
import re
import os.path
files = subprocess.check_output(['git', 'ls-files']).decode('utf-8').rstrip().split('\n')
for fn in files:
if not any(fn.endswith(suff) for suff in ['.cu', '.cpp', '.in', '.h', '.hpp', '.cu', '.cuh', '.cc']):
continue
if not any(fn.startswith(pref) for pref in ["aten/", "torch/"]):
continue
with open(fn, 'r') as f:
c = f.read()
def fmt(p):
return "#include <{}>".format(p)
def repl(m):
p = m.group(1)
if p in ["dlfcn.h", "unistd.h", "nvrtc.h", "cuda.h", "cuda_runtime.h", "cstdint", "cudnn.h", "Python.h", "cusparse.h", "cuda_runtime_api.h", "cuda_fp16.h", "cublas_v2.h", "stdint.h", "curand_kernel.h"]:
return fmt(p)
if any(p.startswith(pref) for pref in ["torch/csrc", "c10/", "ATen/", "caffe2/", "TH/", "THC/", "Eigen/", "gtest/", "zdl/", "gloo/", "onnx/", "miopen/"]):
return fmt(p)
for root in ["aten/src", "torch/lib", ""]:
for bad_root in [os.path.dirname(fn), "aten/src/TH", "aten/src/THC", "torch/csrc"]:
new_p = os.path.relpath(os.path.join(bad_root, p), root)
if not new_p.startswith("../") and (os.path.exists(os.path.join(root, new_p)) or os.path.exists(os.path.join(root, new_p + ".in"))):
return fmt(new_p)
print("ERROR: ", fn, p)
return m.group(0)
new_c = re.sub(r'#include "([^"]+)"', repl, c)
if new_c != c:
print(fn)
with open(fn, 'w') as f:
f.write(new_c)
```
Signed-off-by: Edward Z. Yang <ezyang@fb.com>
Pull Request resolved: https://github.com/pytorch/pytorch/pull/14849
Reviewed By: dzhulgakov
Differential Revision: D13363445
Pulled By: ezyang
fbshipit-source-id: 52361f878a672785f9306c9e9ab2513128092b68
108 lines
2.9 KiB
C++
108 lines
2.9 KiB
C++
#include <torch/csrc/utils/tuple_parser.h>
|
|
|
|
|
|
#include <torch/csrc/DynamicTypes.h>
|
|
#include <torch/csrc/autograd/python_variable.h>
|
|
#include <torch/csrc/utils/python_strings.h>
|
|
#include <torch/csrc/utils/python_numbers.h>
|
|
|
|
#include <string>
|
|
#include <stdexcept>
|
|
#include <vector>
|
|
|
|
namespace torch {
|
|
|
|
TupleParser::TupleParser(PyObject* args, int num_args) : args(args), idx(0) {
|
|
int size = (int) PyTuple_GET_SIZE(args);
|
|
if (num_args >= 0 && size != num_args) {
|
|
std::string msg("missing required arguments (expected ");
|
|
msg += std::to_string(num_args) + " got " + std::to_string(size) + ")";
|
|
throw std::runtime_error(msg);
|
|
}
|
|
}
|
|
|
|
auto TupleParser::parse(bool& x, const std::string& param_name) -> void {
|
|
PyObject* obj = next_arg();
|
|
if (!PyBool_Check(obj)) {
|
|
throw invalid_type("bool", param_name);
|
|
}
|
|
x = (obj == Py_True);
|
|
}
|
|
|
|
auto TupleParser::parse(int& x, const std::string& param_name) -> void {
|
|
PyObject* obj = next_arg();
|
|
if (!THPUtils_checkLong(obj)) {
|
|
throw invalid_type("int", param_name);
|
|
}
|
|
x = THPUtils_unpackLong(obj);
|
|
}
|
|
|
|
auto TupleParser::parse(double& x, const std::string& param_name) -> void {
|
|
PyObject* obj = next_arg();
|
|
if (!THPUtils_checkDouble(obj)) {
|
|
throw invalid_type("float", param_name);
|
|
}
|
|
x = THPUtils_unpackDouble(obj);
|
|
}
|
|
|
|
auto TupleParser::parse(std::vector<int>& x, const std::string& param_name) -> void {
|
|
PyObject* obj = next_arg();
|
|
if (!PyTuple_Check(obj)) {
|
|
throw invalid_type("tuple of int", param_name);
|
|
}
|
|
int size = PyTuple_GET_SIZE(obj);
|
|
x.resize(size);
|
|
for (int i = 0; i < size; ++i) {
|
|
PyObject* item = PyTuple_GET_ITEM(obj, i);
|
|
if (!THPUtils_checkLong(item)) {
|
|
throw invalid_type("tuple of int", param_name);
|
|
}
|
|
x[i] = THPUtils_unpackLong(item);
|
|
}
|
|
}
|
|
|
|
auto TupleParser::parse(std::string& x, const std::string& param_name) -> void {
|
|
PyObject* obj = next_arg();
|
|
if (!THPUtils_checkString(obj)) {
|
|
throw invalid_type("bytes/str", param_name);
|
|
}
|
|
x = THPUtils_unpackString(obj);
|
|
}
|
|
|
|
auto TupleParser::next_arg() -> PyObject* {
|
|
if (idx >= PyTuple_GET_SIZE(args)) {
|
|
throw std::runtime_error("out of range");
|
|
}
|
|
return PyTuple_GET_ITEM(args, idx++);
|
|
}
|
|
|
|
auto TupleParser::invalid_type(const std::string& expected, const std::string& param_name) -> std::runtime_error {
|
|
std::string msg("argument ");
|
|
msg += std::to_string(idx - 1);
|
|
msg += " (";
|
|
msg += param_name;
|
|
msg += ") ";
|
|
msg += "must be ";
|
|
msg += expected;
|
|
|
|
PyObject* obj = PyTuple_GET_ITEM(args, idx -1);
|
|
if (PyTuple_Check(obj)){
|
|
msg += " but got tuple of (";
|
|
int size = PyTuple_GET_SIZE(obj);
|
|
for (int i = 0; i < size; ++i) {
|
|
msg += Py_TYPE(PyTuple_GET_ITEM(obj, i))->tp_name;
|
|
if (i != size - 1){
|
|
msg += ", ";
|
|
}
|
|
}
|
|
msg += ")";
|
|
}
|
|
else{
|
|
msg += ", not ";
|
|
msg += Py_TYPE(PyTuple_GET_ITEM(args, idx - 1))->tp_name;
|
|
}
|
|
return std::runtime_error(msg);
|
|
}
|
|
|
|
} // namespace torch
|