setting for hf transfer mode

Signed-off-by: Vladimir Mandic <mandic00@live.com>
This commit is contained in:
Vladimir Mandic
2025-08-16 16:45:56 -04:00
parent 07d0f125d8
commit fbc50ce3e1
7 changed files with 54 additions and 38 deletions
+3
View File
@@ -2,6 +2,9 @@
## Update for 2025-08-16
- **Features**
- new setting -> huggingface -> download method
default is `rust` as new `xet` is known to cause issues
- **Fixes**
- fix OpenVINO with offloading
- add explicit offload calls on prompt encode
+5 -9
View File
@@ -1315,7 +1315,6 @@ def install_requirements():
# set environment variables controling the behavior of various libraries
def set_environment():
from modules.paths import models_path
log.debug('Setting environment tuning')
os.environ.setdefault('ACCELERATE', 'True')
os.environ.setdefault('ATTN_PRECISION', 'fp16')
@@ -1324,25 +1323,22 @@ def set_environment():
os.environ.setdefault('CUDA_DEVICE_DEFAULT_PERSISTING_L2_CACHE_PERCENTAGE_LIMIT', '0')
os.environ.setdefault('CUDA_LAUNCH_BLOCKING', '0')
os.environ.setdefault('CUDA_MODULE_LOADING', 'LAZY')
os.environ.setdefault('TORCH_CUDNN_V8_API_ENABLED', '1')
os.environ.setdefault('DO_NOT_TRACK', '1')
os.environ.setdefault('FORCE_CUDA', '1')
os.environ.setdefault('GRADIO_ANALYTICS_ENABLED', 'False')
os.environ.setdefault('HF_HUB_DISABLE_EXPERIMENTAL_WARNING', '1')
os.environ.setdefault('HF_HUB_DISABLE_TELEMETRY', '1')
os.environ.setdefault('K_DIFFUSION_USE_COMPILE', '0')
os.environ.setdefault('KINETO_LOG_LEVEL', '3')
os.environ.setdefault('NUMEXPR_MAX_THREADS', '16')
os.environ.setdefault('PYTHONHTTPSVERIFY', '0')
os.environ.setdefault('SAFETENSORS_FAST_GPU', '1')
os.environ.setdefault('TF_CPP_MIN_LOG_LEVEL', '2')
os.environ.setdefault('TF_ENABLE_ONEDNN_OPTS', '0')
os.environ.setdefault('USE_TORCH', '1')
os.environ.setdefault('TORCH_CUDNN_V8_API_ENABLED', '1')
os.environ.setdefault('TORCH_FORCE_NO_WEIGHTS_ONLY_LOAD', '1')
os.environ.setdefault('UVICORN_TIMEOUT_KEEP_ALIVE', '60')
os.environ.setdefault('KINETO_LOG_LEVEL', '3')
os.environ.setdefault('DO_NOT_TRACK', '1')
os.environ.setdefault('USE_TORCH', '1')
os.environ.setdefault('UV_INDEX_STRATEGY', 'unsafe-any-match')
os.environ.setdefault('UV_NO_BUILD_ISOLATION', '1')
os.environ.setdefault('HF_HUB_CACHE', opts.get('hfcache_dir', os.path.join(models_path, 'huggingface')))
os.environ.setdefault('UVICORN_TIMEOUT_KEEP_ALIVE', '60')
allocator = f'garbage_collection_threshold:{opts.get("torch_gc_threshold", 80)/100:0.2f},max_split_size_mb:512'
if opts.get("torch_malloc", "native") == 'cudaMallocAsync':
allocator += ',backend:cudaMallocAsync'
+39 -7
View File
@@ -1,18 +1,51 @@
import os
import gradio as gr
from modules.shared import log, opts
from installer import log
from modules.shared import opts
# initialize huggingface environment
def hf_init():
os.environ.setdefault('HF_HUB_DISABLE_EXPERIMENTAL_WARNING', '1')
os.environ.setdefault('HF_HUB_DISABLE_SYMLINKS_WARNING', '1')
os.environ.setdefault('HF_HUB_DISABLE_EXPERIMENTAL_WARNING', '1')
os.environ.setdefault('HF_HUB_DISABLE_IMPLICIT_TOKEN', '1')
os.environ.setdefault('HUGGINGFACE_HUB_VERBOSITY', 'warning')
os.environ.setdefault('HF_HUB_DISABLE_SYMLINKS_WARNING', '1')
os.environ.setdefault('HF_HUB_DISABLE_TELEMETRY', '1')
os.environ.setdefault('HF_HUB_VERBOSITY', 'warning')
os.environ.setdefault('HF_HUB_DOWNLOAD_TIMEOUT', '60')
os.environ.setdefault('HF_HUB_ETAG_TIMEOUT', '10')
os.environ.setdefault('HF_ENABLE_PARALLEL_LOADING', 'true' if opts.sd_parallel_load else 'false')
os.environ.setdefault('HF_HUB_CACHE', opts.hfcache_dir)
if opts.hf_transfer_mode == 'requests':
os.environ.setdefault('HF_XET_HIGH_PERFORMANCE', 'false')
os.environ.setdefault('HF_HUB_ENABLE_HF_TRANSFER', 'false')
os.environ.setdefault('HF_HUB_DISABLE_XET', 'true')
elif opts.hf_transfer_mode == 'rust':
os.environ.setdefault('HF_XET_HIGH_PERFORMANCE', 'false')
os.environ.setdefault('HF_HUB_ENABLE_HF_TRANSFER', 'true')
os.environ.setdefault('HF_HUB_DISABLE_XET', 'true')
elif opts.hf_transfer_mode == 'xet':
os.environ.setdefault('HF_XET_HIGH_PERFORMANCE', 'true')
os.environ.setdefault('HF_HUB_ENABLE_HF_TRANSFER', 'true')
os.environ.setdefault('HF_HUB_DISABLE_XET', 'false')
obfuscated_token = None
if len(opts.huggingface_token) > 0 and opts.huggingface_token.startswith('hf_'):
obfuscated_token = 'hf_...' + opts.huggingface_token[-4:]
os.environ.setdefault('HF_TOKEN', opts.huggingface_token)
log.info(f'Huggingface init: transfer={opts.hf_transfer_mode} parallel={opts.sd_parallel_load} direct={opts.diffusers_to_gpu} token="{obfuscated_token}" cache="{opts.hfcache_dir}"')
def hf_check_cache():
prev_default = os.environ.get("SD_HFCACHEDIR", None) or os.path.join(os.path.expanduser('~'), '.cache', 'huggingface', 'hub')
from modules.modelstats import stat
if opts.hfcache_dir != prev_default:
size, _mtime = stat(prev_default)
if size//1024//1024 > 0:
log.warning(f'Cache location changed: previous="{prev_default}" size={size//1024//1024} MB')
size, _mtime = stat(opts.hfcache_dir)
log.debug(f'Huggingface cache: path="{opts.hfcache_dir}" size={size//1024//1024} MB')
def hf_search(keyword):
hf_init()
import huggingface_hub as hf
hf_api = hf.HfApi()
models = hf_api.list_models(model_name=keyword, full=True, library="diffusers", limit=50, sort="downloads", direction=-1)
@@ -23,12 +56,11 @@ def hf_search(keyword):
return data
def hf_select(evt: gr.SelectData, data):
def hf_select(evt, data):
return data[evt.index[0]][0]
def hf_download_model(hub_id: str, token, variant, revision, mirror, custom_pipeline):
hf_init()
from modules.modelloader import download_diffusers_model
download_diffusers_model(hub_id, cache_dir=opts.diffusers_dir, token=token, variant=variant, revision=revision, mirror=mirror, custom_pipeline=custom_pipeline)
from modules.sd_models import list_models # pylint: disable=W0621
+1 -12
View File
@@ -4,7 +4,7 @@ import sys
import json
import shlex
import argparse
from modules.errors import log
from installer import log
# parse args, parse again after we have the data-dir and early-read the config file
@@ -123,14 +123,3 @@ class Prioritize:
def __exit__(self, exc_type, exc_val, exc_tb):
sys.path = self.path
self.path = None
def check_cache(opts):
prev_default = os.environ.get("SD_HFCACHEDIR", None) or os.path.join(os.path.expanduser('~'), '.cache', 'huggingface', 'hub')
from modules.modelstats import stat
if opts.hfcache_dir != prev_default:
size, _mtime = stat(prev_default)
if size//1024//1024 > 0:
log.warning(f'Cache location changed: previous="{prev_default}" size={size//1024//1024} MB')
size, _mtime = stat(opts.hfcache_dir)
log.debug(f'Huggingface cache: path="{opts.hfcache_dir}" size={size//1024//1024} MB')
+2 -7
View File
@@ -73,16 +73,11 @@ def copy_diffuser_options(new_pipe, orig_pipe):
set_accelerate(new_pipe)
def set_huggingface_options(op: str, model_type: str):
if model_type is not None: # overrides
pass
def set_huggingface_options():
if shared.opts.diffusers_to_gpu: # and model_type.startswith('Stable Diffusion'):
shared.log.debug(f'Setting {op}: component=accelerate direct={shared.opts.diffusers_to_gpu}')
sd_hijack_accelerate.hijack_accelerate()
else:
sd_hijack_accelerate.restore_accelerate()
if shared.opts.sd_parallel_load:
shared.log.debug(f'Setting {op}: component=huggingface parallel={shared.opts.sd_parallel_load}')
def set_vae_options(sd_model, vae=None, op:str='model', quiet:bool=False):
@@ -614,7 +609,7 @@ def load_diffuser(checkpoint_info=None, op='model', revision=None): # pylint: di
# detect pipeline
pipeline, model_type = sd_detect.detect_pipeline(checkpoint_info.path, op)
set_huggingface_options(op, model_type)
set_huggingface_options()
# preload vae so it can be used as param
vae = None
+2
View File
@@ -651,6 +651,8 @@ options_templates.update(options_section(('huggingface', "Huggingface"), {
"huggingface_sep": OptionInfo("<h2>Huggingface</h2>", "", gr.HTML),
"diffuser_cache_config": OptionInfo(True, "Use cached model config when available"),
"huggingface_token": OptionInfo('', 'HuggingFace token', gr.Textbox, {"lines": 2}),
"hf_transfer_mode": OptionInfo("rust", "HuggingFace download method", gr.Radio, {"choices": ['requests', 'rust', 'xet']}),
"diffusers_model_load_variant": OptionInfo("default", "Preferred Model variant", gr.Radio, {"choices": ['default', 'fp32', 'fp16']}),
"diffusers_vae_load_variant": OptionInfo("default", "Preferred VAE variant", gr.Radio, {"choices": ['default', 'fp32', 'fp16']}),
"custom_diffusers_pipeline": OptionInfo('', 'Load custom Diffusers pipeline'),
+2 -3
View File
@@ -68,8 +68,6 @@ def initialize():
modules.sd_checkpoint.init_metadata()
modules.hashes.init_cache()
paths.check_cache(shared.opts)
modules.sd_samplers.list_samplers()
timer.startup.record("samplers")
@@ -121,8 +119,9 @@ def initialize():
modules.extra_networks.register_default_extra_networks()
timer.startup.record("networks")
from modules.models_hf import hf_init
from modules.models_hf import hf_init, hf_check_cache
hf_init()
hf_check_cache()
if shared.cmd_opts.tls_keyfile is not None and shared.cmd_opts.tls_certfile is not None:
try: