Thank you for the reply.
I ran the code with anomaly mode enabled
with autograd.detect_anomaly():
              (last_observation_tens, action_tens, reward_tens, observation_tens)=self.get_sample()
              target = reward_tens + self.gamma *torch.max(self.model_target(observation_tens, self.G) + observation_tens * (-1e5), dim=1)[0]
              current_q_values= self.model(last_observation_tens, self.G)
              next_q_values=current_q_values.clone()
              current_q_values[range(self.minibatch_length),action_tens,:] = target
              L=self.criterion(current_q_values,next_q_values)
              print('loss:',L.item())
              self.optimizer.zero_grad()
              L.backward(retain_graph=True)
              self.optimizer.step()'''

and got the following output
/pytorch/torch/csrc/autograd/python_anomaly_mode.cpp:40: UserWarning: No forward pass information available. Enable detect anomaly during forward pass for more information.
How do I enable detect anomaly during forward pass?
I also updated the question with the full stack trace.