|
| 1 | +# Copyright (c) 2025 Intel Corporation |
| 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 argparse |
| 16 | + |
| 17 | +import torch |
| 18 | + |
| 19 | +torch.use_deterministic_algorithms(True, warn_only=True) |
| 20 | +from transformers import AutoTokenizer, Llama4ForConditionalGeneration, AutoProcessor |
| 21 | +from neural_compressor.torch.quantization import ( |
| 22 | + AutoRoundConfig, |
| 23 | + convert, |
| 24 | + prepare, |
| 25 | +) |
| 26 | + |
| 27 | + |
| 28 | +class BasicArgumentParser(argparse.ArgumentParser): |
| 29 | + def __init__(self, *args, **kwargs): |
| 30 | + super().__init__(*args, **kwargs) |
| 31 | + self.add_argument("--model", "--model_name", "--model_name_or_path", |
| 32 | + help="model name or path") |
| 33 | + |
| 34 | + self.add_argument('--scheme', default="MXFP4", type=str, |
| 35 | + help="quantizaion scheme.") |
| 36 | + |
| 37 | + self.add_argument("--device", "--devices", default="auto", type=str, |
| 38 | + help="the device to be used for tuning. The default is set to auto," |
| 39 | + "allowing for automatic detection." |
| 40 | + "Currently, device settings support CPU, GPU, and HPU.") |
| 41 | + |
| 42 | + self.add_argument("--export_format", default="llm_compressor", type=str, |
| 43 | + help="the format to save the model" |
| 44 | + ) |
| 45 | + |
| 46 | + self.add_argument("--output_dir", default="./tmp_autoround", type=str, |
| 47 | + help="the directory to save quantized model") |
| 48 | + |
| 49 | + self.add_argument("--fp_layers", default="", type=str, |
| 50 | + help="layers to maintain original data type") |
| 51 | + |
| 52 | + |
| 53 | +def setup_parser(): |
| 54 | + parser = BasicArgumentParser() |
| 55 | + |
| 56 | + parser.add_argument("--iters", "--iter", default=0, type=int, |
| 57 | + help=" iters") |
| 58 | + |
| 59 | + args = parser.parse_args() |
| 60 | + return args |
| 61 | + |
| 62 | + |
| 63 | +def tune(args): |
| 64 | + model_name = args.model |
| 65 | + if model_name[-1] == "/": |
| 66 | + model_name = model_name[:-1] |
| 67 | + print(f"start to quantize {model_name}") |
| 68 | + |
| 69 | + layer_config = {} |
| 70 | + model = Llama4ForConditionalGeneration.from_pretrained(args.model, device_map=None, torch_dtype="auto", trust_remote_code=True) |
| 71 | + tokenizer = AutoTokenizer.from_pretrained(args.model, trust_remote_code=True) |
| 72 | + processor = AutoProcessor.from_pretrained(args.model, trust_remote_code=True) |
| 73 | + fp_layers = args.fp_layers.replace(" ", "").split(",") |
| 74 | + if len(fp_layers) > 0: |
| 75 | + for n, m in model.named_modules(): |
| 76 | + if not isinstance(m, (torch.nn.Linear)): |
| 77 | + continue |
| 78 | + for name in fp_layers: |
| 79 | + if name in n: |
| 80 | + layer_config[n] = {"bits": 16, "act_bits": 16} |
| 81 | + break |
| 82 | + |
| 83 | + qconfig = AutoRoundConfig( |
| 84 | + tokenizer=tokenizer, |
| 85 | + iters=args.iters, |
| 86 | + scheme=args.scheme, |
| 87 | + layer_config=layer_config, |
| 88 | + export_format="llm_compressor", |
| 89 | + output_dir=args.output_dir, |
| 90 | + processor=processor, |
| 91 | + ) |
| 92 | + model = prepare(model, qconfig) |
| 93 | + model = convert(model, qconfig) |
| 94 | + |
| 95 | +if __name__ == '__main__': |
| 96 | + args = setup_parser() |
| 97 | + tune(args) |
0 commit comments