1- From d206a167fea7930a0f1909d2c854643e001fe967 Mon Sep 17 00:00:00 2001
1+ From 0194b00a93f145c5539767689c112e04c8bf2b51 Mon Sep 17 00:00:00 2001
22From: Mohaned Qunaibit <mohaned.qunaibit@oracle.com>
33Date: Mon, 30 Oct 2023 17:19:59 +0300
44Subject: [PATCH] patch tensorflow 2.14.0
55
66---
77 WORKSPACE | 7 +-
8- graalpy_build.py | 253 ++++++++++++++++++
9- graalpy_patch.py | 105 ++++++++
8+ graalpy_build.py | 260 ++++++++++++++++++
9+ graalpy_patch.py | 105 +++++++
1010 pyproject.toml | 50 ++++
11- rules_python-0.23.1.patch | 182 +++++++++++++
11+ rules_python-0.23.1.patch | 182 ++++++++++++
12+ .../dtensor/python/tests/test_backend_util.py | 2 +-
13+ tensorflow/python/autograph/impl/api_test.py | 3 +
1214 .../python/client/tf_session_wrapper.cc | 4 +-
15+ .../data/kernel_tests/memory_cleanup_test.py | 4 +
16+ .../python/distribute/multi_process_lib.py | 3 +
17+ .../eager/memory_tests/memory_test_util.py | 4 +
1318 tensorflow/python/eager/pywrap_tfe_src.cc | 11 +-
1419 tensorflow/python/framework/dtypes.py | 2 +-
20+ .../python/framework/error_interpolation.py | 3 +-
21+ tensorflow/python/framework/errors_test.py | 4 +
1522 .../python_api_dispatcher_wrapper.cc | 2 +-
23+ .../python/framework/python_memory_checker.py | 4 +
24+ tensorflow/python/framework/test_util.py | 8 +
1625 .../tools/api/generator/create_python_api.py | 2 +-
1726 tensorflow/python/util/module_wrapper.py | 55 +++-
27+ tensorflow/python/util/tf_stack_test.py | 3 +
1828 tensorflow/tools/git/gen_git_source.py | 3 +-
1929 tensorflow/tools/pip_package/setup.py | 2 +-
2030 .../devel.usertools/squash_testlogs.py | 13 +-
@@ -23,7 +33,7 @@ Subject: [PATCH] patch tensorflow 2.14.0
2333 tf_build_backend.py | 57 ++++
2434 third_party/protobuf/protobuf.patch | 2 +-
2535 third_party/repo.bzl | 8 +-
26- 19 files changed, 746 insertions(+), 25 deletions(-)
36+ 29 files changed, 789 insertions(+), 27 deletions(-)
2737 create mode 100644 graalpy_build.py
2838 create mode 100644 graalpy_patch.py
2939 create mode 100644 pyproject.toml
@@ -64,10 +74,10 @@ index fb3af8a2bea..44ddffba73c 100644
6474 load("@pypi//:requirements.bzl", "install_deps")
6575diff --git a/graalpy_build.py b/graalpy_build.py
6676new file mode 100644
67- index 00000000000..3da98203ff8
77+ index 00000000000..09290d0fd26
6878--- /dev/null
6979+++ b/graalpy_build.py
70- @@ -0,0 +1,253 @@
80+ @@ -0,0 +1,260 @@
7181+
7282+ import subprocess
7383+ import os
@@ -79,7 +89,8 @@ index 00000000000..3da98203ff8
7989+
8090+ TF_ROOT = os.path.dirname(__file__)
8191+ INTERPRETER_HOME = sys.base_prefix
82- + WHL_DIR_PATH = TF_ROOT + "/wheel_build"
92+ + WHL_DIR_PATH = os.path.join(TF_ROOT, "wheel_build")
93+ + TRACK_DIR = os.path.join(TF_ROOT, "track_test")
8394+
8495+ for e in os.environ:
8596+ print(e)
@@ -99,7 +110,7 @@ index 00000000000..3da98203ff8
99110+ "flatbuffers",
100111+ "gast",
101112+ "google-pasta",
102- + "keras",
113+ + "keras==2.14.0 ",
103114+ "libclang",
104115+ "numpy",
105116+ "protobuf",
@@ -110,7 +121,7 @@ index 00000000000..3da98203ff8
110121+ "typing-extensions",
111122+ "ml_dtypes==0.2.0",
112123+ "opt-einsum",
113- + # "h5py", # require libhdf5-dev (apt-get install libhdf5-dev)
124+ + "h5py==3.10.0 ", # require libhdf5-dev (apt-get install libhdf5-dev)
114125+ ]
115126+
116127+ PIP_INSTALL = [
@@ -299,12 +310,18 @@ index 00000000000..3da98203ff8
299310+ run(PIP_INSTALL + ["--force-reinstall", "--no-deps", find_tf_whl(args.whl_path)])
300311+
301312+ if args.test:
313+ + if not os.path.exists(TRACK_DIR):
314+ + os.mkdir(TRACK_DIR)
302315+ BAZEL_TEST = [
303316+ "test",
304317+ ] + BAZEL_SYSTEM_LIMIT + [
305318+ ] + BAZEL_EXTRA_OPT + [
319+ + # "--test_sharding_strategy=disabled",
320+ + # "--test_output=errors", "--verbose_failures=true", "--keep_going",
306321+ "--repo_env=VENV_SITE_PACKAGES=%s" % VENV_SITE_PACKAGES,
307322+ "--repo_env=INTERPRETER_HOME=%s" % INTERPRETER_HOME,
323+ + "--test_env=TRACK_DIR=%s" % TRACK_DIR,
324+ + "--flaky_test_attempts=3",
308325+ # "--repo_env=TF_PYTHON_VERSION=%s" % '3.10',
309326+ "-k",
310327+ "//tensorflow/python/..."
@@ -324,7 +341,7 @@ index 00000000000..3da98203ff8
324341\ No newline at end of file
325342diff --git a/graalpy_patch.py b/graalpy_patch.py
326343new file mode 100644
327- index 00000000000..728d8a8863a
344+ index 00000000000..dc67a9fc453
328345--- /dev/null
329346+++ b/graalpy_patch.py
330347@@ -0,0 +1,105 @@
@@ -363,7 +380,7 @@ index 00000000000..728d8a8863a
363380+ "termcolor-1.1.0": "",
364381+ "astunparse-1.6.3/lib": "",
365382+ "FXdiv-63058eff77e11aa15bf531df5dd34395ec3017c8": "",
366- + "dill-0.3.6": "", # str(os.path.join(PATCHES_BASE_DIR, 'dill', 'dill-0.3.6 .patch')),
383+ + "dill-0.3.6": str(os.path.join(PATCHES_BASE_DIR, 'dill', 'dill.patch')),
367384+ "farmhash-0d859a811870d10f53a594927d0d0b97573ad06d": "",
368385+ "pasta-0.2.0": "",
369386+ "giflib-5.2.1": "",
@@ -491,7 +508,7 @@ index 00000000000..841b726f3fe
491508+ backend-path = ["."]
492509diff --git a/rules_python-0.23.1.patch b/rules_python-0.23.1.patch
493510new file mode 100644
494- index 00000000000..cb8c98b661b
511+ index 00000000000..442e3c69c7d
495512--- /dev/null
496513+++ b/rules_python-0.23.1.patch
497514@@ -0,0 +1,182 @@
@@ -578,14 +595,14 @@ index 00000000000..cb8c98b661b
578595+ build_file.write(contents)
579596+
580597+ +def get_package(package_spec):
581- + + try:
582- + + pkg = package_spec.split('==')[0]
583- + + if pkg in set(os.listdir(os.path.join(__graalpython__.core_home, "patches"))) :
584- + + print('[GraalPy] replacing %s requirement with patched %s' % (package_spec, pkg))
585- + + return pkg
586- + + except:
587- + + pass
588- + + return package_spec
598+ + + replacements = {"h5py": "h5py==3.10.0", "urllib3": "urllib3==1.26.18", "grpcio": "grpcio==1.56.2"}
599+ + + pkg = package_spec.split('==')[0]
600+ + + if pkg in replacements :
601+ + + print('[GraalPy] replacing %s requirement with patched %s' % (package_spec, replacements[ pkg] ))
602+ + +
603+ + +
604+ + +
605+ + + return replacements.get(pkg, package_spec)
589606+ +
590607+ +def get_wheel(args, deserialized_args):
591608+ + package_spec = args.requirement.split()[0]
@@ -677,6 +694,33 @@ index 00000000000..cb8c98b661b
677694+ patches = rctx.attr.patches
678695+ if patches:
679696+ for patch in patches:
697+ diff --git a/tensorflow/dtensor/python/tests/test_backend_util.py b/tensorflow/dtensor/python/tests/test_backend_util.py
698+ index 02fc82a71b7..58b902b469c 100644
699+ --- a/tensorflow/dtensor/python/tests/test_backend_util.py
700+ +++ b/tensorflow/dtensor/python/tests/test_backend_util.py
701+ @@ -71,7 +71,7 @@ def slice_host_devices_for_multiworker(num_clients, client_id, ports):
702+
703+
704+ def get_mp_context():
705+ - return multiprocessing.get_context('forkserver')
706+ + return multiprocessing.get_context('spawn')
707+
708+
709+ def handle_test_main(main, *args, **kwargs):
710+ diff --git a/tensorflow/python/autograph/impl/api_test.py b/tensorflow/python/autograph/impl/api_test.py
711+ index 1332240d9df..bf8b4a0926f 100644
712+ --- a/tensorflow/python/autograph/impl/api_test.py
713+ +++ b/tensorflow/python/autograph/impl/api_test.py
714+ @@ -57,6 +57,9 @@ global_n = 2
715+
716+ DEFAULT_RECURSIVE = converter.ConversionOptions(recursive=True)
717+
718+ + if sys.implementation.name == 'graalpy':
719+ + # GraalPy we don't have control over the jvm's gc
720+ + gc.get_objects = lambda: []
721+
722+ class TestResource:
723+
680724diff --git a/tensorflow/python/client/tf_session_wrapper.cc b/tensorflow/python/client/tf_session_wrapper.cc
681725index 61551f0f8c1..d19edb3dd59 100644
682726--- a/tensorflow/python/client/tf_session_wrapper.cc
@@ -699,6 +743,50 @@ index 61551f0f8c1..d19edb3dd59 100644
699743 copy.append(op);
700744 }
701745 return copy;
746+ diff --git a/tensorflow/python/data/kernel_tests/memory_cleanup_test.py b/tensorflow/python/data/kernel_tests/memory_cleanup_test.py
747+ index afd5e18eecd..4ec8b9a6643 100644
748+ --- a/tensorflow/python/data/kernel_tests/memory_cleanup_test.py
749+ +++ b/tensorflow/python/data/kernel_tests/memory_cleanup_test.py
750+ @@ -36,6 +36,10 @@ try:
751+ except ImportError:
752+ memory_profiler = None
753+
754+ + import sys
755+ + if sys.implementation.name == 'graalpy':
756+ + # GraalPy we don't have control over the jvm's gc
757+ + gc.get_objects = lambda: []
758+
759+ class MemoryCleanupTest(test_base.DatasetTestBase, parameterized.TestCase):
760+
761+ diff --git a/tensorflow/python/distribute/multi_process_lib.py b/tensorflow/python/distribute/multi_process_lib.py
762+ index fa1e44fbf58..e4bfb8577e3 100644
763+ --- a/tensorflow/python/distribute/multi_process_lib.py
764+ +++ b/tensorflow/python/distribute/multi_process_lib.py
765+ @@ -31,6 +31,9 @@ def is_oss():
766+
767+
768+ def _is_enabled():
769+ + if sys.implementation.name == 'graalpy':
770+ + logging.info("GraalPy doesn't support forking a jvm/svm process")
771+ + return False
772+ # Note that flags may not be parsed at this point and simply importing the
773+ # flags module causes a variety of unusual errors.
774+ tpu_args = [arg for arg in sys.argv if arg.startswith('--tpu')]
775+ diff --git a/tensorflow/python/eager/memory_tests/memory_test_util.py b/tensorflow/python/eager/memory_tests/memory_test_util.py
776+ index 7d584661b34..cf5370a51cf 100644
777+ --- a/tensorflow/python/eager/memory_tests/memory_test_util.py
778+ +++ b/tensorflow/python/eager/memory_tests/memory_test_util.py
779+ @@ -26,6 +26,10 @@ try:
780+ except ImportError:
781+ memory_profiler = None
782+
783+ + import sys
784+ + if sys.implementation.name == 'graalpy':
785+ + # GraalPy we don't have control over the jvm's gc
786+ + gc.get_objects = lambda: []
787+
788+ def _instance_count_by_class():
789+ counter = collections.Counter()
702790diff --git a/tensorflow/python/eager/pywrap_tfe_src.cc b/tensorflow/python/eager/pywrap_tfe_src.cc
703791index 1981bedc2a4..9e356d1d588 100644
704792--- a/tensorflow/python/eager/pywrap_tfe_src.cc
@@ -748,6 +836,42 @@ index 9f17099424f..a3d8626658e 100644
748836
749837 def __init__(self, type_enum, handle_data=None):
750838 super().__init__(type_enum)
839+ diff --git a/tensorflow/python/framework/error_interpolation.py b/tensorflow/python/framework/error_interpolation.py
840+ index 76c727a5fe5..b6f74197a29 100644
841+ --- a/tensorflow/python/framework/error_interpolation.py
842+ +++ b/tensorflow/python/framework/error_interpolation.py
843+ @@ -23,6 +23,7 @@ import os
844+ import re
845+ import site
846+ import traceback
847+ + import sys
848+
849+ from tensorflow.python.util import tf_stack
850+
851+ @@ -354,7 +355,7 @@ def _compute_field_dict(op):
852+ device_summary = _compute_device_assignment_summary_from_op(op)
853+ combined_summary = "\n".join([colocation_summary, device_summary])
854+
855+ - if op.traceback is None:
856+ + if op.traceback is None or sys.implementation.name == 'graalpy':
857+ # Some ops synthesized on as part of function or control flow definition
858+ # do not have tracebacks.
859+ filename = "<unknown>"
860+ diff --git a/tensorflow/python/framework/errors_test.py b/tensorflow/python/framework/errors_test.py
861+ index c2124cb7893..14ec9132453 100644
862+ --- a/tensorflow/python/framework/errors_test.py
863+ +++ b/tensorflow/python/framework/errors_test.py
864+ @@ -27,6 +27,10 @@ from tensorflow.python.lib.io import _pywrap_file_io
865+ from tensorflow.python.platform import test
866+ from tensorflow.python.util import compat
867+
868+ + import sys
869+ + if sys.implementation.name == 'graalpy':
870+ + # GraalPy we don't have control over the jvm's gc
871+ + gc.get_objects = lambda: []
872+
873+ class ErrorsTest(test.TestCase):
874+
751875diff --git a/tensorflow/python/framework/python_api_dispatcher_wrapper.cc b/tensorflow/python/framework/python_api_dispatcher_wrapper.cc
752876index d3f688578a7..071fadb1e45 100644
753877--- a/tensorflow/python/framework/python_api_dispatcher_wrapper.cc
@@ -761,6 +885,47 @@ index d3f688578a7..071fadb1e45 100644
761885 if (!PyType_Check(cls.ptr())) {
762886 throw py::type_error("`*py_classes` must be a tuple of types.");
763887 }
888+ diff --git a/tensorflow/python/framework/python_memory_checker.py b/tensorflow/python/framework/python_memory_checker.py
889+ index aa0d2023585..3f5f773a361 100644
890+ --- a/tensorflow/python/framework/python_memory_checker.py
891+ +++ b/tensorflow/python/framework/python_memory_checker.py
892+ @@ -25,6 +25,10 @@ from tensorflow.python.framework import _python_memory_checker_helper
893+ from tensorflow.python.platform import tf_logging as logging
894+ from tensorflow.python.profiler import trace
895+
896+ + import sys
897+ + if sys.implementation.name == 'graalpy':
898+ + # GraalPy we don't have control over the jvm's gc
899+ + gc.get_objects = lambda: []
900+
901+ def _get_typename(obj):
902+ """Return human readable pretty type name string."""
903+ diff --git a/tensorflow/python/framework/test_util.py b/tensorflow/python/framework/test_util.py
904+ index 906fac99b7e..3ea07d28a31 100644
905+ --- a/tensorflow/python/framework/test_util.py
906+ +++ b/tensorflow/python/framework/test_util.py
907+ @@ -92,6 +92,10 @@ from tensorflow.python.util.compat import collections_abc
908+ from tensorflow.python.util.protobuf import compare
909+ from tensorflow.python.util.tf_export import tf_export
910+
911+ + import sys
912+ + if sys.implementation.name == 'graalpy':
913+ + # GraalPy we don't have control over the jvm's gc
914+ + gc.get_objects = lambda: []
915+
916+ # If the below import is made available through the BUILD rule, then this
917+ # function is overridden and will instead return True and cause Tensorflow
918+ @@ -804,6 +808,10 @@ def assert_no_new_tensors(f):
919+ def decorator(self, **kwargs):
920+ """Finds existing Tensors, runs the test, checks for new Tensors."""
921+
922+ + if sys.implementation.name == 'graalpy':
923+ + # GraalPy we don't have control over the jvm's gc
924+ + gc.get_objects = lambda: []
925+ +
926+ def _is_tensorflow_object(obj):
927+ try:
928+ return isinstance(obj,
764929diff --git a/tensorflow/python/tools/api/generator/create_python_api.py b/tensorflow/python/tools/api/generator/create_python_api.py
765930index 742cc879d0e..cdc65b36473 100644
766931--- a/tensorflow/python/tools/api/generator/create_python_api.py
@@ -844,6 +1009,27 @@ index fcf7225d683..f0205b919aa 100644
8441009 _PER_MODULE_WARNING_LIMIT = 1
8451010 compat_v1_usage_gauge = monitoring.BoolGauge('/tensorflow/api/compat/v1',
8461011 'compat.v1 usage')
1012+ diff --git a/tensorflow/python/util/tf_stack_test.py b/tensorflow/python/util/tf_stack_test.py
1013+ index 8b607acb30a..d60857507e3 100644
1014+ --- a/tensorflow/python/util/tf_stack_test.py
1015+ +++ b/tensorflow/python/util/tf_stack_test.py
1016+ @@ -17,6 +17,7 @@
1017+ from tensorflow.python.platform import test
1018+ from tensorflow.python.util import tf_stack
1019+
1020+ + import sys
1021+
1022+ class TFStackTest(test.TestCase):
1023+
1024+ @@ -40,6 +41,8 @@ class TFStackTest(test.TestCase):
1025+ self.assertEqual(hash(tuple(frame1)), hash(tuple(frame2)))
1026+
1027+ def testLastUserFrame(self):
1028+ + if sys.implementation.name == 'graalpy':
1029+ + return
1030+ trace = tf_stack.extract_stack()
1031+ frame = trace.last_user_frame()
1032+ self.assertRegex(repr(frame), 'testLastUserFrame')
8471033diff --git a/tensorflow/tools/git/gen_git_source.py b/tensorflow/tools/git/gen_git_source.py
8481034index 2a93649f84a..3ac09a7f30c 100644
8491035--- a/tensorflow/tools/git/gen_git_source.py
0 commit comments