Hunyuan-A13B-Instruct / README_CN.md
manaestras's picture
Create README_CN.md
982d481 verified
<p align="center">
<img src="https://dscache.tencent-cloud.cn/upload/uploader/hunyuan-64b418fd052c033b228e04bc77bbc4b54fd7f5bc.png" width="400"/> <br>
</p><p></p>
<p align="center">
🫣&nbsp;<a href="https://huggingface.co/tencent/Hunyuan-A13B-Instruct"><b>Hugging Face</b></a>&nbsp;&nbsp;|&nbsp;&nbsp;
🖥️&nbsp;<a href="https://llm.hunyuan.tencent.com/" style="color: red;"><b>Official Website</b></a>&nbsp;&nbsp;|&nbsp;&nbsp;
🕖&nbsp;<a href="https://cloud.tencent.com/product/hunyuan"><b>HunyuanAPI</b></a>&nbsp;&nbsp;|&nbsp;&nbsp;
🕹️&nbsp;<a href="https://hunyuan.tencent.com/?model=hunyuan-a13b"><b>Demo</b></a>&nbsp;&nbsp;|&nbsp;&nbsp;
<img src="https://avatars.githubusercontent.com/u/109945100?s=200&v=4" width="16"/>&nbsp;<a href="https://modelscope.cn/models/Tencent-Hunyuan/Hunyuan-A13B-Instruct"><b>ModelScope</b></a>
</p>
<p align="center">
<a href="https://github.com/Tencent/Hunyuan-A13B"><b>GITHUB</b></a>
</p>
## 模型介绍
随着人工智能技术的快速发展,大型语言模型(LLMs)在自然语言处理、计算机视觉和科学任务等领域取得了显著进展。然而,随着模型规模的扩大,如何在保持高性能的同时优化资源消耗成为一个关键挑战。为了应对这一挑战,我们研究了混合专家(MoE)模型,当前亮相的 Hunyuan-A13B 模型,拥有800亿总参数和130亿激活参数。不仅在效果上达到了高标准,而且在尺寸上也做到了极致的优化,成功平衡了模型性能与资源占用。
### 核心特性与优势
-**小参数量,高性能**​:仅激活130亿参数(总参数量800亿),即可在多样化基准任务中媲美更大规模模型的竞争力表现
-**混合推理支持**​:同时支持快思考和慢思考两种模式,支持用户灵活选择
-**超长上下文理解**​:原生支持256K上下文窗口,在长文本任务中保持稳定性能
-**增强Agent能力**​:优化Agent能力,在BFCL-v3、τ-Bench等智能体基准测试中领先
-**高效推理**​:采用分组查询注意力(GQA)策略,支持多量化格式,实现高效推理
### 为何选择Hunyuan-A13B?
作为兼具强大性能与计算效率的大模型,Hunyuan-A13B是研究者与开发者在资源受限条件下追求高性能的理想选择。无论学术研究、高性价比AI解决方案开发,还是创新应用探索,本模型都能提供强大的基础支持。
&nbsp;
## 新闻
<br>
* 2025.6.26 我们在Hugging Face开源了 **Hunyuan-A13B-Instruct****Hunyuan-A13B-Pretrain**, **Hunyuan-A13B-Instruct-FP8****Hunyuan-A13B-Instruct-GPTQ-Int4**。并发布了技术报告和训练推理操作手册,详细介绍了模型能力和训练与推理的操作。
## 模型结构
Hunyuan-A13B采用了细粒度混合专家(Fine-grained Mixture of Experts,Fine-grained MoE)架构,包含800亿参数和130亿激活参数,累计训练了超过 20T tokens。该模型支持 256K 的上下文长度,以下为模型结构细节:
* 总参数: 80B
* 激活参数: 13B
* 层数: 32
* Attention Heads: 32
* 共享专家数: 1
* 非共享专家数: 64
* 路由策略: Top-8
* 激活函数: SwiGLU
* 隐层维度: 4096
* 专家隐层维度: 3072
## Benchmark评估榜单
**Hunyuan-A13B-Pretrain** 在 12/14 个任务上超越了Hunyuan上一代52B激活参数的MoE模型Hunyuan-Large,证实了它在预训练任务上出色的能力。与业界更大参数量的Dense和MoE模型相比, Hunyuan-A13B在多个代码和数学任务上都取得了最高分数。在MMLU, MMLU-PRO等诸多众聚合任务上, Hunyuan-A13B达到了与Qwen3-A22B模型同等的水平,表现出优秀的综合能力。
| Model | Hunyuan-Large | Qwen2.5-72B | Qwen3-A22B | Hunyuan-A13B |
|------------------|---------------|--------------|-------------|---------------|
| MMLU | 88.40 | 86.10 | 87.81 | 88.17 |
| MMLU-Pro | 60.20 | 58.10 | 68.18 | 67.23 |
| MMLU-Redux | 87.47 | 83.90 | 87.40 | 87.67 |
| BBH | 86.30 | 85.80 | 88.87 | 87.56 |
| SuperGPQA | 38.90 | 36.20 | 44.06 | 41.32 |
| EvalPlus | 75.69 | 65.93 | 77.60 | 78.64 |
| MultiPL-E | 59.13 | 60.50 | 65.94 | 69.33 |
| MBPP | 72.60 | 76.00 | 81.40 | 83.86 |
| CRUX-I | 57.00 | 57.63 | - | 70.13 |
| CRUX-O | 60.63 | 66.20 | 79.00 | 77.00 |
| MATH | 69.80 | 62.12 | 71.84 | 72.35 |
| CMATH | 91.30 | 84.80 | - | 91.17 |
| GSM8k | 92.80 | 91.50 | 94.39 | 91.83 |
| GPQA | 25.18 | 45.90 | 47.47 | 49.12 |
**Hunyuan-A13B-Instruct** 在多项基准测试中取得了极具有竞争力的表现,尤其是在数学、科学、agent等领域。我们与一些强力模型进行了对比,结果如下所示。
| Topic | Bench | OpenAI-o1-1217 | DeepSeek R1 | Qwen3-A22B | Hunyuan-A13B-Instruct |
|:-------------------:|:-----------------------------:|:-------------:|:------------:|:-----------:|:---------------------:|
| **Mathematics** | AIME 2024<br>AIME 2025<br>MATH | 74.3<br>79.2<br>96.4 | 79.8<br>70<br>94.9 | 85.7<br>81.5<br>94.0 | 87.3<br>76.8<br>94.3 |
| **Science** | GPQA-Diamond<br>OlympiadBench | 78<br>83.1 | 71.5<br>82.4 | 71.1<br>85.7 | 71.2<br>82.7 |
| **Coding** | Livecodebench<br>Fullstackbench<br>ArtifactsBench | 63.9<br>64.6<br>38.6 | 65.9<br>71.6<br>44.6 | 70.7<br>65.6<br>44.6 | 63.9<br>67.8<br>43 |
| **Reasoning** | BBH<br>DROP<br>ZebraLogic | 80.4<br>90.2<br>81 | 83.7<br>92.2<br>78.7 | 88.9<br>90.3<br>80.3 | 89.1<br>91.1<br>84.7 |
| **Instruction<br>Following** | IF-Eval<br>SysBench | 91.8<br>82.5 | 88.3<br>77.7 | 83.4<br>74.2 | 84.7<br>76.1 |
| **Text<br>Creation**| LengthCtrl<br>InsCtrl | 60.1<br>74.8 | 55.9<br>69 | 53.3<br>73.7 | 55.4<br>71.9 |
| **NLU** | ComplexNLU<br>Word-Task | 64.7<br>67.1 | 64.5<br>76.3 | 59.8<br>56.4 | 61.2<br>62.9 |
| **Agent** | BDCL v3<br> τ-Bench<br>ComplexFuncBench<br> $C^3$-Bench | 67.8<br>60.4<br>47.6<br>58.8 | 56.9<br>43.8<br>41.1<br>55.3 | 70.8<br>44.6<br>40.6<br>51.7 | 78.3<br>54.7<br>61.2<br>63.5 |
## 推理和部署
HunyuanLLM可以采用vLLM,sglang或TensorRT-LLM部署。为了简化部署过程HunyuanLLM提供了预构建docker镜像。
## 使用TensorRT-LLM推理
### BF16部署
#### Step1:执行推理
#### 方式1:命令行推理
下面我们展示一个代码片段,采用`TensorRT-LLM`快速请求chat model:
修改 examples/pytorch/quickstart_advanced.py 中如下代码:
```python
from tensorrt_llm import SamplingParams
from tensorrt_llm._torch import LLM
from tensorrt_llm._torch.pyexecutor.config import PyTorchConfig
from tensorrt_llm.llmapi import (EagleDecodingConfig, KvCacheConfig,
MTPDecodingConfig)
prompt = "Write a short summary of the benefits of regular exercise"
def main():
args = parse_arguments()
llm, sampling_params = setup_llm(args)
new_prompts = []
if args.apply_chat_template:
messages = [{"role": "user", "content": f"{prompt}"}]
new_prompts.append(llm.tokenizer.apply_chat_template(
messages, tokenize=False, add_generation_prompt=True)
)
outputs = llm.generate(new_prompts, sampling_params)
for i, output in enumerate(outputs):
prompt = output.prompt
generated_text = output.outputs[0].text
print(f"[{i}] Prompt: {prompt!r}, Generated text: {generated_text!r}")
```
运行方式:
```shell
python3 quickstart_advanced.py --model_dir "HunyuanLLM模型路径" --tp_size 4 --apply_chat_template
```
#### 方式2:服务化推理
下面我们展示使用`TensorRT-LLM`服务化的方式部署模型和请求。
```shell
model_path="HunyuanLLM模型路径"
trtllm-serve <model_path> [--backend pytorch --tp_size <tp> --ep_size <ep> --host <host> --port <port>]
```
服务启动成功后, 运行请求脚本:
```python
### OpenAI Chat Client
from openai import OpenAI
client = OpenAI(
base_url="http://localhost:8000/v1",
api_key="tensorrt_llm",
)
response = client.chat.completions.create(
model="default",
messages=[{
"role": "user",
"content": "Write a short summary of the benefits of regular exercise"
}],
max_tokens=4096,
)
print(response)
```
#### FP8/Int4量化模型部署:
目前 TensorRT-LLM 的 fp8 和 int4 量化模型正在支持中,敬请期待。
## 使用vLLM推理
### Docker:
为了简化部署过程,HunyuanLLM提供了预构建docker镜像:
[hunyuaninfer/hunyuan-large:hunyuan-moe-A13B-vllm](https://hub.docker.com/r/hunyuaninfer/hunyuan-large/tags) 。您只需要下载模型文件并用下面代码启动docker即可开始推理模型。
```shell
# 拉取
docker pull hunyuaninfer/hunyuan-large:hunyuan-moe-A13B-vllm
# 起镜像
docker run --name hunyuanLLM_infer -itd --privileged --user root --net=host --ipc=host --gpus=8 hunyuaninfer/hunyuan-large:hunyuan-moe-A13B-vllm
```
注: Docker容器权限管理。以上代码采用特权模式(--privileged)启动Docker容器会赋予容器较高的权限,增加数据泄露和集群安全风险。建议在非必要情况下避免使用特权模式,以降低安全威胁。对于必须使用特权模式的场景,应进行严格的安全评估,并实施相应的安全监控、加固措施。
### BF16部署
BF16可以在2张显存超过80G的GPU卡上部署,如果长文推荐TP4。按如下步骤执行:
运行命令前请先设置如下环境变量:
```shell
export MODEL_PATH=PATH_TO_MODEL
```
#### Step1:执行推理
#### 方式1:命令行推理
下面我们展示一个代码片段,采用`vLLM`快速请求chat model:
注: vLLM组件远程代码执行防护。下列代码中vLLM组件的trust-remote-code配置项若被启用,将允许加载并执行来自远程模型仓库的代码,这可能导致恶意代码的执行。除非业务需求明确要求,否则建议该配置项处于禁用状态,以降低潜在的安全威胁。
```python
import os
from typing import List, Optional
from vllm import LLM, SamplingParams
from vllm.inputs import PromptType
from transformers import AutoTokenizer
model_path=os.environ.get('MODEL_PATH')
tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
llm = LLM(model=model_path,
tokenizer=model_path,
trust_remote_code=True,
dtype='bfloat16',
tensor_parallel_size=4,
gpu_memory_utilization=0.9)
sampling_params = SamplingParams(
temperature=0.7, top_p=0.8, max_tokens=4096, top_k=20, repetition_penalty=1.05)
messages = [
{
"role": "system",
"content": "You are a helpful assistant.",
},
{"role": "user", "content": "Write a short summary of the benefits of regular exercise"},
]
tokenized_chat = tokenizer.apply_chat_template(messages, tokenize=True, add_generation_prompt=True, return_tensors="pt")
dummy_inputs: List[PromptType] = [{
"prompt_token_ids": batch
} for batch in tokenized_chat.numpy().tolist()]
outputs = llm.generate(dummy_inputs, 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}")
```
#### 方式2:服务化推理
下面我们展示使用`vLLM`服务化的方式部署模型并请求
在主节点上运行:
```shell
export VLLM_HOST_IP=${LOCAL_IP}
```
接着我们启动服务,运行 :
```shell
cd inference
sh run_server.sh
```
运行`run_server.sh`成功后, 运行请求脚本:
```shell
sh openapi.sh
```
注意修改`openapi.sh`中的`${LOCAL_IP}``${MODEL_PATH}`为服务对应值。
### 量化模型部署:
本部分介绍采用vLLM部署量化后模型的流程。
镜像:部署镜像同BF16。
#### Int8量化模型部署:
部署Int8-weight-only版本HunYuan-A13B模型只需设置`run_server_int8.sh`中的环境变量:
```SHELL
export MODEL_PATH=PATH_TO_BF16_MODEL
```
接着我们启动Int8服务。运行:
```shell
sh run_server_int8.sh
```
运行`run_server_int8.sh`成功后, 运行请求脚本:
```shell
sh openapi.sh
```
#### Int4量化模型部署:
部署Int4-weight-only版本HunYuan-A13B模型只需设置`run_server_int4.sh`中的环境变量,采用GPTQ方式:
```SHELL
export MODEL_PATH=PATH_TO_INT4_MODEL
```
接着我们启动Int4服务。运行:
```shell
sh run_server_int4.sh
```
运行`run_server_int4.sh`成功后, 运行请求脚本:
```shell
sh openapi.sh
```
#### FP8量化模型部署:
部署W8A8C8版本HunYuan-A13B模型只需设置`run_server_int8.sh`中的环境变量:
```shell
export MODEL_PATH=PATH_TO_FP8_MODEL
```
接着我们启动FP8服务。运行:
```shell
sh run_server_fp8.sh
```
运行`run_server_fp8.sh`成功后, 运行请求脚本:
```shell
sh openapi.sh
```
### 性能评估:
本部分介绍采用vLLM部署各个模型(原始模型和量化模型)的效率测试结果,包括不同Batchsize下的推理速度(tokens/s), 测试环境(腾讯云,H80(96G)GPU x 卡数):
测试命令:
```python
python3 benchmark_throughput.py --backend vllm \
--input-len 2048 \
--output-len 14336 \
--model $MODEL_PATH \
--tensor-parallel-size $TP \
--use-v2-block-manager \
--async-engine \
--trust-remote-code \
--num_prompts $BATCH_SIZE \
--max-num-seqs $BATCH_SIZE
```
| 推理框架 | 模型 | 部署卡数 | input_length | batch=1 | batch=16 | batch=32 |
|------|-----------------------------|-----------|-------------------------|---------------------|----------------------|----------------------|
| vLLM | Hunyuan-A13B-Instruct | 8 | 2048 | 190.84 | 1246.54 | 1981.99 |
| vLLM | Hunyuan-A13B-Instruct | 4 | 2048 | 158.90 | 779.10 | 1301.75 |
| vLLM | Hunyuan-A13B-Instruct | 2 | 2048 | 111.72 | 327.31 | 346.54 |
| vLLM | Hunyuan-A13B-Instruct(int8 weight only) | 2 | 2048 | 109.10 | 444.17 | 721.93 |
| vLLM | Hunyuan-A13B-Instruct(W8A8C8-FP8) | 2 | 2048 | 91.83 | 372.01 | 617.70 |
| vLLM | Hunyuan-A13B-Instruct(W8A8C8-FP8) | 1 | 2048 | 60.07 | 148.80 | 160.41 |
## 使用sglang推理
### BF16部署
#### Step1:执行推理
#### 方式1:命令行推理
下面我们展示一个代码片段,采用`sglang`快速请求chat model:
```python
import sglang as sgl
from transformers import AutoTokenizer
model_path=os.environ.get('MODEL_PATH')
tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
messages = [
{
"role": "system",
"content": "You are a helpful assistant.",
},
{"role": "user", "content": "Write a short summary of the benefits of regular exercise"},
]
prompts = []
prompts.append(tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True
))
print(prompts)
llm = sgl.Engine(
model_path=model_path,
tp_size=4,
trust_remote_code=True,
mem_fraction_static=0.7,
)
sampling_params = {"temperature": 0.7, "top_p": 0.8, "top_k": 20, "max_new_tokens": 4096}
outputs = llm.generate(prompts, sampling_params)
for prompt, output in zip(prompts, outputs):
print(f"Prompt: {prompt}\nGenerated text: {output['text']}")
```
#### 方式2:服务化推理
下面我们展示使用`sglang`服务化的方式部署模型和请求。
```shell
model_path="HunyuanLLM模型路径"
python3 -u -m sglang.launch_server \
--model-path $model_path \
--tp 4 \
--trust-remote-code \
```
服务启动成功后, 运行请求脚本:
```python
import openai
client = openai.Client(
base_url="http://localhost:30000/v1", api_key="EMPTY")
response = client.chat.completions.create(
model="default",
messages= [
{"role": "user", "content": "Write a short summary of the benefits of regular exercise"},
],
temperature=0.7,
max_tokens=4096,
extra_body={"top_p": 0.8, "top_k": 20}
)
print(response)
```
#### FP8/Int4量化模型部署:
目前 sglang 的 fp8 和 int4 量化模型正在支持中,敬请期待。
## 交互式Demo Web
hunyuan-A13B 现已开放网页demo。访问 https://hunyuan.tencent.com/?model=hunyuan-a13b 即可简单体验我们的模型。
<br>
## 引用
如果你觉得我们的工作对你有帮助,欢迎引用我们的<a href="report/Hunyuan_A13B_Technical_Report.pdf">技术报告</a>
<br>
## 联系我们
如果你想给我们的研发和产品团队留言,欢迎联系我们腾讯混元LLM团队。你可以通过邮件(hunyuan[email protected])联系我们。