README.md 5.12 KB
Newer Older
chenych's avatar
chenych committed
1
2
# LongCat_sglang

chenych's avatar
chenych committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
## 论文
[LongCat-Flash Technical Report](https://github.com/meituan-longcat/LongCat-Flash-Chat/blob/main/tech_report.pdf)

## 模型结构
美团开源模型龙猫,一个强大且高效的语言模型,拥有总计 5600 亿个参数,采用了创新的专家混合(MoE)架构。该模型采用了一种动态计算机制,能够根据上下文需求激活 186 亿至 313 亿个参数(平均约 270 亿个),从而在计算效率和性能之间实现了优化。

<div align=center>
    <img src="./doc/model.png"/>
</div>

## 算法原理
LongCat-Flash 是基于两个关键原则进行设计和优化的:高效计算利用以及高效训练和推理。具体而言:

(1)由于并非所有标记都同等重要,在 MoE 块中引入了零计算专家机制,根据标记的重要性动态分配计算预算,即根据上下文需求激活 186 亿至 313 亿个参数(总参数量为 5600 亿)。为确保计算负载一致,采用由 PID 控制器调整的专家偏差,使每个标记平均激活约 270 亿个参数。

(2)由于在 MoE 模型扩展过程中通信开销成为瓶颈,引入了快捷连接的 MoE(ScMoE)设计,以扩大计算与通信的重叠窗口。结合定制的基础设施优化,该设计能够支持在数万个加速器上进行大规模训练,并实现高吞吐量和低延迟的推理。

<div align=center>
    <img src="./doc/methods.png"/>
</div>

## 环境配置
### 硬件需求
DCU型号:K100_AI,节点数量:4台,卡数:32 张。

`-v 路径``docker_name``imageID`根据实际情况修改

### Docker(方法一)
```bash
docker pull image.sourcefind.cn:5000/dcu/admin/base/vllm:0.9.2-ubuntu22.04-dtk25.04.1-rc5-rocblas101839-0811-das1.6-py3.10-20250812-beta
docker run -it --shm-size 200g --network=host --name {docker_name} --privileged --device=/dev/kfd --device=/dev/dri --device=/dev/mkfd --group-add video --cap-add=SYS_PTRACE --security-opt seccomp=unconfined -u root -v /path/your_code_data/:/path/your_code_data/ -v /opt/hyhal/:/opt/hyhal/:ro {imageID} bash

## 安装sglang
git clone https://developer.sourcefind.cn/codes/OpenDAS/sglang

cd sglang
# 编译安装sgl-kernel包
cd sgl-kernel
python setup_hip.py install
# 安装sglang Python依赖
cd ..
pip install -e "python[all_hip]"
pip install transformers==4.56.0

cd /your_code_path/longcat_sglang
```

### Dockerfile(方法二)
```bash
cd docker
docker build --no-cache -t longcat:latest .
docker run -it --shm-size 200g --network=host --name {docker_name} --privileged --device=/dev/kfd --device=/dev/dri --device=/dev/mkfd --group-add video --cap-add=SYS_PTRACE --security-opt seccomp=unconfined -u root -v /path/your_code_data/:/path/your_code_data/ -v /opt/hyhal/:/opt/hyhal/:ro {imageID} bash

## 安装sglang
git clone https://developer.sourcefind.cn/codes/OpenDAS/sglang

cd sglang
# 编译安装sgl-kernel包
cd sgl-kernel
python setup_hip.py install
# 安装sglang Python依赖
cd ..
pip install -e "python[all_hip]"
pip install transformers==4.56.0

cd /your_code_path/longcat_sglang
```

### Anaconda(方法三)
关于本项目DCU显卡所需的特殊深度学习库可从[光合](https://developer.sourcefind.cn/tool/)开发者社区下载安装。
```bash
DTK: 25.04.1
python: 3.10.12
torch: 2.5.1+das.opt1.dtk25041
deepspeed: 0.14.2+das.opt1.dtk25041
transformers: 4.56.0
```
`Tips:以上dtk驱动、python、torch等DCU相关工具版本需要严格一一对应`

``` bash
## 安装sglang
git clone https://developer.sourcefind.cn/codes/OpenDAS/sglang

cd sglang
# 编译安装sgl-kernel包
cd sgl-kernel
python setup_hip.py install
# 安装sglang Python依赖
cd ..
pip install -e "python[all_hip]"
pip install transformers==4.56.0
```

## 数据集


## 训练
暂无

## 推理
### sglang推理方法
#### server 多机
样例模型:[LongCat-Flash-Chat](https://huggingface.co/meituan-longcat/LongCat-Flash-Chat)

启动sglang server,每个节点都需要启动
> 参数说明:
> x.x.x.x 对应主节点的IP
>
> 主节点NODE_RANK为0,工作节点分别为1,2,3
>
> nnodes 节点数量, 每个节点都需要执行下面脚本,NODE_RANK根据每个节点输入

主节点启动示例:
```bash
bash start_sglang.sh 0
```

启动完成后可通过以下方式访问:
```bash
curl http://127.0.0.2:8001/v1/chat/completions \
    -H "Content-Type: application/json" \
    -d '{
        "model": "meituan-longcat/LongCat-Flash-Chat",
        "messages": [
            {
                "role": "user",
                "content": "Explain Machine Learning to me in a nutshell."
            }
        ],
        "temperature": 0.15,
        "top_p": 1.0,
        "max_tokens": 2048,
        "stream": false
}'
```

## result
<div align=center>
    <img src="./doc/results-dcu.png"/>
</div>

### 精度
DCU与GPU精度一致,推理框架:sglang。

## 应用场景
### 算法类别
对话问答

### 热点应用行业
制造,广媒,家居,教育

## 预训练权重
- [LongCat-Flash-Chat](https://huggingface.co/meituan-longcat/LongCat-Flash-Chat)

## 源码仓库及问题反馈
- https://developer.sourcefind.cn/codes/modelzoo/longcat_sglang

## 参考资料
- https://github.com/meituan-longcat/LongCat-Flash-Chat