Skip to content

Commit 07dbd86

Browse files
Datta0pre-commit-ci[bot]danielhanchen
authored
Misc fixes (#4018)
* convert print to logger * Print but cleaner * Hide model on multiple devices * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fix typo * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix typo transfomers -> transformers, revert MoE message change * Update MoE detection message to show num_experts and target_modules * Fix llama-cli path in save info message * target_parameters warning for moe * fix should_convert_module for llm_int8_skip_modules * fix should_convert_module for llm_int8_skip_modules * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Logging filters * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * negation * remove should_convert_module patch * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Daniel Hanchen <danielhanchen@users.noreply.github.com>
1 parent 6e81570 commit 07dbd86

File tree

3 files changed

+63
-18
lines changed

3 files changed

+63
-18
lines changed

unsloth/models/_utils.py

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@
103103
DEVICE_COUNT,
104104
ALLOW_PREQUANTIZED_MODELS,
105105
)
106+
from ..import_fixes import UNSLOTH_ENABLE_LOGGING
106107
from unsloth_zoo.log import logger
107108
from unsloth_zoo.tokenizer_utils import (
108109
patch_tokenizer as _patch_tokenizer,
@@ -255,8 +256,45 @@ def filter(self, x):
255256
return not (self.text in x.getMessage())
256257

257258

259+
# Replace warning messages (analogous to HideLoggingMessage but for warnings.warn)
260+
class ReplaceWarningMessage:
261+
"""
262+
Intercepts warnings.warn calls and replaces matching messages with Unsloth branded ones.
263+
Uses a list of registered (match_text, replacement, category) rules checked in order.
264+
"""
265+
266+
_rules = []
267+
_original_showwarning = None
268+
_installed = False
269+
270+
@classmethod
271+
def add_rule(cls, match_text, replacement, category = None):
272+
cls._rules.append((match_text, replacement, category))
273+
if not cls._installed:
274+
cls._install()
275+
276+
@classmethod
277+
def _install(cls):
278+
cls._original_showwarning = warnings.showwarning
279+
cls._installed = True
280+
281+
def _patched_showwarning(
282+
message, category, filename, lineno, file = None, line = None
283+
):
284+
msg_str = str(message)
285+
for match_text, replacement, match_category in cls._rules:
286+
if match_text in msg_str and (
287+
match_category is None or category is match_category
288+
):
289+
print(replacement)
290+
return
291+
cls._original_showwarning(message, category, filename, lineno, file, line)
292+
293+
warnings.showwarning = _patched_showwarning
294+
295+
258296
# Stop vLLM messages
259-
if os.environ.get("UNSLOTH_ENABLE_LOGGING", "0") != "1":
297+
if not UNSLOTH_ENABLE_LOGGING:
260298
try:
261299
from vllm.worker.worker import logger as vllm_worker_logger
262300

@@ -539,6 +577,27 @@ def remove(self):
539577
except:
540578
pass
541579

580+
# Hide HF Hub unauthenticated request warnings
581+
try:
582+
from huggingface_hub.utils._http import logger as hf_http_logger
583+
584+
hf_http_logger.addFilter(
585+
HideLoggingMessage("You are sending unauthenticated requests")
586+
)
587+
del hf_http_logger
588+
except:
589+
pass
590+
591+
# Replace PEFT target_parameters warning with Unsloth branded message for MoE models
592+
ReplaceWarningMessage.add_rule(
593+
match_text = "target_parameters",
594+
replacement = (
595+
"Unsloth: PEFT set target_parameters but found no matching parameters.\n"
596+
"This is expected for MoE models - Unsloth handles MoE expert LoRA targeting separately."
597+
),
598+
category = RuntimeWarning,
599+
)
600+
542601
# Patch get_model_param_count to record correct 4bit / 8bit
543602
from transformers.trainer_pt_utils import is_deepspeed_zero3_enabled
544603

@@ -939,7 +998,7 @@ def _is_openai_available():
939998
xformers_attention = None
940999
xformers_version = None
9411000
except Exception as e:
942-
if os.environ.get("UNSLOTH_ENABLE_LOGGING", "0") != "0":
1001+
if UNSLOTH_ENABLE_LOGGING:
9431002
print(
9441003
"========\nSwitching to PyTorch attention since your Xformers is broken.\n========\n"
9451004
)

unsloth/models/llama.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3086,14 +3086,7 @@ def get_peft_model(
30863086
gc.collect()
30873087
clean_gpu_cache()
30883088

3089-
import warnings as _w
3090-
3091-
with _w.catch_warnings():
3092-
_w.filterwarnings(
3093-
"ignore",
3094-
message = ".*target_parameters.*were set but no parameter was matched.*",
3095-
)
3096-
model = _get_peft_model(model, lora_config)
3089+
model = _get_peft_model(model, lora_config)
30973090
# Fix LoraConfig.auto_mapping is None
30983091
fix_lora_auto_mapping(model)
30993092

unsloth/models/vision.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,14 +1224,7 @@ def get_peft_model(
12241224
model,
12251225
use_gradient_checkpointing = use_gradient_checkpointing,
12261226
)
1227-
import warnings as _w
1228-
1229-
with _w.catch_warnings():
1230-
_w.filterwarnings(
1231-
"ignore",
1232-
message = ".*target_parameters.*were set but no parameter was matched.*",
1233-
)
1234-
model = _get_peft_model(model, lora_config)
1227+
model = _get_peft_model(model, lora_config)
12351228
# Apply QAT + LoRA if specified
12361229
if qat_scheme is not None:
12371230
print("Unsloth: Applying QAT to mitigate quantization degradation")

0 commit comments

Comments
 (0)