flash-attn2: add flash-attn2-npu kernel - #64
Conversation
|
Ready for review. @danieldk @drbh @MekkCyber |
MekkCyber
left a comment
There was a problem hiding this comment.
Hey @zheliuyu, Thanks so much for your contribution! It looks really great. Right now, we can’t merge it into kernels-community because we’re trying to keep the number of maintained kernels small and manageable—we usually only include the most widely used ones or those needed in our frameworks.
But don’t let that stop you! You can absolutely host your kernel on your Hub organization and use it from there. The whole idea of the kernels project is to give the community a space to share their work and let others benefit from it.
While this code wasn't merged, your suggestions were convincing. Thanks very much. I'll now focus on reusing work from |
Introduction
Following the design of
huggingface/transformersandhuggingface/kernels, I've created theflash-attn2-npuproject to supersede the current NPU flash attention implementation: https://github.com/huggingface/transformers/blob/main/src/transformers/integrations/npu_flash_attention.pyDependencies
huggingface/kernels. Add support for NPU kernelize/layers kernels#155huggingface/kernels-builder. Thehuggingface/kernels-builderrequires no additional NPU adaptations and can be used directly. Becasue, most NPU acceleration methods are implemented in torch_npu, which is a pure python project. Of course, I'll keep a close watch on any future NPU-specific adaptations needed forkernels-builder.Follow CONTRIBUTING.md
flash-attn2-npu.flash-attn2folder?doGetKernelCheck = falseto avoid torch_npu ImportError when no GPUs. Instead, I will provide the validation results for the LoRA algorithm within the transformers.test_flash_attn2_npu.py.transformers.Test by transformers + LLaMA-Factory
LLaMA-Factory: https://github.com/hiyouga/LLaMA-Factory
Config
Qwen3_lora_sft.yaml, copied fromexamples/train_lora/llama3_lora_sft.yamlRun
llamafactory-cli train examples/train_lora/Qwen3_lora_sft.yamlAutoModel.from_pretrained(attn_implementation="flash_attention_2")
Built in implementation: https://github.com/huggingface/transformers/blob/main/src/transformers/integrations/npu_flash_attention.py
"epoch": 1.0
"total_flos": 547575576920064.0
"train_loss": 1.5288773348731717
"train_runtime": 666.4749
AutoModel.from_pretrained(attn_implementation="kernels-ext-npu/flash-attn2-npu")
Built from kernels-community: this pr.
The build outputs are temporarily stored for use by transformers: https://huggingface.co/kernels-ext-npu/flash-attn2-npu/tree/main
"epoch": 1.0
"total_flos": 547575576920064.0
"train_loss": 1.5290487386884482
"train_runtime": 670.0417