Llama 2-7B模型输出限制在511个标记的问题

我在使用Llama 2-7B模型时遇到了一个问题,模型的输出始终被限制在仅511个标记,尽管理论上该模型应该能够生成最多4096个标记的输出。

我尝试将max_tokens参数设置为更高的值,例如3000,并通过从模型的总标记限制(4096个标记)中减去提示标记来计算可用标记。然而,尽管进行了这些调整,我仍然只能收到最多511个标记的输出。

以下是我用来与模型交互的代码片段:

import psutilimport osimport warningsfrom llama_cpp import Llama# Suppress warningswarnings.filterwarnings("ignore")# Path to the modelmodel_path = "C:/Llama_project/models/llama-2-7b-chat.Q2_K.gguf"# Load the modelllm = Llama(model_path=model_path)# System message to set the behavior of the assistantsystem_message = "You are a helpful assistant."# Function to ask questionsdef ask_question(question):    # Use user input for the question prompt    prompt = f"Answer the following question: {question}"    # Calculate the remaining tokens for output based on the model's 4096 token limit    prompt_tokens = len(prompt.split())  # Rough token count estimate    max_output_tokens = 4096 - prompt_tokens  # Tokens left for output        # Monitor memory usage before calling the model    process = psutil.Process(os.getpid())    mem_before = process.memory_info().rss / 1024 ** 2  # Memory in MB    # Get the output from the model with the calculated max tokens for output    output = llm(prompt=prompt, max_tokens=max_output_tokens, temperature=0.7, top_p=1.0)    # Monitor memory usage after calling the model    mem_after = process.memory_info().rss / 1024 ** 2  # Memory in MB        # Clean the output and return only the answer text    return output["choices"][0]["text"].strip()# Main loop for user interactionwhile True:    user_input = input("Ask a question (or type 'exit' to quit): ")        if user_input.lower() == 'exit':        print("Exiting the program.")        break        # Get the model's response    answer = ask_question(user_input)        # Print only the answer    print(f"Answer: {answer}")

问题详情:

  • 模型:Llama 2-7B(Q2_K版本)
  • 预期输出:我期望得到接近最大标记限制的响应(3000个或更多标记)。
  • 实际输出:无论提示长度如何,输出都被限制在511个标记。

尝试过:

  • 将max_tokens设置为3000或更高。
  • 通过从模型的总标记限制中减去提示长度来计算可用标记。

我期望模型能够生成接近标记限制的响应(理想情况下接近3000个标记或更多,具体取决于输入),但它始终生成限制在511个标记的输出。


回答:

尝试在Llama构造函数中将n_ctx设置为2048,如下所示:

Llama(n_ctx=2048, model_path=model_path)

这个参数告诉模型提示和响应的最大长度总和是多少。

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注