模型简介
模型特点
模型能力
使用案例
🚀 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-v0.1模型的简单结构。
- 多服务方式支持:支持TGI、vLLM等服务方式,可在Python中使用相关库调用。
- 高性能部署:可以部署在单个AWS
g5.2x
实例上,适合资源受限环境。
📦 安装指南
从Python代码(HuggingFace transformers)使用MistralLite
需要安装以下必要的包:
- 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
在TGI上运行MistralLite
使用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
在Amazon SageMaker上部署MistralLite
需要安装sagemaker 2.192.1或更高版本:
pip install sagemaker==2.192.1
在vLLM上运行MistralLite
安装和使用vLLM的文档请参考这里。
💻 使用示例
基础用法
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']}")
高级用法
在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)
在Amazon SageMaker上进行推理
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)
在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)
# Print the outputs.
for output in outputs:
prompt = output.prompt
generated_text = output.outputs[0].text
print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}")
📚 详细文档
MistralLite与Mistral-7B-Instruct-v0.1的对比
模型 | 长上下文微调情况 | 最大上下文长度 | 旋转嵌入适配 | 滑动窗口大小 |
---|---|---|---|---|
Mistral-7B-Instruct-v0.1 | 最多8K个标记 | 32K | rope_theta = 10000 | 4096 |
MistralLite | 最多16K个标记 | 32K | rope_theta = 1000000 | 16384 |
开发MistralLite的动机
自Mistral-7B-Instruct-v0.1发布以来,该模型因其在广泛基准测试中的出色表现而越来越受欢迎。但大多数基准测试是在短上下文
上进行评估的,对于其在长上下文任务中的性能研究较少。因此,我们针对专门设计用于评估大语言模型处理长上下文能力的基准测试对Mistral-7B-Instruct-v0.1
进行了评估。尽管该模型在小于4096个标记的长上下文上表现具有一定竞争力,但在更长上下文上的性能存在一些局限性。为了提高其在更长上下文上的性能,我们对Mistral 7B模型进行了微调,得到了MistralLite
。该模型在长上下文处理性能上相较于Mistral-7B-Instruct-v0.1有了显著提升
。详细的长上下文评估结果
如下:
1. 主题检索
模型名称 | 输入长度2851 | 输入长度5568 | 输入长度8313 | 输入长度11044 | 输入长度13780 |
---|---|---|---|---|---|
Mistral-7B-Instruct-v0.1 | 100% | 50% | 2% | 0% | 0% |
MistralLite | 100% | 100% | 100% | 100% | 98% |
2. 行检索
模型名称 | 输入长度3818 | 输入长度5661 | 输入长度7505 | 输入长度9354 | 输入长度11188 | 输入长度12657 |
---|---|---|---|---|---|---|
Mistral-7B-Instruct-v0.1 | 98% | 62% | 42% | 42% | 32% | 30% |
MistralLite | 98% | 92% | 88% | 76% | 70% | 60% |
3. 密钥检索
模型名称 | 输入长度3264 | 输入长度5396 | 输入长度8329 | 输入长度10197 |
---|---|---|---|---|
Mistral-7B-Instruct-v0.1 | 100% | 50% | 20% | 30% |
MistralLite | 100% | 100% | 100% | 100% |
4. 长输入文本问答
模型名称 | 测试集准确率 | 困难子集准确率 |
---|---|---|
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系统可用于新型应用,例如基于相似文本描述或图像的个性化推荐、基于面试笔记的候选人匹配、基于成功记录或聊天会话对话的客户服务下一步最佳行动推荐等。
模型详情
属性 | 详情 |
---|---|
开发者 | AWS贡献者 |
模型类型 | Mistral-7B-v0.1 |
语言 | 英语 |
微调权重来源 | Mistral-7B-v0.1 |
微调数据 | SLidingEncoder and Decoder (SLED)、(Long) Natural Questions (NQ)、OpenAssistant Conversations Dataset (OASST1) |
支持的服务框架 | Text-Generation-Inference 1.1.0、vLLM、HuggingFace transformers、HuggingFace Text Generation Inference (TGI) container on SageMaker |
模型许可证 | Apache 2.0 |
联系方式 | GitHub问题 |
推理代码 | Github仓库 |
MistralLite LM-Eval结果
方法
请参考https://huggingface.co/spaces/HuggingFaceH4/open_llm_leaderboard 版本号:revision=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 |
🔧 技术细节
在微调过程中,MistralLite采用了自适应旋转嵌入(Rotary Embedding)和滑动窗口技术,以增强其长上下文处理能力。旋转嵌入的参数调整为rope_theta = 1000000,滑动窗口大小设置为16384,这些调整使得模型在长上下文任务中能够更好地捕捉上下文信息。同时,模型使用了FlashAttention-2技术,提高了计算效率。
📄 许可证
本模型采用Apache 2.0许可证。
⚠️ 重要提示
- 使用MistralLite时,请使用以下提示模板:
<|prompter|>What are the main challenges to support a long context for LLM?</s><|assistant|>
- 首次使用MistralLite进行推理时,可能需要一段短暂的“预热”时间,大约需要10秒。但后续推理会更快,能更及时地返回结果。这种预热时间是正常现象,完成初始化后不会影响系统的整体性能。
- 在使用MistralLite模型之前,重要的是进行自己的独立评估,并采取措施确保你的使用符合你自己的特定质量控制实践和标准,以及符合适用于你和你的内容的当地规则、法律、法规、许可证和条款。
💡 使用建议



