Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Qwen2MoE #593

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions auto_gptq/modeling/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from .phi import PhiGPTQForCausalLM
from .qwen import QwenGPTQForCausalLM
from .qwen2 import Qwen2GPTQForCausalLM
from .qwen2_moe import Qwen2MoeGPTQForCausalLM
from .rw import RWGPTQForCausalLM
from .stablelmepoch import StableLMEpochGPTQForCausalLM
from .starcoder2 import Starcoder2GPTQForCausalLM
Expand Down
11 changes: 10 additions & 1 deletion auto_gptq/modeling/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@
preprocess_checkpoint_qigen,
simple_dispatch_model,
unpack_awq,
get_moe_inside_layer_modules,
)


logger = logging.getLogger(__name__)
handler = logging.StreamHandler()
formatter = logging.Formatter("%(levelname)s - %(message)s")
Expand Down Expand Up @@ -398,6 +398,11 @@ def store_input_hook(_, args, kwargs):
inside_layer_modules = self.inside_layer_modules
if not self.quantize_config.true_sequential:
inside_layer_modules = [sum(inside_layer_modules, [])]

if hasattr(self.model.config, "num_experts"):
inside_layer_modules = get_moe_inside_layer_modules(self.inside_layer_modules,
self.model.config.num_experts)
Comment on lines +402 to +404
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why is self.inside_layer_modules not enough?

Copy link
Author

Choose a reason for hiding this comment

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

We cannot write the names of all the parameters in our MoE model because we may have more experts. Besides, we may have different numbers of experts for different model sizes.

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree it is a bit clunky but you can just add like the highest amount of experts you may have and the models with less will work too.

Copy link
Author

Choose a reason for hiding this comment

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

I agree it is a bit clunky but you can just add like the highest amount of experts you may have and the models with less will work too.

It will work, but I think the current codes look much better than writing hundreds of parameter names in the modeling file.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't disagree, but I would prefer a separate PR for for something like this that others could rely on. I know that is just for slightly easier git history. I just prefer PRs to stay focused and do the minimum changes required. Feel free to ignore me, it's not that important.


quantizers = {}
for i in range(len(layers)):
logger.info(f"Start quantizing layer {i + 1}/{len(layers)}")
Expand Down Expand Up @@ -1007,6 +1012,10 @@ def skip(*args, **kwargs):
config, trust_remote_code=trust_remote_code, torch_dtype=torch_dtype
)

if hasattr(config, "num_experts"):
cls.inside_layer_modules = get_moe_inside_layer_modules(cls.inside_layer_modules,
config.num_experts)

layers = find_layers(model)
ignore_layers = [cls.lm_head_name] + cls.outside_layer_modules
for name in list(layers.keys()):
Expand Down
2 changes: 2 additions & 0 deletions auto_gptq/modeling/_const.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
SUPPORTED_MODELS.append("phi")
if compare_transformers_version("v4.38.0", op="ge"):
SUPPORTED_MODELS.append("gemma")
if compare_transformers_version("v4.39.0.dev0", op="ge"):
SUPPORTED_MODELS.append("qwen2_moe")
if compare_transformers_version("v4.39.0.dev0", op="ge"):
SUPPORTED_MODELS.append("starcoder2")

Expand Down
15 changes: 15 additions & 0 deletions auto_gptq/modeling/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,21 @@ def get_checkpoints(model_name_or_path: str, extensions: List[str], possible_mod
return False, resolved_archive_file, true_model_basename


# generate inside layer modules for MoE models with massive experts
def get_moe_inside_layer_modules(inside_layer_modules, num_experts):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you add type hints and a small description here?

new_inside_layer_modules = []
for names in inside_layer_modules:
new_inside_layer_modules.append([])
for n in names:
if "{expert_idx}" in n:
for expert_idx in range(num_experts):
new_inside_layer_modules[-1].append(n.replace("{expert_idx}", str(expert_idx)))
else:
new_inside_layer_modules[-1].append(n)

return new_inside_layer_modules


__all__ = [
"get_device",
"move_to_device",
Expand Down
2 changes: 2 additions & 0 deletions auto_gptq/modeling/auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from .phi import PhiGPTQForCausalLM
from .qwen import QwenGPTQForCausalLM
from .qwen2 import Qwen2GPTQForCausalLM
from .qwen2_moe import Qwen2MoeGPTQForCausalLM
from .rw import RWGPTQForCausalLM
from .stablelmepoch import StableLMEpochGPTQForCausalLM
from .starcoder2 import Starcoder2GPTQForCausalLM
Expand Down Expand Up @@ -53,6 +54,7 @@
"starcoder2": Starcoder2GPTQForCausalLM,
"mixtral": MixtralGPTQForCausalLM,
"qwen2": Qwen2GPTQForCausalLM,
"qwen2_moe": Qwen2MoeGPTQForCausalLM,
"longllama": LongLlamaGPTQForCausalLM,
"gemma": GemmaGPTQForCausalLM,
"phi": PhiGPTQForCausalLM,
Expand Down
18 changes: 18 additions & 0 deletions auto_gptq/modeling/qwen2_moe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from ._base import BaseGPTQForCausalLM


class Qwen2MoeGPTQForCausalLM(BaseGPTQForCausalLM):
layer_type = "Qwen2DecoderLayer"
layers_block_name = "model.layers"
outside_layer_modules = ["model.embed_tokens", "model.norm"]
inside_layer_modules = [
["self_attn.k_proj", "self_attn.v_proj", "self_attn.q_proj"],
["self_attn.o_proj"],
["mlp.shared_expert.up_proj", "mlp.shared_expert.gate_proj"],
["mlp.shared_expert.down_proj"],
["mlp.experts.{expert_idx}.up_proj", "mlp.experts.{expert_idx}.gate_proj"],
["mlp.experts.{expert_idx}.down_proj"],
]


__all__ = ["Qwen2MoeGPTQForCausalLM"]