CoolFace
Apppublic

chendl/compositional_test

sourceHugging Faceupdated 3y agoView on Hugging Face
1likes
check_task_guides.py127 linesDownload Raw Back to utils
1# coding=utf-82# 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 at7#8#     http://www.apache.org/licenses/LICENSE-2.09#10# Unless required by applicable law or agreed to in writing, software11# 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 and14# limitations under the License.15 16import argparse17import os18 19from transformers.utils import direct_transformers_import20 21 22# All paths are set with the intent you should run this script from the root of the repo with the command23# python utils/check_task_guides.py24TRANSFORMERS_PATH = "src/transformers"25PATH_TO_TASK_GUIDES = "docs/source/en/tasks"26 27 28def _find_text_in_file(filename, start_prompt, end_prompt):29    """30    Find the text in `filename` between a line beginning with `start_prompt` and before `end_prompt`, removing empty31    lines.32    """33    with open(filename, "r", encoding="utf-8", newline="\n") as f:34        lines = f.readlines()35    # Find the start prompt.36    start_index = 037    while not lines[start_index].startswith(start_prompt):38        start_index += 139    start_index += 140 41    end_index = start_index42    while not lines[end_index].startswith(end_prompt):43        end_index += 144    end_index -= 145 46    while len(lines[start_index]) <= 1:47        start_index += 148    while len(lines[end_index]) <= 1:49        end_index -= 150    end_index += 151    return "".join(lines[start_index:end_index]), start_index, end_index, lines52 53 54# This is to make sure the transformers module imported is the one in the repo.55transformers_module = direct_transformers_import(TRANSFORMERS_PATH)56 57TASK_GUIDE_TO_MODELS = {58    "asr.mdx": transformers_module.models.auto.modeling_auto.MODEL_FOR_CTC_MAPPING_NAMES,59    "audio_classification.mdx": transformers_module.models.auto.modeling_auto.MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING_NAMES,60    "language_modeling.mdx": transformers_module.models.auto.modeling_auto.MODEL_FOR_CAUSAL_LM_MAPPING_NAMES,61    "image_classification.mdx": transformers_module.models.auto.modeling_auto.MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING_NAMES,62    "masked_language_modeling.mdx": transformers_module.models.auto.modeling_auto.MODEL_FOR_MASKED_LM_MAPPING_NAMES,63    "multiple_choice.mdx": transformers_module.models.auto.modeling_auto.MODEL_FOR_MULTIPLE_CHOICE_MAPPING_NAMES,64    "object_detection.mdx": transformers_module.models.auto.modeling_auto.MODEL_FOR_OBJECT_DETECTION_MAPPING_NAMES,65    "question_answering.mdx": transformers_module.models.auto.modeling_auto.MODEL_FOR_QUESTION_ANSWERING_MAPPING_NAMES,66    "semantic_segmentation.mdx": transformers_module.models.auto.modeling_auto.MODEL_FOR_SEMANTIC_SEGMENTATION_MAPPING_NAMES,67    "sequence_classification.mdx": transformers_module.models.auto.modeling_auto.MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING_NAMES,68    "summarization.mdx": transformers_module.models.auto.modeling_auto.MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES,69    "token_classification.mdx": transformers_module.models.auto.modeling_auto.MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMES,70    "translation.mdx": transformers_module.models.auto.modeling_auto.MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES,71    "video_classification.mdx": transformers_module.models.auto.modeling_auto.MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING_NAMES,72    "document_question_answering.mdx": transformers_module.models.auto.modeling_auto.MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING_NAMES,73    "monocular_depth_estimation.mdx": transformers_module.models.auto.modeling_auto.MODEL_FOR_DEPTH_ESTIMATION_MAPPING_NAMES,74}75 76# This list contains model types used in some task guides that are not in `CONFIG_MAPPING_NAMES` (therefore not in any77# `MODEL_MAPPING_NAMES` or any `MODEL_FOR_XXX_MAPPING_NAMES`).78SPECIAL_TASK_GUIDE_TO_MODEL_TYPES = {79    "summarization.mdx": ("nllb",),80    "translation.mdx": ("nllb",),81}82 83 84def get_model_list_for_task(task_guide):85    """86    Return the list of models supporting given task.87    """88    model_maping_names = TASK_GUIDE_TO_MODELS[task_guide]89    special_model_types = SPECIAL_TASK_GUIDE_TO_MODEL_TYPES.get(task_guide, set())90    model_names = {91        code: name92        for code, name in transformers_module.MODEL_NAMES_MAPPING.items()93        if (code in model_maping_names or code in special_model_types)94    }95    return ", ".join([f"[{name}](../model_doc/{code})" for code, name in model_names.items()]) + "\n"96 97 98def check_model_list_for_task(task_guide, overwrite=False):99    """For a given task guide, checks the model list in the generated tip for consistency with the state of the lib and overwrites if needed."""100 101    current_list, start_index, end_index, lines = _find_text_in_file(102        filename=os.path.join(PATH_TO_TASK_GUIDES, task_guide),103        start_prompt="<!--This tip is automatically generated by `make fix-copies`, do not fill manually!-->",104        end_prompt="<!--End of the generated tip-->",105    )106 107    new_list = get_model_list_for_task(task_guide)108 109    if current_list != new_list:110        if overwrite:111            with open(os.path.join(PATH_TO_TASK_GUIDES, task_guide), "w", encoding="utf-8", newline="\n") as f:112                f.writelines(lines[:start_index] + [new_list] + lines[end_index:])113        else:114            raise ValueError(115                f"The list of models that can be used in the {task_guide} guide needs an update. Run `make fix-copies`"116                " to fix this."117            )118 119 120if __name__ == "__main__":121    parser = argparse.ArgumentParser()122    parser.add_argument("--fix_and_overwrite", action="store_true", help="Whether to fix inconsistencies.")123    args = parser.parse_args()124 125    for task_guide in TASK_GUIDE_TO_MODELS.keys():126        check_model_list_for_task(task_guide, args.fix_and_overwrite)127