|
| 1 | +""" |
| 2 | +This file specifies how MLC's OLMo parameter maps from other formats, for example HuggingFace |
| 3 | +PyTorch, HuggingFace safetensors. |
| 4 | +""" |
| 5 | + |
| 6 | +import functools |
| 7 | + |
| 8 | +import numpy as np |
| 9 | + |
| 10 | +from mlc_llm.loader import ExternMapping |
| 11 | +from mlc_llm.quantization import Quantization |
| 12 | + |
| 13 | +from .olmo_model import OLMoConfig, OLMoForCausalLM |
| 14 | +from .olmo_quantization import awq_quant |
| 15 | + |
| 16 | + |
| 17 | +def huggingface(model_config: OLMoConfig, quantization: Quantization) -> ExternMapping: |
| 18 | + """Returns a parameter mapping that maps from the names of MLC LLM parameters to |
| 19 | + the names of HuggingFace PyTorch parameters. |
| 20 | +
|
| 21 | + Parameters |
| 22 | + ---------- |
| 23 | + model_config : OLMoConfig |
| 24 | + The configuration of the OLMo model. |
| 25 | +
|
| 26 | + quantization : Quantization |
| 27 | + The quantization configuration. |
| 28 | +
|
| 29 | + Returns |
| 30 | + ------- |
| 31 | + param_map : ExternMapping |
| 32 | + The parameter mapping from MLC to HuggingFace PyTorch. |
| 33 | + """ |
| 34 | + model = OLMoForCausalLM(model_config) |
| 35 | + if quantization is not None: |
| 36 | + model.to(quantization.model_dtype) |
| 37 | + _, _named_params, _ = model.export_tvm( # type: ignore[misc] |
| 38 | + spec=model.get_default_spec(), |
| 39 | + allow_extern=True, |
| 40 | + ) |
| 41 | + named_parameters = dict(_named_params) |
| 42 | + |
| 43 | + mapping = ExternMapping() |
| 44 | + |
| 45 | + for i in range(model_config.num_hidden_layers): |
| 46 | + # Add QKV in self attention |
| 47 | + attn = f"model.layers.{i}.self_attn" |
| 48 | + mlc_name = f"{attn}.qkv_proj.weight" |
| 49 | + mlc_param = named_parameters[mlc_name] |
| 50 | + mapping.add_mapping( |
| 51 | + mlc_name, |
| 52 | + [ |
| 53 | + f"{attn}.q_proj.weight", |
| 54 | + f"{attn}.k_proj.weight", |
| 55 | + f"{attn}.v_proj.weight", |
| 56 | + ], |
| 57 | + functools.partial( |
| 58 | + lambda q, k, v, dtype: np.concatenate([q, k, v], axis=0).astype(dtype), |
| 59 | + dtype=mlc_param.dtype, |
| 60 | + ), |
| 61 | + ) |
| 62 | + # Add gates in MLP |
| 63 | + mlp = f"model.layers.{i}.mlp" |
| 64 | + mlc_name = f"{mlp}.gate_up_proj.weight" |
| 65 | + mlc_param = named_parameters[mlc_name] |
| 66 | + mapping.add_mapping( |
| 67 | + mlc_name, |
| 68 | + [ |
| 69 | + f"{mlp}.gate_proj.weight", |
| 70 | + f"{mlp}.up_proj.weight", |
| 71 | + ], |
| 72 | + functools.partial( |
| 73 | + lambda gate, up, dtype: np.concatenate([gate, up], axis=0).astype(dtype), |
| 74 | + dtype=mlc_param.dtype, |
| 75 | + ), |
| 76 | + ) |
| 77 | + # inv_freq is not used in the model |
| 78 | + mapping.add_unused(f"{attn}.rotary_emb.inv_freq") |
| 79 | + |
| 80 | + for mlc_name, mlc_param in named_parameters.items(): |
| 81 | + if mlc_name not in mapping.param_map: |
| 82 | + mapping.add_mapping( |
| 83 | + mlc_name, |
| 84 | + [mlc_name], |
| 85 | + functools.partial( |
| 86 | + lambda x, dtype: x.astype(dtype), |
| 87 | + dtype=mlc_param.dtype, |
| 88 | + ), |
| 89 | + ) |
| 90 | + return mapping |
| 91 | + |
| 92 | + |
| 93 | +def awq(model_config: OLMoConfig, quantization: Quantization) -> ExternMapping: |
| 94 | + """Returns a parameter mapping that maps from the names of MLC LLM parameters to |
| 95 | + the names of AWQ parameters. |
| 96 | + Parameters |
| 97 | + ---------- |
| 98 | + model_config : OLMoConfig |
| 99 | + The configuration of the OLMo model. |
| 100 | +
|
| 101 | + quantization : Quantization |
| 102 | + The quantization configuration. |
| 103 | +
|
| 104 | + Returns |
| 105 | + ------- |
| 106 | + param_map : ExternMapping |
| 107 | + The parameter mapping from MLC to AWQ. |
| 108 | + """ |
| 109 | + model, _ = awq_quant(model_config, quantization) |
| 110 | + _, _named_params, _ = model.export_tvm( # type: ignore[misc] |
| 111 | + spec=model.get_default_spec(), # type: ignore[attr-defined] |
| 112 | + allow_extern=True, |
| 113 | + ) |
| 114 | + named_parameters = dict(_named_params) |
| 115 | + |
| 116 | + mapping = ExternMapping() |
| 117 | + |
| 118 | + for i in range(model_config.num_hidden_layers): |
| 119 | + # Add QKV in self attention |
| 120 | + attn = f"model.layers.{i}.self_attn" |
| 121 | + for quantize_suffix in ["qweight", "qzeros", "scales"]: |
| 122 | + mlc_name = f"{attn}.qkv_proj.{quantize_suffix}" |
| 123 | + assert mlc_name in named_parameters |
| 124 | + mlc_param = named_parameters[mlc_name] |
| 125 | + mapping.add_mapping( |
| 126 | + mlc_name, |
| 127 | + [ |
| 128 | + f"{attn}.q_proj.{quantize_suffix}", |
| 129 | + f"{attn}.k_proj.{quantize_suffix}", |
| 130 | + f"{attn}.v_proj.{quantize_suffix}", |
| 131 | + ], |
| 132 | + functools.partial( |
| 133 | + lambda q, k, v, dtype: np.concatenate( |
| 134 | + [q, k, v], |
| 135 | + axis=1, # AWQ GEMM would transpose the weight |
| 136 | + ).astype(dtype), |
| 137 | + dtype=mlc_param.dtype, |
| 138 | + ), |
| 139 | + ) |
| 140 | + |
| 141 | + # Concat gate and up in MLP |
| 142 | + mlp = f"model.layers.{i}.mlp" |
| 143 | + for quantize_suffix in ["qweight", "qzeros", "scales"]: |
| 144 | + mlc_name = f"{mlp}.gate_up_proj.{quantize_suffix}" |
| 145 | + assert mlc_name in named_parameters |
| 146 | + mlc_param = named_parameters[mlc_name] |
| 147 | + mapping.add_mapping( |
| 148 | + mlc_name, |
| 149 | + [ |
| 150 | + f"{mlp}.gate_proj.{quantize_suffix}", |
| 151 | + f"{mlp}.up_proj.{quantize_suffix}", |
| 152 | + ], |
| 153 | + functools.partial( |
| 154 | + lambda gate, up, dtype: np.concatenate( |
| 155 | + [gate, up], |
| 156 | + axis=1, # AWQ GEMM would transpose the weight |
| 157 | + ).astype(dtype), |
| 158 | + dtype=mlc_param.dtype, |
| 159 | + ), |
| 160 | + ) |
| 161 | + |
| 162 | + # inv_freq is not used in the model |
| 163 | + mapping.add_unused(f"{attn}.rotary_emb.inv_freq") |
| 164 | + |
| 165 | + for mlc_name, mlc_param in named_parameters.items(): |
| 166 | + if mlc_name not in mapping.param_map: |
| 167 | + mapping.add_mapping( |
| 168 | + mlc_name, |
| 169 | + [mlc_name], |
| 170 | + functools.partial(lambda x, dtype: x.astype(dtype), dtype=mlc_param.dtype), |
| 171 | + ) |
| 172 | + return mapping |
0 commit comments