模型简介
模型特点
模型能力
使用案例
🚀 MistralLite模型
MistralLite是一个经过微调的Mistral-7B-v0.1语言模型,具备增强的长上下文处理能力(最多支持32K个标记)。在微调过程中,通过采用自适应旋转嵌入(Rotary Embedding)和滑动窗口,MistralLite能够在多个长上下文检索和问答任务中表现得更为出色,同时保留了原始模型简单的结构。MistralLite适用于长上下文行和主题检索、摘要生成、问答等应用场景。它可以部署在单个AWS g5.2x
实例上,并通过Sagemaker Huggingface文本生成推理(TGI)端点运行,非常适合在资源受限的环境中对性能有较高要求的应用。你也可以直接使用TGI Docker容器来运行MistralLite模型。此外,MistralLite还支持像vLLM这样的其他服务方式,你可以在Python中使用HuggingFace transformers和FlashAttention-2库来使用MistralLite。
✨ 主要特性
- 长上下文处理能力强:最多支持32K个标记的长上下文处理。
- 性能提升显著:在多个长上下文相关任务中表现优于Mistral-7B-Instruct-v0.1。
- 部署方式多样:支持多种部署方式,如在AWS实例上通过Sagemaker端点部署、使用TGI Docker容器部署、使用vLLM服务等。
📦 安装指南
使用Python代码(HuggingFace transformers)
需要安装以下必要的包:
- transformers 4.34.0 或更高版本
- flash-attn 2.3.1.post1 或更高版本
- accelerate 0.23.0 或更高版本
pip install transformers==4.34.0
pip install flash-attn==2.3.1.post1 --no-build-isolation
pip install accelerate==0.23.0
💻 使用示例
基础用法
from transformers import AutoModelForCausalLM, AutoTokenizer
import transformers
import torch
model_id = "amazon/MistralLite"
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(model_id,
torch_dtype=torch.bfloat16,
use_flash_attention_2=True,
device_map="auto",)
pipeline = transformers.pipeline(
"text-generation",
model=model,
tokenizer=tokenizer,
)
prompt = "<|prompter|>What are the main challenges to support a long context for LLM?</s><|assistant|>"
sequences = pipeline(
prompt,
max_new_tokens=400,
do_sample=False,
return_full_text=False,
num_return_sequences=1,
eos_token_id=tokenizer.eos_token_id,
)
for seq in sequences:
print(f"{seq['generated_text']}")
重要提示:使用MistralLite时,请使用以下提示模板:
<|prompter|>What are the main challenges to support a long context for LLM?</s><|assistant|>
📚 详细文档
与Mistral-7B-Instruct-v0.1的对比
MistralLite与Mistral-7B-Instruct-v0.1相似,它们的异同点总结如下:
属性 | 详情 |
---|---|
模型类型 | Mistral-7B-v0.1微调版本 |
训练数据 | SLidingEncoder and Decoder (SLED)、(Long) Natural Questions (NQ)、OpenAssistant Conversations Dataset (OASST1) |
最大上下文长度 | 32K |
旋转嵌入适配 | Mistral-7B-Instruct-v0.1:rope_theta = 10000;MistralLite:rope_theta = 1000000 |
滑动窗口大小 | Mistral-7B-Instruct-v0.1:4096;MistralLite:16384 |
开发动机
自Mistral-7B-Instruct-v0.1发布以来,该模型因其在广泛基准测试中的出色表现而越来越受欢迎。但大多数基准测试是在短上下文
上进行评估的,对于其在长上下文任务中的性能研究较少。因此,我们针对专门评估大语言模型处理长上下文能力的基准测试对Mistral-7B-Instruct-v0.1
进行了评估。尽管该模型在小于4096个标记的长上下文上表现具有一定竞争力,但在更长的上下文上存在一些性能限制。为了提高其在长上下文上的性能,我们对Mistral 7B模型进行了微调,得到了MistralLite
。该模型在长上下文处理性能上相较于Mistral-7B-Instruct-v0.1有了显著提升
。详细的长上下文评估结果
如下:
-
主题检索 | 模型名称 | 输入长度2851 | 输入长度5568 | 输入长度8313 | 输入长度11044 | 输入长度13780 | |----------|-------------:|-------------:|------------:|-----------:|-----------:| | Mistral-7B-Instruct-v0.1 | 100% | 50% | 2% | 0% | 0% | | MistralLite | 100% | 100% | 100% | 100% | 98% |
-
行检索 | 模型名称 | 输入长度3818 | 输入长度5661 | 输入长度7505 | 输入长度9354 | 输入长度11188 | 输入长度12657 | |----------|-------------:|-------------:|------------:|-----------:|-----------:|-----------:| | Mistral-7B-Instruct-v0.1 | 98% | 62% | 42% | 42% | 32% | 30% | | MistralLite | 98% | 92% | 88% | 76% | 70% | 60% |
-
密钥检索 | 模型名称 | 输入长度3264 | 输入长度5396 | 输入长度8329 | 输入长度10197 | |----------|-------------:|-------------:|------------:|-----------:| | Mistral-7B-Instruct-v0.1 | 100% | 50% | 20% | 30% | | MistralLite | 100% | 100% | 100% | 100% |
-
长输入文本问答 | 模型名称 | 测试集准确率 | 难子集准确率 | |----------|-------------:|-------------:| | Mistral-7B-Instruct-v0.1 | 44.3% | 39.7% | | MistralLite | 64.4% | 56.2% |
13400个标记长上下文的示例测试
- 上下文来自Amazon Aurora常见问题解答
- 问题:请告诉我pgvector如何助力生成式AI,并给我一些示例。
- MistralLite的回答:
pgvector是Amazon Aurora PostgreSQL兼容版支持的PostgreSQL开源扩展。
你可以使用pgvector在数据库中存储、搜索、索引和查询由机器学习(ML)和人工智能(AI)模型生成的数十亿个嵌入向量,例如来自Amazon Bedrock(有限预览版)或Amazon SageMaker的嵌入向量。向量嵌入是一种数值表示,代表文本、图像和视频等内容的语义含义。
使用pgvector,你可以在Aurora PostgreSQL数据库中查询嵌入向量,对这些以向量形式表示的数据类型与Aurora中的其他表格数据进行高效的语义相似性搜索。这使得生成式AI和其他AI/ML系统能够用于新型应用,例如基于相似文本描述或图像的个性化推荐、基于面试笔记的候选人匹配、基于成功对话记录或聊天会话的客户服务下一步最佳行动推荐等等。
MistralLite LM-Eval结果
方法
- 请参考https://huggingface.co/spaces/HuggingFaceH4/open_llm_leaderboard
- 版本:4ececff
- 注意:我们使用
--model hf-causal-experimental
而不是--model hf-causal
结果
平均值 | hellaswag | arc_challenge | truthful_qa (mc2) | MMLU (acc) |
---|---|---|---|---|
0.57221 | 0.81617 | 0.58874 | 0.38275 | 0.5012 |
🔧 技术细节
- 开发者:AWS贡献者
- 模型类型:Mistral-7B-v0.1
- 语言:英语
- 微调基础权重:Mistral-7B-v0.1
- 支持的服务框架:
- 模型许可证:Apache 2.0
- 联系方式:GitHub问题
- 推理代码:GitHub仓库
📄 许可证
本模型采用Apache 2.0许可证。
不同服务方式说明
在TGI上服务MistralLite
重要提示:
启动TGI服务器
使用TGI 1.1.0或更高版本。官方Docker容器为:ghcr.io/huggingface/text-generation-inference:1.1.0
示例Docker参数:
docker run -d --gpus all --shm-size 1g -p 443:80 -v $(pwd)/models:/data ghcr.io/huggingface/text-generation-inference:1.1.0 \
--model-id amazon/MistralLite \
--max-input-length 16000 \
--max-total-tokens 16384 \
--max-batch-prefill-tokens 16384 \
--trust-remote-code
进行推理
使用TGI进行推理的示例Python代码(需要text_generation
0.6.1或更高版本):
pip install text_generation==0.6.1
from text_generation import Client
SERVER_PORT = 443
SERVER_HOST = "localhost"
SERVER_URL = f"{SERVER_HOST}:{SERVER_PORT}"
tgi_client = Client(f"http://{SERVER_URL}", timeout=60)
def invoke_tgi(prompt,
random_seed=1,
max_new_tokens=400,
print_stream=True,
assist_role=True):
if (assist_role):
prompt = f"<|prompter|>{prompt}</s><|assistant|>"
output = ""
for response in tgi_client.generate_stream(
prompt,
do_sample=False,
max_new_tokens=max_new_tokens,
return_full_text=False,
#temperature=None,
#truncate=None,
#seed=random_seed,
#typical_p=0.2,
):
if hasattr(response, "token"):
if not response.token.special:
snippet = response.token.text
output += snippet
if (print_stream):
print(snippet, end='', flush=True)
return output
prompt = "What are the main challenges to support a long context for LLM?"
result = invoke_tgi(prompt)
重要提示:首次使用MistralLite进行推理时,可能需要一个短暂的“预热”期,这可能需要10多秒。不过,后续的推理应该会更快,并能更及时地返回结果。这个预热期是正常的,一旦初始化期完成,不会影响系统的整体性能。
在Amazon SageMaker上部署MistralLite
重要提示:
安装必要的包
需要安装sagemaker 2.192.1或更高版本。
pip install sagemaker==2.192.1
将模型部署为SageMaker端点
要在SageMaker端点上部署MistralLite,请按照以下示例代码操作:
import sagemaker
from sagemaker.huggingface import HuggingFaceModel, get_huggingface_llm_image_uri
import time
sagemaker_session = sagemaker.Session()
region = sagemaker_session.boto_region_name
role = sagemaker.get_execution_role()
image_uri = get_huggingface_llm_image_uri(
backend="huggingface", # or lmi
region=region,
version="1.1.0"
)
model_name = "MistralLite-" + time.strftime("%Y-%m-%d-%H-%M-%S", time.gmtime())
hub = {
'HF_MODEL_ID':'amazon/MistralLite',
'HF_TASK':'text-generation',
'SM_NUM_GPUS':'1',
"MAX_INPUT_LENGTH": '16000',
"MAX_TOTAL_TOKENS": '16384',
"MAX_BATCH_PREFILL_TOKENS": '16384',
"MAX_BATCH_TOTAL_TOKENS": '16384',
}
model = HuggingFaceModel(
name=model_name,
env=hub,
role=role,
image_uri=image_uri
)
predictor = model.deploy(
initial_instance_count=1,
instance_type="ml.g5.2xlarge",
endpoint_name=model_name,
)
进行推理
要调用端点,请按照以下示例代码操作:
input_data = {
"inputs": "<|prompter|>What are the main challenges to support a long context for LLM?</s><|assistant|>",
"parameters": {
"do_sample": False,
"max_new_tokens": 400,
"return_full_text": False,
#"typical_p": 0.2,
#"temperature":None,
#"truncate":None,
#"seed": 1,
}
}
result = predictor.predict(input_data)[0]["generated_text"]
print(result)
或者通过boto3,示例代码如下:
import boto3
import json
def call_endpoint(client, prompt, endpoint_name, paramters):
client = boto3.client("sagemaker-runtime")
payload = {"inputs": prompt,
"parameters": parameters}
response = client.invoke_endpoint(EndpointName=endpoint_name,
Body=json.dumps(payload),
ContentType="application/json")
output = json.loads(response["Body"].read().decode())
result = output[0]["generated_text"]
return result
client = boto3.client("sagemaker-runtime")
parameters = {
"do_sample": False,
"max_new_tokens": 400,
"return_full_text": False,
#"typical_p": 0.2,
#"temperature":None,
#"truncate":None,
#"seed": 1,
}
endpoint_name = predictor.endpoint_name
prompt = "<|prompter|>What are the main challenges to support a long context for LLM?</s><|assistant|>"
result = call_endpoint(client, prompt, endpoint_name, parameters)
print(result)
在vLLM上服务MistralLite
关于安装和使用vLLM的文档请参考此处。 重要提示:如需端到端示例Jupyter笔记本,请参考此链接。
将vLLM用作服务器
将vLLM用作服务器时,传递--model amazon/MistralLite
参数,例如:
python3 -m vllm.entrypoints.api_server --model amazon/MistralLite
在Python代码中使用vLLM
在Python代码中使用vLLM时,请参考以下示例代码:
from vllm import LLM, SamplingParams
prompts = [
"<|prompter|>What are the main challenges to support a long context for LLM?</s><|assistant|>",
]
sampling_params = SamplingParams(temperature=0, max_tokens=100)
llm = LLM(model="amazon/MistralLite",)
outputs = llm.generate(prompts, sampling_params)
# 打印输出。
for output in outputs:
prompt = output.prompt
generated_text = output.outputs[0].text
print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}")
局限性
在使用MistralLite模型之前,进行独立评估并采取措施确保你的使用符合你自己的特定质量控制实践和标准,以及符合适用于你和你的内容的当地规则、法律、法规、许可证和条款是非常重要的。



