akankshakshurai/cuda-error-resolution-analysis huggingface dataset:
How to split filters into individual RGB channels

johndoe123: <p>I have a filter output from a single layer in the network, how do I then split the output into individual RGB channels to view the filter in each channel separately. I currently have this, but it just looks like the image is coloured:</p>
<pre><code class="lang-auto">    weights = weights - weights.min()
    weights = weights / weights.max()
    display = torchvision.utils.make_grid(weights, nrow = 8)
    display = display.permute(1, 2, 0)
    plt.imshow(display[:,:,0], cmap='Reds')
    plt.show()
    plt.imshow(display[:,:,0], cmap='Greens')
    plt.show()
    plt.imshow(display[:,:,0], cmap='Blues')
</code></pre>
<p>which gives images:<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/2/f/2fb0c19d0f3df55e60ace42b8f9e392610f8d76a.png" alt="image" data-base62-sha1="6NT8FaJOeJsldT5CNaHJ1MYNyki" width="257" height="251"><br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/b/3/b394df0d8544cc412b479bc23db23bd367b75d9f.png" alt="image" data-base62-sha1="pCEqc5weI9wk8jp8FIyBO9zb1AH" width="257" height="251"><br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/4/c/4c42a56f16c8f7cbbcde9ce32337a1fa69cc9dd1.png" alt="image" data-base62-sha1="aSD4KAo5fSRqyLIXF97DYCuULhn" width="257" height="251"></p>

ptrblck: <p>The output uses the specified colormaps and you could change it to e.g. grayscale, if you don’t want to use them in their current setup.<br>
I don’t know, how <code>weights</code> were created, but assuming it’s a tensor containing the filters, where each has 3 input channels, I would split this tensor into the R, G, and B parts, and visualize them separately.</p>
How to reuse model variables?

ysleer: <p>I want to duplicate multiple models and train each with a different dataloader.</p>
<p>Simply cloning the model is expected to waste that much memory.</p>
<p>So I’m looking for a way to use memory efficiently.</p>
<p>I know that TensorFlow has an option called reuse like variable_scope(reuse=True).</p>
<p>Is there any way I can use it similarly in Pytorch??</p>
<p>Thank you.</p>

ptrblck: <p>I’m not sure how reusing parameters in different training runs would work.<br>
In the end, each training loop would update the model parameters, and they would diverge, so I don’t think it’s possible.</p>

ysleer: <p>Thank you for answer.</p>
<p>Then I will change the question a little bit.</p>
<p>Is there a function in Pytorch that functions similar to tensorflow variable_scope reuse option?</p>

ptrblck: <p>If I understand the <a href="https://www.tensorflow.org/api_docs/python/tf/compat/v1/variable_scope">docs</a> correctly, the <code>variable_scope</code> would create new variables using the specified names (scopes).<br>
If you set <code>reuse=True</code>, TF would check, if a variable with this name was already created and return it instead of creating a new one (or raising an error?).</p>
<pre><code class="lang-python">with tf.compat.v1.variable_scope("foo"):
    v = tf.compat.v1.get_variable("v", [1])
with tf.compat.v1.variable_scope("foo", reuse=True):
    v1 = tf.compat.v1.get_variable("v", [1])
assert v1 == v
</code></pre>
<p>In PyTorch you don’t define tensor creations in a graph with scopes etc. and can directly create the tensor and reuse it:</p>
<pre><code class="lang-python">x = torch.randn(1)
y = x + 1
# reuse x
z = x + 2
</code></pre>

ysleer: <p>Thank you for answer!</p>

Eta_C: <p>By the way, be care about inplace operator such as <code>nn.ReLU(replace=True)</code>, <code>div_</code>, <code>mul_</code> … <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"></p>

ysleer: <p>I’m sorry, but I’ll ask one more question.</p>
<p>In the example above, is the memory of reused x shared?</p>

ptrblck: <p>Yes, <code>x</code> is the same tensor, so the memory and all its attributes will be used.<br>
The outputs <code>y</code> and <code>z</code> are of course different tensors.</p>
Inference, Dropout ,Saving&loading models, torchvision.transforms&data augmentation, Transfer learning

Dimitrios_Tosidis: <div class="youtube-onebox lazy-video-container" data-video-id="lvJ7N7r5FfY" data-video-title="Deep Learning with Pytorch" data-provider-name="youtube">
  <a href="https://www.youtube.com/watch?v=lvJ7N7r5FfY" target="_blank" rel="noopener nofollow ugc">
    <img class="youtube-thumbnail" src="https://img.youtube.com/vi/lvJ7N7r5FfY/maxresdefault.jpg" title="Deep Learning with Pytorch" width="690" height="388">
  </a>
</div>
Gpu waits for cpu so long time

Alva-2020: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/6/3/63d46a1d5b172172d2bcb6fdfa995f4f1807e401.png" data-download-href="https://discuss.pytorch.org/uploads/default/63d46a1d5b172172d2bcb6fdfa995f4f1807e401" title="1615125703(1)"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/6/3/63d46a1d5b172172d2bcb6fdfa995f4f1807e401_2_690x239.png" alt="1615125703(1)" data-base62-sha1="ef8mFebaTvqABlOW6kCuqePsdYR" width="690" height="239" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/6/3/63d46a1d5b172172d2bcb6fdfa995f4f1807e401_2_690x239.png, https://discuss.pytorch.org/uploads/default/optimized/3X/6/3/63d46a1d5b172172d2bcb6fdfa995f4f1807e401_2_1035x358.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/6/3/63d46a1d5b172172d2bcb6fdfa995f4f1807e401_2_1380x478.png 2x" data-dominant-color="192723"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">1615125703(1)</span><span class="informations">1729×600 64.6 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div> <div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/5/4/54d3d60dd47d7aafab5a7b3e80e0ef4b989bfcc0.png" data-download-href="https://discuss.pytorch.org/uploads/default/54d3d60dd47d7aafab5a7b3e80e0ef4b989bfcc0" title="1615125816(1)"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/5/4/54d3d60dd47d7aafab5a7b3e80e0ef4b989bfcc0_2_690x293.png" alt="1615125816(1)" data-base62-sha1="c6pXN32dVZs9200mQ5Wlwq8NTYA" width="690" height="293" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/5/4/54d3d60dd47d7aafab5a7b3e80e0ef4b989bfcc0_2_690x293.png, https://discuss.pytorch.org/uploads/default/optimized/3X/5/4/54d3d60dd47d7aafab5a7b3e80e0ef4b989bfcc0_2_1035x439.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/5/4/54d3d60dd47d7aafab5a7b3e80e0ef4b989bfcc0_2_1380x586.png 2x" data-dominant-color="1B2825"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">1615125816(1)</span><span class="informations">1701×724 82.5 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div><br>
The first several batchsize training speech is ok, the utilization rate of cpu is ok, after several batchsize, cpu becomes slower, gpu waits for cpu  so long time? Please give me some suggestions. Thanks very much.</p>

ptrblck: <p>Could you explain what “cpu becomes slower” means? Do you see a decrease in its frequency or any other performance limiting issue? If so, could you check, if your system might be overheating?</p>
Https://pytorch.org/tutorials/intermediate/torchvision_tutorial.html

Punisher_Singh: <p><a href="https://pytorch.org/tutorials/intermediate/torchvision_tutorial.html" class="onebox" target="_blank" rel="noopener nofollow ugc">https://pytorch.org/tutorials/intermediate/torchvision_tutorial.html</a></p>
<p>I am using PyTorch for object detection and refining an existing model (transfer learning) as described in the following link - <a href="https://pytorch.org/tutorials/intermediate/torchvision_tutorial.html" class="inline-onebox" rel="noopener nofollow ugc">TorchVision Object Detection Finetuning Tutorial — PyTorch Tutorials 1.8.0 documentation</a></p>
<p>While different transformations are used for image augmentation (horizontal flip), the tutorial doesn’t mention anything on transforming the bounding box/annotation to ensure they are in line with the transformed image. Am I missing something basic here?</p>

ptrblck: <p>The used transformations in <a href="https://pytorch.org/tutorials/intermediate/torchvision_tutorial.html#putting-everything-together">this section</a> are used from <a href="https://github.com/pytorch/vision/blob/c991db82abba12e664eeac14c9b643d0f1f1a7df/references/detection/transforms.py"><code>references/detection/transforms.py</code></a> as mentioned in the tutorial, and will apply the transformations to the <code>image</code> and <code>target</code>.</p>
Using dataparallel module to train GNMT

zzsunshine: <p>Hi all,</p>
<p>I found there is a way to use DistributedDataparallel (DDP) to train GNMT model. I want to ask whether we could use dataparallel (<a href="https://pytorch.org/tutorials/beginner/blitz/data_parallel_tutorial.html" class="inline-onebox" rel="noopener nofollow ugc">Optional: Data Parallelism — PyTorch Tutorials 1.8.0 documentation</a>) module to train GNMT model? What is the difference when training GNMT.</p>
<p>Appreciate any help</p>
How to access the element within the cudaTensor

zzsunshine: <p>Hi,<br>
There is a const Tensor&amp; tensor in a .cpp file, and the tensor is of cudaTensor type. I want to access the elements within the tensor and make a change. I create .cu file to define a kernel to change the elements within the tensor, but the problem here is that<br>
I am not sure if it is a correct way to pass tensor.data_ptr() as the parameter to cuda kernel and use *tensor.data_ptr() to access the element in the tensor in the cuda kernel. If not, is there other way to access the data?</p>
<p>What I tried is as following:<br>
in .cpp<br>
tensor_change((float*)tensor.data_ptr(), number);<br>
in .cu<br>
<strong>global</strong>  void tc(float* po, int number) {<br>
printf("%d\n", number);<br>
printf(“pval = %p\n”, po);<br>
printf(“val = %f\n”, *po);<br>
printf(“val_next = %f\n”, *(po+1)); }</p>
<p>void tensor_change(float* po, int number){<br>
tc&lt;&lt;&lt;1, 1&gt;&gt;&gt;(po, number); }</p>
<p>The result is:<br>
2856768<br>
pval = 0x7faf27200000<br>
val = 0.000000<br>
val_next = 0.000000</p>
<p>Any help will be appreciated!<br>
Thanks</p>

Lin_Jia: <p>Have you tried those 2 ways:</p>
<ol>
<li>using index API, <a href="https://pytorch.org/cppdocs/notes/tensor_indexing.html" rel="nofollow noopener">https://pytorch.org/cppdocs/notes/tensor_indexing.html</a>
</li>
</ol>
<p>and</p>
<ol start="2">
<li><a href="https://pytorch.org/cppdocs/notes/tensor_basics.html" rel="nofollow noopener">https://pytorch.org/cppdocs/notes/tensor_basics.html</a></li>
</ol>
<p>CUDA accessors<br>
<strong>global</strong> void packed_accessor_kernel(<br>
PackedTensorAccessor64&lt;float, 2&gt; foo,<br>
float* trace) {<br>
int i=threadIdx.x<br>
gpuAtomicAdd(trace, foo[i][i])<br>
}</p>
<p>torch::Tensor foo = torch::rand({12, 12});</p>
<p>// assert foo is 2-dimensional and holds floats.<br>
auto foo_a = foo.packed_accessor64&lt;float,2&gt;();<br>
float trace = 0;</p>
<p>packed_accessor_kernel&lt;&lt;&lt;1, 12&gt;&gt;&gt;(foo_a, &amp;trace);</p>

Lin_Jia: <p>BTW, accessor API has higher speed compared to index API, it is more suitable if you need large amount of API calls.</p>

zzsunshine: <p>Really appreciate your help! And sorry that I missed this message. Accessor API is a good approach.</p>
Visualise sequential model feature maps

johndoe123: <p>I am extracting the feature maps from every activation layer after convolutions, and I am struggling to visualise the output:</p>
<pre><code class="lang-auto">def get_fmaps(input, model):
    fmaps = []
    for i in [0, 3, 6, 8, 10]:  ##conv2d index
        out = []
        out.append(model.features[0].forward(input))
        out.append(model.features[1].forward(out[0]))
        for x in range(2,i+1):
            out.append(model.features[x].forward(out[x-1]))
        fmaps.append(model.features[i+1].forward(out[i])) ##output after activation function
    return fmaps
</code></pre>
<p>I’m not sure if this is the correct way to do it, but once I have returned fmaps, how do I visualise the feature maps?</p>

ptrblck: <p>I think the better way would be to use forward hooks as explained in <a href="https://discuss.pytorch.org/t/how-can-l-load-my-best-model-as-a-feature-extractor-evaluator/17254/6">this post</a>. Your current approach could break, if you are using functional API calls inside the <code>forward</code> and you would need to add them.</p>
<p>Once you have the tensors, you could visualize them via e.g. <code>matplotlib</code>.</p>

johndoe123: <p>Thank you for the link.<br>
From what I can tell, in the example you are calling them by name I.e “fc2”, is there a way instead to call by indexing, since I know which layers are Conv2d, and I would like to access every conv2d output</p>
<p>Something like this:<br>
model.features[i].register_forward_hook(get_activation(i))</p>

ptrblck: <p>The <code>get_activation</code> method is just used for convenience to be able to pass names to it and you can use any name you want.<br>
The actual registered hook is defined in the <code>hook</code> method inside <code>get_activation</code>.</p>
PyTorch3D [Fit Textured Volume] dataset

antonia0912: <p>I’m checking one tutorial of PyTorch3D at Github, named <a href="https://github.com/facebookresearch/pytorch3d/blob/master/docs/tutorials/fit_textured_volume.ipynb" rel="noopener nofollow ugc">Fit Textured Volume</a>, but I haven’t found the dataset for this training.</p>
<p>So is anybody can help me find this dataset or send me the link?  Thanks a lot.</p>
Error loading tcaffe2_detectron_ops_gpu.dll, or one of its dependencies

Mr.Crab: <p>i used pyinstaller to pack my code into exe in pytorch1.8. The python code and exe can run well in the virtual environment of pytorch 1.8, but it report an error when i run the exe without virtual environment of on another computer. I checked the torch folder in the dist, i make sure that the caffe2_detectron_ops_gpu.dll file do exist in the folder. What led to this question? can what should i do to resolve it.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/6/1/615454cd3708e30653f66a2ebe97311f5aad891f.png" data-download-href="https://discuss.pytorch.org/uploads/default/615454cd3708e30653f66a2ebe97311f5aad891f" title="微信截图_20210308095006"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/6/1/615454cd3708e30653f66a2ebe97311f5aad891f_2_690x447.png" alt="微信截图_20210308095006" data-base62-sha1="dT0ZB4Ux7krBdGN6ZdKOVkRQ3M3" width="690" height="447" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/6/1/615454cd3708e30653f66a2ebe97311f5aad891f_2_690x447.png, https://discuss.pytorch.org/uploads/default/original/3X/6/1/615454cd3708e30653f66a2ebe97311f5aad891f.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/6/1/615454cd3708e30653f66a2ebe97311f5aad891f.png 2x" data-dominant-color="F6F6F7"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">微信截图_20210308095006</span><span class="informations">935×607 60.4 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
Whether Activation quantize dynamically during dynamic quantization

Kbcai: <p>Pytorch official documents mentioned that “the weights are quantized ahead of time but the activations are dynamically quantized during inference” .  It also offers code for the simplest implementation.</p>
<p>However, when i tried to convert a floating-type model to one int-type model, the weights are quantized ahead of time, while the activations of intermidiate layer (e.g. Linear layer) seems that still keep floating type.</p>
<p>Is there some discrepancy or just my mistakenly understanding that question?</p>
<p>For example, the quantized fc layer just do this inference:<br>
input_fp32 X FC_params_int8 → output_fp32.  I can’t find how it quantize activations dynamically.</p>

raghuramank100: <p>Yes, activations are quantized dynamically: i.e for every batch, the activations are quantized prior to the linear operation. This is done by calculating the dynamic range of the activations (min and max) and then quantizing the activations to 8 bits. This happens in C++ as part of the operator implementation itself, you can see the details at:</p><aside class="onebox githubblob">
  <header class="source">
      <a href="https://github.com/pytorch/pytorch/blob/master/aten/src/ATen/native/quantized/cpu/qlinear_dynamic.cpp#L64" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>
  <article class="onebox-body">
    <h4><a href="https://github.com/pytorch/pytorch/blob/master/aten/src/ATen/native/quantized/cpu/qlinear_dynamic.cpp#L64" target="_blank" rel="noopener nofollow ugc">pytorch/pytorch/blob/master/aten/src/ATen/native/quantized/cpu/qlinear_dynamic.cpp#L64</a></h4>
<pre class="onebox"><code class="lang-cpp"><ol class="start lines" start="54" style="counter-reset: li-counter 53 ;">
<li>    /*m=*/input_ptr,</li><li>    /*min=*/&amp;x_min,</li><li>    /*max=*/&amp;x_max,</li><li>    /*len=*/input.numel());</li><li></li><li>// Input tensor is quantized as 8-bit unsigned values</li><li>static constexpr int precision = 8;</li><li>static constexpr bool is_signed = false;</li><li></li><li>// Calculate scale and zero point for quantization of input tensor</li><li class="selected">auto q_params = quant_utils::ChooseQuantizationParams(</li><li>    /*min=*/x_min,</li><li>    /*max=*/x_max,</li><li>    /*qmin=*/is_signed ? -(1 &lt;&lt; (precision - 1)) : 0,</li><li>    /*qmax=*/</li><li>    is_signed ? ((1 &lt;&lt; (precision - 1)) - 1) : (1 &lt;&lt; precision) - 1,</li><li>    /*preserve_sparsity=*/false,</li><li>    /*force_scale_power_of_two=*/false,</li><li>    /*reduce_range=*/reduce_range);</li><li></li><li>q_params.precision = precision;</li>
</ol></code></pre>


  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>
How to get the weights of quantized module?

marka17: <p>Hi!</p>
<p>Let me have the following module:</p>
<pre><code class="lang-python">m = nn.quantized.dynamic.Linear(20, 30, dtype=torch.qint8)
m = torch.jit.script(m)
</code></pre>
<p>How can I get the weights of the <code>m</code> module back?</p>

raghuramank100: <aside class="quote no-group" data-username="marka17" data-post="1" data-topic="113750">
<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/marka17/48/10905_2.png" class="avatar"> marka17:</div>
<blockquote>
<pre><code class="lang-auto">m = nn.quantized.dynamic.Linear(20, 30, dtype=torch.qint8)
m = torch.jit.script(m)
</code></pre>
</blockquote>
</aside>
<p>Use the _weight_bias() method to get the weights back:</p>
<p>(wt,bias) = m._packed_params._weight_bias()</p><aside class="onebox githubblob" data-onebox-src="https://github.com/pytorch/pytorch/blob/main/torch/nn/quantized/modules/linear.py#L32">
  <header class="source">

      <a href="https://github.com/pytorch/pytorch/blob/main/torch/nn/quantized/modules/linear.py#L32" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>

  <article class="onebox-body">
    <h4><a href="https://github.com/pytorch/pytorch/blob/main/torch/nn/quantized/modules/linear.py#L32" target="_blank" rel="noopener nofollow ugc">pytorch/pytorch/blob/main/torch/nn/quantized/modules/linear.py#L32</a></h4>



    <pre class="onebox"><code class="lang-py">
      <ol class="start lines" start="1" style="counter-reset: li-counter 0 ;">
          <li class="selected"># flake8: noqa: F401</li>
          <li>r"""Quantized Modules.</li>
          <li></li>
          <li>This file is in the process of migration to `torch/ao/nn/quantized`, and</li>
          <li>is kept here for compatibility while the migration process is ongoing.</li>
          <li>If you are adding a new entry/functionality, please, add it to the</li>
          <li>appropriate file under the `torch/ao/nn/quantized/modules`,</li>
          <li>while adding an import statement here.</li>
          <li>"""</li>
          <li></li>
          <li>__all__ = ['LinearPackedParams', 'Linear']</li>
          <li></li>
          <li>from torch.ao.nn.quantized.modules.linear import Linear</li>
          <li>from torch.ao.nn.quantized.modules.linear import LinearPackedParams</li>
      </ol>
    </code></pre>



  </article>

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

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

jacobbuckman: <p>Numpy has an in-depth guide on subclassing ndarray, allowing arrays to be associated with additional attributes and behaviors while still maintaining the core ndarray functionality:<br>
<a href="https://docs.scipy.org/doc/numpy/user/basics.subclassing.html" class="onebox" target="_blank" rel="nofollow noopener">https://docs.scipy.org/doc/numpy/user/basics.subclassing.html</a></p>
<p>Does any similar feature exist for PyTorch tensors? I’ve tried several things, but unfortunately not been able to get it working. I found a <a href="https://discuss.pytorch.org/t/subclassing-torch-tensor/23754">similar discussion</a> from a year ago but no good answer was provided.</p>

anantguptadbl: <p>I gave it a shot over and above what was discussed in the <a href="https://discuss.pytorch.org/t/subclassing-torch-tensor/23754">older discussion</a> that you have pasted</p>
<p>Example</p>
<pre><code class="lang-auto">import torch
from torch import nn
from torch.autograd import Variable
import torch.nn.functional as F
class MyObject(torch.Tensor): 
    @staticmethod 
    def __new__(cls, x, extra_data, *args, **kwargs): 
        return super().__new__(cls, x, *args, **kwargs) 
      
    def __init__(self, x, extra_data): 
        self.extra_data = extra_data

    def clone(self, *args, **kwargs): 
        return MyObject(super().clone(*args, **kwargs), self.extra_data)

    def to(self, *args, **kwargs):
        new_obj = MyObject([], self.extra_data)
        tempTensor=super().to(*args, **kwargs)
        new_obj.data=tempTensor.data
        new_obj.requires_grad=tempTensor.requires_grad
        return(new_obj)
</code></pre>
<pre><code class="lang-auto">obj1 = MyObject([1, 2, 3], 'extra_data_123')
obj1.requires_grad_(True)
print(obj1.requires_grad)
obj2 = obj1.to('cuda')
print(obj2.requires_grad)
t1 = torch.Tensor([1, 2, 3])
t1.requires_grad_(True)
t2 = t1.to('cuda')
print(t2.requires_grad)
</code></pre>
<pre><code class="lang-auto">True
True
True
</code></pre>
<p>Hope this helps</p>

jacobbuckman: <p>This almost works, but it fails to capture one of the key benefits of the numpy subclassing: various operations, most notably slicing and basic operations like addition, preserve the added information. I’d like to be able to run: <code>print(obj1[:2].extra_data)</code>.</p>

jacobbuckman: <p>Furthermore, is there any way to subclass boolean, integer, or other types of tensors? Currently, using them as a base class throws an error. But I can find no way to modify or set the type of a tensor subclass; it’s stuck on float32.</p>

tyoc213: <p>Is this still the case?</p>

turian: <p><a class="mention" href="/u/tyoc213">@tyoc213</a> see here: <a href="https://discuss.pytorch.org/t/typeerror-expected-cpu-got-cuda-when-sublcassing-inheriting-torch-tensor-class/110489/5" class="inline-onebox">TypeError: expected CPU (got CUDA) when sublcassing / inheriting torch.Tensor class - #5 by turian</a></p>
TypeError: expected CPU (got CUDA) when sublcassing / inheriting torch.Tensor class

ProGamerGov: <p>The code below results in an error when it’s run with a cuda tensor input. How do I resolve the error?</p>
<pre><code class="lang-auto">import torch

class TestTensor(torch.Tensor):
    def test(self):
          print('test')

x = torch.ones(1,3,4,4).cuda()

x_out = TestTensor(x)
</code></pre>
<p>Throws the following error:</p>
<pre><code class="lang-auto">Traceback (most recent call last):
  File "test_tensor.py", line 8, in &lt;module&gt;
    x_out = TestTensor(x)
TypeError: expected CPU (got CUDA)
</code></pre>

albanD: <p>Hi,</p>
<p>I am not sure if this is the expected behavior…<br>
Could you open an issue on github please so that we can track this?</p>
<p>Also, if possible, make sure you can reproduce this with the latest nightly build because this feature is in active development and it might have been fixed.</p>

jordie_s: <p>Hi,</p>
<p>I just tried the nightly build and this is still an issue for me. Did a github issue get made for this?</p>

turian: <p>Here is <a href="https://github.com/pytorch/pytorch/issues/52265" rel="noopener nofollow ugc">the issue</a>. We have the same issue.</p>
<p>What is the fix we should apply?</p>
<pre><code class="lang-auto">import torch

class Signal(torch.Tensor):
    @property
    def batch_size(self):
        assert self.ndim == 2
        return self.shape[0]

    @property
    def num_samples(self):
        assert self.ndim == 2
        return self.shape[1]

N = 88200
batch_size = 64
pitch = Signal(torch.zeros(batch_size, N, device='cuda'))
</code></pre>
<pre><code class="lang-auto">TypeError: expected CPU (got CUDA)
</code></pre>
<p>What should we be doing instead?</p>

turian: <p>Solution, according to the issue:</p>
<aside class="quote no-group quote-modified" data-username="turian" data-post="4" data-topic="110489">
<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/turian/48/30217_2.png" class="avatar"> turian:</div>
<blockquote>
<pre><code class="lang-auto">import torch

class Signal(torch.Tensor):
    @property
    def batch_size(self):
        assert self.ndim == 2
        return self.shape[0]

    @property
    def num_samples(self):
        assert self.ndim == 2
        return self.shape[1]

N = 88200
batch_size = 64
pitch = torch.zeros(batch_size, N, device='cuda').as_subclass(Signal)
</code></pre>
</blockquote>
</aside>
Can jit.trace be run after I already assigned an optimizer?

tjoseph: <p>Hello,<br>
can I use <code>torch.jit.trace</code> after I already assigned the optimizer? I am currently met with loss explosion when I add <code>torch.jit.trace</code>.</p>
<p>Example pseudo code:</p>
<pre><code class="lang-auto">model = Model()  # Model is an nn.Module
loss_fn = LossFn(model)  # LossFn is an nn.Module without any parameters
optimizer = SGD(model.parameters())

batch = next(dataloader)
loss_fn = torch.jit.trace(loss_fn, (batch,))


# Training
for batch in dataloader:
    loss = loss_fn(batch)
    loss.backward()
    optimizer.step()
    optimizer.zero_grad()
</code></pre>
Applying l2 normalization and recieve the same shape as input tensor

hamedB: <p>Hi<br>
I have already seen some topic about the normalization and no one include my problem.</p>
<p>I have a code in Keras which applies l2 normalization on a matrix and returns the result with the same shape of input:<br>
K.l2_normalize(input, axis=0)</p>
<p>However, It seems that torch.norm function reduces the dimension of input tensor. Is there any equivalent keras norm function in the pytorch or should I implement it from scratch?<br>
I want to have a l2 normalized tensor with the same shape as input</p>
<p>Thank you in advance.</p>

ZimoNitrome: <p>Pytorch has <code>torch.nn.MSELoss()</code> and the functional <code>torch.nn.functional.mse_loss()</code>.</p>
<p>Passing <code>reduce=False</code> to any of these will keep your dimensions:</p>
<pre><code class="lang-auto">a = torch.rand([2, 3, 20, 20])
b = torch.rand([2, 3, 20, 20])

torch.nn.functional.mse_loss(a, b, reduce=False).shape

&gt; torch.Size([2, 3, 20, 20])
</code></pre>
Newbie using Pytorch3D

stevenirl: <p>I’ll preface this by saying I’m very new to pytorch and coding in general and a lot of my errors are probably concepts that just go over my head.</p>
<p>I’m a student trying to utilize some of these models in my own studies, specifically the texture renderer and point cloud voxel model. I’ve started to use the google colab however whenever I try to put in my own .obj, .mtl, .jpg I get a 403 error. I was wondering if that is because the access to individual experimentation with the code is restricted or if it’s a problem on my end. Thanks!</p>

ptrblck: <p>I assume the 403 forbidden error is raised by Colab or any other server and not by PyTorch or any library using it.<br>
If so I guess your drive/server/… doesn’t allow Colab to read the data and you would need to mount it somehow etc.</p>

stevenirl: <p>Ok thanks! Do you know of any good tutorials or starting points you could direct me towards?</p>
Libtorch c++ equivalent of F.normalize(x, p=2, dim=1)

AbaqusNastan: <p>To normalise, in python I simply do:</p>
<pre><code class="lang-auto">F.normalize(x, p=2, dim=1)
</code></pre>
<p>I was wondering what is the libtorch c++ equivalent of this.</p>

ptrblck: <p>This should work:</p>
<pre><code class="lang-auto">namespace F = torch::nn::functional;
F::normalize(input, F::NormalizeFuncOptions().p(1).dim(-1));
</code></pre>

AbaqusNastan: <p>Awesome! thank you <a class="mention" href="/u/ptrblck">@ptrblck</a> your help is invaluable.</p>
Learning parameters of sinusoids

timlod: <p>Hi!<br>
I was wondering if there’s a proper way to learn the parameters of a sinusoid by gradient descent?<br>
Here’s an example setup:</p>
<pre><code class="lang-auto">x = torch.arange(0, 5, 1 / sr) # =5s of audio
y = torch.sin(2 * np.pi * 2.85 * x) # want to get sinusoid with frequency 2.85
f = nn.Parameter(torch.tensor(2.0)) # initialize frequency parameter as 2.85

opt = torch.optim.AdamW([f], lr=0.001)
loss = nn.MSELoss()

for i in range(100):
    out = torch.sin(2 * np.pi * f * x)
    l = loss(out, y)
    l.backward()
    opt.step()
    opt.zero_grad()
</code></pre>
<p>This won’t learn the correct value for <code>f</code> (2.85), but rather stay around 2.<br>
Is there a way to set this up so it can learn through a periodic function?</p>
<p>Thanks for any input!</p>
<p>edit: simplified code somewhat</p>

yoyololicon: <p>I think maybe evaluate the loss in frequency domain via fft will be easier, because directly train on time domain, in my experiences, is generally very hard</p>
"Kinda" breaking backprop gradient with discrete values?

ZimoNitrome: <p>I have been trying to implement a “color quantization module” for a project but I am getting stuck.<br>
I have tried to simplify the techniques I am using to play around with what is actually possible in PyTorch.</p>
<p>Consider:</p>
<pre><code class="lang-python">n = 10

for input, target in dataloader:
    output = model(input)
    output = (n*output).round() / n   # &lt;---
    loss = l1_loss(output, target)
    loss.backward()
</code></pre>
<p>Here, we limit the values that <code>output</code> can take to only 10 unique ones. This makes the values discrete and does not throw any error that the gradient is broken BUT training seems pretty much impossible</p>
<p>Is there any way to train models this way? What I want to do in the future is to vary <code>n</code> per sample.<br>
Maybe <code>torch.quantization</code> has a use case for this? I am not very familiar with that module and have been under the impression that it is done for performance sake and might use a static resolution (<code>n</code>).</p>

ptrblck: <p>The <code>round</code> operation would kill the gradient (almost) everywhere, so it’s indeed not very useful for training as seen here:</p>
<pre><code class="lang-python">x = torch.randn(10, requires_grad=True)
y = torch.round(x)
y.mean().backward()
print(x.grad)
&gt; tensor([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
</code></pre>
<p>You could try to implement <a href="https://discuss.pytorch.org/t/relu-with-leaky-derivative/32818/2">this approach</a> posted by <a class="mention" href="/u/tom">@tom</a>, which would be able to use different “paths” during the forward and backward call.</p>

ZimoNitrome: <p>Thanks ptr. Is there a list of functions like this which “almost always” break the gradient or do we just have to experiment?</p>
<p>Using min() and max() most likely have similar behavior as round() but does not throw “gradient broken error” like argmin() and argmax() do.</p>

ptrblck: <p>I don’t know, if there is a list collecting these operations, but you could “draw” the applied method for different values (in mind or with any software library) and check, how the derivative would look.<br>
For these “almost always” breaking methods, the gradient would be zero almost everywhere beside some specific points.</p>
Wiki.vec pre-trained word embedding don't make any improvment

Aiman_Mutasem-bellh: <p>Hello all <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>
<p>I’m trying to apply wiki.vec as pre-trained word embedding like the below lines of code, but don’t make any improvements.</p>
<blockquote>
<p>MIN_COUNT = 2<br>
url = ‘<a href="https://dl.fbaipublicfiles.com/fasttext/vectors-wiki/wiki.ar.vec" rel="noopener nofollow ugc">https://dl.fbaipublicfiles.com/fasttext/vectors-wiki/wiki.ar.vec</a>’<br>
TRG.build_vocab(train_data, vectors=Vectors(‘wiki.ar.vec’, url=url), unk_init = torch.Tensor.normal_, min_freq=MIN_COUNT</p>
</blockquote>
<p>Is it enough or I missed something?</p>
<p>Kind regards,<br>
Aiman Solyman</p>

AbdulsalamBande: <p>Yes you might not see improvements because in your model you need to do something like this .<br>
I did the same thing in the project below</p>
<pre><code class="lang-auto">self.embedding = nn.Embedding(n_tokens, embedding_size)
self.embedding.weight.data.copy_(pretrained_vectors)
</code></pre>
<p><a href="https://github.com/abdulsalam-bande/Kaggle_Projects/blob/master/jigsaw-toxic-comment-classification-challenge/Main.ipynb" rel="noopener nofollow ugc">Link to Project that contains Your problem solution</a></p>

Aiman_Mutasem-bellh: <p>Thank you Mr. <a class="mention" href="/u/AbdulsalamBande">@AbdulsalamBande</a>  <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>
Transfer Learning with imbalanced dataset

wenglish: <p>Hello,</p>
<p>I recently started working with pytorch and went through the transfer learning tutorial. I now have that up and running, but the dataset I am working with is very imbalanced and relatively small of 10,000 images. What steps should I take to improve the transfer learning tutorial for a imbalanced dataset?</p>
<p>Thank you!</p>

ptrblck: <p>One approach would be to use a <code>WeightedRandomSampler</code> (<a href="https://discuss.pytorch.org/t/how-to-handle-imbalanced-classes/11264/2">here</a> is a small example of its usage) or use weights in your loss function (e.g. <code>nn.CrossEntropyLoss</code> accepts a <code>weight</code> argument).</p>

wenglish: <p>For using weights in my loss function, what values would I use? Would it be the percentages of each class?</p>

ptrblck: <p>That would be a possibility.<br>
I would recommend to track a valid metric (accuracy might be misleading due to the imbalanced dataset) and try out different weighting.</p>

wenglish: <p>Would I have to change my training function if I do pass in weights to the loss function?</p>

ptrblck: <p>What do you mean by training function?</p>

wenglish: <p>The train_model() function given by the tutorial, the fitting function. Would I have to change that?</p>

ptrblck: <p>Ah, OK. No you could just pass your new (weighted) criterion and it should work.</p>

ss007k: <p>Which method is recommended/gives better metrics? Using the weighted sampler in dataloader OR using weights in the crossentropy loss function?</p>

ptrblck: <p>You should try both methods, as I cannot universally claim one method works better than the other.<br>
Based on past posts in this forum I’ve seen both claims <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"></p>
CrossEntropy with softmax?

Kapil_Rana: <p>I have doubt.  I read that CrossEntropy is combination of logsoftmax and nllloss. In my case i want to apply softmax in last layer (not logsoftmax), so which loss function I have to use. Is CrossEntropyloss is good enough.)</p>

KFrank: <p>Hi Kapil!</p>
<aside class="quote no-group" data-username="Kapil_Rana" data-post="1" data-topic="113812" data-full="true">
<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/kapil_rana/48/20951_2.png" class="avatar"> Kapil_Rana:</div>
<blockquote>
<p>In my case i want to apply softmax in last layer</p>
</blockquote>
</aside>
<p>You don’t actually want to apply <code>softmax()</code> as the last layer of your<br>
model.  You should simply use the output of your last <code>Linear</code> layer<br>
(to be understood as <em>logits</em>), and pass them to <code>CrossEntropyLoss</code>.</p>
<p>If you really need probabilities (rather than logits) for some purpose<br>
(and you probably don’t), you should still use <code>CrossEntropyLoss</code><br>
as your loss function, passing in logits, and separately generate<br>
the probabilities by applying <code>softmax()</code> to the output of your model.</p>
<aside class="quote no-group">
<blockquote>
<p>Is CrossEntropyloss is good enough.</p>
</blockquote>
</aside>
<p>If you <em>insist</em> on building a model that outputs probabilities by<br>
using <code>softmax()</code> as your last layer, then you shouldn’t use<br>
<code>CrossEntropyLoss</code> as it doesn’t expect probabilities as inputs.<br>
You would have to write your own version of cross-entropy that<br>
does take probabilities (straightforward to do), but this approach<br>
is numerically less stable than passing logits to <code>CrossEntropyLoss</code><br>
(even thought the two approaches are mathematically equivalent).</p>
<p>Best.</p>
<p>K. Frank</p>

Kapil_Rana: <p>Actually, I was regenerating the results of one paper, where they have used softmax at the last layer. They did not mention the loss function. So it is kind of mandatory to apply softmax at the last layer. Is there any alternative that does exactly as same as mentioned. or is there any custom implementation of cross-entropy loss (the basic cross-entropy loss  -y_i log y_i  where is i is true label.)</p>

MrPositron: <p>You can use cross entropy loss from here: <a href="https://datascience.stackexchange.com/questions/55962/pytorch-doing-a-cross-entropy-loss-when-the-predictions-already-have-probabiliti/55964#55964" class="inline-onebox" rel="noopener nofollow ugc">neural network - Pytorch doing a cross entropy loss when the predictions already have probabilities - Data Science Stack Exchange</a></p>

Kapil_Rana: <p>Thank You MrPositron.</p>
Getting error csv_reader() got an unexpected keyword argument 'encoding'

krishna511: <pre><code class="lang-auto">def csv_reader(add):
    with open(outfilename, 'r',encoding='utf8') as f:
        reader = csv.reader(f, delimiter=';')
        headers = next(reader)
        data = np.array(list(reader)).astype(float)
        
    return data[:,2:] `Preformatted text`
</code></pre>
<p>I am using above function to read a csv file But when I ran this below snippet</p>
<pre><code class="lang-auto">MFCC = csv_reader(outfilename,encoding='utf8')

</code></pre>
<p>Getting this error<br>
csv_reader() got an unexpected keyword argument ‘encoding’</p>
<p>Tried removing this encoding keyword too . but then its throwing:<br>
UnicodeDecodeError: ‘charmap’ codec can’t decode byte 0x8f in position 33: character maps to </p>
<p>Actually this csv file is a feature extraction through opensmile toolkit. The csv file which is created after execution is like<br>
Á† œ	ÀÃ}rÁ`eÌ@×oGÁË¹ô@ÄîÁ©@ówÁ¯"ÀàV@GAuïó@ë’BÎÆÒ?i»¿‰&lt; ?×z?‘E=¿ŽŽÌ¿Ä¦s¾´j@-8A@5w¾øºšÀæåÀe¾¼Í=ãÆƒ&gt;ì<br>
Can anyone guide one this plz.<br>
Regards</p>

ptrblck: <p>This issue doesn’t seem to be PyTorch-specific and I would recommend to post it to a Python-related board (or StackOverflow) for faster help. <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"></p>
<p>That being said, as you’ve already explained, <code>csv_reader</code> doesn’t accept an <code>encoding</code> argument so you would have to remove it.<br>
For the encoding error, maybe <a href="https://stackoverflow.com/questions/9233027/unicodedecodeerror-charmap-codec-cant-decode-byte-x-in-position-y-character">this post</a> is helpful.</p>

krishna511: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> Thanks sir<br>
I have removed that argument alraedy <img src="https://discuss.pytorch.org/images/emoji/apple/frowning.png?v=9" title=":frowning:" class="emoji" alt=":frowning:"><br>
In fact tried all encoding formats.</p>
Got a question about Implement of nn.Transformer

MarcusNerva: <p>The shape of input tensor src(S, N, E) and output tensor tgt(T, N, E) are both let their length(i.e. S and T) infront of batch(i.e. N). I am very curious. Why the programmer arrange their shape in this way? By habit, we always put ’ batch ’ at the first dim.</p>

ptrblck: <p>I guess for performance reasons as was also done in RNNs.</p>
nn.Parameter changes input tensor

yzxhd: <p>I want to use pytorch to optimize a simple loss function. The only parameters are <code>w</code>. Now I want to initialize <code>w</code> with <code>w0</code>. However, I find that after <code>loss.backward()</code>, <code>w0</code> also change. How to make sure the initial tensor <code>w0</code> is not changed? Does <code>nn.Parameter</code> copy reference?</p>
<pre><code class="lang-auto">import numpy as np
import torch
import torch.nn as nn

class mymodel(nn.Module):
    def __init__(self, w0):
        super().__init__() 
        self.w = nn.Parameter(w0)
    def forward(self, x):
        diff = self.w * x - 10
        self.loss = torch.mean(diff * diff)
        return self.loss

w0 = torch.randn(2, 3)
model = mymodel(w0)
print(w0)
print(model.w)

optimizer = torch.optim.Adam(model.parameters(), lr = 0.1)
inputs = torch.randn(2, 3)
loss = model(inputs)
optimizer.zero_grad()
loss.backward()
optimizer.step()

print(w0)
print(model.w)
</code></pre>
<p>Results:</p>
<pre><code class="lang-auto">tensor([[-0.6700,  1.4510, -0.0059],
        [-0.0139, -1.0681,  0.4844]])
Parameter containing:
tensor([[-0.6700,  1.4510, -0.0059],
        [-0.0139, -1.0681,  0.4844]], requires_grad=True)

tensor([[-0.5700,  1.5510,  0.0941],
        [ 0.0861, -0.9681,  0.3844]])
Parameter containing:
tensor([[-0.5700,  1.5510,  0.0941],
        [ 0.0861, -0.9681,  0.3844]], requires_grad=True)
</code></pre>

crcrpar: <p>I think <code>nn.Parameter(w0.clone().detach())</code> will fix the problem.</p>
<p>notebook: <a href="https://gist.github.com/crcrpar/5f084ae9b7a2bf5a4bfb4408207b737b" class="inline-onebox" rel="noopener nofollow ugc">nn-parameter-changes-input-tensor.ipynb · GitHub</a></p>
<p>Reference: See warning block of <code>new_tensor</code> <a href="https://pytorch.org/docs/stable/tensors.html" class="inline-onebox" rel="noopener nofollow ugc">torch.Tensor — PyTorch 1.8.0 documentation</a></p>
Parallel for Loop

Ohm: <p>How can we make the following for loop calculated in parallel and get the result? Please give a runnable example. Thanks.<br>
“”"<br>
Participated_Workers = [1,5,8]<br>
for P in zip(Participated_Workers ):<br>
loss_worker, temp = worker_new(net_state_dict, VGG, CUDAA, P[0], Itr, data[cs1]]))<br>
net_parameters_SGD.append(temp_SGD[0])<br>
cs1 += 1<br>
“”""</p>
Model inference memory crush

nzinc: <p>Hi<br>
When I’m trying to evaluate class prediction on test dataset my laptop start freezing, I suppose somewhere in my code there is a memory leak.</p>
<p>Code below</p>
<pre><code class="lang-auto">test_tr = transforms.Compose([
        transforms.ToPILImage(),
        transforms.Resize((IMG_SHAPE, IMG_SHAPE)),
        transforms.ToTensor()
        ])

class data_test(torch.utils.data.Dataset):
    def __init__(self, path):
        self.path = path
        self.names = list(os.listdir(path))

    def __len__(self):
        return len(self.names)

    def __getitem__(self, ind):
        img_name = self.names[ind]
        x = imread(os.path.join(self.path, str(img_name)))
        if len(x.shape) == 2 or x.shape[-1] == 1:
            x = gray2rgb(x)
        x = test_tr(x)
        return x, img_name

def infer(model_path, test_img_dir):
    dataset = data_test(test_img_dir)
    loader = DataLoader(dataset, 16, num_workers=1)
    #model = EfficientNet.from_pretrained('efficientnet-b1', num_classes=50)
    model = torch.load(model_path, map_location=torch.device('cpu'))
    model.eval()
    res = {}
    for x, y in loader:
        out = model(x)
        for i, bird in enumerate(out):
            res[y[i]] = torch.argmax(bird).item()
    return res
</code></pre>
<p>Load model is from EfficientNet library, so I don’t missed up with model class)<br>
When I’m run <code>infer</code> it can compute for several batches (1-3 iteration in loader) and after that laptop is freezing. Where is the weak place in code?</p>

ZimoNitrome: <p>Have you tried different <code>num_workers</code> for the <code>DataLoader</code>?<br>
<code>num_workers=0</code> has been the most stable one from personal experience.</p>
<p>Otherwise I don’t see anything wrong with your code. Maybe the model is large and simply performs very slowly on CPU.</p>

nzinc: <p>Wow, really problem was with <code>num_workers=1</code>. Can you explain why, please?</p>

ZimoNitrome: <p>In my understanding, <code>num_workers=1</code> uses multithreading while <code>num_workers=0</code> does not.<br>
Using multi-threading means that you need to run the majority of your training loop inside a <code>if __name__ == '__main__':</code> just due to how Python handles multi-threading.</p>
<p>The multi-threading in <code>DataLoader</code> seems to be a bit sensitive as well. Using a 16 thread system, I am only able to run the dataloader on 6-8 threads. Using more threads have previously crashed training randomly.</p>
PytorchStreamWriter failed writing central directory

af_luther: <p>I have deployed an inference server using uWSGI + Flask + PyTorch on AWS Amazon Linux with Elastic Inference.</p>
<p>I have uWSGI configured at 2 threads and 4 processes.</p>
<p>In most circumstances clients send three requests in a row which are processed in parallel. The first usually returns in less than half a second, the next two is less than a second.</p>
<p>After months of stable processing, I recently started doing some scalability testing and decided to try increasing the number of processes to 6 on a single server. Now I have started getting some intermittent issues which I have not seen before:</p>
<pre><code class="lang-auto">terminate called after throwing an instance of ‘c10::Error’
what(): [enforce fail at inline_container.cc:316] . PytorchStreamWriter failed writing central directory: file write failed
</code></pre>
<p>I cannot find any references to this error message. My best guess is that it is some kind of concurrency issue; perhaps my increased number of processes is now making it possibly, or at least more likely, for two processes to try and write the the same file at the same time (whatever that file may be in the “central directory” – whatever that is).</p>
<p>One possible solution might be to set lazy or lazy-apps to true in uWSGI, but first I’d like to better understand the root cause of the error message.</p>
<p>What is the “central directory” and what could cause this error? Is it indeed concurrency?</p>
<p>I load the model once in my python flask script<br>
<code>model = torch.jit.load(model_path).eval()</code><br>
and execute the model in the service method with the given input data.</p>