Skip to content

Commit 2dea601

Browse files
ralphbeanclaude
andcommitted
[Cache] Fix environment variable handling for offline mode
Previously, llm-compressor ignored HF_HUB_CACHE and other environment variables when loading models and datasets, making offline mode difficult to use with unified cache directories. This change: - Removes hard-coded TRANSFORMERS_CACHE in model_load/helpers.py to respect HF_HOME, HF_HUB_CACHE environment variables - Propagates cache_dir from model_args to dataset_args to enable unified cache directory for both models and datasets - Updates dataset loading to use cache_dir parameter instead of hardcoded None Now users can specify cache_dir parameter or use HF_HOME/HF_HUB_CACHE environment variables for true offline operation. Signed-off-by: Ralph Bean <[email protected]> Co-Authored-By: Claude <[email protected]>
1 parent 33ef5f4 commit 2dea601

File tree

4 files changed

+17
-3
lines changed

4 files changed

+17
-3
lines changed

src/llmcompressor/args/dataset_arguments.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,14 @@ class DatasetArguments(CustomDatasetArguments):
150150
default=False,
151151
metadata={"help": "Overwrite the cached preprocessed datasets or not."},
152152
)
153+
cache_dir: Optional[str] = field(
154+
init=False,
155+
default=None,
156+
metadata={
157+
"help": "Where to store the pretrained datasets from huggingface.co. "
158+
"This field is set from model_args.cache_dir to enable unified caching."
159+
},
160+
)
153161
preprocessing_num_workers: Optional[int] = field(
154162
default=None,
155163
metadata={"help": "The number of processes to use for the preprocessing."},

src/llmcompressor/args/utils.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,9 @@ def parse_args(
7979
# silently assign tokenizer to processor
8080
resolve_processor_from_model_args(model_args)
8181

82+
# copy cache_dir from model_args to dataset_args to support offline mode
83+
# with a single unified cache directory. This allows both models and datasets
84+
# to use the same cache when cache_dir is specified
85+
dataset_args.cache_dir = model_args.cache_dir
86+
8287
return model_args, dataset_args, recipe_args, training_args, output_dir

src/llmcompressor/pytorch/model_load/helpers.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,16 +149,17 @@ def copy_python_files_from_model_cache(model, save_path: str):
149149
import shutil
150150

151151
from huggingface_hub import hf_hub_download
152-
from transformers import TRANSFORMERS_CACHE
153152
from transformers.utils import http_user_agent
154153

155154
cache_path = config._name_or_path
156155
if not os.path.exists(cache_path):
157156
user_agent = http_user_agent()
157+
# Use cache_dir=None to respect HF_HOME, HF_HUB_CACHE, and other
158+
# environment variables for cache location
158159
config_file_path = hf_hub_download(
159160
repo_id=cache_path,
160161
filename="config.json",
161-
cache_dir=TRANSFORMERS_CACHE,
162+
cache_dir=None,
162163
force_download=False,
163164
user_agent=user_agent,
164165
)

src/llmcompressor/transformers/finetune/data/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ def load_dataset(self):
195195
logger.debug(f"Loading dataset {self.dataset_args.dataset}")
196196
return get_raw_dataset(
197197
self.dataset_args,
198-
None,
198+
cache_dir=self.dataset_args.cache_dir,
199199
split=self.split,
200200
streaming=self.dataset_args.streaming,
201201
**self.dataset_args.raw_kwargs,

0 commit comments

Comments
 (0)