Looking further into the code, it seems you are calling detach() multiple times, e.g. also in torch_values.detach(), are calling item() in value.item(), and are recreating tensors, which will also break the computation graph in:
torch_values = torch.Tensor(values).view(-1)
torch_rewards = torch.Tensor(rewards)
torch_logprobs = torch.Tensor(logprobs)
returns = torch.tensor(returns, requires_grad = True).view(-1)

All these operations will create new tensors without a history and Autograd won’t be able to calculate the gradients for all preceding operations.
Instead of recreating the tensors you should just use them in all further operations.