Skip to content

Conversation

@Chillee
Copy link
Collaborator

@Chillee Chillee commented May 22, 2019

Fixes #18440

I calculate a derived index from start,stop,step as start + step*index. When start=0 and step=1 (the defaults/range(n)), this is the same behavior as before.

Unluckily, it seems that we do not optimize out operations like x*1 or x+0. That means that we're doing lots of redundant operations when we don't need to. EDIT: More specifically, it seems like we only do this optimization for (tensor, scalar): https://github.com/pytorch/pytorch/blob/master/torch/csrc/jit/passes/peephole.cpp#L128

The most annoying part of this code is calculating the number of iterations, given start, stop, step. I ended up going with the formula (abs(stop-start) + abs(step)-1)//abs(step). Other intuitively appealing formulas like (stop-start + step -1)//step don't work for negative numbers.

I tried using SymbolicVariable for the calculations, but it seems that symbolicvariable only outputs ops for tensors, not the integers we have.

@Chillee Chillee requested a review from jamesr66a May 22, 2019 03:05
@pytorchbot pytorchbot added the oncall: jit Add this issue/PR to JIT oncall triage queue label May 22, 2019
Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Chillee has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@Chillee
Copy link
Collaborator Author

Chillee commented May 22, 2019

I decided to add special casing for the common case for a couple reasons:

  1. We don't currently do the simple peephole optimizations on integers. That means that this causes some performance regression (however negligible).
  2. Changing the common case IR generated touches a lot of code, and seems to cause quite a bit of tests to fail. Reverting the common case to the old behavior fixes those tests.
  3. Special casing for the common case is very simple.

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Chillee has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

Copy link
Collaborator

@jamesr66a jamesr66a left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good! Some small stuff to fixup and we'll have to look at the tests but I think we're on the right track here

Copy link
Contributor

@eellison eellison left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great 🔥a few comments

@jamesr66a jamesr66a requested review from driazati and zdevito May 22, 2019 21:01
Copy link
Contributor

@zdevito zdevito left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool -- I think we should make the range/scale computations either TorchScript code or a builtin. Otherwise it will be too hard to maintain.

@jamesr66a jamesr66a requested a review from suo May 22, 2019 21:52
@jamesr66a
Copy link
Collaborator

TestScript.test_for_range_no_arg needs its regex updated

@pytorchbot pytorchbot added the module: internals Related to internal abstractions in c10 and ATen label May 22, 2019
@Chillee Chillee force-pushed the jitrangeloops branch 2 times, most recently from ba66992 to 9bed93a Compare May 23, 2019 00:02
@Chillee Chillee force-pushed the jitrangeloops branch 2 times, most recently from 54cfcf2 to 8ff19ea Compare May 23, 2019 17:02
Copy link
Collaborator

@jamesr66a jamesr66a left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. I added some comments about small stuff

Copy link
Collaborator

@jamesr66a jamesr66a left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ayy lmoa

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Chillee is landing this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@Chillee
Copy link
Collaborator Author

Chillee commented May 29, 2019

@pytorchbot rebase this please.

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Chillee is landing this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

zdevito pushed a commit to zdevito/ATen that referenced this pull request May 29, 2019
Summary:
Fixes #18440

I calculate a derived index from `start,stop,step` as `start + step*index`. When `start=0` and `step=1` (the defaults/`range(n)`), this is the same behavior as before.

Unluckily, it seems that we do not optimize out operations like `x*1` or `x+0`. That means that we're doing lots of redundant operations when we don't need to. EDIT: More specifically, it seems like we only do this optimization for (tensor, scalar): https://github.com/pytorch/pytorch/blob/master/torch/csrc/jit/passes/peephole.cpp#L128

The most annoying part of this code is calculating the number of iterations, given `start, stop, step`. I ended up going with the formula `(abs(stop-start) + abs(step)-1)//abs(step)`. Other intuitively appealing formulas like `(stop-start + step -1)//step` don't work for negative numbers.

I tried using `SymbolicVariable` for the calculations, but it seems that `symbolicvariable` only outputs ops for `tensors`, not the integers we have.
Pull Request resolved: pytorch/pytorch#20795

Differential Revision: D15446869

Pulled By: Chillee

fbshipit-source-id: 6085545ace04e25985c6ac870226f7a651f670d5
@facebook-github-bot
Copy link
Contributor

@Chillee merged this pull request in dd903eb.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Merged module: internals Related to internal abstractions in c10 and ATen oncall: jit Add this issue/PR to JIT oncall triage queue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[JIT] Support range(start, stop[, step]) in script

8 participants