You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_text_ranking.py 2.8 kB

[to #42322933] NLP 1030 Refactor Features: 1. Refactor the directory structure of nlp models. All model files are placed into either the model folder or the task_model folder 2. Refactor all the comments to google style 3. Add detail comments to important tasks and nlp models, to list the description of the model, and its preprocessor&trainer 4. Model Exporting now supports a direct all to TorchModelExporter(no need to derive from it) 5. Refactor model save_pretrained method to support direct running(independent from trainer) 6. Remove the judgement of Model in the pipeline base class, to support outer register models running in our pipelines 7. Nlp trainer now has a NLPTrainingArguments class , user can pass arguments into the dataclass, and use it as a normal cfg_modify_fn, to simplify the operation of modify cfg. 8. Merge the BACKBONES and the MODELS, so user can get a backbone with the Model.from_pretrained call 9. Model.from_pretrained now support a task argument, so user can use a backbone and load it with a specific task class. 10. Support Preprocessor.from_pretrained method 11. Add standard return classes to important nlp tasks, so some of the pipelines and the models are independent now, the return values of the models will always be tensors, and the pipelines will take care of the conversion to numpy and the following stuffs. 12. Split the file of the nlp preprocessors, to make the dir structure more clear. Bugs Fixing: 1. Fix a bug that lr_scheduler can be called earlier than the optimizer's step 2. Fix a bug that the direct call of Pipelines (not from pipeline(xxx)) throws error 3. Fix a bug that the trainer will not call the correct TaskDataset class 4. Fix a bug that the internal loading of dataset will throws error in the trainer class Link: https://code.alibaba-inc.com/Ali-MaaS/MaaS-lib/codereview/10490585
2 years ago
[to #42322933] NLP 1030 Refactor Features: 1. Refactor the directory structure of nlp models. All model files are placed into either the model folder or the task_model folder 2. Refactor all the comments to google style 3. Add detail comments to important tasks and nlp models, to list the description of the model, and its preprocessor&trainer 4. Model Exporting now supports a direct all to TorchModelExporter(no need to derive from it) 5. Refactor model save_pretrained method to support direct running(independent from trainer) 6. Remove the judgement of Model in the pipeline base class, to support outer register models running in our pipelines 7. Nlp trainer now has a NLPTrainingArguments class , user can pass arguments into the dataclass, and use it as a normal cfg_modify_fn, to simplify the operation of modify cfg. 8. Merge the BACKBONES and the MODELS, so user can get a backbone with the Model.from_pretrained call 9. Model.from_pretrained now support a task argument, so user can use a backbone and load it with a specific task class. 10. Support Preprocessor.from_pretrained method 11. Add standard return classes to important nlp tasks, so some of the pipelines and the models are independent now, the return values of the models will always be tensors, and the pipelines will take care of the conversion to numpy and the following stuffs. 12. Split the file of the nlp preprocessors, to make the dir structure more clear. Bugs Fixing: 1. Fix a bug that lr_scheduler can be called earlier than the optimizer's step 2. Fix a bug that the direct call of Pipelines (not from pipeline(xxx)) throws error 3. Fix a bug that the trainer will not call the correct TaskDataset class 4. Fix a bug that the internal loading of dataset will throws error in the trainer class Link: https://code.alibaba-inc.com/Ali-MaaS/MaaS-lib/codereview/10490585
2 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # Copyright (c) Alibaba, Inc. and its affiliates.
  2. import shutil
  3. import unittest
  4. from modelscope.hub.snapshot_download import snapshot_download
  5. from modelscope.models import Model
  6. from modelscope.models.nlp import BertForTextRanking
  7. from modelscope.pipelines import pipeline
  8. from modelscope.pipelines.nlp import TextRankingPipeline
  9. from modelscope.preprocessors import TextRankingPreprocessor
  10. from modelscope.utils.constant import Tasks
  11. from modelscope.utils.test_utils import test_level
  12. class TextRankingTest(unittest.TestCase):
  13. models = [
  14. 'damo/nlp_corom_passage-ranking_english-base',
  15. 'damo/nlp_rom_passage-ranking_chinese-base'
  16. ]
  17. inputs = {
  18. 'source_sentence': ["how long it take to get a master's degree"],
  19. 'sentences_to_compare': [
  20. "On average, students take about 18 to 24 months to complete a master's degree.",
  21. 'On the other hand, some students prefer to go at a slower pace and choose to take '
  22. 'several years to complete their studies.',
  23. 'It can take anywhere from two semesters'
  24. ]
  25. }
  26. @unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
  27. def test_run_by_direct_model_download(self):
  28. for model_id in self.models:
  29. cache_path = snapshot_download(model_id)
  30. tokenizer = TextRankingPreprocessor(cache_path)
  31. model = BertForTextRanking.from_pretrained(cache_path)
  32. pipeline1 = TextRankingPipeline(model, preprocessor=tokenizer)
  33. pipeline2 = pipeline(
  34. Tasks.text_ranking, model=model, preprocessor=tokenizer)
  35. print(f'sentence: {self.inputs}\n'
  36. f'pipeline1:{pipeline1(input=self.inputs)}')
  37. print()
  38. print(f'pipeline2: {pipeline2(input=self.inputs)}')
  39. @unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
  40. def test_run_with_model_from_modelhub(self):
  41. for model_id in self.models:
  42. model = Model.from_pretrained(model_id)
  43. tokenizer = TextRankingPreprocessor(model.model_dir)
  44. pipeline_ins = pipeline(
  45. task=Tasks.text_ranking, model=model, preprocessor=tokenizer)
  46. print(pipeline_ins(input=self.inputs))
  47. @unittest.skipUnless(test_level() >= 1, 'skip test in current test level')
  48. def test_run_with_model_name(self):
  49. for model_id in self.models:
  50. pipeline_ins = pipeline(task=Tasks.text_ranking, model=model_id)
  51. print(pipeline_ins(input=self.inputs))
  52. @unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
  53. def test_run_with_default_model(self):
  54. pipeline_ins = pipeline(task=Tasks.text_ranking)
  55. print(pipeline_ins(input=self.inputs))
  56. if __name__ == '__main__':
  57. unittest.main()