diff --git a/BUILD.bazel b/BUILD.bazel index 32608b42b96..703abac16c7 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -542,6 +542,7 @@ header_template_rule( cc_library( name = "aten_headers", hdrs = [ + "torch/csrc/Export.h", "torch/csrc/WindowsTorchApiMacro.h", "torch/csrc/jit/frontend/function_schema_parser.h", ] + glob([ diff --git a/tools/autograd/templates/Functions.h b/tools/autograd/templates/Functions.h index 0540bb65b33..641488909e3 100644 --- a/tools/autograd/templates/Functions.h +++ b/tools/autograd/templates/Functions.h @@ -6,11 +6,10 @@ #include #include -#include "torch/csrc/THP_export.h" #include "torch/csrc/autograd/function.h" #include "torch/csrc/autograd/variable.h" #include "torch/csrc/autograd/saved_variable.h" -#include +#include namespace torch { namespace autograd { namespace generated { diff --git a/tools/build_variables.bzl b/tools/build_variables.bzl index a8add3034cd..6239c6e63dc 100644 --- a/tools/build_variables.bzl +++ b/tools/build_variables.bzl @@ -71,6 +71,7 @@ def libtorch_generated_sources(gencode_pattern): # copied from https://github.com/pytorch/pytorch/blob/f99a693cd9ff7a9b5fdc71357dac66b8192786d3/aten/src/ATen/core/CMakeLists.txt jit_core_headers = [ "torch/csrc/utils/memory.h", + "torch/csrc/Export.h", "torch/csrc/WindowsTorchApiMacro.h", "torch/csrc/jit/frontend/source_range.h", "torch/csrc/jit/serialization/callstack_debug_info_serialization.h", diff --git a/torch/csrc/Exceptions.h b/torch/csrc/Exceptions.h index 36472a7eafe..f6cf5f341b0 100644 --- a/torch/csrc/Exceptions.h +++ b/torch/csrc/Exceptions.h @@ -9,10 +9,9 @@ #include #include -#include +#include #include #include -#include #include #include @@ -259,9 +258,9 @@ bool THPException_init(PyObject *module); namespace torch { -THP_CLASS std::string processErrorMsg(std::string str); +TORCH_PYTHON_API std::string processErrorMsg(std::string str); -THP_API bool get_cpp_stacktraces_enabled(); +TORCH_PYTHON_API bool get_cpp_stacktraces_enabled(); // Abstract base class for exceptions which translate to specific Python types struct PyTorchError : public std::exception { diff --git a/torch/csrc/Export.h b/torch/csrc/Export.h new file mode 100644 index 00000000000..4bcd910f127 --- /dev/null +++ b/torch/csrc/Export.h @@ -0,0 +1,9 @@ +#pragma once + +#include + +#ifdef THP_BUILD_MAIN_LIB +#define TORCH_PYTHON_API C10_EXPORT +#else +#define TORCH_PYTHON_API C10_IMPORT +#endif diff --git a/torch/csrc/Generator.h b/torch/csrc/Generator.h index bab668751ee..d2d03e585dc 100644 --- a/torch/csrc/Generator.h +++ b/torch/csrc/Generator.h @@ -1,9 +1,9 @@ #pragma once +#include #include #include -#include // NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init) struct THPGenerator { @@ -14,16 +14,16 @@ struct THPGenerator { // Creates a new Python object wrapping the default at::Generator. The reference is // borrowed. The caller should ensure that the at::Generator object lifetime // last at least as long as the Python wrapper. -THP_API PyObject * THPGenerator_initDefaultGenerator(at::Generator cdata); +TORCH_PYTHON_API PyObject * THPGenerator_initDefaultGenerator(at::Generator cdata); #define THPGenerator_Check(obj) \ PyObject_IsInstance(obj, THPGeneratorClass) -THP_API PyObject *THPGeneratorClass; +TORCH_PYTHON_API extern PyObject *THPGeneratorClass; bool THPGenerator_init(PyObject *module); -THP_API PyObject * THPGenerator_Wrap(at::Generator gen); +TORCH_PYTHON_API PyObject * THPGenerator_Wrap(at::Generator gen); // Creates a new Python object for a Generator. The Generator must not already // have a PyObject* associated with it. diff --git a/torch/csrc/THP.h b/torch/csrc/THP.h index 9c0dd406ee6..146e6c532a7 100644 --- a/torch/csrc/THP.h +++ b/torch/csrc/THP.h @@ -5,7 +5,7 @@ #include #include -#include +#include // Back-compatibility macros, Thanks to http://cx-oracle.sourceforge.net/ // define PyInt_* macros for Python 3.x. NB: We must include Python.h first, diff --git a/torch/csrc/THP_export.h b/torch/csrc/THP_export.h deleted file mode 100644 index 991d50d9292..00000000000 --- a/torch/csrc/THP_export.h +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef THP_EXPORT_H -#define THP_EXPORT_H - -#ifdef _WIN32 -# ifdef THP_BUILD_MAIN_LIB -# define THP_API extern __declspec(dllexport) -# define THP_CLASS __declspec(dllexport) -# else -# define THP_API extern __declspec(dllimport) -# define THP_CLASS __declspec(dllimport) -# endif -#else -# define THP_API extern -# define THP_CLASS -#endif - -#endif diff --git a/torch/csrc/WindowsTorchApiMacro.h b/torch/csrc/WindowsTorchApiMacro.h index 44ae3b3b818..8d829006f38 100644 --- a/torch/csrc/WindowsTorchApiMacro.h +++ b/torch/csrc/WindowsTorchApiMacro.h @@ -1,9 +1,2 @@ #pragma once - -#include - -#ifdef _WIN32 -#define TORCH_PYTHON_API -#else -#define TORCH_PYTHON_API TORCH_API -#endif +#include diff --git a/torch/csrc/autograd/python_saved_variable_hooks.h b/torch/csrc/autograd/python_saved_variable_hooks.h index 145301f6f7e..f8c215f555e 100644 --- a/torch/csrc/autograd/python_saved_variable_hooks.h +++ b/torch/csrc/autograd/python_saved_variable_hooks.h @@ -4,7 +4,7 @@ #include #include #include -#include +#include #include namespace py = pybind11; diff --git a/torch/csrc/autograd/python_variable.h b/torch/csrc/autograd/python_variable.h index 30bfb61bcbf..c3e2c1d0efa 100644 --- a/torch/csrc/autograd/python_variable.h +++ b/torch/csrc/autograd/python_variable.h @@ -5,7 +5,7 @@ #include #include -#include +#include #include // Python object that backs torch.autograd.Variable @@ -21,11 +21,11 @@ struct THPVariable { TORCH_API void registerPythonTensorClass(const std::string& device, PyObject* python_tensor_class); -THP_API PyObject *THPVariableClass; -THP_API PyObject *ParameterClass; +TORCH_PYTHON_API extern PyObject *THPVariableClass; +TORCH_PYTHON_API extern PyObject *ParameterClass; bool THPVariable_initModule(PyObject *module); -THP_API PyObject * THPVariable_Wrap(at::TensorBase var); +TORCH_PYTHON_API PyObject * THPVariable_Wrap(at::TensorBase var); static inline bool THPVariable_CheckTypeExact(PyTypeObject* tp) { // Check that a python object is a `Tensor`, but not a `Tensor` subclass. @@ -61,4 +61,4 @@ inline const at::Tensor& THPVariable_Unpack(PyObject* obj) { return THPVariable_Unpack(reinterpret_cast(obj)); } -THP_API c10::impl::PyInterpreter* getPyInterpreter(); +TORCH_PYTHON_API c10::impl::PyInterpreter* getPyInterpreter(); diff --git a/torch/csrc/autograd/python_variable_indexing.cpp b/torch/csrc/autograd/python_variable_indexing.cpp index 19e74030fff..6393b82bd26 100644 --- a/torch/csrc/autograd/python_variable_indexing.cpp +++ b/torch/csrc/autograd/python_variable_indexing.cpp @@ -2,7 +2,7 @@ #include #include -#include +#include #include #include #include diff --git a/torch/csrc/distributed/c10d/comm.hpp b/torch/csrc/distributed/c10d/comm.hpp index 4690c355ce7..43ffdb07d78 100644 --- a/torch/csrc/distributed/c10d/comm.hpp +++ b/torch/csrc/distributed/c10d/comm.hpp @@ -3,6 +3,7 @@ #include #include #include +#include namespace c10d { @@ -85,7 +86,7 @@ class TORCH_API GradBucket { // Requires implementing 1) `runHook` method that communicates gradients // asynchronously, and 2) `parseHookResult` method that converts the hook // result into a tensor. -class TORCH_PYTHON_API CommHookInterface { +class TORCH_API CommHookInterface { public: virtual ~CommHookInterface() = default; @@ -121,9 +122,8 @@ inline at::Tensor parseCppCommHookResult( // This CppCommHook interface only requires implementing runHook method that // potentially uses a state. -// Still need TORCH_PYTHON_API instead of TORCH_API to support Windows platform. template -class TORCH_PYTHON_API CppCommHookInterface : public CommHookInterface { +class CppCommHookInterface : public CommHookInterface { public: explicit CppCommHookInterface(T& state) : state_(state) {} diff --git a/torch/csrc/generic/Storage.h b/torch/csrc/generic/Storage.h index 572f84af0de..a2063593207 100644 --- a/torch/csrc/generic/Storage.h +++ b/torch/csrc/generic/Storage.h @@ -4,7 +4,7 @@ #include -THP_API PyObject * THPStorage_(New)(c10::intrusive_ptr ptr); +TORCH_PYTHON_API PyObject * THPStorage_(New)(c10::intrusive_ptr ptr); extern PyObject *THPStorageClass; #include diff --git a/torch/csrc/utils.h b/torch/csrc/utils.h index cf9717c815b..fb0beb74101 100644 --- a/torch/csrc/utils.h +++ b/torch/csrc/utils.h @@ -126,8 +126,8 @@ #define THPUtils_assert(cond, ...) THPUtils_assertRet(nullptr, cond, __VA_ARGS__) #define THPUtils_assertRet(value, cond, ...) \ if (THP_EXPECT(!(cond), 0)) { THPUtils_setError(__VA_ARGS__); return value; } -THP_API void THPUtils_setError(const char *format, ...); -THP_API void THPUtils_invalidArguments( +TORCH_PYTHON_API void THPUtils_setError(const char *format, ...); +TORCH_PYTHON_API void THPUtils_invalidArguments( PyObject *given_args, PyObject *given_kwargs, const char *function_name, size_t num_options, ...);