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

File tree

15 files changed

+34
-49
lines changed

15 files changed

+34
-49
lines changed

BUILD.bazel

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 2 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 3 additions & 4 deletions
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

Lines changed: 9 additions & 0 deletions
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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 0 additions & 17 deletions
This file was deleted.

torch/csrc/WindowsTorchApiMacro.h

Lines changed: 1 addition & 8 deletions
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

Lines changed: 1 addition & 1 deletion
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;

0 commit comments

Comments
 (0)