模型概述
模型特點
模型能力
使用案例
🚀 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模型之前,重要的是進行自己的獨立評估,並採取措施確保你的使用符合你自己的特定質量控制實踐和標準,以及符合適用於你和你的內容的當地規則、法律、法規、許可證和條款。
💡 使用建議



