Size the interpolate flops from the whole output tensor - #8394
Size the interpolate flops from the whole output tensor#8394vineethsaivs wants to merge 2 commits into
Conversation
Signed-off-by: Vineeth Sai <vineethsai4444@gmail.com>
ebarkhordar
left a comment
There was a problem hiding this comment.
The size fix looks right. All eight of your parametrizations match result.numel() at a356a4f, in a clean python:3.11-slim container on torch 2.14.0+cpu.
The control is the part I would look at again. scale_factor only agrees when the scales are integers, because torch floors each output dim separately while the branch multiplies the scales together and truncates once:
(8,3,32,32) scale_factor=1.4 out (8,3,44,44) flops 48168 numel 46464
(8,3,32,32) scale_factor=(1.4,2.6) out (8,3,44,83) flops 89456 numel 87648
(2,3,7,7,7) scale_factor=1.5 out (2,3,10,10,10) flops 6945 numel 6000
(1,4,20,20) scale_factor=(0.7,1.9) out (1,4,14,38) flops 2127 numel 2128
That last one is off by one in the other direction, from 1600 * (0.7*1.9) landing just under 2128.
So it is the same bug one branch over, which is why scale_factor=2 passing as your control does not say much. Building it the way you built the size side, flops = _prod(input.shape[:2]) and then flops *= math.floor(dim * scale) across input.shape[2:], gives result.numel() on all ten cases I ran, your three included.
git log -G on that line reaches #7353, which corrected the exponent from len(input) to input.ndim - 2 and left the flooring alone, so it reads as unconsidered rather than deliberate. Your call whether it belongs in this PR.
The branch multiplied the scales together and truncated once, while torch floors each output dim on its own, so the two only agree when every scale is an integer. scale_factor=1.4 on an (8,3,32,32) input reported 48168 flops against a real output of 46464 elements, and (0.7,1.9) on (1,4,20,20) reported 2127 against 2128. Size the job from the batch and channel dims and multiply by floor(dim * scale) per spatial dim, matching the size branch. Seven scale_factor parametrizations added, four of them non-integer; the integer ones are unchanged. Signed-off-by: Vineeth Sai <vineethsai4444@gmail.com>
|
You are right, and it is worse than the control being weak: the Reproduced your four cases exactly on torch 2.11.0+cpu, so it is not version-specific: The branch now sizes the job the way the Seven Your read of #7353 matches what I see: it corrected the exponent and left the flooring alone, so this had never been looked at. Thanks for actually running it rather than eyeballing the diff. The scalar- |
|
Checked On |
|
Thanks for running it against real |
_upsample_flops_computecounts only the output spatial shape whenF.interpolateis called withsize=, dropping the batch and channel dims. Its ownscale_factorbranch sizes the job frominput.numel()and is correct, so the same op gets two different answers depending on which argument you pass.Cause:
return int(_prod(size)), 0.sizeis the output spatial shape only, and a scalarsizeapplies to every spatial dim, not just one.Fix: multiply by
input.shape[:2], and raise a scalarsizetoinput.ndim - 2.Test:
pytest tests/unit/profiling/flops_profiler/test_flops_profiler.py -m sequential -k "interpolate or elementwise or conv_transpose"-> 7 failed / 14 passed before, 21 passed after. Newtest_interpolate_flopsassertsflops == result.numel()over 3d/4d/5d inputs, tuple and scalarsize, with thescale_factorcase as the unchanged control.[8,3,32,32]size=(64,64)[8,3,32,32]size=64[8,3,32,32]scale_factor=2[2,3,8,8,8]size=16