You can find the complete jupyter notebook in our examples repository.
Import loop in pytorch/torchgen/model.py
Visualize Import Loops in PyTorch
Using Codegen, we discovered several import cycles in PyTorch’s codebase. The code to gather and visualize these loops is as follows:Import loops in pytorch/torchgen/model.py

import_symbol.is_dynamic
property. If any edge in a strongly connected component is dynamic, runtime conflicts are typically resolved.
However, we discovered an import loop worth investigating between flex_decoding.py and flex_attention.py:

flex_decoding.py
imports flex_attention.py
twice — once dynamically and once at top-level. This mixed static/dynamic import pattern from the same module creates potential runtime instability.
Thus, we propose the following refactoring using Codegen:
Move Shared Code to a Separate utils.py
File
utils.py
as well as resolve the imports from both files to point to the newly created file solving this potential unpredictable error that could lead issues later on.