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

refactor minicpm optimization #11816

Merged
Merged
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
54 changes: 19 additions & 35 deletions python/llm/src/ipex_llm/transformers/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -747,17 +747,20 @@ def _optimize_pre(model, qtype=None):
if model.config.model_type == "llama":
from ipex_llm.transformers.models.llama import merge_qkv
model.apply(merge_qkv)
if model.config.model_type == "minicpm":
from ipex_llm.transformers.models.minicpm import merge_qkv
model.apply(merge_qkv)
if model.config.model_type == "minicpmv":
from ipex_llm.transformers.models.minicpmv import merge_qkv
model.vpm.apply(merge_qkv)
if model.config.hidden_size == 3584 and model.config.vocab_size == 151666:
if model.config.hidden_size == 2304 and model.config.vocab_size == 122753:
model.llm.config.model_type = "minicpm"
elif model.config.hidden_size == 3584 and model.config.vocab_size == 151666:
model.llm.config.model_type = "qwen2"
_optimize_pre(model.llm, qtype=qtype)
model.llm.config.model_type = "minicpmv"
elif model.config.hidden_size == 4096 and model.config.vocab_size == 128256:
model.llm.config.model_type = "llama"
_optimize_pre(model.llm, qtype=qtype)
model.llm.config.model_type = "minicpmv"
_optimize_pre(model.llm, qtype=qtype)
model.llm.config.model_type = "minicpmv"

return model

Expand Down Expand Up @@ -1699,31 +1702,16 @@ def safe_bmm_fwd(*args, **kwargs):
module.StableLmModel,
stablelm_model_forward
)
elif model.config.model_type == 'minicpm':
elif model.config.model_type == "minicpm":
modeling_module_name = model.__class__.__module__
module = importlib.import_module(modeling_module_name)
if version.parse(trans_version) >= version.parse("4.39.0"):
from ipex_llm.transformers.models.minicpm import minicpm_attention_forward_4_39
convert_forward(model,
module.MiniCPMAttention,
minicpm_attention_forward_4_39)
else:
from ipex_llm.transformers.models.minicpm import minicpm_attention_forward
convert_forward(model,
module.MiniCPMAttention,
minicpm_attention_forward)
from ipex_llm.transformers.models.minicpm import minicpm_model_forward

convert_forward(model,
module.MiniCPMMLP,
llama_mlp_forward)
convert_forward(model,
module.MiniCPMRMSNorm,
llama_rms_norm_forward)

convert_forward(model,
module.MiniCPMModel,
minicpm_model_forward)
from ipex_llm.transformers.models.minicpm import minicpm_attention_forward
from ipex_llm.transformers.models.minicpm import minicpm_model_forward_wrapper
convert_forward(model, module.MiniCPMAttention, minicpm_attention_forward)
convert_forward(model, module.MiniCPMMLP, llama_mlp_forward)
convert_forward(model, module.MiniCPMRMSNorm, llama_rms_norm_forward)
minicpm_model_forward = minicpm_model_forward_wrapper(module.MiniCPMModel.forward)
convert_forward(model, module.MiniCPMModel, minicpm_model_forward)
elif model.config.model_type == "minicpmv":
modeling_module_name = model.__class__.__module__
module = importlib.import_module(modeling_module_name)
Expand All @@ -1734,18 +1722,14 @@ def safe_bmm_fwd(*args, **kwargs):
if model.config.hidden_size == 2304 and model.config.vocab_size == 122753:
# MiniCPM-V 2
model.llm.config.model_type = "minicpm"
_optimize_post(model.llm, lightweight_bmm=lightweight_bmm)
model.llm.config.model_type = "minicpmv"
if model.config.hidden_size == 3584 and model.config.vocab_size == 151666:
elif model.config.hidden_size == 3584 and model.config.vocab_size == 151666:
# MiniCPM-V 2.6
model.llm.config.model_type = "qwen2"
_optimize_post(model.llm, lightweight_bmm=lightweight_bmm)
model.llm.config.model_type = "minicpmv"
elif model.config.hidden_size == 4096 and model.config.vocab_size == 128256:
# MiniCPM-V 2.5
model.llm.config.model_type = "llama"
_optimize_post(model.llm, lightweight_bmm=lightweight_bmm)
model.llm.config.model_type = "minicpmv"
_optimize_post(model.llm, lightweight_bmm=lightweight_bmm)
model.llm.config.model_type = "minicpmv"

vpm_modeling_module_name = model.vpm.__class__.__module__
vpm_module = importlib.import_module(vpm_modeling_module_name)
Expand Down
Loading
Loading