Skip to content

[models] Keep the early stop of MASTER decoding out of the ONNX export - #2225

Merged
felixdittrich92 merged 2 commits into
mindee:mainfrom
sibisrinivasb:fix-master-onnx-export
Nov 13, 2026
Merged

felixdittrich92 merged 2 commits into
mindee:mainfrom
sibisrinivasb:fix-master-onnx-export

Conversation

@sibisrinivasb

Copy link
Copy Markdown
Contributor

Fixes #2224.

What

MASTER.decode now skips its early stop when the model is exportable, the same guard PARSeq already has:

# Stop decoding if all sequences have reached the EOS token
# NOTE: `break` isn't correctly translated to Onnx so we don't break here if we want to export
if not self.exportable and (ys == self.vocab_size).any(dim=-1).all():
    break

Why

export_model_to_onnx traces the model, so the stop added in #2118 was evaluated once on the dummy input and the exported graph kept only the decoding steps the dummy needed. PyTorch inference is unchanged and keeps the early stop.

master(pretrained=True, exportable=True), exported with torch.rand((2, 3, 32, 128)) under five seeds, then run on 24 rendered words of 1 to 22 characters (all 24 read correctly by PyTorch):

seed words identical to PyTorch, main with this change
0 3 / 24 24 / 24
1 16 / 24 24 / 24
2 6 / 24 24 / 24
3 6 / 24 24 / 24
4 1 / 24 24 / 24

An exported MASTER therefore always runs max_length - 1 decoding steps, as before #2118.

Test

test_master_decode_early_stop forces EOS at the first decoding step and counts the decoder calls: 1 without exportable, max_length - 1 with it. It fails on main for exportable=True and needs no pretrained weights.

Checked

  • pytest tests/pytorch/test_models_recognition_pt.py -k master: 12 passed, 1 skipped (the master case of test_models_onnx_export, under 16 GB here).
  • ruff check and ruff format --check on the two files: clean.
  • Not tested: GPU, other torch / onnxruntime versions (here torch 2.15.0, onnxruntime 1.31.1), the dynamo exporter.

The early stop added to MASTER.decode in mindee#2118 is traced by export_model_to_onnx (dynamo=False): the exported graph then runs only as many decoding steps as the dummy input needed, and longer words come out truncated or filled with a repeated character. Guard the stop with not self.exportable, as PARSeq does, and add a test that fails without the guard.
@codecov

codecov Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.15%. Comparing base (dbcee0d) to head (44b9ab7).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2225      +/-   ##
==========================================
- Coverage   98.16%   98.15%   -0.01%     
==========================================
  Files         172      172              
  Lines       11977    11977              
==========================================
- Hits        11757    11756       -1     
- Misses        220      221       +1     
Flag Coverage Δ
unittests 98.15% <100.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@felixdittrich92 felixdittrich92 self-assigned this Nov 13, 2026
@felixdittrich92 felixdittrich92 added type: bug Something isn't working ext: tests Related to tests folder topic: text recognition Related to the task of text recognition topic: onnx ONNX-related labels Nov 13, 2026
@felixdittrich92 felixdittrich92 added this to the 1.2.0 milestone Nov 13, 2026

@felixdittrich92 felixdittrich92 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @sibisrinivasb 👍

Only a small change and we can merge.

@felixdittrich92
felixdittrich92 merged commit c7111b6 into mindee:main Nov 13, 2026
56 of 57 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ext: tests Related to tests folder topic: onnx ONNX-related topic: text recognition Related to the task of text recognition type: bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ONNX export of MASTER keeps only as many decoding steps as the dummy input needed (the early stop in decode is traced)

2 participants