yuanzhoulvpi2017 / zero_nlp

中文nlp解决方案(大模型、数据、模型、训练、推理)
MIT License
3.03k stars 368 forks source link

train_llava 报错ValueError: Cannot use chat template functions because tokenizer.chat_template is not set and no template argument was passed! #187

Closed weiaicunzai closed 3 weeks ago

weiaicunzai commented 4 weeks ago

感谢你的代码,我跟着你的代码自己写了一下,如下

from transformers import AutoTokenizer, AutoModelForCausalLM, AutoModel, AutoProcessor, LlavaConfig, LlavaForConditionalGeneration, LlavaProcessor
import torch

def save_weights():

    clip_path = '/mnt/dolphinfs/ssd_pool/docker/user/hadoop-mlm/by/train_llava/model_weights/clip_vit'
    qwen_dir = "/mnt/dolphinfs/ssd_pool/docker/user/hadoop-mlm/by/train_llava/model_weights/qwen1.5-0.5b"

    qwen_model = AutoModelForCausalLM.from_pretrained(qwen_dir)
    qwen_tokenizer = AutoTokenizer.from_pretrained(qwen_dir)

    clip_model = AutoModel.from_pretrained(clip_path)

    qwen_config = qwen_model.config
    vit_config = clip_model.vision_model.config

    llava_config = LlavaConfig(vit_config, qwen_config)
    llava_model = LlavaForConditionalGeneration(llava_config)
    llava_model.vision_tower.vision_model = clip_model.vision_model
    llava_model.language_model = qwen_model

    llava_model.config.pad_token_id = qwen_tokenizer.pad_token_id
    llava_model.config.image_token_index = qwen_tokenizer.encode('<image>')[0]

    # save 
    llava_model.save_pretrained('pretrained_model/model001')
    qwen_tokenizer.save_pretrained("pretrained_model/model001")
    autoprocessor = AutoProcessor.from_pretrained(clip_path)
    autoprocessor.save_pretrained('pretrained_model/model001')

# test 
def test():
    model_path = '/mnt/dolphinfs/ssd_pool/docker/user/hadoop-mlm/by/train_llava/pretrained_model/model001'

    llava_processor = LlavaProcessor.from_pretrained(model_path)
    llava_tokenizer = AutoTokenizer.from_pretrained(model_path)
    llava_model = LlavaForConditionalGeneration.from_pretrained(model_path)

    prompt_text = "<image>\nWhat are these?"

    messages = [
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": prompt_text},
    ]

    prompt = llava_processor.tokenizer.apply_chat_template(
        messages, tokenize=False, template="{role}\n{content}", add_generation_prompt=True
    )

    print(prompt)

if __name__ == '__main__':
    # save_weights()
    test()

目前卡在了这一行

    prompt = llava_processor.tokenizer.apply_chat_template(
        messages, tokenize=False, template="{role}\n{content}", add_generation_prompt=True
    )

报错:

Traceback (most recent call last):
  File "/mnt/dolphinfs/ssd_pool/docker/user/hadoop-mlm/by/train_llava/build_model.py", line 65, in <module>
    test()
  File "/mnt/dolphinfs/ssd_pool/docker/user/hadoop-mlm/by/train_llava/build_model.py", line 53, in test
    prompt = llava_processor.tokenizer.apply_chat_template(
  File "/mnt/dolphinfs/ssd_pool/docker/user/hadoop-mlm/by/conda_env/by/lib/python3.10/site-packages/transformers/tokenization_utils_base.py", line 1803, in apply_chat_template
    chat_template = self.get_chat_template(chat_template, tools)
  File "/mnt/dolphinfs/ssd_pool/docker/user/hadoop-mlm/by/conda_env/by/lib/python3.10/site-packages/transformers/tokenization_utils_base.py", line 1964, in get_chat_template
    raise ValueError(
ValueError: Cannot use chat template functions because tokenizer.chat_template is not set and no template argument was passed! For information about writing templates and setting the tokenizer.chat_template attribute, please see the documentation at https://huggingface.co/docs/transformers/main/en/chat_templating

请教一下,我错在哪里了?实在不知道为啥。

yuanzhoulvpi2017 commented 3 weeks ago
  1. 用的是什么版本的transformers,版本太低的话,tokenizer是没有这个apply_chat_template方法的。
weiaicunzai commented 3 weeks ago
  1. 用的是什么版本的transformers,版本太低的话,tokenizer是没有这个apply_chat_template方法的。

感谢回复,我发现是全部processor,vit,llm 都存到model001文件夹了。