Extend Transformers Trainer Class to Enable PyTorch Torchscript for Inference
Author: jianan-gu
Pull request: yes
Labels: name: Performance

# What does this PR do?

<!--
Congratulations! You've made it this far! You're not quite done yet though.

Once merged, your PR is going to appear in the release notes with the title you set, so make sure it's a great title that fully reflects the extent of your awesome contribution.

Then, please replace this with a description of the change and which issue is fixed (if applicable). Please also include relevant motivation and context. List any dependencies (if any) that are required for this change.

Once you're done, someone will review your PR shortly (see the section "Who can review?" below to tag some potential reviewers). They may suggest changes to make the code even better. If no one reviewed your PR after a week has passed, don't hesitate to post a new comment @-mentioning the same persons---sometimes notifications get lost.
-->

<!-- Remove if not applicable -->

This PR intends to extend Transformers Trainer Class with PyTorch [Torchscript](https://pytorch.org/docs/stable/generated/torch.jit.trace.html) (`torch.jit.trace`) to speed up model inference with just-in-time compilation.

Users could simply enable "--jit_mode" Trainer input args to get benifits.


## 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.

<!-- Your PR will be replied to more quickly if you can figure out the right person to tag with @

 If you know how to use git blame, that is the easiest way, otherwise, here is a rough guide of **who to tag**.
 Please tag fewer than 3 people.

Models:

- albert, bert, xlm: @LysandreJik
- blenderbot, bart, marian, pegasus, encoderdecoder,  t5: @patrickvonplaten, @patil-suraj
- longformer, reformer, transfoxl, xlnet: @patrickvonplaten
- fsmt: @stas00
- funnel: @sgugger
- gpt2: @patrickvonplaten, @LysandreJik
- rag: @patrickvonplaten, @lhoestq
- tensorflow: @LysandreJik

Library:

- benchmarks: @patrickvonplaten
- deepspeed: @stas00
- ray/raytune: @richardliaw, @amogkam
- text generation: @patrickvonplaten
- tokenizers: @n1t0, @LysandreJik
- trainer: @sgugger
- pipelines: @LysandreJik

Documentation: @sgugger

HF projects:

- datasets: [different repo](https://github.com/huggingface/datasets)
- rust tokenizers: [different repo](https://github.com/huggingface/tokenizers)

Examples:

- maintained examples (not research project or legacy): @sgugger, @patil-suraj
- research_projects/bert-loses-patience: @JetRunner
- research_projects/distillation: @VictorSanh

 -->


Comments:
_The documentation is not available anymore as the PR was closed or merged._

This may be more [optimum](https://github.com/huggingface/optimum/)'s domain than Transformers, what do you think @mfuntowicz @LysandreJik @lewtun ?

> This may be more [optimum](https://github.com/huggingface/optimum/)'s domain than Transformers, what do you think @mfuntowicz @LysandreJik @lewtun ?

Thanks for the ping and thank you @jianan-gu for opening the PR! 

I agree that inference-based improvements are better suited in `optimum`, so in this case we'd need to implement a dedicated `torchscript` subpackage that effectively extends the `Trainer` in this manner. 

Having said that, I wonder if it's overkill to create a new package just to support this feature, which is natively included in PyTorch. 

@mfuntowicz @michaelbenayoun do you see any other benefits that could be had by having a dedicated subpackage for `torchscript` in `optimum`? 

I'd be happy to work on this PR if you give me a green light that you'll accept this feature.

We can mark is as experimental - should you decide that it'd fit better elsewhere down the road. So it would be easier to experiment and try it on. 

Adding UT Tests in test_trainer.py for jit which covers `evaluate `and `predict`.
Thanks.

> What is the sounds of a tree falling in the forest when there is nobody to hear it?
> 
> All these new features need to be documented on the user-side so that they actually get used. So the same as with IPEX let's add user docs and as before ideally a small benchmark to give users an incentive to try the feature.
> 
> updade: I added a doc for inference as we haven't created it and then you can push the usage examples there - like we did with IPEX PR. So please fill out the blank and then we are good to go I think.
> 
> and added a few nits in the code.

Though this jit mode works both for CPU and GPU, the current IPEX release covers CPU side optimizations with jit mode for model inference. Therefore we only have added and updated the inference doc for CPU (perf_infer_cpu.mdx), please take a review of the contents @stas00 , thanks. (for the small benchmark, for now, we only have relative performance numbers like shown in https://github.com/huggingface/transformers/issues/17137, so we will prepare for the numbers with --skip_memory_metrics 0 as a follow-up) 


@sgugger, would you like to have a quick look at the last changes since your review - mainly the newly added doc. Thank you!

otherwise it's good to be merged.