运行Phi-4-mini-instruct模型遇RuntimeError:张量维度2与3不匹配如何解决?
解决Phi-4-mini-instruct模型运行时的Tensor维度不匹配错误
问题描述
在Google Colab T4 Notebook中严格复制Phi-4-mini-instruct官方示例代码执行,出现以下错误:
RuntimeError: Tensors must have same number of dimensions: got 2 and 3
同时伴随警告:do_sample设为False但temperature设为0.0,该参数仅在采样生成模式下生效。
报错原因
- transformers版本不兼容:Phi-4系列模型依赖较新的transformers库版本,Colab默认安装的版本可能无法正确处理模型的对话格式输入,导致张量维度计算出错。
- 生成参数冲突:
do_sample=False(贪心搜索模式)下设置temperature=0.0属于无效配置,虽然是警告,但可能触发后续生成逻辑中的维度异常。
解决方案
方案1:升级依赖库并修正参数
首先在Colab中执行以下命令升级依赖:
!pip install --upgrade transformers accelerate torch
然后修改生成参数,移除temperature=0.0(因为do_sample=False时该参数无意义):
import torch from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline torch.random.manual_seed(0) model_path = "microsoft/Phi-4-mini-instruct" model = AutoModelForCausalLM.from_pretrained( model_path, device_map="auto", torch_dtype="auto", trust_remote_code=True, ) tokenizer = AutoTokenizer.from_pretrained(model_path) messages = [ {"role": "system", "content": "You are a helpful AI assistant."}, {"role": "user", "content": "Can you provide ways to eat combinations of bananas and dragonfruits?"}, {"role": "assistant", "content": "Sure! Here are some ways to eat bananas and dragonfruits together: 1. Banana and dragonfruit smoothie: Blend bananas and dragonfruits together with some milk and honey. 2. Banana and dragonfruit salad: Mix sliced bananas and dragonfruits together with some lemon juice and honey."}, {"role": "user", "content": "What about solving an 2x + 3 = 7 equation?"}, ] pipe = pipeline( "text-generation", model=model, tokenizer=tokenizer, ) generation_args = { "max_new_tokens": 500, "return_full_text": False, "do_sample": False, # 贪心搜索模式,无需temperature } output = pipe(messages, **generation_args) print(output[0]['generated_text'])
方案2:手动构建对话Prompt(避免pipeline自动处理的兼容性问题)
如果升级依赖后仍有问题,可以手动使用tokenizer的对话模板构建输入文本:
import torch from transformers import AutoModelForCausalLM, AutoTokenizer torch.random.manual_seed(0) model_path = "microsoft/Phi-4-mini-instruct" model = AutoModelForCausalLM.from_pretrained( model_path, device_map="auto", torch_dtype="auto", trust_remote_code=True, ) tokenizer = AutoTokenizer.from_pretrained(model_path) messages = [ {"role": "system", "content": "You are a helpful AI assistant."}, {"role": "user", "content": "Can you provide ways to eat combinations of bananas and dragonfruits?"}, {"role": "assistant", "content": "Sure! Here are some ways to eat bananas and dragonfruits together: 1. Banana and dragonfruit smoothie: Blend bananas and dragonfruits together with some milk and honey. 2. Banana and dragonfruit salad: Mix sliced bananas and dragonfruits together with some lemon juice and honey."}, {"role": "user", "content": "What about solving an 2x + 3 = 7 equation?"}, ] # 手动应用对话模板 prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True) inputs = tokenizer(prompt, return_tensors="pt").to(model.device) generation_args = { "max_new_tokens": 500, "return_full_text": False, "do_sample": False, } outputs = model.generate(**inputs, **generation_args) print(tokenizer.decode(outputs[0], skip_special_tokens=True))
内容的提问来源于stack exchange,提问作者최영진
相关产品推荐
相关产品推荐

