Compare commits

...

10 Commits

Author SHA1 Message Date
ba6df09596 lintrunner 2025-10-19 16:26:25 +00:00
dead987b0e lintrunner 2025-10-19 13:04:05 +00:00
4de5f40f52 Add comment about #include in .c files 2025-10-18 18:53:57 -03:00
df582a36b6 lintrunner 2025-10-18 18:53:57 -03:00
e1635df37f Remove some defined(_WIN32) from dynamo 2025-10-18 18:53:57 -03:00
b8c6ac2b87 remove #pragma message 2025-10-18 18:53:57 -03:00
3884ec37f3 Import python_compat.h 2025-10-18 18:53:57 -03:00
576e35e176 Add Python 3.14 ifdef guards 2025-10-18 18:53:57 -03:00
d00fa92127 Wrap code in #if defined(_WIN32) 2025-10-18 18:53:57 -03:00
8b302de0c5 Fix MSCV C++ compilation error of pycore_stackref.h header
Wraps the header in a C file and compile it using a C compiler, which should support designated initializers

Fix issue #160647
2025-10-18 18:53:56 -03:00
7 changed files with 77 additions and 18 deletions

View File

@ -926,6 +926,7 @@ libtorch_python_core_sources = [
"torch/csrc/dynamo/guards.cpp",
"torch/csrc/dynamo/utils.cpp",
"torch/csrc/dynamo/init.cpp",
"torch/csrc/dynamo/stackref_bridge.c",
"torch/csrc/functorch/init.cpp",
"torch/csrc/fx/node.cpp",
"torch/csrc/mps/Module.cpp",

View File

@ -2,7 +2,17 @@
#include <torch/csrc/dynamo/cpython_includes.h>
#include <torch/csrc/dynamo/debug_macros.h>
#if IS_PYTHON_3_15_PLUS || (IS_PYTHON_3_14_PLUS && defined(_WIN32))
// Include CPython header files here (.c file) as MSVC C++ compiler cannot
// compile pycore_stackref.h. See PyTorch issue #160647
#if IS_PYTHON_3_14_PLUS && defined(_WIN32)
#define Py_BUILD_CORE
#include <internal/pycore_stackref.h>
#include <internal/pycore_genobject.h>
#include <internal/pycore_interpframe.h>
#undef Py_BUILD_CORE
#endif
#if IS_PYTHON_3_15_PLUS
const uint8_t* THP_PyOpcode_Caches = NULL;
int THP_PyOpcode_Caches_size = 0;

View File

@ -21,6 +21,8 @@
#if IS_PYTHON_3_11_PLUS
#include <internal/pycore_frame.h>
#include <torch/csrc/dynamo/stackref_bridge.h>
#if IS_PYTHON_3_14_PLUS && !defined(_WIN32)
#include <internal/pycore_code.h>
#include <internal/pycore_genobject.h>
@ -38,14 +40,10 @@
extern "C" {
#endif
#if IS_PYTHON_3_14_PLUS && !defined(_WIN32)
#if IS_PYTHON_3_14_PLUS
#define F_CODE(x) ((PyCodeObject*)PyStackRef_AsPyObjectBorrow(x->f_executable))
#define PREV_INSTR(x) (x)->instr_ptr
#elif IS_PYTHON_3_14_PLUS && defined(_WIN32)
#define F_CODE(x) ((PyCodeObject*)((x)->f_executable.bits))
#define F_CODE(x) \
((PyCodeObject*)THP_PyStackRef_AsPyObjectBorrow(&x->f_executable))
#define PREV_INSTR(x) (x)->instr_ptr
#else
@ -56,14 +54,13 @@ extern "C" {
#else
#define F_CODE(x) ((PyCodeObject*)(x)->f_code)
#define PREV_INSTR(x) (x)->prev_instr
#endif
#endif // IS_PYTHON_3_13_PLUS
#endif // IS_PYTHON_3_14_PLUS
#if IS_PYTHON_3_14_PLUS && !defined(_WIN32)
#define FUNC(x) ((PyFunctionObject*)PyStackRef_AsPyObjectBorrow((x)->f_funcobj))
#elif IS_PYTHON_3_14_PLUS && defined(_WIN32)
#define FUNC(x) ((PyFunctionObject*)((x)->f_funcobj.bits))
#if IS_PYTHON_3_14_PLUS
#define FUNC(x) \
((PyFunctionObject*)THP_PyStackRef_AsPyObjectBorrow(&x->f_funcobj))
#elif IS_PYTHON_3_12_PLUS
#define FUNC(x) ((PyFunctionObject*)(x)->f_funcobj)
#else

View File

@ -10,6 +10,14 @@
#include <torch/csrc/dynamo/eval_frame_cpp.h>
#include <torch/csrc/utils/python_compat.h>
#if IS_PYTHON_3_14_PLUS && defined(_WIN32)
#define Py_BUILD_CORE
#include <internal/pycore_stackref.h>
#include <internal/pycore_code.h>
#include <internal/pycore_interpframe.h>
#undef Py_BUILD_CORE
#endif
PyObject* guard_error_hook = NULL;
PyObject* guard_complete_hook = NULL;
@ -36,7 +44,7 @@ void eval_frame_callback_set(PyObject* obj) {
// 3.15 Not supported at all. See cpython_defs.c for hints
// 3.14 currently not fully supported on Windows
#if !(IS_PYTHON_3_15_PLUS || (IS_PYTHON_3_14_PLUS && defined(_WIN32)))
#if !(IS_PYTHON_3_15_PLUS)
#define DECLARE_PYOBJ_ATTR(name) \
static PyObject* THPPyInterpreterFrame_##name( \

View File

@ -28,7 +28,7 @@ FrameLocalsMapping::FrameLocalsMapping(FrameLocalsFrameType* frame)
PyCodeObject* co = F_CODE(frame);
_framelocals.resize(co->co_nlocalsplus, nullptr);
#if IS_PYTHON_3_15_PLUS || (IS_PYTHON_3_14_PLUS && defined(_WIN32))
#if IS_PYTHON_3_15_PLUS
TORCH_CHECK(false, "Python 3.15+ / 3.14 on Windows not supported");
#elif IS_PYTHON_3_14_PLUS
if (!frame->stackpointer) {
@ -63,11 +63,12 @@ FrameLocalsMapping::FrameLocalsMapping(FrameLocalsFrameType* frame)
};
auto offset = co->co_nlocalsplus - co->co_nfreevars;
#if IS_PYTHON_3_15_PLUS || (IS_PYTHON_3_14_PLUS && defined(_WIN32))
#if IS_PYTHON_3_15_PLUS
TORCH_CHECK(false, "Python 3.15+ / 3.14 on Windows not supported");
#elif IS_PYTHON_3_14_PLUS
for (int i = 0; i < offset; i++) {
update_framelocals(i, PyStackRef_AsPyObjectBorrow(frame->localsplus[i]));
update_framelocals(
i, THP_PyStackRef_AsPyObjectBorrow(&frame->localsplus[i]));
}
#else
for (int i = 0; i < offset; i++) {
@ -76,7 +77,7 @@ FrameLocalsMapping::FrameLocalsMapping(FrameLocalsFrameType* frame)
#endif
// Get references to closure variables
#if IS_PYTHON_3_15_PLUS || (IS_PYTHON_3_14_PLUS && defined(_WIN32))
#if IS_PYTHON_3_15_PLUS
PyObject* closure;
TORCH_CHECK(false, "Python 3.15+ / 3.14 on Windows not supported");
#else

View File

@ -0,0 +1,24 @@
// Compile this file as C, not C++.
// Wrap inclusion of pycore_stackref.h inside a pure C file and ensure this file
// is compiled as C (not C++). This avoids MSVCs “designated initializers
// require /std:c++20” error, since pycore_stackref.h uses C99-style designated
// initializers that are not supported in older C++ standards, but is supported
// in C.
#include <torch/csrc/utils/python_compat.h>
#if IS_PYTHON_3_14_PLUS
#define Py_BUILD_CORE
#include <Python.h>
#include <internal/pycore_stackref.h>
#include <torch/csrc/dynamo/stackref_bridge.h>
#undef Py_BUILD_CORE
PyObject* THP_PyStackRef_AsPyObjectBorrow(void* stackref) {
_PyStackRef *sr = (_PyStackRef*)stackref;
return PyStackRef_AsPyObjectBorrow(*sr);
}
#endif

View File

@ -0,0 +1,18 @@
#pragma once
#include <torch/csrc/utils/python_compat.h>
#if IS_PYTHON_3_14_PLUS
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
// Use a void* to avoid exposing the internal _PyStackRef union on this
// translation unit
PyObject* THP_PyStackRef_AsPyObjectBorrow(void* stackref);
#ifdef __cplusplus
}
#endif // __cplusplus
#endif // IS_PYTHON_3_14_PLUS