m0_73786595 2025-01-20 17:47 采纳率: 25%
浏览 61

ChatGLM4-9B推理报错


 
Special tokens have been added in the vocabulary, make sure the associated word embeddings are fine-tuned or trained.
Loading checkpoint shards: 100%|███████████████████████████████████████████████████████| 10/10 [00:11<00:00,  1.10s/it]
Some parameters are on the meta device because they were offloaded to the cpu.
Welcome to the GLM-4-9B CLI chat. Type your messages below.
 
You: hello
GLM-4:
Exception in thread Thread-2 (generate):
Traceback (most recent call last):
  File "D:\conda\envs\env_name\lib\threading.py", line 1016, in _bootstrap_inner
    self.run()
  File "D:\conda\envs\env_name\lib\threading.py", line 953, in run
    self._target(*self._args, **self._kwargs)
  File "D:\conda\envs\env_name\lib\site-packages\torch\utils\_contextlib.py", line 116, in decorate_context
    return func(*args, **kwargs)
  File "D:\conda\envs\env_name\lib\site-packages\transformers\generation\utils.py", line 1622, in generate
    result = self._sample(
  File "D:\conda\envs\env_name\lib\site-packages\transformers\generation\utils.py", line 2841, in _sample
    model_kwargs = self._update_model_kwargs_for_generation(
  File "C:\Users\Administrator\.cache\huggingface\modules\transformers_modules\glm-4-9b-chat\modeling_chatglm.py", line 929, in _update_model_kwargs_for_generation
    cache_name, cache = self._extract_past_from_model_output(outputs)
ValueError: too many values to unpack (expected 2)

transformers包换了4.40.0(官方要求), 4.40.2, 4.39.3都没用,不知怎么办了

img

另外outputs.past_key_values的值是不是有问题?

  • 写回答

3条回答 默认 最新

  • master_chenchengg 2025-01-23 19:14
    关注

    答案来星火AI,希望能对题主有所帮助!

    ChatGLM4-9B推理报错可能涉及多个方面的问题,包括模型配置、输入数据格式、环境依赖等。以下是一些常见的原因和解决方法:

    1. 模型配置问题

    解释

    模型配置文件中可能存在错误或不兼容的参数设置,导致推理时出现异常。

    示例

    {
        "model_name": "chatglm4-9b",
        "max_length": 512,
        "temperature": 0.7,
        "top_p": 0.9
    }
    

    确保配置文件中的参数与模型要求一致。

    2. 输入数据格式问题

    解释

    输入数据格式不正确,例如缺少必要的字段或字段值不符合预期格式,可能导致推理失败。

    示例

    input_data = {
        "prompt": "你好,今天天气怎么样?",
        "max_tokens": 100,
        "temperature": 0.7
    }
    

    确保输入数据包含所有必需的字段,并且字段值符合预期格式。

    3. 环境依赖问题

    解释

    运行环境可能缺少某些依赖库或版本不兼容,导致推理过程中出现错误。

    示例

    pip install torch==1.10.0 transformers==4.15.0
    

    确保安装的库版本与模型要求一致。

    4. 代码实现问题

    解释

    代码实现中可能存在逻辑错误或未处理的异常情况,导致推理失败。

    示例

    import torch
    from transformers import AutoModelForCausalLM, AutoTokenizer
    
    def chat(prompt):
        model_name = "THUDM/chatglm4-9b"
        tokenizer = AutoTokenizer.from_pretrained(model_name)
        model = AutoModelForCausalLM.from_pretrained(model_name)
        
        inputs = tokenizer(prompt, return_tensors="pt")
        outputs = model.generate(**inputs, max_length=100, temperature=0.7)
        return tokenizer.decode(outputs[0], skip_special_tokens=True)
    
    try:
        response = chat("你好,今天天气怎么样?")
        print(response)
    except Exception as e:
        print(f"Error during inference: {e}")
    

    在代码中添加异常处理,以便更好地调试和定位问题。

    5. 硬件资源不足

    解释

    推理过程中可能需要大量的计算资源,如果硬件资源不足(如显存不足),可能导致推理失败。

    示例

    import torch
    
    if not torch.cuda.is_available():
        raise RuntimeError("CUDA is not available. Please check your hardware configuration.")
    else:
        device = torch.device("cuda")
        model.to(device)
    

    确保有足够的硬件资源,并正确配置设备。

    总结

    ChatGLM4-9B推理报错可能由多种原因引起,包括模型配置、输入数据格式、环境依赖、代码实现以及硬件资源等。通过逐一排查这些可能的原因,可以找到并解决问题。

    评论

报告相同问题?

问题事件

  • 创建了问题 1月20日