使用HuggingFacePipeline加载Llama-2-7b-hf却显示GPT2的问题排查
问题:LangChain中HuggingFacePipeline显示加载gpt2而非Llama-2-7b-hf
我尝试在LangChain中通过HuggingFacePipeline运行meta-llama/Llama-2-7b-hf,代码配置如下,但打印llm实例时发现显示加载的是gpt2模型,明明已经通过transformers.AutoModelForCausalLM.from_pretrained指定了meta-llama/Llama-2-7b-hf,想知道哪里操作有误?
bnb_config = transformers.BitsAndBytesConfig( load_in_4bit=True, bnb_4bit_quant_type='nf4', bnb_4bit_use_double_quant=True, bnb_4bit_compute_dtype=bfloat16, # llm_int8_enable_fp32_cpu_offload=True ) model_config = transformers.AutoConfig.from_pretrained( "meta-llama/Llama-2-7b-hf", use_auth_token=hf_auth ) tokenizer = transformers.AutoTokenizer.from_pretrained("meta-llama/Llama-2-7b-hf") model = transformers.AutoModelForCausalLM.from_pretrained( "meta-llama/Llama-2-7b-hf", trust_remote_code=True, config=model_config, quantization_config=bnb_config, device_map="auto", use_auth_token=hf_auth ) model.eval() pipe = transformers.pipeline( model=model, tokenizer=tokenizer, task="text-generation", return_full_text=True, temperature=0.1, # 'randomness' of outputs, 0.0 is the min and 1.0 the max max_new_tokens=64, # mex number of tokens to generate in the output repetition_penalty=1.1 # without this output begins repeating ) llm = HuggingFacePipeline(pipeline=pipe) print(llm)
输出结果:
>>>HuggingFacePipeline Params: {'model_id': 'gpt2', 'model_kwargs': None}
原因与解决方法
这是LangChain的HuggingFacePipeline类的显示问题,并非真的加载了gpt2模型:当直接传入预构建的transformers pipeline实例时,LangChain无法自动识别正确的模型ID,会默认显示
gpt2,但实际运行的仍是你加载的Llama-2-7b-hf模型。验证实际模型:可以执行文本生成测试确认:
response = llm.predict("Hello, how are you?") print(response)
从生成内容的风格和特征就能判断是否为Llama-2的输出。
- 修正打印显示的模型ID:初始化HuggingFacePipeline时手动指定
model_id参数即可:
llm = HuggingFacePipeline(pipeline=pipe, model_id="meta-llama/Llama-2-7b-hf") print(llm)
此时打印结果会显示正确的模型ID。
内容的提问来源于stack exchange,提问作者Eric Kumar
相关产品推荐
相关产品推荐

