# Feature Request # Multi Query Attention (MQA) supprot request
Thanks for your wonderful work that FasterTransformer is a great job that benefit many people. Fastertransformer works very well on Multi Head Attention (MHA) like GPT/OPT/Bloom etc..
However, Multi Query Attention (MQA) is widely used in recent years, in such like GptBigCode model. FT have no implementatoin for MQA layer now.
Will you or any other people have any plan to support MQA layer in FT ? Please leave a comment.
saw the similar request in https://github.com/NVIDIA/FasterTransformer/issues/603
saw the similar request in #603
Were you able to figure this out? I'm too trying to convert falcon-7b to FT which has MQA.
I am planning to work on MQA in FT in July. Currently investigating which work exactly is required.
Still investigating. After that I will start to try to figure it out.
One more thing is that newest WizardCoder (Based on GPTBigCode, have better performance than starcoder) support 4-bit inference. Which is already fast.
However the FasterTransformer does not support 4-bit quantization. If we realize the FasterTransformer for MQA, will it be faster than 4-bit inference.
These are the thing that I need to find out.
New vLLM can help optimize the throughput of GPTBigCode.
As it is off-the-sherf.
https://github.com/vllm-project/vllm
I am planning to work on MQA in FT in July. Currently investigating which work exactly is required.
Are you achieve to support MQA? can you share your code?
请问 您那边在FT上支持MQA么,如果完成的话 可以分享一下么
MQA的支持工作已经完成,Wizard的FT加速也完成了。由于一些信息安全洞原因,我不能提供源码,但是我可以提供支持MQA的复现步骤。
MQA和GQA都可以复用MHA的结构。只需要将WQKV中的KV权重copy n_head份即可。 这句话是重点,请仔细理解一下这句话。特别是torch的MQA代码,建议和torch的MHA代码详细比较一下,理解一下为什么MQA可以复用MHA的步骤。
基于复用MHA结构,你不用修改任何FT的cpp源码,也不用从头写cuda,只需要把权重正确转换即可。
按照以下步骤实现MQA的FT,总耗时大概在3周之内可以完成。如果你对FT比较熟练的话,1周到2周应该也可以完成。
- 使用santacoder进行调试(因为它比较小,只有1.1B。如果使用16B的模型,加载一次权重都要耗费非常长的时间,调试起来不如1.1B的快)
- santacoder-mha与GPT2的结构相同,可以很快对齐到FT实现上。先实现这个。
- 使用santacoder-mqa。这里需要注意,还是上面说的,理解结构,将KV_cache拷贝n_head份。确保santacoder-mqa的FT和torch对齐。
- 这个时候,你已经掌握了MQA的实现步骤。然后即可使用大权重的比如16B的模型进行转换和对齐。
注意,整个步骤不需要重写FT的cpp代码,只需要在权重和调用方式上做一定的复用。如果有问题,请随时问我。再次抱歉不能提供源码。
请问 您那边在FT上支持MQA么,如果完成的话 可以分享一下么
The support work for MQA has been completed, and the FT acceleration for Wizard has also been completed. Due to some information security reasons, I cannot provide the source code, but I can provide the reproduction steps for supporting MQA.
Both MQA and GQA can reuse the structure of MHA. Just copy the KV weights in WQKV n_head times. This sentence is the key point, please understand it carefully. Especially for the torch MQA code, it is recommended to compare it with the torch MHA code in detail to understand why MQA can reuse the structure of MHA.
Based on reusing the MHA structure, you don't need to modify any FT cpp source code or write cuda from scratch, just convert the weights correctly can complete it.
Follow the steps below to implement MQA's FT, which can be completed in about 3 weeks. If you are proficient in FT, it can be completed in 1 to 2 weeks.
- Use santacoder for debugging (because it is relatively small, only 1.1B. If you use a 16B model, loading weights once takes a very long time, and debugging is not as fast as 1.1B).
- Santacoder-mha is aligned with the GPT2 structure and can be quickly aligned with FT implementation. Implement this first.
- Use santacoder-mqa. Note that, as mentioned above, understand the structure and copy KV_cache n_head times. Make sure that santacoder-mqa's FT is aligned with torch.
- At this point, you have mastered the implementation steps of MQA. Then you can use a large model for conversion and alignment.
Note that the entire process does not require rewriting the FT cpp code...
If you have any question , please do not hesitate to leave a comment.
Newest InternLM can also accelerate GQA, and it can support GPT-Q 4-bit quant.
https://github.com/InternLM/lmdeploy#introduction
InternalLM seems more efficient than FT.
As MQA and GQA have been solved, I close this issue.