|
| 1 | +# Copyright 2025 Tencent Inc. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import re |
| 16 | + |
| 17 | +import torch.nn as nn |
| 18 | + |
| 19 | +from ...compressor.quant.core import PTQSaveVllmHF |
| 20 | +from ...utils.utils import find_layers |
| 21 | +from ..base_model import BaseLLMModel |
| 22 | +from ..model_factory import SlimModelFactory |
| 23 | + |
| 24 | + |
| 25 | +@SlimModelFactory.register |
| 26 | +class GLM(BaseLLMModel): |
| 27 | + def __init__( |
| 28 | + self, |
| 29 | + model=None, |
| 30 | + deploy_backend="vllm", |
| 31 | + ): |
| 32 | + super().__init__( |
| 33 | + model=model, |
| 34 | + deploy_backend=deploy_backend, |
| 35 | + ) |
| 36 | + self.block_name = "model.layers" |
| 37 | + |
| 38 | + def get_observer_layers(self): |
| 39 | + names = [ |
| 40 | + "k_proj", |
| 41 | + "v_proj", |
| 42 | + "q_proj", |
| 43 | + "o_proj", |
| 44 | + "up_proj", |
| 45 | + "gate_proj", |
| 46 | + "down_proj", |
| 47 | + ] |
| 48 | + obs_layers = [nn.Linear] |
| 49 | + observer_layers_dict = {} |
| 50 | + layers_dict = find_layers(self.model, layers=obs_layers) |
| 51 | + |
| 52 | + ignore_layers = self.skip_layer_names() |
| 53 | + for name, module in layers_dict.items(): |
| 54 | + if name.startswith(self.block_name) and name.split(".")[-1] in names: |
| 55 | + observer_layers_dict[name] = module |
| 56 | + else: |
| 57 | + ignore_layers.append(name) |
| 58 | + ignore_layers = sorted(list(set(ignore_layers))) |
| 59 | + self.quant_config.quant_algo_info["ignore_layers"] = ignore_layers |
| 60 | + |
| 61 | + if self.quant_config.custom_observe_layers_names != "default": |
| 62 | + for custom_observe_name in self.quant_config.custom_observe_layers_names: |
| 63 | + for default_name in observer_layers_dict.keys(): |
| 64 | + if custom_observe_name not in default_name: |
| 65 | + observer_layers_dict.pop(default_name) |
| 66 | + return observer_layers_dict |
| 67 | + |
| 68 | + def get_smooth_mapping_layers(self, smooth_config, mappings=None): |
| 69 | + if mappings is None: |
| 70 | + mappings = [ |
| 71 | + (["q_proj", "k_proj", "v_proj"], "input_layernorm"), |
| 72 | + (["gate_proj", "up_proj"], "post_attention_layernorm"), |
| 73 | + ] |
| 74 | + print(f"smooth mappings={mappings}") |
| 75 | + assert len(mappings) == 2 |
| 76 | + assert smooth_config.smooth_first_linears or smooth_config.smooth_last_linears |
| 77 | + return super().get_smooth_mapping_layers(smooth_config, mappings) |
| 78 | + |
| 79 | + def get_parent_dict(self, observer_layers_dict): |
| 80 | + parent_mapping = {r"experts\.\d+": "experts"} |
| 81 | + parent_dict = {} |
| 82 | + for layer_name in observer_layers_dict.keys(): |
| 83 | + parent_name = layer_name |
| 84 | + for k, v in parent_mapping.items(): |
| 85 | + parent_name = re.sub(k, v, layer_name) |
| 86 | + if parent_name != layer_name: |
| 87 | + parent_dict[layer_name] = parent_name |
| 88 | + return parent_dict |
| 89 | + |
| 90 | + def get_save_func(self): |
| 91 | + if self.deploy_backend in ["vllm", "huggingface"]: |
| 92 | + return PTQSaveVllmHF |
| 93 | + else: |
| 94 | + raise NotImplementedError( |
| 95 | + f"deploy_backend {self.deploy_backend} is not supported for saving." |
| 96 | + ) |
| 97 | + |
| 98 | + def fuse_observer_amax(self, sub_layer, name): |
| 99 | + if "q_proj" in name or "k_proj" in name or "v_proj" in name: |
| 100 | + prefix = name.rsplit(".", 1)[0] |
| 101 | + q_name = f"{prefix}.q_proj" |
| 102 | + k_name = f"{prefix}.k_proj" |
| 103 | + v_name = f"{prefix}.v_proj" |
| 104 | + |
| 105 | + weight_scales = [] |
| 106 | + for key in [q_name, k_name, v_name]: |
| 107 | + tensor = self.weight_observer_amax_dict[key] |
| 108 | + weight_scales.append(tensor) |
| 109 | + weight_observer_amax = max(weight_scales) |
| 110 | + |
| 111 | + act_scales = [] |
| 112 | + for key in [q_name, k_name, v_name]: |
| 113 | + tensor = self.input_observer_amax_dict[key] |
| 114 | + act_scales.append(tensor) |
| 115 | + input_observer_amax = max(act_scales) |
| 116 | + elif "gate_proj" in name or "up_proj" in name: |
| 117 | + prefix = name.rsplit(".", 1)[0] |
| 118 | + gate_name = f"{prefix}.gate_proj" |
| 119 | + up_name = f"{prefix}.up_proj" |
| 120 | + |
| 121 | + weight_scales = [] |
| 122 | + for key in [gate_name, up_name]: |
| 123 | + tensor = self.weight_observer_amax_dict[key] |
| 124 | + weight_scales.append(tensor) |
| 125 | + weight_observer_amax = max(weight_scales) |
| 126 | + |
| 127 | + act_scales = [] |
| 128 | + for key in [gate_name, up_name]: |
| 129 | + tensor = self.input_observer_amax_dict[key] |
| 130 | + act_scales.append(tensor) |
| 131 | + input_observer_amax = max(act_scales) |
| 132 | + else: |
| 133 | + weight_observer_amax = self.weight_observer_amax_dict[name] |
| 134 | + input_observer_amax = self.input_observer_amax_dict[name] |
| 135 | + |
| 136 | + return weight_observer_amax, input_observer_amax |
0 commit comments