Skip to content

Avoid floating point math for ceil operations#44729

Open
xenova wants to merge 5 commits intomainfrom
export-ceil-floor-fix
Open

Avoid floating point math for ceil operations#44729
xenova wants to merge 5 commits intomainfrom
export-ceil-floor-fix

Conversation

@xenova
Copy link
Copy Markdown
Contributor

@xenova xenova commented Mar 15, 2026

What does this PR do?

This PR introduces a helper utility function, int_div_ceil, which performs math.ceil(a / b) for non-negative integer operands. This is necessary as the current approach is both error-prone and imprecise (especially when exporting models via torch dynamo, or similar).

This bug was originally discovered when exporting granite-speech to ONNX (microsoft/onnxruntime#27661), where the current approach will require casting integers to floats then back to integers, causing precision issues

  CPU: Ceil(165/15) = 11 (PASS)
  GPU: Ceil(165/15) = 12 (FAIL)

A similar issue seems to have been in the library for the fuyu processor:

math.ceil(torch.tensor(300).cuda() / 30) == 11

whereas

int_div_ceil(torch.tensor(300).cuda(), 30) == 10

operates as expected (because operations are performed as integers).

Fixes # (issue)

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline,
    Pull Request section?
  • Was this discussed/approved via a Github issue or the forum? Please add a link
    to it if that's the case.
  • Did you make sure to update the documentation with your changes? Here are the
    documentation guidelines, and
    here are tips on formatting docstrings.
  • Did you write any new necessary tests?

Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

@ArthurZucker

@github-actions
Copy link
Copy Markdown
Contributor

[For maintainers] Suggested jobs to run (before merge)

run-slow: bit, chmv2, dac, depth_pro, dpt, encodec, eomt, falcon_mamba, fuyu, gemma3, granite_speech, higgs_audio_v2_tokenizer, idefics3, jamba, layoutlmv2, lfm2_vl

@xenova
Copy link
Copy Markdown
Contributor Author

xenova commented Mar 15, 2026

ci formatting issue seems to be unrelated 🤷

@HuggingFaceDocBuilderDev
Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants