Skip to content

Commit a7c08a6

Browse files
authored
Harmonize HF environment variables + deprecate use_auth_token (huggingface#6066)
* Harmonize HF environment variables + deprecate use_auth_token * fix import * fix
1 parent 165fb12 commit a7c08a6

19 files changed

+202
-204
lines changed

commands/fp16_safetensors.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import glob
2121
import json
22+
import warnings
2223
from argparse import ArgumentParser, Namespace
2324
from importlib import import_module
2425

@@ -32,12 +33,12 @@
3233

3334

3435
def conversion_command_factory(args: Namespace):
35-
return FP16SafetensorsCommand(
36-
args.ckpt_id,
37-
args.fp16,
38-
args.use_safetensors,
39-
args.use_auth_token,
40-
)
36+
if args.use_auth_token:
37+
warnings.warn(
38+
"The `--use_auth_token` flag is deprecated and will be removed in a future version. Authentication is now"
39+
" handled automatically if user is logged in."
40+
)
41+
return FP16SafetensorsCommand(args.ckpt_id, args.fp16, args.use_safetensors)
4142

4243

4344
class FP16SafetensorsCommand(BaseDiffusersCLICommand):
@@ -62,7 +63,7 @@ def register_subcommand(parser: ArgumentParser):
6263
)
6364
conversion_parser.set_defaults(func=conversion_command_factory)
6465

65-
def __init__(self, ckpt_id: str, fp16: bool, use_safetensors: bool, use_auth_token: bool):
66+
def __init__(self, ckpt_id: str, fp16: bool, use_safetensors: bool):
6667
self.logger = logging.get_logger("diffusers-cli/fp16_safetensors")
6768
self.ckpt_id = ckpt_id
6869
self.local_ckpt_dir = f"/tmp/{ckpt_id}"
@@ -75,8 +76,6 @@ def __init__(self, ckpt_id: str, fp16: bool, use_safetensors: bool, use_auth_tok
7576
"When `use_safetensors` and `fp16` both are False, then this command is of no use."
7677
)
7778

78-
self.use_auth_token = use_auth_token
79-
8079
def run(self):
8180
if version.parse(huggingface_hub.__version__) < version.parse("0.9.0"):
8281
raise ImportError(
@@ -87,7 +86,7 @@ def run(self):
8786
from huggingface_hub import create_commit
8887
from huggingface_hub._commit_api import CommitOperationAdd
8988

90-
model_index = hf_hub_download(repo_id=self.ckpt_id, filename="model_index.json", token=self.use_auth_token)
89+
model_index = hf_hub_download(repo_id=self.ckpt_id, filename="model_index.json")
9190
with open(model_index, "r") as f:
9291
pipeline_class_name = json.load(f)["_class_name"]
9392
pipeline_class = getattr(import_module("diffusers"), pipeline_class_name)
@@ -96,7 +95,7 @@ def run(self):
9695
# Load the appropriate pipeline. We could have use `DiffusionPipeline`
9796
# here, but just to avoid any rough edge cases.
9897
pipeline = pipeline_class.from_pretrained(
99-
self.ckpt_id, torch_dtype=torch.float16 if self.fp16 else torch.float32, use_auth_token=self.use_auth_token
98+
self.ckpt_id, torch_dtype=torch.float16 if self.fp16 else torch.float32
10099
)
101100
pipeline.save_pretrained(
102101
self.local_ckpt_dir,

configuration_utils.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,16 @@
2727

2828
import numpy as np
2929
from huggingface_hub import create_repo, hf_hub_download
30-
from huggingface_hub.utils import EntryNotFoundError, RepositoryNotFoundError, RevisionNotFoundError
30+
from huggingface_hub.utils import (
31+
EntryNotFoundError,
32+
RepositoryNotFoundError,
33+
RevisionNotFoundError,
34+
validate_hf_hub_args,
35+
)
3136
from requests import HTTPError
3237

3338
from . import __version__
3439
from .utils import (
35-
DIFFUSERS_CACHE,
3640
HUGGINGFACE_CO_RESOLVE_ENDPOINT,
3741
DummyObject,
3842
deprecate,
@@ -275,6 +279,7 @@ def get_config_dict(cls, *args, **kwargs):
275279
return cls.load_config(*args, **kwargs)
276280

277281
@classmethod
282+
@validate_hf_hub_args
278283
def load_config(
279284
cls,
280285
pretrained_model_name_or_path: Union[str, os.PathLike],
@@ -311,7 +316,7 @@ def load_config(
311316
local_files_only (`bool`, *optional*, defaults to `False`):
312317
Whether to only load local model weights and configuration files or not. If set to `True`, the model
313318
won't be downloaded from the Hub.
314-
use_auth_token (`str` or *bool*, *optional*):
319+
token (`str` or *bool*, *optional*):
315320
The token to use as HTTP bearer authorization for remote files. If `True`, the token generated from
316321
`diffusers-cli login` (stored in `~/.huggingface`) is used.
317322
revision (`str`, *optional*, defaults to `"main"`):
@@ -329,11 +334,11 @@ def load_config(
329334
A dictionary of all the parameters stored in a JSON configuration file.
330335
331336
"""
332-
cache_dir = kwargs.pop("cache_dir", DIFFUSERS_CACHE)
337+
cache_dir = kwargs.pop("cache_dir", None)
333338
force_download = kwargs.pop("force_download", False)
334339
resume_download = kwargs.pop("resume_download", False)
335340
proxies = kwargs.pop("proxies", None)
336-
use_auth_token = kwargs.pop("use_auth_token", None)
341+
token = kwargs.pop("token", None)
337342
local_files_only = kwargs.pop("local_files_only", False)
338343
revision = kwargs.pop("revision", None)
339344
_ = kwargs.pop("mirror", None)
@@ -376,7 +381,7 @@ def load_config(
376381
proxies=proxies,
377382
resume_download=resume_download,
378383
local_files_only=local_files_only,
379-
use_auth_token=use_auth_token,
384+
token=token,
380385
user_agent=user_agent,
381386
subfolder=subfolder,
382387
revision=revision,
@@ -385,8 +390,7 @@ def load_config(
385390
raise EnvironmentError(
386391
f"{pretrained_model_name_or_path} is not a local folder and is not a valid model identifier"
387392
" listed on 'https://huggingface.co/models'\nIf this is a private repository, make sure to pass a"
388-
" token having permission to this repo with `use_auth_token` or log in with `huggingface-cli"
389-
" login`."
393+
" token having permission to this repo with `token` or log in with `huggingface-cli login`."
390394
)
391395
except RevisionNotFoundError:
392396
raise EnvironmentError(

loaders/ip_adapter.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@
1515
from typing import Dict, Union
1616

1717
import torch
18+
from huggingface_hub.utils import validate_hf_hub_args
1819
from safetensors import safe_open
1920

2021
from ..utils import (
21-
DIFFUSERS_CACHE,
22-
HF_HUB_OFFLINE,
2322
_get_model_file,
2423
is_transformers_available,
2524
logging,
@@ -43,6 +42,7 @@
4342
class IPAdapterMixin:
4443
"""Mixin for handling IP Adapters."""
4544

45+
@validate_hf_hub_args
4646
def load_ip_adapter(
4747
self,
4848
pretrained_model_name_or_path_or_dict: Union[str, Dict[str, torch.Tensor]],
@@ -77,7 +77,7 @@ def load_ip_adapter(
7777
local_files_only (`bool`, *optional*, defaults to `False`):
7878
Whether to only load local model weights and configuration files or not. If set to `True`, the model
7979
won't be downloaded from the Hub.
80-
use_auth_token (`str` or *bool*, *optional*):
80+
token (`str` or *bool*, *optional*):
8181
The token to use as HTTP bearer authorization for remote files. If `True`, the token generated from
8282
`diffusers-cli login` (stored in `~/.huggingface`) is used.
8383
revision (`str`, *optional*, defaults to `"main"`):
@@ -88,12 +88,12 @@ def load_ip_adapter(
8888
"""
8989

9090
# Load the main state dict first.
91-
cache_dir = kwargs.pop("cache_dir", DIFFUSERS_CACHE)
91+
cache_dir = kwargs.pop("cache_dir", None)
9292
force_download = kwargs.pop("force_download", False)
9393
resume_download = kwargs.pop("resume_download", False)
9494
proxies = kwargs.pop("proxies", None)
95-
local_files_only = kwargs.pop("local_files_only", HF_HUB_OFFLINE)
96-
use_auth_token = kwargs.pop("use_auth_token", None)
95+
local_files_only = kwargs.pop("local_files_only", None)
96+
token = kwargs.pop("token", None)
9797
revision = kwargs.pop("revision", None)
9898

9999
user_agent = {
@@ -110,7 +110,7 @@ def load_ip_adapter(
110110
resume_download=resume_download,
111111
proxies=proxies,
112112
local_files_only=local_files_only,
113-
use_auth_token=use_auth_token,
113+
token=token,
114114
revision=revision,
115115
subfolder=subfolder,
116116
user_agent=user_agent,

loaders/lora.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,13 @@
1818
import safetensors
1919
import torch
2020
from huggingface_hub import model_info
21+
from huggingface_hub.utils import validate_hf_hub_args
2122
from packaging import version
2223
from torch import nn
2324

2425
from .. import __version__
2526
from ..models.modeling_utils import _LOW_CPU_MEM_USAGE_DEFAULT, load_model_dict_into_meta
2627
from ..utils import (
27-
DIFFUSERS_CACHE,
28-
HF_HUB_OFFLINE,
2928
USE_PEFT_BACKEND,
3029
_get_model_file,
3130
convert_state_dict_to_diffusers,
@@ -132,6 +131,7 @@ def load_lora_weights(
132131
)
133132

134133
@classmethod
134+
@validate_hf_hub_args
135135
def lora_state_dict(
136136
cls,
137137
pretrained_model_name_or_path_or_dict: Union[str, Dict[str, torch.Tensor]],
@@ -174,7 +174,7 @@ def lora_state_dict(
174174
local_files_only (`bool`, *optional*, defaults to `False`):
175175
Whether to only load local model weights and configuration files or not. If set to `True`, the model
176176
won't be downloaded from the Hub.
177-
use_auth_token (`str` or *bool*, *optional*):
177+
token (`str` or *bool*, *optional*):
178178
The token to use as HTTP bearer authorization for remote files. If `True`, the token generated from
179179
`diffusers-cli login` (stored in `~/.huggingface`) is used.
180180
revision (`str`, *optional*, defaults to `"main"`):
@@ -195,12 +195,12 @@ def lora_state_dict(
195195
"""
196196
# Load the main state dict first which has the LoRA layers for either of
197197
# UNet and text encoder or both.
198-
cache_dir = kwargs.pop("cache_dir", DIFFUSERS_CACHE)
198+
cache_dir = kwargs.pop("cache_dir", None)
199199
force_download = kwargs.pop("force_download", False)
200200
resume_download = kwargs.pop("resume_download", False)
201201
proxies = kwargs.pop("proxies", None)
202-
local_files_only = kwargs.pop("local_files_only", HF_HUB_OFFLINE)
203-
use_auth_token = kwargs.pop("use_auth_token", None)
202+
local_files_only = kwargs.pop("local_files_only", None)
203+
token = kwargs.pop("token", None)
204204
revision = kwargs.pop("revision", None)
205205
subfolder = kwargs.pop("subfolder", None)
206206
weight_name = kwargs.pop("weight_name", None)
@@ -239,7 +239,7 @@ def lora_state_dict(
239239
resume_download=resume_download,
240240
proxies=proxies,
241241
local_files_only=local_files_only,
242-
use_auth_token=use_auth_token,
242+
token=token,
243243
revision=revision,
244244
subfolder=subfolder,
245245
user_agent=user_agent,
@@ -265,7 +265,7 @@ def lora_state_dict(
265265
resume_download=resume_download,
266266
proxies=proxies,
267267
local_files_only=local_files_only,
268-
use_auth_token=use_auth_token,
268+
token=token,
269269
revision=revision,
270270
subfolder=subfolder,
271271
user_agent=user_agent,

loaders/single_file.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@
1818
import requests
1919
import torch
2020
from huggingface_hub import hf_hub_download
21+
from huggingface_hub.utils import validate_hf_hub_args
2122

2223
from ..utils import (
23-
DIFFUSERS_CACHE,
24-
HF_HUB_OFFLINE,
2524
deprecate,
2625
is_accelerate_available,
2726
is_omegaconf_available,
@@ -52,6 +51,7 @@ def from_ckpt(cls, *args, **kwargs):
5251
return cls.from_single_file(*args, **kwargs)
5352

5453
@classmethod
54+
@validate_hf_hub_args
5555
def from_single_file(cls, pretrained_model_link_or_path, **kwargs):
5656
r"""
5757
Instantiate a [`DiffusionPipeline`] from pretrained pipeline weights saved in the `.ckpt` or `.safetensors`
@@ -81,7 +81,7 @@ def from_single_file(cls, pretrained_model_link_or_path, **kwargs):
8181
local_files_only (`bool`, *optional*, defaults to `False`):
8282
Whether to only load local model weights and configuration files or not. If set to `True`, the model
8383
won't be downloaded from the Hub.
84-
use_auth_token (`str` or *bool*, *optional*):
84+
token (`str` or *bool*, *optional*):
8585
The token to use as HTTP bearer authorization for remote files. If `True`, the token generated from
8686
`diffusers-cli login` (stored in `~/.huggingface`) is used.
8787
revision (`str`, *optional*, defaults to `"main"`):
@@ -154,12 +154,12 @@ def from_single_file(cls, pretrained_model_link_or_path, **kwargs):
154154

155155
original_config_file = kwargs.pop("original_config_file", None)
156156
config_files = kwargs.pop("config_files", None)
157-
cache_dir = kwargs.pop("cache_dir", DIFFUSERS_CACHE)
157+
cache_dir = kwargs.pop("cache_dir", None)
158158
resume_download = kwargs.pop("resume_download", False)
159159
force_download = kwargs.pop("force_download", False)
160160
proxies = kwargs.pop("proxies", None)
161-
local_files_only = kwargs.pop("local_files_only", HF_HUB_OFFLINE)
162-
use_auth_token = kwargs.pop("use_auth_token", None)
161+
local_files_only = kwargs.pop("local_files_only", None)
162+
token = kwargs.pop("token", None)
163163
revision = kwargs.pop("revision", None)
164164
extract_ema = kwargs.pop("extract_ema", False)
165165
image_size = kwargs.pop("image_size", None)
@@ -253,7 +253,7 @@ def from_single_file(cls, pretrained_model_link_or_path, **kwargs):
253253
resume_download=resume_download,
254254
proxies=proxies,
255255
local_files_only=local_files_only,
256-
use_auth_token=use_auth_token,
256+
token=token,
257257
revision=revision,
258258
force_download=force_download,
259259
)
@@ -293,6 +293,7 @@ class FromOriginalVAEMixin:
293293
"""
294294

295295
@classmethod
296+
@validate_hf_hub_args
296297
def from_single_file(cls, pretrained_model_link_or_path, **kwargs):
297298
r"""
298299
Instantiate a [`AutoencoderKL`] from pretrained ControlNet weights saved in the original `.ckpt` or
@@ -322,7 +323,7 @@ def from_single_file(cls, pretrained_model_link_or_path, **kwargs):
322323
local_files_only (`bool`, *optional*, defaults to `False`):
323324
Whether to only load local model weights and configuration files or not. If set to True, the model
324325
won't be downloaded from the Hub.
325-
use_auth_token (`str` or *bool*, *optional*):
326+
token (`str` or *bool*, *optional*):
326327
The token to use as HTTP bearer authorization for remote files. If `True`, the token generated from
327328
`diffusers-cli login` (stored in `~/.huggingface`) is used.
328329
revision (`str`, *optional*, defaults to `"main"`):
@@ -379,12 +380,12 @@ def from_single_file(cls, pretrained_model_link_or_path, **kwargs):
379380
)
380381

381382
config_file = kwargs.pop("config_file", None)
382-
cache_dir = kwargs.pop("cache_dir", DIFFUSERS_CACHE)
383+
cache_dir = kwargs.pop("cache_dir", None)
383384
resume_download = kwargs.pop("resume_download", False)
384385
force_download = kwargs.pop("force_download", False)
385386
proxies = kwargs.pop("proxies", None)
386-
local_files_only = kwargs.pop("local_files_only", HF_HUB_OFFLINE)
387-
use_auth_token = kwargs.pop("use_auth_token", None)
387+
local_files_only = kwargs.pop("local_files_only", None)
388+
token = kwargs.pop("token", None)
388389
revision = kwargs.pop("revision", None)
389390
image_size = kwargs.pop("image_size", None)
390391
scaling_factor = kwargs.pop("scaling_factor", None)
@@ -425,7 +426,7 @@ def from_single_file(cls, pretrained_model_link_or_path, **kwargs):
425426
resume_download=resume_download,
426427
proxies=proxies,
427428
local_files_only=local_files_only,
428-
use_auth_token=use_auth_token,
429+
token=token,
429430
revision=revision,
430431
force_download=force_download,
431432
)
@@ -490,6 +491,7 @@ class FromOriginalControlnetMixin:
490491
"""
491492

492493
@classmethod
494+
@validate_hf_hub_args
493495
def from_single_file(cls, pretrained_model_link_or_path, **kwargs):
494496
r"""
495497
Instantiate a [`ControlNetModel`] from pretrained ControlNet weights saved in the original `.ckpt` or
@@ -519,7 +521,7 @@ def from_single_file(cls, pretrained_model_link_or_path, **kwargs):
519521
local_files_only (`bool`, *optional*, defaults to `False`):
520522
Whether to only load local model weights and configuration files or not. If set to True, the model
521523
won't be downloaded from the Hub.
522-
use_auth_token (`str` or *bool*, *optional*):
524+
token (`str` or *bool*, *optional*):
523525
The token to use as HTTP bearer authorization for remote files. If `True`, the token generated from
524526
`diffusers-cli login` (stored in `~/.huggingface`) is used.
525527
revision (`str`, *optional*, defaults to `"main"`):
@@ -555,12 +557,12 @@ def from_single_file(cls, pretrained_model_link_or_path, **kwargs):
555557
from ..pipelines.stable_diffusion.convert_from_ckpt import download_controlnet_from_original_ckpt
556558

557559
config_file = kwargs.pop("config_file", None)
558-
cache_dir = kwargs.pop("cache_dir", DIFFUSERS_CACHE)
560+
cache_dir = kwargs.pop("cache_dir", None)
559561
resume_download = kwargs.pop("resume_download", False)
560562
force_download = kwargs.pop("force_download", False)
561563
proxies = kwargs.pop("proxies", None)
562-
local_files_only = kwargs.pop("local_files_only", HF_HUB_OFFLINE)
563-
use_auth_token = kwargs.pop("use_auth_token", None)
564+
local_files_only = kwargs.pop("local_files_only", None)
565+
token = kwargs.pop("token", None)
564566
num_in_channels = kwargs.pop("num_in_channels", None)
565567
use_linear_projection = kwargs.pop("use_linear_projection", None)
566568
revision = kwargs.pop("revision", None)
@@ -603,7 +605,7 @@ def from_single_file(cls, pretrained_model_link_or_path, **kwargs):
603605
resume_download=resume_download,
604606
proxies=proxies,
605607
local_files_only=local_files_only,
606-
use_auth_token=use_auth_token,
608+
token=token,
607609
revision=revision,
608610
force_download=force_download,
609611
)

0 commit comments

Comments
 (0)