How to suppress Constant folding Warning?

DzenanZ: <p>During training process, my console is swamped by <code>Warning: Constant folding - Only steps=1 can be constant folded for opset &gt;= 10 onnx::Slice op. Constant folding not applied.</code> I tried a few variants of</p>
<pre data-code-wrap="py"><code class="lang-py">warnings.filterwarnings(
    "once",
    message="Constant folding not applied",
)
</code></pre>
<p>in both <code>util.py</code> and <code>train.py</code>, to no effect.</p>
<p>Digging through the code, I have traced a lot of those warnigs to <code>torch._C._jit_pass_onnx_graph_shape_type_inference(graph, params_dict, _export_onnx_opset_version)</code> on line 252 of <code>C:\Users\Dzenan\miniconda3\envs\deep_learning\Lib\site-packages\torch\onnx\utils.py</code>. My PyTorch Version is 1.11.0. I assume that <code>torch._C</code> calls C or C++ code (Python debugger does not step into it). Maybe origination in C/C++ code makes those warnings funky, so they don’t get the same treatment as other Python warnings?</p>
<p>I invoke it via:</p>
<pre data-code-wrap="py"><code class="lang-py">torch.onnx.export(
    model,
    dummy_input,
    model_path,
    export_params=True,
    opset_version=11,
    do_constant_folding=False,
    input_names=["input"],
    output_names=["output"],
    dynamic_axes={"input": {0: "batch_size"}, "output": {0: "batch_size"}},
)
</code></pre>
<p>Stack trace and locals at line 252:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/a/1/a1a8857bb18c30454ea23149de257660291a515d.png" data-download-href="https://discuss.pytorch.org/uploads/default/a1a8857bb18c30454ea23149de257660291a515d" title="Screenshot 2022-10-17 17.38.30"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/a/1/a1a8857bb18c30454ea23149de257660291a515d_2_690x132.png" alt="Screenshot 2022-10-17 17.38.30" data-base62-sha1="n45Vy40zNf4iBL4Gi1sCLXSheEZ" width="690" height="132" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/a/1/a1a8857bb18c30454ea23149de257660291a515d_2_690x132.png, https://discuss.pytorch.org/uploads/default/optimized/3X/a/1/a1a8857bb18c30454ea23149de257660291a515d_2_1035x198.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/a/1/a1a8857bb18c30454ea23149de257660291a515d_2_1380x264.png 2x" data-dominant-color="424546"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screenshot 2022-10-17 17.38.30</span><span class="informations">1781×341 38.5 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div><br>
Does anyone have a suggestion?</p>

DzenanZ: <p>There is also an <a href="https://github.com/pytorch/pytorch/issues/73843" rel="noopener nofollow ugc">open issue</a> at GitHub. I tried suggestions from <a href="https://stackoverflow.com/questions/14463277/how-to-disable-python-warnings" rel="noopener nofollow ugc">Stack Overflow</a>, to no effect.</p>

DzenanZ: <p>I see that some people looked at this - thank you. Are there really no suggestions?</p>

DzenanZ: <p>As far as I can tell, this problem is PyTorch-specific. Is this forum not the best place to look for answer?</p>

ptrblck: <p>While the issue seems to be raised by PyTorch, I believe the ONNX code owners might not be looking into the discussion board a lot.<br>
I don’t know why the warning is still raised even after you’ve used <code>do_constant_folding=False</code>, so feel free to comment on the open GitHub issue so that the code owners could see it.<br>
A brute-force approach would be to rip out the warning from <a href="https://github.com/pytorch/pytorch/blob/7e43c6f28e9b0699dd6f0a3803513feacc60eaf3/torch/csrc/jit/passes/onnx/constant_fold.cpp#L179-L181">here</a> and rebuild a custom PyTorch version in case you cannot filter it out.</p>

leshabirukov: <p>You can supress theese warnings hard way, (but not so hard as ripping code from PyTorch):</p>
<pre><code class="lang-auto">import sys
import os

class SuppressStream(object): 

    def __init__(self):
        self.orig_stream_fileno = sys.stderr.fileno()

    def __enter__(self):
        self.orig_stream_dup = os.dup(self.orig_stream_fileno)
        self.devnull = open(os.devnull, 'w')
        os.dup2(self.devnull.fileno(), self.orig_stream_fileno)

    def __exit__(self, type, value, traceback):
        os.close(self.orig_stream_fileno)
        os.dup2(self.orig_stream_dup, self.orig_stream_fileno)
        os.close(self.orig_stream_dup)
        self.devnull.close()

with SuppressStream():
    torch.onnx.export( model, ...

</code></pre>
<p>Thanks to<br>
<a href="https://stackoverflow.com/questions/24277488/in-python-how-to-capture-the-stdout-from-a-c-shared-library-to-a-variable/57677370#57677370" rel="noopener nofollow ugc">In python, how to capture the stdout from a c++ shared library to a variable - Stack Overflow</a></p><aside class="onebox githubissue" data-onebox-src="https://github.com/joblib/joblib/issues/868#issuecomment-1034684910">
  <header class="source">

      <a href="https://github.com/joblib/joblib/issues/868#issuecomment-1034684910" target="_blank" rel="noopener nofollow ugc">github.com/joblib/joblib</a>
  </header>

  <article class="onebox-body">
    <div class="github-row">
  <div class="github-icon-container" title="Issue" data-github-private-repo="false">
	  <svg width="60" height="60" class="github-icon" viewBox="0 0 14 16" aria-hidden="true"><path fill-rule="evenodd" d="M7 2.3c3.14 0 5.7 2.56 5.7 5.7s-2.56 5.7-5.7 5.7A5.71 5.71 0 0 1 1.3 8c0-3.14 2.56-5.7 5.7-5.7zM7 1C3.14 1 0 4.14 0 8s3.14 7 7 7 7-3.14 7-7-3.14-7-7-7zm1 3H6v5h2V4zm0 6H6v2h2v-2z"></path></svg>
  </div>

  <div class="github-info-container">
    <h4>
      <a href="https://github.com/joblib/joblib/issues/868#issuecomment-1034684910" target="_blank" rel="noopener nofollow ugc">How can I suppress output  from all child processes ??</a>
    </h4>

    <div class="github-info">
      <div class="date">
        opened <span class="discourse-local-date" data-format="ll" data-date="2019-04-08" data-time="05:56:54" data-timezone="UTC">05:56AM - 08 Apr 19 UTC</span>
      </div>

        <div class="date">
          closed <span class="discourse-local-date" data-format="ll" data-date="2022-02-10" data-time="09:25:00" data-timezone="UTC">09:25AM - 10 Feb 22 UTC</span>
        </div>

      <div class="user">
        <a href="https://github.com/eromoe" target="_blank" rel="noopener nofollow ugc">
          <img alt="eromoe" src="https://avatars.githubusercontent.com/u/3938751?v=4" class="onebox-avatar-inline" width="20" height="20">
          eromoe
        </a>
      </div>
    </div>

    <div class="labels">
    </div>
  </div>
</div>

  <div class="github-row">
    <p class="github-body-container">Hello, 

I am using facebook prophet to forecast future salecount. 
There is <span class="show-more-container"><a href="" rel="noopener" class="show-more">…</a></span><span class="excerpt hidden">a problem that pystan which prohpet depended would output optimizer messages while training .

I found a way to stop that annoying message from  https://github.com/facebook/prophet/issues/223#issuecomment-310497971  , but it only work if you run it in main process .

The output suppress decorator : 
```
class suppress_stdout_stderr(object):
    '''
    A context manager for doing a "deep suppression" of stdout and stderr in
    Python, i.e. will suppress all print, even if the print originates in a
    compiled C/Fortran sub-function.
       This will not suppress raised exceptions, since exceptions are printed
    to stderr just before a script exits, and after the context manager has
    exited (at least, I think that is why it lets exceptions through).

    '''
    def __init__(self):
        # Open a pair of null files
        self.null_fds = [os.open(os.devnull, os.O_RDWR) for x in range(2)]
        # Save the actual stdout (1) and stderr (2) file descriptors.
        self.save_fds = [os.dup(1), os.dup(2)]

    def __enter__(self):
        # Assign the null pointers to stdout and stderr.
        os.dup2(self.null_fds[0], 1)
        os.dup2(self.null_fds[1], 2)

    def __exit__(self, *_):
        # Re-assign the real stdout/stderr back to (1) and (2)
        os.dup2(self.save_fds[0], 1)
        os.dup2(self.save_fds[1], 2)
        # Close the null files
        for fd in self.null_fds + self.save_fds:
            os.close(fd)
```

func:

    def func():
       with suppress_stdout_stderr():
         p = Propet(*kwargs).fit(training_data)


If running in multiprocess like :

1. joblib

        Parallel(n_jobs=process_num)(
            delayed(func)(pid) for pid in pids
        )

2. multiprocessing

        with Pool(1) as p:
            r = p.map(func, pids)


The annoying log messages would still print.


I have no idea why the decorator didn't work in joblib and multiprocess . And there is no guide in joblib cover my case .</span></p>
  </div>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>