I am trying to sum a tensor over its first n axes, where n is a parameter I don’t know in advance. Why something like the following doesn’t work?

v = torch.randn(100, 20, 10)
torch.sum(v, range(2)) # I would write range(n) here.

This gives an error:

TypeError: sum() received an invalid combination of arguments - got (Tensor, range), but expected one of:
* (Tensor input, torch.dtype dtype)
didn't match because some of the arguments have invalid types: (Tensor, range)
* (Tensor input, tuple of names dim, bool keepdim, torch.dtype dtype, Tensor out)
* (Tensor input, tuple of ints dim, bool keepdim, torch.dtype dtype, Tensor out)

However if I instantiate the range:

v = torch.randn(100, 20, 10)
torch.sum(v, list(range(2)))

Then it works. But this seems inefficient for large n.

In general Tensors with a very large number of dimensions are not as efficient. Cuda will limit you to 25 dimensions if I recall correctly. And on cpu, you might hit slowdowns.
But to answer your question, sum does expect a list/tuple and does not accept a generator (what range returns).

Yes, it’s a different argument, sorry. The efficiency seemed more important to me at first but as you explained that’s not so important. But even if that doesn’t matter, convenience is also a plus . I have updated the Github issue.