pytorch - 💡(How to fix) Fix print is repeated sometimes when the calc is done on gpu [6 comments, 4 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
pytorch/pytorch#178049Fetched 2026-04-08 01:07:21
View on GitHub
Comments
6
Participants
4
Timeline
11
Reactions
0
Timeline (top)
commented ×6labeled ×3mentioned ×1subscribed ×1

Code Example

print(f"if dim == {dim},   with time {(time_end - time_start)} and {(time_end - time_start)/test_time} per test")
                print(f"result_length__log2_mul_result_length__log2__vs__log2__min   ={str_the_list(result_length__log2_mul_result_length__log2__vs__log2__min, 3)}")
                print(f"result_length__log2_mul_result_length__log2__vs__log2__avg   ={str_the_list(result_length__log2_mul_result_length__log2__vs__log2__avg, 3)}")
                print(f"halfway_mat__vs__log4__min                                   ={str_the_list(halfway_mat__vs__log4__min                                   , 3)}")
                print(f"halfway_mat__vs__log4__avg                                   ={str_the_list(halfway_mat__vs__log4__avg                                   , 3)}")
                print(f"result_length__log14_mul_result_length__log14__vs__log14__min={str_the_list(result_length__log14_mul_result_length__log14__vs__log14__min, 3)}")
                print(f"result_length__log14_mul_result_length__log14__vs__log14__avg={str_the_list(result_length__log14_mul_result_length__log14__vs__log14__avg, 3)}")
RAW_BUFFERClick to expand / collapse

🐛 Describe the bug

[[[No immediate action needed]]]

If the calc is done on gpu, and result is printed, sometimes it prints more than once. Although it's still acceptable.

It looks like if dim == 100, with time 0:00:23.553793 and 0:00:00.157025 per test result_length__log2_mul_result_length__log2__vs__log2__min =[ 15.633, 15.616, 15.627, 15.611, 15.583, 15.577, 15.611, 15.587] 150 if dim == 100, with time 0:00:23.553793 and 0:00:00.157025 per test result_length__log2_mul_result_length__log2__vs__log2__min =[ 15.633, 15.616, 15.627, 15.611, 15.583, 15.577, 15.611, 15.587] if dim == 100, with time 0:00:23.553793 and 0:00:00.157025 per test result_length__log2_mul_result_length__log2__vs__log2__min =[ 15.633, 15.616, 15.627, 15.611, 15.583, 15.577, 15.611, 15.587] result_length__log2_mul_result_length__log2__vs__log2__min =[ 15.633, 15.616, 15.627, 15.611, 15.583, 15.577, 15.611, 15.587] result_length__log2_mul_result_length__log2__vs__log2__avg =[ 15.713, 15.719, 15.697, 15.692, 15.675, 15.680, 15.693, 15.698] halfway_mat__vs__log4__min =[ 15.799, 15.805, 15.801, 15.803, 15.794, 15.759, 15.740, 15.740] halfway_mat__vs__log4__avg =[ 15.822, 15.826, 15.820, 15.819, 15.811, 15.786, 15.771, 15.764] result_length__log14_mul_result_length__log14__vs__log14__min=[ 8.492, 8.525, 8.505, 8.523, 8.541, 8.520, 8.480, 8.523] result_length__log14_mul_result_length__log14__vs__log14__avg=[ 8.636, 8.632, 8.629, 8.631, 8.634, 8.620, 8.631, 8.632] result_mat__vs__random_result_mat__min =[ 9.217, 9.151, 9.050, 9.015, 8.953, 8.760, 8.666, 8.677] result_mat__vs__random_result_mat__avg =[ 9.359, 9.258, 9.179, 9.116, 9.060, 8.872, 8.813, 8.786]

The code is

print(f"if dim == {dim},   with time {(time_end - time_start)} and {(time_end - time_start)/test_time} per test")
                print(f"result_length__log2_mul_result_length__log2__vs__log2__min   ={str_the_list(result_length__log2_mul_result_length__log2__vs__log2__min, 3)}")
                print(f"result_length__log2_mul_result_length__log2__vs__log2__avg   ={str_the_list(result_length__log2_mul_result_length__log2__vs__log2__avg, 3)}")
                print(f"halfway_mat__vs__log4__min                                   ={str_the_list(halfway_mat__vs__log4__min                                   , 3)}")
                print(f"halfway_mat__vs__log4__avg                                   ={str_the_list(halfway_mat__vs__log4__avg                                   , 3)}")
                print(f"result_length__log14_mul_result_length__log14__vs__log14__min={str_the_list(result_length__log14_mul_result_length__log14__vs__log14__min, 3)}")
                print(f"result_length__log14_mul_result_length__log14__vs__log14__avg={str_the_list(result_length__log14_mul_result_length__log14__vs__log14__avg, 3)}")

Although some result doesn't rely on anything retrived back from gpu, it still repeats. Like: if dim == 100, with time 0:00:23.553793 and 0:00:00.157025 per test

Versions

2.9, win11, py 3.13. Probably.

extent analysis

Fix Plan

The issue seems to be related to concurrent execution of print statements. To fix this, we can use a lock to synchronize access to the print statements.

  • Create a lock object: import threading; print_lock = threading.Lock()
  • Acquire the lock before printing and release it afterwards:
with print_lock:
    print(f"if dim == {dim},   with time {(time_end - time_start)} and {(time_end - time_start)/test_time} per test")
    print(f"result_length__log2_mul_result_length__log2__vs__log2__min   ={str_the_list(result_length__log2_mul_result_length__log2__vs__log2__min, 3)}")
    print(f"result_length__log2_mul_result_length__log2__vs__log2__avg   ={str_the_list(result_length__log2_mul_result_length__log2__vs__log2__avg, 3)}")
    print(f"halfway_mat__vs__log4__min                                   ={str_the_list(halfway_mat__vs__log4__min                                   , 3)}")
    print(f"halfway_mat__vs__log4__avg                                   ={str_the_list(halfway_mat__vs__log4__avg                                   , 3)}")
    print(f"result_length__log14_mul_result_length__log14__vs__log14__min={str_the_list(result_length__log14_mul_result_length__log14__vs__log14__min, 3)}")
    print(f"result_length__log14_mul_result_length__log14__vs__log14__avg={str_the_list(result_length__log14_mul_result_length__log14__vs__log14__avg, 3)}")

Verification

To verify that the fix worked, run the code and check if the print statements are executed sequentially without any overlaps.

Extra Tips

  • Make sure to release the lock after printing to avoid deadlocks.
  • Consider using a logging library instead of print statements for more flexibility and control over output.
  • If you're using a GPU for computations, ensure that the GPU is properly synchronized with the CPU to avoid any concurrency issues.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

pytorch - 💡(How to fix) Fix print is repeated sometimes when the calc is done on gpu [6 comments, 4 participants]