Link: https://code.alibaba-inc.com/Ali-MaaS/MaaS-lib/codereview/10491891master
| @@ -22,7 +22,7 @@ class TestExportSbertSequenceClassification(unittest.TestCase): | |||
| shutil.rmtree(self.tmp_dir) | |||
| super().tearDown() | |||
| @unittest.skipUnless(test_level() >= 1, 'skip test in current test level') | |||
| @unittest.skip | |||
| def test_export_sbert_sequence_classification(self): | |||
| model = Model.from_pretrained(self.model_id) | |||
| print( | |||
| @@ -71,7 +71,7 @@ class MsDatasetTest(unittest.TestCase): | |||
| @unittest.skipUnless(test_level() >= 1, 'skip test in current test level') | |||
| @require_torch | |||
| def test_to_torch_dataset_text(self): | |||
| model_id = 'damo/bert-base-sst2' | |||
| model_id = 'damo/nlp_structbert_sentence-similarity_chinese-tiny' | |||
| nlp_model = Model.from_pretrained(model_id) | |||
| preprocessor = SequenceClassificationPreprocessor( | |||
| nlp_model.model_dir, | |||
| @@ -93,7 +93,7 @@ class MsDatasetTest(unittest.TestCase): | |||
| def test_to_tf_dataset_text(self): | |||
| import tensorflow as tf | |||
| tf.compat.v1.enable_eager_execution() | |||
| model_id = 'damo/bert-base-sst2' | |||
| model_id = 'damo/nlp_structbert_sentence-similarity_chinese-tiny' | |||
| nlp_model = Model.from_pretrained(model_id) | |||
| preprocessor = SequenceClassificationPreprocessor( | |||
| nlp_model.model_dir, | |||
| @@ -17,12 +17,12 @@ class TextGPT3GenerationTest(unittest.TestCase): | |||
| self.model_dir_13B = snapshot_download(self.model_id_13B) | |||
| self.input = '好的' | |||
| @unittest.skipUnless(test_level() >= 2, 'skip test in current test level') | |||
| @unittest.skip('distributed gpt3 1.3B, skipped') | |||
| def test_gpt3_1_3B(self): | |||
| pipe = pipeline(Tasks.text_generation, model=self.model_id_1_3B) | |||
| print(pipe(self.input)) | |||
| @unittest.skipUnless(test_level() >= 2, 'skip test in current test level') | |||
| @unittest.skip('distributed gpt3 2.7B, skipped') | |||
| def test_gpt3_2_7B(self): | |||
| pipe = pipeline(Tasks.text_generation, model=self.model_id_2_7B) | |||
| print(pipe(self.input)) | |||
| @@ -1,100 +0,0 @@ | |||
| # Copyright (c) Alibaba, Inc. and its affiliates. | |||
| import unittest | |||
| from modelscope.models import Model | |||
| from modelscope.msdatasets import MsDataset | |||
| from modelscope.pipelines import pipeline | |||
| from modelscope.pipelines.nlp import SequenceClassificationPipeline | |||
| from modelscope.preprocessors import SequenceClassificationPreprocessor | |||
| from modelscope.utils.constant import Tasks | |||
| from modelscope.utils.demo_utils import DemoCompatibilityCheck | |||
| from modelscope.utils.test_utils import test_level | |||
| class SequenceClassificationTest(unittest.TestCase, DemoCompatibilityCheck): | |||
| sentence1 = 'i like this wonderful place' | |||
| def setUp(self) -> None: | |||
| self.model_id = 'damo/bert-base-sst2' | |||
| self.task = Tasks.text_classification | |||
| def predict(self, pipeline_ins: SequenceClassificationPipeline): | |||
| from easynlp.appzoo import load_dataset | |||
| set = load_dataset('glue', 'sst2') | |||
| data = set['test']['sentence'][:3] | |||
| results = pipeline_ins(data[0]) | |||
| print(results) | |||
| results = pipeline_ins(data[1]) | |||
| print(results) | |||
| print(data) | |||
| def printDataset(self, dataset: MsDataset): | |||
| for i, r in enumerate(dataset): | |||
| if i > 10: | |||
| break | |||
| print(r) | |||
| # @unittest.skipUnless(test_level() >= 0, 'skip test in current test level') | |||
| @unittest.skip('nlp model does not support tensor input, skipped') | |||
| def test_run_with_model_from_modelhub(self): | |||
| model = Model.from_pretrained(self.model_id) | |||
| preprocessor = SequenceClassificationPreprocessor( | |||
| model.model_dir, first_sequence='sentence', second_sequence=None) | |||
| pipeline_ins = pipeline( | |||
| task=Tasks.text_classification, | |||
| model=model, | |||
| preprocessor=preprocessor) | |||
| print(f'sentence1: {self.sentence1}\n' | |||
| f'pipeline1:{pipeline_ins(input=self.sentence1)}') | |||
| # @unittest.skipUnless(test_level() >= 0, 'skip test in current test level') | |||
| @unittest.skip('nlp model does not support tensor input, skipped') | |||
| def test_run_with_model_name(self): | |||
| text_classification = pipeline( | |||
| task=Tasks.text_classification, model=self.model_id) | |||
| result = text_classification( | |||
| MsDataset.load( | |||
| 'xcopa', | |||
| subset_name='translation-et', | |||
| namespace='damotest', | |||
| split='test', | |||
| target='premise')) | |||
| self.printDataset(result) | |||
| # @unittest.skipUnless(test_level() >= 2, 'skip test in current test level') | |||
| @unittest.skip('nlp model does not support tensor input, skipped') | |||
| def test_run_with_default_model(self): | |||
| text_classification = pipeline(task=Tasks.text_classification) | |||
| result = text_classification( | |||
| MsDataset.load( | |||
| 'xcopa', | |||
| subset_name='translation-et', | |||
| namespace='damotest', | |||
| split='test', | |||
| target='premise')) | |||
| self.printDataset(result) | |||
| # @unittest.skipUnless(test_level() >= 1, 'skip test in current test level') | |||
| @unittest.skip('nlp model does not support tensor input, skipped') | |||
| def test_run_with_modelscope_dataset(self): | |||
| text_classification = pipeline(task=Tasks.text_classification) | |||
| # loaded from modelscope dataset | |||
| dataset = MsDataset.load( | |||
| 'xcopa', | |||
| subset_name='translation-et', | |||
| namespace='damotest', | |||
| split='test', | |||
| target='premise') | |||
| result = text_classification(dataset) | |||
| self.printDataset(result) | |||
| @unittest.skip('demo compatibility test is only enabled on a needed-basis') | |||
| def test_demo_compatibility(self): | |||
| self.compatibility_check() | |||
| if __name__ == '__main__': | |||
| unittest.main() | |||
| @@ -38,7 +38,8 @@ class TestFinetuneSequenceClassification(unittest.TestCase): | |||
| shutil.rmtree(self.tmp_dir) | |||
| super().tearDown() | |||
| @unittest.skipUnless(test_level() >= 1, 'skip test in current test level') | |||
| @unittest.skip( | |||
| 'Skip testing trainer repeatable, because it\'s unstable in daily UT') | |||
| def test_trainer_repeatable(self): | |||
| import torch # noqa | |||
| @@ -169,11 +169,25 @@ class TestTrainerWithNlp(unittest.TestCase): | |||
| cfg.preprocessor.label = 'label' | |||
| cfg.preprocessor.train['label2id'] = {'0': 0, '1': 1} | |||
| cfg.preprocessor.val['label2id'] = {'0': 0, '1': 1} | |||
| cfg.train.dataloader.batch_size_per_gpu = 2 | |||
| cfg.train.hooks = [{ | |||
| 'type': 'CheckpointHook', | |||
| 'interval': 3, | |||
| 'by_epoch': False, | |||
| }, { | |||
| 'type': 'TextLoggerHook', | |||
| 'interval': 1 | |||
| }, { | |||
| 'type': 'IterTimerHook' | |||
| }, { | |||
| 'type': 'EvaluationHook', | |||
| 'interval': 1 | |||
| }] | |||
| cfg.train.work_dir = self.tmp_dir | |||
| cfg_file = os.path.join(self.tmp_dir, 'config.json') | |||
| cfg.dump(cfg_file) | |||
| dataset = MsDataset.load('clue', subset_name='afqmc', split='train') | |||
| dataset = dataset.to_hf_dataset().select(range(128)) | |||
| dataset = dataset.to_hf_dataset().select(range(4)) | |||
| kwargs = dict( | |||
| model=model_id, | |||
| train_dataset=dataset, | |||
| @@ -190,7 +204,7 @@ class TestTrainerWithNlp(unittest.TestCase): | |||
| PRIORITY = Priority.VERY_LOW | |||
| def after_iter(self, trainer): | |||
| if trainer.iter == 12: | |||
| if trainer.iter == 3: | |||
| raise MsRegressTool.EarlyStopError('Test finished.') | |||
| if 'EarlyStopHook' not in [ | |||
| @@ -207,12 +221,11 @@ class TestTrainerWithNlp(unittest.TestCase): | |||
| results_files = os.listdir(self.tmp_dir) | |||
| self.assertIn(f'{trainer.timestamp}.log.json', results_files) | |||
| trainer = build_trainer(default_args=kwargs) | |||
| regress_tool = MsRegressTool(baseline=False) | |||
| with regress_tool.monitor_ms_train( | |||
| trainer, 'trainer_continue_train', level='strict'): | |||
| trainer.train(os.path.join(self.tmp_dir, 'iter_12.pth')) | |||
| trainer.train(os.path.join(self.tmp_dir, 'iter_3.pth')) | |||
| @unittest.skipUnless(test_level() >= 2, 'skip test in current test level') | |||
| def test_trainer_with_model_and_args(self): | |||