Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
70 commits
Select commit Hold shift + click to select a range
e82164f
Add anymodel directories to feature/puzzletron
danielkorzekwa Mar 4, 2026
2099df3
Make any_model conversion working.
danielkorzekwa Mar 5, 2026
eb5cf8a
Update child_init.py with anymodel version
danielkorzekwa Mar 5, 2026
c9de41c
fix attention pruning
danielkorzekwa Mar 5, 2026
3c1bc1f
Add trust_remote_code to load_model_config (default to false)
danielkorzekwa Mar 5, 2026
8357136
Make activation scoring working
danielkorzekwa Mar 5, 2026
6cc2194
Comment all tested models aside of llama_3_1_8b_instruct
danielkorzekwa Mar 5, 2026
ee4e1e3
Delete not needed decilm test
danielkorzekwa Mar 5, 2026
449b523
Fix broken tests
danielkorzekwa Mar 5, 2026
fb27bba
Update puzzletron_nas_pluging to any_model version
danielkorzekwa Mar 5, 2026
b350f82
Correct test resources used by tests.
danielkorzekwa Mar 5, 2026
fafe5a3
Disable puzzletron tests (will be enabled after all any_model logic i…
danielkorzekwa Mar 5, 2026
e988248
Merge branch 'dkorzekwa/anymodel_core' into dkorzekwa/anymodel_activa…
danielkorzekwa Mar 6, 2026
c717852
Comment out not implemented models.
danielkorzekwa Mar 6, 2026
030f126
format python docs
danielkorzekwa Mar 6, 2026
8dcdfbf
Merge branch 'dkorzekwa/anymodel_core' into dkorzekwa/anymodel_activa…
danielkorzekwa Mar 6, 2026
70df0df
Use trust_remote_code in force_cache_dynamic_modules()
danielkorzekwa Mar 6, 2026
bb56662
Merge branch 'dkorzekwa/anymodel_core' into dkorzekwa/anymodel_activa…
danielkorzekwa Mar 6, 2026
ecd953e
Fix anymodel pruning
danielkorzekwa Mar 6, 2026
ee8f538
Fix buid docs issue.
danielkorzekwa Mar 6, 2026
c9b76a1
Merge branch 'dkorzekwa/anymodel_core' into dkorzekwa/anymodel_activa…
danielkorzekwa Mar 6, 2026
6e3af61
Merge branch 'dkorzekwa/anymodel_activation_scoring' into dkorzekwa/a…
danielkorzekwa Mar 6, 2026
0ad6d92
Merging build_library_and_stats
danielkorzekwa Mar 6, 2026
995eb1a
Merging anymodel: calc_one_block_scores
danielkorzekwa Mar 6, 2026
34081c9
Mering any_model: calc_one_block_scores
danielkorzekwa Mar 6, 2026
ed5c00f
merge any_model: mip_and_realize_models
danielkorzekwa Mar 6, 2026
993b5ec
Add all anymodel models but gptoss
danielkorzekwa Mar 6, 2026
6e9f03b
Make nemotron-nano-12b-v2 to work (set trust_remote_code=true)
danielkorzekwa Mar 9, 2026
e8b7a7d
merge anymodel for nemotron-3-nano-30b-a3b-base-bf16
danielkorzekwa Mar 9, 2026
47414d5
Clarify readme and avoid reusing the same reference in llama_converter.
danielkorzekwa Mar 9, 2026
a8305d8
Fix tied-embedding handling before writing the safetensors index.
danielkorzekwa Mar 9, 2026
68421a5
Fix NaN ranking currently selects NaNs as “best” experts by default.
danielkorzekwa Mar 9, 2026
d6b8028
Code clean up.
danielkorzekwa Mar 9, 2026
ecd2341
Code clean up.
danielkorzekwa Mar 10, 2026
f9d845d
code clean up
danielkorzekwa Mar 10, 2026
d171b01
Merge branch 'dkorzekwa/anymodel_core' into dkorzekwa/anymodel_activa…
danielkorzekwa Mar 10, 2026
722da90
Merge branch 'dkorzekwa/anymodel_activation_scoring' into dkorzekwa/a…
danielkorzekwa Mar 10, 2026
934ab2f
code clean up
danielkorzekwa Mar 10, 2026
0f14ec3
Merge branch 'dkorzekwa/anymodel_pruning' into dkorzekwa/anymodel_bui…
danielkorzekwa Mar 10, 2026
dcb9e02
remove not needed comment
danielkorzekwa Mar 10, 2026
0c9ea5d
Merge branch 'dkorzekwa/anymodel_build_library_and_stats' into dkorze…
danielkorzekwa Mar 10, 2026
5b310e2
Merge branch 'dkorzekwa/any_model_calc_one_block_scores' into dkorzek…
danielkorzekwa Mar 10, 2026
4f82b1c
Merge branch 'dkorzekwa/mip_and_realize_models' into dkorzekwa/any_mo…
danielkorzekwa Mar 10, 2026
176a435
Fix a broken test_puzzletron test on 2 gpus.
danielkorzekwa Mar 10, 2026
02e2c9b
Merge branch 'dkorzekwa/anymodel_activation_scoring' into dkorzekwa/a…
danielkorzekwa Mar 10, 2026
92c4419
Merge branch 'dkorzekwa/anymodel_pruning' into dkorzekwa/anymodel_bui…
danielkorzekwa Mar 10, 2026
aa1eb3e
Merge branch 'dkorzekwa/anymodel_build_library_and_stats' into dkorze…
danielkorzekwa Mar 10, 2026
2b84a96
Merge branch 'dkorzekwa/any_model_calc_one_block_scores' into dkorzek…
danielkorzekwa Mar 10, 2026
fb838c0
Merge branch 'dkorzekwa/mip_and_realize_models' into dkorzekwa/any_mo…
danielkorzekwa Mar 10, 2026
cb6b182
Add mamba to puzzletron dependencies.
danielkorzekwa Mar 12, 2026
670bb34
Update mamba-ssm and casual-conv1d dependences (remove pinpoint versi…
danielkorzekwa Mar 13, 2026
0e1b591
Install mamba-ssm and causal-conv1d in testenv:cuda13-gpu-puzzletron
danielkorzekwa Mar 13, 2026
ca845ec
Fix installing dependencies in testenv:cuda13-gpu-puzzletron
danielkorzekwa Mar 13, 2026
be825bc
Fix anymodel for qwen3 8B in 2 gpus
danielkorzekwa Mar 13, 2026
7fd1afa
Fix pipeline parallelism issue for wen3-vl-30b-a3b-instruct-qwen3_vl-…
danielkorzekwa Mar 13, 2026
7d7b609
Fix multi-gpu issue for nemotron-nano-12b-v2
danielkorzekwa Mar 13, 2026
249af9d
Fix no_op in any_model
danielkorzekwa Mar 13, 2026
b80583c
Merge branch 'feature/puzzletron' into dkorzekwa/any_model_other_models
danielkorzekwa Mar 13, 2026
1dd742e
Fix nemotron_h_model_descriptor.
danielkorzekwa Mar 14, 2026
4a6ebbe
Fix tox -e build-docs
danielkorzekwa Mar 14, 2026
585f0ed
pin mamba/casual-conv1d versions to fix failing assertion for test_pu…
danielkorzekwa Mar 14, 2026
7fb5d9a
Fix for installing mamba-ssm
danielkorzekwa Mar 14, 2026
75d3d69
Fix broken test for nemotron-3-nano-30b-a3b-base-bf16
danielkorzekwa Mar 14, 2026
0e5722d
code clean up
danielkorzekwa Mar 14, 2026
2dd9735
Make test_puzzletron test deterministic
danielkorzekwa Mar 15, 2026
3561de5
Comment out all models but nemotron-3-nano-30b-a3b-base-bf16 to check…
danielkorzekwa Mar 15, 2026
27866de
Implement Qwen3VLRemoveExpertsIndependentHook
danielkorzekwa Mar 15, 2026
52922a4
# Initialize weights to ensure all parameters are properly initialized
danielkorzekwa Mar 16, 2026
c234fb4
Fix non-deterministic test_puzzletron test
danielkorzekwa Mar 16, 2026
53dcd10
Fix for unsetting CUDA_VISIBLE_DEVICES
danielkorzekwa Mar 16, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ repos:
rev: v1.17.1
hooks:
- id: mypy
# Exclude HF config directories to avoid duplicate module errors (e.g., configuration_nemotron_h.py exists in multiple model configs)
exclude: "tests/gpu/torch/puzzletron/resources/hf_configs/"

- repo: https://github.com/pre-commit/mirrors-clang-format
rev: v21.1.0
Expand Down
68 changes: 23 additions & 45 deletions modelopt/torch/nas/plugins/megatron_hooks/base_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1142,61 +1142,39 @@ def __call__(


class Qwen3VLRemoveExpertsIndependentHook(RemoveExpertsIndependentHook):
"""Expert removal importance hook for Qwen3-VL models.

TODO: Implement get_router_logits_and_routed_experts based on Qwen3-VL MoE forward pass.
"""
"""Expert removal importance hook for Qwen3-VL models."""

def get_router_logits_and_routed_experts(
self, hidden_states: torch.Tensor, router_logits: torch.Tensor | None = None
) -> tuple[torch.Tensor, torch.Tensor]:
"""Extract router logits and expert outputs for Qwen3-VL MoE.

Note: This is a placeholder implementation. Implement based on Qwen3VLMoeSparseMoe forward.
Based on Qwen3VLMoeSparseMoe forward pass.
"""
batch_size = (
hidden_states.shape[0] * hidden_states.shape[1]
if hidden_states.ndim > 2
else hidden_states.shape[0]
)
router_logits_out = torch.zeros(
batch_size, self.num_local_experts, device=hidden_states.device
)
routed_experts = hidden_states.view(-1, hidden_states.shape[-1])
return router_logits_out, routed_experts
orig_shape = hidden_states.shape

# Flatten to (num_tokens, hidden_size) for processing
hidden_states_flat = hidden_states.reshape(-1, self.moe.hidden_size)

class GptOssRemoveExpertsIndependentHook(RemoveExpertsIndependentHook):
"""Expert removal importance hook for GPT-OSS models.
if router_logits is None:
router_logits = self.moe.gate(hidden_states_flat)

routing_weights = torch.nn.functional.softmax(router_logits, dim=-1, dtype=torch.float)
routing_weights, router_indices = torch.topk(routing_weights, self.moe.top_k, dim=-1)
routing_weights = routing_weights / routing_weights.sum(dim=-1, keepdim=True)
routing_weights = routing_weights.to(hidden_states_flat.dtype)
router_weights = torch.zeros_like(router_logits).scatter_(
1, router_indices, routing_weights
)

TODO: Implement get_router_logits_and_routed_experts based on GPT-OSS MoE forward pass.
This is a placeholder implementation that allows the framework to run.
"""
# Reshape hidden_states for moe.experts (expects 3D: batch, seq, hidden)
# router_weights and router_indices remain 2D (num_tokens, num_experts)
batch_size = orig_shape[0] if hidden_states.ndim == 3 else 1
hidden_states_3d = hidden_states_flat.reshape(batch_size, -1, self.moe.hidden_size)

def get_router_logits_and_routed_experts(
self, hidden_states: torch.Tensor, router_logits: torch.Tensor | None = None
) -> tuple[torch.Tensor, torch.Tensor]:
"""Extract router logits and expert outputs for GPT-OSS MoE.
routed_out = self.moe.experts(hidden_states_3d, router_weights, router_indices)

Note: This is a placeholder implementation. For proper expert scoring,
implement based on GptOssSparseMoeBlock forward pass.
# Return in same shape as input
routed_out = routed_out.reshape(*orig_shape)

Args:
hidden_states: Input tensor of shape (batch, seq_len, hidden_dim)
router_logits: Optional pre-computed router logits

Returns:
tuple of (router_logits, routed_experts):
- router_logits: Shape (num_tokens, num_local_experts) - zeros as placeholder
- routed_experts: Original hidden states (no-op)
"""
batch_size = (
hidden_states.shape[0] * hidden_states.shape[1]
if hidden_states.ndim > 2
else hidden_states.shape[0]
)
router_logits_out = torch.zeros(
batch_size, self.num_local_experts, device=hidden_states.device
)
routed_experts = hidden_states.view(-1, hidden_states.shape[-1])
return router_logits_out, routed_experts
return router_logits, routed_out
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@

from typing import Type

import torch

from modelopt.torch.nas.plugins.megatron_hooks.base_hooks import ForwardHook as ActivationsHook
from modelopt.torch.puzzletron.tools.logger import aprint
from modelopt.torch.puzzletron.utils.dummy_modules import DummyBlock, DummyModule


def register_activation_hooks(
Expand Down Expand Up @@ -51,6 +54,16 @@ def register_activation_hooks(
module_names_to_hook = pruning_mixin.get_module_names_to_hook(model)
activation_hooks = dict()
for block_idx, module_name in module_names_to_hook:
try:
module = model.get_submodule(module_name)
except AttributeError:
# Module doesn't exist on this rank's shard (e.g., in distributed setup)
continue

# Skip dummy modules - they don't have real activations to hook
if isinstance(module, (DummyModule, DummyBlock)):
continue

block_config = None
if block_idx is not None:
block_config = model.config.block_configs[block_idx]
Comment on lines +67 to +69
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Guard block_config lookup before indexing.

Line 69 assumes model.config.block_configs exists and that block_idx is in range. That can raise AttributeError/IndexError at runtime for incompatible configs.

Suggested fix
-        block_config = None
-        if block_idx is not None:
-            block_config = model.config.block_configs[block_idx]
+        block_config = None
+        if block_idx is not None:
+            block_configs = getattr(getattr(model, "config", None), "block_configs", None)
+            if block_configs is None or not (0 <= block_idx < len(block_configs)):
+                raise ValueError(
+                    f"Invalid block_idx={block_idx} for model block_configs."
+                )
+            block_config = block_configs[block_idx]
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@modelopt/torch/puzzletron/activation_scoring/activation_hooks/utils.py`
around lines 67 - 69, The current block_config lookup assumes
model.config.block_configs exists and block_idx is valid; guard this by first
checking hasattr/getattr for model.config.block_configs (or that
getattr(model.config, "block_configs", None) is not None), verify block_idx is
an int and within range (0 <= block_idx < len(model.config.block_configs))
before indexing, and fall back to None if any check fails; update the
block_config assignment logic around block_config/block_idx to perform these
validations to avoid AttributeError/IndexError.

Expand All @@ -59,13 +72,25 @@ def register_activation_hooks(
"block_config": block_config,
}

module = model.get_submodule(module_name)
hook = hook_class(module, curr_activation_hooks_kwargs)
module.register_forward_hook(hook)
activation_hooks[module_name] = hook

if len(activation_hooks) == 0:
raise ValueError("couldn't find any hooks")
# In distributed mode, it's okay for a rank to have 0 hooks if it doesn't own
# the target modules (e.g., with hybrid patterns like "*-" where different
# ranks own different layer types). However, we still want to catch real bugs
# where no hooks are found at all.
is_distributed = torch.distributed.is_available() and torch.distributed.is_initialized()
if is_distributed:
aprint(
"No hooks registered on this rank. This is expected if this rank "
"doesn't own any layers matching the hook pattern (e.g., in hybrid "
"patterns with distributed model sharding)."
)
else:
raise ValueError("couldn't find any hooks")

Comment on lines +79 to +93
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Distributed mode can silently proceed with zero hooks globally.

On Lines 85-90, distributed runs only log when local hooks are zero. If all ranks have zero hooks, this returns successfully and downstream scoring can run without instrumentation.

Suggested fix
     if len(activation_hooks) == 0:
@@
-        if is_distributed:
-            aprint(
-                "No hooks registered on this rank. This is expected if this rank "
-                "doesn't own any layers matching the hook pattern (e.g., in hybrid "
-                "patterns with distributed model sharding)."
-            )
+        if is_distributed:
+            local_count = torch.tensor([0], device="cuda" if torch.cuda.is_available() else "cpu")
+            global_count = local_count.clone()
+            torch.distributed.all_reduce(global_count, op=torch.distributed.ReduceOp.SUM)
+            if global_count.item() == 0:
+                raise ValueError("couldn't find any hooks on any distributed rank")
+            aprint(
+                "No hooks registered on this rank. This is expected if this rank "
+                "doesn't own any layers matching the hook pattern (e.g., in hybrid "
+                "patterns with distributed model sharding)."
+            )
         else:
             raise ValueError("couldn't find any hooks")
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@modelopt/torch/puzzletron/activation_scoring/activation_hooks/utils.py`
around lines 79 - 93, The current check only logs when the local rank has zero
activation_hooks, which lets a distributed job proceed if every rank has zero
hooks; change the logic so that when torch.distributed.is_available() and
torch.distributed.is_initialized() you compute the global sum of
len(activation_hooks) across ranks (e.g., create a tensor from
len(activation_hooks) and use torch.distributed.all_reduce) and then raise
ValueError if the global sum is 0; keep the existing local informational aprint
when local len is 0 but ensure the global check prevents silent success when all
ranks found zero hooks.

aprint(f"Found the following hooks: {activation_hooks.keys()}")
if len(activation_hooks) > 0:
aprint(f"Found the following hooks: {activation_hooks.keys()}")
return activation_hooks
8 changes: 6 additions & 2 deletions modelopt/torch/puzzletron/anymodel/converter/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,10 @@ def convert_configs_in_dirs(
cls,
input_dir: Path,
output_dir: Path,
trust_remote_code: bool = False,
):
"""Convert config and add block_configs."""
config = load_model_config(input_dir)
config = load_model_config(input_dir, trust_remote_code=trust_remote_code)

block_configs = cls.create_block_configs_from_main_config(config)
out_config = copy.deepcopy(config)
Expand Down Expand Up @@ -179,7 +180,10 @@ def convert(
output_dir: Path to the output AnyModel checkpoint.
"""
cls.copy_checkpoint_files(input_dir, output_dir)
config = cls.convert_configs_in_dirs(input_dir, output_dir)
trust_remote_code = descriptor.requires_trust_remote_code()
config = cls.convert_configs_in_dirs(
input_dir, output_dir, trust_remote_code=trust_remote_code
)
cls.convert_model_weights(
input_dir, output_dir, descriptor=descriptor, num_hidden_layers=config.num_hidden_layers
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ def block_config_to_layer_overrides(block_config: BlockConfig) -> Dict[str, Any]
"""
raise NotImplementedError

@staticmethod
def requires_trust_remote_code() -> bool:
"""Whether this model descriptor requires trust_remote_code=True for loading.

Models that use custom code (e.g., via auto_map in config) should override
this to return True.

Returns:
True if trust_remote_code=True is required, False otherwise.
"""
return False

@staticmethod
def mlp_no_op_post_init(decoder_layer: nn.Module):
"""Post-init callback to alter a decoder layer so that FFN/mlp subblock performs as no-op.
Expand Down
12 changes: 6 additions & 6 deletions modelopt/torch/puzzletron/anymodel/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
# Import models to trigger factory registration
# from modelopt.torch.puzzletron.anymodel.models.gpt_oss_20b import *
from modelopt.torch.puzzletron.anymodel.models.llama import *
# from modelopt.torch.puzzletron.anymodel.models.mistral_small import *
# from modelopt.torch.puzzletron.anymodel.models.nemotron_h import *
# from modelopt.torch.puzzletron.anymodel.models.nemotron_h_v2 import *
# from modelopt.torch.puzzletron.anymodel.models.qwen2 import *
# from modelopt.torch.puzzletron.anymodel.models.qwen3_8b import *
# from modelopt.torch.puzzletron.anymodel.models.qwen3_vl_30b_a3b_instruct import *
from modelopt.torch.puzzletron.anymodel.models.mistral_small import *
from modelopt.torch.puzzletron.anymodel.models.nemotron_h import *
from modelopt.torch.puzzletron.anymodel.models.nemotron_h_v2 import *
from modelopt.torch.puzzletron.anymodel.models.qwen2 import *
from modelopt.torch.puzzletron.anymodel.models.qwen3_8b import *
from modelopt.torch.puzzletron.anymodel.models.qwen3_vl_30b_a3b_instruct import *
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# 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.

from modelopt.torch.puzzletron.anymodel.models.mistral_small.mistral_small_converter import (
MistralSmallConverter,
)
from modelopt.torch.puzzletron.anymodel.models.mistral_small.mistral_small_model_descriptor import (
MistralSmallModelDescriptor,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# 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.
# mypy: ignore-errors

from typing import List

from transformers import MistralConfig

from modelopt.torch.puzzletron.anymodel.converter import Converter, ConverterFactory
from modelopt.torch.puzzletron.decilm.deci_lm_hf_code.block_config import (
AttentionConfig,
BlockConfig,
FFNConfig,
)


@ConverterFactory.register_decorator("mistral_small")
class MistralSmallConverter(Converter):
@staticmethod
def create_block_configs_from_main_config(config: MistralConfig) -> List[BlockConfig]:
num_hidden_layers = config.num_hidden_layers

block_config = BlockConfig(
attention=AttentionConfig(no_op=False, num_key_value_heads=config.num_key_value_heads),
ffn=FFNConfig(no_op=False, intermediate_size=config.intermediate_size),
).to_dict()

block_configs = [block_config] * num_hidden_layers
return block_configs
Loading
Loading