Closed floyddcn closed 8 months ago
transformers:4.35.2 微调方式:lora 训练模型:sqlcoder2(GPTBigCodeModel架构) LLaMA-Factory:v0.3.0
对于LLaMA-Factory做了一些小的适配如下(为了支持最新版transformer中gptbigcode的flashattn2):
之前在issue问过,hiyouga大佬让使用c_attn进行sqlcoder的lora训练;结果在升级transformers高版本使用flashattn2时(transformers在11月14日,GPTBigCodeModel已经支持flashattn2啦)发现微调后的模型使用flashattn2推理会报错:
于是修改了flash attn2的一小段代码 (flash_attn/flash_attn_interface.py): 这样是不报错了,但是推理输出全是0:"000000...000"这样的奇怪结果或直接predict为空!
看了llama和其他使用qkv多矩阵映射的 情况,思考qkv类型不一致是否因为只对c_attn进行的微调,故采用q_attn,c_attn作为lora微调target(--lora_target q_attn,c_attn )重新微调(已经恢复了 [尝试1] 中修改的flashattn代码)后果然也不报错,但是推理结果依然为空。 NCCL_P2P_DISABLE=1 CUDA_VISIBLE_DEVICES=0,1,2,6,7 torchrun --nproc_per_node 5 src/train_bash.py \ --stage sft \ --model_name_or_path /workspace/nlp_models/sqlcoder2/ \ --do_train True \ --overwrite_cache False \ --finetuning_type lora \ --template vanilla \ --flash_attn True \ --dataset train_merge_1117_ns \ --cutoff_len 8192 \ --learning_rate 5e-4 \ --num_train_epochs 50 \ --max_samples 100000 \ --per_device_train_batch_size 2 \ --gradient_accumulation_steps 8 \ --lora_rank 8 \ --lora_target q_attn,c_attn \ --fp16 True \ --output_dir /workspace/projs/ft/sqlcoder2/1123.1/ \ --logging_steps 5 \ --save_steps 100 \ --lr_scheduler_type cosine \ --max_grad_norm 1 \ --overwrite_output_dir
NCCL_P2P_DISABLE=1 CUDA_VISIBLE_DEVICES=0,1,2,6,7 torchrun --nproc_per_node 5 src/train_bash.py \ --stage sft \ --model_name_or_path /workspace/nlp_models/sqlcoder2/ \ --do_train True \ --overwrite_cache False \ --finetuning_type lora \ --template vanilla \ --flash_attn True \ --dataset train_merge_1117_ns \ --cutoff_len 8192 \ --learning_rate 5e-4 \ --num_train_epochs 50 \ --max_samples 100000 \ --per_device_train_batch_size 2 \ --gradient_accumulation_steps 8 \ --lora_rank 8 \ --lora_target q_attn,c_attn \ --fp16 True \ --output_dir /workspace/projs/ft/sqlcoder2/1123.1/ \ --logging_steps 5 \ --save_steps 100 \ --lr_scheduler_type cosine \ --max_grad_norm 1 \ --overwrite_output_dir
export lora得到全量模型后,使用flashattn2推理,依然是空输出~ CUDA_VISIBLE_DEVICES=4 python src/export_model.py \ --model_name_or_path /workspace/nlp_models/sqlcoder2/ \ --template vanilla \ --finetuning_type lora \ --checkpoint_dir /workspace/projs/ft/sqlcoder2/1123.1/checkpoint-300 \ --export_dir /workspace/projs/ft/sqlcoder2/1123.1/checkpoint-300-merged
CUDA_VISIBLE_DEVICES=4 python src/export_model.py \ --model_name_or_path /workspace/nlp_models/sqlcoder2/ \ --template vanilla \ --finetuning_type lora \ --checkpoint_dir /workspace/projs/ft/sqlcoder2/1123.1/checkpoint-300 \ --export_dir /workspace/projs/ft/sqlcoder2/1123.1/checkpoint-300-merged
不使用flashattn2推理,无论是否export都可以输出正确的内容的!
请问需要怎样修改才能使用flashattn2进行lora微调后的模型推理呢?(因为flashattn2推理加速还挺快的,所以还是想要能在项目上用上哈)
大佬,有什么建议么?
环境
transformers:4.35.2 微调方式:lora 训练模型:sqlcoder2(GPTBigCodeModel架构) LLaMA-Factory:v0.3.0
问题
之前在issue问过,hiyouga大佬让使用c_attn进行sqlcoder的lora训练;结果在升级transformers高版本使用flashattn2时(transformers在11月14日,GPTBigCodeModel已经支持flashattn2啦)发现微调后的模型使用flashattn2推理会报错:![image](https://github.com/hiyouga/LLaMA-Factory/assets/48442748/2dacbe03-3c8d-4c43-be2f-b8aceabf29df)
尝试1
于是修改了flash attn2的一小段代码 (flash_attn/flash_attn_interface.py):
这样是不报错了,但是推理输出全是0:"000000...000"这样的奇怪结果或直接predict为空!
尝试2
看了llama和其他使用qkv多矩阵映射的 情况,思考qkv类型不一致是否因为只对c_attn进行的微调,故采用q_attn,c_attn作为lora微调target(--lora_target q_attn,c_attn )重新微调(已经恢复了 [尝试1] 中修改的flashattn代码)后果然也不报错,但是推理结果依然为空。
NCCL_P2P_DISABLE=1 CUDA_VISIBLE_DEVICES=0,1,2,6,7 torchrun --nproc_per_node 5 src/train_bash.py \ --stage sft \ --model_name_or_path /workspace/nlp_models/sqlcoder2/ \ --do_train True \ --overwrite_cache False \ --finetuning_type lora \ --template vanilla \ --flash_attn True \ --dataset train_merge_1117_ns \ --cutoff_len 8192 \ --learning_rate 5e-4 \ --num_train_epochs 50 \ --max_samples 100000 \ --per_device_train_batch_size 2 \ --gradient_accumulation_steps 8 \ --lora_rank 8 \ --lora_target q_attn,c_attn \ --fp16 True \ --output_dir /workspace/projs/ft/sqlcoder2/1123.1/ \ --logging_steps 5 \ --save_steps 100 \ --lr_scheduler_type cosine \ --max_grad_norm 1 \ --overwrite_output_dir
尝试3
export lora得到全量模型后,使用flashattn2推理,依然是空输出~
CUDA_VISIBLE_DEVICES=4 python src/export_model.py \ --model_name_or_path /workspace/nlp_models/sqlcoder2/ \ --template vanilla \ --finetuning_type lora \ --checkpoint_dir /workspace/projs/ft/sqlcoder2/1123.1/checkpoint-300 \ --export_dir /workspace/projs/ft/sqlcoder2/1123.1/checkpoint-300-merged
不使用flashattn2推理
不使用flashattn2推理,无论是否export都可以输出正确的内容的!
请问需要怎样修改才能使用flashattn2进行lora微调后的模型推理呢?(因为flashattn2推理加速还挺快的,所以还是想要能在项目上用上哈)