Repository: tensorflow
Issue: "invalid static_cast" on AVX512FP16 (e.g. Sapphire Rapids)
Labels: stat:awaiting tensorflower,type:bug,type:build/install,comp:core,TF 2.15

### Issue type

Bug

### Have you reproduced the bug with TensorFlow Nightly?

Yes

### Source

source

### TensorFlow version

2.15.1

### Custom code

No

### OS platform and distribution

RHEL 8

### Mobile device

_No response_

### Python version

3.10

### Bazel version

6.1.0

### GCC/compiler version

GCC 13.1

### CUDA/cuDNN version

_No response_

### GPU model and memory

_No response_

### Current behavior?

Failing build with

```
external/eigen_archive/Eigen/src/Core/MathFunctions.h:429:12: error: invalid ‘static_cast’ from type ‘const Eigen::internal::eigen_packet_wrapper<__vector(4) long long int, 1>’ to type ‘__vector(16) float’
429 | return static_cast(x);
```

when compiling for CPUs supporting AVX512 FP16 extensions, e.g. Sapphire Rapids

This is the same issue as #62047 reported by @OH-AU which was closes by @mihaimaruseac because an unsupported Python version was used which is however unrelated to this failure.

After some analysis it turns out to be an issue in Eigen: https://gitlab.com/libeigen/eigen/-/issues/2829 which might be fixed by https://gitlab.com/libeigen/eigen/-/merge_requests/1639

So TF can either use that MR as a patch or update Eigen once the MR is merged

### Standalone code to reproduce the issue

```shell
TF_PYTHON_VERSION=3.10 CFLAGS="-O3 -march=native -fPIC" CXXFLAGS=$CFLAGS LIBRARY_PATH=$LD_RUN_PATH LD_LIBRARY_PATH=$LD_RUN_PATH \
LDFLAGS="-fPIC  -Wl,--disable-new-dtags -Wl,--rpath -Wl,${LD_RUN_PATH}" bazel build -j 24 --config=opt -c opt  --copt=-march=native \
--config=mkl --config=tensorrt  //tensorflow/tools/pip_package:build_pip_package --repo_env=TF_PYTHON_VERSION=3.10
```


### Relevant log output

```shell
In file included from external/eigen_archive/Eigen/Core:182,
                 from tensorflow/core/kernels/linalg/matrix_inverse_op.cc:22:
external/eigen_archive/Eigen/src/Core/MathFunctions.h: In instantiation of 'static NewType Eigen::internal::cast_impl<OldType, NewType, EnableIf>::run(const OldType&) [with OldType = Eigen::internal::eigen_packet_wrapper<__vector(4) long long int, 1>; NewType = __vector(16) float; EnableIf = void]':
external/eigen_archive/Eigen/src/Core/GenericPacketMath.h:268:48:   required from 'static TgtPacket Eigen::internal::pcast_generic<SrcPacket, TgtPacket, false, false>::run(const SrcPacket&) [with SrcPacket = Eigen::internal::eigen_packet_wrapper<__vector(4) long long int, 1>; TgtPacket = __vector(16) float]'
external/eigen_archive/Eigen/src/Core/GenericPacketMath.h:289:50:   required from 'TgtPacket Eigen::internal::pcast(const SrcPacket&) [with SrcPacket = eigen_packet_wrapper<__vector(4) long long int, 1>; TgtPacket = __vector(16) float]'
external/eigen_archive/Eigen/src/Core/CoreEvaluators.h:789:52:   required from 'DstPacketType Eigen::internal::unary_evaluator<Eigen::CwiseUnaryOp<Eigen::internal::core_cast_op<SrcType, DstType>, ArgType>, Eigen::internal::IndexBased>::packet(Eigen::Index) const [with int LoadMode = 0; DstPacketType = __vector(16) float; typename std::enable_if<Eigen::internal::find_packet_by_size<SrcType, Eigen::internal::unpacket_traits<DstPacketType>::size>::value, bool>::type <anonymous> = true; SrcType = Eigen::half; DstType = float; ArgType = const Eigen::Map<const Eigen::Matrix<Eigen::half, -1, -1, 1, -1, -1>, 0, Eigen::Stride<0, 0> >; typename Eigen::CwiseUnaryOp<Eigen::internal::core_cast_op<SrcType, DstType>, ArgType>::Scalar = float; Eigen::Index = long int]'
external/eigen_archive/Eigen/src/Core/AssignEvaluator.h:706:110:   required from 'void Eigen::internal::generic_dense_assignment_kernel<DstEvaluatorTypeT, SrcEvaluatorTypeT, Functor, Version>::assignPacket(Eigen::Index) [with int StoreMode = 64; int LoadMode = 0; Packet = __vector(16) float; DstEvaluatorTypeT = Eigen::internal::evaluator<Eigen::Matrix<float, -1, -1, 1, -1, -1> >; SrcEvaluatorTypeT = Eigen::internal::evaluator<Eigen::CwiseUnaryOp<Eigen::internal::core_cast_op<Eigen::half, float>, const Eigen::Map<const Eigen::Matrix<Eigen::half, -1, -1, 1, -1, -1>, 0, Eigen::Stride<0, 0> > > >; Functor = Eigen::internal::assign_op<float, float>; int Version = 0; Eigen::Index = long int]'
external/eigen_archive/Eigen/src/Core/AssignEvaluator.h:462:75:   required from 'static constexpr void Eigen::internal::dense_assignment_loop<Kernel, 3, 0>::run(Kernel&) [with Kernel = Eigen::internal::generic_dense_assignment_kernel<Eigen::internal::evaluator<Eigen::Matrix<float, -1, -1, 1, -1, -1> >, Eigen::internal::evaluator<Eigen::CwiseUnaryOp<Eigen::internal::core_cast_op<Eigen::half, float>, const Eigen::Map<const Eigen::Matrix<Eigen::half, -1, -1, 1, -1, -1>, 0, Eigen::Stride<0, 0> > > >, Eigen::internal::assign_op<float, float>, 0>]'
external/eigen_archive/Eigen/src/Core/AssignEvaluator.h:810:37:   [ skipping 3 instantiation contexts, use -ftemplate-backtrace-limit=0 to disable ]
external/eigen_archive/Eigen/src/Core/AssignEvaluator.h:883:27:   required from 'void Eigen::internal::call_assignment(Dst&, const Src&, const Func&, std::enable_if_t<(! evaluator_assume_aliasing<Src>::value), void*>) [with Dst = Eigen::Matrix<float, -1, -1, 1, -1, -1>; Src = Eigen::CwiseUnaryOp<core_cast_op<Eigen::half, float>, const Eigen::Map<const Eigen::Matrix<Eigen::half, -1, -1, 1, -1, -1>, 0, Eigen::Stride<0, 0> > >; Func = assign_op<float, float>; std::enable_if_t<(! evaluator_assume_aliasing<Src>::value), void*> = void*; typename evaluator_traits<SrcXprType>::Shape = Eigen::DenseShape]'
external/eigen_archive/Eigen/src/Core/AssignEvaluator.h:861:18:   required from 'void Eigen::internal::call_assignment(Dst&, const Src&) [with Dst = Eigen::Matrix<float, -1, -1, 1, -1, -1>; Src = Eigen::CwiseUnaryOp<core_cast_op<Eigen::half, float>, const Eigen::Map<const Eigen::Matrix<Eigen::half, -1, -1, 1, -1, -1>, 0, Eigen::Stride<0, 0> > >]'
external/eigen_archive/Eigen/src/Core/PlainObjectBase.h:771:32:   required from 'Derived& Eigen::PlainObjectBase<Derived>::_set(const Eigen::DenseBase<OtherDerived>&) [with OtherDerived = Eigen::CwiseUnaryOp<Eigen::internal::core_cast_op<Eigen::half, float>, const Eigen::Map<const Eigen::Matrix<Eigen::half, -1, -1, 1, -1, -1>, 0, Eigen::Stride<0, 0> > >; Derived = Eigen::Matrix<float, -1, -1, 1, -1, -1>]'
external/eigen_archive/Eigen/src/Core/Matrix.h:227:24:   required from 'Eigen::Matrix<Scalar_, Rows_, Cols_, Options_, MaxRows_, MaxCols_>& Eigen::Matrix<Scalar_, Rows_, Cols_, Options_, MaxRows_, MaxCols_>::operator=(const Eigen::DenseBase<OtherDerived>&) [with OtherDerived = Eigen::CwiseUnaryOp<Eigen::internal::core_cast_op<Eigen::half, float>, const Eigen::Map<const Eigen::Matrix<Eigen::half, -1, -1, 1, -1, -1>, 0, Eigen::Stride<0, 0> > >; Scalar_ = float; int Rows_ = -1; int Cols_ = -1; int Options_ = 1; int MaxRows_ = -1; int MaxCols_ = -1]'
external/eigen_archive/Eigen/src/LU/PartialPivLU.h:135:12:   required from 'Eigen::PartialPivLU<MatrixType, PermutationIndex>& Eigen::PartialPivLU<MatrixType, PermutationIndex>::compute(const Eigen::EigenBase<OtherDerived>&) [with InputType = Eigen::CwiseUnaryOp<Eigen::internal::core_cast_op<Eigen::half, float>, const Eigen::Map<const Eigen::Matrix<Eigen::half, -1, -1, 1, -1, -1>, 0, Eigen::Stride<0, 0> > >; MatrixType_ = Eigen::Matrix<float, -1, -1, 1, -1, -1>; PermutationIndex_ = int]'
tensorflow/core/kernels/linalg/matrix_inverse_op.cc:113:31:   required from here
external/eigen_archive/Eigen/src/Core/MathFunctions.h:429:12: error: invalid 'static_cast' from type 'const Eigen::internal::eigen_packet_wrapper<__vector(4) long long int, 1>' to type '__vector(16) float'
  429 |     return static_cast<NewType>(x);
      |            ^~~~~~~~~~~~~~~~~~~~~~~
```