Speeding up inference with ONNX
Hello,
It seems that Transformers inference is still an issue (?), however recently Microsoft team release some nice optimization for Transformer for ONNX runtime. It seems that CPU inference get huge speedup.
See blog: https://cloudblogs.microsoft.com/opensource/2020/01/21/microsoft-onnx-open-source-optimizations-transformer-inference-gpu-cpu/
And example: https://github.com/onnx/tutorials/blob/master/tutorials/Inference-PyTorch-Bert-Model-for-High-Performance-in-ONNX-Runtime.ipynb
Would it make sense to consider integrate some conversion to onnx in inference code for espnet?
If you are interested I could start with some tests and maybe try to propose something?
This is very interesting. Yes, we really want to work in this direction (whether onnix or jit), but so far we don't have enough resources to do it. If you help that direction, this is very very helpful for us.
Probably you can start to check https://github.com/espnet/espnet/pull/1402 We try to make unified and flexible designs for ASR decoders with efficient parallelization. If we put onnix conversion on top of it, we may have both benefits of efficiency and flexibility more (but you can, of course, start it from our legacy beam search).
https://github.com/espnet/espnet/blob/e10b2cc916ec78f9b8757463e726711464e1e6e5/espnet/nets/pytorch_backend/e2e_asr_transformer.py#L313-L318
I tried to test it before and you can also try it by passing model.recognize(..., use_jit=True) but the jit version could not reproduce the existing one. This may be because there is some dynamic code execution inside the decoder forward. Maybe we should try this again in the latest pytorch and split dynamic and static pieces for jit.
I did some test, while on Bert in transformers package I can see significant (around x2 speedup in my setup), but on encoder and decoder module of espnet (v1) there is no speedup.
I guess the optimization is quite specific to Bert and its implementation in the package. Anyway it doesn't work out of the box as in the tutorial above. Maybe a rewrite of transformer using more standard block now available in pytorch could help with the performance.
I am not sure about JIT though, I don't have much experience about it but in pytorch runtime it would not provide huge speedup I think
Slightly related question: I found that decoders do return caches that could be use to speedup next iteration, how ever it is not used. What is the reason not using it?
It is for a new model-independent implementation (https://github.com/espnet/espnet/blob/master/espnet/nets/beam_search.py) for asr_recog.py --api v2 .... So we did not provide the cache mechanism in the old model-specific impl.
Ok thanks, as it doesn’t work out of the box with ONNX, let’s close this issue for now and reconsider after some time.