akankshakshurai/cuda-error-resolution-analysis huggingface dataset:
What is the license of the PyTorch C++ API?

tepitepi: <p>Hi there.<br>
I have two questions about the PyTorch C++ API and its license.</p>
<p>Q1. What is the difference between “PyTorch C++ API” and “libtorch”?<br>
(I just want to know that they are the same or not?)<br>
<a href="https://pytorch.org/cppdocs/" class="onebox" target="_blank" rel="noopener nofollow ugc">https://pytorch.org/cppdocs/</a></p>
<p>Q2. Where is the LICENSE file for PyTorch C++API?<br>
I found the PyTorch’s license at GitHub. Is the same license applied to PyTorch C++API?</p><aside class="onebox githubblob" data-onebox-src="https://github.com/pytorch/pytorch/blob/master/LICENSE">
  <header class="source">

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

  <article class="onebox-body">
    <h4><a href="https://github.com/pytorch/pytorch/blob/master/LICENSE" target="_blank" rel="noopener nofollow ugc">pytorch/pytorch/blob/master/LICENSE</a></h4>


      <pre><code class="lang-">From PyTorch:

Copyright (c) 2016-     Facebook, Inc            (Adam Paszke)
Copyright (c) 2014-     Facebook, Inc            (Soumith Chintala)
Copyright (c) 2011-2014 Idiap Research Institute (Ronan Collobert)
Copyright (c) 2012-2014 Deepmind Technologies    (Koray Kavukcuoglu)
Copyright (c) 2011-2012 NEC Laboratories America (Koray Kavukcuoglu)
Copyright (c) 2011-2013 NYU                      (Clement Farabet)
Copyright (c) 2006-2010 NEC Laboratories America (Ronan Collobert, Leon Bottou, Iain Melvin, Jason Weston)
Copyright (c) 2006      Idiap Research Institute (Samy Bengio)
Copyright (c) 2001-2004 Idiap Research Institute (Ronan Collobert, Samy Bengio, Johnny Mariethoz)

From Caffe2:

Copyright (c) 2016-present, Facebook Inc. All rights reserved.

All contributions by Facebook:
Copyright (c) 2016 Facebook Inc.

All contributions by Google:
</code></pre>



  This file has been truncated. <a href="https://github.com/pytorch/pytorch/blob/master/LICENSE" target="_blank" rel="noopener nofollow ugc">show original</a>

  </article>

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

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


ptrblck: <ol>
<li>
<p>Yes, I think they are referring to the same API.</p>
</li>
<li>
<p>I would think a single license is used for the entirety of the repository, but adding <a class="mention" href="/u/jspisak">@jspisak</a> to correct me or confirm it.</p>
</li>
</ol>

tepitepi: <p><a class="mention" href="/u/ptrblck">@ptrblck</a><br>
Thank you for your very quick reply!<br>
My long-time question is solved.</p>
<p>Thanks again,</p>
Unexpected (for some) keys in load_state_dict

Hamzeh: <p>I try to load a saved model like this:</p>
<pre><code class="lang-auto">state = torch.load(args.ckpt)
model = GeneratorSeg(args.size, args.latent, args.n_mlp, image_mode=args.image_mode,
                         channel_multiplier=args.channel_multiplier, seg_dim=args.seg_dim
                         ).to(device)
new_state_dict = OrderedDict()
    for k, v in checkpoint['g_ema'].
        print(k) # I can see there are "convs.12.conv.weight", "convs.12.conv.blur.kernel" keys
        new_state_dict[k] = v

  model.load_state_dict(new_state_dict)
  model.eval()
</code></pre>
<p>But I get this error:<br>
<strong>Unexpected key(s) in state_dict: “convs.12.conv.weight”, “convs.12.conv.blur.kernel”,</strong><br>
It is a bit confusing, when I print the keys, I can see that there are such keys in checkpoints.<br>
Does anybody have any idea?</p>

ptrblck: <p>The key mismatch errors are raised e.g. if you’ve changed the model architecture or generally the attributes names of the parameters or buffers after saving the <code>state_dict</code>.<br>
In your case the <code>“convs.12.conv.weight”, “convs.12.conv.blur.kernel”</code> are unexpected in the <code>state_dict</code> and cannot be mapped to the current model. I.e. if you check the parameter names of the model you should not be able to find them using these keys:</p>
<pre><code class="lang-python">for name, param in model.named_parameters():
    print(name)
</code></pre>
<p>and they might have been moved to another attribute name etc.</p>
C++ Extension: How to return a custom object

eduardo4jesus: <p>How to return new object types?</p>
<p>In my case I am writing a cpp extension that has the following signature</p>
<pre><code class="lang-auto">std::vector&lt;my_struct_t&gt; func();
</code></pre>
<p>How do I tell <code>PYBIND11</code> how to convert <code>my_struct_t</code> to a python object?</p>
CNN ASR getting nan and core dump after epoch 1 with custom dataset

user12233: <p>I’m trying to implement the code from <a href="https://colab.research.google.com/drive/1IPpwx4rX32rqHKpLz7dc8sOKspUa-YKO?undefined" rel="noopener nofollow ugc">here</a> using a custom data set.  I’m able to get the code to run with the librispeech dataset but when I use my dataset I get the following:</p>
<p><strong>Train Epoch: 1 [0/2875 (0%)]    Loss: 10.740855</strong></p>
<p>Then the next value for the loss would be <strong>NAN</strong></p>
<p>Any help is appreciated!</p>
<p>I added gradient clipping here:</p>
<pre><code class="lang-auto">loss.backward()
torch.nn.utils.clip_grad_norm_(model.parameters(), 5)
optimizer.step()
</code></pre>
<p>Once I do that I get the following:</p>
<p><strong>Train Epoch: 1 [0/2875 (0%)]    Loss: 10.740855</strong><br>
<strong>Segmentation fault (core dumped)</strong></p>
<p>My dataset has clips from 3 to 14 seconds.</p>

Henry_Chibueze: <p>What loss function are you using? can you post the code block of how you pass arguments to it as well?</p>

user12233: <p>Thanks for the reply!  I’m using the CTC loss function:</p>
<p><code>criterion = nn.CTCLoss(blank=0).to(device)</code></p>
<p>Below is the block of code I use to train the model which is the arguments I pass to the loss function:</p>
<pre><code class="lang-auto">def train(model, device, train_loader, criterion, optimizer, scheduler, epoch, iter_meter, experiment):
    model.train()
    data_len = len(train_loader.dataset)
    with experiment.train():
        for batch_idx, _data in enumerate(train_loader):
            spectrograms, labels, input_lengths, label_lengths = _data
            spectrograms, labels = spectrograms.to(device), labels.to(device)
            optimizer.zero_grad()
            output = model(spectrograms)  # (batch, time, n_class)
            output = F.log_softmax(output, dim=2)
            output = output.transpose(0, 1) # (time, batch, n_class)
            loss = criterion(output, labels, input_lengths, label_lengths)
            loss.backward()

            torch.nn.utils.clip_grad_norm_(model.parameters(), 5)

            experiment.log_metric('loss', loss.item(), step=iter_meter.get())
            experiment.log_metric('learning_rate', scheduler.get_lr(), step=iter_meter.get())

            optimizer.step()
            scheduler.step()
            iter_meter.step()
            if batch_idx % 100 == 0 or batch_idx == data_len:
                print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
                    epoch, batch_idx * len(spectrograms), data_len,
                    100. * batch_idx / len(train_loader), loss.item()))
</code></pre>
Slow down training with PowerSGD during training

jinyuan.feng: <p><strong>Training Info:</strong><br>
GPU Device Type: A100<br>
Number of GPU: 8</p>
<p><strong>Code snippet:</strong><br>
process_group = dist.new_group(ranks=None, backend=“nccl”)<br>
DPP_model.register_comm_hook(<br>
PowerSGDState(process_group=process_group, matrix_approximation_rank=1),<br>
batched_powerSGD_hook,<br>
)<br>
Reference: <a href="https://pytorch.org/docs/stable/ddp_comm_hooks.html" class="inline-onebox" rel="noopener nofollow ugc">DDP Communication Hooks — PyTorch 1.11.0 documentation</a></p>

H-Huang: <p>Hi <a class="mention" href="/u/jinyuan.feng">@jinyuan.feng</a>, are you saying that there is a slow down in your training when you include PowerSGD? How are you detecting the slowdown and what is the alternative script you are using.</p>
Dynamic inputs for PyTorch TensorRT model?

iamexperimentingnow: <p>Hi, I have looked into PyTorch TensorRT document, I have a question in the below line the <code>inputs</code> variable takes <code>min_shape, opt_shape, max_shape</code> does it means that I can leverage this for my use-case where my model takes dynamic input tensors.</p>
<pre><code class="lang-auto">import torch_tensorrt
model = MyModel().eval() # torch module needs to be in eval (not training) mode

inputs = [torch_tensorrt.Input(
            min_shape=[1, 1, 16, 16],
            opt_shape=[1, 1, 32, 32],
            max_shape=[1, 1, 64, 64],
            dtype=torch.half,
        )]
</code></pre>
<p>I have built the model which takes dynamic inputs like the below example,</p>
<pre><code class="lang-auto">1. example1 = torch.rand(1, 3, 224, 224)
2. example2 = torch.rand(1, 3, 224, 550)
3. example3 = torch.rand(1, 3, 640, 480)
</code></pre>
<p>so, can I consider example1 as <code>min_shape</code> and example2 as <code>max_shape</code> ?</p>
<p>like</p>
<pre><code class="lang-auto">inputs = [torch_tensorrt.Input(
            min_shape=[1, 3, 224, 224],
            max_shape=[1, 3, 640, 480],
            dtype=torch.half,
        )]
</code></pre>
<p>could you please clarify this? <a class="mention" href="/u/ptrblck">@ptrblck</a></p>

ptrblck: <p>Yes, I think you are right as the description fits the <a href="https://pytorch.org/TensorRT/py_api/torch_tensorrt.html?highlight=input#torch_tensorrt.Input">docs</a>:</p>
<blockquote>
<p><strong>shape_mode</strong> (<em>torch_tensorrt.Input._ShapeMode</em>) – Is input statically or dynamically shaped<br>
<strong>shape</strong> (<em>Tuple</em> <em>or</em> <em>Dict</em>) –<br>
Either a single Tuple or a dict of tuples defining the input shape. Static shaped inputs will have a single tuple. Dynamic inputs will have a dict of the form …</p>
</blockquote>

iamexperimentingnow: <aside class="quote no-group" data-username="ptrblck" data-post="2" data-topic="154495">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p>dict of tuples defining the i</p>
</blockquote>
</aside>
<p><a class="mention" href="/u/ptrblck">@ptrblck</a> thanks for confirming <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>
Screwed up installation preventing reinstalls

Random12: <p>I started with pytorch version 1.9 but I had to update it for my project so I ran</p>
<p><code>conda install pytorch torchvision torchaudio cudatoolkit=10.2 -c pytorch</code></p>
<p>to get it to version 1.11. But then I found out my gpu is too new for this version of pytorch</p>
<p><code>with CUDA capability sm_86 is not compatible with the current PyTorch installation. The current PyTorch install supports CUDA capabilities sm_37 sm_50 sm_60 sm_70.</code></p>
<p>so I try</p>
<p><code>conda install pytorch torchvision torchaudio cudatoolkit=11.3 -c pytorch</code></p>
<p>to use cuda 11.3</p>
<p>and now it complains about conflicts and how it has to use flexible solver and its taking forever examining files one by one</p>
<pre><code class="lang-auto">Collecting package metadata (current_repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.
Solving environment: failed with repodata from current_repodata.json, will retry with next repodata source.
Collecting package metadata (repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.
Solving environment: \ 
Found conflicts! Looking for incompatible packages.
This can take several minutes.  Press CTRL-C to abort.
</code></pre>
<p>I tried uninstalling pytorch</p>
<pre><code class="lang-auto">`conda uninstall pytorch`
</code></pre>
<p>but its still tripping up and going ot the hours long flexible solver installation.</p>

ptrblck: <p>If the conflict resolution in your current environment takes too long (of if <code>conda</code> isn’t able to solve it) you could create a new virtual environment and install the PyTorch binaries there.</p>
How to use Python multiprocessing with cuda?

Nick_Lin: <p>Hello everyone, I’m trying to train a model which I split each layer into different GPU:</p>
<pre><code class="lang-auto">class ToyModel(nn.Module): // Consider one ToyModel as one layer in the original model
    def __init__(self, x_in, x_out, device):
        super().__init__()
        self.layer = nn.Linear(x_in, x_out).to(device)
        self.device = device
        
    def forward(self, x, y):
        x = self.layer(x.to(self.device))
        self.loss = nn.MSELoss(x, y.to(device))
        return x.detach()

def train(arg):
    model = arg[0]
    opt = arg[1]
    queue_in = arg[2]
    queue_out = arg[3]
    index = arg[4]
    print(f'{index} layer start training')
    for i in range(10):  // Assuming there are only 10 batches of data
        out = model(queue_in.get())
        queue_out.put(out)
        model.loss.backward()
        opt.step()
        opt.zero_grad()
    print(f'{index} layer finish training')
        
if __name__ == '__main__':
    
    mp.set_start_method('spawn')
    model = {}
    opt = {}
    queue = {}
    m = mp.Manager()
    for i in range(3):  // Assumming original model has three layers
        device = 'cuda:' + str(i)
        model[i] = ToyModel(100,100, device)    
        opt[i] = torch.optim.SGD(lr = 0.001)
        queue[i] = m.Queue()
    queue[4] = m.Queue()
    x = torch.randn(2000, 100)
    y = torch.randn(2000, 100)
    x = iter(x.split(200, dim = 0))
    y = iter(y.split(200, dim = 0))
    
    for splitX, splitY in zip(x,y):
        data = (splitX, splitY)
        queue[0].put(data)
    
    p = mp.Pool(3)
    arg = []
    for i in range(3):
        arg.append([model[i], opt[i], queue[i], queue[i+1], i])    

    p.map(train, arg)
</code></pre>
<p>However, I find it get stucked when the program try to train the second layer and third layer, could anyone tell me where might be wrong? Thanks.</p>
How to uncover a model

Nidheesh: <p>Hi team,<br>
I have a pytorch model (.pth file) with me. Could you please help me to identify various layers in it.</p>
<p>Thank you<br>
Nidheesh</p>

swap: <pre><code class="lang-auto">model = torch.load("model.pth", map_location='cpu')
layers = [module for module in model.modules()]
</code></pre>
Batch sorting in pytorch?

tanzila_rahman: <p>I need help to implement batch sorting in Pytorch. Suppose I have a tensor of size 6x492x5 where 6 is the batch size. I want to sort each batch element (i.e. 492x5 dimensional feature) based on last column (i.e. 5th column) of it. Can anyone please help me?</p>

bhushans23: <pre><code class="lang-auto">x = torch.randn((6, 492, 5))
sorted_x, _ = x.sort()
</code></pre>
<p>Reference: <a href="https://pytorch.org/docs/stable/torch.html#torch.sort" rel="nofollow noopener">https://pytorch.org/docs/stable/torch.html#torch.sort</a></p>

tanzila_rahman: <p>But sort function doesn’t do exactly that I want. <img src="https://discuss.pytorch.org/uploads/default/original/2X/2/26599e30cafd62e96b7c297720beaaf456007eae.png" alt="1112" data-base62-sha1="5tg9gkNE29sYg5VF3Xgj8kU9iWy" width="477" height="413"></p>
<p>I want to sort each batch element based on the last highlighted column. Moreover, the sorting should be descending order. And the output should be like this:</p>
<p>tensor([[[0.3400, 0.6520, 0.0520, 0.5200, 0.200],<br>
[0.0900, 0.0100, 0.1000, 0.2000, 0.1200],<br>
[1.0000, 0.5000, 0.3000, 0.0000, 0.1100]],<br>
.<br>
.<br>
.])</p>

nima_rafiee: <p>You need to consider that each of item from the second dim (492) also has 5 items. How you<br>
want them to be sorted?<br>
In general there is no direct way to do it. but you can store the column you want in a new array then<br>
use <code>torch.sort()</code> with which you will get the indices of the sorted items. with that you can use <code>torch.index_select()</code> and get a sorted tensor. but you need to be careful of the other dimensions  not<br>
just the batch and the one through which you want to sort.</p>

mova: <p>Bit late to the party, but oh well.<br>
With this you can select the feature you want to sort by.</p>
<pre><code class="lang-auto">def sort_by_feature(batch: torch.Tensor, ifeature: int):
    assert 0 &lt;= ifeature &lt;= batch.shape[-1]
    sorted_ftx_idxs = torch.argsort(batch[..., ifeature]).reshape(-1)
    batch_idxs = (
        torch.arange(batch.shape[0]).repeat_interleave(batch.shape[1]).reshape(-1)
    )
    batch_sorted = batch[batch_idxs, sorted_ftx_idxs, :].reshape(*batch.shape)
    return batch_sorted
</code></pre>
<p>Additionally you need to set <code>descending=True</code> in the <code>argsort</code>.</p>
Converting Tensor_float32 to Bitmap

2kan: <p>I can transform Bitmap to Tensor_float32 with method TensorImageUtils.bitmapToFloat32Tensor() but how can I get a bitmap from the Tensor_float32? Found that solution but it is only for grayscale images: <a href="https://itnext.io/converting-pytorch-float-tensor-to-android-rgba-bitmap-with-kotlin-ffd4602a16b6" rel="nofollow noopener">https://itnext.io/converting-pytorch-float-tensor-to-android-rgba-bitmap-with-kotlin-ffd4602a16b6</a></p>

sepiiii: <p>Hi, I have the same problem, did you find a solution?</p>

Silence-Monk: <p>// I faced the same problem, and I found the function itself</p>
<blockquote>
<p>TensorImageUtils.bitmapToFloat32Tensor()</p>
</blockquote>
<p>tortures the RGB colorspace. You should try to convert yuv to a bitmap and use</p>
<blockquote>
<p>TensorImageUtils.bitmapToFloat32Tensor</p>
</blockquote>
<p>instead for <strong>NOW</strong>.</p>
<p>// I modified the code from <strong>phillies</strong> (up) to get the coloful bitmap. Note that the format of an output tensor is typically NCHW.</p>
<p>// Here’s my function in Kotlin. Hopefully it works in your case:</p>
<pre><code class="lang-auto">private fun floatArrayToBitmap(floatArray: FloatArray, width: Int, height: Int) : Bitmap {

        // Create empty bitmap in ARGB format
        val bmp: Bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
        val pixels = IntArray(width * height * 4)

        // mapping smallest value to 0 and largest value to 255
        val maxValue = floatArray.max() ?: 1.0f
        val minValue = floatArray.min() ?: -1.0f
        val delta = maxValue-minValue

        // Define if float min..max will be mapped to 0..255 or 255..0
        val conversion = { v: Float -&gt; ((v-minValue)/delta*255.0f).roundToInt()}

        // copy each value from float array to RGB channels
        for (i in 0 until width * height) {
            val r = conversion(floatArray[i])
            val g = conversion(floatArray[i+width*height])
            val b = conversion(floatArray[i+2*width*height])
            pixels[i] = rgb(r, g, b) // you might need to import for rgb()
        }
        bmp.setPixels(pixels, 0, width, 0, 0, width, height)

        return bmp
    }
</code></pre>
<p>Hopefully future releases of PyTorch Mobile will fix this bug.</p>

codingHappily: <p>Could you provide the code of java?</p>

saidDotNet: <p>hello, I have a problem in the same context.<br>
I have an image shape [1,4,480,640], and I want to convert it to a tensor of the same shape.<br>
BUT when I used  TensorImageUtils.bitmaptoFloat() it gives me a shape of [1,3,480,640].<br>
any help please with java code.</p>
Fit Gaussian Mixture Model

the-dharma-bum: <p>Hello !</p>
<p>I’d like to train a very basic Mixture of 2 Gaussians to segment background in a 2d image.<br>
However I think I’m confused on how to use torch.distribution.</p>
<p>This is what I’m doing:</p>
<ul>
<li>first I prepare my 2d numpy array by doing:</li>
</ul>
<pre><code class="lang-python">x = torch.from_numpy(image.reshape((image.size, 1)))
</code></pre>
<ul>
<li>then I define a Module as bellow:</li>
</ul>
<pre><code class="lang-python">class GaussianMixtureModel(torch.nn.Module):

    def __init__(self, n_components: int=2):
        super().__init__()
        weights = torch.ones(n_components, )
        means   = torch.randn(n_components, )
        stdevs  = torch.tensor(np.abs(np.random.randn(n_components, )))
        self.weights = torch.nn.Parameter(weights)
        self.means   = torch.nn.Parameter(means)
        self.stdevs  = torch.nn.Parameter(stdevs)
    
    def forward(self, x):
        mix  = D.Categorical(self.weights)
        comp = D.Normal(self.means, self.stdevs)
        gmm  = D.MixtureSameFamily(mix, comp)
        return - gmm.log_prob(x).mean()
</code></pre>
<ul>
<li>then I define a basic training procedure:</li>
</ul>
<pre><code class="lang-python">model = GaussianMixtureModel(n_components=2)
optim = torch.optim.SGD(model.parameters(), lr=0.001, momentum=0.9)

n_iter = 1_000
for _ in range(n_iter):
    loss = model(x)
    loss.backward()
    optim.step()
    optim.zero_grad()
</code></pre>
<p>But I get either:</p>
<ul>
<li>The parameter probs has invalid values (error caught when defining mix in the forward method)</li>
<li>The parameters scale has invalid values (error caught when defining comp in the forward method</li>
</ul>
<p>So I guess I’m doing something wrong with my distributions definitions.<br>
Could you please help me ?</p>
<p>Thanks a lot !</p>

Tessa_van_der_Heiden: <p>Hi Luc,</p>
<p>The scale, so std, shouldn’t be negative nor very large.</p>
<p>One way to circumvent this is to use:</p>
<pre><code class="lang-auto">std_weight = 1e-4
comp = D.Normal(self.means, std_weight * self.stdevs.abs())
</code></pre>
<p>And for start: I would update the stdevs at all. In that way, you only train the means and weights, which in some applications works.</p>
<p>Best,<br>
Tessa</p>
Blur image directly after inference on GPU

Johnny_Cash: <p>Is there any way to run blurring on a torch tensor on a GPU? I am running a segmentation model and would like to blur the segmented areas of the original image directly on the gpu afterwards (not put it onto the CPU first to blur).</p>

Andrei_Cristea: <p>Does <code>torchvision.transforms.GaussianBlur</code> do what you need?</p>
<pre><code class="lang-auto">import torch, torchvision
device = torch.device("cuda")
tensor = torch.rand(3, 224, 224).to(device)
blurred_tensor = torchvision.transforms.GaussianBlur(3)(tensor)
print(blurred_tensor.device)

Output:
cuda:0
</code></pre>

Johnny_Cash: <p>Cool thanks you! Is the blurring operation also done on the gpu?</p>

Andrei_Cristea: <p>I believe so. Perhaps <a class="mention" href="/u/ptrblck">@ptrblck</a> can you confirm?</p>

ptrblck: <p>Yes, you are right and internally <a href="https://github.com/pytorch/vision/blob/12bb88738a5b6b96767c4165282f644495780a80/torchvision/transforms/functional_tensor.py#L767-L790">this method</a> will be used, which will apply a <code>conv2d</code> on the input tensor using the specified <code>img.device</code> arguments.<br>
If a <code>PIL.Image</code> is passed it’ll be transformed to a tensor before the blur transformation is applied and the result will then be transformed back to a <code>PIL.Image</code> as seen <a href="https://github.com/pytorch/vision/blob/12bb88738a5b6b96767c4165282f644495780a80/torchvision/transforms/functional.py#L1344-L1354">here</a> (so the CPU will be used in this case).</p>

Johnny_Cash: <p>Cool thank you very much!</p>
How to perform grid_sample with 5-D shapes of grid?

MariosOreo: <p>Hello,</p>
<p>Here is the case,</p>
<pre><code class="lang-auto">grid.shape = [num, batch, height, width, 2] 
feature.shape = [num, batch, c, height, width]
</code></pre>
<p>Now, I want to use <code>grid_sample</code> for each grid and its corresponding feature (in terms of dimension 0), but <code>grid_sample</code> only support 4-D grid as input. So how can I perform <code>grid_sample</code> in above case while avoiding to use loop?</p>
<p>Thanks in advance!</p>
Extend ImageFolder in pytorch to return a tensor of a different shape?

ana_lika: <p>How to extend torch.datasets.ImageFolder  in pytorch to return a tensor of a different shape?</p>
<p>It currently returns: torch.Size([1, 3, 256, 256]). I want to return [1, 10, 3, 256, 256].</p>
<p>I have a directory with multiple images separated into folders. Each folder has up to 3000 images. I would like to modify the getitem function so that it returns bags of images, where each bag contains 10 images.</p>
<p>Thank you!</p>

ptrblck: <p>Is this a double post from <a href="https://discuss.pytorch.org/t/how-can-i-modify-pytorch-dataset-getitem-function-to-return-a-bag-of-10-images/154489/2">here</a> or are these use cases somehow different?</p>
Segfault while running BERT

_chloe: <p>I’m using</p>
<ul>
<li>Ubuntu 18.04 with GeForce 1080Ti</li>
<li>Docker 20.10.17<br>
(docker image from ufoym/deepo)</li>
<li>Python 3.8.9</li>
<li>Tensorflow 2.8.0</li>
<li>Torch 1.12.0.dev20220327+cu113</li>
</ul>
<p>trying to run bert-base-uncased model. I cannot do the parallel running (bert has a bug so I have to downgrade to 1.4 for parallel running), so it was running on 1 GPU.<br>
It stoped after 1 epoch completes (after almost 20 hours of running), and found out it’s because of gnome segfault error.</p>
<pre><code class="lang-auto">Segfault at 0 ip 00007fd1c1c14461 sp 00007ffee88ea458 error 4 in libc-2.27.so[7fd1c1a86000+1e7000]
</code></pre>
<p>Is there any reason why or any way to fix?</p>

ptrblck: <p>Could you check the backtrace via <code>gdb</code> and post it here, please?</p>
Perciever pytorch shape issue

max1812: <p>RuntimeError: Given normalized_shape=[29], expected input with shape [*, 29], but got input of size[64, 672, 250]</p>
<p>code:-<br>
model_conv = Perceiver(<br>
input_channels = 3,<br>
input_axis = 2,<br>
num_freq_bands = 6,<br>
max_freq = 10.,<br>
depth = 6,<br>
num_latents = 256,<br>
latent_dim = 512,<br>
cross_heads = 1,<br>
latent_heads = 8,<br>
cross_dim_head = 64,<br>
latent_dim_head = 64,<br>
num_classes = 1000,<br>
attn_dropout = 0.,<br>
ff_dropout = 0.,<br>
weight_tie_layers = False,<br>
fourier_encode_data = True,<br>
self_per_cross_attn = 2<br>
)</p>
<p><span class="hashtag">#num_ftrs</span> = model_conv.fc.in_features<br>
model_conv.to_logits[2] =  nn.Linear(4096, 2)</p>
<p>model_conv = model_conv.to(device)</p>

ptrblck: <p>I guess the error is raised by an <a href="https://pytorch.org/docs/stable/generated/torch.nn.LayerNorm.html"><code>nn.LayerNorm</code></a> layer, which received an input in a wrong shape. Check where this norm layer is used and make sure the activation shape fits the expected shape.</p>
How can I Modify pytorch dataset __getitem__ function to return a bag of 10 images?

ana_lika: <p>I have a directory with multiple images separated into folders. Each folder has up to 3000 images. I would like to modify the pytorch dataset <strong>getitem</strong> function so that it returns bags of images, where each bag contains 10 images.</p>
<p>Here is what I have so far:</p>
<pre><code>transform = transforms.Compose([transforms.Resize(255),
                            transforms.CenterCrop(224),
                            transforms.ToTensor()
                           ])
dataset = datasets.ImageFolder('./../BCNB/patches/WSI_1', transform=transform)
data_loader = torch.utils.data.DataLoader(dataset, batch_size = 1)
</code></pre>
<p>My output of DataLoader should be a tensor with a shape of [1, 10, 3, 256, 256].<br>
Any input would be very helpful!</p>
<p>Thank you very much in advance!</p>

ptrblck: <p>I don’t know how these 10 samples should be loaded, but in case you want to pass multiple indices from the sampler to the <code>__getitem__</code> you could use a <code>BatchSampler</code> as described <a href="https://discuss.pytorch.org/t/dataloader-sample-by-slices-from-dataset/113005/4">here</a>.</p>
How to understand this MNIST dataloading code?

SouthTorch: <p>I successfully used the following code to download the training and testing data of MNIST dataset, but I don’t understand how two things work:</p>
<p>1: train_data and test_data all got data from datasets.MNIST, but train_data got 60K data, test_data got  another 10K data, which syntax differentiate the downloading of different data? Train=True/False is the button to switch that?</p>
<p>2: why test_data get 10K new data without saying download=True.</p>
<p>Here is the code:</p>
<pre><code class="lang-auto">train_data=torchvision.datasets.MNIST('../data', train=True, download=True,
                   transform=torchvision.transforms.Compose([
                       torchvision.transforms.ToTensor(),
                       torchvision.transforms.Normalize((0.1307,), (0.3081,))
                   ]))

test_data=torchvision.datasets.MNIST('../data', train=False, 
                   transform=torchvision.transforms.Compose([
                       torchvision.transforms.ToTensor(),
                       torchvision.transforms.Normalize((0.1307,), (0.3081,))
                   ]))
</code></pre>
<p>Thanks.</p>

ptrblck: <ol>
<li>
<p>Yes, the <code>train=True/False</code> argument determines if the <code>dataset</code> uses the training or testing dataset as seen <a href="https://github.com/pytorch/vision/blob/12bb88738a5b6b96767c4165282f644495780a80/torchvision/datasets/mnist.py#L121-L128">here</a>.</p>
</li>
<li>
<p>The complete data will be downloaded before the corresponding train/test binary will be loaded as seen <a href="https://github.com/pytorch/vision/blob/12bb88738a5b6b96767c4165282f644495780a80/torchvision/datasets/mnist.py#L98-L104">here</a>.</p>
</li>
</ol>

SouthTorch: <p>Thanks, ptrblck!</p>
<p>You really amazed me when pointing out those code lines.</p>
<p>Do you guys really read the code of an API when learning it? or Are you one of the code contributors?</p>
<p>Thanks.</p>

ptrblck: <p>Ha, thanks. I’ve seen the code a lot of times already to be familiar with it. <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=12" title=":wink:" class="emoji" alt=":wink:" loading="lazy" width="20" height="20"></p>
Compile libtorch c++ api from source

natan: <p>Hey, i need to compile libtorch with c++ api to QNX operating system on arm64. for that i need to get libtorch source code and to adjust it to my environment. is there anyway to get the source code of libtorch with c++ api. without clone and build the whole pytorch project?<br>
P.S. if its more easy i can manage with building only libtorch c++ support for jit.<br>
Thanks.</p>

WMF1997: <p>Hello,</p>
<p>Sorry, the libtorch you can download from <a href="http://pytorch.org" rel="nofollow noopener">pytorch.org</a> is based on x64(amd64) architecture. Other architecture’s libtorch ( armv7l on raspberry pi(raspian), aarch64(arm64) on raspberry pi(ubuntu) ) and pytorch must be compiled from source, or ask somebody else to get the compiled result.</p>
<p>I want to tell you my experience of building libtorch on Raspberry 4 4GB (Raspian, armv7l)</p>
<p>Since you just want the libtorch C++ api, and Python API is not in consideration, and QNX is a UNIX operating system. emm…</p>
<p>you QNX should have a python3.5+ (I tested cmake+make, but it does not work at all, so I changed the reply here)</p>
<p>clone the pytorch source code:</p>
<pre><code class="lang-bash">git clone --recursive https://github.com/pytorch/pytorch.git
</code></pre>
<p>and then</p>
<pre><code class="lang-bash">cd pytorch
python3 tools/build_libtorch.py
</code></pre>
<p>to get libtorch on aarch64(arm64) devices.</p>
<p>(in fact, as far as I observed from pytorch’s building guide, this building method(cmake+make) is not that recommended, but it works)</p>
<p>Compiling directly on arm device really needs a long time… so if possible, you can try cross-platform compiling tools. I do not know if there exists that tool and option for compiling libtorch.</p>
<p>That is what i have experienced and thought, hope it might help you a little.</p>
<p>yours sincerely,<br>
<a class="mention" href="/u/wmf1997">@wmf1997</a></p>

Alio: <p>Thank you. This looks like it might have built a working LibTorch package for my AGX Xavier.<br>
My problem now is figuring out how to use it. When I make a build folder and I type cmake -DCMAKE_PREFIX_PATH=/absolute/path/to/libtorch …, where is the Libtorch directory? Is this how I would build a project?</p>

Tumb1eweed: <p>It’s true to build libtorch on arm device would take 5-6 hours, probably…Would it be faster by using cross compile toolchain on x86-64 device? And Why?</p>
LibTorch: "version `GLIBC_2.34' not found (required by /usr/lib/libc10.so)"

JonathanHampton: <p>I am attempting to use libtorch on a aarch64 embedded device. I built from source in a aarch64 Docker container on my host (in attempts to speed up build time). I moved the generated .so files to the target device however when attempting to use them I get: “version `GLIBC_2.34’ not found (required by /usr/lib/libc10.so)”. The target device is glibc 2.32 and so is the Docker container I used to build libtorch.</p>
<p>Am I missing something here? should I build an older version?</p>

tom: <aside class="quote no-group" data-username="JonathanHampton" data-post="1" data-topic="154548">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/jonathanhampton/48/25956_2.png" class="avatar"> JonathanHampton:</div>
<blockquote>
<p>“version `GLIBC_2.34’ not found (required by /usr/lib/libc10.so)”. The target device is glibc 2.32 and so is the Docker container I used to build libtorch.</p>
</blockquote>
</aside>
<p>I think it is pretty safe to say that something picked up a newer glibc during the build.</p>
<p>Best regards</p>
<p>Thomas</p>
How to quantize a Swin transformer model?

MST007: <p>Hello,</p>
<p>I’m a beginner and I have a project where I need to quantize models using pytorch. I managed to find a way to quantize a resnet model but it seems like it doesn’t work on a Swin Tranformer model since this model has a different architecture. Has anyone any ideas on how to quantize a Swin transformer model?</p>
Saving meta data in torch file?

pumplerod: <p>I’m wondering if there is a way to save some small amount of arbitrary meta data header to a torch file when using <code>torch.save( file_path)</code>.  Something that would allow me to query the file before loading, for example by running <code>% head torchfile.pt</code> from the command line which might allow me to embed a version for the class I’ve constructed.</p>