codingutf8 2023 the huggingface inc team licensed under the apache license version 2 0 the license you may not use this file except in compliance with the license you may obtain a copy of the license at http www apache orglicenseslicense2 0 unless required by applicable law or agreed to in writing software distributed under the license is distributed on an as is basis without warranties or conditions of any kind either express or implied see the license for the specific language governing permissions and limitations under the license this is required to make the module import works when the python process is running from the root of the repo def getmodulepathtestfile get the module of a model test file testmodulepath getmodulepathtestfile testmodule importlib importmoduletestmodulepath return testmodule def gettesterclassestestfile sort with class names get all test classes in a model test file with attribute allmodelclasses that are nonempty these are usually the model test classes containing the nonslow tests to run and are subclasses of one of the classes modeltestermixin tfmodeltestermixin or flaxmodeltestermixin as well as a subclass of unittest testcase exceptions include ragtestmixin and its subclasses tfflaxmodeltestermixin is also an attribute in specific model test module let s exclude them by checking allmodelclasses is not empty which also excludes other special classes sort with class names get all model classes that appear in allmodelclasses attributes in a model test file testclasses gettestclassestestfile modelclasses set for testclass in testclasses modelclasses updatetestclass allmodelclasses sort with class names return sortedmodelclasses keylambda x x name def getmodeltesterfromtestclasstestclass tfflaxmodeltestermixin has this attribute default to none let s skip this case get all test classes in testfile that have modelclass in their allmodelclasses testclasses gettestclassestestfile targettestclasses for testclass in testclasses if modelclass in testclass allmodelclasses targettestclasses appendtestclass sort with class names return sortedtargettestclasses keylambda x x name def gettesterclassesformodeltestfile modelclass sort with class names get a mapping from test classes to model tester classes in testfile this uses gettestclasses which may return classes that are not subclasses of unittest testcase get a mapping from model classes to test classes in testfile modelclasses getmodelclassestestfile modeltestmapping modelclass gettestclassesformodeltestfile modelclass for modelclass in modelclasses return modeltestmapping def getmodeltotestermappingtestfile make the information succinct and easy to read avoid the full class representation like class transformers models bert modelingbert bertformaskedlm when displaying the results instead we use class name bertformaskedlm for the readability coding utf 8 2023 the huggingface inc team licensed under the apache license version 2 0 the license you may not use this file except in compliance with the license you may obtain a copy of the license at http www apache org licenses license 2 0 unless required by applicable law or agreed to in writing software distributed under the license is distributed on an as is basis without warranties or conditions of any kind either express or implied see the license for the specific language governing permissions and limitations under the license this is required to make the module import works when the python process is running from the root of the repo the argument test_file in this file refers to a model test file this should be a string of the from tests models test_modeling_ py return the module path of a model test file get the module of a model test file get all classes in a model test file whose names ends with modeltester sort with class names get all test classes in a model test file with attribute all_model_classes that are non empty these are usually the model test classes containing the non slow tests to run and are subclasses of one of the classes modeltestermixin tfmodeltestermixin or flaxmodeltestermixin as well as a subclass of unittest testcase exceptions include ragtestmixin and its subclasses tf flax modeltestermixin is also an attribute in specific model test module let s exclude them by checking all_model_classes is not empty which also excludes other special classes sort with class names get all model classes that appear in all_model_classes attributes in a model test file sort with class names get the model tester class of a model test class tf flax modeltestermixin has this attribute default to none let s skip this case get all test classes in test_file that have model_class in their all_model_classes sort with class names get all model tester classes in test_file that are associated to model_class sort with class names get a mapping from test classes to model tester classes in test_file this uses get_test_classes which may return classes that are not subclasses of unittest testcase get a mapping from model classes to test classes in test_file get a mapping from model classes to model tester classes in test_file make the information succinct and easy to read avoid the full class representation like class transformers models bert modeling_bert bertformaskedlm when displaying the results instead we use class name bertformaskedlm for the readability
import importlib
import os
import sys



sys.path.append(".")


r


def get_module_path(test_file):
    
    components = test_file.split(os.path.sep)
    if components[0:2] != ["tests", "models"]:
        raise ValueError(
            "`test_file` should start with `tests/models/` (with `/` being the OS specific path separator). Got "
            f"{test_file} instead."
        )
    test_fn = components[-1]
    if not test_fn.endswith("py"):
        raise ValueError(f"`test_file` should be a python file. Got {test_fn} instead.")
    if not test_fn.startswith("test_modeling_"):
        raise ValueError(
            f"`test_file` should point to a file name of the form `test_modeling_*.py`. Got {test_fn} instead."
        )

    components = components[:-1] + [test_fn.replace(".py", "")]
    test_module_path = ".".join(components)

    return test_module_path


def get_test_module(test_file):
    
    test_module_path = get_module_path(test_file)
    test_module = importlib.import_module(test_module_path)

    return test_module


def get_tester_classes(test_file):
    
    tester_classes = []
    test_module = get_test_module(test_file)
    for attr in dir(test_module):
        if attr.endswith("ModelTester"):
            tester_classes.append(getattr(test_module, attr))

    
    return sorted(tester_classes, key=lambda x: x.__name__)


def get_test_classes(test_file):
    
    test_classes = []
    test_module = get_test_module(test_file)
    for attr in dir(test_module):
        attr_value = getattr(test_module, attr)
        
        
        model_classes = getattr(attr_value, "all_model_classes", [])
        if len(model_classes) > 0:
            test_classes.append(attr_value)

    
    return sorted(test_classes, key=lambda x: x.__name__)


def get_model_classes(test_file):
    
    test_classes = get_test_classes(test_file)
    model_classes = set()
    for test_class in test_classes:
        model_classes.update(test_class.all_model_classes)

    
    return sorted(model_classes, key=lambda x: x.__name__)


def get_model_tester_from_test_class(test_class):
    
    test = test_class()
    if hasattr(test, "setUp"):
        test.setUp()

    model_tester = None
    if hasattr(test, "model_tester"):
        
        if test.model_tester is not None:
            model_tester = test.model_tester.__class__

    return model_tester


def get_test_classes_for_model(test_file, model_class):
    
    test_classes = get_test_classes(test_file)

    target_test_classes = []
    for test_class in test_classes:
        if model_class in test_class.all_model_classes:
            target_test_classes.append(test_class)

    
    return sorted(target_test_classes, key=lambda x: x.__name__)


def get_tester_classes_for_model(test_file, model_class):
    
    test_classes = get_test_classes_for_model(test_file, model_class)

    tester_classes = []
    for test_class in test_classes:
        tester_class = get_model_tester_from_test_class(test_class)
        if tester_class is not None:
            tester_classes.append(tester_class)

    
    return sorted(tester_classes, key=lambda x: x.__name__)


def get_test_to_tester_mapping(test_file):
    
    test_classes = get_test_classes(test_file)
    test_tester_mapping = {test_class: get_model_tester_from_test_class(test_class) for test_class in test_classes}
    return test_tester_mapping


def get_model_to_test_mapping(test_file):
    
    model_classes = get_model_classes(test_file)
    model_test_mapping = {
        model_class: get_test_classes_for_model(test_file, model_class) for model_class in model_classes
    }
    return model_test_mapping


def get_model_to_tester_mapping(test_file):
    
    model_classes = get_model_classes(test_file)
    model_to_tester_mapping = {
        model_class: get_tester_classes_for_model(test_file, model_class) for model_class in model_classes
    }
    return model_to_tester_mapping


def to_json(o):
    
    if isinstance(o, str):
        return o
    elif isinstance(o, type):
        return o.__name__
    elif isinstance(o, (list, tuple)):
        return [to_json(x) for x in o]
    elif isinstance(o, dict):
        return {to_json(k): to_json(v) for k, v in o.items()}
    else:
        return o