Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
shivamshrirao
GitHub Repository: shivamshrirao/diffusers
Path: blob/main/utils/check_config_docstrings.py
1440 views
1
# coding=utf-8
2
# Copyright 2023 The HuggingFace Inc. team.
3
#
4
# Licensed under the Apache License, Version 2.0 (the "License");
5
# you may not use this file except in compliance with the License.
6
# You may obtain a copy of the License at
7
#
8
# http://www.apache.org/licenses/LICENSE-2.0
9
#
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
15
16
import importlib
17
import inspect
18
import os
19
import re
20
21
22
# All paths are set with the intent you should run this script from the root of the repo with the command
23
# python utils/check_config_docstrings.py
24
PATH_TO_TRANSFORMERS = "src/transformers"
25
26
27
# This is to make sure the transformers module imported is the one in the repo.
28
spec = importlib.util.spec_from_file_location(
29
"transformers",
30
os.path.join(PATH_TO_TRANSFORMERS, "__init__.py"),
31
submodule_search_locations=[PATH_TO_TRANSFORMERS],
32
)
33
transformers = spec.loader.load_module()
34
35
CONFIG_MAPPING = transformers.models.auto.configuration_auto.CONFIG_MAPPING
36
37
# Regex pattern used to find the checkpoint mentioned in the docstring of `config_class`.
38
# For example, `[bert-base-uncased](https://huggingface.co/bert-base-uncased)`
39
_re_checkpoint = re.compile("\[(.+?)\]\((https://huggingface\.co/.+?)\)")
40
41
42
CONFIG_CLASSES_TO_IGNORE_FOR_DOCSTRING_CHECKPOINT_CHECK = {
43
"CLIPConfigMixin",
44
"DecisionTransformerConfigMixin",
45
"EncoderDecoderConfigMixin",
46
"RagConfigMixin",
47
"SpeechEncoderDecoderConfigMixin",
48
"VisionEncoderDecoderConfigMixin",
49
"VisionTextDualEncoderConfigMixin",
50
}
51
52
53
def check_config_docstrings_have_checkpoints():
54
configs_without_checkpoint = []
55
56
for config_class in list(CONFIG_MAPPING.values()):
57
checkpoint_found = False
58
59
# source code of `config_class`
60
config_source = inspect.getsource(config_class)
61
checkpoints = _re_checkpoint.findall(config_source)
62
63
for checkpoint in checkpoints:
64
# Each `checkpoint` is a tuple of a checkpoint name and a checkpoint link.
65
# For example, `('bert-base-uncased', 'https://huggingface.co/bert-base-uncased')`
66
ckpt_name, ckpt_link = checkpoint
67
68
# verify the checkpoint name corresponds to the checkpoint link
69
ckpt_link_from_name = f"https://huggingface.co/{ckpt_name}"
70
if ckpt_link == ckpt_link_from_name:
71
checkpoint_found = True
72
break
73
74
name = config_class.__name__
75
if not checkpoint_found and name not in CONFIG_CLASSES_TO_IGNORE_FOR_DOCSTRING_CHECKPOINT_CHECK:
76
configs_without_checkpoint.append(name)
77
78
if len(configs_without_checkpoint) > 0:
79
message = "\n".join(sorted(configs_without_checkpoint))
80
raise ValueError(f"The following configurations don't contain any valid checkpoint:\n{message}")
81
82
83
if __name__ == "__main__":
84
check_config_docstrings_have_checkpoints()
85
86