|
| 1 | +""" |
| 2 | +This file specifies how MLC's Mixtral parameter maps from other formats, for example HuggingFace |
| 3 | +PyTorch, HuggingFace safetensors. |
| 4 | +""" |
| 5 | +import functools |
| 6 | + |
| 7 | +import numpy as np |
| 8 | + |
| 9 | +from mlc_chat.loader import ExternMapping |
| 10 | +from mlc_chat.quantization import Quantization |
| 11 | + |
| 12 | +from .mixtral_model import MixtralConfig, MixtralForCasualLM |
| 13 | + |
| 14 | + |
| 15 | +def huggingface(model_config: MixtralConfig, quantization: Quantization) -> ExternMapping: |
| 16 | + """Returns a parameter mapping that maps from the names of MLC LLM parameters to |
| 17 | + the names of HuggingFace PyTorch parameters. |
| 18 | +
|
| 19 | + Parameters |
| 20 | + ---------- |
| 21 | + model_config : MixtralConfig |
| 22 | + The configuration of the Mixtral model. |
| 23 | +
|
| 24 | + quantization : Quantization |
| 25 | + The quantization configuration. |
| 26 | +
|
| 27 | + Returns |
| 28 | + ------- |
| 29 | + param_map : ExternMapping |
| 30 | + The parameter mapping from MLC to HuggingFace PyTorch. |
| 31 | + """ |
| 32 | + model = MixtralForCasualLM(model_config) |
| 33 | + if quantization is not None: |
| 34 | + model.to(quantization.model_dtype) |
| 35 | + _, _named_params, _ = model.export_tvm( # type: ignore[misc] |
| 36 | + spec=model.get_default_spec(), |
| 37 | + allow_extern=True, |
| 38 | + ) |
| 39 | + named_parameters = dict(_named_params) |
| 40 | + |
| 41 | + mapping = ExternMapping() |
| 42 | + |
| 43 | + for i in range(model_config.num_hidden_layers): |
| 44 | + # Add QKV in self attention |
| 45 | + attn = f"model.layers.{i}.self_attn" |
| 46 | + mlc_name = f"{attn}.qkv_proj.weight" |
| 47 | + mlc_param = named_parameters[mlc_name] |
| 48 | + mapping.add_mapping( |
| 49 | + mlc_name, |
| 50 | + [ |
| 51 | + f"{attn}.q_proj.weight", |
| 52 | + f"{attn}.k_proj.weight", |
| 53 | + f"{attn}.v_proj.weight", |
| 54 | + ], |
| 55 | + functools.partial( |
| 56 | + lambda q, k, v, dtype: np.concatenate([q, k, v], axis=0).astype(dtype), |
| 57 | + dtype=mlc_param.dtype, |
| 58 | + ), |
| 59 | + ) |
| 60 | + |
| 61 | + # Add gates in MLP (when MoE is enabled) |
| 62 | + mlp = f"model.layers.{i}.block_sparse_moe" |
| 63 | + mlc_mlp = f"model.layers.{i}.moe" |
| 64 | + mlc_name = f"{mlc_mlp}.e1_e3.weight" |
| 65 | + mlc_param = named_parameters[mlc_name] |
| 66 | + |
| 67 | + def combine_expert_gate_up(*hf_params, dtype): |
| 68 | + stack = [] |
| 69 | + for i in range(0, len(hf_params), 2): |
| 70 | + stack.append(np.concatenate([hf_params[i], hf_params[i + 1]], axis=0)) |
| 71 | + return np.stack(stack, axis=0).astype(dtype) |
| 72 | + |
| 73 | + mapping.add_mapping( |
| 74 | + mlc_name, |
| 75 | + functools.reduce( |
| 76 | + lambda a, b: a + b, |
| 77 | + [ |
| 78 | + [ |
| 79 | + f"{mlp}.experts.{expert_id}.w1.weight", |
| 80 | + f"{mlp}.experts.{expert_id}.w3.weight", |
| 81 | + ] |
| 82 | + for expert_id in range(model_config.num_local_experts) |
| 83 | + ], |
| 84 | + ), |
| 85 | + functools.partial( |
| 86 | + combine_expert_gate_up, |
| 87 | + dtype=mlc_param.dtype, |
| 88 | + ), |
| 89 | + ) |
| 90 | + |
| 91 | + mlc_name = f"{mlc_mlp}.e2.weight" |
| 92 | + mlc_param = named_parameters[mlc_name] |
| 93 | + mapping.add_mapping( |
| 94 | + mlc_name, |
| 95 | + [ |
| 96 | + f"{mlp}.experts.{expert_id}.w2.weight" |
| 97 | + for expert_id in range(model_config.num_local_experts) |
| 98 | + ], |
| 99 | + functools.partial( |
| 100 | + lambda *hf_params, dtype: np.stack(hf_params, axis=0).astype(dtype), |
| 101 | + dtype=mlc_param.dtype, |
| 102 | + ), |
| 103 | + ) |
| 104 | + |
| 105 | + mlc_name = f"{mlc_mlp}.gate.weight" |
| 106 | + mlc_param = named_parameters[mlc_name] |
| 107 | + mapping.add_mapping( |
| 108 | + mlc_name, |
| 109 | + [f"{mlp}.gate.weight"], |
| 110 | + functools.partial( |
| 111 | + lambda x, dtype: x.astype(dtype), |
| 112 | + dtype=mlc_param.dtype, |
| 113 | + ), |
| 114 | + ) |
| 115 | + |
| 116 | + # inv_freq is not used in the model |
| 117 | + mapping.add_unused(f"{attn}.rotary_emb.inv_freq") |
| 118 | + |
| 119 | + for mlc_name, mlc_param in named_parameters.items(): |
| 120 | + if mlc_name not in mapping.param_map: |
| 121 | + mapping.add_mapping( |
| 122 | + mlc_name, |
| 123 | + [mlc_name], |
| 124 | + functools.partial( |
| 125 | + lambda x, dtype: x.astype(dtype), |
| 126 | + dtype=mlc_param.dtype, |
| 127 | + ), |
| 128 | + ) |
| 129 | + return mapping |
0 commit comments