Export function symbols to enable Windows build of Intel Extension for PyTorch (#98054)
This PR is to export specific function symbols into .dll shared library on Windows platform to support Windows build for [Intel Extension for PyTorch](https://github.com/intel/intel-extension-for-pytorch).
TORCH_API/TORCH_PYTHON_API/PYBIND11_EXPORT are macros that decorate the function as dllexport while compilation, so that the function symbol will be exported into the .dll shared library file on Windows platform. It is necessary for other libraries (such as IPEX) to import and call these functions through dynamic linking of PyTorch on Windows platform.
The code changes of this PR adds decorators to export specific functions used by IPEX.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/98054
Approved by: https://github.com/ezyang
diff --git a/torch/csrc/Dtype.h b/torch/csrc/Dtype.h
index 648a809..70cee85 100644
--- a/torch/csrc/Dtype.h
+++ b/torch/csrc/Dtype.h
@@ -23,6 +23,8 @@
obj == (PyObject*)(&PyBool_Type) || obj == (PyObject*)(&PyLong_Type);
}
-PyObject* THPDtype_New(at::ScalarType scalar_type, const std::string& name);
+TORCH_API PyObject* THPDtype_New(
+ at::ScalarType scalar_type,
+ const std::string& name);
void THPDtype_init(PyObject* module);
diff --git a/torch/csrc/DynamicTypes.h b/torch/csrc/DynamicTypes.h
index 7ca1894..1ca813c 100644
--- a/torch/csrc/DynamicTypes.h
+++ b/torch/csrc/DynamicTypes.h
@@ -33,6 +33,6 @@
bool& is_typed_storage);
bool isStorage(PyObject* obj);
-THPDtype* getTHPDtype(at::ScalarType scalarType);
+TORCH_PYTHON_API THPDtype* getTHPDtype(at::ScalarType scalarType);
THPLayout* getTHPLayout(at::Layout layout);
} // namespace torch
diff --git a/torch/csrc/Exceptions.h b/torch/csrc/Exceptions.h
index b904265..7c3545a 100644
--- a/torch/csrc/Exceptions.h
+++ b/torch/csrc/Exceptions.h
@@ -314,7 +314,7 @@
// Translates to Python ValueError
struct ValueError : public PyTorchError {
using PyTorchError::PyTorchError;
- ValueError(const char* format, ...) TORCH_FORMAT_FUNC(2, 3);
+ TORCH_PYTHON_API ValueError(const char* format, ...) TORCH_FORMAT_FUNC(2, 3);
PyObject* python_type() override {
return PyExc_ValueError;
}
diff --git a/torch/csrc/Stream.h b/torch/csrc/Stream.h
index ec98320..656e37a 100644
--- a/torch/csrc/Stream.h
+++ b/torch/csrc/Stream.h
@@ -1,6 +1,7 @@
#ifndef THP_STREAM_INC
#define THP_STREAM_INC
+#include <c10/macros/Export.h>
#include <torch/csrc/python_headers.h>
struct THPStream {
@@ -8,7 +9,7 @@
int64_t device_type;
int64_t device_index;
};
-extern PyTypeObject* THPStreamClass;
+extern TORCH_API PyTypeObject* THPStreamClass;
void THPStream_init(PyObject* module);
diff --git a/torch/csrc/tensor/python_tensor.h b/torch/csrc/tensor/python_tensor.h
index ab74e05..9040f84 100644
--- a/torch/csrc/tensor/python_tensor.h
+++ b/torch/csrc/tensor/python_tensor.h
@@ -27,7 +27,7 @@
// TODO: This is nuts! There is no reason to let the default tensor type id
// change. Probably only store ScalarType, as that's the only flex point
// we support.
-c10::DispatchKey get_default_dispatch_key();
+TORCH_API c10::DispatchKey get_default_dispatch_key();
at::Device get_default_device();
// Gets the ScalarType for the default tensor type.
diff --git a/torch/csrc/utils.h b/torch/csrc/utils.h
index c08e42d..62fe707 100644
--- a/torch/csrc/utils.h
+++ b/torch/csrc/utils.h
@@ -170,7 +170,7 @@
bool THPUtils_checkIntTuple(PyObject* arg);
std::vector<int> THPUtils_unpackIntTuple(PyObject* arg);
-void THPUtils_addPyMethodDefs(
+TORCH_PYTHON_API void THPUtils_addPyMethodDefs(
std::vector<PyMethodDef>& vector,
PyMethodDef* methods);
@@ -179,7 +179,7 @@
typedef THPPointer<THPGenerator> THPGeneratorPtr;
typedef class THPPointer<THPStorage> THPStoragePtr;
-std::vector<int64_t> THPUtils_unpackLongs(PyObject* arg);
+TORCH_PYTHON_API std::vector<int64_t> THPUtils_unpackLongs(PyObject* arg);
PyObject* THPUtils_dispatchStateless(
PyObject* tensor,
const char* name,
diff --git a/torch/csrc/utils/object_ptr.h b/torch/csrc/utils/object_ptr.h
index 359e177..35302e2 100644
--- a/torch/csrc/utils/object_ptr.h
+++ b/torch/csrc/utils/object_ptr.h
@@ -1,9 +1,10 @@
#pragma once
+#include <torch/csrc/Export.h>
#include <torch/csrc/python_headers.h>
template <class T>
-class THPPointer {
+class TORCH_PYTHON_API THPPointer {
public:
THPPointer() : ptr(nullptr){};
explicit THPPointer(T* ptr) noexcept : ptr(ptr){};
diff --git a/torch/csrc/utils/python_arg_parser.h b/torch/csrc/utils/python_arg_parser.h
index 24c870f..c8c70c0 100644
--- a/torch/csrc/utils/python_arg_parser.h
+++ b/torch/csrc/utils/python_arg_parser.h
@@ -133,7 +133,7 @@
PyObject* args[N];
};
-struct PythonArgParser {
+struct PYBIND11_EXPORT PythonArgParser {
explicit PythonArgParser(
std::vector<std::string> fmts,
bool traceable = false);