Skip to content

Commit

Permalink
Change Dynamo's custom ops warning message to be less spammy (#128456)
Browse files Browse the repository at this point in the history
Summary:
This is a short-term fix (for 2.4). In the longer term we should
fix pytorch/pytorch#128430

The problem is that warnings.warn that are inside Dynamo print
all the time. Python warnings are supposed to print once, unless their
cache is reset: Dynamo ends up resetting that cache everytime it runs.

As a workaround we provide our own warn_once cache that is keyed on the
warning msg. I am not worried about this increasing memory usage because
that's effectively what python's warnings.warn cache does.

X-link: pytorch/pytorch#128456
Approved by: https://github.com/anijain2305

Reviewed By: clee2000

Differential Revision: D58501328

Pulled By: zou3519

fbshipit-source-id: 99dcfddfae27de2f1de6e9685aa990a738531199
  • Loading branch information
zou3519 authored and facebook-github-bot committed Jun 13, 2024
1 parent 04a1339 commit ab687a0
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions userbenchmark/dynamo/dynamobench/_dynamo/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import time
import types
import typing
import warnings
import weakref
from contextlib import contextmanager
from functools import lru_cache, wraps
Expand Down Expand Up @@ -2751,3 +2752,18 @@ def __init__(self, s):

def __repr__(self):
return self.s


warn_once_cache: Set[str] = set()


def warn_once(msg, stacklevel=1):
# Dynamo causes all warnings.warn (in user code and in Dynamo code) to print all the time.
# https://github.com/pytorch/pytorch/issues/128427.
# warn_once is a workaround: if the msg has been warned on before, then we will not
# warn again.
# NB: it's totally ok to store a cache of all the strings: this is what warnings.warn does as well.
if msg in warn_once_cache:
return
warn_once_cache.add(msg)
warnings.warn(msg, stacklevel=stacklevel + 1)

0 comments on commit ab687a0

Please sign in to comment.