akankshakshurai/cuda-error-resolution-analysis huggingface dataset:
[solved] where is torch.rand() code?

Ashish_Bairwa: <p>I don’t know if it’s the right place to ask this one. But i’m searching for the code of rand and it’s implementation used in the pytorch.</p>

Ashish_Bairwa: <p>I think it’s working on the same mersenne twister.<br>
[SOLVED]</p>

Reactive9435: <p>I am looking for this as well. Did you manage to figure out where the implementation is?</p>
PyTorch not detecting AMD GPU although ROCM installed

David_Lee: <p>OS: Ubuntu 20.04</p>
<pre><code class="lang-auto">$ rocm-smi

======================= ROCm System Management Interface =======================
================================= Concise Info =================================
GPU  Temp   AvgPwr  SCLK    MCLK    Fan  Perf  PwrCap  VRAM%  GPU%  
0    46.0c  8.0W    800Mhz  100Mhz  0%   auto  180.0W    2%   0%    
================================================================================
============================= End of ROCm SMI Log ==============================
</code></pre>
<pre><code class="lang-auto">
$ rocminfo | grep 'Name:'
  Name:                    Intel(R) Core(TM) i3-10100 CPU @ 3.60GHz
  Marketing Name:          Intel(R) Core(TM) i3-10100 CPU @ 3.60GHz
  Vendor Name:             CPU                                
  Name:                    gfx1010                            
  Marketing Name:          AMD Radeon RX 5700 XT              
  Vendor Name:             AMD                                
      Name:                    amdgcn-amd-amdhsa--gfx1010:xnack-
</code></pre>
<p>However, Pytorch is not able to detect GPU:</p>
<pre><code class="lang-auto">$ python3

Python 3.9.16 (main, Mar 8 2023, 14:00:05)

[GCC 11.2.0] :: Anaconda, Inc. on linux

Type "help", "copyright", "credits" or "license" for more information.

&gt;&gt;&gt; import torch

&gt;&gt;&gt; print("GPU available:", torch.cuda.is_available())

GPU available: False
</code></pre>
<p>What’s going wrong here?</p>

bhattah: <p>What is your pytorch and ROCm versions? Not all versions of ROCm and not all AMD GPUs are supported. Please see this link for details: <a href="https://pytorch.org/blog/pytorch-for-amd-rocm-platform-now-available-as-python-package/" rel="noopener nofollow ugc">https://pytorch.org/blog/pytorch-for-amd-rocm-platform-now-available-as-python-package/</a></p>

David_Lee: <p>OS Version: Ubuntu 20.04 LTS<br>
PyTorch Version: 2.0<br>
ROCM version: 5.0.2</p>
<p>Are they compatible?</p>
<pre><code class="lang-auto">$ python -c "import torch; print(torch.__version__)"
2.0.0+cu117
</code></pre>
<pre><code class="lang-auto">$ apt show rocm-libs -a

Package: rocm-libs

Version: 5.0.2.50002-72

Priority: optional

Section: devel

Maintainer: ROCm Libs Support &lt;rocm-libs.support@amd.com&gt;

Installed-Size: 13.3 kB

Depends: hipblas, hipfft, hipsolver, hipsparse, miopen-hip, rccl, rocalution, rocblas, rocfft, rocrand, rocsolver, rocsparse, rocm-core, hipblas-dev, hipcub-dev, hipfft-dev, hipsolver-dev, hipsparse-dev, miopen-hip-dev, rccl-dev, rocalution-dev, rocblas-dev, rocfft-dev, rocprim-dev, rocrand-dev, rocsolver-dev, rocsparse-dev, rocthrust-dev

Homepage: https://github.com/RadeonOpenCompute/ROCm

Download-Size: 898 B

APT-Sources: https://repo.radeon.com/rocm/apt/5.0.2 ubuntu/main amd64 Packages

Description: Radeon Open Compute (ROCm) Runtime software stack
</code></pre>
I get nan\inf as an output

Human: <p>I want to make the model able to give me the weight of any mass (F = ma = mass * 9.8 = weight)<br>
I know it’s a stupid idea, but I try to start with the simple things</p>
<pre><code class="lang-auto">import torch
import numpy as np

def make_data (n):
  x,y = [],[]
  for i in range(n):
    mass = np.random.randint(1,1000,1)[0]
    x.append([mass])
    y.append([mass*9.8])
  return x,y

data = make_data(2000)
x = torch.autograd.Variable( torch.Tensor(data[0]) )
y = torch.autograd.Variable( torch.Tensor(data[1]) )
lr = 0.1

class ML (torch.nn.Module):
  def __init__ (self):
    super(ML,self).__init__()
    self.layer = torch.nn.Linear(1,1)
  def forward (self,inp):
    out = self.layer(inp)
    return out

model = ML()
entropyLoss = torch.nn.MSELoss(size_average=False)
gradientDecent = torch.optim.SGD(model.parameters(), lr=lr)

for epoch in range(200):
  pred  = model(x)
  error = entropyLoss(pred, y)
  gradientDecent.zero_grad()
  error.backward()
  gradientDecent.step()
  print(f'Epoch {epoch} : {error.item()}')
</code></pre>
<p>then i get this :</p>
<pre><code class="lang-auto">Epoch 0 : 68154060800.0
Epoch 1 : 1.2420331504344075e+27
Epoch 2 : inf
Epoch 3 : inf
Epoch 4 : inf
Epoch 5 : inf
Epoch 6 : nan
Epoch 7 : nan
....
Epoch 199 : nan
</code></pre>
<p>i try also to test it :</p>
<pre><code class="lang-auto">&gt;&gt;&gt; model(torch.autograd.Variable(torch.Tensor([[4.0]])))
tensor([[nan]], grad_fn=&lt;AddmmBackward0&gt;)
</code></pre>

ptrblck: <p>Your learning rate is too high for the calculated loss, which also sums the sample losses.<br>
I.e. in the first iteration you already have a loss of <code>~1e+10</code>, which will create gradients with a large magnitude and then update the parameters with a learning rate of <code>0.1</code>.<br>
Eecrease the learning rate to e.g. <code>1e-8</code> and remove the <code>size_average=False</code> argument.<br>
This allows the model to train in ~2000 epochs.</p>
<p>Alternatively, normalize the inputs and output and de-normalize them during the model inference phase.</p>

Human: <p>Thanks it works … but i done it with 9999 epoch .<img src="https://discuss.pytorch.org/images/emoji/apple/fire.png?v=12" title=":fire:" class="emoji" alt=":fire:" loading="lazy" width="20" height="20"></p>
Stopping integer addition wrapping when we exceed bounds

jambo6: <p>Current behavior when I add two int16 values such that the sum exceeds int16 bounds is that they wrap around, rather than stay at bounds or produce infs. This is inconsistent with float behaviour which produces ints.</p>
<p>I assume the behaviour is intentional, but I cant find a reference to int 16 addition wrapping. Perhaps I am searching incorrectly.</p>
<p>Example comparison of behaviour</p>
<pre><code class="lang-auto">data = torch.tensor([0])

# fp16
data_fp16 = data.to(torch.float16)
data_fp16 + 66000     # Gets an inf

# int16
data_int16 = data.to(torch.int16)
data_int16 + 66000    # Wraps and returns 464
</code></pre>
<p>Is there a way of modifying the behaviour? Ideally I want to be able to add two int16 tensors and nothing to be able to exceed bounds, with no wrapping.</p>
<p>At the moment I’m bypassing this by casting the first tensor as int32, then adding the two together, then clamping, then casting back. However its a little inconvinient. Is there a way to change the behaviour?</p>
Bayesian regression for categorical respons variable

Matthijs: <p>Hi all,</p>
<p>I just started out using pytorch so bear with me. I have a dataset of 60000 explanatory variables and 324 categorical response variables.  I want to use feature selection on this dataset to see which variables explain this dataset the best and use it for other algorithms.</p>
<p>My priors and likelihood are very simple. These will not be the final distributions, but they work for this example.</p>
<p>As of right now, I use Bayesian linear regression to predict the weights of each of the explanatory variables and see what the 100 most influential dependent variables are by checking the intercept of the weights.</p>
<p>However, since I don’t know how to properly use/predict categorical response variables, I put the variable name in a dictionary and change the variable to its indices. This is not the correct way to do it, but I do now have the knowledge to do so. How do I properly predict which variable is most influencial for categorical response variables and what kind of model should I use?</p>
<pre><code class="lang-auto"># Load data
data = pd.read_csv('gene.csv')

# extract the labels
labels = data.columns[1:]
label_string = [label for label in labels.values]
dictionary = {k: v for k, v in enumerate(label_string)}

# map the labels to their corresponding indices
data.iloc[:, 0] = range(len(data))

y = torch.tensor(data.iloc[:, 0].values).double()
X = torch.tensor(data.iloc[:, 1:].values).double()

# Define the model
def create_model(X, y):
    num_features = X.shape[1]
    # Priors on model parameters
    w = pyro.sample('w', dist.Normal(torch.zeros(num_features), 1)).double()
    b = pyro.sample('b', dist.Normal(0, 10)).double()
    # Linear regression
    mu = torch.matmul(X, w) + b
    # Likelihood of observed data
    pyro.sample('y', dist.Normal(mu, 1), obs=y)

nuts_kernel = NUTS(create_model)
mcmc = MCMC(nuts_kernel, num_samples=1000, warmup_steps=500)
mcmc.run(X, y)
posterior_samples = mcmc.get_samples()

# Extract the means and standard deviations of the posterior distributions of the model parameters
w_mean = posterior_samples['w'].mean(axis=0)
w_std = posterior_samples['w'].std(axis=0)
b_mean = posterior_samples['b'].mean()
b_std = posterior_samples['b'].std()

# get 100 most influencial weights

# Get mean absolute weight for each feature 
mean_abs_weights = abs(posterior_samples['w']).mean(axis=0)


# Sort features based on mean absolute weight
sorted_features = np.argsort(mean_abs_weights)
sorted_features_flipped = torch.flip(sorted_features, [0])

# Get indices of top 100 features
top_100_features = sorted_features_flipped[:100]

#print incides top 100 features
print(top_100_features)


</code></pre>
Using dynamic weighting between two loss functions

ays: <p>Hello, I have two loss functions of different magnitudes e.g</p>
<pre><code class="lang-auto">loss1 = 0.5
loss2 = 32131313
</code></pre>
<p>I’m looking for an appropriate method of balancing the two losses before adding them together for back propagation. Note that both losses change during training and can be negative in some instances. what method can I use to balance both losses at every instance during training?</p>
Google Colab cudnn RuntimeError: cuDNN error: CUDNN_STATUS_EXECUTION_FAILED

Niraja: <p>I am getting this error while running trained model on google colab.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/0/c/0ca11ac3aba576d38b4d4e7922a6eef5d3059342.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/0ca11ac3aba576d38b4d4e7922a6eef5d3059342" title="error"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/0/c/0ca11ac3aba576d38b4d4e7922a6eef5d3059342_2_690x334.jpeg" alt="error" data-base62-sha1="1NITsDQ04byK6pXrRCFfLR9WAue" width="690" height="334" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/0/c/0ca11ac3aba576d38b4d4e7922a6eef5d3059342_2_690x334.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/0/c/0ca11ac3aba576d38b4d4e7922a6eef5d3059342_2_1035x501.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/0/c/0ca11ac3aba576d38b4d4e7922a6eef5d3059342_2_1380x668.jpeg 2x" data-dominant-color="F7F7F8"><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">error</span><span class="informations">1552×753 191 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>
I tried to search this error that how to solve but I didn’t get any answer. Please tell me how to solve this error. Thank you in advance.</p>

ptrblck: <p>Could you post a minimal and executable code snippet to reproduce the issue and post also the output of <code>python -m torch.utils.collect_env</code>, please?</p>

ConvolutionalAtom: <p>Just as a general advice on colab runtime, try to reproduce your error several times by deleting and re-initializing runtime and re-running code. It can be that the specific runtime at the moment might be problematic. This happened to me a few times</p>
<p>Edit: This is of course unless you can spot the origin of the problem</p>
Disparity between PyTorch and MATLAB Results

Sakib1263: <p>Dear PyTorch Community Members and Experts,</p>
<p>We have been doing some research that involved using Deep Learning Networks to classify images of Chest X-Ray for various diseases (e.g. COVID-19, Viral Pneumonia, SARS, MERS, etc.). To our surprise, we noticed a big gap between the results from MATLAB and PyTorch. We used all the parameters the same during Training or Testing (e.g. Train and Test Data, Learning Rate, Number of Folds, Optimizer, Cost function, etc.). For the same models (e.g. VGG_16), MATLAB was performing significantly better, more than 5% in some cases, which is huge in the extreme (e.g. 90% is much better than 85%). Does anyone have any idea what could be the issue here? We prefer PyTorch results for our paper but MATLAB is performing better.</p>
<p>Best Regards,<br>
SM</p>

Zifei_XU: <p>Hi, Bro. I had a same situation as you met. The result from MATLAB is better than pytorch.<br>
All the sets are same including initial parameters method.</p>

eduardomenezes: <p>I am having exactly the same situation, in a problem of time-series regression using an autoencoder. I have set all the parameters to be the same, but still does not work.<br>
Anyone could know the reason?!</p>
Most efficient way to extract output of every Convolutional layer

pytorchuser101: <p>I want to use hooks to extract the output of every convolutional layer in a CNN. What would be a more efficient way than registering every convolutional layer manually ?</p>
<p>I was thinking of running a for loop with all conv layer names, but even that may require some manual hardcoding the names.</p>

ptrblck: <p>You could iterate all modules and check for their type via e.g.:</p>
<pre><code class="lang-python">model = models.resnet18()

for name, module in model.named_modules():
    if isinstance(module, nn.Conv2d):
        print(f"registering hook for {name}")
        module.register_forward_hook(lambda m, input, output: print(output.shape))

# registering hook for conv1
# registering hook for layer1.0.conv1
# registering hook for layer1.0.conv2
# registering hook for layer1.1.conv1
# registering hook for layer1.1.conv2
# registering hook for layer2.0.conv1
# registering hook for layer2.0.conv2
# registering hook for layer2.0.downsample.0
# registering hook for layer2.1.conv1
# registering hook for layer2.1.conv2
# registering hook for layer3.0.conv1
# registering hook for layer3.0.conv2
# registering hook for layer3.0.downsample.0
# registering hook for layer3.1.conv1
# registering hook for layer3.1.conv2
# registering hook for layer4.0.conv1
# registering hook for layer4.0.conv2
# registering hook for layer4.0.downsample.0
# registering hook for layer4.1.conv1
# registering hook for layer4.1.conv2


x = torch.randn(1, 3, 224, 224)
out = model(x)

# torch.Size([1, 64, 112, 112])
# torch.Size([1, 64, 56, 56])
# torch.Size([1, 64, 56, 56])
# torch.Size([1, 64, 56, 56])
# torch.Size([1, 64, 56, 56])
# torch.Size([1, 128, 28, 28])
# torch.Size([1, 128, 28, 28])
# torch.Size([1, 128, 28, 28])
# torch.Size([1, 128, 28, 28])
# torch.Size([1, 128, 28, 28])
# torch.Size([1, 256, 14, 14])
# torch.Size([1, 256, 14, 14])
# torch.Size([1, 256, 14, 14])
# torch.Size([1, 256, 14, 14])
# torch.Size([1, 256, 14, 14])
# torch.Size([1, 512, 7, 7])
# torch.Size([1, 512, 7, 7])
# torch.Size([1, 512, 7, 7])
# torch.Size([1, 512, 7, 7])
# torch.Size([1, 512, 7, 7])
</code></pre>
Possible typo in the documentation for GRU

amangupta: <p>Hi,</p>
<p>I wanted to point out a possible typo on the website.<br>
<a href="https://pytorch.org/docs/stable/generated/torch.nn.GRU.html" class="onebox" target="_blank" rel="noopener nofollow ugc">https://pytorch.org/docs/stable/generated/torch.nn.GRU.html</a><br>
The hidden state update equation should be ht = (1-zt)<em>h_t-1 + zt</em>nt, not the other way round.</p>
<p>Intuitively, it makes sense that there is a typo, also the paper by the creators of GRU also use the form of the equation that I mention above (ref: <a href="https://arxiv.org/abs/1412.3555" class="inline-onebox" rel="noopener nofollow ugc">[1412.3555] Empirical Evaluation of Gated Recurrent Neural Networks on Sequence Modeling</a>)</p>
<p>Can you please confirm?</p>
<p>Regards,<br>
Aman</p>

ptrblck: <p>Could you create an issue on <a href="https://github.com/pytorch/pytorch/issues">GitHub</a> so that the code owners could take a look at it, please?</p>
SimCLR TRaining

GioSfat: <p>Hello Everyone.<br>
I have an understanding problem with Contrastive Learning and SimCLR framework.<br>
The part of simclr I cannot understand is how you calculate the linear evaluation after you train your model with unlabeled dataset and finding the contrastive loss for every epoch you have set.</p>
<p>I read a lot of articles where they say about creating a Linear Classifier and train it to get the metrics(loss, accuracy etc.) you want. The training of Linear Classifier is the same as we train a classification model, for example:</p>
<pre><code class="lang-auto">for epoch in range (num_epochs):
    trainCorrect = 0
    #Initialize losses 
    batch_train_loss = 0.0
    #Network Training
    ImageClassifier.train()
    for batch in tqdm(train_loader):
      X, y = batch
      X, y = X.to(device), y.to(device)
      output = LinearClassifier(X)
      train_loss = loss_fn(output, y)
      batch_train_loss += train_loss.item()
      optimizer.zero_grad()
      train_loss.backward()
      optimizer.step()
      trainCorrect += (yhat.argmax(1) == y).type(torch.float).sum().item()
</code></pre>
<p>Or you need something else for the metrics?<br>
Thanks a lot in advance.</p>
ValueError: Incompatible dimensions (2, 5) (2, 5)

mnahondo: <p>import cvxpy as cp<br>
import numpy as np</p>
<p>n_users = 5<br>
n_base_stations = 2<br>
P_max = 10  # Maximum transmission power<br>
Gamma_th = 5  # Interference threshold<br>
eta = np.array([1, 2, 3, 4, 5])  # Weighting factors for each user<br>
g = np.random.randn(n_base_stations, n_users) + 1j * np.random.randn(n_base_stations, n_users)  # Channel matrix<br>
r = np.array([2, 1, 3, 2, 1])  # Vector of rate requirements for each user</p>
<h1>
<a name="optimization-variables-1" class="anchor" href="#optimization-variables-1"></a>Optimization variables</h1>
<p>w = cp.Variable((n_base_stations, n_users), complex=True)</p>
<h1>
<a name="customized-satisfaction-level-rate-in-this-case-2" class="anchor" href="#customized-satisfaction-level-rate-in-this-case-2"></a>Customized satisfaction level (rate in this case)</h1>
<p>satisfaction = r / cp.log(1 + cp.abs(g @ w) ** 2)</p>
<h1>
<a name="objective-function-3" class="anchor" href="#objective-function-3"></a>Objective function</h1>
<p>obj = cp.sum(eta * satisfaction)</p>
<h1>
<a name="constraints-4" class="anchor" href="#constraints-4"></a>Constraints</h1>
<p>constraints = []<br>
for j in range(n_base_stations):<br>
constraints.append(cp.sum(cp.square(cp.abs(w[j]))) &lt;= P_max)</p>
<p>for i in range(n_users):<br>
interference_sum = cp.sum([cp.square(cp.abs(cp.sum(g[j, i] * w[j, u] for j in range(n_base_stations)))) for u in range(n_users) if u != i])<br>
constraints.append(interference_sum &lt;= Gamma_th)</p>
<h1>
<a name="optimization-problem-5" class="anchor" href="#optimization-problem-5"></a>Optimization problem</h1>
<p>prob = cp.Problem(cp.Maximize(obj), constraints)</p>
<p>prob.solve()</p>
<p>print(“Optimal beamforming solution:\n”, w.value)</p>
<p>hello friends iam solving a second order cone optimisation problem and i get this error: raise<br>
ValueError: Incompatible dimensions (2, 5) (2, 5)</p>

ptrblck: <p>The question seems to be unrelated to PyTorch so I would recommend posting it into a <code>cvxpy</code>-specific discussion board as you would find the experts there.</p>
Got signal SIGSEGV error when I use NCCL backend for DDP

LuoXin-s: <h3>
<a name="bug-describe-the-bug-1" class="anchor" href="#bug-describe-the-bug-1"></a><img src="https://discuss.pytorch.org/images/emoji/apple/bug.png?v=12" title=":bug:" class="emoji" alt=":bug:" loading="lazy" width="20" height="20"> Describe the bug</h3>
<p>I can run this <a href="https://github.com/pytorch/examples/blob/main/distributed/ddp/main.py" rel="noopener nofollow ugc">code</a> successfully on default gloo backend, and it will fail when I shift to nccl backend.</p>
<p>Note that I have a higher cuda version than pytorch built, however I expect it could work without problem as per <a href="https://discuss.pytorch.org/t/install-pytorch-with-cuda-12-1/174294">Install pytorch with Cuda 12.1 1 </a>.</p>
<p>I have tried to use python 3.8, however the problem persists.</p>
<h3>
<a name="versions-2" class="anchor" href="#versions-2"></a>Versions</h3>
<p>Below is my environments:<br>
PyTorch version: 2.0.0+cu117<br>
CUDA used to build PyTorch: 11.7</p>
<p>OS: Ubuntu 22.04.2 LTS (x86_64)<br>
GCC version: (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0<br>
Libc version: glibc-2.35</p>
<p>Python version: 3.9.12 (main, Apr 5 2022, 06:56:58) [GCC 7.5.0] (64-bit runtime)<br>
Python platform: Linux-5.15.0-60-generic-x86_64-with-glibc2.35<br>
Is CUDA available: True<br>
CUDA runtime version: 12.0.140<br>
CUDA_MODULE_LOADING set to: LAZY<br>
GPU models and configuration:<br>
GPU 0: NVIDIA A100-SXM4-80GB<br>
GPU 1: NVIDIA A100-SXM4-80GB</p>
<p>Nvidia driver version: 525.60.13<br>
cuDNN version: Could not collect<br>
HIP runtime version: N/A<br>
MIOpen runtime version: N/A<br>
Is XNNPACK available: True</p>

LuoXin-s: <p>Solve by set <code>NCCL_NET=Socket</code> according to the suggestion provided by my cluster manager, does not known reasons.</p>

ptrblck: <p>Double post from <a href="https://discuss.pytorch.org/t/how-to-fix-signal-11-sigsegv-problem-when-i-use-ddp/178752">here</a>.</p>
MPS out of memory problem

vladimir.novikov76: <p>Hello I am a novice developer and I am trying to train my model on the mac m1 using mps but I don’t know how to avoid the MPS out of memory error.  There are no methods like empty_cache() in mps, can you please help me with this, I don’t know who to contact. I will be glad for your support and thank you very much for such a wonderful framework</p>
AssertionError: Torch not compiled with CUDA enabled!

levi: <p>ive tried 3 times to install and setup pytorch and cuda and no matter how i go about install it there will be an error in the installation, im trying to use a 4090 on windows pro 64x and it keeps defaulting to cpu which should be fine on this system but i would like it to use cuda</p>

ptrblck: <p>Could you post the used install commands as well as the install log, please?</p>

levi: <p>i pip installed py torch through their websites link i chose 11.8 pip stable , i manually installed the version of cuda my 4090 is usign which was 12.1 . when i try to install pytorch with just pip install pytorch it says the following (  note: This error originates from a subprocess, and is likely not a problem with pip.<br>
ERROR: Failed building wheel for pytorch<br>
Running setup.py clean for pytorch<br>
Failed to build pytorch<br>
ERROR: Could not build wheels for pytorch, which is required to install pyproject.toml-based projects)<br>
i have the latest drivers, could it be that im not installing it to a new environment?</p>

ptrblck: <p>It seems you are somehow trying to <strong>build</strong> PyTorch instead of just installing the binaries.<br>
Since you didn’t post the install command I wouldn’t know what went wrong and how to fix it.</p>
Is cuda 12.1 compatible for my geforce gtx 1050 Ti , which cudnn to use and nvidia driver. what to do please

mirz: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/b/a/ba5bf85e6c6773534aafcec293ddd6870fc84c44.png" data-download-href="https://discuss.pytorch.org/uploads/default/ba5bf85e6c6773534aafcec293ddd6870fc84c44" title="ccccccccCapture"><img src="https://discuss.pytorch.org/uploads/default/original/3X/b/a/ba5bf85e6c6773534aafcec293ddd6870fc84c44.png" alt="ccccccccCapture" data-base62-sha1="qABQZzJQGSs4SgiZqQ7vFghCm1e" width="690" height="313" data-dominant-color="141414"><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">ccccccccCapture</span><span class="informations">954×433 11 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>
<p>NVIDIA-SMI 531.29                 Driver Version: 531.29       CUDA Version: 12.1<br>
pytorch 2.0</p>
<p>how do i use my Nvidia Geforce GTX 1050 Ti  , what are the things and steps  needed to install and executed</p>

ptrblck: <p>Yes, the driver is compatible with your Pascal GPU and you can just install the pip wheel or conda binary as described <a href="https://pytorch.org/get-started/locally/">here</a>.<br>
Note that the binaries ship with their own CUDA libs and dependencies. Your locally installed CUDA toolkit will be used if you build PyTorch from source or a custom CUDA extension.</p>

mirz: <p>CAN anyone guide me to exact steps of installation from scratch as i have removed/uninstalled cuda12.1.<br>
so i have only nvidia driver {Driver Version: 531.29} installed in my laptop, also i have python3.9.13 installed. Also  i am using pytorch2.0(cpu).<br>
what i have to install for my Nvidia Geforce GTX 1050 Ti , which version cuda, cudnn etc</p>

ptrblck: <p>Just visit the link from my previous post, select the desired PyTorch version, OS, Package manager, CUDA, copy/paste the command, and execute it in your terminal.</p>
<p>E.g. to install PyTorch 2.0.0 with CUDA 11.7 as a pip wheel on Linux, run <code>pip install torch</code>.</p>
I need your help

jose_victor: <p>hello, at start of story i’m starting on this wonderful world of pytorch and AI, i made my frist Neural Network (NN), and in the moment of the test she give that error</p>
<pre><code class="lang-auto">'bool' object is not iterable
  File "File_path(i just change to send that error, all the paths are ok)", line 36, in train
    for i, data in enumerate(dataloader):
  File "File_path(i just change to send that error, all the paths are ok)", line 11, in &lt;module&gt;
    model.eval()
TypeError: 'bool' object is not iterable```</code></pre>

marksaroufim: <p>Hi <a class="mention" href="/u/jose_victor">@jose_victor</a> welcome to our forum, typically it’s easier for us to help debug if you can share a minimal repro. In your case the problem seems to be that the <code>dataloader</code> object is a boolean and indeed you cannot iterate over that. I’d double check how it was created</p>

jose_victor: <p>thanks guy, well, i know the dataloader who i use on test make this error, but to training the NN i create a dataloader that same form of the dataloader from test, and idk why the test dataloader to test make this error and the train not, why both are created of the same form</p>
Torch.compile interaction with autocast and no_grad

vadimkantorov: <p>Where should I put <code>no_grad</code> and <code>autocast</code> contexts? Inside the function that will be <code>torch.compile</code>’d? Or should I <code>torch.compile</code> first and then wrap the calls with <code>autocast</code> / <code>no_grad</code>?</p>
<p>In theory knowing the autocast context and if we don’t need to save intermediate tensors can lead to stonger optimization and more aggressive inplace / economic memory placement</p>

bdhirsh: <p>Today, if you try to <code>torch.compile</code> a module / function that internally uses <code>autocast</code>/<code>no_grad</code> context managers, dynamo will graph break on them.</p>
<p>So I’d recommend putting them outside for now:</p>
<pre><code class="lang-auto">@torch.compile
def f(args):
    ...

with torch.cuda.amp.autocast():
    out = f(...)

with torch.no_grad():
    out = f(...)
</code></pre>
<p>I think we want to fix this though, and avoid graph breaking on these context managers. So longer term the answer is “it shouldn’t matter” - feel free to file an issue though!</p>

vadimkantorov: <p>Thank you! Created a sister discussion in <a href="https://github.com/pytorch/pytorch/issues/100241" class="inline-onebox" rel="noopener nofollow ugc">Interaction of torch.no_grad and torch.autocast context managers with torch.compile · Issue #100241 · pytorch/pytorch · GitHub</a></p>
Libtorch mingw64

nerc: <p>hello,<br>
I would like to integrate pytorch features to a c++ project I have under mingw64, I have exported and have it coded in .pt format, I’m unable to compile and link it using libtorch for windows.<br>
I have succeeded the same under ubuntu.<br>
is there dedicated libtorch for mingw? how should I proceed? thanks in advance for your help.</p>
Simple regression problem - NN will not converge

endrew: <p>I am training a neural network to predict the frequency of a sine wave. What happens is that the network learns to always predicts the mean value of the labels (the frequencies in the training data). What am I doing wrong?</p>
<p>Please see google colab code here:</p><aside class="onebox allowlistedgeneric" data-onebox-src="https://colab.research.google.com/drive/1S--ze44lLqM1hoz_SjlNCAY99JCwTApn?usp=sharing">
  <header class="source">
      <img src="https://ssl.gstatic.com/colaboratory-static/common/7eb5b53986a0c4bf9c4183ea7e84af3b/img/favicon.ico" class="site-icon" width="128" height="128">

      <a href="https://colab.research.google.com/drive/1S--ze44lLqM1hoz_SjlNCAY99JCwTApn?usp=sharing" target="_blank" rel="noopener nofollow ugc">colab.research.google.com</a>
  </header>

  <article class="onebox-body">
    <img src="https://colab.research.google.com/img/colab_favicon_256px.png" class="thumbnail onebox-avatar" width="260" height="260">

<h3><a href="https://colab.research.google.com/drive/1S--ze44lLqM1hoz_SjlNCAY99JCwTApn?usp=sharing" target="_blank" rel="noopener nofollow ugc">Google Colaboratory</a></h3>



  </article>

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

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

<p>Thanks!</p>

eqy: <p>The warning given:</p>
<pre><code class="lang-auto">/usr/local/lib/python3.10/dist-packages/torch/nn/modules/loss.py:536: UserWarning: Using a target size (torch.Size([10])) that is different to the input size (torch.Size([10, 1])). This will likely lead to incorrect results due to broadcasting. Please ensure they have the same size.
  return F.mse_loss(input, target, reduction=self.reduction)
</code></pre>
<p>is important here, as the MSE calculation will incorrectly broadcast <code>yhat - y_batch</code> to a shape of <code>[10, 10]</code> during the difference calculation before computing the mean. This broadcasting causes MSE to incorrectly exhibit the averaging effect observed as it would compute the “mean squared error” between each data point and <em>all</em> of the labels, not just the corresponding one.</p>
<p>By fixing the output shape with<br>
<code>yhat = yhat.squeeze()</code><br>
I get</p>
<pre><code class="lang-auto">tensor(0.0034, grad_fn=&lt;MseLossBackward0&gt;)
tensor(0.0014, grad_fn=&lt;MseLossBackward0&gt;)
tensor(0.0003, grad_fn=&lt;MseLossBackward0&gt;)
tensor(0.0030, grad_fn=&lt;MseLossBackward0&gt;)
tensor(0.0097, grad_fn=&lt;MseLossBackward0&gt;)
tensor(0.0008, grad_fn=&lt;MseLossBackward0&gt;)
tensor(0.0006, grad_fn=&lt;MseLossBackward0&gt;)
tensor(0.0005, grad_fn=&lt;MseLossBackward0&gt;)
tensor(0.0010, grad_fn=&lt;MseLossBackward0&gt;)
tensor(0.0005, grad_fn=&lt;MseLossBackward0&gt;)
</code></pre>
<p><img src="https://discuss.pytorch.org/uploads/default/original/3X/a/1/a12a284d3403ba3c1928af7ddee40d0964bbe7b8.png" alt="image" data-base62-sha1="mZJc46gQEjEeOgLTf7P0d3pFFkk" width="543" height="413"></p>

endrew: <p>That’s awesome! Thanks a lot!!!</p>
Why does Torch computation operation call DtoH memcpy?

rhwang: <p>Here’s a code example.</p>
<pre><code class="lang-auto">import torch
import torch.cuda.nvtx as nvtx_cuda

A = torch.rand(size=(1024*4, 1024*4), device="cuda")
B = torch.rand(size=(1024*4, 1024*4), device="cuda")
C = torch.rand(size=(1024*4, 1024*4), device="cuda")
D = torch.rand(size=(1024*4, 1024*4), device="cuda")
E = torch.rand(size=(1024*4, 1024*4), device="cuda")
F = torch.rand(size=(1024*4, 1024*4), device="cuda")

torch.cuda.cudart().cudaProfilerStart()

idx_mask = torch.rand(1024, device="cuda")
active_idx = torch.rand(1024, device="cuda")
nvtx_cuda.range_push("S0")
active_idx = torch.nonzero(idx_mask, as_tuple=True)
nvtx_cuda.range_pop()

nvtx_cuda.range_push("S1")
C = A.matmul(B)
F = D.matmul(E)
nvtx_cuda.range_pop()

print(active_idx)

torch.cuda.cudart().cudaProfilerStop()

</code></pre>
<p>Regarding the code, I’m very curious about why <code>active_idx = torch.nonzero(idx_mask, as_tuple=True)</code> calls CudaMemcpyAsync.</p>
<p>Here’re my points.</p>
<ul>
<li>
<code>idx_mask</code> and <code>active_idx</code> are both on the GPU first.</li>
<li>So, all operations should be conducted on a GPU (independent of the CPU).</li>
<li>I suspect that the python <code>torch.nonzero</code> calls a CUDA kernel that does a non-zero operation in C++ within its underneath code because, in any way, the kernel should be called by the CPU thread. For example, GPU should send something like a pointer to the CPU, and the CPU launches the kernel.</li>
<li>So I make an example that calls a GEMM kernel, but that kernel does not call any memory operation (This also does the GEMM operation for tensors on a GPU).</li>
<li>In summary, what I want to do is eliminate the memory operation (GPU-&gt;CPU), making it independent of the CPU or at least make it to be a non-blocking operation which means eliminating the cudaSyncThread (the green block in the figure).</li>
</ul>
<p><a href="https://i.stack.imgur.com/YVbaC.png" rel="noopener nofollow ugc"><img src="https://i.stack.imgur.com/YVbaC.png" alt="enter image description here" width="690" height="166"></a><br>
This is a non-zero operation.</p>
<p><a href="https://i.stack.imgur.com/IkJsc.png" rel="noopener nofollow ugc"><img src="https://i.stack.imgur.com/IkJsc.png" alt="enter image description here" width="690" height="115"></a><br>
This is a GEMM kernel.</p>
<p><strong>Please note that the cudaMemasync is not called by the GEMM kernel in Figure 2.</strong></p>

eqy: <p>Have you experimented to see what conditions produce/don’t produce the <code>cudaMemcpyAsync</code> call in <code>S1</code> (e.g., removing the print statement / removing the matmul calls)?</p>

ptrblck: <p><code>torch.nonzero()</code> causes a host-device synchronization as described in the <a href="https://pytorch.org/docs/stable/generated/torch.nonzero.html#torch.nonzero">docs</a> as, I believe, the size of the output tensor has to be computed on the host first since its data-dependent.</p>

rhwang: <p>Thanks, I really appreciate it.</p>
How to use 1 .pth file to create multi-model on singel-gpu?

nistarlwc: <p>try to create 2 models but them use same gpu-memory.</p>
<pre><code class="lang-auto">model1 = torch.jit.load(CHECKPOINT_DIR)
model2 = torch.jit.load(CHECKPOINT_DIR)
with torch.no_grad():
        im_gpu1 = im_in.cuda()
        output1 = model1.forward(im_gpu1)
        im_gpu2 = im_in.cuda()
        output2 = model2.forward(im_gpu2)
</code></pre>
<p>when run 1 model, use 2G gpu-memory, I want to create more models to use more gpu-memory.<br>
but when run 2 model, use 2G gpu-memory too, the second model don’t use extra more gpu-memory, why?</p>
Problem with the torch_Dataloader_OASIS function, specifically with the drop_last=True parameter

Naeem_Hussain: <p>I am working on Unsupervised Medical Images Segmentation unable to set the path according to database. It seems like that Dataloader function is not working properly. Below is my code.</p>
<p><span class="hashtag">#The</span> path accordingly<br>
OASIS_img_dir = ‘i set the path according to my directory’<br>
OASIS_seg_dir = ‘i set the path according to my directory’<br>
resized_shape = (80, 96, 112</p>
<p>train_OASIS = torch_Dataloader_OASIS(OASIS_img_dir, OASIS_seg_dir, ‘train’, resized_shape, 1, None)<br>
test_OASIS = torch_Dataloader_OASIS(OASIS_img_dir, OASIS_seg_dir, ‘test’, resized_shape, 1, None))</p>
<p>Got this error:<br>
line 22, in <br>
train_OASIS = torch_Dataloader_OASIS(OASIS_img_dir, OASIS_seg_dir, ‘train’, resized_shape, 1, None)</p>
Building local html - no module named pytorch_sphinx_theme

MylesJP: <p>Hi all, I am trying to build local documentation on my Windows system with Anaconda but I am getting an error “No module named pytorch_sphinx_theme” when I try to do so. I have everything from requirements.txt installed and I get this error on my Linux system as well. Here is the traceback:<br>
Traceback (most recent call last):<br>
File “C:\Users\Myles\AppData\Local\Programs\Python\Python311\Lib\site-packages\sphinx\config.py”, line 343, in eval_config_file<br>
exec(code, namespace)<br>
File “C:\Windows\System32\pytorch\docs\source\conf.py”, line 38, in <br>
import pytorch_sphinx_theme<br>
ModuleNotFoundError: No module named ‘pytorch_sphinx_theme’</p>
<p>I’ve tried running “pip install pytorch_sphinx_theme” but that didn’t seem to work. Any advice would be much appreciated! Thanks in advance.</p>

ptrblck: <p>The requirement is specified <a href="https://github.com/pytorch/pytorch/blob/d1fbd33c70205d7567c9e8f070bbbd5fc22d4a41/docs/cpp/requirements.txt#L6">here</a> and I would assume you could also build it yourself from <a href="https://github.com/pytorch/pytorch_sphinx_theme">this repository</a>.</p>
Why is there a special case for 2D in nn.Softmax with dim=None?

gengrill: <p>Hi,</p>
<p>I have a question about <a href="https://github.com/pytorch/pytorch/blob/be8c7c06b6d74c168d358b3887db26b82e83e421/torch/nn/functional.py#L1858" rel="noopener nofollow ugc">this code</a>:</p>
<pre><code class="lang-auto">if ndim == 0 or ndim == 1 or ndim == 3:
    ret = 0
else:
    ret = 1
</code></pre>
<p>Why are we picking 1 as a dimension for the 2D case?</p>

ptrblck: <p>From the Slack cross-post:</p>
<blockquote>
<p>I would guess because 2D tensors often represent <code>[batch_size, nb_classes]</code> where <code>dim1</code> contains the logits/features/etc.</p>
</blockquote>

gengrill: <p>Thank you, that is helpful!</p>
Yolov8 people count

Ahmed_Khalifa_Al_Man: <p>greetings<br>
I have been having issues with the code below ,i need it to show the pedestrian count and remove the boxes and labels with no success we have trained ultralytics data set on visdrone using yolov8x and was trained on local machine, part two of the question is how to increase the accuracy during training. I have epoch set to 100 if we increase it to 200 or 500 will i get better trained model.i am using thonny IDE<br>
please Advise<br>
import cv2<br>
import cvlib as cv<br>
import pandas as pd<br>
import numpy as np<br>
from vidgear.gears import CamGear<br>
from cvlib.object_detection import draw_bbox<br>
from ultralytics import YOLO<br>
frame = stream.read()<br>
count += 1<br>
if count % 5 != 0:<br>
continue</p>
<p>frame = cv2.resize(frame, (1020, 600))<br>
bbox, label, conf = cv.detect_common_objects(frame)<br>
frame = draw_bbox(frame, bbox, label, conf)</p>
<h1>
<a name="object-counting-1" class="anchor" href="#object-counting-1"></a>Object counting</h1>
<p>car_count = label.count(‘car’)<br>
pedestrian_count = label.count(‘pedestrian’)<br>
person_count = label.count(‘person’)<br>
truck_count = label.count(‘truck’)<br>
m_cycle_count = label.count(‘motorcycle’)</p>
<p>cv2.putText(frame, f’Cars: {car_count}‘, (30, 480), cv2.FONT_HERSHEY_PLAIN, 2, (255, 255, 255), 2)<br>
cv2.putText(frame, f’People: {person_count}’, (30, 510), cv2.FONT_HERSHEY_PLAIN, 2, (255, 255, 255), 2)<br>
cv2.putText(frame, f’Trucks: {truck_count}‘, (30, 544), cv2.FONT_HERSHEY_PLAIN, 2, (255, 255, 255), 2)<br>
cv2.putText(frame, f’Motorcycles: {m_cycle_count}’, (30, 574), cv2.FONT_HERSHEY_PLAIN, 2, (255, 255, 255), 2)<br>
cv2.putText(frame, f’Pedestrians: {pedestrian_count}', (30, 400), cv2.FONT_HERSHEY_PLAIN, 2, (255, 255, 255), 2)</p>
<p>results = model.predict(frame, show=True)</p>
<p>if cv2.waitKey(1) &amp; 0xFF == 27:<br>
break</p>
Nvidia-smi equivalent for M1/M2 pro

overfitted: <p>Hi,</p>
<p>I very recently bought the MBP M2 Pro and I have successfully installed PyTorch with MPS backend. Nevertheless, I couldn’t find any tool to check GPU memory usage from the command line. Does anyone know if there is any tool available for Apple Silicon GPUs equivalent to nvidia-smi? Thanks!</p>

marksaroufim: <p>The builtin activity monitor works</p>

Hayao41: <p>Apple silicon uses RAM and swap as the memory of GPU. Try the ‘htop’ cmd to monitor your resources.</p>
<pre><code class="lang-auto">brew install htop
htop
</code></pre>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/f/b/fbdc5740ba9ba1d73412394da52c756d3fe3666e.png" data-download-href="https://discuss.pytorch.org/uploads/default/fbdc5740ba9ba1d73412394da52c756d3fe3666e" title="Screenshot 2023-04-28 at 00.04.21"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/f/b/fbdc5740ba9ba1d73412394da52c756d3fe3666e_2_690x60.png" alt="Screenshot 2023-04-28 at 00.04.21" data-base62-sha1="zW3Vexmrj3B2SLKeBWtLzLiUfD0" width="690" height="60" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/f/b/fbdc5740ba9ba1d73412394da52c756d3fe3666e_2_690x60.png, https://discuss.pytorch.org/uploads/default/optimized/3X/f/b/fbdc5740ba9ba1d73412394da52c756d3fe3666e_2_1035x90.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/f/b/fbdc5740ba9ba1d73412394da52c756d3fe3666e_2_1380x120.png 2x" data-dominant-color="1B211F"><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">Screenshot 2023-04-28 at 00.04.21</span><span class="informations">2039×179 167 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>
A metal backend PyTorch build from source guide, co-worked with ChatGPT

Hayao41: <p>Visit this link to access the guide: <a href="https://www.notion.so/Build-METAL-Backend-PyTorch-from-Source-359cd77337d54ce0a55c74efd9f376a5" rel="noopener nofollow ugc">Build METAL Backend PyTorch from Source</a>. This beginner-friendly tutorial will walk you through the process of building from source. Keep in mind that you may need to modify some steps based on your specific version and platform. Enjoy and have fun!</p>
Questions about Equalization

cokespace2: <p>There’re kinds of <code>Configs</code> within quantization, for example we have <code>backend_config</code> that specify which platform we are using, and <code>QConfigMapping</code>, but I still cannot fully understand that what the <code>EqualiztionConfig</code> suppose to do.</p>
<p>In many sample codes, this just a <code>None</code> input, and pytorch would generate the default one, can anyone share some informations about this?</p>

jcaip: <p>Hi <a class="mention" href="/u/cokespace2">@cokespace2</a> ,</p>
<p>Could you link me to the EqualizationConfig you are referring to? I am not too familiar with this part of the codebase.</p>
<p>Thanks,<br>
Jesse</p>

cokespace2: <p>Sure, for example the EqualizationConfig would be using here: <a href="https://github.com/pytorch/pytorch/blob/main/torch/ao/quantization/quantize_fx.py#L98" class="inline-onebox" rel="noopener nofollow ugc">pytorch/quantize_fx.py at main · pytorch/pytorch · GitHub</a></p>

jcaip: <p>I think it’s a way to implement the weight equalization described in this paper: <a href="https://openaccess.thecvf.com/content_ICCV_2019/papers/Nagel_Data-Free_Quantization_Through_Weight_Equalization_and_Bias_Correction_ICCV_2019_paper.pdf" rel="noopener nofollow ugc">https://openaccess.thecvf.com/content_ICCV_2019/papers/Nagel_Data-Free_Quantization_Through_Weight_Equalization_and_Bias_Correction_ICCV_2019_paper.pdf</a></p>
<p>I don’t think you need to worry about this flag unless you’re looking to do weight equalization.</p>

cokespace2: <p>Thanks lot!<br>
Then I should leave it be, thanks again.</p>
Module 'torch.backends.cudnn' has no attribute 'get_handle'

Jianduo: <p>I didn’t find any other meeting the same issue. My cuda and cudnn versions are:<br>
torch.cuda.is_available()<br>
True<br>
torch.version.cuda<br>
‘10.2’<br>
torch.backends.cudnn.is_available()<br>
True<br>
torch.backends.cudnn.version()<br>
7605</p>
<p>When try to train a model with LSTM, I got an error as follow:<br>
File “D:/mlearning/model_code\hydroDL\model\rnn.py”, line 301, in forward<br>
handle = torch.backends.cudnn.get_handle()</p>
<p>File “D:\anaconda3\envs\pytorch-gpu\lib\site-packages\torch\backends_<em>init</em>_.py”, line 47, in <strong>getattr</strong><br>
return self.m.<strong>getattribute</strong>(attr)</p>
<p>AttributeError: module ‘torch.backends.cudnn’ has no attribute ‘get_handle’</p>
<p>Look forward to help.</p>
<p>Many thanks!</p>
<p>Jianduo</p>

ptrblck: <p><code>torch.backends.cudnn.get_handle()</code> was removed in PyTorch <code>1.5.0</code> in <a href="https://github.com/pytorch/pytorch/pull/33678">this PR</a> so you might need to update your code to be compatible with the latest PyTorch release.</p>

Jianduo: <p>Thanks! But I’m a new user of Python, could I use PyTorch 1.4 instead?</p>

ptrblck: <p>You could downgrade, but I wouldn’t recommend doing so as you might be running into already fixed issues and would miss the latest features.<br>
What’s the use case and why do you need to access the handle?</p>

Jianduo: <p>Hi ptrblck,</p>
<p>Thanks for your help. In fact, I would like to use the code from others who published a paper in Nature Communication. In this paper, they used LSTM to train a model.</p>
<p>Due to the PyTorch version, there are a lot of “no attribute” errors.</p>
<p>I should learn more about the difference between versions of PyTorch now.</p>
Old problem but strange things:Trying to backward through the graph a second time

andy_sherlock: <p>old problem but I didn’t use the intermediate results or calculate anything.<br>
below is the code<br>
and I found unexpectedly when I remove the sentence of</p>
<pre><code class="lang-auto">            for p in llist:
                print(p)
</code></pre>
<p>the code work!<br>
but with the print(p) I will encounter this problem.<br>
I wonder what happened?</p>
<pre><code class="lang-auto"> for epoch in range(epochs):

            target_model.train()
            for idx,(data, label) in enumerate(self.holded_dataloader):
                optimizer.zero_grad()

                z = self.GM_model.G.gen_noise(label.size()[0]).cuda()
                trigger = self.GM_model.G(z)
                data, label = data.cuda(), label.cuda()
                                
                poisoned_data = data.detach().clone()
                poisoned_data = self.GM_model.add_trigger( poisoned_data, self.GM_model.transform(
                    (trigger).view(-1, (1 if self.args.dataset=='mnist' else 3), self.args.ylen, self.args.xlen),
                    self.GM_model.dataset_stats(self.args.dataset)), self.args)
                
                ploss = lossfunc(target_model(poisoned_data), label.detach().clone().fill_(self.target_class))
                bloss = self.unlearning_step(target_model, self.teacher, poisoned_data.clone(), label.clone() , optimizer, self.args.device, self.KL_temperature)
                
                loss = beta*bloss- p*ploss
                
                llist.append(loss)
                
                loss.backward()
                optimizer.step()     
          
            # print('loss is ',end='')
            # for p in llist:
            #     print(p)
            # print()
</code></pre>
<p>the error report is<br>
Trying to backward through the graph a second time (or directly access saved tensors after they have already been freed). Saved intermediate values of the graph are freed when you call .backward() or autograd.grad(). Specify retain_graph=True if you need to backward through the graph a second time or if you need to access saved tensors after calling backward.</p>

ptrblck: <p>This reminds me of <a href="https://discuss.pytorch.org/t/error-with-view-no-grad-and-inplace-modify/173082">this issue</a> which raised a similar error.</p>
<p>Could you add the missing code parts to make the snippet executable, please?</p>

andy_sherlock: <p>Really thanks for the reply!<br>
The code I put forwards recently was modified, so I can’t reproduce it either.<br>
As I was not available these days, I will check it out and try to reproduce it later.<br>
Thanks anyway for your reply!</p>