The for loop could probably be reduced to this:
n = 10
s = torch.arange(n, dtype=torch.float)
g = torch.arange(n, dtype=torch.float)
torch.exp(s + g.view(n, 1)).sum([0, 1])

Could you check, if this gives the right answer? It should be faster than the for loops.