> For the complete documentation index, see [llms.txt](https://docs.clore.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.clore.ai/guides/guides_v2-zh/yu-yan-mo-xing/glm-47-flash.md).

# GLM-4.7-Flash

在 Clore.ai 上部署 Zhipu AI 的 GLM-4.7-Flash（30B MoE）——高效语言模型，SWE-bench 表现达 59.2%

> GLM-4.7-Flash 是一款 **300亿参数的专家混合** 智谱AI推出的语言模型，每个 token 仅激活 30 亿参数。它在代码和推理任务上表现出色，在 SWE-bench 上达到 59.2%，同时 FP16 推理仅需 10-12GB 显存。采用以下许可发布： **MIT 许可证**，对于寻求前沿模型质量且单卡成本可负担的开发者来说，是理想选择。

## 一览

* **模型大小**：总计 300 亿 / 激活 30 亿参数（MoE）
* **许可证**：MIT（完全可商用）
* **上下文**：128K tokens
* **性能**：SWE-bench 59.2%，HumanEval 75.4%
* **显存**：FP16 约 10-12GB，INT8 约 6GB
* **速度**：RTX 4090 上约 45-60 tok/s

## 为什么选择 GLM-4.7-Flash？

**高效性能**：GLM-4.7-Flash 的表现远超其体量。尽管仅使用 30 亿激活参数，但在代码基准上优于许多 700 亿以上的稠密模型。MoE 架构以 70 亿模型的推理成本提供 300 亿模型的质量。

**单 GPU 友好**：不同于需要多 GPU 部署的超大模型，GLM-4.7-Flash 在单张 RTX 4090 或 A100 40GB 上也能流畅运行。这使它非常适合开发、微调以及高性价比的生产部署。

**代码专家**：凭借 59.2% 的 SWE-bench 成绩，GLM-4.7-Flash 在软件工程任务上表现出色——代码生成、调试、重构和技术文档。它能理解 20 多种编程语言，并具备深度上下文感知。

**MIT 许可证**：无使用限制。可商用部署、微调或修改，无需担心许可证问题。完整权重和训练配方均可免费获取。

## GPU 推荐

| GPU          | 显存   | 性能          | 每日成本\*  |
| ------------ | ---- | ----------- | ------- |
| **RTX 4090** | 24GB | \~50 tok/s  | \~$2.10 |
| **RTX 3090** | 24GB | \~35 tok/s  | \~$1.10 |
| A100 40GB    | 40GB | \~80 tok/s  | \~$3.50 |
| A100 80GB    | 80GB | \~90 tok/s  | \~$4.00 |
| H100         | 80GB | \~120 tok/s | \~$6.00 |

**最佳性价比**：RTX 4090 为 GLM-4.7-Flash 提供了性能与成本的最佳平衡。

\*Clore.ai 市场预估价格

## 使用 vLLM 部署

### 安装 vLLM

```bash
pip install vllm>=0.6.0
# 或最新版本
pip install git+https://github.com/vllm-project/vllm.git
```

### 单 GPU 设置

```bash
vllm serve THUDM/glm-4-flash \\
  --model THUDM/glm-4-flash \\
  --tensor-parallel-size 1 \\
  --dtype float16 \\
  --max-model-len 32768 \\
  --served-model-name glm-4.7-flash \\
  --trust-remote-code
```

### 查询服务器

```python
from openai import OpenAI

client = OpenAI(
    base_url="http://localhost:8000/v1", 
    api_key="EMPTY"
)

response = client.chat.completions.create(
    model="glm-4.7-flash",
    messages=[
        {"role": "system", "content": "你是一名 Python 专家开发者。"},
        {"role": "user", "content": "编写一个使用异步 SQLAlchemy 和 JWT 认证的 FastAPI 应用"}
    ],
    max_tokens=2048,
    temperature=0.7
)

print(response.choices[0].message.content)
```

## 使用 SGLang 部署

SGLang 通常能为 MoE 模型提供更高的吞吐量：

```bash
pip install "sglang[all]>=0.3.0"

# 启动服务器
python -m sglang.launch_server \\
  --model-path THUDM/glm-4-flash \\
  --port 30000 \\
  --host 0.0.0.0 \\
  --dtype float16 \\
  --tp-size 1 \\
  --context-length 32768
```

## 使用 Ollama 部署

本地开发的简单设置：

```bash
# 安装 Ollama
curl -fsSL https://ollama.com/install.sh | sh

# 拉取模型（将下载约 18GB）
ollama pull glm4:7b-chat

# 交互式运行
ollama run glm4:7b-chat

# API 模式
ollama serve
```

然后通过 REST API 查询：

```python
import requests

response = requests.post('http://localhost:11434/api/generate',
    json={
        'model': 'glm4:7b-chat',
        'prompt': '解释 GLM-4.7-Flash 中的 MoE 架构',
        'stream': False
    }
)

print(response.json()['response'])
```

## Docker 模板

```dockerfile
FROM nvidia/cuda:12.8.1-devel-ubuntu22.04

# 安装 Python 3.10
RUN apt-get update && apt-get install -y python3.10 python3-pip curl

# 安装 vLLM
RUN pip install vllm>=0.6.0 transformers

# 预下载模型（可选）
# RUN python3 -c "from transformers import AutoModel; AutoModel.from_pretrained('THUDM/glm-4-flash', trust_remote_code=True)"

EXPOSE 8000

CMD ["vllm", "serve", "THUDM/glm-4-flash", \\
     "--host", "0.0.0.0", \\
     "--port", "8000", \\
     "--tensor-parallel-size", "1", \\
     "--dtype", "float16", \\
     "--trust-remote-code"]
```

构建并运行：

```bash
docker build -t glm-4.7-flash .
docker run --gpus all -p 8000:8000 glm-4.7-flash
```

## 代码生成示例

GLM-4.7-Flash 在复杂代码生成方面表现出色：

```python
from openai import OpenAI

client = OpenAI(base_url="http://localhost:8000/v1", api_key="EMPTY")

response = client.chat.completions.create(
    model="glm-4.7-flash",
    messages=[
        {"role": "user", 
         "content": """创建一个 Python 类，用于限流器，要求：
- 令牌桶算法
- 支持 async/await  
- Redis 后端
- 用于函数限流的装饰器
- 完善的错误处理"""}
    ],
    max_tokens=2048,
    temperature=0.3
)

print(response.choices[0].message.content)
```

## 给 Clore.ai 用户的建议

* **内存优化**：使用 `--dtype float16` 以减少显存占用。对于 16GB GPU，请添加 `--max-model-len 16384` 以限制上下文。
* **批量处理**：增加 `--max-num-seqs` ，以便在处理多个请求时获得更高吞吐量。
* **量化**：对于 RTX 3060/4060（12GB），使用 AWQ 或 GPTQ 量化版本，显存占用约 6GB。
* **抢占**：GLM-4.7-Flash 能从中断中优雅恢复——非常适合 Clore.ai 的可抢占实例。
* **上下文长度**：默认 128K 上下文可能过于宽裕。设置 `--max-model-len 32768` 以满足大多数应用。

## 故障排查

| 问题                 | 解决方案                                                |
| ------------------ | --------------------------------------------------- |
| `OutOfMemoryError` | 减少 `--max-model-len` 或使用 `--dtype float16`          |
| 模型加载缓慢             | 预缓存： `huggingface-cli download THUDM/glm-4-flash`   |
| 导入错误               | 更新 transformers： `pip install transformers>=4.40.0` |
| 性能不佳               | 启用 Flash Attention： `pip install flash-attn`        |
| 连接被拒绝              | 检查防火墙： `ufw allow 8000`                             |

## 替代模型

如果 GLM-4.7-Flash 不符合你的需求：

* **Qwen2.5-Coder-7B**：纯代码能力更强，体积更小
* **CodeQwen1.5-7B**：中英双语编码专家
* **GLM-4-9B**：更大的兄弟模型，推理能力更强
* **DeepSeek-V3**：671B MoE，极致性能（多 GPU）

## 资源

* [Hugging Face 上的 GLM-4-Flash](https://huggingface.co/THUDM/glm-4-flash)
* [GLM-4 技术报告](https://arxiv.org/abs/2406.12793)
* [vLLM 文档](https://docs.vllm.ai/)
* [SGLang GitHub](https://github.com/sgl-project/sglang)
* [智谱 AI 平台](https://open.bigmodel.cn/)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.clore.ai/guides/guides_v2-zh/yu-yan-mo-xing/glm-47-flash.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
