Fix ZeroDivisionError in test_cpu_parallel #88#146
Open
Ahmed-Rahil wants to merge 3 commits intometa-pytorch:mainfrom
Open
Fix ZeroDivisionError in test_cpu_parallel #88#146Ahmed-Rahil wants to merge 3 commits intometa-pytorch:mainfrom
Ahmed-Rahil wants to merge 3 commits intometa-pytorch:mainfrom
Conversation
There was a problem hiding this comment.
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()withtime.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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #88
Problem
The test
test_cpu_parallelwas failing withZeroDivisionError: 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 durationtime_for_1Kto evaluate to0.0. This resulted in a division by zero in the assertion logic.Solution
time.time()withtime.perf_counter(), which provides monotonic, nanosecond-resolution timing suitable for benchmarking short operations.max(..., 1e-9)clamp to the return value. This mathematically guarantees the denominator is never exactly zero, even if the operation is effectively instantaneous.Test Plan
time.perf_counter()captures microsecond-level durations wheretime.time()previously returned 0.0.Contributor Checklist
master.