Skip to content

Commit 8d383e2

Browse files
committed
Update torch patch
1 parent 0e5562c commit 8d383e2

File tree

1 file changed

+179
-53
lines changed

1 file changed

+179
-53
lines changed

graalpython/lib-graalpython/patches/torch/torch-2.2.1.patch

Lines changed: 179 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,19 @@ index 68fd53d0..2871915e 100644
317317
auto *tb = reinterpret_cast<PyTracebackObject *>(m_trace.ptr());
318318

319319
// Get the deepest trace possible.
320+
diff --git a/torch/_dynamo/types.py b/torch/_dynamo/types.py
321+
index 0c1deddd..f60e9d72 100644
322+
--- a/torch/_dynamo/types.py
323+
+++ b/torch/_dynamo/types.py
324+
@@ -6,7 +6,7 @@ from typing import Any, Callable, Dict, List, NamedTuple, Optional, Protocol, Un
325+
from typing_extensions import TypeAlias
326+
327+
328+
-if sys.version_info >= (3, 11):
329+
+if sys.version_info >= (3, 11) and not sys.implementation.name == 'graalpy':
330+
from torch._C._dynamo import eval_frame
331+
332+
DynamoFrameType: TypeAlias = eval_frame._PyInterpreterFrame
320333
diff --git a/torch/_tensor_str.py b/torch/_tensor_str.py
321334
index 1293a0fd..dfb4385a 100644
322335
--- a/torch/_tensor_str.py
@@ -331,7 +344,7 @@ index 1293a0fd..dfb4385a 100644
331344
+ torch._C._DisableFuncTorch():
332345
return _str_intern(self, tensor_contents=tensor_contents)
333346
diff --git a/torch/csrc/Module.cpp b/torch/csrc/Module.cpp
334-
index a8bf9903..67f89057 100644
347+
index a8bf9903..0168b101 100644
335348
--- a/torch/csrc/Module.cpp
336349
+++ b/torch/csrc/Module.cpp
337350
@@ -341,46 +341,16 @@ PyObject* THPModule_addDocStr(PyObject* _unused, PyObject* args) {
@@ -390,16 +403,6 @@ index a8bf9903..67f89057 100644
390403
}
391404

392405
Py_INCREF(obj);
393-
@@ -1410,7 +1380,8 @@ PyObject* initModule() {
394-
torch::jit::initJITBindings(module);
395-
torch::monitor::initMonitorBindings(module);
396-
torch::impl::dispatch::initDispatchBindings(module);
397-
- torch::dynamo::initDynamoBindings(module);
398-
+ // GraalPy change: dynamo uses bytecode parsing
399-
+ // torch::dynamo::initDynamoBindings(module);
400-
torch::functorch::impl::initFuncTorchBindings(module);
401-
torch::throughput_benchmark::initThroughputBenchmarkBindings(module);
402-
torch::autograd::initReturnTypes(module);
403406
diff --git a/torch/csrc/autograd/python_variable_indexing.cpp b/torch/csrc/autograd/python_variable_indexing.cpp
404407
index 3b128027..b670f3f2 100644
405408
--- a/torch/csrc/autograd/python_variable_indexing.cpp
@@ -505,68 +508,191 @@ index 688ea19b..5c9c1ecb 100644
505508
}
506509
stop = clip_val(stop);
507510
diff --git a/torch/csrc/dynamo/cpython_defs.c b/torch/csrc/dynamo/cpython_defs.c
508-
index 5d249d99..d00c18bf 100644
511+
index 5d249d99..9f83824e 100644
509512
--- a/torch/csrc/dynamo/cpython_defs.c
510513
+++ b/torch/csrc/dynamo/cpython_defs.c
511-
@@ -1,3 +1,4 @@
514+
@@ -24,6 +24,7 @@
515+
#undef _PyGC_FINALIZED
516+
#endif
517+
518+
+#if 0 // GraalPy change
519+
#define Py_BUILD_CORE
520+
#include <internal/pycore_pystate.h>
521+
#define NEED_OPCODE_TABLES // To get _PyOpcode_Deopt
522+
@@ -31,6 +32,7 @@
523+
#undef NEED_OPCODE_TABLES
524+
#undef Py_BUILD_CORE
525+
#include <internal/pycore_frame.h>
526+
+#endif // GraalPy change
527+
528+
// As a simple way to reduce the impact of ABI changes on the CPython side, this check forces
529+
// us to manually re-check that the function didn't change on the next major version
530+
@@ -39,6 +41,7 @@
531+
// #error "Please ensure that the functions below still match the CPython implementation for 3.12"
532+
#endif
533+
534+
+#if 0 // GraalPy change
535+
// https://github.com/python/cpython/blob/a7715ccfba5b86ab09f86ec56ac3755c93b46b48/Objects/frameobject.c#L1079
536+
static int
537+
THP_PyFrame_OpAlreadyRan(_PyInterpreterFrame *frame, int opcode, int oparg)
538+
@@ -64,10 +67,15 @@ THP_PyFrame_OpAlreadyRan(_PyInterpreterFrame *frame, int opcode, int oparg)
539+
}
540+
return 0;
541+
}
542+
+#endif
543+
544+
// https://github.com/python/cpython/blob/a7715ccfba5b86ab09f86ec56ac3755c93b46b48/Objects/frameobject.c#L1182
545+
int
546+
THP_PyFrame_FastToLocalsWithError(_PyInterpreterFrame *frame) {
547+
+ // GraalPy change
548+
+ PyErr_SetString(PyExc_NotImplementedError, "dynamo compilation is not supported on GraalPy");
549+
+ return -1;
512550
+#if 0
513-
#include <torch/csrc/dynamo/cpython_defs.h>
551+
/* Merge fast locals into f->f_locals */
552+
PyObject *locals = NULL;
553+
PyObject **fast = NULL;
554+
@@ -162,6 +170,7 @@ THP_PyFrame_FastToLocalsWithError(_PyInterpreterFrame *frame) {
555+
}
556+
}
557+
return 0;
558+
+#endif
559+
}
514560

515-
#ifdef _WIN32
516-
@@ -360,3 +361,4 @@ THP_PyFrame_Clear(_PyInterpreterFrame *frame)
561+
// e.g. COPY_FIELD(op, o, globals) becomes
562+
@@ -328,6 +337,7 @@ THP_take_ownership(PyFrameObject *f, _PyInterpreterFrame *frame)
563+
void
564+
THP_PyFrame_Clear(_PyInterpreterFrame *frame)
565+
{
566+
+#if 0 // GraalPy change
567+
/* It is the responsibility of the owning generator/coroutine
568+
* to have cleared the enclosing generator, if any. */
569+
CHECK(frame->owner != FRAME_OWNED_BY_GENERATOR ||
570+
@@ -357,6 +367,7 @@ THP_PyFrame_Clear(_PyInterpreterFrame *frame)
571+
Py_DECREF(frame->f_func);
572+
#endif
573+
Py_DECREF(frame->f_code);
574+
+#endif // GraalPy change
517575
}
518576

519577
#endif
520-
+#endif
521578
diff --git a/torch/csrc/dynamo/eval_frame.c b/torch/csrc/dynamo/eval_frame.c
522-
index 0245b192..f195d97d 100644
579+
index 0245b192..22e58dd5 100644
523580
--- a/torch/csrc/dynamo/eval_frame.c
524581
+++ b/torch/csrc/dynamo/eval_frame.c
525-
@@ -1,3 +1,4 @@
526-
+#if 0 // GraalPy change
582+
@@ -1,8 +1,8 @@
527583
#define PY_SSIZE_T_CLEAN
528584
#include <torch/csrc/dynamo/cpp_shim.h>
529-
#include <torch/csrc/dynamo/cpython_defs.h>
530-
@@ -610,7 +611,7 @@ static inline PyObject* call_callback(
531-
532-
// remember to update the type signature for DynamoCallbackFn.__call__ in torch/_dynamo/types.py
533-
// if this function changes
534-
-#if IS_PYTHON_3_11_PLUS
535-
+#if IS_PYTHON_3_11_PLUS && !defined(GRAALVM_PYTHON)
536-
THPPyInterpreterFrame* frame = THPPyInterpreterFrame_New(_frame);
537-
if (frame == NULL) {
538-
return NULL;
539-
@@ -697,7 +698,7 @@ inline static PyObject* eval_custom_code_impl(
540-
DEBUG_NULL_CHECK(frame);
541-
DEBUG_NULL_CHECK(code);
585+
-#include <torch/csrc/dynamo/cpython_defs.h>
586+
+//#include <torch/csrc/dynamo/cpython_defs.h>
587+
#include <torch/csrc/utils/python_compat.h>
588+
-#include <opcode.h>
589+
+//#include <opcode.h>
590+
#include <stdbool.h>
591+
592+
// Problem in CPython includes when mixing core and non-core build
593+
@@ -12,6 +12,7 @@
594+
#undef _PyGC_FINALIZED
595+
#endif
542596

543-
- #if IS_PYTHON_3_11_PLUS
544-
+ #if IS_PYTHON_3_11_PLUS && !defined(GRAALVM_PYTHON)
597+
+#if 0
598+
// see https://bugs.python.org/issue35886
599+
#if PY_VERSION_HEX >= 0x03080000
600+
#define Py_BUILD_CORE
601+
@@ -131,6 +132,7 @@ THPPyInterpreterFrame* THPPyInterpreterFrame_New(_PyInterpreterFrame* frame) {
545602

546-
// Generate Python function object and _PyInterpreterFrame in a way similar to
547-
// https://github.com/python/cpython/blob/e715da6db1d1d70cd779dc48e1ba8110c51cc1bf/Python/ceval.c#L1130
548-
@@ -1175,3 +1176,6 @@ PyObject* torch_c_dynamo_eval_frame_init(void) {
603+
#define THP_PyFrame_FastToLocalsWithError PyFrame_FastToLocalsWithError
604+
#endif
605+
+#endif
549606

550-
return module;
607+
#ifdef _WIN32
608+
#define unlikely(x) (x)
609+
@@ -199,6 +201,7 @@ inline static void eval_frame_callback_set(PyObject* obj) {
610+
PyThread_tss_set(&eval_frame_callback_key, obj);
551611
}
552-
+#endif // GraalPy change
553-
+#include <stdbool.h>
554-
+bool is_dynamo_compiling = false;
555-
diff --git a/torch/csrc/dynamo/init.cpp b/torch/csrc/dynamo/init.cpp
556-
index 1889ca6f..7f7fbbe8 100644
557-
--- a/torch/csrc/dynamo/init.cpp
558-
+++ b/torch/csrc/dynamo/init.cpp
559-
@@ -1,3 +1,5 @@
560-
+// GraalPy change
612+
613+
+#if 0
614+
static PyObject* _custom_eval_frame_shim(
615+
PyThreadState* tstate,
616+
THP_EVAL_API_FRAME_OBJECT* frame,
617+
@@ -284,6 +287,7 @@ inline static const char* get_frame_name(THP_EVAL_API_FRAME_OBJECT* frame) {
618+
DEBUG_CHECK(PyUnicode_Check(frame->f_code->co_name));
619+
return PyUnicode_AsUTF8(frame->f_code->co_name);
620+
}
621+
+#endif
622+
623+
typedef PyObject FrameState;
624+
/*
625+
@@ -602,6 +606,7 @@ PyObject* _debug_get_cache_entry_list(PyObject* self, PyObject* args) {
626+
return (PyObject*)current_node;
627+
}
628+
629+
+#if 0
630+
static inline PyObject* call_callback(
631+
PyObject* callable,
632+
THP_EVAL_API_FRAME_OBJECT* _frame,
633+
@@ -644,7 +649,6 @@ static PyObject* call_guard_fail_hook(
634+
(Py_ssize_t)index,
635+
(e->next == (CacheEntry*)Py_None ? Py_True : Py_False));
636+
}
637+
-
638+
// Return value: borrowed reference
639+
// Is either Py_None or a PyCodeObject
640+
static PyObject* lookup(CacheEntry* e, THP_EVAL_API_FRAME_OBJECT *frame, CacheEntry* prev, size_t index) {
641+
@@ -1033,6 +1037,7 @@ static PyObject* decrement_working_threads(PyThreadState* tstate) {
642+
}
643+
Py_RETURN_NONE;
644+
}
645+
+#endif
646+
647+
static PyObject* set_eval_frame(PyObject* new_callback, PyThreadState* tstate) {
648+
// Change the eval frame callback and return the old one
649+
@@ -1045,9 +1050,13 @@ static PyObject* set_eval_frame(PyObject* new_callback, PyThreadState* tstate) {
650+
Py_INCREF(old_callback);
651+
652+
if (old_callback != Py_None && new_callback == Py_None) {
653+
- decrement_working_threads(tstate);
654+
+ // GraalPy change
655+
+ PyErr_SetString(PyExc_NotImplementedError, "dynamo compilation is not supported on GraalPy");
656+
+ return NULL;
657+
} else if (old_callback == Py_None && new_callback != Py_None) {
658+
- increment_working_threads(tstate);
659+
+ // GraalPy change
660+
+ PyErr_SetString(PyExc_NotImplementedError, "dynamo compilation is not supported on GraalPy");
661+
+ return NULL;
662+
}
663+
664+
Py_INCREF(new_callback);
665+
@@ -1135,12 +1144,14 @@ static struct PyModuleDef _module = {
666+
667+
668+
PyObject* torch_c_dynamo_eval_frame_init(void) {
561669
+#if 0
562-
#include <torch/csrc/dynamo/init.h>
670+
extra_index = _PyEval_RequestCodeExtraIndex(destroy_extra_state);
671+
if (extra_index < 0) {
672+
PyErr_SetString(PyExc_RuntimeError,
673+
"dynamo: unable to register extra index");
674+
return NULL;
675+
}
676+
+#endif
563677

564-
#include <torch/csrc/Exceptions.h>
565-
@@ -38,3 +40,4 @@ void initDynamoBindings(PyObject* torch) {
678+
int result = PyThread_tss_create(&eval_frame_callback_key);
679+
CHECK(result == 0);
680+
@@ -1153,6 +1164,7 @@ PyObject* torch_c_dynamo_eval_frame_init(void) {
681+
return NULL;
682+
}
566683

567-
} // namespace dynamo
568-
} // namespace torch
684+
+#if 0
685+
#if IS_PYTHON_3_11_PLUS
686+
if (PyType_Ready(&THPPyInterpreterFrameType) < 0) {
687+
return NULL;
688+
@@ -1162,6 +1174,7 @@ PyObject* torch_c_dynamo_eval_frame_init(void) {
689+
return NULL;
690+
}
691+
#endif
569692
+#endif
693+
694+
695+
if (PyType_Ready(&CacheEntryType) < 0) {
570696
diff --git a/torch/csrc/jit/python/python_tracer.cpp b/torch/csrc/jit/python/python_tracer.cpp
571697
index bdc62d33..e16c3715 100644
572698
--- a/torch/csrc/jit/python/python_tracer.cpp

0 commit comments

Comments
 (0)