Skip to content

[Bug] Unable to train Devstral2 #3862

Closed

Description

@Klepackp

Hi all! I'm having issues with finetuning Devstral2 after updating unsloth and transformers packages. GPT_OSS also returns similar error. Any help would be appreciated!

unsloth = 2025.12.9
unsloth_zoo = 2025.12.7
transformers = 5.0.0.dev0
torch = 2.9.1

model, tokenizer = FastLanguageModel.from_pretrained(                                                 
    model_name        = MODEL_DIR,                                                                                                                                           
    max_seq_length    = MAX_SEQ_LENGTH,                                                               
    full_finetuning   = False,                                                                 
    local_files_only  = True,                                                                         
    load_in_4bit      = False,                                                             
    device_map        = "balanced",        
    max_memory        = {0:"78GiB", 1:"78GiB", 2:"78GiB", 3:"78GiB", "cpu":"256GiB"},
    offload_folder    = offload_dir,
    trust_remote_code = True,
    config            = config,
)
model = FastLanguageModel.get_peft_model(
    model,
    r = 4,
    finetune_language_layers   = True, 
    finetune_attention_modules = True, 
    finetune_mlp_modules       = True, 
    lora_alpha = 8,
    lora_dropout = 0.0,
    bias = "none",
    use_gradient_checkpointing = "unsloth",
    random_state = 3407,
    use_rslora = False,
    loftq_config = None,
)
trainer = SFTTrainer(
    model = model,
    tokenizer = tokenizer,
    train_dataset = dataset,
    eval_dataset = None, 
    args = SFTConfig(
        dataset_text_field = "text",
        per_device_train_batch_size = 2,
        gradient_accumulation_steps = 2, 
        warmup_steps = 5,
        max_steps = 15,
        learning_rate = 2e-4, 
        logging_steps = 1,
        optim = "adamw_8bit",
        weight_decay = 0.001,
        lr_scheduler_type = "linear",
        seed = 3407,
        report_to = "none", 
    ),
)

Full log:


  File "/root/oss_aws/devstral_test.py", line 273, in <module>
    _ = trainer.model(input_ids=x, attention_mask=torch.ones_like(x))
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/uns/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1775, in _wrapped_cal
l_impl
    return self._call_impl(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/uns/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1786, in _call_impl
    return forward_call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/uns/lib/python3.12/site-packages/peft/peft_model.py", line 1850, in forward
    return self.base_model(
           ^^^^^^^^^^^^^^^^
  File "/root/uns/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1775, in _wrapped_cal
l_impl
    return self._call_impl(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/uns/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1786, in _call_impl
    return forward_call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/uns/lib/python3.12/site-packages/peft/tuners/tuners_utils.py", line 222, in forward
    return self.model.forward(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/uns/lib/python3.12/site-packages/accelerate/hooks.py", line 175, in new_forward
    output = module._old_forward(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/transformers/src/transformers/utils/generic.py", line 810, in wrapper
    output = func(self, *args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/transformers/src/transformers/models/ministral3/modeling_ministral3.py", line 473, in fo
rward
    outputs: BaseModelOutputWithPast = self.model(
                                       ^^^^^^^^^^^
  File "/root/uns/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1775, in _wrapped_cal
l_impl
    return self._call_impl(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/uns/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1786, in _call_impl
    return forward_call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/transformers/src/transformers/utils/generic.py", line 965, in wrapper
    outputs = func(self, *args, **kwargs)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/transformers/src/transformers/models/ministral3/modeling_ministral3.py", line 409, in fo
rward
    hidden_states = decoder_layer(
                    ^^^^^^^^^^^^^^
  File "/root/transformers/src/transformers/modeling_layers.py", line 94, in __call__
    return super().__call__(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/uns/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1775, in _wrapped_cal
l_impl
    return self._call_impl(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/uns/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1786, in _call_impl
    return forward_call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^   
File "/root/uns/lib/python3.12/site-packages/accelerate/hooks.py", line 175, in new_forward         |
    output = module._old_forward(*args, **kwargs)                                                     |
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                     |
  File "/root/transformers/src/transformers/models/ministral3/modeling_ministral3.py", line 242, in fo|
rward                                                                                                 |
    hidden_states, _ = self.self_attn(                                                                |
                       ^^^^^^^^^^^^^^^                                                                |
  File "/root/uns/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1775, in _wrapped_cal|
l_impl                                                                                                |
    return self._call_impl(*args, **kwargs)                                                           |
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                           |
  File "/root/uns/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1786, in _call_impl  |
    return forward_call(*args, **kwargs)                                                              |
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                              |
  File "/root/uns/lib/python3.12/site-packages/accelerate/hooks.py", line 175, in new_forward         |
    output = module._old_forward(*args, **kwargs)                                                     |
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                     |
  File "/root/transformers/src/transformers/models/ministral3/modeling_ministral3.py", line 144, in fo|
rward                                                                                                 |
    query_states = self.q_proj(hidden_states).view(hidden_shape).transpose(1, 2)                      |
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^                                                         |
  File "/root/uns/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1775, in _wrapped_cal|
l_impl                                                                                                |
    return self._call_impl(*args, **kwargs)                                                           |
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                           |
  File "/root/uns/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1786, in _call_impl  |
    return forward_call(*args, **kwargs)                                                              |
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                              |
  File "/root/uns/lib/python3.12/site-packages/peft/tuners/lora/layer.py", line 757, in forward       |
    result = self.base_layer(x, *args, **kwargs)                                                      |
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                      |
  File "/root/uns/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1775, in _wrapped_cal|
l_impl                                                                                                |
    return self._call_impl(*args, **kwargs)                                                           |
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                           |
  File "/root/uns/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1786, in _call_impl  |
    return forward_call(*args, **kwargs)                                                              |
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                              |
  File "/root/uns/lib/python3.12/site-packages/accelerate/hooks.py", line 175, in new_forward         |
    output = module._old_forward(*args, **kwargs)                                                     |
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                     |
  File "/root/uns/lib/python3.12/site-packages/unsloth/kernels/fp8.py", line 590, in patched_forward  |
    return forward_function(X, self.weight, getattr(self, scale_attr))                                |
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                |
  File "/root/uns/lib/python3.12/site-packages/unsloth/kernels/fp8.py", line 392, in fp8_torch_block_q|
uant_forward                                                                                          |
    return FP8BlockQuantLinear.apply(X, weight, weight_scale)                                         |
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                         |
  File "/root/uns/lib/python3.12/site-packages/torch/autograd/function.py", line 581, in apply        |
    return super().apply(*args, **kwargs)  # type: ignore[misc]                                       |
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                             |
  File "/root/uns/lib/python3.12/site-packages/unsloth/kernels/fp8.py", line 347, in forward          |
    p, q = weight_scale.shape                                                                         |
    ^^^^                                                                                              |
ValueError: not enough values to unpack (expected 2, got 0) 

Activity

  1. shimmyshimmer commented on Jan 8, 2026

    @shimmyshimmer
    Member

    Have you tried using an old version of transformers and see if it works? Currently transformers v5 is still a bit buggy

  2. Klepackp commented on Jan 8, 2026

    @Klepackp
    Author

    Transformers v5 is needed for ministral3 architecture support

  3. Datta0 commented on Jan 9, 2026

    @Datta0
    Contributor

    Hey @Klepackp i see that you're possibly running an FP8 model. Do you mind sharing the model URL or so that I can inspect the model and possibly try reproducing the issue?

  4. Klepackp commented on Jan 9, 2026

    @Klepackp
    Author

    Of course, it's unsloth/Devstral-2-123B-Instruct-2512

  5. Datta0 commented on Jan 9, 2026

    @Datta0
    Contributor

    Hey @Klepackp the model seems to be FP8 with per tensor quantization which we didn't explicitly have support for so far. We do support block quantized FP8 and row/channel quantized FP8 supported.
    I'm working on adding it. Please give us some time for the same. Will update you once that is taken care of :)

  6. Klepackp commented on Jan 9, 2026

    @Klepackp
    Author

    Thanks for the heads up :)

  7. Datta0 commented on Jan 9, 2026

    @Datta0
    Contributor

    Hey @Klepackp I managed to push a fix for this, it'd be of great help if you can confirm whether this works for you.
    For installing this you can do

    pip uninstall unsloth unsloth-zoo transformers -y
    pip install git+https://github.com/datta0/unsloth@fp8_per_tensor
    

    Then you can proceed with your finetuning as usual

    PS: I'm reopening this issue till you confirm that this fix works for you :)

  8. reopened this on Jan 9, 2026
  9. self-assigned this
    on Jan 9, 2026
  10. Klepackp commented on Jan 9, 2026

    @Klepackp
    Author

    Hi, thanks for the great work! While your fix helped partially, we encountered another problem with loss calculations. Currently investigating if the issues are related or separate.

    Traceback (most recent call last):
      File "/root/oss_aws/devstral_ft_test_v3_5.py", line 140, in <module>
        trainer_stats = trainer.train()
                        ^^^^^^^^^^^^^^^
      File "/root/oss_aws/unsloth_compiled_cache/UnslothSFTTrainer.py", line 63, in wrapper
        output = f(self, *args, **kwargs)
                 ^^^^^^^^^^^^^^^^^^^^^^^^
      File "/root/uc/lib/python3.12/site-packages/transformers/trainer.py", line 2168, in train
        return inner_training_loop(
               ^^^^^^^^^^^^^^^^^^^^
      File "/root/uc/lib/python3.12/site-packages/transformers/trainer.py", line 2534, in _inner_training_loop
        tr_loss_step = self.training_step(model, inputs, num_items_in_batch)
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/root/oss_aws/unsloth_compiled_cache/UnslothSFTTrainer.py", line 1069, in training_step
        return super().training_step(*args, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "<string>", line 40, in _unsloth_training_step
      File "/root/oss_aws/unsloth_compiled_cache/UnslothSFTTrainer.py", line 1058, in compute_loss
        outputs = super().compute_loss(
                  ^^^^^^^^^^^^^^^^^^^^^
      File "/root/uc/lib/python3.12/site-packages/unsloth/models/_utils.py", line 1643, in _unsloth_pre_compute_loss
        outputs = self._old_compute_loss(model, inputs, *args, **kwargs)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "<string>", line 40, in compute_loss
      File "/root/uc/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1775, in _wrapped_call_impl
        return self._call_impl(*args, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/root/uc/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1786, in _call_impl
        return forward_call(*args, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/root/uc/lib/python3.12/site-packages/accelerate/utils/operations.py", line 819, in forward
        return model_forward(*args, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/root/uc/lib/python3.12/site-packages/accelerate/utils/operations.py", line 807, in __call__
        return convert_to_fp32(self.model_forward(*args, **kwargs))
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/root/uc/lib/python3.12/site-packages/torch/amp/autocast_mode.py", line 44, in decorate_autocast
        return func(*args, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^
      File "/root/uc/lib/python3.12/site-packages/peft/peft_model.py", line 1923, in forward
        return self.base_model(
               ^^^^^^^^^^^^^^^^
      File "/root/uc/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1775, in _wrapped_call_impl
        return self._call_impl(*args, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/root/uc/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1786, in _call_impl
        return forward_call(*args, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/root/uc/lib/python3.12/site-packages/peft/tuners/tuners_utils.py", line 308, in forward
        return self.model.forward(*args, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/root/uc/lib/python3.12/site-packages/accelerate/hooks.py", line 175, in new_forward
        output = module._old_forward(*args, **kwargs)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/root/uc/lib/python3.12/site-packages/transformers/utils/generic.py", line 790, in wrapper
        output = func(self, *args, **kwargs)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/root/uc/lib/python3.12/site-packages/transformers/models/ministral3/modeling_ministral3.py", line 491, in forward
        loss = self.loss_function(logits=logits, labels=labels, vocab_size=self.config.vocab_size, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/root/uc/lib/python3.12/site-packages/unsloth_zoo/loss_utils.py", line 126, in UnslothForCausalLMLoss
        loss = unsloth_fixed_cross_entropy(shift_logits, shift_labels, num_items_in_batch, ignore_index, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/root/uc/lib/python3.12/site-packages/unsloth_zoo/loss_utils.py", line 96, in unsloth_fixed_cross_entropy
        loss = _fast_cross_entropy_loss(
               ^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/root/uc/lib/python3.12/site-packages/unsloth/kernels/cross_entropy_loss.py", line 440, in fast_cross_entropy_loss
        loss = Fast_CrossEntropyLoss.apply(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/root/uc/lib/python3.12/site-packages/torch/autograd/function.py", line 581, in apply
        return super().apply(*args, **kwargs)  # type: ignore[misc]
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/root/uc/lib/python3.12/site-packages/unsloth/kernels/cross_entropy_loss.py", line 372, in forward
        losses.masked_fill_(labels == -100, 0)  # Don't forget to mask padding out!
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    RuntimeError: expected self and mask to be on the same device, but got mask on cuda:0 and self on cuda:3
    
  11. Datta0 commented on Jan 9, 2026

    @Datta0
    Contributor

    This is perhaps a mutli GPU thing
    definitely unrelated to devstral 2
    Can you try replacing devstral 2 with another model and see if this error persists?
    If yes, can you open another issue for that ? :)

  12. Datta0 commented on Jan 12, 2026

    @Datta0
    Contributor

    Hey @Klepackp I see you closed the issue. Did your multi GPU fail case start working or do you still need help with that ?

  13. Klepackp commented on Jan 13, 2026

    @Klepackp
    Author

    Hi, running the script with os.environ["TOKENIZERS_PARALLELISM"] = "false" fixed the issue and fine-tuning works fine now.

  14. Datta0 commented on Jan 13, 2026

    @Datta0
    Contributor

    Cool, great if it worked for you. Now if someone else faces the issue, the might find help from this :)

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

Metadata

Metadata

Assignees

Labels

bugcurrently fixing

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions