chore(models): remove unused HF PreTrainedModel class flags (#4307)

This PR deletes 12 HuggingFace `PreTrainedModel` class flags from the
trainer models that prime-rl never acts on. The flags described HF
features prime-rl doesn't use, and some misled readers:
`_can_compile_fullgraph = False` on the MoE models suggested they can't
compile with `fullgraph=True`, but `[model.compile] fullgraph` is the
only control.

# Verification

At `c9d98399b` on one H200 node, all of `tests/unit/train/models` passes
(including the GPT-OSS context-parallel test under `torchrun`), as do
the fused LM head and weight-utility tests. The reverse-text SFT
integration test (Qwen3-0.6B, tied embeddings) passes its start and
resume runs with the loss going down. Its full-offload resume and
checkpoint-conversion checks, and the last three DeepSeek V4 fused mHC
kernel cases (kernels this PR does not touch), were stopped before
finishing.

# For Agents Below

## Where prime-rl still touches HuggingFace

The trainer models subclass HF's `PreTrainedModel`, but prime-rl only
uses HF to construct the module tree. Sharding, compile, activation
checkpointing and weight loading are prime-rl's own:

```
setup_model (src/prime_rl/trainer/model.py)
  from_config(config) on meta device    HF
    PreTrainedModel.__init__            HF: attention-impl check (overridden by PreTrainedModelPrimeRL)
    post_init                           HF: merges class flags, ties lm_head to embed_tokens
  inject_prime_lm_head                  prime-rl
  setup_fsdp, apply_ac, apply_compile   prime-rl
  to_empty, load_dcp_from_hf            prime-rl: DCP reads the safetensors checkpoint directly
```

So a class flag matters only if `__init__` or `post_init` acts on it
during this meta-device build. Most HF flags serve paths prime-rl never
takes: `from_pretrained` with `device_map` or `tp_plan`, HF generation,
`set_attn_implementation`, `gradient_checkpointing_enable`. The flag
prime-rl does rely on is `_tied_weights_keys`: `post_init` uses it to
make `lm_head.weight` and `embed_tokens.weight` one parameter, and
`load_dcp_from_hf` skips `lm_head.weight` for tied models because
loading the embeddings fills both. The shared parameter survives
`inject_prime_lm_head`, FSDP sharding and `to_empty`. `from_pretrained`
runs only in unit tests, and the weight transfer to vLLM reads none of
these flags.

## Why It's Safe

When a flag is missing, `post_init` [falls back to an empty
value](https://github.com/huggingface/transformers/blob/v5.19.0/src/transformers/modeling_utils.py#L1300-L1335),
so removing a flag from only some classes is fine. Per flag, against
transformers `v5.19.0` (our pin):

- `_can_compile_fullgraph`: only the [base
default](https://github.com/huggingface/transformers/blob/v5.19.0/src/transformers/modeling_utils.py#L1141)
and a docstring mention it; nothing in transformers reads it.
- `_supports_sdpa`, `_supports_flash_attn`, `_supports_flex_attn`: read
only by HF's
[`_check_and_adjust_attn_implementation`](https://github.com/huggingface/transformers/blob/v5.19.0/src/transformers/modeling_utils.py#L1714)
and
[`set_attn_implementation`](https://github.com/huggingface/transformers/blob/v5.19.0/src/transformers/modeling_utils.py#L1964).
`PreTrainedModelPrimeRL` overrides the first without calling `super`,
and nothing calls the second.
- `_supports_attention_backend`: read only by
[`is_backend_compatible`](https://github.com/huggingface/transformers/blob/v5.19.0/src/transformers/modeling_utils.py#L4728),
which nothing calls.
- `_tp_plan`, `_pp_plan`: copied in
[`init_parallel_plans`](https://github.com/huggingface/transformers/blob/v5.19.0/src/transformers/distributed/mixin.py#L72)
but used only by HF tensor parallelism (enabled by passing a `tp_plan`),
[device-map
warmup](https://github.com/huggingface/transformers/blob/v5.19.0/src/transformers/modeling_utils.py#L4416-L4418)
and a [log-only
check](https://github.com/huggingface/transformers/blob/v5.19.0/src/transformers/modeling_utils.py#L4399-L4400).
prime-rl shards with its own FSDP/EP.
- `_no_split_modules`, `_skip_keys_device_placement`: consumed only by
[`device_map`
dispatch](https://github.com/huggingface/transformers/blob/v5.19.0/src/transformers/integrations/accelerate.py#L348)
([L407](https://github.com/huggingface/transformers/blob/v5.19.0/src/transformers/integrations/accelerate.py#L407)).
- `supports_gradient_checkpointing`: the read that runs on every model
build,
[here](https://github.com/huggingface/transformers/blob/v5.19.0/src/transformers/modeling_utils.py#L1356-L1360),
does something only if the config sets `gradient_checkpointing`.
prime-rl does activation checkpointing itself in `apply_ac`.
- `_can_record_outputs`: feeds HF's output-capturing hooks, which no
prime-rl forward uses.
[`patch_output_recorders`](https://github.com/huggingface/transformers/blob/v5.19.0/src/transformers/monkey_patching.py#L342-L344)
returns early because no patch mapping is registered.
- `_keys_to_ignore_on_load_unexpected`: only filters the unexpected-keys
warning in `from_pretrained`. DCP loading ignores checkpoint keys the
model doesn't have.

Kept:

- `_tied_weights_keys`: `post_init` [calls
`init_weights`](https://github.com/huggingface/transformers/blob/v5.19.0/src/transformers/modeling_utils.py#L1341),
which [calls
`tie_weights`](https://github.com/huggingface/transformers/blob/v5.19.0/src/transformers/modeling_utils.py#L3109)
using the mapping from
[`get_expanded_tied_weights_keys`](https://github.com/huggingface/transformers/blob/v5.19.0/src/transformers/modeling_utils.py#L2480-L2491).
That mapping is `{}` without this flag. `load_dcp_from_hf` then pops
`lm_head.weight` for tied configs, so the head would stay uninitialized.
- The tie holds through setup (torch `v2.13.0`): `inject_prime_lm_head`
reassigns the old head's weight, `setup_fsdp` keeps tied `lm_head` in
the root FSDP group, where FSDP2 [installs one sharded parameter on both
modules](https://github.com/pytorch/pytorch/blob/v2.13.0/torch/distributed/fsdp/_fully_shard/_fsdp_param.py#L761-L768),
and `to_empty` [swaps DTensor params in
place](https://github.com/pytorch/pytorch/blob/v2.13.0/torch/nn/modules/module.py#L968-L986)
rather than creating new ones.
- `config_class` and `base_model_prefix`: HF uses them to resolve
configs and the [base-model prefix in weight
names](https://github.com/huggingface/transformers/blob/v5.19.0/src/transformers/modeling_utils.py#L1473).

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Garrett Goon
2026-10-28 16:39:06 -04:00
committed by GitHub
co-authored by Claude Opus 5.5
parent 2495c3392b
commit a1647464bd
13 changed files with 0 additions and 150 deletions
@@ -279,8 +279,6 @@ class AfmoeDecoderLayer(GradientCheckpointingLayer):
class AfmoePreTrainedModel(PreTrainedModelPrimeRL):
config_class = AfmoeConfig
base_model_prefix = "model"
_no_split_modules = ["AfmoeDecoderLayer"]
_skip_keys_device_placement = ["past_key_values"]
_keep_in_fp32_modules = [
"input_layernorm",
"post_attention_layernorm",
@@ -290,15 +288,6 @@ class AfmoePreTrainedModel(PreTrainedModelPrimeRL):
"k_norm",
"norm",
]
_supports_sdpa = False
_supports_flash_attn = True
_supports_flex_attn = False
_supports_attention_backend = True
_can_compile_fullgraph = False
_can_record_outputs = {
"hidden_states": AfmoeDecoderLayer,
}
supports_gradient_checkpointing = True
@classmethod
def is_hf_state_dict(cls, state_dict: dict[str, torch.Tensor]) -> bool:
@@ -314,8 +303,6 @@ class AfmoePreTrainedModel(PreTrainedModelPrimeRL):
class AfmoeModel(AfmoePreTrainedModel):
_no_split_modules = ["AfmoeDecoderLayer"]
def __init__(self, config: AfmoeConfig):
super().__init__(config)
self.padding_idx = config.pad_token_id
@@ -398,8 +385,6 @@ class AfmoeModel(AfmoePreTrainedModel):
class AfmoeForCausalLM(AfmoePreTrainedModel, GenerationMixin):
_tied_weights_keys = {"lm_head.weight": "model.embed_tokens.weight"}
_tp_plan = {"lm_head": "colwise_rep"}
_pp_plan = {"lm_head": (["hidden_states"], ["logits"])}
def __init__(self, config):
super().__init__(config)
@@ -95,17 +95,6 @@ class DeepseekV4PreTrainedModel(PreTrainedModelPrimeRL):
config: DeepseekV4Config
config_class = DeepseekV4Config
base_model_prefix = "model"
supports_gradient_checkpointing = True
_no_split_modules = ["DeepseekV4DecoderLayer"]
_skip_keys_device_placement = ["past_key_values"]
# V4 attention runs its own fused kernel; no transformers backend can serve it.
_supports_flash_attn = False
_supports_sdpa = False
_supports_flex_attn = False
_can_compile_fullgraph = False
_supports_attention_backend = True
_can_record_outputs = {"hidden_states": DeepseekV4DecoderLayer}
_keys_to_ignore_on_load_unexpected = [r"(^|\.)mtp\..*"]
@classmethod
def cp_blocker(cls, config, cp: int) -> str | None:
@@ -272,8 +261,6 @@ class DeepseekV4Model(DeepseekV4PreTrainedModel):
class DeepseekV4ForCausalLM(DeepseekV4PreTrainedModel, GenerationMixin):
_tied_weights_keys = {"lm_head.weight": "model.embed_tokens.weight"}
_tp_plan = {"lm_head": "colwise_gather_output"}
_pp_plan = {"lm_head": (["hidden_states"], ["logits"])}
def __init__(self, config: DeepseekV4Config):
super().__init__(config)
@@ -119,17 +119,6 @@ class Glm4MoeDecoderLayer(GradientCheckpointingLayer):
class Glm4MoePreTrainedModel(PreTrainedModelPrimeRL):
config: Glm4MoeConfig
base_model_prefix = "model"
supports_gradient_checkpointing = True
_no_split_modules = ["Glm4MoeDecoderLayer"]
_skip_keys_device_placement = ["past_key_values"]
_supports_flash_attn = True
_supports_sdpa = False
_supports_flex_attn = True
_can_compile_fullgraph = False
_supports_attention_backend = True
_can_record_outputs = {
"hidden_states": Glm4MoeDecoderLayer,
}
@classmethod
def keep_in_fp32_for_weight_transfer(cls, name: str) -> bool:
@@ -156,8 +145,6 @@ class Glm4MoePreTrainedModel(PreTrainedModelPrimeRL):
@auto_docstring
class Glm4MoeModel(Glm4MoePreTrainedModel):
_keys_to_ignore_on_load_unexpected = [r"model\.layers\.92.*", r"model\.layers\.46.*"]
def __init__(self, config: Glm4MoeConfig):
super().__init__(config)
self.padding_idx = config.pad_token_id
@@ -239,8 +226,6 @@ class Glm4MoeModel(Glm4MoePreTrainedModel):
@auto_docstring
class Glm4MoeForCausalLM(Glm4MoePreTrainedModel, GenerationMixin):
_tied_weights_keys = {"lm_head.weight": "model.embed_tokens.weight"}
_tp_plan = {"lm_head": "colwise_rep"}
_pp_plan = {"lm_head": (["hidden_states"], ["logits"])}
def __init__(self, config):
super().__init__(config)
@@ -144,9 +144,6 @@ class Glm5NextDecoderLayer(GradientCheckpointingLayer):
class Glm5NextPreTrainedModel(PreTrainedModelPrimeRL):
config_class = Glm5NextConfig
base_model_prefix = "model"
supports_gradient_checkpointing = True
_no_split_modules = ["Glm5NextDecoderLayer"]
_can_compile_fullgraph = False
@classmethod
def cp_blocker(cls, config, cp: int) -> str | None:
@@ -273,9 +270,6 @@ class Glm5NextModel(nn.Module):
class Glm5NextForCausalLM(Glm5NextPreTrainedModel):
_tp_plan = {"lm_head": "colwise_gather_output"}
_pp_plan = {"lm_head": (["hidden_states"], ["logits"])}
def __init__(self, config: Glm5NextConfig | Glm5NextTextConfig) -> None:
super().__init__(config)
text_config = getattr(config, "text_config", config)
@@ -118,17 +118,6 @@ class GlmMoeDsaDecoderLayer(GradientCheckpointingLayer):
class GlmMoeDsaPreTrainedModel(PreTrainedModelPrimeRL):
config: GlmMoeDsaConfig
base_model_prefix = "model"
supports_gradient_checkpointing = True
_no_split_modules = ["GlmMoeDsaDecoderLayer"]
_skip_keys_device_placement = ["past_key_values"]
_supports_flash_attn = True
_supports_sdpa = False
_supports_flex_attn = True
_can_compile_fullgraph = False
_supports_attention_backend = True
_can_record_outputs = {
"hidden_states": GlmMoeDsaDecoderLayer,
}
@classmethod
def cp_blocker(cls, config, cp: int) -> str | None:
@@ -266,8 +255,6 @@ class GlmMoeDsaModel(GlmMoeDsaPreTrainedModel):
@auto_docstring
class GlmMoeDsaForCausalLM(GlmMoeDsaPreTrainedModel, GenerationMixin):
_tied_weights_keys = {"lm_head.weight": "model.embed_tokens.weight"}
_tp_plan = {"lm_head": "colwise_rep"}
_pp_plan = {"lm_head": (["hidden_states"], ["logits"])}
def __init__(self, config):
super().__init__(config)
@@ -85,12 +85,6 @@ class GptOssDecoderLayer(nn.Module):
class GptOssPreTrainedModel(PreTrainedModelPrimeRL):
config: GptOssConfig
base_model_prefix = "model"
supports_gradient_checkpointing = True
_no_split_modules = ["GptOssDecoderLayer"]
_supports_flash_attn = True
_supports_sdpa = False
_can_compile_fullgraph = False
_supports_attention_backend = True
_keep_in_fp32_modules = ["post_attention_layernorm", "input_layernorm", "norm"]
@classmethod
@@ -161,8 +155,6 @@ class GptOssModel(GptOssPreTrainedModel):
class GptOssForCausalLM(GptOssPreTrainedModel):
_tied_weights_keys = {"lm_head.weight": "model.embed_tokens.weight"}
_tp_plan = {"lm_head": "colwise_gather_output"}
_pp_plan = {"lm_head": (["hidden_states"], ["logits"])}
def __init__(self, config: GptOssConfig) -> None:
super().__init__(config)
@@ -256,17 +256,6 @@ class LagunaPreTrainedModel(PreTrainedModelPrimeRL):
config: LagunaConfig
config_class = LagunaConfig
base_model_prefix = "model"
supports_gradient_checkpointing = True
_no_split_modules = ["LagunaDecoderLayer"]
_skip_keys_device_placement = ["past_key_values"]
_supports_flash_attn = True
_supports_sdpa = False
_supports_flex_attn = False
_can_compile_fullgraph = False
_supports_attention_backend = True
_can_record_outputs = {
"hidden_states": LagunaDecoderLayer,
}
@classmethod
def is_hf_state_dict(cls, state_dict: dict[str, Tensor]) -> bool:
@@ -347,8 +336,6 @@ class LagunaModel(LagunaPreTrainedModel):
class LagunaForCausalLM(LagunaPreTrainedModel, GenerationMixin):
_tied_weights_keys = {"lm_head.weight": "model.embed_tokens.weight"}
_tp_plan = {"lm_head": "colwise_gather_output"}
_pp_plan = {"lm_head": (["hidden_states"], ["logits"])}
def __init__(self, config: LagunaConfig):
super().__init__(config)
@@ -100,18 +100,6 @@ class LlamaDecoderLayer(GradientCheckpointingLayer):
class LlamaPreTrainedModel(PreTrainedModelPrimeRL):
config: LlamaConfig
base_model_prefix = "model"
supports_gradient_checkpointing = True
_no_split_modules = ["LlamaDecoderLayer"]
_skip_keys_device_placement = ["past_key_values"]
_supports_flash_attn = True
_supports_sdpa = False
_supports_flex_attn = True
_can_compile_fullgraph = True
_supports_attention_backend = True
_can_record_outputs = {
"hidden_states": LlamaDecoderLayer,
}
# Llama models don't need format conversion - they use standard HF format for both training and inference
@classmethod
@@ -222,8 +210,6 @@ class LlamaModel(LlamaPreTrainedModel):
@auto_docstring
class LlamaForCausalLM(LlamaPreTrainedModel, GenerationMixin):
_tied_weights_keys = {"lm_head.weight": "model.embed_tokens.weight"}
_tp_plan = {"lm_head": "colwise_rep"}
_pp_plan = {"lm_head": (["hidden_states"], ["logits"])}
def __init__(self, config):
super().__init__(config)
@@ -90,17 +90,6 @@ class MiniMaxM2DecoderLayer(GradientCheckpointingLayer):
class MiniMaxM2PreTrainedModel(PreTrainedModelPrimeRL):
config: MiniMaxM2Config
base_model_prefix = "model"
supports_gradient_checkpointing = True
_no_split_modules = ["MiniMaxM2DecoderLayer"]
_skip_keys_device_placement = ["past_key_values"]
_supports_flash_attn = True
_supports_sdpa = False
_supports_flex_attn = True
_can_compile_fullgraph = False
_supports_attention_backend = True
_can_record_outputs = {
"hidden_states": MiniMaxM2DecoderLayer,
}
@classmethod
def is_hf_state_dict(cls, state_dict: dict[str, Tensor]) -> bool:
@@ -195,8 +184,6 @@ class MiniMaxM2Model(MiniMaxM2PreTrainedModel):
@auto_docstring
class MiniMaxM2ForCausalLM(MiniMaxM2PreTrainedModel, GenerationMixin):
_tied_weights_keys = {"lm_head.weight": "model.embed_tokens.weight"}
_tp_plan = {"lm_head": "colwise_rep"}
_pp_plan = {"lm_head": (["hidden_states"], ["logits"])}
def __init__(self, config):
super().__init__(config)
@@ -137,11 +137,6 @@ class MuseGlimmerDecoderLayer(GradientCheckpointingLayer):
class MuseGlimmerPreTrainedModel(PreTrainedModelPrimeRL):
config_class = MuseGlimmerConfig
base_model_prefix = "model"
supports_gradient_checkpointing = True
_no_split_modules = ["MuseGlimmerDecoderLayer"]
_supports_flash_attn = True
_supports_sdpa = False
_can_compile_fullgraph = True
@classmethod
def is_hf_state_dict(cls, state_dict: dict[str, Tensor]) -> bool:
@@ -237,9 +232,6 @@ class MuseGlimmerForCausalLM(MuseGlimmerPreTrainedModel):
"""Text-only MuseGlimmer. Built from the composite checkpoint config, its weights keep the
checkpoint's `model.language_model.*` names; the vision tower is not loaded."""
_tp_plan = {"lm_head": "colwise_gather_output"}
_pp_plan = {"lm_head": (["hidden_states"], ["logits"])}
def __init__(self, config: MuseGlimmerConfig | MuseGlimmerTextConfig) -> None:
super().__init__(config)
text_config = getattr(config, "text_config", config)
@@ -87,17 +87,6 @@ class Qwen3DecoderLayer(GradientCheckpointingLayer):
class Qwen3PreTrainedModel(PreTrainedModelPrimeRL):
config: Qwen3Config
base_model_prefix = "model"
supports_gradient_checkpointing = True
_no_split_modules = ["Qwen3DecoderLayer"]
_skip_keys_device_placement = ["past_key_values"]
_supports_flash_attn = True
_supports_sdpa = False
_supports_flex_attn = True
_can_compile_fullgraph = True
_supports_attention_backend = True
_can_record_outputs = {
"hidden_states": Qwen3DecoderLayer,
}
@classmethod
def is_hf_state_dict(cls, state_dict: dict[str, Tensor]) -> bool:
@@ -197,8 +186,6 @@ class Qwen3Model(Qwen3PreTrainedModel):
@auto_docstring
class Qwen3ForCausalLM(Qwen3PreTrainedModel, GenerationMixin):
_tied_weights_keys = {"lm_head.weight": "model.embed_tokens.weight"}
_tp_plan = {"lm_head": "colwise_gather_output"}
_pp_plan = {"lm_head": (["hidden_states"], ["logits"])}
def __init__(self, config):
super().__init__(config)
@@ -115,9 +115,6 @@ class Qwen3_8FlashNextDecoderLayer(nn.Module):
class Qwen3_8FlashNextPreTrainedModel(PreTrainedModelPrimeRL):
config_class = Qwen3_8FlashNextConfig
base_model_prefix = "model"
supports_gradient_checkpointing = True
_no_split_modules = ["Qwen3_8FlashNextDecoderLayer"]
_can_compile_fullgraph = False
@classmethod
def is_hf_state_dict(cls, state_dict: dict[str, Tensor]) -> bool:
@@ -328,9 +325,6 @@ class Qwen3_8FlashNextVLMModel(nn.Module):
class Qwen3_8FlashNextForCausalLM(Qwen3_8FlashNextPreTrainedModel):
_tp_plan = {"lm_head": "colwise_gather_output"}
_pp_plan = {"lm_head": (["hidden_states"], ["logits"])}
def __init__(self, config: Qwen3_8FlashNextConfig) -> None:
super().__init__(config)
self.is_vlm = hasattr(config, "vision_config")
@@ -119,17 +119,6 @@ class Qwen3MoeDecoderLayer(GradientCheckpointingLayer):
class Qwen3MoePreTrainedModel(PreTrainedModelPrimeRL):
config: Qwen3MoeConfig
base_model_prefix = "model"
supports_gradient_checkpointing = True
_no_split_modules = ["Qwen3MoeDecoderLayer"]
_skip_keys_device_placement = ["past_key_values"]
_supports_flash_attn = True
_supports_sdpa = False
_supports_flex_attn = True
_can_compile_fullgraph = False # MoE models don't work with torch.compile (`torch.where(condition)` not supported)
_supports_attention_backend = True
_can_record_outputs = {
"hidden_states": Qwen3MoeDecoderLayer,
}
@classmethod
def is_hf_state_dict(cls, state_dict: dict[str, Tensor]) -> bool:
@@ -229,8 +218,6 @@ class Qwen3MoeModel(Qwen3MoePreTrainedModel):
@auto_docstring
class Qwen3MoeForCausalLM(Qwen3MoePreTrainedModel, GenerationMixin):
_tied_weights_keys = {"lm_head.weight": "model.embed_tokens.weight"}
_tp_plan = {"lm_head": "colwise_rep"}
_pp_plan = {"lm_head": (["hidden_states"], ["logits"])}
def __init__(self, config):
super().__init__(config)