MCPcopy
hub / github.com/huggingface/transformers / get_tiny_config

Function get_tiny_config

utils/create_dummy_models.py:591–716  ·  view source on GitHub ↗

Retrieve a tiny configuration from `config_class` using each model's `ModelTester`. Args: config_class: Subclass of `PreTrainedConfig`. Returns: An instance of `config_class` with tiny hyperparameters

(config_class, model_class=None, **model_tester_kwargs)

Source from the content-addressed store, hash-verified

589
590# TODO: Sam2Video will fail here
591def get_tiny_config(config_class, model_class=None, **model_tester_kwargs):
592 """Retrieve a tiny configuration from `config_class` using each model's `ModelTester`.
593
594 Args:
595 config_class: Subclass of `PreTrainedConfig`.
596
597 Returns:
598 An instance of `config_class` with tiny hyperparameters
599 """
600 model_type = config_class.model_type
601
602 # For model type like `data2vec-vision` and `donut-swin`, we can't get the config/model file name directly via
603 # `model_type` as it would be sth. like `configuration_data2vec_vision.py`.
604 # A simple way is to use `inspect.getsourcefile(config_class)`.
605 config_source_file = inspect.getsourcefile(config_class)
606 # The modeling file name without prefix (`modeling_`) and postfix (`.py`)
607 modeling_name = config_source_file.split(os.path.sep)[-1].replace("configuration_", "").replace(".py", "")
608 # TODO: remark: several configuration classes might be defined in the same modeling directory.
609 # The test directory is still the same, so we are good here.
610
611 try:
612 print("Importing", model_type_to_module_name(model_type))
613 module_name = model_type_to_module_name(model_type)
614 if not modeling_name.startswith(module_name):
615 raise ValueError(f"{modeling_name} doesn't start with {module_name}!")
616 test_file = os.path.join("tests", "models", module_name, f"test_modeling_{modeling_name}.py")
617 models_to_model_testers = get_model_to_tester_mapping(test_file)
618 # Find the model tester class
619 model_tester_class = None
620 tester_classes = []
621 if model_class is not None:
622 tester_classes = get_tester_classes_for_model(test_file, model_class)
623 else:
624 for _tester_classes in models_to_model_testers.values():
625 tester_classes.extend(_tester_classes)
626 if len(tester_classes) > 0:
627 # sort with the length of the class names first, then the alphabetical order
628 # This is to avoid `T5EncoderOnlyModelTest` is used instead of `T5ModelTest`, which has
629 # `is_encoder_decoder=False` and causes some pipeline tests failing (also failures in `Optimum` CI).
630 # TODO: More fine grained control of the desired tester class.
631 model_tester_class = min(tester_classes, key=lambda x: (len(x.__name__), x.__name__))
632
633 # TODO: SpeechT5ForSpeechToText needs a particular tester to get the working config
634 # TODO: this is hacky however, as all model classes share the same config class but having different tester
635 # TODO: make this more flexible and roubst
636 if config_class.__name__ == "SpeechT5Config":
637 for x in tester_classes:
638 if x.__name__ == "SpeechT5ForSpeechToTextTester":
639 model_tester_class = x
640 break
641
642 except ModuleNotFoundError:
643 error = f"Tiny config not created for {model_type} - cannot find the testing module from the model name."
644 raise ValueError(error)
645
646 if model_tester_class is None:
647 error = f"Tiny config not created for {model_type} - no model tester is found in the testing module."
648 raise ValueError(error)

Callers 2

get_config_overridesFunction · 0.85
buildFunction · 0.85

Calls 11

get_test_moduleFunction · 0.90
_get_exact_configFunction · 0.85
splitMethod · 0.80
joinMethod · 0.80
valuesMethod · 0.45
popMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected