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_dialog_modeling.py 6.6 kB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 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
3 years ago
3 years ago
3 years ago
3 years ago
3 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
3 years ago
3 years ago
3 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
3 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
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. # Copyright (c) Alibaba, Inc. and its affiliates.
  2. import unittest
  3. from typing import List
  4. from modelscope.hub.snapshot_download import snapshot_download
  5. from modelscope.models import Model
  6. from modelscope.models.nlp import SpaceForDialogModeling
  7. from modelscope.outputs import OutputKeys
  8. from modelscope.pipelines import pipeline
  9. from modelscope.pipelines.nlp import DialogModelingPipeline
  10. from modelscope.preprocessors import DialogModelingPreprocessor
  11. from modelscope.utils.constant import Tasks
  12. from modelscope.utils.demo_utils import DemoCompatibilityCheck
  13. from modelscope.utils.test_utils import test_level
  14. class DialogModelingTest(unittest.TestCase, DemoCompatibilityCheck):
  15. def setUp(self) -> None:
  16. self.task = Tasks.task_oriented_conversation
  17. self.model_id = 'damo/nlp_space_dialog-modeling'
  18. test_case = {
  19. 'sng0073': {
  20. 'goal': {
  21. 'taxi': {
  22. 'info': {
  23. 'leaveat': '17:15',
  24. 'destination': 'pizza hut fen ditton',
  25. 'departure': "saint john's college"
  26. },
  27. 'reqt': ['car', 'phone'],
  28. 'fail_info': {}
  29. }
  30. },
  31. 'log': [{
  32. 'user':
  33. "i would like a taxi from saint john 's college to pizza hut fen ditton .",
  34. 'user_delex':
  35. 'i would like a taxi from [value_departure] to [value_destination] .',
  36. 'resp':
  37. 'what time do you want to leave and what time do you want to arrive by ?',
  38. 'sys':
  39. 'what time do you want to leave and what time do you want to arrive by ?',
  40. 'pointer': '0,0,0,0,0,0',
  41. 'match': '',
  42. 'constraint':
  43. "[taxi] destination pizza hut fen ditton departure saint john 's college",
  44. 'cons_delex': '[taxi] destination departure',
  45. 'sys_act': '[taxi] [request] leave arrive',
  46. 'turn_num': 0,
  47. 'turn_domain': '[taxi]'
  48. }, {
  49. 'user': 'i want to leave after 17:15 .',
  50. 'user_delex': 'i want to leave after [value_leave] .',
  51. 'resp':
  52. 'booking completed ! your taxi will be [value_car] contact number is [value_phone]',
  53. 'sys':
  54. 'booking completed ! your taxi will be blue honda contact number is 07218068540',
  55. 'pointer': '0,0,0,0,0,0',
  56. 'match': '',
  57. 'constraint':
  58. "[taxi] destination pizza hut fen ditton departure saint john 's college leave 17:15",
  59. 'cons_delex': '[taxi] destination departure leave',
  60. 'sys_act': '[taxi] [inform] car phone',
  61. 'turn_num': 1,
  62. 'turn_domain': '[taxi]'
  63. }, {
  64. 'user': 'thank you for all the help ! i appreciate it .',
  65. 'user_delex': 'thank you for all the help ! i appreciate it .',
  66. 'resp':
  67. 'you are welcome . is there anything else i can help you with today ?',
  68. 'sys':
  69. 'you are welcome . is there anything else i can help you with today ?',
  70. 'pointer': '0,0,0,0,0,0',
  71. 'match': '',
  72. 'constraint':
  73. "[taxi] destination pizza hut fen ditton departure saint john 's college leave 17:15",
  74. 'cons_delex': '[taxi] destination departure leave',
  75. 'sys_act': '[general] [reqmore]',
  76. 'turn_num': 2,
  77. 'turn_domain': '[general]'
  78. }, {
  79. 'user': 'no , i am all set . have a nice day . bye .',
  80. 'user_delex': 'no , i am all set . have a nice day . bye .',
  81. 'resp': 'you too ! thank you',
  82. 'sys': 'you too ! thank you',
  83. 'pointer': '0,0,0,0,0,0',
  84. 'match': '',
  85. 'constraint':
  86. "[taxi] destination pizza hut fen ditton departure saint john 's college leave 17:15",
  87. 'cons_delex': '[taxi] destination departure leave',
  88. 'sys_act': '[general] [bye]',
  89. 'turn_num': 3,
  90. 'turn_domain': '[general]'
  91. }]
  92. }
  93. }
  94. def generate_and_print_dialog_response(
  95. self, pipelines: List[DialogModelingPipeline]):
  96. result = {}
  97. pipeline_len = len(pipelines)
  98. for step, item in enumerate(self.test_case['sng0073']['log']):
  99. user = item['user']
  100. print('user: {}'.format(user))
  101. result = pipelines[step % pipeline_len]({
  102. 'user_input': user,
  103. 'history': result
  104. })
  105. print('response : {}'.format(result[OutputKeys.OUTPUT]))
  106. @unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
  107. def test_run_by_direct_model_download(self):
  108. cache_path = snapshot_download(self.model_id)
  109. preprocessor = DialogModelingPreprocessor(model_dir=cache_path)
  110. model = SpaceForDialogModeling(
  111. model_dir=cache_path,
  112. text_field=preprocessor.text_field,
  113. config=preprocessor.config)
  114. pipelines = [
  115. DialogModelingPipeline(model=model, preprocessor=preprocessor)
  116. ]
  117. self.generate_and_print_dialog_response(pipelines)
  118. @unittest.skipUnless(test_level() >= 1, 'skip test in current test level')
  119. def test_run_with_model_from_modelhub(self):
  120. model = Model.from_pretrained(self.model_id)
  121. preprocessor = DialogModelingPreprocessor(model_dir=model.model_dir)
  122. pipelines = [
  123. DialogModelingPipeline(model=model, preprocessor=preprocessor)
  124. ]
  125. self.generate_and_print_dialog_response(pipelines)
  126. @unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
  127. def test_run_with_model_name(self):
  128. pipelines = [pipeline(task=self.task, model=self.model_id)]
  129. self.generate_and_print_dialog_response(pipelines)
  130. @unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
  131. def test_run_with_default_model(self):
  132. pipelines = [pipeline(task=self.task)]
  133. self.generate_and_print_dialog_response(pipelines)
  134. @unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
  135. def test_demo_compatibility(self):
  136. self.compatibility_check()
  137. if __name__ == '__main__':
  138. unittest.main()