Code guidance needed, please


(Vitaliy Bondarenko) #1

Hi Guys,

I am Vitaliy, new here. I reported a bug recently https://github.com/pytorch/pytorch/issues/13296#issuecomment-436412021 and would like to work on a fix. Since I am new to C->Python i need some guidance. It would be a great help, if you can point me how the call to torch.Tensor.sum is propagated to C code and where actual summation is happening.

thank you!


(Alban D) #2

I’m not really sure you can fix that in a meaningful manner.
You don’t want to accumulate in a ByteTensor as this would most likely overflow.
What is your plan to fix this?


(Vitaliy Bondarenko) #3

I would like to look at the code first. Could you help point me to the C code?


(Vitaliy Bondarenko) #4

without looking at the code i was thinking about sliced sum as I recently did on my contribution to fast.ai library


(Alban D) #5

Sorry I was away for a few days.

The sum operation is defined in this file in the backend.
There are few levels of indirection before you get there though especially for type dispatching. I’m don’t remember all of them atm. But my best bet is that the accumulation type for ByteTensor is defined as LongTensor and so the casting is done and the op is done as if you had a LongTensor from the beginning.

I’m not familiar with what you call sliced sum here? How would that prevent overflows?


(Vitaliy Bondarenko) #6

Hi,

sorry for so delayed reply.
Problem I had:
compute the sum along 3rd dimension of a tensor 340x340x700k. When tryint to do this, i got overflow message because pytorch tries to convert a tensor to a LongTensor.
Solution
iterate throug slices, for example 340x340x1000, sim them along 3rd dimension and add to the result tensor of size 340x340.

I wend through the code and it looks like the change might require huge amount of effort. but the problem is clear: it is useless to allocate memory for all tensor dimensions when summing it up.


(Alban D) #7

Ho I think I misunderstood you earlier.
You mean that it is useless to change the type of the original big tensor and only the small result tensor should have the right type?
To be clearer, when doing b = a.sum(-1), a should not be converted to long, then the sum on long be called. But the sum on ByteTensor should work with a as is and accumulate in a long tensor?


(Vitaliy Bondarenko) #8

Yes, exactly! This is what I mean, since it is just a waste of memory and in case of huge tensor it is critical (cannot perform a sum operation).


(Alban D) #9

The new TensorIterator might help you do that cleanly.
Maybe @colesbury can give you pointers on how to do that?