akankshakshurai/cuda-error-resolution-analysis huggingface dataset:
RuntimeError: size mismatch (got input: [3], target: [32])

username: ayesha_proma
cooked: <p>import torch<br>
import torch.nn as nn<br>
import torch.nn.functional as F</p>
<p>class BiLSTMWithAttention(nn.Module):<br>
def <strong>init</strong>(self, embedding_dim=768, hidden_dim=128, num_classes=3):<br>
super(BiLSTMWithAttention, self).<strong>init</strong>()<br>
self.bilstm = nn.LSTM(input_size=embedding_dim, hidden_size=hidden_dim, num_layers=1, batch_first=True, bidirectional=True)<br>
self.attn = nn.Linear(2 * hidden_dim, 1)<br>
self.dropout = nn.Dropout(0.3)<br>
self.fc1 = nn.Linear(32, 128)<br>
self.fc2 = nn.Linear(128, num_classes)</p>
<pre><code>def forward(self, x):
    # x shape: (batch_size, seq_len, embedding_dim)
    lstm_out, _ = self.bilstm(x)  # lstm_out: (batch, seq_len, 2*hidden_dim)

    attn_scores = self.attn(lstm_out)  # (batch, seq_len, 1)
    attn_weights = torch.softmax(attn_scores, dim=1)  # (batch, seq_len, 1)

    context = torch.sum(attn_weights * lstm_out, dim=1)  # (batch, 2*hidden_dim)

    x = self.dropout(F.relu(self.fc1(context)))  # (batch, 128)
    return self.fc2(x)  # (batch, num_classes)
</code></pre>

username: ajayrkumar
cooked: <p>There is a bug in your <code>BiLSTMWithAttention</code> class implementation.</p>
<p>The input to <code>fc1</code> will be the <code>context</code> tensor, which has the shape of <code>[batch, 2 * hidden_dim]</code>. Hence the <code>in_features</code> to <code>fc1</code> will be <code>2 * hidden_dim</code> and not simply <code>32</code>. No other changes are necessary.</p>
<p>TLDR:<br>
<code>fc1</code> should be initialized this way in the <code>__init__</code> method: <code>self.fc1 = nn.Linear(2 * hidden_dim, 128)</code></p>
<p>Hope this helps.</p>
<p><em>Kindly enclose your code snippets in code blocks for clarity and ease of reading.</em></p>
Torch.nn.modules vs torch.nn.module

username: SunshineLightHouse
cooked: <p>Is there an error in the Pytorch documentation: <a href="https://docs.pytorch.org/docs/stable/generated/torch.nn.Module.html#torch.nn.Module.modules" class="inline-onebox" rel="noopener nofollow ugc">Module — PyTorch 2.7 documentation</a></p>
<p>because when you do actually implement torch.nn it uses torch.nn.module so in Pytorch documentation why does it show torch.nn.modules?</p>
<p>Is it an error in the documentation ?</p>

username: aaraya
cooked: <p>Hi <a class="mention" href="/u/sunshinelighthouse">@SunshineLightHouse</a>, there is a post that talks about this, please read: <a href="https://discuss.pytorch.org/t/difference-between-nn-module-and-module-from-nn-modules-module/46631/4">https://discuss.pytorch.org/t/difference-between-nn-module-and-module-from-nn-modules-module/46631/4</a></p>
<hr>
<p>Adrian Araya<br>
Machine Learning Engineer at <a href="http://RidgeRun.ai" rel="noopener nofollow ugc">RidgeRun.ai</a><br>
Contact us: support@ridgerun.ai</p>
Torch.split with multiple start and end indices

username: Nielsen
cooked: <p>Given a tensor [0,1,2,3,4,5,6,7,8,9], and a slices tensor of shape [N_SLICES, 2], e.g. [[0,2], [4,7]]. I would like to gather all the respective slices without using for loop (for every [start,end] pair and appending the slices). Is there a method that achieves this?</p>
<p>In the example above, the result should be [0,1,4,5,6]. I believe torch.split only splits into chunks given that the resulting split tensor has the same shape as the original tensor. In my case, the resulting split tensor have less elements than the original tensor.</p>

username: KFrank
cooked: <p>Hi Nielsen!</p>
<aside class="quote no-group" data-username="Nielsen" data-post="1" data-topic="220337" 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/nielsen/48/77172_2.png" class="avatar"> Nielsen:</div>
<blockquote>
<p>Is there a method that achieves this?</p>
<p>In the example above, the result should be [0,1,4,5,6].</p>
</blockquote>
</aside>
<p>Yes, consider:</p>
<pre><code class="lang-plaintext">&gt;&gt;&gt; import torch
&gt;&gt;&gt; torch.__version__
'2.7.0+cu128'
&gt;&gt;&gt; t = torch.tensor ([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
&gt;&gt;&gt; s = torch.tensor ([[0, 2], [4, 7]])
&gt;&gt;&gt; ind = torch.arange (len (t)).expand (s.size (0), -1)
&gt;&gt;&gt; mask = torch.logical_and (ind &gt;= s[:, :1], ind &lt; s[:, 1:]).any (0)
&gt;&gt;&gt; t[mask]
tensor([0, 1, 4, 5, 6])
</code></pre>
<p>Best.</p>
<p>K. Frank</p>
Reducing PyTorch C++ API Library Size for Inference-Only Use

username: fgauthier
cooked: <p>I’ve previously trained a model using the Python API and successfully exported it using <code>torch.jit.trace</code>. Now, I’m deploying this model in C++ and only need the inference functionality.</p>
<p>Here’s a simplified version of the code I’m using:</p>
<pre><code class="lang-auto">torch::Tensor input_tensor = preprocess(input.image());
std::vector&lt;torch::jit::IValue&gt; inputs{input_tensor};
_model.eval();
torch::Tensor output_tensor = _model.forward(inputs).toTensor().to(torch::kCPU);
int classification_score = output_tensor[0].item&lt;int&gt;();
cv::Mat segmentation = interpretSegmentation(output_tensor, input.image());
</code></pre>
<p>In my current <code>CMakeLists.txt</code>, I’m including all the Torch libraries, which results in a build size of nearly 2.5 GB. I’m looking for a way to reduce this, similar to how OpenCV allows selective inclusion of modules, e.g.:</p>
<pre><code class="lang-auto"> FIND_PACKAGE( OpenCV REQUIRED core imgproc)
</code></pre>
<p>Is there a way to include only the minimal necessary components of LibTorch for inference ?</p>
<p>Thanks in advance!</p>

username: zhxchen17
cooked: <p>Hi <a class="mention" href="/u/fgauthier">@fgauthier</a> thanks for bringing up this question. Would you like to elaborate a bit more on why binary size is a problem in your inference use case?</p>