Skip to content

Error, when Finetuning Gemma3 (4bit): AttributeError: 'HybridCache' object has no attribute 'float' #2190

Closed

Description

@phiwi

Libraries:

transformers==4.50.0
unsloth @ git+https://github.com/unslothai/unsloth.git@e80d642bc777f7a219bdd34aea1a77751f066785
unsloth_zoo==2025.3.16

Model:

model, tokenizer = FastLanguageModel.from_pretrained(
    model_name="google/gemma-3-27b-it",
    max_seq_length=120000,
    dtype=None,
    load_in_4bit=True,
    token=hf_key,
)

Full stack trace:

Unsloth: Not an error, but Gemma3ForConditionalGeneration does not accept `num_items_in_batch`.
Using gradient accumulation will be very slightly less accurate.
Read more on gradient accumulation issues here: https://unsloth.ai/blog/gradient
Traceback (most recent call last):
  File "/beegfs/prj/LINDA_LLM/scripts/finetune.py", line 102, in <module>
    trainer_stats = trainer.train()
                    ^^^^^^^^^^^^^^^
  File "/home/pwiesenbach/.venvs/finetune/lib/python3.11/site-packages/transformers/trainer.py", line 2245, in train
    return inner_training_loop(
           ^^^^^^^^^^^^^^^^^^^^
  File "<string>", line 402, in _fast_inner_training_loop
  File "/home/pwiesenbach/.venvs/finetune/lib/python3.11/site-packages/transformers/trainer.py", line 3093, in _maybe_log_save_evaluate
    metrics = self._evaluate(trial, ignore_keys_for_eval)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/pwiesenbach/.venvs/finetune/lib/python3.11/site-packages/transformers/trainer.py", line 3047, in _evaluate
    metrics = self.evaluate(ignore_keys=ignore_keys_for_eval)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/pwiesenbach/.venvs/finetune/lib/python3.11/site-packages/transformers/trainer.py", line 4136, in evaluate
    output = eval_loop(
             ^^^^^^^^^^
  File "/home/pwiesenbach/.venvs/finetune/lib/python3.11/site-packages/transformers/trainer.py", line 4330, in evaluation_loop
    losses, logits, labels = self.prediction_step(model, inputs, prediction_loss_only, ignore_keys=ignore_keys)
                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/pwiesenbach/.venvs/finetune/lib/python3.11/site-packages/transformers/trainer.py", line 4546, in prediction_step
    loss, outputs = self.compute_loss(model, inputs, return_outputs=True)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/pwiesenbach/.venvs/finetune/lib/python3.11/site-packages/unsloth/models/_utils.py", line 1029, in _unsloth_pre_compute_loss
    outputs = self._old_compute_loss(model, inputs, *args, **kwargs)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/pwiesenbach/.venvs/finetune/lib/python3.11/site-packages/transformers/trainer.py", line 3783, in compute_loss
    outputs = model(**inputs)
              ^^^^^^^^^^^^^^^
  File "/home/pwiesenbach/.venvs/finetune/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1739, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/pwiesenbach/.venvs/finetune/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1750, in _call_impl
    return forward_call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/pwiesenbach/.venvs/finetune/lib/python3.11/site-packages/accelerate/utils/operations.py", line 823, in forward
    return model_forward(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/pwiesenbach/.venvs/finetune/lib/python3.11/site-packages/accelerate/utils/operations.py", line 811, in __call__
    return convert_to_fp32(self.model_forward(*args, **kwargs))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/pwiesenbach/.venvs/finetune/lib/python3.11/site-packages/accelerate/utils/operations.py", line 790, in convert_to_fp32
    return recursively_apply(_convert_to_fp32, tensor, test_type=_is_fp16_bf16_tensor)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/pwiesenbach/.venvs/finetune/lib/python3.11/site-packages/accelerate/utils/operations.py", line 119, in recursively_apply
    {
  File "/home/pwiesenbach/.venvs/finetune/lib/python3.11/site-packages/accelerate/utils/operations.py", line 120, in <dictcomp>
    k: recursively_apply(
       ^^^^^^^^^^^^^^^^^^
  File "/home/pwiesenbach/.venvs/finetune/lib/python3.11/site-packages/accelerate/utils/operations.py", line 127, in recursively_apply
    return func(data, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/pwiesenbach/.venvs/finetune/lib/python3.11/site-packages/accelerate/utils/operations.py", line 782, in _convert_to_fp32
    return tensor.float()
           ^^^^^^^^^^^^
AttributeError: 'HybridCache' object has no attribute 'float'

Activity

  1. phiwi commented on Mar 26, 2025

    @phiwi
    Author

    It seems to be a similar problem to #2052, but I don't know how exactly to follow the proposed solution steps. If anyone else does, let me know. Big thanks!

  2. eljandoubi commented on Mar 27, 2025

    @eljandoubi

    See this PR

  3. phiwi commented on Mar 27, 2025

    @phiwi
    Author

    Closed with regard to #2052.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions