|
| 1 | +# coding=utf-8 |
| 2 | +# Copyright 2024 The HuggingFace Team. All rights reserved. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +import gc |
| 17 | +import logging |
| 18 | +import os |
| 19 | +import unittest |
| 20 | + |
| 21 | +import pytest |
| 22 | +import torchao |
| 23 | +from executorch.extension.pybindings.portable_lib import ExecuTorchModule |
| 24 | +from packaging.version import parse |
| 25 | +from transformers import AutoConfig, AutoTokenizer |
| 26 | +from transformers.testing_utils import slow |
| 27 | + |
| 28 | +from optimum.executorch import ExecuTorchModelForCausalLM |
| 29 | + |
| 30 | +from ..utils import check_causal_lm_output_quality |
| 31 | + |
| 32 | + |
| 33 | +os.environ["TOKENIZERS_PARALLELISM"] = "false" |
| 34 | + |
| 35 | + |
| 36 | +class ExecuTorchModelIntegrationTest(unittest.TestCase): |
| 37 | + def __init__(self, *args, **kwargs): |
| 38 | + super().__init__(*args, **kwargs) |
| 39 | + |
| 40 | + @slow |
| 41 | + @pytest.mark.run_slow |
| 42 | + @pytest.mark.skipif( |
| 43 | + parse(torchao.__version__) < parse("0.11.0"), |
| 44 | + reason="Quantization is only available on torchao >= 0.11.0.", |
| 45 | + ) |
| 46 | + def test_codegen_text_generation_with_8da4w_8we(self): |
| 47 | + model_id = "Salesforce/codegen-350M-mono" |
| 48 | + prompt = "def hello_world():" |
| 49 | + tokenizer = AutoTokenizer.from_pretrained(model_id) |
| 50 | + config = AutoConfig.from_pretrained(model_id) |
| 51 | + config.bos_token_id = tokenizer.bos_token_id |
| 52 | + config.eos_token_id = tokenizer.eos_token_id |
| 53 | + model = ExecuTorchModelForCausalLM.from_pretrained( |
| 54 | + model_id, |
| 55 | + config=config, |
| 56 | + recipe="xnnpack", |
| 57 | + **{"qlinear": True, "qembeeding": True}, |
| 58 | + ) |
| 59 | + self.assertIsInstance(model, ExecuTorchModelForCausalLM) |
| 60 | + self.assertIsInstance(model.model, ExecuTorchModule) |
| 61 | + generated_text = model.text_generation( |
| 62 | + tokenizer=tokenizer, |
| 63 | + prompt=prompt, |
| 64 | + max_seq_len=64, |
| 65 | + ) |
| 66 | + logging.info(f"\nGenerated text:\n\t{generated_text}") |
| 67 | + generated_tokens = tokenizer(generated_text, return_tensors="pt").input_ids |
| 68 | + |
| 69 | + # Free memory before loading eager for quality check |
| 70 | + del model |
| 71 | + del tokenizer |
| 72 | + gc.collect() |
| 73 | + |
| 74 | + self.assertTrue(check_causal_lm_output_quality(model_id, generated_tokens)) |
0 commit comments