|
| 1 | +import shutil |
| 2 | +import tempfile |
| 3 | + |
| 4 | +import pytest |
| 5 | +import torch |
| 6 | +from transformers import AutoConfig, AutoModelForCausalLM, AutoTokenizer |
| 7 | +from transformers.models.qwen2.modeling_qwen2 import Qwen2ForCausalLM |
| 8 | + |
| 9 | +from auto_round import AutoRound |
| 10 | +from auto_round import schemes as ar_schemes |
| 11 | +from auto_round.experimental import qmodules as ar_qmodules |
| 12 | +from auto_round.export.export_to_autoround import AutoRoundFormat |
| 13 | +from auto_round.export.export_to_autoround import qlinear_fp as ar_qlinear_fp |
| 14 | +from auto_round.inference.backend import MX_TENSOR_DATA_TYPES |
| 15 | +from auto_round.testing_utils import has_module |
| 16 | + |
| 17 | +testing_scheme_name_lst = [ |
| 18 | + AutoRoundFormat.MXFP8.value, |
| 19 | + AutoRoundFormat.MXFP4.value, |
| 20 | +] |
| 21 | +QMODULE_MAPPING = { |
| 22 | + AutoRoundFormat.MXFP8.value: ar_qmodules.MXFP8QuantLinear, |
| 23 | + AutoRoundFormat.MXFP4.value: ar_qmodules.MXFP4QuantLinear, |
| 24 | +} |
| 25 | +SCHEMES_MAPPING = { |
| 26 | + AutoRoundFormat.MXFP8.value: ar_schemes.MXFP8, |
| 27 | + AutoRoundFormat.MXFP4.value: ar_schemes.MXFP4, |
| 28 | +} |
| 29 | + |
| 30 | + |
| 31 | +@pytest.mark.parametrize("scheme_name", testing_scheme_name_lst) |
| 32 | +@pytest.mark.parametrize("weight_data_type", MX_TENSOR_DATA_TYPES) |
| 33 | +@pytest.mark.parametrize("act_data_type", MX_TENSOR_DATA_TYPES) |
| 34 | +@torch.inference_mode() |
| 35 | +def test_e2e_quant_and_load(scheme_name, weight_data_type, act_data_type): |
| 36 | + # Use a temporary directory for saving the quantized model |
| 37 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 38 | + model_name = "/tf_dataset/auto_round/models/Qwen/Qwen2.5-0.5B-Instruct" |
| 39 | + config = AutoConfig.from_pretrained(model_name) |
| 40 | + config.num_hidden_layers = 2 # Use a smaller model for testing |
| 41 | + |
| 42 | + # Load the tokenizer and model |
| 43 | + tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True) |
| 44 | + model = Qwen2ForCausalLM(config) |
| 45 | + scheme = SCHEMES_MAPPING[scheme_name] |
| 46 | + scheme.data_type = weight_data_type |
| 47 | + scheme.act_data_type = act_data_type |
| 48 | + # Initialize AutoRound for quantization |
| 49 | + autoround = AutoRound( |
| 50 | + model, |
| 51 | + tokenizer, |
| 52 | + scheme=scheme, |
| 53 | + iters=0, |
| 54 | + nsamples=2, |
| 55 | + ) |
| 56 | + |
| 57 | + # Quantize and save the model to the temporary directory |
| 58 | + quantized_model_path = f"{temp_dir}/tmp_autoround" |
| 59 | + autoround.quantize_and_save(format="auto_round", output_dir=quantized_model_path) |
| 60 | + |
| 61 | + # Perform inference with the quantized model |
| 62 | + model = AutoModelForCausalLM.from_pretrained( |
| 63 | + quantized_model_path, |
| 64 | + torch_dtype="auto", |
| 65 | + ) |
| 66 | + model.eval() |
| 67 | + assert has_module( |
| 68 | + model, QMODULE_MAPPING[scheme_name] |
| 69 | + ), f"Expected {QMODULE_MAPPING[scheme_name].__name__} in the model." |
0 commit comments