用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
直接命令不会经过审查 Prompt;运行前请先检查来源。
npx skills add https://github.com/HorizonRobotics/OE-Skills --skill llmcompression-add-model命令会保持在同一行。复制前请横向滚动并检查完整内容。
想先保存到本地?可下载 SkillsMP 当前能够提供的文件。
基于 SOC 职业分类
正在显示 SKILL.md
| name | llmcompression-add-model |
| version | 2.0.3 |
| description | 为 llm_compression 框架新增 LLM/VLM 模型支持。当用户需要在 llm_compression/models/ 中接入新模型时触发。 |
llm_compression/models/<model_name>/model.py、process_utils.py、<model_name>_model.pyllm_compression/configs/<model_name>.ymlllm_compression/models/__init__.py首先检查 leap_llm/models/ 下有没有同名目录:
leap_llm 该模型的 forward() 方法为主要参考(不是 build())<site-packages>/transformers/models/<model_name>/modeling_<model_name>.py同时参考已集成模型的框架写法:
llm_compression/models/qwen2_5_vl/(无 QK-Norm)或 qwen3_vl/(有 QK-Norm + DeepStack)llm_compression/models/qwen3/必须直接阅读 transformers 源码,提取模型结构信息。详细的阅读方法、config 字段速查和对齐检查清单见 transformers_alignment_guide.md。
核心步骤:
configuration_<model>.py:获取 config 字段及默认值modeling_<model>.py:提取 Attention/MLP/DecoderLayer/Model/ForCausalLM 结构整理差异清单,包含三类:
要求:
models/*/blocks/ 下的所有 .py 文件以及 models/*/model.py 派生自 HuggingFace Transformers(Apache 2.0),必须保留 transformers 原始的完整 License 声明,并追加 Horizon 修改声明。
获取正确的 Copyright 行:查看 transformers/models/<model>/modeling_<model>.py 文件头的 Copyright 行,原样复制。不同模型的 Copyright 行不同(如 Qwen 系列含 Alibaba Group,Gemma 系列只有 HuggingFace Team)。
# Copyright <year> <original authors from transformers>. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Modifications Copyright (c) Horizon Robotics. All rights reserved.
此声明适用于 blocks/ 下所有文件(__init__.py、attention.py、mlp.py、transformer_block.py、moe.py、vision_*.py、linear_attention.py、text_*.py 等)以及 model.py。
| 检查项 | llm_compression 映射 |
|---|---|
| proj bias | 与参考源一致,若从 config 读则 getattr(config, "attention_bias", False) |
| q_norm/k_norm | 有则 horizon_plugin_pytorch.nn.RMSNorm(head_dim, eps=config.rms_norm_eps) |
| head_dim | getattr(config, "head_dim", config.hidden_size // config.num_attention_heads) |
| scaling | 成员变量 self.scaling = self.head_dim ** -0.5 |
self.cache_k_fq = QuantStub()
self.cache_v_fq = QuantStub()
self.dequant = DeQuantStub()
build() 中 leap 算子位置插入attention_mask 已是 4D,直接 torch.add,不要 unsqueeze[bsz, num_kv_heads, -1, head_dim] 再 matmulshape 流和代码模板见 reference.md
<Model>RotaryEmbedding,提取 inv_freq 公式_set_cos_sin_cache,只需确认 rope_thetamax_kvcache_len,存为普通属性(不用 register_buffer)quant_input_embeds、quant_cos、quant_sin、quant_attention_mask、dequantbuild() 中 leap 算子输入torch.gather 按 position_ids 索引;VLM mRoPE 需按 mrope_section 分别索引再拼接代码模板见 reference.md
# 取最后 token → norm → lm_head → dequant
return token_logits, new_keys, new_values
from llm_compression.models.generate_utils import (
chunk_prefill_forward, chunk_visual_forward, get_causal_mask,
get_causal_mask_chunks, get_decoder_mask, get_paded_input_ids_attn_mask,
init_kv_cache, init_prefill_kv_cache, is_finished, padding_data,
process_kv_cache,
)
prefill:判断 chunk_prefill → 左 padding → 初始化 KV cache → position_ids 转 int64 → get_causal_mask → forward
decode:init_kv_cache → 循环(argmax/sampling → embed → position_ids.long() → decoder_mask → forward → process_kv_cache)
config.max_kvcache_len / config.max_lm_input_len(update_config_from_custom_config 直接写入 model_config)config.text_config.max_kvcache_len / config.text_config.max_lm_input_len(嵌套 config)| 函数 | 用途 |
|---|---|
get_rope_index | mRoPE 3D position_ids (3, bsz, seq_len) |
gen_inputs_embeds | masked_scatter 将 image embeddings 填入 text |
scatter_deepstack_embeds | DeepStack 中间层 visual features |
update_config_from_custom_config(model_config, self.custom_config.model.text_config)model_config.vision_config 和 model_config.text_configmodel. 前缀,加 lm. 前缀;lm_head.weight → lm.lm_head.weight。注意:不是映射到 prefill.,而是映射到 lm.,因为 model.prefill = model.lm 是引用赋值miss_key 仅含 QuantStub 相关 key,unexpected_key 合理ConstFakeQuant(N) → {"output": qintN},FakeQuantMatmul(a, b, None) → {"input": [qintA, qintB]}SetDynamicQuantTemplate(op_kwargs={nn.Linear: {"block_size": "full", "dim": -1}}) 配置动态量化self.get_kvcache_names(model_name) 遍历构建代码模板见 reference.md
build_model、get_model_trace_dummy_input、get_generated_model、get_generated_model_cfg、get_model_dtype、get_kvcache_names、get_model_input_output_name、input_preprocess、output_postprocess、get_qconfig_setting
所有 import 必须放文件顶部。
必须先问用户模型权重路径。
model_list: [prefill, decode],无 vision_configmodel_list: [visual, prefill, decode],vision_config 默认 image_height/width: 448max_lm_input_len 默认 512,max_kvcache_len 默认 1024,rmsnorm_version: cuda_hpmodel_dtype: float32 是默认精度shared LM 模式:当显存不足以 deepcopy LM 时,可用 model_list: [visual, lm](VLM)或 [lm](LLM),prefill 和 decode 共享同一份权重,显存减半。BaseQModel.is_shared_lm_mode() 自动检测。
注册:llm_compression/models/__init__.py 中 from .<model_name>.<model_name>_model import XxxModel
测试:yml 中 eval_step: 5 → sh llm_compression/scripts/torch_eval.sh(使用项目 conda 环境)→ 成功标准:exit_code: 0 + Evaluation Results 表格
至少完成以下 3 类检查中的 1 类;如果真实权重不可用或模型过大,优先做第 2 类:
build_model + torch_eval.shprocess_utils.generate_funcchunk_prefill=Falsechunk_prefill=Truelinear_attention 路径(如果模型存在)full_attention 路径(如果模型存在)1 层和 2 层两组 case;若前几层没有覆盖到关键结构,可补 1 个特定层型 case当真实模型太大、权重不完整或容易爆显存时:
hidden_size、head_dim、num_attention_heads、num_key_value_heads、rope_parameters、layer_types、线性注意力关键维度num_hidden_layers、vocab_size、num_experts、num_experts_per_tok需要至少记录以下信息:
CUDA_VISIBLE_DEVICES=0,1,2chunk_prefill完成新增模型后,生成报告文件:
llm_compression/reports/<model_name>_integration_report.mdleap_llm 还是 transformers| 报错 | 排查 |
|---|---|
config has no attribute 'text_config' | LLM 直接 config.xxx |
size mismatch at dim 3 | causal_mask 多余 squeeze?kv_len ≠ max_kvcache_len? |
gather() Expected int64 | position_ids 在 process_utils 转 .long() |
| 3D vs 4D shape 不匹配 | RoPE 后未 reshape 回 4D?cur_len 取错维度? |
miss_key 含非 QuantStub key | 权重映射遗漏 |
mat1 and mat2 cannot be multiplied | hidden_size/head_dim 不匹配 |
forward() 参考结构 + build() 参考量化位置;无 leap_llm 时 transformers forward() 是唯一参考,量化位置参考已集成模型详细案例见 qwen3_case_study.md,transformers 对齐详细指南见 transformers_alignment_guide.md。