Path: blob/main/utils/check_config_docstrings.py
1440 views
# coding=utf-81# Copyright 2023 The HuggingFace Inc. team.2#3# Licensed under the Apache License, Version 2.0 (the "License");4# you may not use this file except in compliance with the License.5# You may obtain a copy of the License at6#7# http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# distributed under the License is distributed on an "AS IS" BASIS,11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12# See the License for the specific language governing permissions and13# limitations under the License.1415import importlib16import inspect17import os18import re192021# All paths are set with the intent you should run this script from the root of the repo with the command22# python utils/check_config_docstrings.py23PATH_TO_TRANSFORMERS = "src/transformers"242526# This is to make sure the transformers module imported is the one in the repo.27spec = importlib.util.spec_from_file_location(28"transformers",29os.path.join(PATH_TO_TRANSFORMERS, "__init__.py"),30submodule_search_locations=[PATH_TO_TRANSFORMERS],31)32transformers = spec.loader.load_module()3334CONFIG_MAPPING = transformers.models.auto.configuration_auto.CONFIG_MAPPING3536# Regex pattern used to find the checkpoint mentioned in the docstring of `config_class`.37# For example, `[bert-base-uncased](https://huggingface.co/bert-base-uncased)`38_re_checkpoint = re.compile("\[(.+?)\]\((https://huggingface\.co/.+?)\)")394041CONFIG_CLASSES_TO_IGNORE_FOR_DOCSTRING_CHECKPOINT_CHECK = {42"CLIPConfigMixin",43"DecisionTransformerConfigMixin",44"EncoderDecoderConfigMixin",45"RagConfigMixin",46"SpeechEncoderDecoderConfigMixin",47"VisionEncoderDecoderConfigMixin",48"VisionTextDualEncoderConfigMixin",49}505152def check_config_docstrings_have_checkpoints():53configs_without_checkpoint = []5455for config_class in list(CONFIG_MAPPING.values()):56checkpoint_found = False5758# source code of `config_class`59config_source = inspect.getsource(config_class)60checkpoints = _re_checkpoint.findall(config_source)6162for checkpoint in checkpoints:63# Each `checkpoint` is a tuple of a checkpoint name and a checkpoint link.64# For example, `('bert-base-uncased', 'https://huggingface.co/bert-base-uncased')`65ckpt_name, ckpt_link = checkpoint6667# verify the checkpoint name corresponds to the checkpoint link68ckpt_link_from_name = f"https://huggingface.co/{ckpt_name}"69if ckpt_link == ckpt_link_from_name:70checkpoint_found = True71break7273name = config_class.__name__74if not checkpoint_found and name not in CONFIG_CLASSES_TO_IGNORE_FOR_DOCSTRING_CHECKPOINT_CHECK:75configs_without_checkpoint.append(name)7677if len(configs_without_checkpoint) > 0:78message = "\n".join(sorted(configs_without_checkpoint))79raise ValueError(f"The following configurations don't contain any valid checkpoint:\n{message}")808182if __name__ == "__main__":83check_config_docstrings_have_checkpoints()848586