mirror of
https://github.com/zebrajr/pytorch.git
synced 2025-12-07 00:21:07 +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
67 lines
2.4 KiB
C++
67 lines
2.4 KiB
C++
#pragma once
|
|
|
|
#include <torch/csrc/python_headers.h>
|
|
#include <memory>
|
|
#include <typeinfo>
|
|
|
|
#include <torch/csrc/autograd/function.h>
|
|
#include <torch/csrc/utils/object_ptr.h>
|
|
#include <torch/csrc/Exceptions.h>
|
|
|
|
namespace torch { namespace autograd {
|
|
|
|
struct THPCppFunction {
|
|
PyObject_HEAD
|
|
std::shared_ptr<Function> cdata;
|
|
};
|
|
|
|
template<typename Ctor>
|
|
PyObject* CppFunction_pynew(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
|
{
|
|
THPObjectPtr obj(type->tp_alloc(type, 0));
|
|
if (!obj) return nullptr;
|
|
THPCppFunction* f = (THPCppFunction*)obj.get();
|
|
HANDLE_TH_ERRORS
|
|
new (&f->cdata) std::shared_ptr<Function>(Ctor()(args));
|
|
END_HANDLE_TH_ERRORS
|
|
if (!f->cdata) {
|
|
return nullptr;
|
|
}
|
|
return obj.release();
|
|
}
|
|
|
|
#define THP_FUNCTION_DEFAULT_METHODS \
|
|
{(char*)"_register_hook_dict", (PyCFunction)THPCppFunction_register_hook_dict, METH_O, nullptr}, \
|
|
{(char*)"register_hook", (PyCFunction)THPCppFunction_register_hook, METH_O, nullptr}, \
|
|
{(char*)"name", (PyCFunction)THPCppFunction_name, METH_NOARGS, nullptr}
|
|
|
|
#define THP_FUNCTION_DEFAULT_PROPERTIES \
|
|
{(char*)"next_functions", (getter)THPCppFunction_next_functions, nullptr, nullptr, nullptr}, \
|
|
{(char*)"requires_grad", (getter)THPCppFunction_requires_grad, nullptr, nullptr, nullptr}, \
|
|
{(char*)"metadata", (getter)THPCppFunction_metadata, nullptr, nullptr, nullptr}
|
|
|
|
PyObject* THPCppFunction_next_functions(THPCppFunction* self, PyObject* hook);
|
|
PyObject* THPCppFunction_metadata(THPCppFunction *self, void *_unused);
|
|
PyObject* THPCppFunction_requires_grad(THPCppFunction* self);
|
|
PyObject* THPCppFunction_register_hook_dict(PyObject* self, PyObject* _var);
|
|
PyObject* THPCppFunction_register_hook(PyObject* self, PyObject* hook);
|
|
PyObject* THPCppFunction_name(PyObject* self);
|
|
|
|
PyTypeObject* _initFunctionPyTypeObject(PyTypeObject& type, const char* name,
|
|
PyGetSetDef* function_properties, PyMethodDef* function_methods);
|
|
|
|
PyObject* registerFunctionHook(Function& fn, PyObject* hook);
|
|
|
|
template<typename Ctor>
|
|
PyTypeObject* createForwardFunctionPyTypeObject(PyTypeObject& type, const char* name,
|
|
PyGetSetDef* function_properties=nullptr, PyMethodDef* function_methods=nullptr)
|
|
{
|
|
type.tp_new = &CppFunction_pynew<Ctor>;
|
|
return _initFunctionPyTypeObject(type, name, function_properties, function_methods);
|
|
}
|
|
|
|
void registerCppFunction(const std::type_info& type, PyTypeObject* pytype);
|
|
PyObject* functionToPyObject(std::shared_ptr<Function> cdata);
|
|
|
|
}} // namespace torch::autograd
|