|
| 1 | +# Copyright The FMS Model Optimizer Authors |
| 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 | +"""Implement and register FMS adapters for FP8 checkpoint loading.""" |
| 15 | + |
| 16 | +# Standard |
| 17 | +from typing import Any, Mapping |
| 18 | +import functools |
| 19 | + |
| 20 | +# Local |
| 21 | +from fms_mo.prep import available_packages |
| 22 | + |
| 23 | +if available_packages["fms"]: |
| 24 | + # Third Party |
| 25 | + from fms.modules.linear import get_linear_type |
| 26 | + from fms.utils import serialization |
| 27 | + from fms.utils.config import ModelConfig |
| 28 | + |
| 29 | + # pylint: disable=unused-argument |
| 30 | + # Retaining kwargs input arguments for consistency with other adapter steps. |
| 31 | + # TODO: may be shared with gptq llama |
| 32 | + def _hf_fp8_check( |
| 33 | + input_sd: Mapping[str, Any], |
| 34 | + model_config: ModelConfig | None = None, |
| 35 | + checkpoint_is_fused: bool = False, |
| 36 | + **kwargs, |
| 37 | + ) -> Mapping[str, Any]: |
| 38 | + """Implementation of adapter step for FMS: ensure that when FP8 quantization |
| 39 | + is in use, weights are fused like the model checkpoint. |
| 40 | + """ |
| 41 | + |
| 42 | + has_fused_weights = True |
| 43 | + linear_type = "torch_linear" |
| 44 | + if model_config: |
| 45 | + if not model_config.fused_weights: |
| 46 | + has_fused_weights = False |
| 47 | + if model_config.linear_config: |
| 48 | + linear_type = model_config.linear_config["linear_type"] |
| 49 | + if callable(linear_type): |
| 50 | + # Calling this function with "any" guarantees "fp8" to be returned |
| 51 | + # when loading an HF fp8 checkpoint, and never in any other condition |
| 52 | + linear_type = get_linear_type(model_config.linear_config, "any") |
| 53 | + |
| 54 | + if "fp8" in linear_type and has_fused_weights != checkpoint_is_fused: |
| 55 | + raise ValueError( |
| 56 | + "FP8 HF llama checkpoints cannot be loaded into a model with fused weights" |
| 57 | + ) |
| 58 | + |
| 59 | + return input_sd |
| 60 | + |
| 61 | + serialization.register_adapter_step( |
| 62 | + "llama", |
| 63 | + "hf_fp8_check", |
| 64 | + functools.partial(_hf_fp8_check, checkpoint_is_fused=False), |
| 65 | + ) |
| 66 | + serialization.extend_adapter("llama", "hf", ["hf_fp8_check"]) |
| 67 | + |
| 68 | + serialization.register_adapter_step( |
| 69 | + "granite", |
| 70 | + "hf_fp8_check", |
| 71 | + functools.partial(_hf_fp8_check, checkpoint_is_fused=False), |
| 72 | + ) |
| 73 | + serialization.extend_adapter("granite", "hf", ["hf_fp8_check"]) |
0 commit comments