Rationalize API exports in torch_python (#68095)

Summary:
This renames `WindowsTorchApiMacro.h` to `Export.h` to mirror the c10 header `c10/macros/Export.h` and also updates it to use `C10_EXPORT`/`C10_IMPORT`. This also removes the `THP_API` macro from `THP_export.h` which appears to serve the same purpose.

cc pietern mrshenli pritamdamania87 zhaojuanmao satgera rohan-varma gqchen aazzolini osalpekar jiayisuse SciPioneer H-Huang

Pull Request resolved: https://github.com/pytorch/pytorch/pull/68095

Reviewed By: jbschlosser

Differential Revision: D32810881

Pulled By: albanD

fbshipit-source-id: d6949ccd0d80d6c3e5ec1264207611fcfe2503e3
This commit is contained in:
Peter Bell 2021-12-07 15:22:44 -08:00 committed by Facebook GitHub Bot
parent 829b49b867
commit cd9da3267c
15 changed files with 34 additions and 49 deletions

View file

@ -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([

View file

@ -6,11 +6,10 @@
#include <ATen/core/functional.h>
#include <ATen/TensorGeometry.h>
#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 <torch/csrc/WindowsTorchApiMacro.h>
#include <torch/csrc/Export.h>
namespace torch { namespace autograd { namespace generated {

View file

@ -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",

View file

@ -9,10 +9,9 @@
#include <c10/util/Exception.h>
#include <pybind11/pybind11.h>
#include <torch/csrc/THP_export.h>
#include <torch/csrc/Export.h>
#include <torch/csrc/utils/auto_gil.h>
#include <torch/csrc/jit/runtime/jit_exception.h>
#include <torch/csrc/WindowsTorchApiMacro.h>
#include <c10/util/StringUtil.h>
#include <ATen/detail/FunctionTraits.h>
@ -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 {

9
torch/csrc/Export.h Normal file
View file

@ -0,0 +1,9 @@
#pragma once
#include <c10/macros/Export.h>
#ifdef THP_BUILD_MAIN_LIB
#define TORCH_PYTHON_API C10_EXPORT
#else
#define TORCH_PYTHON_API C10_IMPORT
#endif

View file

@ -1,9 +1,9 @@
#pragma once
#include <torch/csrc/Export.h>
#include <torch/csrc/python_headers.h>
#include <ATen/ATen.h>
#include <torch/csrc/THP_export.h>
// 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.

View file

@ -5,7 +5,7 @@
#include <TH/TH.h>
#include <TH/THTensor.hpp>
#include <torch/csrc/THP_export.h>
#include <torch/csrc/Export.h>
// Back-compatibility macros, Thanks to http://cx-oracle.sourceforge.net/
// define PyInt_* macros for Python 3.x. NB: We must include Python.h first,

View file

@ -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

View file

@ -1,9 +1,2 @@
#pragma once
#include <c10/macros/Export.h>
#ifdef _WIN32
#define TORCH_PYTHON_API
#else
#define TORCH_PYTHON_API TORCH_API
#endif
#include <torch/csrc/Export.h>

View file

@ -4,7 +4,7 @@
#include <torch/csrc/autograd/python_variable.h>
#include <torch/csrc/autograd/saved_variable_hooks.h>
#include <torch/csrc/python_headers.h>
#include <torch/csrc/THP_export.h>
#include <torch/csrc/Export.h>
#include <ATen/ATen.h>
namespace py = pybind11;

View file

@ -5,7 +5,7 @@
#include <ATen/ATen.h>
#include <torch/csrc/autograd/variable.h>
#include <torch/csrc/THP_export.h>
#include <torch/csrc/Export.h>
#include <torch/csrc/Exceptions.h>
// 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<THPVariable*>(obj));
}
THP_API c10::impl::PyInterpreter* getPyInterpreter();
TORCH_PYTHON_API c10::impl::PyInterpreter* getPyInterpreter();

View file

@ -2,7 +2,7 @@
#include <torch/csrc/DynamicTypes.h>
#include <torch/csrc/Exceptions.h>
#include <torch/csrc/THP_export.h>
#include <torch/csrc/Export.h>
#include <torch/csrc/autograd/function.h>
#include <torch/csrc/autograd/python_variable.h>
#include <torch/csrc/autograd/utils/wrap_outputs.h>

View file

@ -3,6 +3,7 @@
#include <ATen/ATen.h>
#include <ATen/core/ivalue.h>
#include <c10d/ProcessGroup.hpp>
#include <torch/csrc/Export.h>
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 <typename T>
class TORCH_PYTHON_API CppCommHookInterface : public CommHookInterface {
class CppCommHookInterface : public CommHookInterface {
public:
explicit CppCommHookInterface(T& state) : state_(state) {}

View file

@ -4,7 +4,7 @@
#include <torch/csrc/StorageDefs.h>
THP_API PyObject * THPStorage_(New)(c10::intrusive_ptr<c10::StorageImpl> ptr);
TORCH_PYTHON_API PyObject * THPStorage_(New)(c10::intrusive_ptr<c10::StorageImpl> ptr);
extern PyObject *THPStorageClass;
#include <torch/csrc/Types.h>

View file

@ -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, ...);