How to print out logs with LOG(INFO) in Python in PyTorch

username: jianyuhuang
cooked: <p>How can we print out the GLOG info level log when running Python code in PyTorch?</p>
<p>For example,<br>
<aside class="onebox githubblob">
  <header class="source">
      <a href="https://github.com/pytorch/pytorch/blob/master/torch/lib/c10d/ProcessGroupNCCL.cpp#L236" target="_blank" rel="nofollow noopener">github.com</a>
  </header>
  <article class="onebox-body">
    <h4><a href="https://github.com/pytorch/pytorch/blob/master/torch/lib/c10d/ProcessGroupNCCL.cpp#L236" target="_blank" rel="nofollow noopener">pytorch/pytorch/blob/master/torch/lib/c10d/ProcessGroupNCCL.cpp#L236</a></h4>
<pre class="onebox"><code class="lang-cpp"><ol class="start lines" start="226" style="counter-reset: li-counter 225 ;">
<li>      // exception to users, after this, ncclCommWatchdog can detect nccl</li>
<li>      // communicators are aborted and clean up devNCCLCommMap_ accordingly.</li>
<li>      // if throwing timed out excepiton without aborting nccl communicators</li>
<li>      // here, it was observed that CUDA GPU will have 100% utilization and</li>
<li>      // can not run new events successfully.</li>
<li>      for (const auto&amp; ncclComm : ncclComms_) {</li>
<li>        ncclComm-&gt;ncclCommAbort();</li>
<li>        const auto&amp; storeKey = getNcclAbortedCommStoreKey(</li>
<li>            buildNcclUniqueIdStr(ncclComm-&gt;getNcclId()));</li>
<li>        store_-&gt;set(storeKey, {});</li>
<li class="selected">        LOG(INFO) &lt;&lt; "Wrote aborted communicator id to store: " &lt;&lt; storeKey;</li>
<li>      }</li>
<li>      throw std::runtime_error("Operation timed out!");</li>
<li>    }</li>
<li>    // Check for errors and throw appropriate exception.</li>
<li>    checkAndThrowException();</li>
<li>    std::this_thread::sleep_for(</li>
<li>        std::chrono::milliseconds(kSynchronizeBusyWaitMillis));</li>
<li>  }</li>
<li>  checkAndThrowException();</li>
<li>}</li>
</ol></code></pre>


  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>
</p>
<p>Checking <a href="https://github.com/pytorch/pytorch/blob/master/c10/util/Logging.cpp" rel="nofollow noopener">https://github.com/pytorch/pytorch/blob/master/c10/util/Logging.cpp</a></p>
<p>It appears that we can set “caffe2_log_level” in c10 log.</p>
<p>Basically,</p>
<pre><code class="lang-auto">from caffe2.python import workspace
workspace.GlobalInit(['caffe2', '--caffe2_log_level=2'])
</code></pre>
<p>However, this only works for Caffe2 code but not c10 code in PyTorch backend.</p>

username: Zhesi_Huang
cooked: <p>bad impl … logging in c10, but it relys on the <code>FLAGS_caffe2_log_level</code> ?  … <img src="https://discuss.pytorch.org/images/emoji/apple/upside_down_face.png?v=9" title=":upside_down_face:" class="emoji" alt=":upside_down_face:"></p>
<p>but i found that it can print the <code>LOG(INFO)</code> in python frontend when building pytorch with <code>USE_GLOG=1</code> (default <code>GOOGLE_STRIP_LOG</code> = 0)</p>
<p>for example</p>
<pre><code class="lang-auto">export CMAKE_PREFIX_PATH=${CONDA_PREFIX:-"$(dirname $(which conda))/../"}

MACOSX_DEPLOYMENT_TARGET=10.9 CC=clang CXX=clang++ MAX_JOBS=32 BUILD_CAFFE2=0 BUILD_CAFFE2_OPS=0 USE_GLOG=1 USE_DISTRIBUTED=1 USE_MKLDNN=0 USE_CUDA=0 USE_FBGEMM=0 USE_NNPACK=0 USE_QNNPACK=0 USE_XNNPACK=0 python setup.py develop
</code></pre>