|
| 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 os |
| 17 | +import subprocess |
| 18 | +import sys |
| 19 | +import tempfile |
| 20 | +import unittest |
| 21 | + |
| 22 | +import pytest |
| 23 | +import torch |
| 24 | +from transformers.testing_utils import slow |
| 25 | + |
| 26 | +from optimum.executorch import ExecuTorchModelForImageClassification |
| 27 | + |
| 28 | +from ..utils import check_close_recursively |
| 29 | + |
| 30 | + |
| 31 | +is_not_macos = sys.platform != "darwin" |
| 32 | + |
| 33 | + |
| 34 | +class ExecuTorchModelIntegrationTest(unittest.TestCase): |
| 35 | + def __init__(self, *args, **kwargs): |
| 36 | + super().__init__(*args, **kwargs) |
| 37 | + |
| 38 | + @slow |
| 39 | + @pytest.mark.run_slow |
| 40 | + def test_vit_export_to_executorch(self): |
| 41 | + model_id = "microsoft/resnet-50" |
| 42 | + task = "image-classification" |
| 43 | + recipe = "xnnpack" |
| 44 | + with tempfile.TemporaryDirectory() as tempdir: |
| 45 | + subprocess.run( |
| 46 | + f"optimum-cli export executorch --model {model_id} --task {task} --recipe {recipe} --output_dir {tempdir}/executorch", |
| 47 | + shell=True, |
| 48 | + check=True, |
| 49 | + ) |
| 50 | + self.assertTrue(os.path.exists(f"{tempdir}/executorch/model.pte")) |
| 51 | + |
| 52 | + @slow |
| 53 | + @pytest.mark.run_slow |
| 54 | + @pytest.mark.skipif(is_not_macos, reason="Only runs on MacOS") |
| 55 | + def test_vit_image_classification_coreml_fp32_cpu(self): |
| 56 | + model_id = "microsoft/resnet-50" |
| 57 | + |
| 58 | + batch_size = 1 |
| 59 | + num_channels = 3 |
| 60 | + height = 224 |
| 61 | + width = 224 |
| 62 | + pixel_values = torch.rand(batch_size, num_channels, height, width) |
| 63 | + |
| 64 | + # Test fetching and lowering the model to ExecuTorch |
| 65 | + import coremltools as ct |
| 66 | + |
| 67 | + et_model = ExecuTorchModelForImageClassification.from_pretrained( |
| 68 | + model_id=model_id, |
| 69 | + recipe="coreml", |
| 70 | + recipe_kwargs={"compute_precision": ct.precision.FLOAT32, "compute_units": ct.ComputeUnit.CPU_ONLY}, |
| 71 | + ) |
| 72 | + et_output = et_model.forward(pixel_values) |
| 73 | + |
| 74 | + # Reference (using XNNPACK as reference because eager model currently segfaults in a PyTorch kernel) |
| 75 | + et_xnnpack = ExecuTorchModelForImageClassification.from_pretrained( |
| 76 | + model_id=model_id, |
| 77 | + recipe="xnnpack", |
| 78 | + ) |
| 79 | + et_xnnpack_output = et_xnnpack.forward(pixel_values) |
| 80 | + |
| 81 | + # Compare with reference |
| 82 | + self.assertTrue(check_close_recursively(et_output, et_xnnpack_output)) |
0 commit comments