2023-02-14 05:02:16 -07:00
|
|
|
import time
|
|
|
|
import os
|
|
|
|
|
|
|
|
from datetime import timedelta
|
|
|
|
from loguru import logger
|
|
|
|
from pathlib import Path
|
|
|
|
from typing import Optional, List
|
|
|
|
|
2023-12-18 02:20:08 -07:00
|
|
|
from huggingface_hub import file_download, hf_api, HfApi, hf_hub_download
|
2023-02-14 05:02:16 -07:00
|
|
|
from huggingface_hub.constants import HUGGINGFACE_HUB_CACHE
|
|
|
|
from huggingface_hub.utils import (
|
|
|
|
LocalEntryNotFoundError,
|
|
|
|
EntryNotFoundError,
|
2023-12-18 02:20:08 -07:00
|
|
|
RevisionNotFoundError, # noqa # Import here to ease try/except in other part of the lib
|
2023-02-14 05:02:16 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
WEIGHTS_CACHE_OVERRIDE = os.getenv("WEIGHTS_CACHE_OVERRIDE", None)
|
2023-12-18 02:20:08 -07:00
|
|
|
HF_HUB_OFFLINE = os.environ.get("HF_HUB_OFFLINE", "0").lower() in ["true", "1", "yes"]
|
2023-02-14 05:02:16 -07:00
|
|
|
|
|
|
|
|
2023-12-21 09:25:22 -07:00
|
|
|
def _cached_weight_files(
|
|
|
|
model_id: str, revision: Optional[str], extension: str
|
|
|
|
) -> List[str]:
|
2023-12-18 02:20:08 -07:00
|
|
|
"""Guess weight files from the cached revision snapshot directory"""
|
|
|
|
d = _get_cached_revision_directory(model_id, revision)
|
|
|
|
if not d:
|
|
|
|
return []
|
|
|
|
filenames = _weight_files_from_dir(d, extension)
|
|
|
|
return filenames
|
|
|
|
|
|
|
|
|
2023-12-21 09:25:22 -07:00
|
|
|
def _weight_hub_files_from_model_info(
|
|
|
|
info: hf_api.ModelInfo, extension: str
|
|
|
|
) -> List[str]:
|
2023-12-18 02:20:08 -07:00
|
|
|
return [
|
2023-05-31 02:57:53 -06:00
|
|
|
s.rfilename
|
|
|
|
for s in info.siblings
|
2023-06-19 01:53:45 -06:00
|
|
|
if s.rfilename.endswith(extension)
|
|
|
|
and len(s.rfilename.split("/")) == 1
|
|
|
|
and "arguments" not in s.rfilename
|
|
|
|
and "args" not in s.rfilename
|
2023-06-23 04:41:13 -06:00
|
|
|
and "training" not in s.rfilename
|
2024-02-26 11:49:28 -07:00
|
|
|
and "medusa_lm_head" not in s.rfilename
|
2023-05-31 02:57:53 -06:00
|
|
|
]
|
2023-02-14 05:02:16 -07:00
|
|
|
|
|
|
|
|
2023-12-18 02:20:08 -07:00
|
|
|
def _weight_files_from_dir(d: Path, extension: str) -> List[str]:
|
|
|
|
# os.walk: do not iterate, just scan for depth 1, not recursively
|
|
|
|
# see _weight_hub_files_from_model_info, that's also what is
|
|
|
|
# done there with the len(s.rfilename.split("/")) == 1 condition
|
|
|
|
root, _, files = next(os.walk(str(d)))
|
2023-12-21 09:25:22 -07:00
|
|
|
filenames = [
|
2023-12-21 09:29:23 -07:00
|
|
|
os.path.join(root, f)
|
2023-12-21 09:25:22 -07:00
|
|
|
for f in files
|
|
|
|
if f.endswith(extension)
|
|
|
|
and "arguments" not in f
|
|
|
|
and "args" not in f
|
|
|
|
and "adapter" not in f
|
|
|
|
and "training" not in f
|
2024-02-26 11:49:28 -07:00
|
|
|
and "medusa_lm_head" not in f
|
2023-12-21 09:25:22 -07:00
|
|
|
]
|
2023-02-14 05:02:16 -07:00
|
|
|
return filenames
|
|
|
|
|
|
|
|
|
2023-12-21 09:25:22 -07:00
|
|
|
def _get_cached_revision_directory(
|
|
|
|
model_id: str, revision: Optional[str]
|
|
|
|
) -> Optional[Path]:
|
2023-02-14 05:02:16 -07:00
|
|
|
if revision is None:
|
|
|
|
revision = "main"
|
|
|
|
|
2023-12-18 02:20:08 -07:00
|
|
|
repo_cache = Path(HUGGINGFACE_HUB_CACHE) / Path(
|
2023-12-21 09:25:22 -07:00
|
|
|
file_download.repo_folder_name(repo_id=model_id, repo_type="model")
|
|
|
|
)
|
2023-02-14 05:02:16 -07:00
|
|
|
|
|
|
|
if not repo_cache.is_dir():
|
|
|
|
# No cache for this model
|
|
|
|
return None
|
|
|
|
|
|
|
|
refs_dir = repo_cache / "refs"
|
|
|
|
snapshots_dir = repo_cache / "snapshots"
|
|
|
|
|
|
|
|
# Resolve refs (for instance to convert main to the associated commit sha)
|
|
|
|
if refs_dir.is_dir():
|
|
|
|
revision_file = refs_dir / revision
|
|
|
|
if revision_file.exists():
|
|
|
|
with revision_file.open() as f:
|
|
|
|
revision = f.read()
|
|
|
|
|
|
|
|
# Check if revision folder exists
|
|
|
|
if not snapshots_dir.exists():
|
|
|
|
return None
|
|
|
|
cached_shas = os.listdir(snapshots_dir)
|
|
|
|
if revision not in cached_shas:
|
|
|
|
# No cache for this revision and we won't try to return a random revision
|
|
|
|
return None
|
|
|
|
|
2023-12-18 02:20:08 -07:00
|
|
|
return snapshots_dir / revision
|
|
|
|
|
|
|
|
|
|
|
|
def weight_hub_files(
|
2023-12-21 09:25:22 -07:00
|
|
|
model_id: str, revision: Optional[str] = None, extension: str = ".safetensors"
|
2023-12-18 02:20:08 -07:00
|
|
|
) -> List[str]:
|
|
|
|
"""Get the weights filenames on the hub"""
|
|
|
|
api = HfApi()
|
|
|
|
|
|
|
|
if HF_HUB_OFFLINE:
|
|
|
|
filenames = _cached_weight_files(model_id, revision, extension)
|
|
|
|
else:
|
|
|
|
# Online case, fetch model info from the Hub
|
|
|
|
info = api.model_info(model_id, revision=revision)
|
|
|
|
filenames = _weight_hub_files_from_model_info(info, extension)
|
|
|
|
|
|
|
|
if not filenames:
|
|
|
|
raise EntryNotFoundError(
|
|
|
|
f"No {extension} weights found for model {model_id} and revision {revision}.",
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
|
|
|
|
return filenames
|
|
|
|
|
|
|
|
|
|
|
|
def try_to_load_from_cache(
|
|
|
|
model_id: str, revision: Optional[str], filename: str
|
|
|
|
) -> Optional[Path]:
|
|
|
|
"""Try to load a file from the Hugging Face cache"""
|
|
|
|
|
|
|
|
d = _get_cached_revision_directory(model_id, revision)
|
|
|
|
if not d:
|
|
|
|
return None
|
|
|
|
|
2023-02-14 05:02:16 -07:00
|
|
|
# Check if file exists in cache
|
2023-12-18 02:20:08 -07:00
|
|
|
cached_file = d / filename
|
2023-02-14 05:02:16 -07:00
|
|
|
return cached_file if cached_file.is_file() else None
|
|
|
|
|
|
|
|
|
|
|
|
def weight_files(
|
|
|
|
model_id: str, revision: Optional[str] = None, extension: str = ".safetensors"
|
|
|
|
) -> List[Path]:
|
|
|
|
"""Get the local files"""
|
2023-03-06 06:39:36 -07:00
|
|
|
# Local model
|
2023-12-18 02:20:08 -07:00
|
|
|
d = Path(model_id)
|
|
|
|
if d.exists() and d.is_dir():
|
|
|
|
local_files = _weight_files_from_dir(d, extension)
|
2023-05-03 03:36:24 -06:00
|
|
|
if not local_files:
|
|
|
|
raise FileNotFoundError(
|
|
|
|
f"No local weights found in {model_id} with extension {extension}"
|
|
|
|
)
|
2023-12-18 02:20:08 -07:00
|
|
|
return [Path(f) for f in local_files]
|
2023-03-06 06:39:36 -07:00
|
|
|
|
2023-02-14 05:02:16 -07:00
|
|
|
try:
|
|
|
|
filenames = weight_hub_files(model_id, revision, extension)
|
|
|
|
except EntryNotFoundError as e:
|
|
|
|
if extension != ".safetensors":
|
|
|
|
raise e
|
|
|
|
# Try to see if there are pytorch weights
|
|
|
|
pt_filenames = weight_hub_files(model_id, revision, extension=".bin")
|
|
|
|
# Change pytorch extension to safetensors extension
|
|
|
|
# It is possible that we have safetensors weights locally even though they are not on the
|
|
|
|
# hub if we converted weights locally without pushing them
|
|
|
|
filenames = [
|
|
|
|
f"{Path(f).stem.lstrip('pytorch_')}.safetensors" for f in pt_filenames
|
|
|
|
]
|
|
|
|
|
|
|
|
if WEIGHTS_CACHE_OVERRIDE is not None:
|
|
|
|
files = []
|
|
|
|
for filename in filenames:
|
|
|
|
p = Path(WEIGHTS_CACHE_OVERRIDE) / filename
|
|
|
|
if not p.exists():
|
2023-05-03 03:36:24 -06:00
|
|
|
raise FileNotFoundError(
|
2023-02-14 05:02:16 -07:00
|
|
|
f"File {p} not found in {WEIGHTS_CACHE_OVERRIDE}."
|
|
|
|
)
|
|
|
|
files.append(p)
|
|
|
|
return files
|
|
|
|
|
|
|
|
files = []
|
|
|
|
for filename in filenames:
|
|
|
|
cache_file = try_to_load_from_cache(
|
|
|
|
model_id, revision=revision, filename=filename
|
|
|
|
)
|
|
|
|
if cache_file is None:
|
|
|
|
raise LocalEntryNotFoundError(
|
|
|
|
f"File {filename} of model {model_id} not found in "
|
|
|
|
f"{os.getenv('HUGGINGFACE_HUB_CACHE', 'the local cache')}. "
|
|
|
|
f"Please run `text-generation-server download-weights {model_id}` first."
|
|
|
|
)
|
|
|
|
files.append(cache_file)
|
|
|
|
|
|
|
|
return files
|
|
|
|
|
|
|
|
|
|
|
|
def download_weights(
|
|
|
|
filenames: List[str], model_id: str, revision: Optional[str] = None
|
|
|
|
) -> List[Path]:
|
|
|
|
"""Download the safetensors files from the hub"""
|
|
|
|
|
2023-12-18 02:20:08 -07:00
|
|
|
def download_file(fname, tries=5, backoff: int = 5):
|
|
|
|
local_file = try_to_load_from_cache(model_id, revision, fname)
|
2023-02-14 05:02:16 -07:00
|
|
|
if local_file is not None:
|
2023-12-18 02:20:08 -07:00
|
|
|
logger.info(f"File {fname} already present in cache.")
|
2023-02-16 09:18:53 -07:00
|
|
|
return Path(local_file)
|
2023-02-14 05:02:16 -07:00
|
|
|
|
2023-12-18 02:20:08 -07:00
|
|
|
for idx in range(tries):
|
2023-05-31 02:57:53 -06:00
|
|
|
try:
|
2023-12-18 02:20:08 -07:00
|
|
|
logger.info(f"Download file: {fname}")
|
|
|
|
stime = time.time()
|
2023-05-31 02:57:53 -06:00
|
|
|
local_file = hf_hub_download(
|
2023-12-18 02:20:08 -07:00
|
|
|
filename=fname,
|
2023-05-31 02:57:53 -06:00
|
|
|
repo_id=model_id,
|
|
|
|
revision=revision,
|
2023-12-18 02:20:08 -07:00
|
|
|
local_files_only=HF_HUB_OFFLINE,
|
2023-05-31 02:57:53 -06:00
|
|
|
)
|
|
|
|
logger.info(
|
2023-12-18 02:20:08 -07:00
|
|
|
f"Downloaded {local_file} in {timedelta(seconds=int(time.time() - stime))}."
|
2023-05-31 02:57:53 -06:00
|
|
|
)
|
|
|
|
return Path(local_file)
|
|
|
|
except Exception as e:
|
2023-12-18 02:20:08 -07:00
|
|
|
if idx + 1 == tries:
|
2023-05-31 02:57:53 -06:00
|
|
|
raise e
|
|
|
|
logger.error(e)
|
2023-06-05 08:09:41 -06:00
|
|
|
logger.info(f"Retrying in {backoff} seconds")
|
|
|
|
time.sleep(backoff)
|
2023-12-18 02:20:08 -07:00
|
|
|
logger.info(f"Retry {idx + 1}/{tries - 1}")
|
2023-02-14 05:02:16 -07:00
|
|
|
|
|
|
|
# We do this instead of using tqdm because we want to parse the logs with the launcher
|
|
|
|
start_time = time.time()
|
|
|
|
files = []
|
2023-02-18 06:04:11 -07:00
|
|
|
for i, filename in enumerate(filenames):
|
|
|
|
file = download_file(filename)
|
|
|
|
|
2023-02-14 05:02:16 -07:00
|
|
|
elapsed = timedelta(seconds=int(time.time() - start_time))
|
2023-02-18 06:04:11 -07:00
|
|
|
remaining = len(filenames) - (i + 1)
|
2023-02-15 08:11:32 -07:00
|
|
|
eta = (elapsed / (i + 1)) * remaining if remaining > 0 else 0
|
2023-02-14 05:02:16 -07:00
|
|
|
|
2023-02-18 06:04:11 -07:00
|
|
|
logger.info(f"Download: [{i + 1}/{len(filenames)}] -- ETA: {eta}")
|
|
|
|
files.append(file)
|
2023-02-14 05:02:16 -07:00
|
|
|
|
2023-02-15 08:11:32 -07:00
|
|
|
return files
|