Skip to content

Round floating-point floor_divide to a whole number like ATen - #23685

Merged
JakeStevens merged 1 commit into
pytorch:mainfrom
usmanmateen:fix/floor-divide-rounding
Oct 14, 2026
Merged

JakeStevens merged 1 commit into
pytorch:mainfrom
usmanmateen:fix/floor-divide-rounding

Conversation

@usmanmateen

@usmanmateen usmanmateen commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #23684.

For floating-point inputs, utils::floor_divide in kernels/portable/cpu/util/math_util.h returned (a - fmod(a, b)) / b, minus 1 when the signs differ. That quotient can be off from a whole number by a rounding error. For example, 4.2 // 1.4 gave 2.9999999999999996 in float64, so .long() on the result gives 2 instead of 3. About 7% of random float64 inputs and 6% of random float32 inputs were affected. For a zero divisor it returned ±inf by the sign of a, so 0.0 // 0.0 gave inf and the sign of a -0.0 divisor was ignored.

This follows ATen's div_floor_floating. It floors the quotient and adds 1 if the quotient was more than 0.5 above the floor. It returns a signed zero when the quotient is 0, and a / b for a zero divisor. The helper is used by floor_divide.out and by div.out_mode / div.Scalar_mode_out with rounding_mode="floor" in the portable kernels, and by the Cadence HiFi and Fusion G3 div operators.

Test plan

Added two tests to kernels/test/op_floor_divide_test.cpp:

  • FloatResultIsWholeNumber covers float64 and float32 cases whose quotient was off from a whole number, including negative results.
  • FloatDivideByZero covers 0/0, ±1/0 and ±1/-0.

Expected values match eager PyTorch. I built portable_kernels_test with CMake (EXECUTORCH_BUILD_TESTS=ON) on Linux x86_64 (WSL2). Before this change, both new tests fail. After it, all OpFloorDivideTest and OpDivOutTest tests pass, and the full portable_kernels_test run passes (1650 tests). clang-format 18.1.3 reports no changes on the two files.

This PR was authored with AI assistance (Claude Code).

cc @nil-is-all

@pytorch-bot

pytorch-bot Bot commented Oct 11, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/23685

Note: Links to docs will display an error until the docs builds have been completed.

✅ No Failures

As of commit 69a3b5e with merge base 6cc8edf (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 11, 2026
@usmanmateen

Copy link
Copy Markdown
Contributor Author

@pytorchbot label "release notes: ops & kernels"

@pytorch-bot pytorch-bot Bot added the release notes: ops & kernels Changes to the opset and any new / changed kernel implementations label Oct 11, 2026
@executorch-triage executorch-triage Bot added the community: contribution PRs coming from community (excluding hardware partners) label Oct 11, 2026

@JakeStevens JakeStevens left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

can you rebase and re run the tests? a lot got skipped. LGTM once we are green

utils::floor_divide returned (a - fmod(a, b)) / b, which can be off from
a whole number by a rounding error (4.2 // 1.4 gave 2.9999999999999996),
and returned +-inf by the sign of a for a zero divisor. Follow ATen's
div_floor_floating: floor the quotient and round it up if it is more
than 0.5 above, and return a / b for a zero divisor.

Fixes pytorch#23684
@usmanmateen
usmanmateen force-pushed the fix/floor-divide-rounding branch from 293bfe2 to 69a3b5e Compare October 13, 2026 19:22
@usmanmateen

Copy link
Copy Markdown
Contributor Author

Rebased on main (6cc8edf). Locally, portable_kernels_test passes 1648 and optimized_kernels_test passes 1655, including the 13 floor_divide and div-mode tests. CI should run fully once the workflows are approved.

@JakeStevens
JakeStevens merged commit 299eadd into pytorch:main Oct 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. community: contribution PRs coming from community (excluding hardware partners) release notes: ops & kernels Changes to the opset and any new / changed kernel implementations

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Portable floor_divide returns non-whole numbers for floats and wrong values for a zero divisor

2 participants