llmcompressor.modeling.gpt_oss
类
-
LinearExpert–单个 MoE 专家,具有独立的门/上/下投影。
-
LinearExperts–多个 LinearExpert 模块的容器,由
函数
-
convert_model_for_quantization_gptoss–就地转换 GPT-OSS 模型
-
find_experts–定位模型 `model.model.layers[*].mlp.experts` 下的 GPT-OSS MoE 专家模块。
LinearExpert
继承自:Module
单个 MoE 专家,具有独立的门/上/下投影。
这镜像了 GPT-OSS 专家的行为:gate = clamp(gate_proj(x)) up = clamp(up_proj(x)) glu = gate * sigmoid(alpha * gate) y = down_proj((up + 1) * glu)
源代码位于 llmcompressor/modeling/gpt_oss.py
LinearExperts
LinearExperts(
hidden_size: int,
intermediate_size: int,
num_experts: int,
alpha: float = 1.702,
limit: float = 7.0,
)
继承自:Module
多个 LinearExpert 模块的容器,由 router_indices / routing_weights 驱动。
这是“独立门/上”的布局。它旨在取代原始 GPT-OSS 的 `experts` 子模块。
方法
-
copy_from_fused_weights–解交错融合的 gate_up 权重/偏置,并复制到单独的 gate/up 专家中。
-
forward–使用路由器输出来实现 MoE 计算。
源代码位于 llmcompressor/modeling/gpt_oss.py
copy_from_fused_weights
copy_from_fused_weights(
legacy_gate_up_W: Tensor,
legacy_gate_up_b: Tensor,
legacy_down_W: Tensor,
legacy_down_b: Tensor,
) -> None
解交错融合的 gate_up 权重/偏置,并复制到单独的 gate/up 专家中。
源代码位于 llmcompressor/modeling/gpt_oss.py
forward
forward(
hidden_states: Tensor,
router_indices: Optional[Tensor] = None,
routing_weights: Optional[Tensor] = None,
) -> torch.Tensor
使用路由器输出来实现 MoE 计算。
这与 GPT-OSS MoE 调用模式兼容:experts(hidden_states, router_indices, routing_weights)
源代码位于 llmcompressor/modeling/gpt_oss.py
convert_model_for_quantization_gptoss
就地转换 GPT-OSS 模型
- 查找所有融合的 MoE 专家块(具有 gate_up_proj/down_proj)。
- 用 LinearExperts 替换它们,LinearExperts 暴露普通的 nn.Linear 参数(gate_proj、up_proj、down_proj),这与 LLM Compressor 的 W4A8 量化配合良好。
源代码位于 llmcompressor/modeling/gpt_oss.py
find_experts
定位模型 `model.model.layers[*].mlp.experts` 下的 GPT-OSS MoE 专家模块。