Generate _ModelInfo properties file when loading to improve loading speed (#23558)
Signed-off-by: Manoel Marques <manoel.marques@ibm.com> Signed-off-by: Manoel Marques <manoelmrqs@gmail.com> Co-authored-by: Harry Mellor <19981378+hmellor@users.noreply.github.com> Co-authored-by: Luka Govedič <ProExpertProg@users.noreply.github.com>
This commit is contained in:
@@ -11,6 +11,7 @@ import tempfile
|
||||
import time
|
||||
from collections import defaultdict
|
||||
from collections.abc import Generator
|
||||
from contextlib import contextmanager
|
||||
from pathlib import Path
|
||||
from typing import Any, Callable, Optional, Union
|
||||
|
||||
@@ -98,6 +99,49 @@ def get_lock(model_name_or_path: Union[str, Path],
|
||||
return lock
|
||||
|
||||
|
||||
@contextmanager
|
||||
def atomic_writer(filepath: Union[str, Path],
|
||||
mode: str = 'w',
|
||||
encoding: Optional[str] = None):
|
||||
"""
|
||||
Context manager that provides an atomic file writing routine.
|
||||
|
||||
The context manager writes to a temporary file and, if successful,
|
||||
atomically replaces the original file.
|
||||
|
||||
Args:
|
||||
filepath (str or Path): The path to the file to write.
|
||||
mode (str): The file mode for the temporary file (e.g., 'w', 'wb').
|
||||
encoding (str): The encoding for text mode.
|
||||
|
||||
Yields:
|
||||
file object: A handle to the temporary file.
|
||||
"""
|
||||
# Create a temporary file in the same directory as the target file
|
||||
# to ensure it's on the same filesystem for an atomic replace.
|
||||
temp_dir = os.path.dirname(filepath)
|
||||
temp_fd, temp_path = tempfile.mkstemp(dir=temp_dir)
|
||||
|
||||
try:
|
||||
# Open the temporary file for writing
|
||||
with os.fdopen(temp_fd, mode=mode, encoding=encoding) as temp_file:
|
||||
yield temp_file
|
||||
|
||||
# If the 'with' block completes successfully,
|
||||
# perform the atomic replace.
|
||||
os.replace(temp_path, filepath)
|
||||
|
||||
except Exception:
|
||||
logger.exception(
|
||||
"Error during atomic write. Original file '%s' not modified",
|
||||
filepath)
|
||||
raise
|
||||
finally:
|
||||
# Clean up the temporary file if it still exists.
|
||||
if os.path.exists(temp_path):
|
||||
os.remove(temp_path)
|
||||
|
||||
|
||||
def maybe_download_from_modelscope(
|
||||
model: str,
|
||||
revision: Optional[str] = None,
|
||||
|
||||
Reference in New Issue
Block a user