Diffusers documentation

AutoModel

You are viewing main version, which requires installation from source. If you'd like regular pip install, checkout the latest stable version (v0.42.1).
Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

AutoModel

AutoModel sınıfı, config.json dosyasından doğru model sınıfını (UNet, transformer, VAE) otomatik olarak algılar ve yükler. Önceden belirli model sınıfının adını bilmenize gerek yoktur. Veri türlerini ve cihaz yerleşimini destekler; farklı model türleri ve kütüphaneler arasında çalışır.

Aşağıdaki örnek, Diffusers'tan bir transformer ve Transformers'tan bir metin kodlayıcı yükler. config.json dosyasının nereden yükleneceğini belirtmek için subfolder parametresini kullanın.

import torch
from diffusers import AutoModel, DiffusionPipeline

transformer = AutoModel.from_pretrained(
    "Qwen/Qwen-Image", subfolder="transformer", dtype=torch.bfloat16, device_map="cuda"  # or "mps", "xpu", "cpu"
)

text_encoder = AutoModel.from_pretrained(
    "Qwen/Qwen-Image", subfolder="text_encoder", dtype=torch.bfloat16, device_map="cuda"
)

Özel modeller

AutoModel, Diffusers’da yer almayan Hub modellerini de yükler. Özel modelleri yüklemek için AutoModel.from_pretrained() içinde trust_remote_code=True değerini ayarlayın.

Özel bir model deposu, model sınıfını içeren bir Python modülü ve "AutoModel" anahtarını "module_file.ClassName" değerine eşleyen bir auto_map girdisi bulunan config.json dosyası gerektirir.

custom/custom-transformer-model/
├── config.json
├── my_model.py
└── diffusion_pytorch_model.safetensors

config.json, özel sınıfa işaret eden auto_map alanını içerir.

{
  "auto_map": {
    "AutoModel": "my_model.MyCustomModel"
  }
}

Ardından trust_remote_code=True ile yükleyin.

import torch
from diffusers import AutoModel

transformer = AutoModel.from_pretrained(
    "custom/custom-transformer-model", trust_remote_code=True, dtype=torch.bfloat16, device_map="cuda"  # or "mps", "xpu", "cpu"
)

Gerçek dünya örneği olarak, Overworld/Waypoint-1-Small, transformer alt klasöründeki birkaç modülde özel bir WorldModel sınıfı barındırır.

transformer/
├── config.json          # auto_map: "model.WorldModel"
├── model.py
├── attn.py
├── nn.py
├── cache.py
├── quantize.py
├── __init__.py
└── diffusion_pytorch_model.safetensors
import torch
from diffusers import AutoModel

transformer = AutoModel.from_pretrained(
    "Overworld/Waypoint-1-Small", subfolder="transformer", trust_remote_code=True, dtype=torch.bfloat16, device_map="cuda"  # or "mps", "xpu", "cpu"
)

Özel model ModelMixin sınıfından türetiliyorsa, regional compilation ve group offloading gibi Diffusers model sınıflarının aynı özelliklerine erişim sağlar.

trust_remote_code=True kullanırken bir önlem olarak, kodun yeni kötü amaçlı kodlarla güncellenmediğinden emin olmak için AutoModel.from_pretrained() içindeki revision argümanına bir commit hash'i iletin (model sahiplerine tamamen güvenmiyorsanız).

transformer = AutoModel.from_pretrained(
    "Overworld/Waypoint-1-Small", subfolder="transformer", trust_remote_code=True, revision="a3d8cb2"
)

Özel modellerin kaydedilmesi

Kaydetme sırasında config.json dosyasına auto_map girdisini otomatik olarak eklemek için register_for_auto_class() kullanın. Bu, yapılandırma dosyasını manuel olarak düzenleme gereksinimini ortadan kaldırır.

# my_model.py
from diffusers import ModelMixin, ConfigMixin

class MyCustomModel(ModelMixin, ConfigMixin):
    ...

MyCustomModel.register_for_auto_class("AutoModel")

model = MyCustomModel(...)
model.save_pretrained("./my_model")

Kaydedilen config.json, auto_map alanını içerecektir.

{
  "auto_map": {
    "AutoModel": "my_model.MyCustomModel"
  }
}

Sonraki adımlar

Özel modellerin uygulanması hakkında daha fazla bilgi için Topluluk bileşenleri kılavuzuna göz atın.

Update on GitHub