Importing TF 2.12, then torch, hangs, but not the other way around
The error in the given code is that the PyTorch version specified in the pip install command does not exist. The correct version for CUDA 11.1 (cu111) is **torch==1.9.1+cu111** , not **torch==2.0.0+cu118**. The correct pip install command should be:
```
pip install torch==1.9.1+cu111 torchvision==0.10.1+cu111 torchaudio==0.9.1 -f https://download.pytorch.org/whl/cu111/torch_stable.html
```
Additionally, if running TensorFlow in a Docker container, you will need to ensure that both TensorFlow and PyTorch are installed inside the container. You can do this by creating a Dockerfile with the following contents:

```
FROM tensorflow/tensorflow:2.12.0-gpu
RUN pip install torch==1.9.1+cu111 torchvision==0.10.1+cu111 torchaudio==0.9.1 -f https://download.pytorch.org/whl/cu111/torch_stable.html
```
Then, build the Docker image using the command **docker build -t my_image .** and run it with **docker run -it my_image**. Hi @pat749, when I run nvidia-smi from within the  tensorflow/tensorflow:2.12.0-gpu docker image, the docker image is using CUDA version 11.8, not 11.1. Therefore I think torch 2.0+cu118 is correct. That package does indeed exist as it raises no complaints when pip installing, nor when importing torch if TF is not already imported. Hi @TortoiseHam, Thanks for reporting the issue!
I was able to replicate the issue in Ubuntu 20.04. Kindly find the screenshot below.
![image](https://user-images.githubusercontent.com/98147397/227942509-e958135d-31ec-4ede-8e7e-acdb6724d573.png)

Thank you! @SuryanarayanaY Could you please look into this issue?  @TortoiseHam , Tested build configuration for Tensorflow 2.12 is cuDNN 8.6 and CUDA 11.8. Make sure while installing torch also this should not conflict.
Also. could you please do one more check by creating two different environments and installing `torch` and `Tensorflow` separately and check the internal package dependency versions using `pip list`.
My hunch is that it might be because of version conflicts on package dependencies. Sure @sachinprasadhs, so starting with the tensorflow docker image to get Cuda 11.8, I then run `pip freeze | xargs pip uninstall -y` to clear all the existing dependencies, then `pip install tensorflow`. This gives the following dependency list:

```
Package                      Version
---------------------------- ---------
absl-py                      1.4.0
astunparse                   1.6.3
cachetools                   5.3.0
certifi                      2022.12.7
charset-normalizer           3.1.0
flatbuffers                  23.3.3
gast                         0.4.0
google-auth                  2.17.1
google-auth-oauthlib         1.0.0
google-pasta                 0.2.0
grpcio                       1.53.0
h5py                         3.8.0
idna                         3.4
importlib-metadata           6.1.0
jax                          0.4.8
keras                        2.12.0
libclang                     16.0.0
Markdown                     3.4.3
MarkupSafe                   2.1.2
ml-dtypes                    0.0.4
numpy                        1.23.5
oauthlib                     3.2.2
opt-einsum                   3.3.0
packaging                    23.0
pip                          23.0.1
protobuf                     4.22.1
pyasn1                       0.4.8
pyasn1-modules               0.2.8
requests                     2.28.2
requests-oauthlib            1.3.1
rsa                          4.9
scipy                        1.10.1
setuptools                   67.6.0
six                          1.16.0
tensorboard                  2.12.1
tensorboard-data-server      0.7.0
tensorboard-plugin-wit       1.8.1
tensorflow                   2.12.0
tensorflow-estimator         2.12.0
tensorflow-io-gcs-filesystem 0.32.0
termcolor                    2.2.0
typing_extensions            4.5.0
urllib3                      1.26.15
Werkzeug                     2.2.3
wheel                        0.40.0
wrapt                        1.14.1
zipp                         3.15.0
```

If I then delete everything and install torch I get:
```
Package            Version
------------------ ------------
certifi            2022.12.7
charset-normalizer 3.1.0
cmake              3.26.1
filelock           3.10.7
idna               3.4
Jinja2             3.1.2
lit                16.0.0
MarkupSafe         2.1.2
mpmath             1.3.0
networkx           3.0
numpy              1.24.2
Pillow             9.4.0
pip                23.0.1
requests           2.28.2
setuptools         67.6.0
sympy              1.11.1
torch              2.0.0+cu118
torchaudio         2.0.1+cu118
torchvision        0.15.1+cu118
triton             2.0.0
typing_extensions  4.5.0
urllib3            1.26.15
wheel              0.40.0
```

If I delete everything, then install TF followed by Torch I get:
```
Package                      Version
---------------------------- ------------
absl-py                      1.4.0
astunparse                   1.6.3
cachetools                   5.3.0
certifi                      2022.12.7
charset-normalizer           3.1.0
cmake                        3.26.1
filelock                     3.10.7
flatbuffers                  23.3.3
gast                         0.4.0
google-auth                  2.17.1
google-auth-oauthlib         1.0.0
google-pasta                 0.2.0
grpcio                       1.53.0
h5py                         3.8.0
idna                         3.4
importlib-metadata           6.1.0
jax                          0.4.8
Jinja2                       3.1.2
keras                        2.12.0
libclang                     16.0.0
lit                          16.0.0
Markdown                     3.4.3
MarkupSafe                   2.1.2
ml-dtypes                    0.0.4
mpmath                       1.3.0
networkx                     3.0
numpy                        1.23.5
oauthlib                     3.2.2
opt-einsum                   3.3.0
packaging                    23.0
Pillow                       9.4.0
pip                          23.0.1
protobuf                     4.22.1
pyasn1                       0.4.8
pyasn1-modules               0.2.8
requests                     2.28.2
requests-oauthlib            1.3.1
rsa                          4.9
scipy                        1.10.1
setuptools                   67.6.0
six                          1.16.0
sympy                        1.11.1
tensorboard                  2.12.1
tensorboard-data-server      0.7.0
tensorboard-plugin-wit       1.8.1
tensorflow                   2.12.0
tensorflow-estimator         2.12.0
tensorflow-io-gcs-filesystem 0.32.0
termcolor                    2.2.0
torch                        2.0.0+cu118
torchaudio                   2.0.1+cu118
torchvision                  0.15.1+cu118
triton                       2.0.0
typing_extensions            4.5.0
urllib3                      1.26.15
Werkzeug                     2.2.3
wheel                        0.40.0
wrapt                        1.14.1
zipp                         3.15.0
```

Comparing this list to the first one, the process of installing torch did not change any of the existing tensorflow installed dependencies, but did add cmake, filelock, Jinja2, lit, mpmath, networkx, Pillow, sympy, triton, and torch/torchvision/torchaudio. Given that there doesn't seem to be any dependency version conflict, and that the two frameworks do both work simultaneously if torch is imported before tensorflow, I think this is more likely related to something TF 2.12 is doing the python or cuda environment on import. Thanks for your time and effort on detailed investigation. 
@angerson, Could you please take a look into this. Thanks! PyTorch 1.9 is quite old and may not work along with TF 2.12 that uses Cuda 11.8.  When I imported TF 1.12 and then Torch 2.0, which are both compatible with Cuda 11.8 on Colab, I don't see any issue.
https://colab.research.google.com/drive/1nsnraqpXqZuM5coBm_Gj8zrn96f9Os4D?usp=sharing

If you want to use Pytorch 1.9 with TF, please use TF 2.11 which is compatible with Cuda 11.2 similar to PyTorch 1.9 Hi @sampathweb, this issue is not related to torch 1.9 - that was an extraneous comment. The issue is related to TF 1.12 + Torch 2.0. It's very interesting that it seems to work inside your colab notebook since it doesn't work within the TF docker container. Any idea what the notebook might be doing that running raw within docker doesn't? Thanks for confirming.  I am able to see the issue in Docker Container and reproduced the hanging issue with TF 2.12 and Torch 2.0. 

```
# docker pull tensorflow/tensorflow:2.12.0 also has same issue
docker pull tensorflow/tensorflow:2.12.0-gpu
docker run -it tensorflow/tensorflow:2.12.0-gpu

# Install Torch 2.0
# GPU wheel URL also has same issue - https://download.pytorch.org/whl/cu118
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu

# As stated by the user, importing torch before TF works fine.
python
import torch # works
import tensorflow # works

python
import tensorflow # Works
import torch # hangs

# Hangs here -
  File "/usr/local/lib/python3.8/dist-packages/torch/__init__.py", line 229, in <module>
    from torch._C import *  # noqa: F403
```
This problem is not there in Colab which has both TF 2.12 and Torch 2.0
@angerson - FYI The hang comes from ABI incompatibility between TF and Torch. Pytorch has compiled their wheels with `_GLIBCXX_USE_CXX11_ABI=0` (which is out-of-date). TensorFlow upgraded to `_GLIBCXX_USE_CXX11_ABI=1` with the release of [TensorFlow 2.9](https://github.com/tensorflow/tensorflow/releases/tag/v2.9.0). TF 2.12 + Torch 2.0 is the first pairing where this happens---I am not sure why. TF 2.11 + Torch 2.0, and TF 2.12 + Torch 1.13, are both fine.

According to `auditwheel inspect`, torch 1.13 -> 2.0 adds a dependency on libgomp.so.1, which seems innocuous. TF 2.11 -> 2.12 added a new shared object dependency on libtensorflow_cc.so.2 with a version called "tensorflow." It is possible that this was unintended and ultimately exposed this particular breakage; I am not sure. @vam-google and @learning-to-play may be interested in this.

You can verify that CXX11 ABI compatibility is (a) problem by checking a wheel compiled explicitly with the CXX11 ABI enabled. I'm exceedingly grateful for https://github.com/pytorch/builder/pull/990, which added CXX11-compatible wheels to Pytorch's CI system, and you can install them both and compare:

```
docker run -it --rm python:3.10 bash
wget https://download.pytorch.org/whl/cpu/torch-2.0.0%2Bcpu-cp310-cp310-linux_x86_64.whl
wget https://download.pytorch.org/whl/cpu-cxx11-abi/torch-2.0.0%2Bcpu.cxx11.abi-cp310-cp310-linux_x86_64.whl
pip install tensorflow-cpu
pip install torch-2.0.0+cpu.cxx11.abi-cp310-cp310-linux_x86_64.whl
python -c "import tensorflow; import torch"
pip uninstall torch
pip install torch-2.0.0+cpu-cp310-cp310-linux_x86_64.whl
python -c "import tensorflow; import torch"
```

The final command hangs, as expected (you can kill it with Ctrl-D).

@MichaelHudgins FYI

Just to be clear, the TF team's stance on this is that it's an issue that needs to be resolved in PyTorch's builds. Also FYI @perfinion  > The hang comes from ABI incompatibility between TF and Torch. Pytorch has compiled their wheels with `_GLIBCXX_USE_CXX11_ABI=0` (which is out-of-date). TensorFlow upgraded to `_GLIBCXX_USE_CXX11_ABI=1` with the release of [TensorFlow 2.9](https://github.com/tensorflow/tensorflow/releases/tag/v2.9.0). TF 2.12 + Torch 2.0 is the first pairing where this happens---I am not sure why. TF 2.11 + Torch 2.0, and TF 2.12 + Torch 1.13, are both fine.

Also, do you have any idea why it works well on colab?
 > Also, do you have any idea why it works well on colab?

No, I don't know. Colab used to build their own version of the wheels. Unsure if that still happens or not
<details><summary>Click to expand!</summary> 
 
 ### Issue Type

Bug

### Have you reproduced the bug with TF nightly?

Yes

### Source

binary

### Tensorflow Version

2.12.0

### Custom Code

No

### OS Platform and Distribution

Linux Ubuntu 20.04.5

### Mobile device

_No response_

### Python version

3.8.10

### Bazel version

_No response_

### GCC/Compiler version

_No response_

### CUDA/cuDNN version

11.8

### GPU model and memory

_No response_

### Current Behaviour?

```shell
If I import tensorflow and then import torch, the torch import line hangs forever without completing. On the other hand, if I import torch first and then import tensorflow there is no problem.

The hang is so severe that no amount of ctr-c can kill it. You have to kill the python process from a separate terminal to free the hung terminal. 

This issue does not exist in tensorflow 2.11.1 or earlier. It also doesn't happen when using older versions of torch like 1.13.1. Since torch followed by tf works but tf followed by torch doesn't, this seems like an issue tf is causing.
```


### Standalone code to reproduce the issue

```shell
docker pull tensorflow/tensorflow:2.12.0-gpu
docker run -it tensorflow/tensorflow:2.12.0-gpu

pip install torch==2.0.0+cu118 torchvision==0.15.1+cu118 torchaudio==2.0.1+cu118 -f https://download.pytorch.org/whl/cu118/torch_stable.html

python

import tensorflow as tf
import torch
```


### Relevant log output

_No response_</details>