|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +"""Unit tests for modelopt.torch.export.layer_utils — MoE detection and expert naming.""" |
| 17 | + |
| 18 | +import pytest |
| 19 | +import torch.nn as nn |
| 20 | + |
| 21 | +from modelopt.torch.export.layer_utils import get_expert_linear_names, is_moe |
| 22 | + |
| 23 | +# --------------------------------------------------------------------------- |
| 24 | +# is_moe tests |
| 25 | +# --------------------------------------------------------------------------- |
| 26 | + |
| 27 | + |
| 28 | +class _FakeSparseMoeBlock(nn.Module): |
| 29 | + """Name ends with 'sparsemoeblock' — detected by naming convention.""" |
| 30 | + |
| 31 | + |
| 32 | +class _FakeMoeLayer(nn.Module): |
| 33 | + """Name contains 'moelayer' — detected by naming convention.""" |
| 34 | + |
| 35 | + |
| 36 | +class _FakeArcticMoe(nn.Module): |
| 37 | + """Name contains 'arcticmoe' — detected by explicit match.""" |
| 38 | + |
| 39 | + |
| 40 | +class _StructuralMoeModule(nn.Module): |
| 41 | + """Has router + experts attributes — detected by structural check.""" |
| 42 | + |
| 43 | + def __init__(self): |
| 44 | + super().__init__() |
| 45 | + self.router = nn.Linear(8, 4) |
| 46 | + self.experts = nn.ModuleList([nn.Linear(8, 8) for _ in range(4)]) |
| 47 | + |
| 48 | + |
| 49 | +class _NotMoeModule(nn.Module): |
| 50 | + """Plain module — should NOT be classified as MoE.""" |
| 51 | + |
| 52 | + def __init__(self): |
| 53 | + super().__init__() |
| 54 | + self.fc = nn.Linear(8, 8) |
| 55 | + |
| 56 | + |
| 57 | +class _PartialStructuralModule(nn.Module): |
| 58 | + """Has router but no experts — should NOT be classified as MoE.""" |
| 59 | + |
| 60 | + def __init__(self): |
| 61 | + super().__init__() |
| 62 | + self.router = nn.Linear(8, 4) |
| 63 | + |
| 64 | + |
| 65 | +@pytest.mark.parametrize( |
| 66 | + "module_cls", |
| 67 | + [_FakeSparseMoeBlock, _FakeMoeLayer, _FakeArcticMoe], |
| 68 | +) |
| 69 | +def test_is_moe_name_based(module_cls): |
| 70 | + assert is_moe(module_cls()) |
| 71 | + |
| 72 | + |
| 73 | +def test_is_moe_structural(): |
| 74 | + assert is_moe(_StructuralMoeModule()) |
| 75 | + |
| 76 | + |
| 77 | +def test_is_moe_negative(): |
| 78 | + assert not is_moe(_NotMoeModule()) |
| 79 | + |
| 80 | + |
| 81 | +def test_is_moe_partial_structural(): |
| 82 | + assert not is_moe(_PartialStructuralModule()) |
| 83 | + |
| 84 | + |
| 85 | +# --------------------------------------------------------------------------- |
| 86 | +# get_expert_linear_names tests |
| 87 | +# --------------------------------------------------------------------------- |
| 88 | + |
| 89 | + |
| 90 | +class _FakeGemma4TextDecoderLayer(nn.Module): |
| 91 | + pass |
| 92 | + |
| 93 | + |
| 94 | +class _FakeMixtralSparseMoeBlock(nn.Module): |
| 95 | + pass |
| 96 | + |
| 97 | + |
| 98 | +class _FakeNemotronHMOE(nn.Module): |
| 99 | + pass |
| 100 | + |
| 101 | + |
| 102 | +def test_get_expert_linear_names_gemma4(): |
| 103 | + assert get_expert_linear_names(_FakeGemma4TextDecoderLayer()) == [ |
| 104 | + "gate_proj", |
| 105 | + "down_proj", |
| 106 | + "up_proj", |
| 107 | + ] |
| 108 | + |
| 109 | + |
| 110 | +def test_get_expert_linear_names_mixtral(): |
| 111 | + assert get_expert_linear_names(_FakeMixtralSparseMoeBlock()) == ["w1", "w2", "w3"] |
| 112 | + |
| 113 | + |
| 114 | +def test_get_expert_linear_names_nemotron(): |
| 115 | + assert get_expert_linear_names(_FakeNemotronHMOE()) == ["up_proj", "down_proj"] |
0 commit comments