|
| 1 | +# Copyright (c) 2025 Intel Corporation |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +# you may not use this file except in compliance with the License. |
| 4 | +# You may obtain a copy of the License at |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# Unless required by applicable law or agreed to in writing, software |
| 7 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 8 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 9 | +# See the License for the specific language governing permissions and |
| 10 | +# limitations under the License. |
| 11 | + |
| 12 | +import time |
| 13 | +from pathlib import Path |
| 14 | + |
| 15 | +import onnx |
| 16 | +from optimum.onnxruntime import ORTModelForCausalLM |
| 17 | +from transformers import AutoTokenizer |
| 18 | + |
| 19 | +import nncf |
| 20 | +from nncf.onnx.quantization.backend_parameters import BackendParameters |
| 21 | + |
| 22 | +ROOT = Path(__file__).parent.resolve() |
| 23 | + |
| 24 | + |
| 25 | +MODEL_ID = "PY007/TinyLlama-1.1B-Chat-v0.3" |
| 26 | +OUTPUT_DIR = ROOT / "tinyllama_compressed" |
| 27 | + |
| 28 | + |
| 29 | +def main(): |
| 30 | + # Export the pretrained model in ONNX format. The OUTPUT_DIR directory |
| 31 | + # will contain model.onnx, model.onnx_data, and some metadata files. |
| 32 | + model = ORTModelForCausalLM.from_pretrained(MODEL_ID, export=True) |
| 33 | + model.save_pretrained(OUTPUT_DIR) |
| 34 | + |
| 35 | + # Load the exported pretrained model as an ONNX model. For models larger than 2GB, |
| 36 | + # set `load_external_data=False` to load only the model's topology without the weights. |
| 37 | + # The weights will be loaded on the fly during compression. To enable this, specify the |
| 38 | + # `BackendParameters.EXTERNAL_DATA_DIR` parameter, which should be the absolute path to |
| 39 | + # the directory containing the model’s external data files. |
| 40 | + onnx_model = onnx.load(OUTPUT_DIR / "model.onnx", load_external_data=False) |
| 41 | + |
| 42 | + compressed_onnx_model = nncf.compress_weights( |
| 43 | + onnx_model, |
| 44 | + mode=nncf.CompressWeightsMode.INT4_SYM, |
| 45 | + ratio=0.8, |
| 46 | + advanced_parameters=nncf.AdvancedCompressionParameters( |
| 47 | + backend_params={BackendParameters.EXTERNAL_DATA_DIR: OUTPUT_DIR} |
| 48 | + ), |
| 49 | + ) |
| 50 | + |
| 51 | + # Replace the original model with the compressed model. |
| 52 | + onnx.save(compressed_onnx_model, OUTPUT_DIR / "model.onnx", save_as_external_data=True) |
| 53 | + |
| 54 | + # Infer Model. |
| 55 | + tokenizer = AutoTokenizer.from_pretrained(MODEL_ID) |
| 56 | + ort_model = ORTModelForCausalLM.from_pretrained(OUTPUT_DIR) |
| 57 | + input_ids = tokenizer("What is PyTorch?", return_tensors="pt").to(device=model.device) |
| 58 | + |
| 59 | + start_t = time.time() |
| 60 | + output = ort_model.generate(**input_ids, max_new_tokens=100) |
| 61 | + print("Elapsed time: ", time.time() - start_t) |
| 62 | + |
| 63 | + output_text = tokenizer.decode(output[0]) |
| 64 | + print(output_text) |
| 65 | + return output_text |
| 66 | + |
| 67 | + |
| 68 | +if __name__ == "__main__": |
| 69 | + main() |
0 commit comments