msModelSlim 基础模型适配 Skill
本 Skill 指导如何为新模型创建基础适配器,使其跑通 W8A8/W4A16 量化流程。
适用范围
- 支持:Decoder-only LLM、理解类 VLM(只处理文本/LLM 主干)
- 不支持:多模态生成(如 Stable Diffusion/Flux/Wan)、Encoder-only、非 Transformers
核心工作流
1. 准备工作
- 下载模型:建议使用
modelscope download下载非权重文件。- 示例:
modelscope download --model <org>/<model> --local_dir ./models/<name> --exclude '*.safetensors'
- 示例:
- 分析模型:阅读
config.json与modeling_*.py,确认结构与实现。- 详见:模型结构分析指南
2. 创建适配器
- 使用模板:
- LLM:
assets/model_adapter_template.py - VLM:
assets/vlm_model_adapter_template.py
- LLM:
- 实现接口:实现
handle_dataset,init_model,generate_model_visit,generate_model_forward,enable_kv_cache。 - 关键原则:
visit与forward必须严格一致。- MoE 模型建议 unpack 为纯线性层。
- 详见:适配器实现指南
3. 注册与安装
- 在
config/config.ini注册模型与入口,并执行bash install.sh。 - 详见:适配器注册指南
4. 验证适配器 (必需)
- 必须执行四步验证:生成测试模型 -> 全回退量化 -> 校验全回退模型与浮点权重严格一致且可完整加载/保存 -> 验证实际量化流程正常(含描述文件规则校验)。
- 详见:适配器验证指南
常用脚本
脚本位于 scripts/ 目录下:
scripts/step1_generate_test_model.pyscripts/step2_run_quantization.pyscripts/step3_verify_weights.pyscripts/step4_verify_quant_description.py
参考资料
- 模型结构分析指南
- 适配器实现指南
- 适配器注册指南
- 适配器验证指南
- 接口检查清单
- 核心工作流