Expose several APIs to public (torch python APIs) (#144525)

Fixes #144302
Try to expose several APIs to public for privateuse1 scenario.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/144525
Approved by: https://github.com/cyyever, https://github.com/albanD
This commit is contained in:
dilililiwhy
2025-01-15 14:34:45 +00:00
committed by PyTorch MergeBot
parent dc8692b0eb
commit 7c52c97a65
5 changed files with 47 additions and 26 deletions

View File

@ -26,10 +26,10 @@ void registerDtypeObject(THPDtype* dtype, at::ScalarType scalarType);
void registerLayoutObject(THPLayout* thp_layout, at::Layout layout);
TORCH_PYTHON_API PyObject* createPyObject(const at::Storage& storage);
at::Storage createStorage(PyObject* obj);
std::tuple<at::Storage, at::ScalarType, bool> createStorageGetType(
PyObject* obj);
bool isStorage(PyObject* obj);
TORCH_PYTHON_API at::Storage createStorage(PyObject* obj);
TORCH_PYTHON_API std::tuple<at::Storage, at::ScalarType, bool>
createStorageGetType(PyObject* obj);
TORCH_PYTHON_API bool isStorage(PyObject* obj);
// Both methods below return a borrowed reference!
TORCH_PYTHON_API THPDtype* getTHPDtype(at::ScalarType scalarType);

View File

@ -17,7 +17,7 @@ struct THPCppFunction {
};
template <typename Ctor>
PyObject* CppFunction_pynew(
TORCH_PYTHON_API PyObject* CppFunction_pynew(
PyTypeObject* type,
PyObject* args,
PyObject* kwds) {
@ -70,29 +70,47 @@ PyObject* CppFunction_pynew(
nullptr \
}
PyObject* THPCppFunction_next_functions(PyObject* self, void* _unused);
PyObject* THPCppFunction_metadata(PyObject* self, void* _unused);
PyObject* THPCppFunction_requires_grad(PyObject* self, void* _unused);
PyObject* THPCppFunction_register_hook_dict(PyObject* self, PyObject* _var);
PyObject* THPCppFunction_register_hook(PyObject* self, PyObject* hook);
PyObject* THPCppFunction_register_prehook(PyObject* self, PyObject* hook);
TORCH_PYTHON_API PyObject* THPCppFunction_next_functions(
PyObject* self,
void* _unused);
TORCH_PYTHON_API PyObject* THPCppFunction_metadata(
PyObject* self,
void* _unused);
TORCH_PYTHON_API PyObject* THPCppFunction_requires_grad(
PyObject* self,
void* _unused);
TORCH_PYTHON_API PyObject* THPCppFunction_register_hook_dict(
PyObject* self,
PyObject* _var);
TORCH_PYTHON_API PyObject* THPCppFunction_register_hook(
PyObject* self,
PyObject* hook);
TORCH_PYTHON_API PyObject* THPCppFunction_register_prehook(
PyObject* self,
PyObject* hook);
PyObject* THPCppFunction_name(PyObject* self, PyObject* noargs);
PyObject* THPCppFunction_sequence_nr(PyObject* self, PyObject* noargs);
PyObject* THPCppFunction_input_metadata(PyObject* self, void* _unused);
TORCH_PYTHON_API PyObject* THPCppFunction_name(
PyObject* self,
PyObject* noargs);
TORCH_PYTHON_API PyObject* THPCppFunction_sequence_nr(
PyObject* self,
PyObject* noargs);
TORCH_PYTHON_API PyObject* THPCppFunction_input_metadata(
PyObject* self,
void* _unused);
PyTypeObject* _initFunctionPyTypeObject(
TORCH_PYTHON_API PyTypeObject* _initFunctionPyTypeObject(
PyTypeObject& type,
const char* name,
PyGetSetDef* function_properties,
PyMethodDef* function_methods);
PyObject* registerFunctionHook(Node& fn, PyObject* hook);
TORCH_PYTHON_API PyObject* registerFunctionHook(Node& fn, PyObject* hook);
PyObject* registerFunctionPreHook(Node& fn, PyObject* hook);
TORCH_PYTHON_API PyObject* registerFunctionPreHook(Node& fn, PyObject* hook);
template <typename Ctor>
PyTypeObject* createForwardFunctionPyTypeObject(
TORCH_PYTHON_API PyTypeObject* createForwardFunctionPyTypeObject(
PyTypeObject& type,
const char* name,
PyGetSetDef* function_properties = nullptr,
@ -102,8 +120,11 @@ PyTypeObject* createForwardFunctionPyTypeObject(
type, name, function_properties, function_methods);
}
void registerCppFunction(const std::type_info& type, PyTypeObject* pytype);
PyObject* functionToPyObject(const std::shared_ptr<Node>& cdata);
TORCH_PYTHON_API void registerCppFunction(
const std::type_info& type,
PyTypeObject* pytype);
TORCH_PYTHON_API PyObject* functionToPyObject(
const std::shared_ptr<Node>& cdata);
TORCH_PYTHON_API bool THPCppFunction_Check(PyObject* obj);

View File

@ -15,8 +15,8 @@ TORCH_API std::vector<pybind11::object> py_symbolize(
std::vector<CapturedTraceback*>& to_symbolize);
// requires GIL to be held, frees any pending free frames
void freeDeadCapturedTracebackFrames();
TORCH_PYTHON_API void freeDeadCapturedTracebackFrames();
void installCapturedTracebackPython();
TORCH_PYTHON_API void installCapturedTracebackPython();
} // namespace torch

View File

@ -4,7 +4,7 @@
#include <torch/csrc/python_headers.h>
template <>
void THPPointer<PyObject>::free() {
TORCH_PYTHON_API void THPPointer<PyObject>::free() {
if (ptr && C10_LIKELY(Py_IsInitialized()))
Py_DECREF(ptr);
}
@ -12,7 +12,7 @@ void THPPointer<PyObject>::free() {
template class THPPointer<PyObject>;
template <>
void THPPointer<PyCodeObject>::free() {
TORCH_PYTHON_API void THPPointer<PyCodeObject>::free() {
if (ptr && C10_LIKELY(Py_IsInitialized()))
Py_DECREF(ptr);
}
@ -20,7 +20,7 @@ void THPPointer<PyCodeObject>::free() {
template class THPPointer<PyCodeObject>;
template <>
void THPPointer<PyFrameObject>::free() {
TORCH_PYTHON_API void THPPointer<PyFrameObject>::free() {
if (ptr && C10_LIKELY(Py_IsInitialized()))
Py_DECREF(ptr);
}

View File

@ -30,7 +30,7 @@ TORCH_API bool only_lift_cpu_tensors();
TORCH_API void set_only_lift_cpu_tensors(bool value);
at::Tensor base_tensor_ctor(PyObject* args, PyObject* kwargs);
at::Tensor legacy_tensor_ctor(
TORCH_PYTHON_API at::Tensor legacy_tensor_ctor(
c10::DispatchKey dispatch_key,
at::ScalarType scalar_type,
PyObject* args,