opensi·si

In many cases, the architecture to use can be inferred from the name or path of the pretrained model provided to the frompretrained() method.

Auto classes

In many cases, the architecture to use can be inferred from the name or path of the pretrained model provided to the from_pretrained() method. Instantiating AutoConfig, AutoModel, or AutoTokenizer automatically creates the appropriate architecture class. For example:

model = AutoModel.from_pretrained("google-bert/bert-base-cased", device_map="auto")

This creates a model instance of BertModel.

Each task has a corresponding AutoModel class.

Extending the Auto Classes

Each auto class provides a method that allows it to be extended with custom classes. For example, if you define a custom model class NewModel, ensure that you also define a corresponding NewModelConfig. You can then register them with the auto classes as shown below:

from transformers import AutoConfig, AutoModel

AutoConfig.register("new-model", NewModelConfig)
AutoModel.register(NewModelConfig, NewModel)

You can then use the auto classes as you normally would.

If your NewModelConfig is a subclass of transformers.PreTrainedConfig, make sure its model_type attribute is set to the same key you use when registering the config (here "new-model").

Likewise, if your NewModel is a subclass of PreTrainedModel, make sure its config_class attribute is set to the same class you use when registering the model (here NewModelConfig).

AutoConfig

[[autodoc]] AutoConfig

AutoTokenizer

[[autodoc]] AutoTokenizer

AutoFeatureExtractor

[[autodoc]] AutoFeatureExtractor

AutoImageProcessor

[[autodoc]] AutoImageProcessor

AutoVideoProcessor

See Video processor backends for backend selection, custom registration, and saving and reloading.

[[autodoc]] AutoVideoProcessor

AutoProcessor

[[autodoc]] AutoProcessor