You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

合并LoRA到基础模型时遇KeyError:模块名含'.'问题求助

解决LoRA合并时的KeyError问题

问题原因

你代码里的PeftModel.from_pretrained(base_model, peft_model_path, output_dir)这一行错误地将输出路径作为第三个位置参数传入。PeftModel.from_pretrained的第三个参数是adapter_name,它要求名称不能包含".",而你的路径里有.cache,直接触发了错误。之前能运行可能是因为没加这个参数,或者版本更新后参数校验逻辑变严格了。

修复步骤

  • 移除PeftModel.from_pretrained调用中的output_dir参数,这个参数不应该在这里传入,输出目录是后续save_pretrained时指定的。
  • 可选:output_dir的括号可以去掉,不影响功能但代码更简洁。

修正后的完整代码

import torch
import os
import logging
import argparse
from tqdm import tqdm
from transformers import AutoModelForCausalLM, AutoTokenizer
from peft import PeftModel


base_model_name_or_path = 'NousResearch/Llama-2-13b-hf'
peft_model_path = 'FinGPT/fingpt-sentiment_llama2-13b_lora'
output_dir = "C:/Users/tjs/.cache/huggingface/FinGPT"
device = "auto"
push_to_hub = False

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)


try:
    if device == 'auto':
        device_arg = {'device_map': 'auto'}
    else:
        device_arg = {'device_map': {"": device}}

    logger.info(f"Loading base model: {base_model_name_or_path}")
    with tqdm(total=1, desc="Loading base model") as pbar:
        base_model = AutoModelForCausalLM.from_pretrained(
            base_model_name_or_path,
            return_dict=True,
            torch_dtype=torch.float16,
            **device_arg
        )
        pbar.update(1)  

    logger.info(f"Loading Peft: {peft_model_path}")
    with tqdm(total=1, desc="Loading Peft model") as pbar:
        # 移除了多余的output_dir参数
        model = PeftModel.from_pretrained(base_model, peft_model_path)
        pbar.update(1)

    logger.info("Running merge_and_unload")
    with tqdm(total=1, desc="Merge and Unload") as pbar:
        model = model.merge_and_unload()
        pbar.update(1)

    tokenizer = AutoTokenizer.from_pretrained(base_model_name_or_path)

    model.save_pretrained(output_dir)
    tokenizer.save_pretrained(output_dir)
    logger.info(f"Model saved to {output_dir}")

except Exception as e:
    logger.exception("An error occurred:")
    raise

内容的提问来源于stack exchange,提问作者user24034470

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 13:20:21