2020
2121# All paths are set with the intent you should run this script from the root of the repo with the command
2222# python utils/check_dummies.py
23- PATH_TO_TRANSFORMERS = "src/transformers "
23+ PATH_TO_DIFFUSERS = "src/diffusers "
2424
2525# Matches is_xxx_available()
26- _re_backend = re .compile (r"is\_([a-z_]*)_available( )" )
26+ _re_backend = re .compile (r"if is\_([a-z_]*)_available\(\ )" )
2727# Matches from xxx import bla
2828_re_single_line_import = re .compile (r"\s+from\s+\S*\s+import\s+([^\(\s].*)\n" )
2929_re_test_backend = re .compile (r"^\s+if\s+not\s+is\_[a-z]*\_available\(\)" )
@@ -50,36 +50,30 @@ def {0}(*args, **kwargs):
5050
5151def find_backend (line ):
5252 """Find one (or multiple) backend in a code line of the init."""
53- if _re_test_backend .search (line ) is None :
53+ backends = _re_backend .findall (line )
54+ if len (backends ) == 0 :
5455 return None
55- backends = [b [0 ] for b in _re_backend .findall (line )]
56- backends .sort ()
57- return "_and_" .join (backends )
56+
57+ return backends [0 ]
5858
5959
6060def read_init ():
6161 """Read the init and extracts PyTorch, TensorFlow, SentencePiece and Tokenizers objects."""
62- with open (os .path .join (PATH_TO_TRANSFORMERS , "__init__.py" ), "r" , encoding = "utf-8" , newline = "\n " ) as f :
62+ with open (os .path .join (PATH_TO_DIFFUSERS , "__init__.py" ), "r" , encoding = "utf-8" , newline = "\n " ) as f :
6363 lines = f .readlines ()
6464
6565 # Get to the point we do the actual imports for type checking
6666 line_index = 0
67- while not lines [line_index ].startswith ("if TYPE_CHECKING" ):
68- line_index += 1
69-
7067 backend_specific_objects = {}
7168 # Go through the end of the file
7269 while line_index < len (lines ):
7370 # If the line is an if is_backend_available, we grab all objects associated.
7471 backend = find_backend (lines [line_index ])
7572 if backend is not None :
76- while not lines [line_index ].startswith (" else:" ):
77- line_index += 1
78- line_index += 1
79-
8073 objects = []
74+ line_index += 1
8175 # Until we unindent, add backend objects to the list
82- while len ( lines [ line_index ]) <= 1 or lines [line_index ].startswith (" " * 8 ):
76+ while not lines [line_index ].startswith ("else:" ):
8377 line = lines [line_index ]
8478 single_line_import_search = _re_single_line_import .search (line )
8579 if single_line_import_search is not None :
@@ -129,7 +123,7 @@ def check_dummies(overwrite=False):
129123 short_names = {"torch" : "pt" }
130124
131125 # Locate actual dummy modules and read their content.
132- path = os .path .join (PATH_TO_TRANSFORMERS , "utils" )
126+ path = os .path .join (PATH_TO_DIFFUSERS , "utils" )
133127 dummy_file_paths = {
134128 backend : os .path .join (path , f"dummy_{ short_names .get (backend , backend )} _objects.py" )
135129 for backend in dummy_files .keys ()
@@ -147,15 +141,15 @@ def check_dummies(overwrite=False):
147141 if dummy_files [backend ] != actual_dummies [backend ]:
148142 if overwrite :
149143 print (
150- f"Updating transformers .utils.dummy_{ short_names .get (backend , backend )} _objects.py as the main "
144+ f"Updating diffusers .utils.dummy_{ short_names .get (backend , backend )} _objects.py as the main "
151145 "__init__ has new objects."
152146 )
153147 with open (dummy_file_paths [backend ], "w" , encoding = "utf-8" , newline = "\n " ) as f :
154148 f .write (dummy_files [backend ])
155149 else :
156150 raise ValueError (
157151 "The main __init__ has objects that are not present in "
158- f"transformers .utils.dummy_{ short_names .get (backend , backend )} _objects.py. Run `make fix-copies` "
152+ f"diffusers .utils.dummy_{ short_names .get (backend , backend )} _objects.py. Run `make fix-copies` "
159153 "to fix this."
160154 )
161155
0 commit comments