pytorch - ✅(Solved) Fix PyTorch profiler warnings spam [1 pull requests, 1 comments, 2 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#180348Fetched 2026-04-16 06:35:07
View on GitHub
Comments
1
Participants
2
Timeline
32
Reactions
0
Author
Assignees
Timeline (top)
mentioned ×14subscribed ×14assigned ×1commented ×1

Fix Action

Fixed

PR fix notes

PR #180387: Fix profiler event clearing warning to only fire when events are actually cleared

Description (problem / solution / changelog)

Summary

The warning added in #168066 (for #148314) fires on every profiler use, including simple single-use with torch.profiler.profile(): ... where there are no cycles and the warning is irrelevant.

The root cause is that the warning checked self.profiler is not None after creating the profiler, so the condition was always true. This change moves the check before profiler recreation, so it only triggers when self.profiler already exists from a previous cycle and is about to be replaced, which is also the moment events are cleared.

Also fixes missing spaces between sentences in the warning message.

Fixes #180348

Test code:

import torch


def test_no_schedule():
    """No schedule — should produce no warning."""
    with torch.profiler.profile() as prof:
        for _ in range(5):
            torch.ones(10)


def test_schedule_with_acc_events():
    """Schedule with acc_events=True — should produce no warning."""
    schedule = torch.profiler.schedule(wait=1, warmup=1, active=2, repeat=2)
    with torch.profiler.profile(schedule=schedule, acc_events=True) as prof:
        for _ in range(10):
            torch.ones(10)
            prof.step()


def test_schedule_without_acc_events():
    """Schedule with acc_events=False — should warn once on second cycle."""
    schedule = torch.profiler.schedule(wait=1, warmup=1, active=2, repeat=2)
    with torch.profiler.profile(schedule=schedule) as prof:
        for _ in range(10):
            torch.ones(10)
            prof.step()


if __name__ == "__main__":
    print("=== Test 1: no schedule ===")
    test_no_schedule()
    print()

    print("=== Test 2: schedule + acc_events=True ===")
    test_schedule_with_acc_events()
    print()

    print("=== Test 3: schedule + acc_events=False ===")
    test_schedule_without_acc_events()

Log:

=== Test 1: no schedule ===
USDT:2026-04-14 14:24:25 3958665:3958665 ActivityProfilerController.cpp:415] profiler_start
USDT:2026-04-14 14:24:25 3958665:3958665 ActivityProfilerController.cpp:455] profiler_stop

=== Test 2: schedule + acc_events=True ===
USDT:2026-04-14 14:24:25 3958665:3958665 ActivityProfilerController.cpp:415] profiler_start
USDT:2026-04-14 14:24:25 3958665:3958665 ActivityProfilerController.cpp:455] profiler_stop
USDT:2026-04-14 14:24:25 3958665:3958665 ActivityProfilerController.cpp:415] profiler_start
USDT:2026-04-14 14:24:25 3958665:3958665 ActivityProfilerController.cpp:455] profiler_stop

=== Test 3: schedule + acc_events=False ===
USDT:2026-04-14 14:24:25 3958665:3958665 ActivityProfilerController.cpp:415] profiler_start
USDT:2026-04-14 14:24:25 3958665:3958665 ActivityProfilerController.cpp:455] profiler_stop
/home/jiannanwang/Workspace/pytorch/torch/profiler/profiler.py:254: UserWarning: Warning: Profiler clears events at the end of each cycle. Only events from the current cycle will be reported. To keep events across cycles, set acc_events=True.
  _warn_once(
USDT:2026-04-14 14:24:25 3958665:3958665 ActivityProfilerController.cpp:415] profiler_start
USDT:2026-04-14 14:24:25 3958665:3958665 ActivityProfilerController.cpp:455] profiler_stop

Changed files

  • torch/profiler/profiler.py (modified, +6/-6)

Code Example

import torch

with torch.profiler.profile():
    torch.ones(10)

---

UserWarning: Warning: Profiler clears events at the end of each cycle.Only events from the current cycle will be reported.To keep events across cycles, set acc_events=True.
  _warn_once(
RAW_BUFFERClick to expand / collapse

🐛 Describe the bug

import torch

with torch.profiler.profile():
    torch.ones(10)

Produces:

UserWarning: Warning: Profiler clears events at the end of each cycle.Only events from the current cycle will be reported.To keep events across cycles, set acc_events=True.
  _warn_once(
  1. The logic in https://github.com/pytorch/pytorch/pull/168066 says "The warning is triggered only once when self.profiler is not None" but the code immediately before the warning initializes self.profiler
  2. The warning message is missing spaces at the end of each sentence.

cc @robieta @chaekit @guotuofeng @guyang3532 @dzhulgakov @davidberard98 @briancoutinho @sraikund16 @sanrise @mwootton @divyanshk @jiannanWang @scotts @ryanzhang22

Is this by intentional?

Versions

on main

extent analysis

TL;DR

The warning is likely due to the profiler's behavior of clearing events at the end of each cycle, and setting acc_events=True may resolve the issue.

Guidance

  • The warning message indicates that the profiler clears events at the end of each cycle, which might be the intended behavior, but it's unclear why the warning is triggered despite self.profiler being initialized.
  • To mitigate the warning, try setting acc_events=True when creating the profiler, as suggested in the warning message.
  • Verify that the warning is resolved by checking the output after applying the suggested change.
  • Investigate the logic in the referenced pull request to understand the intended behavior of the profiler.

Example

import torch

with torch.profiler.profile(acc_events=True):
    torch.ones(10)

Notes

The issue lacks information about the desired behavior of the profiler, making it difficult to provide a definitive solution. The suggested change is based on the warning message and may not be the intended fix.

Recommendation

Apply workaround: setting acc_events=True when creating the profiler, as it may resolve the warning and provide the desired behavior.

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