Failed assertion in tf.linalg.sqrtm (and possibly other functions) crashes entire program instead of raising Exception
Hi @petered ,

I have tested the code with TF2.12 and TF 2.13 and its working fine without any crash in both Linux and MacOS.

Please refer to attached colab [gist](https://colab.sandbox.google.com/gist/SuryanarayanaY/86a8a3d0587a037c239bca9e98601688/61423.ipynb) for Linux

Please refer to attached logs on MacOS.

```
(base) suryanarayanay-macbookpro:Downloads suryanarayanay$ python 61423.py
Metal device set to: Apple M1 Pro

systemMemory: 16.00 GB
maxCacheSize: 5.33 GB

2023-07-31 10:44:48.799377: I tensorflow/core/common_runtime/pluggable_device/pluggable_device_factory.cc:303] Could not identify NUMA node of platform GPU ID 0, defaulting to 0. Your kernel may not have been built with NUMA support.
2023-07-31 10:44:48.799554: I tensorflow/core/common_runtime/pluggable_device/pluggable_device_factory.cc:269] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 0 MB memory) -> physical PluggableDevice (device: 0, name: METAL, pci bus id: <undefined>)
Calculated root
Caught exception:  {{function_node __wrapped__MatrixInverse_device_/job:localhost/replica:0/task:0/device:CPU:0}} Input is not invertible. [Op:MatrixInverse] name: 
2.13.0
(base) suryanarayanay-macbookpro:Downloads suryanarayanay$ cat python 61423.py
cat: python: No such file or directory
# -*- coding: utf-8 -*-
"""61423.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1TEavupf_8DvRPsozzaXe8B2d7Dx4kCx9?resourcekey=0-lU-Xrv3KC0zytE2VrYf0Fw
"""

import tensorflow as tf

def demo_assertion_error_crashes_program():

    degenerate_matrix = tf.ones((3, 3), dtype=tf.float64)
    try:
        tf.linalg.sqrtm(degenerate_matrix)
        # tf.linalg.inv(degenerate_matrix)  # <- This also fails, but raises an actual exception
        print("Calculated root")  # This is never run
    except Exception as err:
        print("Caught exception: ", err)  # Neither is this

if __name__ == '__main__':
    demo_assertion_error_crashes_program()

def demo_assertion_error_crashes_program():

    degenerate_matrix = tf.ones((3, 3), dtype=tf.float64)
    try:
        # tf.linalg.sqrtm(degenerate_matrix)
        tf.linalg.inv(degenerate_matrix)  # <- This also fails, but raises an actual exception
        print("Calculated root")  # This is never run
    except Exception as err:
        print("Caught exception: ", err)  # Neither is this

if __name__ == '__main__':
    demo_assertion_error_crashes_program()

print(tf.__version__)
```

Could you please cross check with TF2.13v and let us know. SInce it is resolved in latest versions it's unlikely to cherry pick for older versions(Tf2.9v etc).

Thanks! This issue is stale because it has been open for 7 days with no activity. It will be closed if no further activity occurs. Thank you. This issue was closed because it has been inactive for 7 days since being marked as stale. Please reopen if you'd like to work on this further. Are you satisfied with the resolution of your issue?
<a href="https://docs.google.com/forms/d/e/1FAIpQLSfaP12TRhd9xSxjXZjcZFNXPGk4kc1-qMdv3gc6bEP90vY1ew/viewform?entry.85265664=Yes&entry.2137816233=https://github.com/tensorflow/tensorflow/issues/61423">Yes</a>
<a href="https://docs.google.com/forms/d/e/1FAIpQLSfaP12TRhd9xSxjXZjcZFNXPGk4kc1-qMdv3gc6bEP90vY1ew/viewform?entry.85265664=No&entry.2137816233=https://github.com/tensorflow/tensorflow/issues/61423">No</a>

### Issue type

Bug

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

Yes

### Source

binary

### TensorFlow version

2.9.1

### Custom code

No

### OS platform and distribution

Macbook 2020 M1 air, Ventura 13.2

### Mobile device

-

### Python version

3.8.13

### Bazel version

_No response_

### GCC/compiler version

_No response_

### CUDA/cuDNN version

_No response_

### GPU model and memory

_No response_

### Current behavior?

Currently, providing a degenerate matrix to tf.linalg.sqrtm crashes the entire program, producing output: 

```
Assertion failed: (T(i,i) >= 0), function matrix_sqrt_quasi_triangular_diagonal, file external/eigen_archive/unsupported/Eigen/src/MatrixFunctions/MatrixSquareRoot.h, line 128.

Process finished with exit code 134 (interrupted by signal 6: SIGABRT)
```

### Standalone code to reproduce the issue

```shell
import tensorflow as tf

def demo_assertion_error_crashes_program():

    degenerate_matrix = tf.ones((3, 3), dtype=tf.float64)
    try:
        tf.linalg.sqrtm(degenerate_matrix)
        # tf.linalg.inv(degenerate_matrix)  # <- This also fails, but raises an actual exception
        print("Calculated root")  # This is never run
    except Exception as err:
        print("Caught exception: ", err)  # Neither is this

if __name__ == '__main__':
    demo_assertion_error_crashes_program()
```


### Relevant log output

```shell
Assertion failed: (T(i,i) >= 0), function matrix_sqrt_quasi_triangular_diagonal, file external/eigen_archive/unsupported/Eigen/src/MatrixFunctions/MatrixSquareRoot.h, line 128.

Process finished with exit code 134 (interrupted by signal 6: SIGABRT)
```

### Workaround

One option is to add a small regularizing term to make it non-degenerate, but I don't (yet) know how to do this such that it always prevents the crash, and also does not significantly affect results then the matrix-square-root would have worked.

Instead, I now just use the Denmann-Beavers iteration to approximate the matrix square-root

```
def tf_denmann_beavers_sqrtm(matrix: tf.Tensor, n_iter=10):
    """
    Approximate the matrix-square-root by Denmann Beavers iteration
        https://en.wikipedia.org/wiki/Square_root_of_a_matrix#By_Denman%E2%80%93Beavers_iteration
    Convergence is not guaranteed.  Use at your own risk!
    This is handy for tflite, which does not yet support tf.linalg.sqrtm
        https://github.com/tensorflow/tensorflow/issues/60154
    Or for regular tensorflow, which crashes your entire program when input matrix is degenerate
        https://github.com/tensorflow/tensorflow/issues/61423
    """
    ym = matrix
    zm = tf.eye(tf.shape(matrix[0])[0], dtype=matrix.dtype)
    for i in range(n_iter):
        ym_ = 0.5 * (ym + tf.linalg.inv(zm))
        zm = 0.5 * (zm + tf.linalg.inv(ym))
        ym = ym_
    return ym
```
... obviously this is not ideal.
