From 52fd78ca16f9e6a53e684691a8b91b3916b0b809 Mon Sep 17 00:00:00 2001
From: Shahzad Malik Muzaffar <Shahzad.Malik.Muzaffar@cern.ch>
Date: Thu, 10 Feb 2022 20:09:28 +0100
Subject: [PATCH 1/7] [TF] Build with GPU support

---
 tensorflow-requires.file |  2 +-
 tensorflow-sources.file  | 18 ++++++++++++------
 2 files changed, 13 insertions(+), 7 deletions(-)

diff --git a/tensorflow-requires.file b/tensorflow-requires.file
index 86cca9d8870..7cd2e1c8152 100644
--- a/tensorflow-requires.file
+++ b/tensorflow-requires.file
@@ -2,5 +2,5 @@ Requires: python3 py3-numpy py3-mock py3-typing py3-typing-extensions
 Requires: py3-keras-applications py3-keras-preprocessing py3-future py3-wrapt py3-gast py3-setuptools
 Requires: py3-cython py3-protobuf py3-astor py3-six py3-termcolor py3-absl-py
 Requires: py3-opt-einsum py3-flatbuffers
-Requires: eigen protobuf zlib libpng libjpeg-turbo curl giflib sqlite grpc flatbuffers py3-pybind11
+Requires: cuda cudnn eigen protobuf zlib libpng libjpeg-turbo curl giflib sqlite grpc flatbuffers py3-pybind11
 BuildRequires: py3-wheel
diff --git a/tensorflow-sources.file b/tensorflow-sources.file
index 2bdaaa4d6c2..bfc8af3eb13 100644
--- a/tensorflow-sources.file
+++ b/tensorflow-sources.file
@@ -3,8 +3,9 @@ BuildRequires: bazel java-env git
 #Keep all requires in separate file, so that can be included in py-tensorflow too
 ## INCLUDE tensorflow-requires
 ## INCLUDE compilation_flags
+## INCLUDE cuda-flags
 
-%define tag         719e00b6f9553de2662b6df2c353d6934e941103
+%define tag         0073b9fe65c29226311e4d5010176b837415cf2c
 %define branch      cms/v%{realversion}
 %define github_user cms-externals
 Source: git+https://github.com/%{github_user}/tensorflow.git?obj=%{branch}/%{tag}&export=tensorflow-%{realversion}&output=/tensorflow-%{realversion}.tgz
@@ -19,7 +20,6 @@ Source: git+https://github.com/%{github_user}/tensorflow.git?obj=%{branch}/%{tag
 %define majorversion %(echo %realversion | cut -d . -f 1)
 
 %prep
-
 %setup -q -n tensorflow-%{realversion}
 sed -i -e 's|lib/python[^/]*/site-packages/|lib/python%{cms_python3_major_minor_version}/site-packages/|'  third_party/systemlibs/pybind11.BUILD
 
@@ -41,14 +41,20 @@ BAZEL_OPTS="$BAZEL_OPTS --copt=-mcpu=native --copt=-mtune=native"
 %endif
 %endif
 BAZEL_OPTS="$BAZEL_OPTS --config=%{build_type} --cxxopt=-std=c++17 %{makeprocesses}"
-BAZEL_OPTS="$BAZEL_OPTS --config=noaws --config=nogcp --config=nohdfs --config=nonccl"
-
+BAZEL_OPTS="$BAZEL_OPTS --config=cuda --config=noaws --config=nogcp --config=nohdfs --config=nonccl"
+
+export GCC_HOST_COMPILER_PREFIX=${GCC_ROOT}/bin
+export GCC_HOST_COMPILER_PATH=$(which gcc)
+export TF_CUDA_COMPUTE_CAPABILITIES=$(echo "compute_%{cuda_arch}" | sed 's|\s\s*|,compute_|g')
+export TF_CUDA_VERSION="$(echo ${CUDA_VERSION} | cut -f1,2 -d.)"
+export TF_CUDA_PATHS="${CUDA_ROOT},${CUDNN_ROOT}"
+export TF_CUDA_CLANG=0
 export TF_NEED_JEMALLOC=0
 export TF_NEED_HDFS=0
 export TF_NEED_GCP=0
 export TF_ENABLE_XLA=0
 export TF_NEED_OPENCL=0
-export TF_NEED_CUDA=0
+export TF_NEED_CUDA=1
 export TF_NEED_VERBS=0
 export TF_NEED_MKL=0
 export TF_NEED_MPI=0
@@ -97,7 +103,7 @@ if [ -d ../build ] ; then
   chmod -R u+w  ../build
   rm -rf ../build
 fi
-./configure
+cuda=Y ./configure
 
 #Generate Python wrappers so that we can use PYTHON*PATH env
 rm -rf %{_builddir}/cms-pytool ; mkdir %{_builddir}/cms-pytool

From 0ab8919d3e3987c0606315d040bb77f085d93193 Mon Sep 17 00:00:00 2001
From: Shahzad Malik Muzaffar <Shahzad.Malik.Muzaffar@cern.ch>
Date: Fri, 11 Feb 2022 16:18:48 +0100
Subject: [PATCH 2/7] disable gpu for slc7_aarch64 due to missing cudnn support

---
 tensorflow-requires.file |  9 ++++++++-
 tensorflow-sources.file  | 14 ++++++++++----
 2 files changed, 18 insertions(+), 5 deletions(-)

diff --git a/tensorflow-requires.file b/tensorflow-requires.file
index 7cd2e1c8152..d75e99d3616 100644
--- a/tensorflow-requires.file
+++ b/tensorflow-requires.file
@@ -2,5 +2,12 @@ Requires: python3 py3-numpy py3-mock py3-typing py3-typing-extensions
 Requires: py3-keras-applications py3-keras-preprocessing py3-future py3-wrapt py3-gast py3-setuptools
 Requires: py3-cython py3-protobuf py3-astor py3-six py3-termcolor py3-absl-py
 Requires: py3-opt-einsum py3-flatbuffers
-Requires: cuda cudnn eigen protobuf zlib libpng libjpeg-turbo curl giflib sqlite grpc flatbuffers py3-pybind11
+Requires: eigen protobuf zlib libpng libjpeg-turbo curl giflib sqlite grpc flatbuffers py3-pybind11
 BuildRequires: py3-wheel
+%if "%{cmsos}" != "slc7_aarch64"
+## INCLUDE cuda-flags
+%define enable_cuda 1
+Requires: cuda cudnn
+%else
+%define enable_cuda 0
+%endif
diff --git a/tensorflow-sources.file b/tensorflow-sources.file
index bfc8af3eb13..dc5a9a80985 100644
--- a/tensorflow-sources.file
+++ b/tensorflow-sources.file
@@ -3,7 +3,6 @@ BuildRequires: bazel java-env git
 #Keep all requires in separate file, so that can be included in py-tensorflow too
 ## INCLUDE tensorflow-requires
 ## INCLUDE compilation_flags
-## INCLUDE cuda-flags
 
 %define tag         0073b9fe65c29226311e4d5010176b837415cf2c
 %define branch      cms/v%{realversion}
@@ -20,6 +19,7 @@ Source: git+https://github.com/%{github_user}/tensorflow.git?obj=%{branch}/%{tag
 %define majorversion %(echo %realversion | cut -d . -f 1)
 
 %prep
+
 %setup -q -n tensorflow-%{realversion}
 sed -i -e 's|lib/python[^/]*/site-packages/|lib/python%{cms_python3_major_minor_version}/site-packages/|'  third_party/systemlibs/pybind11.BUILD
 
@@ -41,20 +41,26 @@ BAZEL_OPTS="$BAZEL_OPTS --copt=-mcpu=native --copt=-mtune=native"
 %endif
 %endif
 BAZEL_OPTS="$BAZEL_OPTS --config=%{build_type} --cxxopt=-std=c++17 %{makeprocesses}"
-BAZEL_OPTS="$BAZEL_OPTS --config=cuda --config=noaws --config=nogcp --config=nohdfs --config=nonccl"
+BAZEL_OPTS="$BAZEL_OPTS --config=noaws --config=nogcp --config=nohdfs --config=nonccl"
 
+%if %{enable_cuda}
+BAZEL_OPTS="$BAZEL_OPTS --config=cuda"
 export GCC_HOST_COMPILER_PREFIX=${GCC_ROOT}/bin
 export GCC_HOST_COMPILER_PATH=$(which gcc)
 export TF_CUDA_COMPUTE_CAPABILITIES=$(echo "compute_%{cuda_arch}" | sed 's|\s\s*|,compute_|g')
 export TF_CUDA_VERSION="$(echo ${CUDA_VERSION} | cut -f1,2 -d.)"
 export TF_CUDA_PATHS="${CUDA_ROOT},${CUDNN_ROOT}"
 export TF_CUDA_CLANG=0
+export cuda=Y
+export TF_NEED_CUDA=1
+%else
+export TF_NEED_CUDA=0
+%endif
 export TF_NEED_JEMALLOC=0
 export TF_NEED_HDFS=0
 export TF_NEED_GCP=0
 export TF_ENABLE_XLA=0
 export TF_NEED_OPENCL=0
-export TF_NEED_CUDA=1
 export TF_NEED_VERBS=0
 export TF_NEED_MKL=0
 export TF_NEED_MPI=0
@@ -103,7 +109,7 @@ if [ -d ../build ] ; then
   chmod -R u+w  ../build
   rm -rf ../build
 fi
-cuda=Y ./configure
+./configure
 
 #Generate Python wrappers so that we can use PYTHON*PATH env
 rm -rf %{_builddir}/cms-pytool ; mkdir %{_builddir}/cms-pytool

From 3a0f6df3d3c01de9abbb79e20d1a414a47aada51 Mon Sep 17 00:00:00 2001
From: Malik Shahzad Muzaffar <shahzad.malik.muzaffar@cern.ch>
Date: Sun, 20 Feb 2022 23:19:29 +0100
Subject: [PATCH 3/7] Update tensorflow-sources.file

---
 tensorflow-sources.file | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tensorflow-sources.file b/tensorflow-sources.file
index dc5a9a80985..9fa39e58a0a 100644
--- a/tensorflow-sources.file
+++ b/tensorflow-sources.file
@@ -40,7 +40,7 @@ BAZEL_OPTS="$BAZEL_OPTS $(echo %{arch_build_flags} | tr ' ' '\n' | grep -v '^$'
 BAZEL_OPTS="$BAZEL_OPTS --copt=-mcpu=native --copt=-mtune=native"
 %endif
 %endif
-BAZEL_OPTS="$BAZEL_OPTS --config=%{build_type} --cxxopt=-std=c++17 %{makeprocesses}"
+BAZEL_OPTS="$BAZEL_OPTS --config=%{build_type} --cxxopt=-std=c++17 --host_cxxopt=-std=c++17 %{makeprocesses}"
 BAZEL_OPTS="$BAZEL_OPTS --config=noaws --config=nogcp --config=nohdfs --config=nonccl"
 
 %if %{enable_cuda}

From 89174228c10224c7f065c8e26034faf86398d647 Mon Sep 17 00:00:00 2001
From: Malik Shahzad Muzaffar <shahzad.malik.muzaffar@cern.ch>
Date: Mon, 21 Feb 2022 09:29:53 +0100
Subject: [PATCH 4/7] Update tensorflow-sources.file

---
 tensorflow-sources.file | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tensorflow-sources.file b/tensorflow-sources.file
index 9fa39e58a0a..2deac7b4fcc 100644
--- a/tensorflow-sources.file
+++ b/tensorflow-sources.file
@@ -4,7 +4,7 @@ BuildRequires: bazel java-env git
 ## INCLUDE tensorflow-requires
 ## INCLUDE compilation_flags
 
-%define tag         0073b9fe65c29226311e4d5010176b837415cf2c
+%define tag         e9547466284d5e96630c5a9c53248e946065aab4
 %define branch      cms/v%{realversion}
 %define github_user cms-externals
 Source: git+https://github.com/%{github_user}/tensorflow.git?obj=%{branch}/%{tag}&export=tensorflow-%{realversion}&output=/tensorflow-%{realversion}.tgz

From 00f05aac8430a083185650fdb1dfddeb82fabfca Mon Sep 17 00:00:00 2001
From: Malik Shahzad Muzaffar <shahzad.malik.muzaffar@cern.ch>
Date: Mon, 21 Feb 2022 10:17:18 +0100
Subject: [PATCH 5/7] Update tensorflow-sources.file

---
 tensorflow-sources.file | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tensorflow-sources.file b/tensorflow-sources.file
index 2deac7b4fcc..028198c4761 100644
--- a/tensorflow-sources.file
+++ b/tensorflow-sources.file
@@ -4,7 +4,7 @@ BuildRequires: bazel java-env git
 ## INCLUDE tensorflow-requires
 ## INCLUDE compilation_flags
 
-%define tag         e9547466284d5e96630c5a9c53248e946065aab4
+%define tag         399b0f02cae076845ca000dffdcbaa2274303dd0
 %define branch      cms/v%{realversion}
 %define github_user cms-externals
 Source: git+https://github.com/%{github_user}/tensorflow.git?obj=%{branch}/%{tag}&export=tensorflow-%{realversion}&output=/tensorflow-%{realversion}.tgz

From 62b6e912d5ca97a7778d3f468523e935c7c5d7a0 Mon Sep 17 00:00:00 2001
From: Shahzad Malik Muzaffar <shahzad.malik.muzaffar@cern.ch>
Date: Tue, 22 Feb 2022 09:25:02 +0100
Subject: [PATCH 6/7] by default disable gpu build support

---
 tensorflow-requires.file | 5 +++--
 tensorflow-sources.file  | 6 ++----
 tensorflow-sources.spec  | 1 +
 3 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/tensorflow-requires.file b/tensorflow-requires.file
index d75e99d3616..e8d419bc140 100644
--- a/tensorflow-requires.file
+++ b/tensorflow-requires.file
@@ -4,10 +4,11 @@ Requires: py3-cython py3-protobuf py3-astor py3-six py3-termcolor py3-absl-py
 Requires: py3-opt-einsum py3-flatbuffers
 Requires: eigen protobuf zlib libpng libjpeg-turbo curl giflib sqlite grpc flatbuffers py3-pybind11
 BuildRequires: py3-wheel
+%if %{enable_gpu}
 %if "%{cmsos}" != "slc7_aarch64"
 ## INCLUDE cuda-flags
-%define enable_cuda 1
 Requires: cuda cudnn
 %else
-%define enable_cuda 0
+%define enable_gpu 0
+%endif
 %endif
diff --git a/tensorflow-sources.file b/tensorflow-sources.file
index 028198c4761..cea24198c8c 100644
--- a/tensorflow-sources.file
+++ b/tensorflow-sources.file
@@ -43,7 +43,7 @@ BAZEL_OPTS="$BAZEL_OPTS --copt=-mcpu=native --copt=-mtune=native"
 BAZEL_OPTS="$BAZEL_OPTS --config=%{build_type} --cxxopt=-std=c++17 --host_cxxopt=-std=c++17 %{makeprocesses}"
 BAZEL_OPTS="$BAZEL_OPTS --config=noaws --config=nogcp --config=nohdfs --config=nonccl"
 
-%if %{enable_cuda}
+%if %{enable_gpu}
 BAZEL_OPTS="$BAZEL_OPTS --config=cuda"
 export GCC_HOST_COMPILER_PREFIX=${GCC_ROOT}/bin
 export GCC_HOST_COMPILER_PATH=$(which gcc)
@@ -52,10 +52,8 @@ export TF_CUDA_VERSION="$(echo ${CUDA_VERSION} | cut -f1,2 -d.)"
 export TF_CUDA_PATHS="${CUDA_ROOT},${CUDNN_ROOT}"
 export TF_CUDA_CLANG=0
 export cuda=Y
-export TF_NEED_CUDA=1
-%else
-export TF_NEED_CUDA=0
 %endif
+export TF_NEED_CUDA=%{enable_gpu}
 export TF_NEED_JEMALLOC=0
 export TF_NEED_HDFS=0
 export TF_NEED_GCP=0
diff --git a/tensorflow-sources.spec b/tensorflow-sources.spec
index de34a960831..6f0a8d49ed8 100644
--- a/tensorflow-sources.spec
+++ b/tensorflow-sources.spec
@@ -3,6 +3,7 @@
 %define python_env PYTHON3PATH
 %define build_type opt
 %define pythonOnly no
+%define enable_gpu 0
 %define vectorize_flag -msse3
 ## INCLUDE tensorflow-sources
 

From a907ab6243dc54e5de059ebb5fac1e261a27d0da Mon Sep 17 00:00:00 2001
From: Shahzad Malik Muzaffar <shahzad.malik.muzaffar@cern.ch>
Date: Tue, 22 Feb 2022 09:32:06 +0100
Subject: [PATCH 7/7] move enable_gpu in tensorflow-requires

---
 tensorflow-requires.file | 1 +
 tensorflow-sources.spec  | 1 -
 2 files changed, 1 insertion(+), 1 deletion(-)

diff --git a/tensorflow-requires.file b/tensorflow-requires.file
index e8d419bc140..f18aff5bf25 100644
--- a/tensorflow-requires.file
+++ b/tensorflow-requires.file
@@ -4,6 +4,7 @@ Requires: py3-cython py3-protobuf py3-astor py3-six py3-termcolor py3-absl-py
 Requires: py3-opt-einsum py3-flatbuffers
 Requires: eigen protobuf zlib libpng libjpeg-turbo curl giflib sqlite grpc flatbuffers py3-pybind11
 BuildRequires: py3-wheel
+%define enable_gpu 0
 %if %{enable_gpu}
 %if "%{cmsos}" != "slc7_aarch64"
 ## INCLUDE cuda-flags
diff --git a/tensorflow-sources.spec b/tensorflow-sources.spec
index 6f0a8d49ed8..de34a960831 100644
--- a/tensorflow-sources.spec
+++ b/tensorflow-sources.spec
@@ -3,7 +3,6 @@
 %define python_env PYTHON3PATH
 %define build_type opt
 %define pythonOnly no
-%define enable_gpu 0
 %define vectorize_flag -msse3
 ## INCLUDE tensorflow-sources