File size: 1,443 Bytes
b03a8f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# modeling_auto.py
from collections import OrderedDict

from transformers.configuration_utils import PretrainedConfig
from transformers.utils import logging

from ...models.camn_audio.modeling_camn_audio import CamnAudioModel
from ...models.camn_audio.configuration_camn_audio import CamnAudioConfig

logger = logging.get_logger(__name__)

MODEL_MAPPING = OrderedDict(
    [
        (CamnAudioConfig, CamnAudioModel),
        # Add other model mappings here if needed
    ]
)


class AutoModel:
    r"""
    AutoModel is a generic model class to instantiate a model from a configuration.
    It is designed to be instantiated using the `from_pretrained` method:
        model = AutoModel.from_pretrained("model_name_or_path")
    """

    @classmethod
    def from_pretrained(cls, pretrained_model_name_or_path, *model_args, **kwargs):
        from transformers import PretrainedConfig
        config = PretrainedConfig.from_pretrained(pretrained_model_name_or_path, **kwargs)
        for config_class, model_class in MODEL_MAPPING.items():
            if isinstance(config, config_class):
                return model_class.from_pretrained(pretrained_model_name_or_path, *model_args, **kwargs)

        raise ValueError(
            f"Unrecognized configuration class {config.__class__} for this kind of AutoModel: {cls.__name__}.\n"
            f"Model type should be one of {', '.join(c.__name__ for c in MODEL_MAPPING.keys())}."
        )