Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

_round_mantissa rounds values just below 0.5 up to 1

Open Beginner friendly
#49 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
85/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python, pytorch

Research direction

Start by reading _round_mantissa in the mx.elemwise_ops module and reproduce the issue with the Python example in the report. Add regression coverage for values just below 0.5 in both modes and verify the stated tie cases still round correctly; done when both modes return 0 for the reported input and the tie behavior is preserved.

Written by the indexing model from the issue text.

Description

Hi, _round_mantissa rounds a value just below 0.5 up to 1 in both the "even" and "nearest" modes. torch.floor(torch.abs(A) + 0.5) is evaluated in float32, and the addition already rounds up to 1.0 before the floor.

import torch
from mx.elemwise_ops import _round_mantissa

a = torch.tensor([float.fromhex("0x1.fffffep-2")])  # just below 0.5
for mode in ("even", "nearest"):
    print(mode, _round_mantissa(a, 3, mode).tolist())
# Actual: [1.0] for both
# Expected: [0.0] for both

In an MX block this shows up as an element just below half the smallest magnitude rounding up. For example, in FP4 E2M1 at scale 2^0, the element 0x1.fffffep-3 becomes 0.5 instead of 0.

Reproduced on main at 7bc41952de394f5cc5e782baf132e7c7542eb4e4, with Python 3.12.13 and PyTorch 2.13.0 on CPU.

torch.round already rounds half to even without the intermediate addition, and the "nearest" branch can compare the fractional part instead:

     elif round == "nearest":
-        A = torch.sign(A) * torch.floor(torch.abs(A) + 0.5)
-    elif round == "even":
         absA = torch.abs(A)
-        # find 0.5, 2.5, 4.5 ...
-        maskA = ((absA - 0.5) % 2 == torch.zeros_like(A)).type(A.dtype)
-        A = torch.sign(A) * (torch.floor(absA + 0.5) - maskA)
+        A = torch.sign(A) * (torch.floor(absA) + (absA - torch.floor(absA) >= 0.5).type(A.dtype))
+    elif round == "even":
+        A = torch.round(A)

With this change both modes give 0 here, ties still round correctly (0.5, 1.5, 2.5 and -2.5), and the output on 8,000 random blocks per element format is unchanged. Happy to send a PR with regression tests :)

Dominant language
Python
Stars
365
Forks
54
PR merge metrics
No merged PRs in 30d

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from microsoft/microxcaling

All issues in microsoft/microxcaling

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.