Autograd error: One of the differentiated Tensors appears to not have been used in the graph. Set allow_unused=True if this is the desired behavior

username: SaiAshish
cooked: <h1>for the calculation of classification loss on the augmented dataset</h1>
<h1>define calc_loss_aug</h1>
<p>def calc_loss_aug(input, labels, bart_model, classifier, max_length = 50):<br>
# get the tokenizers<br>
bart_tokenizer = bart_model.tokenizer</p>
<pre><code>classifier_tokenizer = classifier.tokenizer

# convert input to the bart encodings

sentence = [classifier_tokenizer.decode(g, skip_special_tokens=True, clean_up_tokenization_spaces=False) for g in input]

encoding = bart_tokenizer(sentence, return_tensors='pt', padding=True)

input_ids_bart = torch.nn.Parameter(encoding['input_ids'].float())

attention_mask_bart = torch.nn.Parameter(encoding['attention_mask'].float())

bart_logits = bart_model(input_ids_bart.long(), attention_mask_bart.long(),
 target_ids = input_ids_bart[:,:max_length].long(), decoder_attention_mask = attention_mask_bart[:,:max_length].long()).logits

# find the decoded vector from probabilities
[ _ , summary_ids] = bart_logits.max(dim = -1)

# classifier
out = [bart_tokenizer.decode(g, skip_special_tokens=True, clean_up_tokenization_spaces=False) for g in summary_ids]

encoding = classifier_tokenizer(out, return_tensors='pt', padding=True)

input_ids_classifier = torch.nn.Parameter(encoding['input_ids'].float())

attention_mask_classifier = torch.nn.Parameter(encoding['attention_mask'].float())

loss = classifier(input_ids_classifier.long(), attention_mask_classifier.long(), labels = labels).loss

return loss
</code></pre>
<p>When I run</p>
<p>vector_t_dash = torch.autograd.grad(loss, bart_model.bart_model.model.encoder.parameters(), retain_graph = True),</p>
<p>I am getting the following error, and I have no idea how to solve it?</p>
<p>RuntimeError: One of the differentiated Tensors appears to not have been used in the graph. Set allow_unused=True if this is the desired behavior.</p>
<p>Can anyone help to solve it?</p>

username: albanD
cooked: <p>Hi,</p>
<p>If you expect that some of the parameters in the model are not used, then you can pass <code>allow_unused=True</code> as suggested in the error message.<br>
Otherwise you want to make sure that you only do differentiable ops with Tensors that can require gradients. In particular, only floating point Tensors can require gradients</p>

username: SaiAshish
cooked: <p>How do I pass a tensor output which is with respect to one tokenizer(BART) to another tokenizer(XLNet) without converting it to string because this is where the model is turning out to be non-differentiable?</p>
<p>I have output from the BART model which has to be passed to the XL-Net model. But to do so I have to change the output from BART to the format needed by XL-Net, so I have to convert it to string and pass it through an XL-Net tokenizer. However, when using autograd w.r.t BART model it is becoming a non-differentiable unit.</p>
<p>A way around will be helpful.</p>

username: albanD
cooked: <p>Hi,</p>
<p>If your op is not differentiable, there isn’t much we can do here.<br>
If for your particular use case, you want to specify a special backward for that part that is not differentiable, you can do so with a custom Function: <a href="https://pytorch.org/docs/stable/notes/extending.html" class="inline-onebox">Extending PyTorch — PyTorch 1.7.1 documentation</a></p>

username: SaiAshish
cooked: <p>Hey, Thank you! This is useful.</p>
<p>I found a simple way around for my task, and I have to use argmax in the loss function.<br>
However, I need to find a way to include a differentiable argmax operator. Can you help me in this regard?</p>
<p>Thank you again <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

username: albanD
cooked: <p>Well it is not differentiable either so you either have to use a “soft” version of it if you can like softmax. Or a custom function as I mentionned above.</p>