Skip to content

Commit 630e60a

Browse files
gaztrabismeclaude
andcommitted
Simplify: remove hook workaround, keep idefics3 in VLM list
Moved the requires_grad_pre_hook fix upstream to unsloth-zoo#514. This PR now only adds "idefics3" to VLLM_SUPPORTED_VLM and the error message text. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 96e4d87 commit 630e60a

1 file changed

Lines changed: 9 additions & 41 deletions

File tree

unsloth/models/vision.py

Lines changed: 9 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -127,49 +127,17 @@
127127
)
128128
_compile_config.disable = True # Must set manually
129129

130+
from unsloth_zoo.vllm_utils import (
131+
convert_lora_modules,
132+
return_lora_modules,
133+
)
134+
130135
try:
131136
torch_compiler_set_stance = torch.compiler.set_stance
132137
except:
133138
torch_compiler_set_stance = None
134139

135140

136-
def _fix_requires_grad_hooks_for_kwargs(model):
137-
"""
138-
Fix requires_grad pre-hooks for models whose forward() receives all
139-
arguments via kwargs (e.g. Idefics3 vision encoder).
140-
141-
requires_grad_pre_hook in unsloth_zoo only inspects positional args.
142-
When positional args are empty it raises RuntimeError. This replaces
143-
those hooks with a version that returns gracefully on empty args.
144-
"""
145-
from collections import OrderedDict
146-
147-
def _safe_requires_grad_pre_hook(module, input):
148-
type_input = type(input)
149-
if type_input is torch.Tensor:
150-
input.requires_grad_(True)
151-
elif type_input is tuple or type_input is list:
152-
if len(input) == 0:
153-
return
154-
if torch.is_floating_point(input[0]):
155-
input[0].requires_grad_(True)
156-
157-
158-
for name, module in model.named_modules():
159-
if len(module._forward_pre_hooks) == 0:
160-
continue
161-
new_hooks = OrderedDict()
162-
for hook_id, hook in module._forward_pre_hooks.items():
163-
qualname = getattr(hook, "__qualname__", "")
164-
if "requires_grad_pre_hook" in qualname:
165-
new_hooks[hook_id] = _safe_requires_grad_pre_hook
166-
else:
167-
new_hooks[hook_id] = hook
168-
module._forward_pre_hooks = new_hooks
169-
170-
171-
172-
173141
def unsloth_base_fast_generate(
174142
self,
175143
*args,
@@ -497,7 +465,7 @@ def from_pretrained(
497465
if is_vlm and fast_inference:
498466
if not any(arch in VLLM_SUPPORTED_VLM for arch in model_types):
499467
raise RuntimeError(
500-
f"Unsloth: Fast inference is only supported for Language models and Qwen2.5-VL, Gemma3 among vision models. "
468+
f"Unsloth: Fast inference is only supported for Language models and Qwen2.5-VL, Gemma3, Idefics3 among vision models. "
501469
f"Found architectures: {', '.join(model_types)}!"
502470
)
503471

@@ -530,7 +498,9 @@ def from_pretrained(
530498
vllm_version = ""
531499
elif DEVICE_TYPE == "hip":
532500
gpu_stats = torch.cuda.get_device_properties(0)
533-
gpu_stats_name = resolve_hip_gpu_stats_name(gpu_stats)
501+
gpu_stats_name = (
502+
gpu_stats.name + ". " if gpu_stats.name != "" else "AMD GPU Device. "
503+
)
534504
gpu_version = torch.version.hip
535505
gpu_stats_snippet = f"ROCm Toolkit: {gpu_version}."
536506
try:
@@ -1264,8 +1234,6 @@ def get_peft_model(
12641234
fix_lora_auto_mapping(model)
12651235
# Enable gradients on modules which are trainable
12661236
requires_grad_for_gradient_checkpointing(model)
1267-
# Fix hooks for models with kwargs-only forward (e.g. Idefics3)
1268-
_fix_requires_grad_hooks_for_kwargs(model)
12691237
trust_remote_code = getattr(model, "_unsloth_trust_remote_code", False)
12701238
model = FastBaseModel.post_patch_model(
12711239
model,

0 commit comments

Comments
 (0)