Query CUDA version at runtime

dfalbel: <p>I wonder if there’s a way to query the CUDA version LibTorch has been compiled with at runtime.<br>
I found a few references like:</p>
<aside class="onebox githubblob" data-onebox-src="https://github.com/pytorch/pytorch/blob/e795315c638228d4170f3797356c09a70b2ed4cd/torch/csrc/cuda/Module.cpp#L183-L190">
  <header class="source">

      <a href="https://github.com/pytorch/pytorch/blob/e795315c638228d4170f3797356c09a70b2ed4cd/torch/csrc/cuda/Module.cpp#L183-L190" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>

  <article class="onebox-body">
    <h4><a href="https://github.com/pytorch/pytorch/blob/e795315c638228d4170f3797356c09a70b2ed4cd/torch/csrc/cuda/Module.cpp#L183-L190" target="_blank" rel="noopener nofollow ugc">pytorch/pytorch/blob/e795315c638228d4170f3797356c09a70b2ed4cd/torch/csrc/cuda/Module.cpp#L183-L190</a></h4>



  <pre class="onebox">    <code class="lang-cpp">
      <ol class="start lines" start="183" style="counter-reset: li-counter 182 ;">
          <li>PyObject * THCPModule_getCompiledVersion(PyObject *self, PyObject *noargs)</li>
          <li>{</li>
          <li>#if defined(USE_ROCM)</li>
          <li>  return THPUtils_packInt64((int64_t) ROCM_VERSION);</li>
          <li>#else</li>
          <li>  return THPUtils_packInt64((int64_t) CUDA_VERSION);</li>
          <li>#endif</li>
          <li>}</li>
      </ol>
    </code>
  </pre>


  </article>

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

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

<p>and</p>
<aside class="onebox githubblob" data-onebox-src="https://github.com/pytorch/pytorch/blob/e795315c638228d4170f3797356c09a70b2ed4cd/caffe2/core/common_gpu.h#L98">
  <header class="source">

      <a href="https://github.com/pytorch/pytorch/blob/e795315c638228d4170f3797356c09a70b2ed4cd/caffe2/core/common_gpu.h#L98" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>

  <article class="onebox-body">
    <h4><a href="https://github.com/pytorch/pytorch/blob/e795315c638228d4170f3797356c09a70b2ed4cd/caffe2/core/common_gpu.h#L98" target="_blank" rel="noopener nofollow ugc">pytorch/pytorch/blob/e795315c638228d4170f3797356c09a70b2ed4cd/caffe2/core/common_gpu.h#L98</a></h4>



  <pre class="onebox">    <code class="lang-h">
      <ol class="start lines" start="88" style="counter-reset: li-counter 87 ;">
          <li>
          </li>
<li>#if defined(CUDA_VERSION) &amp;&amp; CUDA_VERSION &gt;= 10000</li>
          <li>#define CAFFE2_CUDA_PTRATTR_MEMTYPE type</li>
          <li>#else</li>
          <li>#define CAFFE2_CUDA_PTRATTR_MEMTYPE memoryType</li>
          <li>#endif</li>
          <li>
          </li>
<li>/**</li>
          <li> * A runtime function to report the cuda version that Caffe2 is built with.</li>
          <li> */</li>
          <li class="selected">inline int CudaVersion() {</li>
          <li>#if defined(USE_ROCM)</li>
          <li>  return ROCM_VERSION;</li>
          <li>#else</li>
          <li>  return CUDA_VERSION;</li>
          <li>#endif</li>
          <li>}</li>
          <li>
          </li>
<li>/**</li>
          <li> * Returns the number of devices.</li>
          <li> */</li>
      </ol>
    </code>
  </pre>


  </article>

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

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

<p>But the first returns a <code>PyObject*</code> that’s not practical to work with and the second is a header file, so I expect it to be filled with whatever <code>CUDA_VERSION</code> is defined when I am compiling my program.</p>

tom: <p>I think there might not be. The version.h would be the most natural place (as in closest match to version.py, the canonical source of information from Python) and it does not have the information.</p>
<p>To my mind, it would be useful to get<br>
<code>tools/setup_helpers/gen_version_header.py</code><br>
to generate the same things as<br>
<code>tools/generate_torch_version.py</code>.</p>
<p>Maybe you could open an issue to discuss this.</p>
<p>Best regards</p>
<p>Thomas</p>

dfalbel: <p>Thanks very much for your answer <a class="mention" href="/u/tom">@tom</a> !<br>
Just opened a ‘Feature Request’ issue in the GitHub repository.<br>
Here’s the link for future reference:</p>
<aside class="onebox githubissue" data-onebox-src="https://github.com/pytorch/pytorch/issues/68272">
  <header class="source">

      <a href="https://github.com/pytorch/pytorch/issues/68272" target="_blank" rel="noopener nofollow ugc">github.com/pytorch/pytorch</a>
  </header>

  <article class="onebox-body">
    <div class="github-row">
  <div class="github-icon-container" title="Issue">
	  <svg width="60" height="60" class="github-icon" viewbox="0 0 14 16" aria-hidden="true"><path 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/pytorch/pytorch/issues/68272" target="_blank" rel="noopener nofollow ugc">Query CUDA version LibTorch has been compiled with</a>
    </h4>

    <div class="github-info">
      <div class="date">
        opened <span class="discourse-local-date" data-format="ll" data-date="2021-11-12" data-time="19:58:32" data-timezone="UTC">07:58PM - 12 Nov 21 UTC</span>
      </div>


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

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

  <div class="github-row">
    <p class="github-body-container">## 🚀 Feature

Allow runtime querying the CUDA version LibTorch has been compil<span class="show-more-container"><a href="" rel="noopener" class="show-more">…</a></span><span class="excerpt hidden">ed with.

## Motivation

We might want to use different code paths depending on the CUDA version LibTorch has been compiled with. Depending on the implementation, this can only be found at runtime.

## Pitch

We want to be able to identify the CUDA version LibTorch has been compiled with. Ideally there would be a function defined in the torch namespace that would allow one to query this information.

## Alternatives

An alternative, suggested by @t-vi in this [post](https://discuss.pytorch.org/t/query-cuda-version-at-runtime/136715/2?u=dfalbel) in the forum would be to have a macros defined in [version.h](https://github.com/pytorch/pytorch/blob/master/torch/csrc/api/include/torch/version.h.in) similar to what already happens with the Python package.

As a workaround It seems possible to identify the version by parsing the `build-version` file inside the pre-built binary package - but that doesn't seem reliable for most use cases.

## Additional context

- https://discuss.pytorch.org/t/query-cuda-version-at-runtime/136715
- https://github.com/mlverse/lltm/issues/2</span></p>
  </div>

  </article>

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

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