模型概述
模型特點
模型能力
使用案例
🚀 Qwen2.5-7B-Instruct-1M GGUF模型
本項目提供了Qwen2.5-7B-Instruct-1M的GGUF模型,採用了超低位量化技術,在保持高精度的同時顯著減少了內存佔用。這些模型適用於多種硬件環境,可根據不同的硬件能力和內存限制進行選擇。
🚀 快速開始
環境要求
Qwen2.5的代碼已集成在最新的Hugging face transformers
庫中,建議使用最新版本的 transformers
。若使用 transformers<4.37.0
,會出現如下錯誤:
KeyError: 'qwen2'
代碼示例
以下是一個使用 apply_chat_template
加載分詞器和模型並生成內容的代碼片段:
from transformers import AutoModelForCausalLM, AutoTokenizer
model_name = "Qwen/Qwen2.5-7B-Instruct-1M"
model = AutoModelForCausalLM.from_pretrained(
model_name,
torch_dtype="auto",
device_map="auto"
)
tokenizer = AutoTokenizer.from_pretrained(model_name)
prompt = "Give me a short introduction to large language model."
messages = [
{"role": "system", "content": "You are Qwen, created by Alibaba Cloud. You are a helpful assistant."},
{"role": "user", "content": prompt}
]
text = tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True
)
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
generated_ids = model.generate(
**model_inputs,
max_new_tokens=512
)
generated_ids = [
output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
]
response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
✨ 主要特性
超低位量化技術
我們最新的量化方法為超低位模型(1 - 2位)引入了精度自適應量化,並在 Llama-3-8B 上通過基準測試驗證了其有效性。該方法採用特定層策略,在保持極高內存效率的同時保留了準確性。
- 動態精度分配:
- 前/後25%的層 → IQ4_XS(選定層)
- 中間50% → IQ2_XXS/IQ3_S(提高效率)
- 關鍵組件保護:
- 嵌入層/輸出層使用Q5_K
- 與標準1 - 2位量化相比,誤差傳播降低38%
量化性能對比(Llama-3-8B)
量化方式 | 標準困惑度 | DynamicGate困惑度 | 困惑度變化 | 標準大小 | DG大小 | 大小變化 | 標準速度 | DG速度 |
---|---|---|---|---|---|---|---|---|
IQ2_XXS | 11.30 | 9.84 | -12.9% | 2.5G | 2.6G | +0.1G | 234s | 246s |
IQ2_XS | 11.72 | 11.63 | -0.8% | 2.7G | 2.8G | +0.1G | 242s | 246s |
IQ2_S | 14.31 | 9.02 | -36.9% | 2.7G | 2.9G | +0.2G | 238s | 244s |
IQ1_M | 27.46 | 15.41 | -43.9% | 2.2G | 2.5G | +0.3G | 206s | 212s |
IQ1_S | 53.07 | 32.00 | -39.7% | 2.1G | 2.4G | +0.3G | 184s | 209s |
關鍵改進:
- 🔥 IQ1_M 的困惑度大幅降低43.9%(從27.46降至15.41)
- 🚀 IQ2_S 的困惑度降低36.9%,僅增加0.2GB
- ⚡ IQ1_S 儘管採用1位量化,但仍保持39.7%的更高準確性
權衡:
- 所有變體的大小均有適度增加(0.1 - 0.3GB)
- 推理速度相當(差異小於5%)
模型使用場景
📌 將模型裝入GPU顯存 ✔ 內存受限的部署環境 ✔ 可容忍1 - 2位誤差的CPU和邊緣設備 ✔ 超低位量化研究
📦 安裝指南
系統準備
為獲得最佳性能,建議使用支持優化內核的Ampere或Hopper架構的GPU。確保系統滿足以下要求:
- CUDA版本:12.1或12.3
- Python版本:>=3.9且<=3.12
顯存要求:
- 處理100萬令牌序列時:
- Qwen2.5-7B-Instruct-1M:至少120GB顯存(跨GPU總和)
- Qwen2.5-14B-Instruct-1M:至少320GB顯存(跨GPU總和)
若GPU顯存不足,仍可使用Qwen2.5-1M處理較短任務。
安裝依賴
目前,需要從我們的自定義分支克隆vLLM倉庫並手動安裝。我們正在努力將該分支合併到主vLLM項目中。
git clone -b dev/dual-chunk-attn git@github.com:QwenLM/vllm.git
cd vllm
pip install -e . -v
啟動vLLM
vLLM支持離線推理或啟動類似OpenAI的服務器。
離線推理示例
from transformers import AutoTokenizer
from vllm import LLM, SamplingParams
# Initialize the tokenizer
tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2.5-7B-Instruct-1M")
# Pass the default decoding hyperparameters of Qwen2.5-7B-Instruct
# max_tokens is for the maximum length for generation.
sampling_params = SamplingParams(temperature=0.7, top_p=0.8, repetition_penalty=1.05, max_tokens=512)
# Input the model name or path. See below for parameter explanation (after the example of openai-like server).
llm = LLM(model="Qwen/Qwen2.5-7B-Instruct-1M",
tensor_parallel_size=4,
max_model_len=1010000,
enable_chunked_prefill=True,
max_num_batched_tokens=131072,
enforce_eager=True,
# quantization="fp8", # Enabling FP8 quantization for model weights can reduce memory usage.
)
# Prepare your prompts
prompt = "Tell me something about large language models."
messages = [
{"role": "system", "content": "You are Qwen, created by Alibaba Cloud. You are a helpful assistant."},
{"role": "user", "content": prompt}
]
text = tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True
)
# generate outputs
outputs = llm.generate([text], 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}")
類似OpenAI服務器示例
vllm serve Qwen/Qwen2.5-7B-Instruct-1M \
--tensor-parallel-size 4 \
--max-model-len 1010000 \
--enable-chunked-prefill --max-num-batched-tokens 131072 \
--enforce-eager \
--max-num-seqs 1
# --quantization fp8 # Enabling FP8 quantization for model weights can reduce memory usage.
之後,可使用curl或Python與部署的模型進行交互。
參數解釋:
--tensor-parallel-size
:設置為使用的GPU數量。7B模型最多使用4個GPU,14B模型最多使用8個GPU。--max-model-len
:定義最大輸入序列長度。若遇到內存不足問題,可減小此值。--max-num-batched-tokens
:設置分塊預填充的塊大小。較小的值可減少激活內存使用,但可能會減慢推理速度。建議設置為131072以獲得最佳性能。--max-num-seqs
:限制併發處理的序列數量。
也可參考我們的文檔瞭解vLLM的使用方法。
故障排除
- 遇到錯誤:"The model's max sequence length (xxxxx) is larger than the maximum number of tokens that can be stored in the KV cache."
KV緩存預留的顯存不足。可考慮減小
max_model_len
或增加tensor_parallel_size
。或者,可減小max_num_batched_tokens
,但這可能會顯著減慢推理速度。 - 遇到錯誤:"torch.OutOfMemoryError: CUDA out of memory."
激活權重預留的顯存不足。可嘗試將
gpu_memory_utilization
設置為0.85或更低,但需注意這可能會減少KV緩存可用的顯存。 - 遇到錯誤:"Input prompt (xxxxx tokens) + lookahead slots (0) is too long and exceeds the capacity of the block manager."
輸入過長。可考慮使用較短的序列或增加
max_model_len
。
💻 使用示例
處理超長文本
為提高長序列的處理準確性和效率,我們基於vLLM開發了一個先進的推理框架,集成了稀疏注意力和長度外推技術。該方法顯著提高了模型對超過256K令牌序列的生成性能,並使長達1M令牌的序列推理速度提高3至7倍。
以下是使用我們的框架部署Qwen2.5-1M模型的詳細步驟:
選擇合適的模型格式
選擇正確的模型格式取決於您的硬件能力和內存限制。
模型格式 | 精度 | 內存使用 | 設備要求 | 最佳使用場景 |
---|---|---|---|---|
BF16 | 最高 | 高 | 支持BF16的GPU/CPU | 減少內存的高速推理 |
F16 | 高 | 高 | 支持FP16的設備 | BF16不可用時的GPU推理 |
Q4_K | 中低 | 低 | CPU或低顯存設備 | 內存受限環境 |
Q6_K | 中 | 中等 | 內存較多的CPU | 量化模型中較高準確性 |
Q8_0 | 高 | 中等 | 有足夠顯存的CPU或GPU | 量化模型中最佳準確性 |
IQ3_XS | 非常低 | 非常低 | 超低內存設備 | 極致內存效率和低準確性 |
Q4_0 | 低 | 低 | ARM或低內存設備 | llama.cpp可針對ARM設備優化 |
包含的文件及詳情
Qwen2.5-7B-Instruct-1M-bf16.gguf
:模型權重保存為BF16格式。若要將模型重新量化為其他格式,可使用此文件。適用於支持BF16加速的設備。Qwen2.5-7B-Instruct-1M-f16.gguf
:模型權重保存為F16格式。適用於支持FP16的設備,尤其是BF16不可用的情況。Qwen2.5-7B-Instruct-1M-bf16-q8_0.gguf
:輸出層和嵌入層保持為BF16格式,其他層量化為Q8_0。適用於支持BF16且需要量化版本的設備。Qwen2.5-7B-Instruct-1M-f16-q8_0.gguf
:輸出層和嵌入層保持為F16格式,其他層量化為Q8_0。Qwen2.5-7B-Instruct-1M-q4_k.gguf
:輸出層和嵌入層量化為Q8_0,其他層量化為Q4_K。適用於內存有限的CPU推理。Qwen2.5-7B-Instruct-1M-q4_k_s.gguf
:最小的Q4_K變體,以犧牲準確性為代價減少內存使用。適用於極低內存設置。Qwen2.5-7B-Instruct-1M-q6_k.gguf
:輸出層和嵌入層量化為Q8_0,其他層量化為Q6_K。Qwen2.5-7B-Instruct-1M-q8_0.gguf
:完全Q8量化的模型,準確性更高。需要更多內存,但提供更高的精度。Qwen2.5-7B-Instruct-1M-iq3_xs.gguf
:IQ3_XS量化,針對極致內存效率進行優化。適用於超低內存設備。Qwen2.5-7B-Instruct-1M-iq3_m.gguf
:IQ3_M量化,提供中等塊大小以提高準確性。適用於低內存設備。Qwen2.5-7B-Instruct-1M-q4_0.gguf
:純Q4_0量化,針對ARM設備進行優化。適用於低內存環境。建議使用IQ4_NL以獲得更好的準確性。
測試模型
如果您發現這些模型有用,請點擊“點贊”!同時,幫助我們測試具有量子就緒安全檢查功能的AI網絡監控助手: 👉 免費網絡監控器
💬 測試方法:
- 點擊任何頁面右下角的聊天圖標。
- 選擇一個AI助手類型:
TurboLLM
(GPT-4-mini)FreeLLM
(開源)TestLLM
(僅支持CPU的實驗性模型)
測試內容
我們正在挑戰小型開源模型在AI網絡監控中的極限,具體包括:
- 針對即時網絡服務的函數調用
- 模型可以多小,同時仍能處理:
- 自動化Nmap掃描
- 量子就緒檢查
- Metasploit集成
🟡 TestLLM – 當前的實驗性模型(在6個CPU線程上運行llama.cpp):
- ✅ 零配置設置
- ⏳ 30秒加載時間(推理速度慢,但無API成本)
- 🔧 尋求幫助! 如果您對邊緣設備AI感興趣,讓我們一起合作!
其他助手
🟢 TurboLLM – 使用gpt-4-mini進行:
- 即時網絡診斷
- 自動化滲透測試 (Nmap/Metasploit)
- 🔑 通過下載我們的免費網絡監控代理獲取更多令牌
🔵 HugLLM – 開源模型(約80億參數):
- 比TurboLLM多2倍的令牌
- AI日誌分析
- 🌐 在Hugging Face推理API上運行
💡 測試用的AI命令示例:
"Give me info on my websites SSL certificate"
"Check if my server is using quantum safe encyption for communication"
"Run a quick Nmap vulnerability test"
📚 詳細文檔
模型介紹
Qwen2.5-1M是Qwen2.5系列模型的長上下文版本,支持長達100萬令牌的上下文長度。與Qwen2.5 128K版本相比,Qwen2.5-1M在處理長上下文任務時表現出顯著提升,同時在短任務中也保持了良好的性能。
模型具有以下特點:
- 類型:因果語言模型
- 訓練階段:預訓練和後訓練
- 架構:帶有RoPE、SwiGLU、RMSNorm和注意力QKV偏置的transformers
- 參數數量:76.1億
- 非嵌入參數數量:65.3億
- 層數:28
- 注意力頭數量(GQA):Q為28,KV為4
- 上下文長度:完整1,010,000令牌,生成8192令牌
建議使用我們的自定義vLLM進行部署,該框架引入了稀疏注意力和長度外推方法,以確保長上下文任務的效率和準確性。具體指導請參考此部分。您也可以使用支持Qwen2.5的先前框架進行推理,但對於超過262,144令牌的序列,可能會出現準確性下降的情況。
更多詳細信息,請參考我們的博客、GitHub、技術報告和文檔。
🔧 技術細節
評估與性能
📄 許可證
本項目採用Apache-2.0許可證,詳情請見許可證鏈接。
引用
如果您覺得我們的工作有幫助,請引用以下內容:
@misc{qwen2.5-1m,
title = {Qwen2.5-1M: Deploy Your Own Qwen with Context Length up to 1M Tokens},
url = {https://qwenlm.github.io/blog/qwen2.5-1m/},
author = {Qwen Team},
month = {January},
year = {2025}
}
@article{qwen2.5,
title={Qwen2.5-1M Technical Report},
author={An Yang and Bowen Yu and Chengyuan Li and Dayiheng Liu and Fei Huang and Haoyan Huang and Jiandong Jiang and Jianhong Tu and Jianwei Zhang and Jingren Zhou and Junyang Lin and Kai Dang and Kexin Yang and Le Yu and Mei Li and Minmin Sun and Qin Zhu and Rui Men and Tao He and Weijia Xu and Wenbiao Yin and Wenyuan Yu and Xiafei Qiu and Xingzhang Ren and Xinlong Yang and Yong Li and Zhiying Xu and Zipeng Zhang},
journal={arXiv preprint arXiv:2501.15383},
year={2025}
}



