Repository: pytorch
Issue: Eliminate and test circular imports in PyTorch
Labels: module: tests,module: lint,triaged,module: python frontend

### 🐛 Describe the bug

Some circular dependencies in `torch` are not covered in test `TestImports.test_circular_dependencies`. The test uses `importlib` to import the module in the current process, which has a side effect on subsequent modules in the test.

https://github.com/pytorch/pytorch/blob/21144ce5704f5d95dff8d28e3a389c798b03afe3/test/test_testing.py#L2268-L2272

While doing a raw import in a subprocess, the import system will raise an error for circular import.

```text
Failed to import 32 module(s):
  torch._inductor.codegen.cpp_gemm_template                    # circular import
  torch._inductor.codegen.cpp_micro_gemm                       # circular import
  torch._inductor.codegen.cpp_template                         # circular import
  torch._inductor.codegen.cpp_template_kernel                  # circular import
  torch._inductor.kernel                                       # circular import
  torch._inductor.kernel.bmm                                   # circular import
  torch._inductor.kernel.conv                                  # circular import
  torch._inductor.kernel.flex_attention                        # circular import
  torch._inductor.kernel.mm                                    # circular import
  torch._inductor.kernel.mm_common                             # circular import
  torch._inductor.kernel.mm_plus_mm                            # circular import
  torch._inductor.kernel.unpack_mixed_mm                       # circular import
  torch._inductor.mkldnn_lowerings                             # circular import
  torch._inductor.quantized_lowerings                          # circular import
  torch.ao.pruning._experimental.data_sparsifier.benchmarks.dlrm_utils
  torch.ao.pruning._experimental.data_sparsifier.benchmarks.evaluate_disk_savings
  torch.ao.pruning._experimental.data_sparsifier.benchmarks.evaluate_forward_time
  torch.ao.pruning._experimental.data_sparsifier.benchmarks.evaluate_model_metrics
  torch.ao.pruning._experimental.data_sparsifier.lightning.callbacks.data_sparsity
  torch.ao.pruning._experimental.data_sparsifier.lightning.tests.test_callbacks
  torch.backends._coreml.preprocess
  torch.contrib._tensorboard_vis
  torch.csrc.lazy.test_mnist
  torch.distributed._tensor.examples.visualize_sharding_example
  torch.distributed.elastic.rendezvous.etcd_rendezvous
  torch.distributed.elastic.rendezvous.etcd_rendezvous_backend
  torch.distributed.elastic.rendezvous.etcd_store
  torch.distributed.examples.memory_tracker_example
  torch.distributed.tensor.parallel.fsdp                       # circular import
  torch.export._unlift                                         # circular import
  torch.testing._internal.distributed.distributed_test
  torch.utils.model_dump.__main__
```

The snippet:

```python
import concurrent.futures
import glob
import itertools
import os
import subprocess
import sys
import threading

import torch


def check_module_importable(module, max_length, lock):
    proc = subprocess.run(
        [sys.executable, '-c', f'import {module}'],
        timeout=10.0,
        capture_output=True,
        check=False,
    )
    if proc.returncode != 0:
        with lock:
            print('=' * (max_length + 18))
            print(f'import {module:<{max_length}s}  # FAILURE')
            print()
            print(proc.stderr.decode().strip())
            print('=' * (max_length + 18))
    else:
        with lock:
            print(f'import {module:<{max_length}s}  # SUCCESS')

    return proc.returncode


def main():
    os.chdir(os.path.dirname(os.path.dirname(torch.__file__)))

    submodules = [
        path.removesuffix('.py').removesuffix('/__init__').replace('/', '.')
        for path in glob.iglob('torch/**/*.py', recursive=True)
    ]

    modules = sorted(
        set(
            itertools.chain.from_iterable(
                itertools.accumulate(mod.split('.'), lambda p, x: f'{p}.{x}') for mod in submodules
            )
        )
    )

    max_length = max(len(mod) for mod in modules)
    lock = threading.Lock()

    with concurrent.futures.ThreadPoolExecutor() as executor:
        return_codes = list(
            executor.map(
                check_module_importable,
                modules,
                itertools.repeat(max_length),
                itertools.repeat(lock),
            )
        )

    if any(return_codes):
        failed_imports = [mod for mod, return_code in zip(modules, return_codes) if return_code]
        print()
        print('>' * (max_length + 18))
        print(f'Failed to import {len(failed_imports)} module(s):')
        for mod in failed_imports:
            print(f'  {mod}')
        return 1
    return 0


if __name__ == '__main__':
    sys.exit(main())
```

<details>
<summary>Partial Output</summary>

```text
==========================================================================================================
import torch._inductor.codegen.cpp_gemm_template                                                 # FAILURE

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/codegen/cpp_gemm_template.py", line 5, in <module>
    from .. import ir, lowering as L
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/lowering.py", line 6089, in <module>
    from . import kernel
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/kernel/__init__.py", line 1, in <module>
    from . import mm, mm_common, mm_plus_mm, unpack_mixed_mm
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/kernel/mm.py", line 6, in <module>
    from torch._inductor.codegen.cpp_gemm_template import CppPackedGemmTemplate
ImportError: cannot import name 'CppPackedGemmTemplate' from partially initialized module 'torch._inductor.codegen.cpp_gemm_template' (most likely due to a circular import) (/home/PanXuehai/Projects/pytorch/torch/_inductor/codegen/cpp_gemm_template.py)
==========================================================================================================
==========================================================================================================
import torch._inductor.codegen.cpp_micro_gemm                                                    # FAILURE

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/codegen/cpp_micro_gemm.py", line 13, in <module>
    from .cpp_template_kernel import CppTemplateKernel
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/codegen/cpp_template_kernel.py", line 8, in <module>
    from .. import codecache, config, ir, lowering as L
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/lowering.py", line 6089, in <module>
    from . import kernel
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/kernel/__init__.py", line 1, in <module>
    from . import mm, mm_common, mm_plus_mm, unpack_mixed_mm
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/kernel/mm.py", line 6, in <module>
    from torch._inductor.codegen.cpp_gemm_template import CppPackedGemmTemplate
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/codegen/cpp_gemm_template.py", line 11, in <module>
    from .cpp_micro_gemm import create_micro_gemm
ImportError: cannot import name 'create_micro_gemm' from partially initialized module 'torch._inductor.codegen.cpp_micro_gemm' (most likely due to a circular import) (/home/PanXuehai/Projects/pytorch/torch/_inductor/codegen/cpp_micro_gemm.py)
==========================================================================================================
==========================================================================================================
import torch._inductor.codegen.cpp_template_kernel                                               # FAILURE

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/codegen/cpp_template_kernel.py", line 8, in <module>
    from .. import codecache, config, ir, lowering as L
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/lowering.py", line 6089, in <module>
    from . import kernel
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/kernel/__init__.py", line 1, in <module>
    from . import mm, mm_common, mm_plus_mm, unpack_mixed_mm
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/kernel/mm.py", line 6, in <module>
    from torch._inductor.codegen.cpp_gemm_template import CppPackedGemmTemplate
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/codegen/cpp_gemm_template.py", line 11, in <module>
    from .cpp_micro_gemm import create_micro_gemm
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/codegen/cpp_micro_gemm.py", line 13, in <module>
    from .cpp_template_kernel import CppTemplateKernel
ImportError: cannot import name 'CppTemplateKernel' from partially initialized module 'torch._inductor.codegen.cpp_template_kernel' (most likely due to a circular import) (/home/PanXuehai/Projects/pytorch/torch/_inductor/codegen/cpp_template_kernel.py)
==========================================================================================================
==========================================================================================================
import torch._inductor.codegen.cpp_template                                                      # FAILURE

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/codegen/cpp_template.py", line 16, in <module>
    from .cpp_template_kernel import CppTemplateCaller, CppTemplateKernel
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/codegen/cpp_template_kernel.py", line 8, in <module>
    from .. import codecache, config, ir, lowering as L
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/lowering.py", line 6089, in <module>
    from . import kernel
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/kernel/__init__.py", line 1, in <module>
    from . import mm, mm_common, mm_plus_mm, unpack_mixed_mm
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/kernel/mm.py", line 6, in <module>
    from torch._inductor.codegen.cpp_gemm_template import CppPackedGemmTemplate
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/codegen/cpp_gemm_template.py", line 11, in <module>
    from .cpp_micro_gemm import create_micro_gemm
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/codegen/cpp_micro_gemm.py", line 13, in <module>
    from .cpp_template_kernel import CppTemplateKernel
ImportError: cannot import name 'CppTemplateKernel' from partially initialized module 'torch._inductor.codegen.cpp_template_kernel' (most likely due to a circular import) (/home/PanXuehai/Projects/pytorch/torch/_inductor/codegen/cpp_template_kernel.py)
==========================================================================================================
==========================================================================================================
import torch._inductor.kernel                                                                    # FAILURE

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/kernel/__init__.py", line 1, in <module>
    from . import mm, mm_common, mm_plus_mm, unpack_mixed_mm
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/kernel/mm.py", line 6, in <module>
    from torch._inductor.codegen.cpp_gemm_template import CppPackedGemmTemplate
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/codegen/cpp_gemm_template.py", line 5, in <module>
    from .. import ir, lowering as L
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/lowering.py", line 6091, in <module>
    import_submodule(kernel)
  File "/home/PanXuehai/Projects/pytorch/torch/_dynamo/utils.py", line 2006, in import_submodule
    importlib.import_module(f"{mod.__name__}.{filename[:-3]}")
  File "/home/PanXuehai/Miniconda3/envs/pytorch-deps/lib/python3.12/importlib/__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/kernel/bmm.py", line 19, in <module>
    from .mm import _is_static_problem
ImportError: cannot import name '_is_static_problem' from partially initialized module 'torch._inductor.kernel.mm' (most likely due to a circular import) (/home/PanXuehai/Projects/pytorch/torch/_inductor/kernel/mm.py)
==========================================================================================================
==========================================================================================================
import torch._inductor.kernel.conv                                                               # FAILURE

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/kernel/__init__.py", line 1, in <module>
    from . import mm, mm_common, mm_plus_mm, unpack_mixed_mm
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/kernel/mm.py", line 6, in <module>
    from torch._inductor.codegen.cpp_gemm_template import CppPackedGemmTemplate
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/codegen/cpp_gemm_template.py", line 5, in <module>
    from .. import ir, lowering as L
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/lowering.py", line 6091, in <module>
    import_submodule(kernel)
  File "/home/PanXuehai/Projects/pytorch/torch/_dynamo/utils.py", line 2006, in import_submodule
    importlib.import_module(f"{mod.__name__}.{filename[:-3]}")
  File "/home/PanXuehai/Miniconda3/envs/pytorch-deps/lib/python3.12/importlib/__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/kernel/bmm.py", line 19, in <module>
    from .mm import _is_static_problem
ImportError: cannot import name '_is_static_problem' from partially initialized module 'torch._inductor.kernel.mm' (most likely due to a circular import) (/home/PanXuehai/Projects/pytorch/torch/_inductor/kernel/mm.py)
==========================================================================================================
==========================================================================================================
import torch._inductor.kernel.bmm                                                                # FAILURE

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/kernel/__init__.py", line 1, in <module>
    from . import mm, mm_common, mm_plus_mm, unpack_mixed_mm
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/kernel/mm.py", line 6, in <module>
    from torch._inductor.codegen.cpp_gemm_template import CppPackedGemmTemplate
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/codegen/cpp_gemm_template.py", line 5, in <module>
    from .. import ir, lowering as L
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/lowering.py", line 6091, in <module>
    import_submodule(kernel)
  File "/home/PanXuehai/Projects/pytorch/torch/_dynamo/utils.py", line 2006, in import_submodule
    importlib.import_module(f"{mod.__name__}.{filename[:-3]}")
  File "/home/PanXuehai/Miniconda3/envs/pytorch-deps/lib/python3.12/importlib/__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/kernel/bmm.py", line 19, in <module>
    from .mm import _is_static_problem
ImportError: cannot import name '_is_static_problem' from partially initialized module 'torch._inductor.kernel.mm' (most likely due to a circular import) (/home/PanXuehai/Projects/pytorch/torch/_inductor/kernel/mm.py)
==========================================================================================================
==========================================================================================================
import torch._inductor.kernel.flex_attention                                                     # FAILURE

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/kernel/__init__.py", line 1, in <module>
    from . import mm, mm_common, mm_plus_mm, unpack_mixed_mm
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/kernel/mm.py", line 6, in <module>
    from torch._inductor.codegen.cpp_gemm_template import CppPackedGemmTemplate
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/codegen/cpp_gemm_template.py", line 5, in <module>
    from .. import ir, lowering as L
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/lowering.py", line 6091, in <module>
    import_submodule(kernel)
  File "/home/PanXuehai/Projects/pytorch/torch/_dynamo/utils.py", line 2006, in import_submodule
    importlib.import_module(f"{mod.__name__}.{filename[:-3]}")
  File "/home/PanXuehai/Miniconda3/envs/pytorch-deps/lib/python3.12/importlib/__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/kernel/bmm.py", line 19, in <module>
    from .mm import _is_static_problem
ImportError: cannot import name '_is_static_problem' from partially initialized module 'torch._inductor.kernel.mm' (most likely due to a circular import) (/home/PanXuehai/Projects/pytorch/torch/_inductor/kernel/mm.py)
==========================================================================================================
==========================================================================================================
import torch._inductor.kernel.mm                                                                 # FAILURE

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/kernel/__init__.py", line 1, in <module>
    from . import mm, mm_common, mm_plus_mm, unpack_mixed_mm
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/kernel/mm.py", line 6, in <module>
    from torch._inductor.codegen.cpp_gemm_template import CppPackedGemmTemplate
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/codegen/cpp_gemm_template.py", line 5, in <module>
    from .. import ir, lowering as L
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/lowering.py", line 6091, in <module>
    import_submodule(kernel)
  File "/home/PanXuehai/Projects/pytorch/torch/_dynamo/utils.py", line 2006, in import_submodule
    importlib.import_module(f"{mod.__name__}.{filename[:-3]}")
  File "/home/PanXuehai/Miniconda3/envs/pytorch-deps/lib/python3.12/importlib/__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/kernel/bmm.py", line 19, in <module>
    from .mm import _is_static_problem
ImportError: cannot import name '_is_static_problem' from partially initialized module 'torch._inductor.kernel.mm' (most likely due to a circular import) (/home/PanXuehai/Projects/pytorch/torch/_inductor/kernel/mm.py)
==========================================================================================================
==========================================================================================================
import torch._inductor.kernel.mm_common                                                          # FAILURE

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/kernel/__init__.py", line 1, in <module>
    from . import mm, mm_common, mm_plus_mm, unpack_mixed_mm
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/kernel/mm.py", line 6, in <module>
    from torch._inductor.codegen.cpp_gemm_template import CppPackedGemmTemplate
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/codegen/cpp_gemm_template.py", line 5, in <module>
    from .. import ir, lowering as L
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/lowering.py", line 6091, in <module>
    import_submodule(kernel)
  File "/home/PanXuehai/Projects/pytorch/torch/_dynamo/utils.py", line 2006, in import_submodule
    importlib.import_module(f"{mod.__name__}.{filename[:-3]}")
  File "/home/PanXuehai/Miniconda3/envs/pytorch-deps/lib/python3.12/importlib/__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/kernel/bmm.py", line 19, in <module>
    from .mm import _is_static_problem
ImportError: cannot import name '_is_static_problem' from partially initialized module 'torch._inductor.kernel.mm' (most likely due to a circular import) (/home/PanXuehai/Projects/pytorch/torch/_inductor/kernel/mm.py)
==========================================================================================================
==========================================================================================================
import torch._inductor.kernel.unpack_mixed_mm                                                    # FAILURE

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/kernel/__init__.py", line 1, in <module>
    from . import mm, mm_common, mm_plus_mm, unpack_mixed_mm
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/kernel/mm.py", line 6, in <module>
    from torch._inductor.codegen.cpp_gemm_template import CppPackedGemmTemplate
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/codegen/cpp_gemm_template.py", line 5, in <module>
    from .. import ir, lowering as L
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/lowering.py", line 6091, in <module>
    import_submodule(kernel)
  File "/home/PanXuehai/Projects/pytorch/torch/_dynamo/utils.py", line 2006, in import_submodule
    importlib.import_module(f"{mod.__name__}.{filename[:-3]}")
  File "/home/PanXuehai/Miniconda3/envs/pytorch-deps/lib/python3.12/importlib/__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/kernel/bmm.py", line 19, in <module>
    from .mm import _is_static_problem
ImportError: cannot import name '_is_static_problem' from partially initialized module 'torch._inductor.kernel.mm' (most likely due to a circular import) (/home/PanXuehai/Projects/pytorch/torch/_inductor/kernel/mm.py)
==========================================================================================================
==========================================================================================================
import torch._inductor.kernel.mm_plus_mm                                                         # FAILURE

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/kernel/__init__.py", line 1, in <module>
    from . import mm, mm_common, mm_plus_mm, unpack_mixed_mm
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/kernel/mm.py", line 6, in <module>
    from torch._inductor.codegen.cpp_gemm_template import CppPackedGemmTemplate
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/codegen/cpp_gemm_template.py", line 5, in <module>
    from .. import ir, lowering as L
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/lowering.py", line 6091, in <module>
    import_submodule(kernel)
  File "/home/PanXuehai/Projects/pytorch/torch/_dynamo/utils.py", line 2006, in import_submodule
    importlib.import_module(f"{mod.__name__}.{filename[:-3]}")
  File "/home/PanXuehai/Miniconda3/envs/pytorch-deps/lib/python3.12/importlib/__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/kernel/bmm.py", line 19, in <module>
    from .mm import _is_static_problem
ImportError: cannot import name '_is_static_problem' from partially initialized module 'torch._inductor.kernel.mm' (most likely due to a circular import) (/home/PanXuehai/Projects/pytorch/torch/_inductor/kernel/mm.py)
==========================================================================================================
==========================================================================================================
import torch._inductor.mkldnn_lowerings                                                          # FAILURE

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/mkldnn_lowerings.py", line 5, in <module>
    from torch._inductor.kernel.mm_common import mm_args
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/kernel/__init__.py", line 1, in <module>
    from . import mm, mm_common, mm_plus_mm, unpack_mixed_mm
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/kernel/mm.py", line 6, in <module>
    from torch._inductor.codegen.cpp_gemm_template import CppPackedGemmTemplate
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/codegen/cpp_gemm_template.py", line 5, in <module>
    from .. import ir, lowering as L
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/lowering.py", line 6091, in <module>
    import_submodule(kernel)
  File "/home/PanXuehai/Projects/pytorch/torch/_dynamo/utils.py", line 2006, in import_submodule
    importlib.import_module(f"{mod.__name__}.{filename[:-3]}")
  File "/home/PanXuehai/Miniconda3/envs/pytorch-deps/lib/python3.12/importlib/__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/kernel/bmm.py", line 19, in <module>
    from .mm import _is_static_problem
ImportError: cannot import name '_is_static_problem' from partially initialized module 'torch._inductor.kernel.mm' (most likely due to a circular import) (/home/PanXuehai/Projects/pytorch/torch/_inductor/kernel/mm.py)
==========================================================================================================
==========================================================================================================
import torch._inductor.quantized_lowerings                                                       # FAILURE

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/quantized_lowerings.py", line 2, in <module>
    from . import lowering
  File "/home/PanXuehai/Projects/pytorch/torch/_inductor/lowering.py", line 6095, in <module>
    quantized_lowerings.register_quantized_ops()
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: partially initialized module 'torch._inductor.quantized_lowerings' has no attribute 'register_quantized_ops' (most likely due to a circular import)
==========================================================================================================
==========================================================================================================
import torch.ao.pruning._experimental.data_sparsifier.benchmarks.dlrm_utils                      # FAILURE

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/PanXuehai/Projects/pytorch/torch/ao/pruning/_experimental/data_sparsifier/benchmarks/dlrm_utils.py", line 2, in <module>
    from dlrm_s_pytorch import DLRM_Net  # type: ignore[import]
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ModuleNotFoundError: No module named 'dlrm_s_pytorch'
==========================================================================================================
==========================================================================================================
import torch.ao.pruning._experimental.data_sparsifier.benchmarks.evaluate_disk_savings           # FAILURE

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/PanXuehai/Projects/pytorch/torch/ao/pruning/_experimental/data_sparsifier/benchmarks/evaluate_disk_savings.py", line 6, in <module>
    from dlrm_utils import get_dlrm_model, get_valid_name  # type: ignore[import]
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ModuleNotFoundError: No module named 'dlrm_utils'
==========================================================================================================
==========================================================================================================
import torch.ao.pruning._experimental.data_sparsifier.benchmarks.evaluate_forward_time           # FAILURE

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/PanXuehai/Projects/pytorch/torch/ao/pruning/_experimental/data_sparsifier/benchmarks/evaluate_forward_time.py", line 3, in <module>
    from dlrm_s_pytorch import unpack_batch  # type: ignore[import]
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ModuleNotFoundError: No module named 'dlrm_s_pytorch'
==========================================================================================================
==========================================================================================================
import torch.ao.pruning._experimental.data_sparsifier.benchmarks.evaluate_model_metrics          # FAILURE

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/PanXuehai/Projects/pytorch/torch/ao/pruning/_experimental/data_sparsifier/benchmarks/evaluate_model_metrics.py", line 3, in <module>
    from dlrm_s_pytorch import unpack_batch  # type: ignore[import]
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ModuleNotFoundError: No module named 'dlrm_s_pytorch'
==========================================================================================================
==========================================================================================================
import torch.ao.pruning._experimental.data_sparsifier.lightning.tests.test_callbacks             # FAILURE

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/PanXuehai/Projects/pytorch/torch/ao/pruning/_experimental/data_sparsifier/lightning/tests/test_callbacks.py", line 6, in <module>
    from torch.ao.pruning._experimental.data_sparsifier.lightning.callbacks.data_sparsity import (
  File "/home/PanXuehai/Projects/pytorch/torch/ao/pruning/_experimental/data_sparsifier/lightning/callbacks/data_sparsity.py", line 4, in <module>
    import pytorch_lightning as pl  # type: ignore[import]
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ModuleNotFoundError: No module named 'pytorch_lightning'
==========================================================================================================
==========================================================================================================
import torch.ao.pruning._experimental.data_sparsifier.lightning.callbacks.data_sparsity          # FAILURE

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/PanXuehai/Projects/pytorch/torch/ao/pruning/_experimental/data_sparsifier/lightning/callbacks/data_sparsity.py", line 4, in <module>
    import pytorch_lightning as pl  # type: ignore[import]
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ModuleNotFoundError: No module named 'pytorch_lightning'
==========================================================================================================
==========================================================================================================
import torch.backends._coreml.preprocess                                                         # FAILURE

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/PanXuehai/Projects/pytorch/torch/backends/_coreml/preprocess.py", line 5, in <module>
    import coremltools as ct  # type: ignore[import]
    ^^^^^^^^^^^^^^^^^^^^^^^^
ModuleNotFoundError: No module named 'coremltools'
==========================================================================================================
==========================================================================================================
import torch.contrib._tensorboard_vis                                                            # FAILURE

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/PanXuehai/Projects/pytorch/torch/contrib/_tensorboard_vis.py", line 17, in <module>
    raise ImportError("TensorBoard visualization of GraphExecutors requires having "
ImportError: TensorBoard visualization of GraphExecutors requires having TensorFlow installed
==========================================================================================================
==========================================================================================================
import torch.csrc.lazy.test_mnist                                                                # FAILURE

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/PanXuehai/Projects/pytorch/torch/csrc/lazy/test_mnist.py", line 5, in <module>
    from torchvision import datasets, transforms
ModuleNotFoundError: No module named 'torchvision'
==========================================================================================================
==========================================================================================================
import torch.distributed._tensor.examples.visualize_sharding_example                             # FAILURE

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/PanXuehai/Projects/pytorch/torch/distributed/_tensor/examples/visualize_sharding_example.py", line 7, in <module>
    world_size = int(os.environ["WORLD_SIZE"])
                     ~~~~~~~~~~^^^^^^^^^^^^^^
  File "<frozen os>", line 685, in __getitem__
KeyError: 'WORLD_SIZE'
==========================================================================================================
==========================================================================================================
import torch.distributed.elastic.rendezvous.etcd_rendezvous                                      # FAILURE

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/PanXuehai/Projects/pytorch/torch/distributed/elastic/rendezvous/etcd_rendezvous.py", line 16, in <module>
    import etcd  # type: ignore[import]
    ^^^^^^^^^^^
ModuleNotFoundError: No module named 'etcd'
==========================================================================================================
==========================================================================================================
import torch.distributed.elastic.rendezvous.etcd_rendezvous_backend                              # FAILURE

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/PanXuehai/Projects/pytorch/torch/distributed/elastic/rendezvous/etcd_rendezvous_backend.py", line 12, in <module>
    from etcd import Client as EtcdClient  # type: ignore[import]
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ModuleNotFoundError: No module named 'etcd'
==========================================================================================================
==========================================================================================================
import torch.distributed.elastic.rendezvous.etcd_store                                           # FAILURE

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/PanXuehai/Projects/pytorch/torch/distributed/elastic/rendezvous/etcd_store.py", line 13, in <module>
    import etcd  # type: ignore[import]
    ^^^^^^^^^^^
ModuleNotFoundError: No module named 'etcd'
==========================================================================================================
==========================================================================================================
import torch.distributed.examples.memory_tracker_example                                         # FAILURE

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/PanXuehai/Projects/pytorch/torch/distributed/examples/memory_tracker_example.py", line 2, in <module>
    import torchvision
ModuleNotFoundError: No module named 'torchvision'
==========================================================================================================
==========================================================================================================
import torch.distributed.tensor.parallel.fsdp                                                    # FAILURE

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/PanXuehai/Projects/pytorch/torch/distributed/tensor/parallel/fsdp.py", line 21, in <module>
    from torch.distributed.fsdp._common_utils import _set_fsdp_flattened
  File "/home/PanXuehai/Projects/pytorch/torch/distributed/fsdp/__init__.py", line 2, in <module>
    from .fully_sharded_data_parallel import (
  File "/home/PanXuehai/Projects/pytorch/torch/distributed/fsdp/fully_sharded_data_parallel.py", line 26, in <module>
    import torch.distributed.fsdp._traversal_utils as traversal_utils
  File "/home/PanXuehai/Projects/pytorch/torch/distributed/fsdp/_traversal_utils.py", line 12, in <module>
    from torch.distributed._composable.contract import _get_registry
  File "/home/PanXuehai/Projects/pytorch/torch/distributed/_composable/__init__.py", line 3, in <module>
    from .fully_shard import fully_shard
  File "/home/PanXuehai/Projects/pytorch/torch/distributed/_composable/fully_shard.py", line 12, in <module>
    from torch.distributed.fsdp._init_utils import (
  File "/home/PanXuehai/Projects/pytorch/torch/distributed/fsdp/_init_utils.py", line 58, in <module>
    from torch.distributed.tensor.parallel.fsdp import DTensorExtensions
ImportError: cannot import name 'DTensorExtensions' from partially initialized module 'torch.distributed.tensor.parallel.fsdp' (most likely due to a circular import) (/home/PanXuehai/Projects/pytorch/torch/distributed/tensor/parallel/fsdp.py)
==========================================================================================================
==========================================================================================================
import torch.export._unlift                                                                      # FAILURE

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/PanXuehai/Projects/pytorch/torch/export/_unlift.py", line 7, in <module>
    from torch._export.utils import _check_input_constraints_for_graph
  File "/home/PanXuehai/Projects/pytorch/torch/_export/__init__.py", line 43, in <module>
    from torch.export._unlift import _create_stateful_graph_module
ImportError: cannot import name '_create_stateful_graph_module' from partially initialized module 'torch.export._unlift' (most likely due to a circular import) (/home/PanXuehai/Projects/pytorch/torch/export/_unlift.py)
==========================================================================================================
==========================================================================================================
import torch.testing._internal.distributed.distributed_test                                      # FAILURE

/home/PanXuehai/Projects/pytorch/torch/testing/_internal/distributed/distributed_test.py:40: DeprecationWarning: `TorchScript` support for functional optimizers is deprecated and will be removed in a future PyTorch release. Consider using the `torch.compile` optimizer instead.
  from torch.distributed.optim import _apply_optimizer_in_backward
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/PanXuehai/Projects/pytorch/torch/testing/_internal/distributed/distributed_test.py", line 191, in <module>
    BACKEND = os.environ["BACKEND"]
              ~~~~~~~~~~^^^^^^^^^^^
  File "<frozen os>", line 685, in __getitem__
KeyError: 'BACKEND'
==========================================================================================================
==========================================================================================================
import torch.utils.model_dump.__main__                                                           # FAILURE

usage: -c [-h] [--style {json,html}] [--title TITLE] model
-c: error: the following arguments are required: model
==========================================================================================================

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Failed to import 32 module(s):
  torch._inductor.codegen.cpp_gemm_template
  torch._inductor.codegen.cpp_micro_gemm
  torch._inductor.codegen.cpp_template
  torch._inductor.codegen.cpp_template_kernel
  torch._inductor.kernel
  torch._inductor.kernel.bmm
  torch._inductor.kernel.conv
  torch._inductor.kernel.flex_attention
  torch._inductor.kernel.mm
  torch._inductor.kernel.mm_common
  torch._inductor.kernel.mm_plus_mm
  torch._inductor.kernel.unpack_mixed_mm
  torch._inductor.mkldnn_lowerings
  torch._inductor.quantized_lowerings
  torch.ao.pruning._experimental.data_sparsifier.benchmarks.dlrm_utils
  torch.ao.pruning._experimental.data_sparsifier.benchmarks.evaluate_disk_savings
  torch.ao.pruning._experimental.data_sparsifier.benchmarks.evaluate_forward_time
  torch.ao.pruning._experimental.data_sparsifier.benchmarks.evaluate_model_metrics
  torch.ao.pruning._experimental.data_sparsifier.lightning.callbacks.data_sparsity
  torch.ao.pruning._experimental.data_sparsifier.lightning.tests.test_callbacks
  torch.backends._coreml.preprocess
  torch.contrib._tensorboard_vis
  torch.csrc.lazy.test_mnist
  torch.distributed._tensor.examples.visualize_sharding_example
  torch.distributed.elastic.rendezvous.etcd_rendezvous
  torch.distributed.elastic.rendezvous.etcd_rendezvous_backend
  torch.distributed.elastic.rendezvous.etcd_store
  torch.distributed.examples.memory_tracker_example
  torch.distributed.tensor.parallel.fsdp
  torch.export._unlift
  torch.testing._internal.distributed.distributed_test
  torch.utils.model_dump.__main__
```

</details>

### Versions

nightly

cc @mruberry @ZainRizvi @albanD