Skip to content

CohereASR training-loss double-shift bug (same pattern as Moonshine #46784) #46894

Description

@sharmax-vikas

System Info

  • transformers version: 5.13.0.dev0 (main branch at commit dc5a497)
    • Platform: Windows
      • Python version: 3.x
        • Using GPU: N/A (code-level bug, not runtime)

Who can help?

@eustlb @ebezzam @vasqu

Information

  • The official example scripts
  • My own modified scripts

Tasks

  • An officially supported task in the examples folder (such as GLUE/SQuAD, ...)
  • My own task or dataset (give details below)

Reproduction

aIn src/transformers/models/cohere_asr/modular_cohere_asr.py, CohereAsrForConditionalGeneration.forward() has a double-shift bug:

  1. Labels are shifted right via shift_tokens_right() (line 483) to create decoder_input_ids
    1. Then self.loss_function() is called (line 503), which maps to ForCausalLMLoss
    1. ForCausalLMLoss shifts labels again internally (labels[..., 1:] in loss/loss_utils.py line 63-64)
      This means the model trains against labels[..., 1:] instead of labels.

This is the exact same bug that was fixed in Moonshine in PR #46784 (commit d8c2354). The Moonshine fix replaced self.loss_function() with CrossEntropyLoss(), but CohereAsr — which inherits from Moonshine and overrides forward — was not updated.

# Buggy code (line 503 in modular_cohere_asr.py):
loss = self.loss_function(logits=logits, labels=labels, vocab_size=self.config.vocab_size)

# Should be:
loss_fct = CrossEntropyLoss()
loss = loss_fct(logits.reshape(-1, self.config.vocab_size), labels.reshape(-1))

Expected behavior

Training loss should be computed directly against labels without any additional shifting, since shift_tokens_right() already handles the decoder input alignment. This matches the behavior of Whisper, Bart, and the now-fixed Moonshine model. The fix should use CrossEntropyLoss() instead of self.loss_function(), as done in Moonshine PR #46784.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions