Skip to content

Commit cd9da32

Browse files
peterbell10facebook-github-bot
authored andcommitted
Rationalize API exports in torch_python (pytorch#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: pytorch#68095 Reviewed By: jbschlosser Differential Revision: D32810881 Pulled By: albanD fbshipit-source-id: d6949ccd0d80d6c3e5ec1264207611fcfe2503e3
1 parent 829b49b commit cd9da32

15 files changed

+34
-49
lines changed

BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,7 @@ header_template_rule(
542542
cc_library(
543543
name = "aten_headers",
544544
hdrs = [
545+
"torch/csrc/Export.h",
545546
"torch/csrc/WindowsTorchApiMacro.h",
546547
"torch/csrc/jit/frontend/function_schema_parser.h",
547548
] + glob([

tools/autograd/templates/Functions.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@
66
#include <ATen/core/functional.h>
77
#include <ATen/TensorGeometry.h>
88

9-
#include "torch/csrc/THP_export.h"
109
#include "torch/csrc/autograd/function.h"
1110
#include "torch/csrc/autograd/variable.h"
1211
#include "torch/csrc/autograd/saved_variable.h"
13-
#include <torch/csrc/WindowsTorchApiMacro.h>
12+
#include <torch/csrc/Export.h>
1413

1514
namespace torch { namespace autograd { namespace generated {
1615

tools/build_variables.bzl

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ def libtorch_generated_sources(gencode_pattern):
7171
# copied from https://github.com/pytorch/pytorch/blob/f99a693cd9ff7a9b5fdc71357dac66b8192786d3/aten/src/ATen/core/CMakeLists.txt
7272
jit_core_headers = [
7373
"torch/csrc/utils/memory.h",
74+
"torch/csrc/Export.h",
7475
"torch/csrc/WindowsTorchApiMacro.h",
7576
"torch/csrc/jit/frontend/source_range.h",
7677
"torch/csrc/jit/serialization/callstack_debug_info_serialization.h",

torch/csrc/Exceptions.h

+3-4
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@
99

1010
#include <c10/util/Exception.h>
1111
#include <pybind11/pybind11.h>
12-
#include <torch/csrc/THP_export.h>
12+
#include <torch/csrc/Export.h>
1313
#include <torch/csrc/utils/auto_gil.h>
1414
#include <torch/csrc/jit/runtime/jit_exception.h>
15-
#include <torch/csrc/WindowsTorchApiMacro.h>
1615
#include <c10/util/StringUtil.h>
1716
#include <ATen/detail/FunctionTraits.h>
1817

@@ -259,9 +258,9 @@ bool THPException_init(PyObject *module);
259258

260259
namespace torch {
261260

262-
THP_CLASS std::string processErrorMsg(std::string str);
261+
TORCH_PYTHON_API std::string processErrorMsg(std::string str);
263262

264-
THP_API bool get_cpp_stacktraces_enabled();
263+
TORCH_PYTHON_API bool get_cpp_stacktraces_enabled();
265264

266265
// Abstract base class for exceptions which translate to specific Python types
267266
struct PyTorchError : public std::exception {

torch/csrc/Export.h

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#pragma once
2+
3+
#include <c10/macros/Export.h>
4+
5+
#ifdef THP_BUILD_MAIN_LIB
6+
#define TORCH_PYTHON_API C10_EXPORT
7+
#else
8+
#define TORCH_PYTHON_API C10_IMPORT
9+
#endif

torch/csrc/Generator.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#pragma once
22

3+
#include <torch/csrc/Export.h>
34
#include <torch/csrc/python_headers.h>
45
#include <ATen/ATen.h>
56

6-
#include <torch/csrc/THP_export.h>
77

88
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init)
99
struct THPGenerator {
@@ -14,16 +14,16 @@ struct THPGenerator {
1414
// Creates a new Python object wrapping the default at::Generator. The reference is
1515
// borrowed. The caller should ensure that the at::Generator object lifetime
1616
// last at least as long as the Python wrapper.
17-
THP_API PyObject * THPGenerator_initDefaultGenerator(at::Generator cdata);
17+
TORCH_PYTHON_API PyObject * THPGenerator_initDefaultGenerator(at::Generator cdata);
1818

1919
#define THPGenerator_Check(obj) \
2020
PyObject_IsInstance(obj, THPGeneratorClass)
2121

22-
THP_API PyObject *THPGeneratorClass;
22+
TORCH_PYTHON_API extern PyObject *THPGeneratorClass;
2323

2424
bool THPGenerator_init(PyObject *module);
2525

26-
THP_API PyObject * THPGenerator_Wrap(at::Generator gen);
26+
TORCH_PYTHON_API PyObject * THPGenerator_Wrap(at::Generator gen);
2727

2828
// Creates a new Python object for a Generator. The Generator must not already
2929
// have a PyObject* associated with it.

torch/csrc/THP.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include <TH/TH.h>
66
#include <TH/THTensor.hpp>
77

8-
#include <torch/csrc/THP_export.h>
8+
#include <torch/csrc/Export.h>
99

1010
// Back-compatibility macros, Thanks to http://cx-oracle.sourceforge.net/
1111
// define PyInt_* macros for Python 3.x. NB: We must include Python.h first,

torch/csrc/THP_export.h

-17
This file was deleted.

torch/csrc/WindowsTorchApiMacro.h

+1-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,2 @@
11
#pragma once
2-
3-
#include <c10/macros/Export.h>
4-
5-
#ifdef _WIN32
6-
#define TORCH_PYTHON_API
7-
#else
8-
#define TORCH_PYTHON_API TORCH_API
9-
#endif
2+
#include <torch/csrc/Export.h>

torch/csrc/autograd/python_saved_variable_hooks.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <torch/csrc/autograd/python_variable.h>
55
#include <torch/csrc/autograd/saved_variable_hooks.h>
66
#include <torch/csrc/python_headers.h>
7-
#include <torch/csrc/THP_export.h>
7+
#include <torch/csrc/Export.h>
88
#include <ATen/ATen.h>
99

1010
namespace py = pybind11;

torch/csrc/autograd/python_variable.h

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include <ATen/ATen.h>
66

77
#include <torch/csrc/autograd/variable.h>
8-
#include <torch/csrc/THP_export.h>
8+
#include <torch/csrc/Export.h>
99
#include <torch/csrc/Exceptions.h>
1010

1111
// Python object that backs torch.autograd.Variable
@@ -21,11 +21,11 @@ struct THPVariable {
2121

2222
TORCH_API void registerPythonTensorClass(const std::string& device, PyObject* python_tensor_class);
2323

24-
THP_API PyObject *THPVariableClass;
25-
THP_API PyObject *ParameterClass;
24+
TORCH_PYTHON_API extern PyObject *THPVariableClass;
25+
TORCH_PYTHON_API extern PyObject *ParameterClass;
2626

2727
bool THPVariable_initModule(PyObject *module);
28-
THP_API PyObject * THPVariable_Wrap(at::TensorBase var);
28+
TORCH_PYTHON_API PyObject * THPVariable_Wrap(at::TensorBase var);
2929

3030
static inline bool THPVariable_CheckTypeExact(PyTypeObject* tp) {
3131
// 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) {
6161
return THPVariable_Unpack(reinterpret_cast<THPVariable*>(obj));
6262
}
6363

64-
THP_API c10::impl::PyInterpreter* getPyInterpreter();
64+
TORCH_PYTHON_API c10::impl::PyInterpreter* getPyInterpreter();

torch/csrc/autograd/python_variable_indexing.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#include <torch/csrc/DynamicTypes.h>
44
#include <torch/csrc/Exceptions.h>
5-
#include <torch/csrc/THP_export.h>
5+
#include <torch/csrc/Export.h>
66
#include <torch/csrc/autograd/function.h>
77
#include <torch/csrc/autograd/python_variable.h>
88
#include <torch/csrc/autograd/utils/wrap_outputs.h>

torch/csrc/distributed/c10d/comm.hpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <ATen/ATen.h>
44
#include <ATen/core/ivalue.h>
55
#include <c10d/ProcessGroup.hpp>
6+
#include <torch/csrc/Export.h>
67

78
namespace c10d {
89

@@ -85,7 +86,7 @@ class TORCH_API GradBucket {
8586
// Requires implementing 1) `runHook` method that communicates gradients
8687
// asynchronously, and 2) `parseHookResult` method that converts the hook
8788
// result into a tensor.
88-
class TORCH_PYTHON_API CommHookInterface {
89+
class TORCH_API CommHookInterface {
8990
public:
9091
virtual ~CommHookInterface() = default;
9192

@@ -121,9 +122,8 @@ inline at::Tensor parseCppCommHookResult(
121122

122123
// This CppCommHook interface only requires implementing runHook method that
123124
// potentially uses a state.
124-
// Still need TORCH_PYTHON_API instead of TORCH_API to support Windows platform.
125125
template <typename T>
126-
class TORCH_PYTHON_API CppCommHookInterface : public CommHookInterface {
126+
class CppCommHookInterface : public CommHookInterface {
127127
public:
128128
explicit CppCommHookInterface(T& state) : state_(state) {}
129129

torch/csrc/generic/Storage.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
#include <torch/csrc/StorageDefs.h>
66

7-
THP_API PyObject * THPStorage_(New)(c10::intrusive_ptr<c10::StorageImpl> ptr);
7+
TORCH_PYTHON_API PyObject * THPStorage_(New)(c10::intrusive_ptr<c10::StorageImpl> ptr);
88
extern PyObject *THPStorageClass;
99

1010
#include <torch/csrc/Types.h>

torch/csrc/utils.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@
126126
#define THPUtils_assert(cond, ...) THPUtils_assertRet(nullptr, cond, __VA_ARGS__)
127127
#define THPUtils_assertRet(value, cond, ...) \
128128
if (THP_EXPECT(!(cond), 0)) { THPUtils_setError(__VA_ARGS__); return value; }
129-
THP_API void THPUtils_setError(const char *format, ...);
130-
THP_API void THPUtils_invalidArguments(
129+
TORCH_PYTHON_API void THPUtils_setError(const char *format, ...);
130+
TORCH_PYTHON_API void THPUtils_invalidArguments(
131131
PyObject *given_args, PyObject *given_kwargs,
132132
const char *function_name, size_t num_options, ...);
133133

0 commit comments

Comments
 (0)