Skip to content

Commit 13044b2

Browse files
desertfirepytorchmergebot
authored andcommitted
Move c10/macros/Export.h to torch/standalone (pytorch#154850)
Summary: The goal of this PR and future follow-up PRs is to group a set of header files required by AOTInductor Standalone in a separate directory, ensuring they are implemented in a header-only manner. Test Plan: CI Bifferential Revision: D75756619 Pull Request resolved: pytorch#154850 Approved by: https://github.com/janeyx99
1 parent a7e496a commit 13044b2

File tree

13 files changed

+134
-88
lines changed

13 files changed

+134
-88
lines changed

.lintrunner.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ include_patterns = [
8787
'torch/csrc/**/*.cpp',
8888
'torch/nativert/**/*.h',
8989
'torch/nativert/**/*.cpp',
90+
'torch/standalone/**/*.h',
9091
'test/cpp/**/*.h',
9192
'test/cpp/**/*.cpp',
9293
]
@@ -240,6 +241,7 @@ include_patterns = [
240241
'torch/nativert/*.cpp',
241242
'torch/nativert/**/*.h',
242243
'torch/nativert/**/*.cpp',
244+
'torch/standalone/**/*.h',
243245
]
244246
exclude_patterns = [
245247
# The negative filters below are to exclude files that include onnx_pb.h or

BUILD.bazel

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,14 @@ flatbuffer_cc_library(
671671
out_prefix = "torch/csrc/jit/serialization/",
672672
)
673673

674+
cc_library(
675+
name = "torch_standalone_headers",
676+
hdrs = glob([
677+
"torch/standalone/**/*.h"
678+
]),
679+
visibility = ["//visibility:public"],
680+
)
681+
674682
cc_library(
675683
name = "torch_headers",
676684
hdrs = if_cuda(

buckbuild.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -945,6 +945,7 @@ def define_buck_targets(
945945
[
946946
("torch/csrc/api/include", "torch/**/*.h"),
947947
("", "torch/csrc/**/*.h"),
948+
("", "torch/standalone/**/*.h"),
948949
("", "torch/script.h"),
949950
("", "torch/library.h"),
950951
("", "torch/custom_class.h"),

c10/core/build.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ def define_targets(rules):
8080
deps = [
8181
":ScalarType",
8282
"//third_party/cpuinfo",
83+
"//:torch_standalone_headers",
8384
"//c10/macros",
8485
"//c10/util:TypeCast",
8586
"//c10/util:base",

c10/macros/Export.h

Lines changed: 2 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,11 @@
11
#ifndef C10_MACROS_EXPORT_H_
22
#define C10_MACROS_EXPORT_H_
33

4-
/* Header file to define the common scaffolding for exported symbols.
5-
*
6-
* Export is by itself a quite tricky situation to deal with, and if you are
7-
* hitting this file, make sure you start with the background here:
8-
* - Linux: https://gcc.gnu.org/wiki/Visibility
9-
* - Windows:
10-
* https://docs.microsoft.com/en-us/cpp/cpp/dllexport-dllimport?view=vs-2017
11-
*
12-
* Do NOT include this file directly. Instead, use c10/macros/Macros.h
13-
*/
14-
15-
// You do not need to edit this part of file unless you are changing the core
16-
// pytorch export abstractions.
17-
//
18-
// This part defines the C10 core export and import macros. This is controlled
19-
// by whether we are building shared libraries or not, which is determined
20-
// during build time and codified in c10/core/cmake_macros.h.
21-
// When the library is built as a shared lib, EXPORT and IMPORT will contain
22-
// visibility attributes. If it is being built as a static lib, then EXPORT
23-
// and IMPORT basically have no effect.
24-
25-
// As a rule of thumb, you should almost NEVER mix static and shared builds for
26-
// libraries that depend on c10. AKA, if c10 is built as a static library, we
27-
// recommend everything dependent on c10 to be built statically. If c10 is built
28-
// as a shared library, everything dependent on it should be built as shared. In
29-
// the PyTorch project, all native libraries shall use the macro
30-
// C10_BUILD_SHARED_LIB to check whether pytorch is building shared or static
31-
// libraries.
32-
33-
// For build systems that do not directly depend on CMake and directly build
34-
// from the source directory (such as Buck), one may not have a cmake_macros.h
35-
// file at all. In this case, the build system is responsible for providing
36-
// correct macro definitions corresponding to the cmake_macros.h.in file.
37-
//
38-
// In such scenarios, one should define the macro
39-
// C10_USING_CUSTOM_GENERATED_MACROS
40-
// to inform this header that it does not need to include the cmake_macros.h
41-
// file.
42-
434
#ifndef C10_USING_CUSTOM_GENERATED_MACROS
445
#include <c10/macros/cmake_macros.h>
456
#endif // C10_USING_CUSTOM_GENERATED_MACROS
467

47-
#ifdef _WIN32
48-
#define C10_HIDDEN
49-
#if defined(C10_BUILD_SHARED_LIBS)
50-
#define C10_EXPORT __declspec(dllexport)
51-
#define C10_IMPORT __declspec(dllimport)
52-
#else
53-
#define C10_EXPORT
54-
#define C10_IMPORT
55-
#endif
56-
#else // _WIN32
57-
#if defined(__GNUC__)
58-
#define C10_EXPORT __attribute__((__visibility__("default")))
59-
#define C10_HIDDEN __attribute__((__visibility__("hidden")))
60-
#else // defined(__GNUC__)
61-
#define C10_EXPORT
62-
#define C10_HIDDEN
63-
#endif // defined(__GNUC__)
64-
#define C10_IMPORT C10_EXPORT
65-
#endif // _WIN32
66-
67-
#ifdef NO_EXPORT
68-
#undef C10_EXPORT
69-
#define C10_EXPORT
70-
#endif
71-
72-
// Definition of an adaptive XX_API macro, that depends on whether you are
73-
// building the library itself or not, routes to XX_EXPORT and XX_IMPORT.
74-
// Basically, you will need to do this for each shared library that you are
75-
// building, and the instruction is as follows: assuming that you are building
76-
// a library called libawesome.so. You should:
77-
// (1) for your cmake target (usually done by "add_library(awesome, ...)"),
78-
// define a macro called AWESOME_BUILD_MAIN_LIB using
79-
// target_compile_options.
80-
// (2) define the AWESOME_API macro similar to the one below.
81-
// And in the source file of your awesome library, use AWESOME_API to
82-
// annotate public symbols.
83-
84-
// Here, for the C10 library, we will define the macro C10_API for both import
85-
// and export.
86-
87-
// This one is being used by libc10.so
88-
#ifdef C10_BUILD_MAIN_LIB
89-
#define C10_API C10_EXPORT
90-
#else
91-
#define C10_API C10_IMPORT
92-
#endif
8+
#include <torch/standalone/macros/Export.h>
939

9410
// This one is being used by libtorch.so
9511
#ifdef CAFFE2_BUILD_MAIN_LIB
@@ -159,4 +75,4 @@
15975
#define C10_API_ENUM
16076
#endif
16177

162-
#endif // C10_MACROS_MACROS_H_
78+
#endif // C10_MACROS_EXPORT_H_

c10/macros/build.bzl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ def define_targets(rules):
1212
linkstatic = True,
1313
local_defines = ["C10_BUILD_MAIN_LIB"],
1414
visibility = ["//visibility:public"],
15+
deps = [
16+
"//:torch_standalone_headers",
17+
],
1518
)
1619

1720
rules.cmake_configure_file(

c10/ovrsource_defs.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ def define_c10_ovrsource(name, is_mobile):
7474
],
7575
}),
7676
exported_deps = [
77+
"//xplat/caffe2:torch_standalone_headers",
7778
":ovrsource_c10_cmake_macros.h",
7879
"//arvr/third-party/gflags:gflags",
7980
"//third-party/cpuinfo:cpuinfo",

caffe2/CMakeLists.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,7 +1289,8 @@ endif()
12891289
target_include_directories(torch_cpu PRIVATE ${ATen_CPU_INCLUDE})
12901290

12911291
target_include_directories(torch_cpu PRIVATE
1292-
${TORCH_SRC_DIR}/csrc)
1292+
${TORCH_SRC_DIR}/csrc
1293+
${TORCH_SRC_DIR}/standalone)
12931294

12941295
target_include_directories(torch_cpu PRIVATE
12951296
${TORCH_ROOT}/third_party/miniz-3.0.2)
@@ -1308,9 +1309,12 @@ target_include_directories(torch_cpu PRIVATE
13081309
target_include_directories(torch_cpu PRIVATE
13091310
${TORCH_ROOT}/third_party/nlohmann/include)
13101311

1311-
install(DIRECTORY "${TORCH_SRC_DIR}/csrc"
1312+
install(DIRECTORY
1313+
"${TORCH_SRC_DIR}/csrc"
1314+
"${TORCH_SRC_DIR}/standalone"
13121315
DESTINATION ${TORCH_INSTALL_INCLUDE_DIR}/torch
13131316
FILES_MATCHING PATTERN "*.h" PATTERN "*.hpp")
1317+
13141318
install(FILES
13151319
"${TORCH_SRC_DIR}/script.h"
13161320
"${TORCH_SRC_DIR}/extension.h"

test/cpp/aoti_abi_check/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ set(AOTI_ABI_CHECK_TEST_SRCS
55
${AOTI_ABI_CHECK_TEST_ROOT}/main.cpp
66
${AOTI_ABI_CHECK_TEST_ROOT}/test_cast.cpp
77
${AOTI_ABI_CHECK_TEST_ROOT}/test_dtype.cpp
8+
${AOTI_ABI_CHECK_TEST_ROOT}/test_macros.cpp
89
${AOTI_ABI_CHECK_TEST_ROOT}/test_math.cpp
910
${AOTI_ABI_CHECK_TEST_ROOT}/test_rand.cpp
1011
${AOTI_ABI_CHECK_TEST_ROOT}/test_vec.cpp
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <gtest/gtest.h>
2+
3+
#include <torch/standalone/macros/Export.h>
4+
5+
namespace torch {
6+
namespace aot_inductor {
7+
8+
C10_API bool equal(int a, int b) {
9+
return a == b;
10+
}
11+
12+
TEST(TestMacros, TestC10API) {
13+
EXPECT_TRUE(equal(1, 1));
14+
EXPECT_FALSE(equal(1, 2));
15+
}
16+
17+
} // namespace aot_inductor
18+
} // namespace torch

0 commit comments

Comments
 (0)