transformers icon indicating copy to clipboard operation
transformers copied to clipboard

Replace the frequent permutation generations with a pre-allocated permutation table

Open FindHao opened this issue 1 year ago • 9 comments

What does this PR do?

This PR fixes the performance issue in model big_bird. In model bigbird, the random masks are generated each time, and it calls a huge number of np.random.permutation which has big overheads. the function bigbird_block_rand_mask is only executed when the values of from_seq_length and to_seq_length are less than 4096 and the values of from_block_size and to_seq_length are 64. With all the above limitations, the number of all possible permutations is less than 249,984. Since all variables mentioned above are configured only once at the initialization stage, we generate all possible permutations at this stage and only randomly generate the indexes by using numpy.random.randint when we need permutations. This optimization significantly reduces the computation on the CPU, and its speedup for the whole model is about 1.12X in TorchBench.

Before submitting

  • [ ] This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • [x] Did you read the contributor guideline, Pull Request section?
  • [ ] Was this discussed/approved via a Github issue or the forum? Please add a link to it if that's the case.
  • [ ] Did you make sure to update the documentation with your changes? Here are the documentation guidelines, and here are tips on formatting docstrings.
  • [ ] Did you write any new necessary tests?

Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag members/contributors who may be interested in your PR.

@ArthurZucker and @younesbelkada

FindHao avatar Feb 07 '23 19:02 FindHao

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint.

@ArthurZucker Could you please also help me check the CI errors? In the first CI test 'add model like runner', the error is src/transformers/models/bigbird_pegasus/modeling_bigbird_pegasus.py:271:21: F821 Undefined name permutations. but this PR doesn't modify the code in bigbird_pegasus and I can't find the related imports etc. in bigbird_pegasus.

FindHao avatar Feb 10 '23 15:02 FindHao

Sure! Seems like you need to run make style and make fix-copies. If make style does not work, (meaning the tests for quality are still failing) then you have to do pip install --upgrade -e ".[dev]". Basically to make sure that the black` version you are using is the correct one

ArthurZucker avatar Feb 16 '23 14:02 ArthurZucker

You also need to rebase on main !

ArthurZucker avatar Feb 17 '23 09:02 ArthurZucker

Okay! Now fixing this should only need make fixup. But to make sure you have the correct black version run pip install -e . should do the trick. Once you are ready for a final review, feel free to ping @sgugger

ArthurZucker avatar Mar 02 '23 20:03 ArthurZucker

This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the contributing guidelines are likely to be ignored.

github-actions[bot] avatar Mar 27 '23 15:03 github-actions[bot]

sorry for the late update. will finish it this week.

FindHao avatar Mar 27 '23 15:03 FindHao

Hi @ArthurZucker , I updated the docstrings. As for the fast function, I explained why it couldn't be replaced with np.random.randint in this reply. Could you double-check it?

FindHao avatar Apr 26 '23 20:04 FindHao

Sorry for the delay, went in holidays. This seems like a big modification, so I think having performance stats would be better before merging (this adds a layer of complexity which we are not usually big fans of)>

ArthurZucker avatar May 30 '23 15:05 ArthurZucker

Sorry for the delay, went in holidays. This seems like a big modification, so I think having performance stats would be better before merging (this adds a layer of complexity which we are not usually big fans of)>

Hi @ArthurZucker, thanks for your comments! I agree that a big project like huggingface should have high code quality requirements! no worries.

FindHao avatar May 31 '23 04:05 FindHao

This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the contributing guidelines are likely to be ignored.

github-actions[bot] avatar Jun 24 '23 15:06 github-actions[bot]