Torch.linalg.lstsq takes way too long

cperezln: <p>Hi, I am currently working with some least square problems on feedforward neural network. Long story short, I am trying to approximate some of the results obtained by the network with a linear resolution, using lstsq. The problem is the following: whereas my colleague achieve pretty fast results using lstsq (locally, not in a computing cluster), of the order of 0.5s to solve the problem, mine is taking a pretty long time (approx 30s, where the neural network is being trained with 1000 epoch in half that time). My question is, can this be somehow related with the PyTorch installation? I have heard that maybe some of the linear algebra optimized libraries are not correctly linked with my current PyTorch installation, but I really don’t know how to check/solve it.</p>
<p>Have had someone else problems with these methods taking way too long?</p>
<p>If any source code/installation configuration is needed, please let me know.</p>
<p>Thanks!</p>

Soumya_Kundu: <p>Code you potentially share your code and your exact install env?</p>

KFrank: <p>Hi Cristian!</p>
<aside class="quote no-group quote-modified" data-username="cperezln" data-post="1" data-topic="221799" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/cperezln/48/77777_2.png" class="avatar"> cperezln:</div>
<blockquote>
<p>using lstsq.<br>
…<br>
Have had someone else problems with these methods taking way too long?</p>
</blockquote>
</aside>
<p>This is a long shot, but this would be expected if you’re using intel ‘xpu’ instead of nvidia’<br>
‘cuda’ for your work.</p>
<p>Pytorch with xpu accelerates the bulk of the core tensor operations, but many, if not all<br>
<code>linalg</code> operations aren’t supported on the xpu and fall back to the cpu:</p>
<pre><code class="lang-plaintext">&gt;&gt;&gt; import torch
&gt;&gt;&gt; torch.__version__
'2.7.1+xpu'
&gt;&gt;&gt; a = torch.randn (4, 4, device = 'xpu')
&gt;&gt;&gt; b = torch.randn (4, 4, device = 'xpu')
&gt;&gt;&gt; torch.linalg.lstsq (a, b)
&lt;python-input-4&gt;:1: UserWarning: Aten Op fallback from XPU to CPU happends. This may have performance implications. If need debug the fallback ops please set environment variable `PYTORCH_DEBUG_XPU_FALLBACK=1`  (Triggered internally at /pytorch/build/xpu/ATen/RegisterXPU_0.cpp:53693.)
torch.return_types.linalg_lstsq(
solution=tensor([[ 0.9394, -1.1806,  0.2140, -0.0070],
        [-0.4890, -0.1218, -0.0116, -0.4881],
        [-2.8667, -0.9415, -0.6335, -2.3435],
        [-0.3946, -0.4613,  0.5303, -0.5121]], device='xpu:0'),
residuals=tensor([], device='xpu:0'),
rank=tensor(4, device='xpu:0'),
singular_values=tensor([], device='xpu:0'))
</code></pre>
<p>Best.</p>
<p>K. Frank</p>

cperezln: <p>Hi Frank,<br>
my current torch version is 2.6.0+cu124, which is cuda-based.</p>
<p>I think the problem may be a problem related with the BLAS library… Have you ever encountered something like that?</p>

KFrank: <p>Hi Cristian!</p>
<aside class="quote no-group" data-username="cperezln" data-post="4" data-topic="221799" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/cperezln/48/77777_2.png" class="avatar"> cperezln:</div>
<blockquote>
<p>Have you ever encountered something like that?</p>
</blockquote>
</aside>
<p>No I haven’t.  When cuda works for me on, say, basic tensor operations, I haven’t had any<br>
trouble with <code>torch.linalg</code> (but I don’t have experience with a variety of cuda hardware).</p>
<p>Best.</p>
<p>K. Frank</p>