基于对大模型推理原理的兴趣,本项目跟随 tiny-llm 课程,将苹果芯片相关实现适配到 Nvidia GPU(RTX 4070),完成 Qwen3-4B-Instruct-2507 的本地部署与推理。显存不足时可选用 Qwen3-0.5B,重点在于学习与复现技术路线。
- Nvidia GPU:需安装 CUDA 及对应版本 PyTorch
- 验证与依赖:
torchao、torchtune(用于测试与对齐) - FlashAttention:需安装
triton - 模型加载:
transformers(国内可设export HF_ENDPOINT=https://hf-mirror.com)
推荐环境:
| 依赖 | 版本 |
|---|---|
| torch | 2.6.0+cu126 |
| torchao | 0.15.0 |
| torchtune | 0.6.1 |
| transformers | 4.57.6 |
| triton | 3.2.0 |
| # | 任务 | 测试命令 |
|---|---|---|
| 1 | ✅ scaled_dot_product_attention | python -m unittest llm.test.attention_test.TestScaleDotAttention |
| 2 | ✅ MultiHeadAttention | python -m unittest llm.test.attention_test.TestMultiHeadAttention |
| 3 | ✅ RoPE 旋转位置编码 | python -m unittest llm.test.rope_test |
| 4 | ✅ RMSNorm | python -m unittest llm.test.norm_test |
| 5 | ✅ 千问 MLP | python -m unittest llm.test.mlp_test |
| 6.1 | ✅ Attention 添加 GQA | python -m unittest llm.test.attention_test.TestScaleDotAttention |
| 6.2 | ✅ MultiHeadAttention GQA | python -m unittest llm.test.attention_test.TestGroupedMultiHeadAttention |
| 7 | ✅ Tied Embedding | python -m unittest llm.test.tie_embedding_test.TestTieEmbedding |
| 8 | ✅ Qwen3 TransformerBlock | — |
| 9 | ✅ 加载 Qwen3 并简单推理 | python -m llm.executor.run_model |
| # | 优化 | 测试命令 |
|---|---|---|
| 1.1 | ✅ TopK 采样 | python -m llm.executor.run_model --topk 100 |
| 1.2 | ✅ TopP 采样 | python -m llm.executor.run_model --topp 0.7 |
| 2 | ✅ Prefill-Decode 分离与 KV Cache | python -m llm.executor.run_model --kv_cache 1 |
| 3 | ✅ FlashAttention V1 (Triton) | python -m unittest llm.test.flash_attn_test · python -m llm.executor.run_model --use_flash 1 |
| 4 | ✅ 连续批处理与 Chunk Prefill | python -m llm.executor.continue_batch |
| 5 | ✅ PageAttention | python -m llm.executor.continue_batch --use_page 1 |
大模型对话还会涉及许多 优化与工程方向,更偏大模型服务与 Agent,例如:
- Speculative Decoding:小模型草稿 + 大模型并行验证
- Memory:长期 / 短期记忆管理
- RAG:检索增强生成
- MCP (Model Context Protocol):与外部数据源、工具的协议
- Skills:多工具编排与 Agent 行为定义
RAG 涉及向量检索与数据库,Memory 涉及摘要与历史压缩,各家实现差异较大,与 GPU 推理优化关系相对独立。本仓库主要聚焦单卡推理与 KV/Attention 优化。后续回去学多卡相关的内容。
