ChatGLM2-6B icon indicating copy to clipboard operation
ChatGLM2-6B copied to clipboard

[BUG/Help] 使用官方代码ptuing时,推理时大量重复输出。

Open FanWan opened this issue 1 year ago • 18 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

Current Behavior

{"labels": "樊乐,编辑,最近,,,文档", "predict": "查询一下的答案,我搜的答案,查询一下一下音量,我搜的答案,我找到答案,我再找答案,找到答案,找到答案,找到答案,找到答案,找到答案,找到答案,找到答案,找到答案,找到答案,找到答案,找到答案,找到答案,找到答案,找到答案,找到答案,找到答案,找到答案,找到答案,找到答案,找到答案,找到答案,找到答案,找到答案,找到答案,找到答案,找到答 案,找到答案,找到答案,找到答案,找到答案,找到答案,找到答案,找到答案,找到答案,找到答案,找到答案,找到答案,找到答案,找到答案,找到答案,找到答案,找到答案,找到答案,找到答案,找到答案,找到答案,找到答案,找到答案,找到答案,找到答案,找到答案,找到答案,找到答案,找到答案,找到答案,找到答案,找到答案,找到答案,找到答案,找到答案,答案,找到答案,找到答案,找到答案,找到答案,找到答案,答案,找到答案,找到答案,答案,答案,答案,找到答案,找到答案,找到答案,答案,找到答案,答案,找到答案,找到答案,答案,找到答案,找到答案,答案,找到答案,答案,答案,找到答案,答案,答案,答案,找到答案,答案,答案,找到答案,答案,答案,找到答案,找到答案,答案,答案,答案,答案,找到答案,答案,找到答案,答案,找到答案,答案,找到答案,答案,找到答案,答案,答案,找到答案,找到答案,找到答案,答案,答案,答案,找到答案,找到答案,找到答案,答案,找到答案,答案,答案,答案,找到答案,找到答案,答案,答案,答案,找到答案,答案,答案,答案,答案,找到答案,答案,答案,答案,答案,答案,找到答案,答案,答案,找到答案,答案,答案,答案,答案,找到答案,答案,答案,答案,答案,答案,答案,找到答案,答案,找到答案,找到答案,答案,答案,答案,答案,答案,找到答案,答案,答案,找到答案,找到答案,找到答案, 答案,答案,答案,找到答案,答案,答案,答案,找到答案,答案,答案,找到答案,找到答案,答案,答案,找到答案,答案,找到答案,答案,答案,找到答案,答案,答案,找到答案,答案,答案,答案,找到答案,找到答 案,答案,答案,答案,找到答案,找到答案,答案,找到答案,答案,答案,找到答案,找到答案,找到答案,答案,找到答案,答案,答案,找到答案,答案,答案,答案,答案,答案,答案,找到答案,答案,答案,答案,找到答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答 案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,找到答案,答案,答案,答案,答案,找到答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答案,答案"}

Expected Behavior

No response

Steps To Reproduce

1、先利用官方脚本train.sh,训练时如下参数: PRE_SEQ_LEN=128 LR=2e-2 NUM_GPUS=1 SAVE_MODEL_PATH=chatglm2_sft_0705 export CUDA_VISIBLE_DEVICES=3 torchrun --standalone --nnodes=1 --nproc-per-node=$NUM_GPUS main.py
--do_train
--train_file /share/code/ChatGLM2-6B/ptuning/data/train-20230619.json
--validation_file /share/code/ChatGLM2-6B/ptuning/data/eval-20230619.json
--preprocessing_num_workers 10
--prompt_column prompt
--response_column output
--overwrite_cache
--model_name_or_path /share/llm_models/glm/chatglm2-6b
--output_dir output/$SAVE_MODEL_PATH
--overwrite_output_dir
--max_source_length 1536
--max_target_length 512
--per_device_train_batch_size 8
--per_device_eval_batch_size 1
--gradient_accumulation_steps 16
--predict_with_generate
--max_steps 150
--logging_steps 2
--save_steps 10
--learning_rate $LR
--pre_seq_len $PRE_SEQ_LEN
--quantization_bit 4 > log/wf_train_0705.log 2>&1 &

2、先利用官方脚本eval.sh,进行推理,参数如下: PRE_SEQ_LEN=128 CHECKPOINT=chatglm2_sft_0705 STEP=50 NUM_GPUS=1 export CUDA_VISIBLE_DEVICES=1 torchrun --standalone --nnodes=1 --nproc-per-node=$NUM_GPUS main.py
--do_predict
--test_file /share/code/ChatGLM2-6B/ptuning/data/eval-20230619.json
--overwrite_cache
--prompt_column prompt
--response_column output
--model_name_or_path /share/llm_models/glm/chatglm2-6b
--ptuning_checkpoint ./output/$CHECKPOINT/checkpoint-$STEP
--output_dir ./output/$CHECKPOINT/eval
--overwrite_output_dir
--max_source_length 1536
--max_target_length 512
--per_device_eval_batch_size 8
--predict_with_generate
--pre_seq_len $PRE_SEQ_LEN
--quantization_bit 4 > log/wf_eval_0705.log 2>&1 &

Environment

- OS:
- Python:
- Transformers:
- PyTorch:
- CUDA Support (`python -c "import torch; print(torch.cuda.is_available())"`) :

Anything else?

No response

FanWan avatar Jul 06 '23 09:07 FanWan

环境配置:cuda 11.4, Transformers 4.30.2, torch 2.0.1, Python 3.10

FanWan avatar Jul 06 '23 09:07 FanWan

py3.8/3.7 tf 4.27.1-4.29.2 torch1.13.1试试

lilongxian avatar Jul 06 '23 09:07 lilongxian

你好你有遇到过 ttributeError: 'ChatGLMModel' object has no attribute 'prefix_encoder' 报错吗

evilbear avatar Jul 06 '23 09:07 evilbear

从hf拉新代码,可解决。6月25日版本中没有这个参数所以报错,现在有了

lilongxian avatar Jul 06 '23 09:07 lilongxian

你好你有遇到过 ttributeError: 'ChatGLMModel' object has no attribute 'prefix_encoder' 报错吗

这个没有哈。

FanWan avatar Jul 06 '23 09:07 FanWan

class ChatGLMModel(ChatGLMPreTrainedModel):

new

    self.pre_seq_len = config.pre_seq_len  # 128
    self.prefix_projection = config.prefix_projection  # False
    if self.pre_seq_len is not None:
        for param in self.parameters():
            param.requires_grad = False

        self.prefix_tokens = torch.arange(self.pre_seq_len).long()
        self.prefix_encoder = PrefixEncoder(config)
        self.dropout = torch.nn.Dropout(0.1)

lilongxian avatar Jul 06 '23 09:07 lilongxian

Here

lilongxian avatar Jul 06 '23 09:07 lilongxian

py3.8/3.7 tf 4.27.1-4.29.2 torch1.13.1试试 requirements里面要求transformers==4.30.2,torch>=2.0

FanWan avatar Jul 06 '23 09:07 FanWan

GLM2新增加了注意力前置编码器,和GLM1一样。 class PrefixEncoder(torch.nn.Module): 区别是:GLM2注意力前置编码器是对k v 头的位置向量range(128)编码,编码维度与MQA注意力kv对齐,即512,一共28个transformer层,所以共 28*512

lilongxian avatar Jul 06 '23 09:07 lilongxian

requirements里面要求transformers==4.30.2,torch>=2.0 指的是:想要你使用SDPA MQA注意力的高效.
TORCH < 2.0时会自动转用传统注意力机制就像BERT那样。

lilongxian avatar Jul 06 '23 10:07 lilongxian

CoreAttention(torch.nn.Module): if pytorch_major_version >= 2: query_layer, key_layer, value_layer = [k.permute(1, 2, 0, 3) for k in [query_layer, key_layer, value_layer]] if attention_mask is None and query_layer.shape[2] == key_layer.shape[2]: context_layer = torch.nn.functional.scaled_dot_product_attention(query_layer, key_layer, value_layer, is_causal=True) else: if attention_mask is not None: attention_mask = ~attention_mask context_layer = torch.nn.functional.scaled_dot_product_attention(query_layer, key_layer, value_layer, attention_mask) context_layer = context_layer.permute(2, 0, 1, 3) new_context_layer_shape = context_layer.size()[:-2] + (self.hidden_size_per_partition,) context_layer = context_layer.reshape(*new_context_layer_shape) else: 使用传统经典注意力.........

lilongxian avatar Jul 06 '23 10:07 lilongxian

使用lora微调后,也是重复输出 image

isacck avatar Jul 07 '23 02:07 isacck

py3.8/3.7 tf 4.27.1-4.29.2 torch1.13.1试试 requirements里面要求transformers==4.30.2,torch>=2.0

这个配置,能解决不断重复生成的问题吗?

sc-lj avatar Jul 07 '23 07:07 sc-lj

我的环境时这样的,可以的。但有时候还是会重复生成,这个可能和GLM模型的stop_token等停止搜索条件有关,我也在解决中,,

lilongxian avatar Jul 07 '23 08:07 lilongxian

你好,微调后出现重复的问题解决了吗。另外,将torch版本换成1.13后是否能一定程度上缓解重复的问题。

QJShan avatar Jul 12 '23 01:07 QJShan

mark

qinqinqaq avatar Jul 17 '23 01:07 qinqinqaq

检查训练和预测的 eos_id是否一致(可见:https://github.com/THUDM/ChatGLM2-6B/issues/270 预测的时候,不要用贪婪的方式生成,设置点随机性。

shixiaoyangaaa avatar Jul 17 '23 03:07 shixiaoyangaaa

你好,我用的chatglm,也会遇到上述问题,请问现在有方法解决了吗

white-0629 avatar Apr 07 '24 09:04 white-0629