This also fails
trans_matrix = torch.zeros((trans_params.shape[0], 2, 3)).to(trans_params.device)
trans_matrix[:,0,0] = trans_params[:, 1] * torch.cos(trans_params[:, 0])
trans_matrix[:,0,1] = trans_params[:, 1] * -1 * torch.sin(trans_params[:, 0])
trans_matrix[:,0,2] = trans_params[:, 2]
trans_matrix[:,1,0] = trans_params[:, 1] * torch.sin(trans_params[:, 0])
trans_matrix[:,1,1] = trans_params[:, 1] * torch.cos(trans_params[:, 0])
trans_matrix[:,1,2] = trans_params[:, 3]

with

RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation

but interestingly not in the first line, but in
trans_matrix[:,1,1] = trans_params[:, 1] * torch.cos(trans_params[:, 0])

Could it be that the actual error is not in this code snipped but somewhere earlier in my code? How can I find out what is going wrong?