Repository: Project-MONAI/MONAI

Issue:
shared location for different jit builds
**Describe the bug**
building different versions of the _extensions currently uses the same module name,
This can cause issues of incompatible compiled modules.

log:
https://github.com/Project-MONAI/MONAI/runs/2870435032?check_suite_focus=true#step:6:9618

```
======================================================================
ERROR: test_cuda_3_1_batches_3_dimensions_1_channels_2_classes_1_mixtures (tests.test_gmm.GMMTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/parameterized/parameterized.py", line 533, in standalone_func
    return func(*(a + p.args), **p.kwargs)
  File "/__w/MONAI/MONAI/tests/test_gmm.py", line 323, in test_cuda
    gmm = GaussianMixtureModel(features_tensor.size(1), mixture_count, class_count)
  File "/__w/MONAI/MONAI/monai/networks/layers/gmm.py", line 42, in __init__
    "gmm", {"CHANNEL_COUNT": channel_count, "MIXTURE_COUNT": mixture_count, "MIXTURE_SIZE": mixture_size}
  File "/__w/MONAI/MONAI/monai/_extensions/loader.py", line 86, in load_module
    verbose=verbose_build,
  File "/opt/conda/lib/python3.6/site-packages/torch/utils/cpp_extension.py", line 952, in load
    keep_intermediates=keep_intermediates)
  File "/opt/conda/lib/python3.6/site-packages/torch/utils/cpp_extension.py", line 1168, in _jit_compile
    return _import_module_from_library(name, build_directory, is_python_module)
  File "/opt/conda/lib/python3.6/site-packages/torch/utils/cpp_extension.py", line 1493, in _import_module_from_library
    return imp.load_module(module_name, file, path, description)
  File "/opt/conda/lib/python3.6/imp.py", line 243, in load_module
    return load_dynamic(name, filename, file)
  File "/opt/conda/lib/python3.6/imp.py", line 343, in load_dynamic
    return _load(spec)
  File "<frozen importlib._bootstrap>", line 684, in _load
  File "<frozen importlib._bootstrap>", line 658, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 571, in module_from_spec
  File "<frozen importlib._bootstrap_external>", line 922, in create_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
ImportError: /github/home/.cache/torch_extensions/gmm_1_2_1/gmm_1_2_1.so: undefined symbol: _ZN2at5emptyEN3c108ArrayRefIlEERKNS0_13TensorOptionsENS0_8optionalINS0_12MemoryFormatEEE
```



Discussion:

Resolving patch:
diff --git a/monai/_extensions/loader.py b/monai/_extensions/loader.py
--- a/monai/_extensions/loader.py
+++ b/monai/_extensions/loader.py
@@ -9,6 +9,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+import platform
 from _thread import interrupt_main
 from contextlib import contextmanager
 from glob import glob
@@ -18,7 +19,7 @@
 
 import torch
 
-from monai.utils.module import optional_import
+from monai.utils.module import get_torch_version_tuple, optional_import
 
 dir_path = path.dirname(path.realpath(__file__))
 
@@ -61,6 +62,8 @@ def load_module(
     if not path.exists(module_dir):
         raise ValueError(f"No extension module named {module_name}")
 
+    platform_str = f"_{platform.system()}_{platform.python_version()}_"
+    platform_str += "".join(f"{v}" for v in get_torch_version_tuple()[:2])
     # Adding configuration to module name.
     if defines is not None:
         module_name = "_".join([module_name] + [f"{v}" for v in defines.values()])
@@ -69,6 +72,7 @@ def load_module(
     source = glob(path.join(module_dir, "**", "*.cpp"), recursive=True)
     if torch.cuda.is_available():
         source += glob(path.join(module_dir, "**", "*.cu"), recursive=True)
+        platform_str += f"_{torch.version.cuda}"
 
     # Constructing compilation argument list.
     define_args = [] if not defines else [f"-D {key}={defines[key]}" for key in defines]
@@ -78,8 +82,9 @@ def load_module(
     with timeout(build_timeout, "Build appears to be blocked. Is there a stopped process building the same extension?"):
         load, _ = optional_import("torch.utils.cpp_extension", name="load")  # main trigger some JIT config in pytorch
         # This will either run the build or return the existing .so object.
+        name = module_name + platform_str.replace(".", "_")
         module = load(
-            name=module_name,
+            name=name,
             sources=source,
             extra_cflags=define_args,
             extra_cuda_cflags=define_args,


Test patch: