Skip to content

Fix ZeroDivisionError in test_cpu_parallel #88#146

Open
Ahmed-Rahil wants to merge 3 commits intometa-pytorch:mainfrom
Ahmed-Rahil:fix-issue-88-zero-division
Open

Fix ZeroDivisionError in test_cpu_parallel #88#146
Ahmed-Rahil wants to merge 3 commits intometa-pytorch:mainfrom
Ahmed-Rahil:fix-issue-88-zero-division

Conversation

@Ahmed-Rahil
Copy link

Summary

Fixes #88

Problem

The test test_cpu_parallel was failing with ZeroDivisionError: float division by zero.
This occurred because the benchmarked operation (measure(1000)) runs extremely fast on modern CPUs. The original timer, time.time(), has insufficient resolution (often ~15ms on Windows/some CI), causing the measured duration time_for_1K to evaluate to 0.0. This resulted in a division by zero in the assertion logic.

Solution

  1. High-Resolution Timing: Replaced time.time() with time.perf_counter(), which provides monotonic, nanosecond-resolution timing suitable for benchmarking short operations.
  2. Safety Guard: Added a max(..., 1e-9) clamp to the return value. This mathematically guarantees the denominator is never exactly zero, even if the operation is effectively instantaneous.
  3. Robustness: Increased the loop count from 10 to 20 to slightly stabilize the measurement, though the timer change is the primary fix.

Test Plan

  • Verification: Ran the modified test logic locally. Confirmed that time.perf_counter() captures microsecond-level durations where time.time() previously returned 0.0.
  • CI: Waiting for CircleCI to pass the full suite.

Contributor Checklist

  • I have signed the CLA.
  • The code adheres to the project's linting standards.
  • Tests have been added/updated to cover the changes.
  • My branch is up-to-date with master.

Copilot AI review requested due to automatic review settings January 20, 2026 17:37
@meta-cla meta-cla bot added the cla signed label Jan 20, 2026
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR addresses a ZeroDivisionError in the test_cpu_parallel test that occurred when using time.time() for benchmarking fast operations. The low resolution of time.time() caused measured durations to be 0.0 on some systems, leading to division by zero errors.

Changes:

  • Replaced time.time() with time.perf_counter() for high-resolution timing
  • Added a safety guard max(finish - start, 1e-9) to prevent zero durations
  • Modified the assertion logic to include torch.get_num_threads() in the calculation

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Author

@Ahmed-Rahil Ahmed-Rahil left a comment

Choose a reason for hiding this comment

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

Done

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ZeroDivisionError: float division by zero in test_cpu_parallel

2 participants