Sum a tensor over a range of axes

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.

Hi,

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).

1 Like

Thanks. But there is no fundamental reason why it could not accept a range, right?

I opened a feature request issue:

I don’t think there is any reason for it to accept the range because it will check all the values before doing work.

Convenience? range(n) is better to type and read than list(range(n)).

Right. But that is a different argument than the one you mentionned above which was " But this seems inefficient for large n .".

1 Like

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 :wink:. I have updated the Github issue.

1 Like