Read the init and extracts PyTorch, TensorFlow, SentencePiece and Tokenizers objects.
()
| 65 | |
| 66 | |
| 67 | def read_init(): |
| 68 | """Read the init and extracts PyTorch, TensorFlow, SentencePiece and Tokenizers objects.""" |
| 69 | with open(os.path.join(PATH_TO_DIFFUSERS, "__init__.py"), "r", encoding="utf-8", newline="\n") as f: |
| 70 | lines = f.readlines() |
| 71 | |
| 72 | # Get to the point we do the actual imports for type checking |
| 73 | line_index = 0 |
| 74 | while not lines[line_index].startswith("if TYPE_CHECKING"): |
| 75 | line_index += 1 |
| 76 | |
| 77 | backend_specific_objects = {} |
| 78 | # Go through the end of the file |
| 79 | while line_index < len(lines): |
| 80 | # If the line contains is_backend_available, we grab all objects associated with the `else` block |
| 81 | backend = find_backend(lines[line_index]) |
| 82 | if backend is not None: |
| 83 | while not lines[line_index].startswith(" else:"): |
| 84 | line_index += 1 |
| 85 | line_index += 1 |
| 86 | objects = [] |
| 87 | # Until we unindent, add backend objects to the list |
| 88 | while len(lines[line_index]) <= 1 or lines[line_index].startswith(" " * 8): |
| 89 | line = lines[line_index] |
| 90 | single_line_import_search = _re_single_line_import.search(line) |
| 91 | if single_line_import_search is not None: |
| 92 | objects.extend(single_line_import_search.groups()[0].split(", ")) |
| 93 | elif line.startswith(" " * 12): |
| 94 | objects.append(line[12:-2]) |
| 95 | line_index += 1 |
| 96 | |
| 97 | if len(objects) > 0: |
| 98 | backend_specific_objects[backend] = objects |
| 99 | else: |
| 100 | line_index += 1 |
| 101 | |
| 102 | return backend_specific_objects |
| 103 | |
| 104 | |
| 105 | def create_dummy_object(name, backend_name): |
searching dependent graphs…