for the calculation of classification loss on the augmented dataset
define calc_loss_aug
def calc_loss_aug(input, labels, bart_model, classifier, max_length = 50):
# get the tokenizers
bart_tokenizer = bart_model.tokenizer
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

When I run
vector_t_dash = torch.autograd.grad(loss, bart_model.bart_model.model.encoder.parameters(), retain_graph = True),
I am getting the following error, and I have no idea how to solve it?
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.
Can anyone help to solve it?