How to suppress " SourceChangeWarning"?

I am using pytorch to deploy models in a way where processes communicate over stdout/stderr pipes. I therefore want to suppress all messages coming from the pytorch backend library which are not important.

I am getting messages like “SourceChangeWarning: source code of class ‘mymoduleclasshere’ has changed. you can retrieve the original source code by accessing the object’s source attribute or set torch.nn.Module.dump_patches = True and use the patch tool to revert the changes.”

I know the source code has changed and I am fine with that, how can I suppress that message?

The warning is implemented using python’s standard warnings library, thus
import warnings warnings.filterwarnings("ignore")
should do the trick.


import warnings

This is a great solution and suppresses all the warnings. However, is it possible to suppress only a specific Warning from PyTorch?

Of course. You can do like this:

import warnings
from torch.serialization import SourceChangeWarning
warnings.filterwarnings("ignore", category=SourceChangeWarning)

As the example suggested, you need to identify a specific warning you want to suppress, which is usually indicated in original warning messages.