Diffusers 文件
單個檔案
加入 Hugging Face 社群
並獲得增強的文件體驗
開始使用
單個檔案
方法 from_single_file() 允許您載入:
- 儲存在單個檔案中的模型,這在您使用來自擴散生態系統(如 Automatic1111)的模型,並且通常依賴於單個檔案佈局來儲存和共享模型時非常有用。
- 以其原始分發佈局儲存的模型,這在您使用透過其他服務進行微調的模型,並希望將其直接載入到 Diffusers 模型物件和管道中時非常有用。
閱讀 模型檔案和佈局 指南,瞭解 Diffusers 多資料夾佈局與單個檔案佈局之間的區別,以及如何載入儲存在這些不同佈局中的模型。
支援的管道
- StableDiffusionPipeline
- StableDiffusionImg2ImgPipeline
- StableDiffusionInpaintPipeline
- StableDiffusionControlNetPipeline
- StableDiffusionControlNetImg2ImgPipeline
- StableDiffusionControlNetInpaintPipeline
- StableDiffusionUpscalePipeline
- StableDiffusionXLPipeline
- StableDiffusionXLImg2ImgPipeline
- StableDiffusionXLInpaintPipeline
- StableDiffusionXLInstructPix2PixPipeline
- StableDiffusionXLControlNetPipeline
- StableDiffusionXLKDiffusionPipeline
- StableDiffusion3Pipeline
- LatentConsistencyModelPipeline
- LatentConsistencyModelImg2ImgPipeline
- StableDiffusionControlNetXSPipeline
- StableDiffusionXLControlNetXSPipeline
- LEditsPPPipelineStableDiffusion
- LEditsPPPipelineStableDiffusionXL
- PIAPipeline
支援的模型
- UNet2DConditionModel
StableCascadeUNet
- AutoencoderKL
- ControlNetModel
- SD3Transformer2DModel
- FluxTransformer2DModel
FromSingleFileMixin
將以 .ckpt
格式儲存的模型權重載入到 DiffusionPipeline 中。
from_single_file
< 來源 >( pretrained_model_link_or_path **kwargs )
引數
- pretrained_model_link_or_path (
str
或os.PathLike
, 可選) — 可以是:- Hub 上的
.ckpt
檔案連結(例如"https://huggingface.co/<repo_id>/blob/main/<path_to_file>.ckpt"
)。 - 包含所有管道權重的 檔案 路徑。
- Hub 上的
- torch_dtype (
str
或torch.dtype
, 可選) — 覆蓋預設的torch.dtype
並使用其他資料型別載入模型。 - force_download (
bool
, 可選, 預設為False
) — 是否強制(重新)下載模型權重和配置檔案,如果存在快取版本則覆蓋。 - cache_dir (
Union[str, os.PathLike]
, 可選) — 下載的預訓練模型配置的快取目錄路徑,如果未使用標準快取。 - proxies (
Dict[str, str]
, 可選) — 按協議或端點使用的代理伺服器字典,例如{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}
。代理在每個請求中都使用。 - local_files_only (
bool
, 可選, 預設為False
) — 是否只加載本地模型權重和配置檔案。如果設定為True
,模型將不會從 Hub 下載。 - token (
str
或 bool, 可選) — 用於遠端檔案的 HTTP bearer 授權令牌。如果為True
,則使用diffusers-cli login
生成的令牌(儲存在~/.huggingface
中)。 - revision (
str
, 可選, 預設為"main"
) — 要使用的特定模型版本。它可以是分支名稱、標籤名稱、提交 ID 或 Git 允許的任何識別符號。 - original_config_file (
str
, 可選) — 用於訓練模型的原始配置檔案路徑。如果未提供,將從檢查點檔案推斷配置檔案。 - config (
str
, 可選) — 可以是:- Hub 上預訓練管道的 repo id 字串(例如
CompVis/ldm-text2im-large-256
)。 - 包含 Diffusers 格式的管道元件配置的 目錄 路徑(例如
./my_pipeline_directory/
)。
- Hub 上預訓練管道的 repo id 字串(例如
- disable_mmap (‘bool’, 可選, 預設為 ‘False’) — 載入 Safetensors 模型時是否停用記憶體對映 (mmap)。當模型位於網路掛載或硬碟上時,此選項可以表現更好。
- kwargs (剩餘的關鍵字引數字典,可選) — 可用於覆蓋可載入和可儲存變數(特定管道類的管道元件)。被覆蓋的元件直接傳遞給管道的
__init__
方法。更多資訊請參閱下面的示例。
從以 .ckpt
或 .safetensors
格式儲存的預訓練管道權重例項化 DiffusionPipeline。預設情況下,管道設定為評估模式(model.eval()
)。
示例
>>> from diffusers import StableDiffusionPipeline
>>> # Download pipeline from huggingface.co and cache.
>>> pipeline = StableDiffusionPipeline.from_single_file(
... "https://huggingface.co/WarriorMama777/OrangeMixs/blob/main/Models/AbyssOrangeMix/AbyssOrangeMix.safetensors"
... )
>>> # Download pipeline from local file
>>> # file is downloaded under ./v1-5-pruned-emaonly.ckpt
>>> pipeline = StableDiffusionPipeline.from_single_file("./v1-5-pruned-emaonly.ckpt")
>>> # Enable float16 and move to GPU
>>> pipeline = StableDiffusionPipeline.from_single_file(
... "https://huggingface.co/stable-diffusion-v1-5/stable-diffusion-v1-5/blob/main/v1-5-pruned-emaonly.ckpt",
... torch_dtype=torch.float16,
... )
>>> pipeline.to("cuda")
FromOriginalModelMixin
將以 .ckpt
或 .safetensors
格式儲存的預訓練權重載入到模型中。
from_single_file
< 來源 >( pretrained_model_link_or_path_or_dict: typing.Optional[str] = None **kwargs )
引數
- pretrained_model_link_or_path_or_dict (
str
, 可選) — 可以是:- Hub 上的
.safetensors
或.ckpt
檔案連結(例如"https://huggingface.co/<repo_id>/blob/main/<path_to_file>.safetensors"
)。 - 包含元件模型權重的本地 檔案 路徑。
- 包含元件模型權重的狀態字典。
- Hub 上的
- config (
str
, 可選) —- Hub 上預訓練管道的 repo id 字串(例如
CompVis/ldm-text2im-large-256
)。 - 包含 Diffusers 格式的管道元件配置的 目錄 路徑(例如
./my_pipeline_directory/
)。
- Hub 上預訓練管道的 repo id 字串(例如
- subfolder (
str
, 可選, 預設為""
) — Hub 上或本地大型模型倉庫中模型檔案的子資料夾位置。 - original_config (
str
, 可選) — 字典或 yaml 檔案的路徑,其中包含模型的原始格式配置。如果提供了字典,將用於初始化模型配置。 - torch_dtype (
torch.dtype
, 可選) — 覆蓋預設的torch.dtype
並使用其他資料型別載入模型。 - force_download (
bool
, 可選, 預設為False
) — 是否強制(重新)下載模型權重和配置檔案,如果存在快取版本則覆蓋。 - cache_dir (
Union[str, os.PathLike]
, 可選) — 下載的預訓練模型配置的快取目錄路徑,如果未使用標準快取。 - proxies (
Dict[str, str]
, 可選) — 按協議或端點使用的代理伺服器字典,例如{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}
。代理在每個請求中都使用。 - local_files_only (
bool
, 可選, 預設為False
) — 是否只加載本地模型權重和配置檔案。如果設定為 True,模型將不會從 Hub 下載。 - token (
str
或 bool, 可選) — 用於遠端檔案的 HTTP bearer 授權令牌。如果為True
,則使用diffusers-cli login
生成的令牌(儲存在~/.huggingface
中)。 - revision (
str
, 可選, 預設為"main"
) — 要使用的特定模型版本。它可以是分支名稱、標籤名稱、提交 ID 或 Git 允許的任何識別符號。 - disable_mmap (‘bool’, 可選, 預設為 ‘False’) — 載入 Safetensors 模型時是否停用記憶體對映 (mmap)。當模型位於網路掛載或硬碟上時,此選項可以表現更好,因為網路掛載或硬碟可能無法很好地處理 mmap 的隨機訪問特性。
- kwargs (剩餘的關鍵字引數字典,可選) — 可用於覆蓋可載入和可儲存變數(例如特定管道類的管道元件)。被覆蓋的元件直接傳遞給管道的
__init__
方法。更多資訊請參閱下面的示例。
從以原始 .ckpt
或 .safetensors
格式儲存的預訓練權重例項化模型。預設情況下,模型設定為評估模式(model.eval()
)。