Skip to content

Commit

Permalink
Fix PPC crash in tensorflow/core/grappler/optimizers:arithmetic_optim…
Browse files Browse the repository at this point in the history
…izer_test_cpu
  • Loading branch information
Flamefire committed Feb 7, 2023
1 parent 8579246 commit ca2e0f3
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ exts_list = [
'%(name)s-2.9.1_remove-duplicate-gpu-tests.patch',
'%(name)s-%(version)s_disable-avx512-extensions.patch',
'%(name)s-%(version)s_fix-eigen-atan-on-PPC.patch',
'%(name)s-%(version)s_fix-eigen-gemm-on-PPC.patch',
'%(name)s-%(version)s_fix-link-error.patch',
'%(name)s-%(version)s_remove-libclang-and-io-gcs-deps.patch',
],
Expand All @@ -189,6 +190,8 @@ exts_list = [
'fb8e7694b5d2377cc44e6674ff85a7c50dc725f2f507cbcfda65f129f534b1cc'},
{'TensorFlow-2.11.0_fix-eigen-atan-on-PPC.patch':
'd9f4779f72ffd2c5f9c5da0a7735328dd25756515edccf603087dba2bf4d1612'},
{'TensorFlow-2.11.0_fix-eigen-gemm-on-PPC.patch':
'a7c3a1307c07ca5a3c70483900c55f316ee0ff8e647c1a818182fafe6d98b4e9'},
{'TensorFlow-2.11.0_fix-link-error.patch':
'0a2f5c9c5be425f305bdc08f5a5ffce210e66f6ad4120d94ea0209246fc0449f'},
{'TensorFlow-2.11.0_remove-libclang-and-io-gcs-deps.patch':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ exts_list = [
'%(name)s-2.9.1_remove-duplicate-gpu-tests.patch',
'%(name)s-%(version)s_disable-avx512-extensions.patch',
'%(name)s-%(version)s_fix-eigen-atan-on-PPC.patch',
'%(name)s-%(version)s_fix-eigen-gemm-on-PPC.patch',
'%(name)s-%(version)s_fix-link-error.patch',
'%(name)s-%(version)s_remove-libclang-and-io-gcs-deps.patch',
],
Expand All @@ -184,6 +185,8 @@ exts_list = [
'fb8e7694b5d2377cc44e6674ff85a7c50dc725f2f507cbcfda65f129f534b1cc'},
{'TensorFlow-2.11.0_fix-eigen-atan-on-PPC.patch':
'd9f4779f72ffd2c5f9c5da0a7735328dd25756515edccf603087dba2bf4d1612'},
{'TensorFlow-2.11.0_fix-eigen-gemm-on-PPC.patch':
'a7c3a1307c07ca5a3c70483900c55f316ee0ff8e647c1a818182fafe6d98b4e9'},
{'TensorFlow-2.11.0_fix-link-error.patch':
'0a2f5c9c5be425f305bdc08f5a5ffce210e66f6ad4120d94ea0209246fc0449f'},
{'TensorFlow-2.11.0_remove-libclang-and-io-gcs-deps.patch':
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
Fix a SIGSEGV in e.g. //tensorflow/core/grappler/optimizers:arithmetic_optimizer_test_cpu
See https://gitlab.com/libeigen/eigen/-/issues/2608

Author: Alexander Grund (TU Dresden)

diff --git a/third_party/eigen3/fix-ppc-gemm.patch b/third_party/eigen3/fix-ppc-gemm.patch
new file mode 100644
index 00000000000..6a9faf462e0
--- /dev/null
+++ b/third_party/eigen3/fix-ppc-gemm.patch
@@ -0,0 +1,91 @@
+diff --git a/Eigen/src/Core/arch/AltiVec/MatrixProductCommon.h b/Eigen/src/Core/arch/AltiVec/MatrixProductCommon.h
+index 28868ca5a..35e7f673e 100644
+--- a/Eigen/src/Core/arch/AltiVec/MatrixProductCommon.h
++++ b/Eigen/src/Core/arch/AltiVec/MatrixProductCommon.h
+@@ -30,23 +30,6 @@ EIGEN_ALWAYS_INLINE void gemm_extra_row(
+ const Packet& pAlpha,
+ const Packet& pMask);
+
+-template<typename Scalar, typename Packet, typename DataMapper, const Index accCols, bool ConjugateLhs, bool ConjugateRhs, bool LhsIsReal, bool RhsIsReal>
+-EIGEN_STRONG_INLINE void gemm_extra_cols(
+- const DataMapper& res,
+- const Scalar* blockA,
+- const Scalar* blockB,
+- Index depth,
+- Index strideA,
+- Index offsetA,
+- Index strideB,
+- Index offsetB,
+- Index col,
+- Index rows,
+- Index cols,
+- Index remaining_rows,
+- const Packet& pAlpha,
+- const Packet& pMask);
+-
+ template<typename Packet>
+ EIGEN_ALWAYS_INLINE Packet bmask(const Index remaining_rows);
+
+diff --git a/Eigen/src/Core/arch/AltiVec/MatrixProductMMA.h b/Eigen/src/Core/arch/AltiVec/MatrixProductMMA.h
+index aa1cbf88f..3cd4ab32a 100644
+--- a/Eigen/src/Core/arch/AltiVec/MatrixProductMMA.h
++++ b/Eigen/src/Core/arch/AltiVec/MatrixProductMMA.h
+@@ -427,6 +427,49 @@ EIGEN_ALWAYS_INLINE void gemmMMA_cols(
+ }
+ }
+
++#define MICRO_EXTRA(MICRO_EXTRA_UNROLL, value, is_col) \
++ switch(value) { \
++ default: \
++ MICRO_EXTRA_UNROLL(1) \
++ break; \
++ case 2: \
++ if (is_col || (sizeof(Scalar) == sizeof(float))) { \
++ MICRO_EXTRA_UNROLL(2) \
++ } \
++ break; \
++ case 3: \
++ if (is_col || (sizeof(Scalar) == sizeof(float))) { \
++ MICRO_EXTRA_UNROLL(3) \
++ } \
++ break; \
++ }
++
++#define MICRO_EXTRA_COLS(N) \
++ gemmMMA_cols<Scalar, Packet, RhsPacket2, DataMapper, N, accCols>(res, blockA, blockB, depth, strideA, offsetA, strideB, offsetB, col, rows, remaining_rows, pAlpha, pMask);
++
++template<typename Scalar, typename Packet, typename RhsPacket2, typename DataMapper, const Index accCols>
++EIGEN_STRONG_INLINE void gemmMMA_extra_cols(
++ const DataMapper& res,
++ const Scalar* blockA,
++ const Scalar* blockB,
++ Index depth,
++ Index strideA,
++ Index offsetA,
++ Index strideB,
++ Index offsetB,
++ Index col,
++ Index rows,
++ Index cols,
++ Index remaining_rows,
++ const Packet& pAlpha,
++ const Packet& pMask)
++{
++ MICRO_EXTRA(MICRO_EXTRA_COLS, cols-col, true)
++}
++
++#undef MICRO_EXTRA
++#undef MICRO_EXTRA_COLS
++
+ template<typename Scalar, typename Packet, typename RhsPacket, typename DataMapper, const Index accRows, const Index accCols>
+ void gemmMMA(const DataMapper& res, const Scalar* blockA, const Scalar* blockB, Index rows, Index depth, Index cols, Scalar alpha, Index strideA, Index strideB, Index offsetA, Index offsetB)
+ {
+@@ -448,7 +491,7 @@ void gemmMMA(const DataMapper& res, const Scalar* blockA, const Scalar* blockB,
+
+ if (col != cols)
+ {
+- gemm_extra_cols<Scalar, Packet, DataMapper, accCols>(res, blockA, blockB, depth, strideA, offsetA, strideB, offsetB, col, rows, cols, remaining_rows, pAlpha, pMask);
++ gemmMMA_extra_cols<Scalar, Packet, RhsPacket2, DataMapper, accCols>(res, blockA, blockB, depth, strideA, offsetA, strideB, offsetB, col, rows, cols, remaining_rows, pAlpha, pMask);
+ }
+ }
+
diff --git a/third_party/eigen3/workspace.bzl b/third_party/eigen3/workspace.bzl
index 6c8744aeec4..2a2a7b474c9 100644
--- a/third_party/eigen3/workspace.bzl
+++ b/third_party/eigen3/workspace.bzl
@@ -14,7 +14,7 @@ def repo():
tf_http_archive(
name = "eigen_archive",
build_file = "//third_party/eigen3:eigen_archive.BUILD",
- patch_file = ["//third_party/eigen3:disable-atan-on-ppc.patch"],
+ patch_file = ["//third_party/eigen3:disable-atan-on-ppc.patch", "//third_party/eigen3:fix-ppc-gemm.patch"],
sha256 = EIGEN_SHA256,
strip_prefix = "eigen-{commit}".format(commit = EIGEN_COMMIT),
urls = tf_mirror_urls("https://gitlab.com/libeigen/eigen/-/archive/{commit}/eigen-{commit}.tar.gz".format(commit = EIGEN_COMMIT)),

0 comments on commit ca2e0f3

Please sign in to comment.