How to get the queue for xpu device

Robert_Cohn: <p>I have built pytorch from source with xpu support. I want to write a pytorch extension that uses xpu.  I need to get the queue. This worked for ipex:</p>
<pre><code class="lang-auto">    auto device_type = c10::DeviceType::XPU;
    c10::impl::VirtualGuardImpl impl(device_type);
    c10::Stream c10_stream = impl.getStream(c10::Device(device_type));
    auto&amp; q = torch::xpu::get_queue_from_stream(c10_stream);
</code></pre>
<p>But I get:</p>
<pre><code class="lang-auto">    /shared/tpi0/users/rscohn1/projects/cutlass/spex/examples/my-sycl/add_.cpp:12:27: error: no member named 'get_queue_from_stream' in namespace 'torch::xpu'
       12 |     auto&amp; q = torch::xpu::get_queue_from_stream(c10_stream);
          |               ~~~~~~~~~~~~^
</code></pre>
<p>I also tried this:</p>
<pre><code class="lang-auto">auto&amp; q = at::xpu::getCurrentSYCLQueue();
</code></pre>
<p>and</p>
<pre><code class="lang-auto"> c10::xpu::getCurrentXPUStream().queue();      
</code></pre>
<p>But none of them are recognized. How do you get the queue? Should extensions that worked with ipex work? Are there examples?</p>

Robert_Cohn: <p>From looking at pytorch xpu implementation, I think I want to do something like this:</p>
<pre><code class="lang-auto">torch::Tensor add_(torch::Tensor a, torch::Tensor b) {
    // Check if the input tensors are on the same device
    if (a.device() != b.device()) {
        throw std::invalid_argument("Input tensors must be on the same device");
    }
    auto q = torch::xpu::getCurrentXPUStream(a.device().index());
</code></pre>
<p>Which means that I have to explicitly include:</p>
<pre><code class="lang-auto">#include &lt;c10/xpu/XPUStream.h&gt;
</code></pre>
<p>But this file includes <code>xpu_cmake_macros.h</code></p>
<pre><code class="lang-auto">    In file included from /shared/tpi0/users/rscohn1/projects/cutlass/spex/.venv/lib/python3.10/site-packages/torch/include\
/c10/xpu/XPUStream.h:5:
    In file included from /shared/tpi0/users/rscohn1/projects/cutlass/spex/.venv/lib/python3.10/site-packages/torch/include\
/c10/xpu/XPUFunctions.h:4:
    In file included from /shared/tpi0/users/rscohn1/projects/cutlass/spex/.venv/lib/python3.10/site-packages/torch/include\
/c10/xpu/XPUDeviceProp.h:3:
    /shared/tpi0/users/rscohn1/projects/cutlass/spex/.venv/lib/python3.10/site-packages/torch/include/c10/xpu/XPUMacros.h:4\
:10: fatal error: 'c10/xpu/impl/xpu_cmake_macros.h' file not found
        4 | #include &lt;c10/xpu/impl/xpu_cmake_macros.h&gt;
</code></pre>
<p>and that is not in installed with pytorch:</p>
<pre><code class="lang-auto">rscohn1@gkdse-dnp-23:spex$ find .venv -name 'xpu_cmake*'
rscohn1@gkdse-dnp-23:spex$
</code></pre>

guangyey: <p>Hi <a class="mention" href="/u/robert_cohn">@Robert_Cohn</a> Sorry for causing you trouble. Let me clarify that <code>get_queue_from_stream</code> is an API before IPEX 2.1. From IPEX 2.3, please use <code>c10::xpu::getCurrentXPUStream().queue</code> to obtain the SYCL queue.<br>
BTW, <code>c10/xpu/impl/xpu_cmake_macros.h</code> seems a bug. I will check and fix it as soon as possible. I will keep you informed of any updates.<br>
Thanks very much.</p>

guangyey: <p>Fixed in <a href="https://github.com/pytorch/pytorch/pull/132847" class="inline-onebox" rel="noopener nofollow ugc">Add xpu_cmake_macros.h to xpu build by guangyey · Pull Request #132847 · pytorch/pytorch · GitHub</a>.</p>

Robert_Cohn: <p>It works now. Thanks!</p>

guangyey: <p>Glad I could help.<br>
Welcome to any feedback~</p>