[dynamo] fix potential 3.12+ THP_PyOpcode_Caches init error seen internally (#165200)

Another attempt at merging https://github.com/pytorch/pytorch/pull/164597 due to CLA signing failure.

Differential Revision: [D84397377](https://our.internmc.facebook.com/intern/diff/D84397377)
Pull Request resolved: https://github.com/pytorch/pytorch/pull/165200
Approved by: https://github.com/anijain2305, https://github.com/mlazos
This commit is contained in:
William Wen
2025-10-10 16:50:53 -07:00
committed by PyTorch MergeBot
parent 5ad7611b52
commit 5dbca58bd0
3 changed files with 22 additions and 26 deletions

View File

@ -5,13 +5,15 @@
#if IS_PYTHON_3_15_PLUS || (IS_PYTHON_3_14_PLUS && defined(_WIN32))
const uint8_t* THP_PyOpcode_Caches = NULL;
const int THP_PyOpcode_Caches_size = 0;
int THP_PyOpcode_Caches_size = 0;
void THP_PyThreadState_PopFrame(
PyThreadState* tstate,
_PyInterpreterFrame* frame) {}
void THP_PyFrame_Clear(_PyInterpreterFrame* frame) {}
void init_THPCaches() {}
#else
#if IS_PYTHON_3_11_PLUS
@ -481,16 +483,13 @@ void THP_PyThreadState_PopFrame(
#endif
#if IS_PYTHON_3_11_PLUS
const uint8_t* THP_PyOpcode_Caches = _PyOpcode_Caches;
const int THP_PyOpcode_Caches_size = sizeof(_PyOpcode_Caches) / sizeof(uint8_t);
#else
const uint8_t* THP_PyOpcode_Caches = NULL;
const int THP_PyOpcode_Caches_size = 0;
int THP_PyOpcode_Caches_size = 0;
void init_THPCaches() {
#if IS_PYTHON_3_11_PLUS
THP_PyOpcode_Caches = _PyOpcode_Caches;
THP_PyOpcode_Caches_size = sizeof(_PyOpcode_Caches) / sizeof(uint8_t);
#endif
}
#endif // IS_PYTHON_3_15_PLUS

View File

@ -28,13 +28,13 @@ void THP_PyThreadState_PopFrame(
// pointers to _PyOpcode_Caches for C++
#ifdef __cplusplus
extern "C" const uint8_t* THP_PyOpcode_Caches;
extern "C" const int THP_PyOpcode_Caches_size;
#else
extern "C" {
#endif
extern const uint8_t* THP_PyOpcode_Caches;
extern const int THP_PyOpcode_Caches_size;
extern int THP_PyOpcode_Caches_size;
void init_THPCaches();
#ifdef __cplusplus
} // extern "C"
#endif

View File

@ -21,18 +21,8 @@ PYBIND11_MAKE_OPAQUE(std::vector<uint8_t>)
namespace torch::dynamo {
#if IS_PYTHON_3_11_PLUS
std::vector<uint8_t> _PyOpcode_Caches_vec(
THP_PyOpcode_Caches,
THP_PyOpcode_Caches + THP_PyOpcode_Caches_size);
#else
std::vector<uint8_t> _PyOpcode_Caches_vec;
#endif
using torch::dynamo::autograd::torch_c_dynamo_compiled_autograd_init;
namespace {
@ -265,6 +255,13 @@ void initDynamoBindings(PyObject* torch) {
m.def("_load_precompile_entry", &_load_precompile_entry);
m.def("_debug_get_precompile_entries", &_debug_get_precompile_entries);
py::bind_vector<std::vector<uint8_t>>(m, "VectorUInt8");
init_THPCaches();
if (THP_PyOpcode_Caches != nullptr) {
_PyOpcode_Caches_vec.insert(
_PyOpcode_Caches_vec.end(),
THP_PyOpcode_Caches,
THP_PyOpcode_Caches + THP_PyOpcode_Caches_size);
}
m.attr("py_opcode_caches") = _PyOpcode_Caches_vec;
m.def("code_framelocals_names", &code_framelocals_names);
_register_functions(dynamo);