mirror of
https://github.com/zebrajr/pytorch.git
synced 2025-12-07 12:21:27 +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
118 lines
3.7 KiB
C++
118 lines
3.7 KiB
C++
#include <Python.h>
|
|
#include <torch/csrc/autograd/functions/accumulate_grad.h>
|
|
#include <torch/csrc/autograd/functions/basic_ops.h>
|
|
#include <torch/csrc/autograd/functions/tensor.h>
|
|
#include <torch/csrc/autograd/functions/pybind.h>
|
|
#include <torch/csrc/autograd/python_cpp_function.h>
|
|
#include <torch/csrc/autograd/generated/python_functions.h>
|
|
#include <torch/csrc/jit/python_tracer.h>
|
|
#include <torch/csrc/utils/pybind.h>
|
|
#include <torch/csrc/utils/tuple_parser.h>
|
|
|
|
using namespace torch::autograd;
|
|
using torch::TupleParser;
|
|
|
|
struct DelayedErrorCtor {
|
|
DelayedError* operator()(PyObject* args) {
|
|
std::string msg;
|
|
int num_inputs;
|
|
|
|
TupleParser parser(args, 2);
|
|
parser.parse(msg, "msg");
|
|
parser.parse(num_inputs, "num_inputs");
|
|
|
|
return new DelayedError(msg, num_inputs);
|
|
}
|
|
};
|
|
|
|
struct NoCtor {
|
|
Function* operator()(PyObject* args) {
|
|
throw std::runtime_error("Cannot construct");
|
|
}
|
|
};
|
|
|
|
template<typename C, typename T>
|
|
static void addClass(PyObject* module, PyTypeObject& type, const char* name,
|
|
PyGetSetDef* function_properties=nullptr, PyMethodDef* function_methods=nullptr)
|
|
{
|
|
createForwardFunctionPyTypeObject<T>(type, name, function_properties, function_methods);
|
|
Py_INCREF(&type);
|
|
PyModule_AddObject(module, name, (PyObject*)&type);
|
|
registerCppFunction(typeid(C), &type);
|
|
}
|
|
|
|
template<typename T, typename ValueT, typename ParamsT, ValueT ParamsT::*ptr,
|
|
typename ConvertArgT, PyObject* (*Convert)(ConvertArgT)>
|
|
PyObject* getTupleAttr(PyObject* obj, void* _unused)
|
|
{
|
|
HANDLE_TH_ERRORS
|
|
THPCppFunction* self = (THPCppFunction*)obj;
|
|
auto& arr = ((T*)(self->cdata.get()))->*ptr;
|
|
auto num_elems = arr.size();
|
|
THPObjectPtr py_tuple(PyTuple_New(num_elems));
|
|
if (!py_tuple) return nullptr;
|
|
for (size_t i = 0; i < num_elems; ++i) {
|
|
PyTuple_SET_ITEM(py_tuple.get(), i, Convert(arr[i]));
|
|
}
|
|
return py_tuple.release();
|
|
END_HANDLE_TH_ERRORS
|
|
}
|
|
|
|
template<typename T, typename ValueT, typename ParamsT, ValueT ParamsT::*ptr,
|
|
typename ConvertArgT, PyObject* (*Convert)(ConvertArgT)>
|
|
PyObject* getValueAttr(PyObject* obj, void* _unused)
|
|
{
|
|
HANDLE_TH_ERRORS
|
|
THPCppFunction* self = (THPCppFunction*)obj;
|
|
auto& val = ((T*)(self->cdata.get()))->*ptr;
|
|
return Convert(val);
|
|
END_HANDLE_TH_ERRORS
|
|
}
|
|
|
|
static PyObject* accumulateGradVar(PyObject *_self, void* _unused)
|
|
{
|
|
THPCppFunction* self = (THPCppFunction*)_self;
|
|
auto grad_acc = (AccumulateGrad*)self->cdata.get();
|
|
return THPVariable_Wrap(grad_acc->variable);
|
|
}
|
|
|
|
static struct PyGetSetDef accumulate_grad_properties[] = {
|
|
THP_FUNCTION_DEFAULT_PROPERTIES,
|
|
{(char*)"variable", accumulateGradVar, nullptr, nullptr, nullptr},
|
|
{nullptr}
|
|
};
|
|
|
|
void THPAutograd_initFunctions()
|
|
{
|
|
THPObjectPtr module(PyModule_New("torch._C._functions"));
|
|
if (!module) throw python_error();
|
|
|
|
static PyTypeObject AccumulateGradClass;
|
|
addClass<AccumulateGrad, NoCtor>(module, AccumulateGradClass, "AccumulateGrad", accumulate_grad_properties);
|
|
|
|
static PyTypeObject ErrorClass;
|
|
addClass<Error, NoCtor>(module, ErrorClass, "Error");
|
|
|
|
static PyTypeObject NotImplementedClass;
|
|
addClass<NotImplemented, NoCtor>(module, NotImplementedClass, "NotImplemented");
|
|
|
|
static PyTypeObject DelayedErrorClass;
|
|
addClass<DelayedError, DelayedErrorCtor>(module, DelayedErrorClass, "DelayedError");
|
|
|
|
static PyTypeObject CopyBackwardsClass;
|
|
addClass<CopyBackwards, NoCtor>(module, CopyBackwardsClass, "CopyBackwards");
|
|
|
|
static PyTypeObject CopySlicesClass;
|
|
addClass<CopySlices, NoCtor>(module, CopySlicesClass, "CopySlices");
|
|
|
|
generated::initialize_autogenerated_functions();
|
|
|
|
auto c_module = THPObjectPtr(PyImport_ImportModule("torch._C"));
|
|
if (!c_module) throw python_error();
|
|
|
|
Py_INCREF(module);
|
|
if (PyModule_AddObject(c_module, "_functions", module) < 0) {
|
|
throw python_error();
|
|
}
|
|
}
|