Skip to content

[Feature] Compute WER/CER metrics with Gemma3 #3470

Closed

Description

@thewh1teagle

Hey!
I'm using unsloth to fine tune gemma3-270m model for task of converting Hebrew into IPA phonemes (G2P).
It's similar task like translating Hebrew into English.
I have 5 million pairs of Hebrew <> IPA phonemes and I use the default training recipe

I tried to use this compute_metrics approrch:

def compute_metrics(eval_pred, tokenizer):
    predictions, labels = eval_pred
    breakpoint()
    
    # Convert logits to token IDs (take argmax)
    if predictions.ndim == 3:  # (batch_size, seq_len, vocab_size)
        predictions = np.argmax(predictions, axis=-1)
    
    # Decode predictions
    decoded_preds = tokenizer.batch_decode(predictions, skip_special_tokens=True)
    
    # Replace -100 in labels (used for padding) with pad_token_id
    labels = np.where(labels != -100, labels, tokenizer.pad_token_id)
    decoded_labels = tokenizer.batch_decode(labels, skip_special_tokens=True)

    wer_score = wer.compute(predictions=decoded_preds, references=decoded_labels)
    cer_score = cer.compute(predictions=decoded_preds, references=decoded_labels)

    wer_acc = (1 - wer_score) * 100
    cer_acc = (1 - cer_score) * 100

    return {
        "wer": wer_score,
        "cer": cer_score,
        "wer_acc": wer_acc,
        "cer_acc": cer_acc,
    }

But it seems like the decoded predictions/labels are invalid. it contains non readable characters.
How can I correctly compute WER/CER during training? this is very important evaluation for such tasks since the loss doesn't tell much about the performance.

This is the training code I use:

should be comfortable reproducible code. It takes few minutes to setup the full training including the 100MB data I trained on

Thank you!

Related

Activity

  1. added
    feature request
    on Oct 17, 2025
  2. self-assigned this
    on Oct 9, 2026
  3. added a commit that references this issue on Oct 9, 2026
    371da3d
  4. danielhanchen commented on Oct 9, 2026

    @danielhanchen
    Member

    Fixed! Sorry on the delay. Two things were going on.

    1. The unreadable text comes from the decoding itself: eval_pred.predictions is a single teacher-forced forward pass, so argmax(logits) at position t is the guess for token t + 1, and decoding it unshifted (and over the prompt tokens too) gives garbage even with plain transformers. Shift by one and keep only the labelled positions:
    def preprocess_logits_for_metrics(logits, labels):
        if isinstance(logits, tuple): logits = logits[0]
        return logits.argmax(-1)
    
    def compute_metrics(eval_pred):
        preds  = eval_pred.predictions[:, :-1]  # logits[t] predicts token t+1
        labels = eval_pred.label_ids[:, 1:]
        mask   = labels != -100
        pred_texts  = [tokenizer.decode(p[m], skip_special_tokens = True) for p, m in zip(preds, mask)]
        label_texts = [tokenizer.decode(l[m], skip_special_tokens = True) for l, m in zip(labels, mask)]
        return {"wer": wer.compute(predictions = pred_texts, references = label_texts),
                "cer": cer.compute(predictions = pred_texts, references = label_texts)}

    For true WER / CER on G2P, run model.generate on the eval prompts (for example in a TrainerCallback.on_evaluate), since the shifted predictions are still teacher-forced.

    1. There were also two real Unsloth bugs, fixed in Fix evaluate() under auto padding-free and keep compute_metrics rows per example #13136: with compute_metrics set, Unsloth still enabled padding-free batching, so each row of predictions held a whole batch concatenated instead of one example; and trainer.evaluate() called outside train() reported a wrong loss on packed batches (Gemma 3 270M eval loss 3.82 instead of 2.37). On a Hebrew to IPA setup like yours, Gemma 3 270M now gets eval loss 0.822 and CER 0.209 with the shifted decoding, matching plain transformers (0.819, 0.209).

    To update: uv pip install --upgrade unsloth unsloth_zoo --force-reinstall --no-cache-dir (if it's not in the current release yet it'll be in the next one). Reopen if it still happens.

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

Metadata

Metadata

Assignees

Labels

feature request

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions