|
| 1 | +# Copyright (c) Samsung Electronics Co. LTD |
| 2 | +# All rights reserved |
| 3 | +# |
| 4 | +# Licensed under the BSD License (the "License"); you may not use this file |
| 5 | +# except in compliance with the License. See the license file in the root |
| 6 | +# directory of this source tree for more details. |
| 7 | + |
| 8 | +import os |
| 9 | +import unittest |
| 10 | + |
| 11 | +from executorch.backends.samsung.serialization.compile_options import ( |
| 12 | + gen_samsung_backend_compile_spec, |
| 13 | +) |
| 14 | +from executorch.backends.samsung.test.tester import SamsungTester |
| 15 | +from executorch.backends.samsung.test.utils.utils import TestConfig |
| 16 | + |
| 17 | +from executorch.examples.samsung.scripts.mobilebert_finetune import MobileBertFinetune |
| 18 | +from transformers import AutoTokenizer |
| 19 | + |
| 20 | + |
| 21 | +def patch_mobilebert_finetuning(model_cache_dir: str): |
| 22 | + assert os.path.isdir( |
| 23 | + model_cache_dir |
| 24 | + ), "Can not found model cache dirrecory for mobilebert finetuning" |
| 25 | + |
| 26 | + def _monkeypatch_load_tokenizer(self): |
| 27 | + tokenizer = AutoTokenizer.from_pretrained(model_cache_dir) |
| 28 | + return tokenizer |
| 29 | + |
| 30 | + old_func = MobileBertFinetune.load_tokenizer |
| 31 | + MobileBertFinetune.load_tokenizer = _monkeypatch_load_tokenizer |
| 32 | + return old_func |
| 33 | + |
| 34 | + |
| 35 | +def recover_mobilebert_finetuning(old_func): |
| 36 | + MobileBertFinetune.load_tokenizer = old_func |
| 37 | + |
| 38 | + |
| 39 | +class Test_Milestone_MobileBertFinetune(unittest.TestCase): |
| 40 | + @classmethod |
| 41 | + def setUpClass(cls): |
| 42 | + assert (model_cache_dir := os.getenv("MODEL_CACHE")), "MODEL_CACHE not set!" |
| 43 | + cls.model_cache_dir = os.path.join(model_cache_dir, "mobilebert") |
| 44 | + cls._old_func = patch_mobilebert_finetuning(cls.model_cache_dir) |
| 45 | + |
| 46 | + @classmethod |
| 47 | + def tearDownClass(cls): |
| 48 | + recover_mobilebert_finetuning(cls._old_func) |
| 49 | + |
| 50 | + # This model need to be fixed according new transformer version |
| 51 | + @unittest.skip |
| 52 | + def test_mobilebert_finetuning_fp16(self): |
| 53 | + mobilebert_finetune = MobileBertFinetune() |
| 54 | + model, _ = mobilebert_finetune.get_finetune_mobilebert(self.model_cache_dir) |
| 55 | + example_input = mobilebert_finetune.get_example_inputs() |
| 56 | + tester = SamsungTester( |
| 57 | + model, example_input, [gen_samsung_backend_compile_spec(TestConfig.chipset)] |
| 58 | + ) |
| 59 | + |
| 60 | + ( |
| 61 | + tester.export() |
| 62 | + .to_edge_transform_and_lower() |
| 63 | + .to_executorch() |
| 64 | + .run_method_and_compare_outputs(inputs=example_input, atol=0.008) |
| 65 | + ) |
0 commit comments