Repository: tensorflow
Issue: ROCm: Importing PyTorch before TensorFlow causes TensorFlow to fail completely
Labels: stat:awaiting tensorflower,type:bug,type:build/install,TF 2.11

<details><summary>Click to expand!</summary> 
 
 ### Issue Type

Bug

### Have you reproduced the bug with TF nightly?

No

### Source

binary

### Tensorflow Version

v2.11.1-3812-gef4eebff7d4 2.11.1

### Custom Code

No

### OS Platform and Distribution

Arch Linux (EndeavourOS)

### Mobile device

_No response_

### Python version

3.10.11

### Bazel version

_No response_

### GCC/Compiler version

_No response_

### CUDA/cuDNN version

ROCm 5.4.3

### GPU model and memory

Radeon VII/16 GiB

### Current Behaviour?

The title suffices. A workaround is to ensure `tensorflow` is imported before `torch`.

### Standalone code to reproduce the issue

```shell
python -c "import torch; import tensorflow as tf; tf.zeros(1)"
```


### Relevant log output

```shell
2023-05-19 22:22:33.756830: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX2 FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
v2.11.1-3812-gef4eebff7d4 2.11.1
(sd) [habbasi@hameer-imacpro11 kohya-trainer]$ python -c "import tensorflow as tf; import torch; tf.zeros(1)"
2023-05-19 22:25:50.708366: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX2 FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2023-05-19 22:25:53.172076: I tensorflow/compiler/xla/stream_executor/rocm/rocm_gpu_executor.cc:843] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2023-05-19 22:25:53.172177: I tensorflow/compiler/xla/stream_executor/rocm/rocm_gpu_executor.cc:843] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2023-05-19 22:25:53.172216: I tensorflow/compiler/xla/stream_executor/rocm/rocm_gpu_executor.cc:843] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2023-05-19 22:25:53.172542: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX2 FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2023-05-19 22:25:53.173978: I tensorflow/compiler/xla/stream_executor/rocm/rocm_gpu_executor.cc:843] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2023-05-19 22:25:53.174095: I tensorflow/compiler/xla/stream_executor/rocm/rocm_gpu_executor.cc:843] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2023-05-19 22:25:53.174137: I tensorflow/compiler/xla/stream_executor/rocm/rocm_gpu_executor.cc:843] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/habbasi/mambaforge/envs/sd/lib/python3.10/site-packages/tensorflow/python/util/traceback_utils.py", line 153, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "/home/habbasi/mambaforge/envs/sd/lib/python3.10/site-packages/tensorflow/python/eager/context.py", line 588, in ensure_initialized
    context_handle = pywrap_tfe.TFE_NewContext(opts)
tensorflow.python.framework.errors_impl.InternalError: hipGetDevice() failed. Status: invalid device ordinal
```
</details>

xref pytorch/pytorch#101900