-
Notifications
You must be signed in to change notification settings - Fork 569
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
权重合并后重新加载训练时出现错误 #556
Comments
你好大哥,你们预训练完后lora模块正常吗,我预训练完后才48B,你们是改动其他地方了吗,能不能指点以下,谢谢大哥 |
你的问题里出现了 |
我知道这个问题,这里32000是原始llama2的词汇表大小,55296为扩充后的词汇表大小,但是训练正常、合并权重后重新加载训练新的任务时出现以上问题。所以存在什么问题?需要手工改词汇表config中的大小么? |
@ymcui 催老师这个怎么处理呢? |
你前面说 |
是这样的,前面做的词汇表扩充是我自己训练的一个词汇表,大小为90894。出现类似的错误。 |
合并时有没有出现报错?合并后继续训练,具体的训练脚本是什么? |
合并时没有出现错误,然后当我加载合并的模型想继续做sft时出现如上错误 |
不管是做sft,还是用run_clm_pt_with_peft.py不制定词汇表路径(不扩充词汇表方式),做继续预训练,都出现以上错误。 |
我建议你一步一步说明(每一步输入是什么输出是什么),贴出相应命令。 另外,根据上面的描述,已经出现三个不同的词表大小了,有理由怀疑你文件存放是否出现错乱(比如模型中的词表大小是A,但tokenizer的大小是B,诸如此类),自己多排查一下吧。 |
第一步,配置环境: path/python -m pip install -r requirements.txt 以上训练完毕后, 随后重新用https://github.com/ymcui/Chinese-LLaMA-Alpaca-2/blob/main/scripts/training/run_pt.sh启动, |
|
这里我直接指定新合并后的词汇表 elif model_args.tokenizer_name_or_path:
tokenizer = LlamaTokenizer.from_pretrained("合并后的路径", **tokenizer_kwargs) |
你上面修改代码就很容易出现问题,例如你实际传入的模型权重和tokenizer手动修改的路径不对应,建议你进一步自查一下。 另外,从你列出的信息,你还修改了代码的内部逻辑,我仍然无法提供准确的建议,因为具体的脚本命令、参数还是没有。 你第一步到第四步以及合并权重都没有出错,训练的逻辑已经在第四步走通了,合并后的模型进一步训练反而出了问题,是不是你合并的权重本身有问题呢?打印一下embedding_size看看吧。 |
训练基本没问题,模型合并后重新用如下脚本加载也会出现同样的错误。 from transformers import AutoConfig, AutoModelForCausalLM, AutoTokenizer
from transformers import LlamaTokenizer
from typing import TYPE_CHECKING, Any, Dict, Tuple
model_name_or_path="merge_llama2_with_chinese_lora"
llama_tokenizer = LlamaTokenizer.from_pretrained(model_name_or_path)# 原生LLaMA分词模型
print(len(llama_tokenizer))
def _get_init_kwargs(model_args: "ModelArguments") -> Dict[str, Any]:
return {
"trust_remote_code": True,
"cache_dir": "./",
"revision": "1.2",
"token": "11",
}
init_kwargs = _get_init_kwargs("ModelArguments")
tokenizer = AutoTokenizer.from_pretrained(model_name_or_path, **init_kwargs)
tokenizer_vocab_size = len(tokenizer)
print(tokenizer_vocab_size)
config = AutoConfig.from_pretrained(model_name_or_path, **init_kwargs)
print(config.vocab_size)
model = AutoModelForCausalLM.from_pretrained(model_name_or_path, config=config, **init_kwargs)
#print(mode.vocab_size) |
我合并的模型是: |
|
我加载了原始llama2进行了测试,没问题。确保代码没问题。 # 运行脚本前请仔细阅读wiki(https://github.com/ymcui/Chinese-LLaMA-Alpaca-2/wiki/pt_scripts_zh)
# Read the wiki(https://github.com/ymcui/Chinese-LLaMA-Alpaca-2/wiki/pt_scripts_zh) carefully before running the script
lr=2e-4
lora_rank=64
lora_alpha=128
lora_trainable="q_proj,v_proj,k_proj,o_proj,gate_proj,down_proj,up_proj"
modules_to_save="embed_tokens,lm_head"
lora_dropout=0.05
pretrained_model=per_models/Llama-2-7b-hf
chinese_tokenizer_path=tokens_cym
dataset_dir=./data
data_cache=temp_data_cache_dir
per_device_train_batch_size=1
gradient_accumulation_steps=8
block_size=512
output_dir=output_dir
deepspeed_config_file=ds_zero2_no_offload.json
#torchrun --nnodes 1 --nproc_per_node 1
python -m torch.distributed.run --nnodes 1 --nproc_per_node 1 run_clm_pt_with_peft.py \
--deepspeed ${deepspeed_config_file} \
--model_name_or_path ${pretrained_model} \
--tokenizer_name_or_path ${chinese_tokenizer_path} \
--dataset_dir ${dataset_dir} \
--data_cache_dir ${data_cache} \
--validation_split_percentage 0.001 \
--per_device_train_batch_size ${per_device_train_batch_size} \
--do_train \
--seed $RANDOM \
--fp16 \
--num_train_epochs 1 \
--lr_scheduler_type cosine \
--learning_rate ${lr} \
--warmup_ratio 0.05 \
--weight_decay 0.01 \
--logging_strategy steps \
--logging_steps 10 \
--save_strategy steps \
--save_total_limit 3 \
--save_steps 200 \
--gradient_accumulation_steps ${gradient_accumulation_steps} \
--preprocessing_num_workers 8 \
--block_size ${block_size} \
--output_dir ${output_dir} \
--overwrite_output_dir \
--ddp_timeout 30000 \
--logging_first_step True \
--lora_rank ${lora_rank} \
--lora_alpha ${lora_alpha} \
--trainable ${lora_trainable} \
--lora_dropout ${lora_dropout} \
--modules_to_save ${modules_to_save} \
--torch_dtype float16 \
--load_in_kbits 16 \
--save_safetensors False \
--gradient_checkpointing \
--ddp_find_unused_parameters False
|
合并模型的参数为: python merge_llama2_with_chinese_lora_low_mem.py \
--base_model per_models/Llama-2-7b \
--lora_model output_dir/pt_lora_model \
--output_type huggingface \
--output_dir ./merge_llama2_with_chinese_lora/
|
命令本身没有问题,前提是你没修改开源的代码。那你检查看看 |
1、打印一下各个key及大小,没有问题。大概13G |
所以基本判定是合并权重有问题。而且我说的是 |
还要清楚一点,你开始训练没有问题,合并后训练才有问题,说明你训练本身生成的lora权重有问题或者合并步骤有问题,你要逐一去排查,并且用正确的权重去排除代码问题。 |
-rw-r--r-- 1 root root 502 Apr 8 15:05 adapter_config.json
-rw-r--r-- 1 root root 443 Apr 8 15:05 adapter_model.bin
-rw-r--r-- 1 root root 549 Apr 8 15:05 special_tokens_map.json
-rw-r--r-- 1 root root 1.1K Apr 8 15:05 tokenizer_config.json
-rw-r--r-- 1 root root 825K Apr 8 15:05 tokenizer.model 此外我用如下脚本打印时直接没结果: import torch
import os
lora_model_path="./output_dir/pt_lora_model"
lora_state_dict = torch.load(os.path.join(lora_model_path,'adapter_model.bin'),map_location='cpu')
print(len(lora_state_dict))
for key in lora_state_dict.items():
print(key) 输出为:
|
生成的output_dir/pt_lora_model下的结果为:
这怎么定位呢? |
另外,你们用此脚本先增量预训练、然后合并模型再微调过么?我这仅仅加了自己的一个训练数据集、别的啥也没改,可就是出现问题。 |
你保存的adapter_model.bin 只有443B大小。 |
hi ,催老师,方便发下训练词汇表的全部参数么?
|
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your consideration. |
Closing the issue, since no updates observed. Feel free to re-open if you need any further assistance. |
提交前必须检查以下项目
问题类型
模型转换和合并
基础模型
Chinese-LLaMA-2 (7B/13B)
操作系统
Linux
详细描述问题
我针对llama2-7b做增量词汇表扩充且做了增量预训练,然后训练后的模型合并后,再次加载新的模型训练时出现如下问题:
ValueError: Trying to set a tensor of shape torch.Size([32000, 4096]) in "weight" (which has shape torch.Size([90894, 4096])), this look incorrect.
还需要替换啥呢?
依赖情况(代码类问题务必提供)
运行日志或截图
ValueError: Trying to set a tensor of shape torch.Size([32000, 4096]) in "weight" (which has shape torch.Size([90894, 4096])), this look incorrect.
我完全按照这个上的说明往下走的,用给定的已经扩充的词汇表,然后做了增量预训练,随后合并pt_lora_model在的参数,再加载合并后的模型做别的微调时,出现如下现象,请问怎么处理呢?需要修改词汇表中的词汇表大小?
ValueError: Trying to set a tensor of shape torch.Size([32000, 4096]) in "weight" (which has shape torch.Size([55296, 4096])), this look incorrect.
The text was updated successfully, but these errors were encountered: