moe.py 3.12 KB
Newer Older
chenych's avatar
chenych committed
1
# Copyright 2025 the LlamaFactory team.
chenych's avatar
chenych committed
2
3
4
5
6
7
8
9
10
11
12
13
14
#
# 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.

chenych's avatar
chenych committed
15
from typing import TYPE_CHECKING
chenych's avatar
chenych committed
16
17
18

import torch
from transformers.integrations import is_deepspeed_zero3_enabled
luopl's avatar
luopl committed
19
20

from ...extras.misc import check_version
chenych's avatar
chenych committed
21
22
23
24
25
26
27
28


if TYPE_CHECKING:
    from transformers import PretrainedConfig, PreTrainedModel

    from ...hparams import ModelArguments


chenych's avatar
chenych committed
29
def _set_z3_leaf_modules(model: "PreTrainedModel", leaf_modules: list["torch.nn.Module"]) -> None:
luopl's avatar
luopl committed
30
    check_version("deepspeed>=0.13.0")
chenych's avatar
chenych committed
31
32
33
34
35
36
    from deepspeed.utils import set_z3_leaf_modules  # type: ignore

    set_z3_leaf_modules(model, leaf_modules)


def add_z3_leaf_module(model: "PreTrainedModel") -> None:
chenych's avatar
chenych committed
37
    r"""Set module as a leaf module to skip partitioning in deepspeed zero3."""
chenych's avatar
chenych committed
38
39
40
    if not is_deepspeed_zero3_enabled():
        return

luopl's avatar
luopl committed
41
42
    model_type = getattr(model.config, "model_type", None)
    if model_type == "dbrx":
chenych's avatar
chenych committed
43
44
45
46
        from transformers.models.dbrx.modeling_dbrx import DbrxFFN

        _set_z3_leaf_modules(model, [DbrxFFN])

luopl's avatar
luopl committed
47
    if model_type == "jamba":
chenych's avatar
chenych committed
48
49
50
51
        from transformers.models.jamba.modeling_jamba import JambaSparseMoeBlock

        _set_z3_leaf_modules(model, [JambaSparseMoeBlock])

luopl's avatar
luopl committed
52
    if model_type == "jetmoe":
chenych's avatar
chenych committed
53
54
55
56
        from transformers.models.jetmoe.modeling_jetmoe import JetMoeMoA, JetMoeMoE

        _set_z3_leaf_modules(model, [JetMoeMoA, JetMoeMoE])

chenych's avatar
chenych committed
57
58
59
60
61
62
    if model_type in ["kimi_vl", "deepseek_v3"]:
        check_version("transformers>=4.51.1")
        from transformers.models.deepseek_v3.modeling_deepseek_v3 import DeepseekV3MoE

        _set_z3_leaf_modules(model, [DeepseekV3MoE])

luopl's avatar
luopl committed
63
    if model_type == "mixtral":
chenych's avatar
chenych committed
64
65
66
67
        from transformers.models.mixtral.modeling_mixtral import MixtralSparseMoeBlock

        _set_z3_leaf_modules(model, [MixtralSparseMoeBlock])

chenych's avatar
chenych committed
68
    if model_type == "qwen2_moe":
chenych's avatar
chenych committed
69
70
71
72
73
74
        from transformers.models.qwen2_moe.modeling_qwen2_moe import Qwen2MoeSparseMoeBlock

        _set_z3_leaf_modules(model, [Qwen2MoeSparseMoeBlock])


def configure_moe(config: "PretrainedConfig", model_args: "ModelArguments", is_trainable: bool) -> None:
luopl's avatar
luopl committed
75
    model_type = getattr(config, "model_type", None)
chenych's avatar
chenych committed
76
    if model_args.moe_aux_loss_coef is not None:
luopl's avatar
luopl committed
77
        if model_type in ["jamba", "mixtral", "qwen2_moe"]:
chenych's avatar
chenych committed
78
79
            setattr(config, "router_aux_loss_coef", model_args.moe_aux_loss_coef)

luopl's avatar
luopl committed
80
        elif model_type == "deepseek":
chenych's avatar
chenych committed
81
82
            setattr(config, "aux_loss_alpha", model_args.moe_aux_loss_coef)

luopl's avatar
luopl committed
83
        elif model_type == "jetmoe":
chenych's avatar
chenych committed
84
85
            setattr(config, "aux_loss_coef", model_args.moe_aux_loss_coef)

luopl's avatar
luopl committed
86
    if model_type in ["dbrx", "jamba", "jetmoe", "mixtral", "qwen2_moe"]:
chenych's avatar
chenych committed
87
        setattr(config, "output_router_logits", is_trainable)