Update vision.py

This commit is contained in:
Daniel Han 2025-08-22 04:02:59 -07:00
commit f35077388d

View file

@ -73,6 +73,9 @@ global PROMPT_LOOPKUP
PROMPT_LOOPKUP = dict()
from transformers import GenerationConfig, CompileConfig, HybridCache
from transformers import PretrainedConfig
HAS_TORCH_DTYPE = "torch_dtype" in PretrainedConfig.__doc__
_compile_config = CompileConfig(
fullgraph = False,
dynamic = None,
@ -118,7 +121,7 @@ def unsloth_base_fast_generate(
bsz = input_ids.shape[0]
FastBaseModel.for_inference(self)
dtype = _get_dtype(self.config.torch_dtype)
dtype = _get_dtype(getattr(self.config, "dtype", None) or getattr(self.config, "torch_dtype", None))
# Check if VLM
is_vlm = any(
@ -213,7 +216,8 @@ def unsloth_base_fast_generate(
cache_implementation = None
if cache_implementation is not None:
swa = getattr(getattr(self.config, "text_config", self.config), "sliding_window", None)
if swa == 0 or type(swa) is not int:
if (swa == 0 or type(swa) is not int) \
and (getattr(self, "_can_compile_fullgraph", True) is True):
cache_implementation = "static"
else:
cache_implementation = "hybrid"
@ -245,7 +249,6 @@ def unsloth_base_fast_generate(
return output
pass
class FastBaseModel:
@staticmethod
@ -358,12 +361,13 @@ class FastBaseModel:
custom_datatype = os.environ["UNSLOTH_FORCE_CUSTOM_DTYPE"]
assert custom_datatype.count(";") >= 4
checker, _dtype, _bnb_compute_dtype, _custom_datatype, execute_code = custom_datatype.split(";", 4)
# Allow custom dtypes on all runs
allow_all_runs = (checker == "all")
# Allow only on float16 datatypes
allow_float16_runs = (checker == "float16" and dtype == torch.float16)
allow_float16_runs = (
(checker == "float16" or checker == "torch.float16") and \
(dtype == torch.float16 or os.environ.get("UNSLOTH_FORCE_FLOAT32", "0") == "1")
)
if allow_all_runs or allow_float16_runs:
if eval(_dtype) is not None:
dtype = eval(_dtype)
@ -383,7 +387,7 @@ class FastBaseModel:
if not ("attn_implementation" in kwargs):
kwargs["attn_implementation"] = "sdpa"
if not supports_sdpa:
print(f"Unsloth: {model_type_arch.title()} does not support SDPA - switching to eager!")
print(f"Unsloth: {model_type_arch.title()} does not support SDPA - switching to fast eager.")
del kwargs["attn_implementation"]
pass
@ -440,11 +444,17 @@ class FastBaseModel:
torch_dtype = dtype
if do_forced_float32: torch_dtype = torch.bfloat16
if HAS_TORCH_DTYPE:
kwargs["torch_dtype"] = torch_dtype
else:
# Transformers removed torch_dtype
kwargs["dtype"] = torch_dtype
raise_handler = RaiseUninitialized()
model = auto_model.from_pretrained(
model_name,
device_map = device_map,
torch_dtype = torch_dtype,
# torch_dtype = torch_dtype, # Transformers removed torch_dtype
# quantization_config = bnb_config,
token = token,
trust_remote_code = trust_remote_code,
@ -695,7 +705,9 @@ class FastBaseModel:
full_finetuning = os.environ.get("UNSLOTH_ENABLE_FULL_FINETUNING", "0") == "1"
float32_mixed_precision = True
if _get_dtype(model.config.torch_dtype) == torch.bfloat16 and full_finetuning:
if _get_dtype(
getattr(model.config, "dtype", None) or getattr(model.config, "torch_dtype", None)
) == torch.bfloat16 and full_finetuning:
# Use bfloat16 precision for full finetuning
float32_mixed_precision = False