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_generation.py 11 kB

[to #42322933] add/refactor nlp models source code and finetune 1. add sbert,veco,palm,space source code 2. support sbert sequence classification, token classification finetune 3. support veco sequence classification finetune 4. support palm nlg finetune evaluation result: https://sheet.alibaba-inc.com/#/sheet/f7fdcc7f22bd5105 sheet:Maas 5. add ut for finetunes 6. add veco's taskdataset processor 7. add a common trainer for nlp, and a specific trainer for veco 8. merge some duplicate codes of models, preprocessors, pipelines Link: https://code.alibaba-inc.com/Ali-MaaS/MaaS-lib/codereview/9574105 * add basic class of hook&metrics * pre-commit passed * change some comments * pre commit passed * 1. remove accuracy's groups 2. remove useless hooks 3. simplify priorities * pre-commit passed * fix a comment * Merge branch 'master' into finetune_hooks_metrics # Conflicts: # modelscope/metainfo.py * pre-commit passed * add basic class of hook&metrics * pre-commit passed * change some comments * pre commit passed * 1. remove accuracy's groups 2. remove useless hooks 3. simplify priorities * pre-commit passed * fix a comment * Merge branch 'feat/finetune' of gitlab.alibaba-inc.com:Ali-MaaS/MaaS-lib into feat/finetune * mv hooks related to modelscope/trainers/hooks * mv priority back * add torch mdoel base and test * update hooks, trainer, import_util * add torch epoch based trainer and dis utils * add hooks * fix warmup * format code stype and fix warmup and add warmup unittest * fix impls * pre-commit check passed * update hook and add EpochBasedTrainer * add trainer unittest * Merge branch 'feat/add_hooks' into feat/add_task # Conflicts: # modelscope/models/base_torch.py # modelscope/trainers/hooks/hook.py # modelscope/trainers/trainer.py * update unittest name * rewrite taskdataset to trainer * fix trainer and add unittest * add unittest * code: run to forward * run through... but ugly code * arrange some cls * fix some errs * revert some mistakes * init check in * Merge branch 'feat/add_hooks' into feat/add_task # Conflicts: # modelscope/trainers/trainer.py * test with bigger epoch and size * add the default metrics class * move build metrics code to a method * merge add_task * merge origin add_task * add device initialization * remove preprocessor arg for bool * add task models * move metric collect logic to metrics class * pre-commit passed * fix cr comments * precommit passed * add task models * Merge remote-tracking branch 'origin/feat/add_task' into feat/backbone_head * add comment * change comment formats. * fix comments * fix ut bug * fix comments * add wrapper check * fix comments * pre commit passed * fix cr comments * solve a loop import problem * fix ut bug * fix ut errors * change dummydataset to msdataset * precommit passed * merge add task * backbone-head is build, model is not correctly loaded * model load states matched * result matched * lint * add veco/palm_v2 code * merge master * merge master success running * add repr model name level * Merge branch 'feat/veco_palm' into feat/finetune_sbert_veco * model test for training * add token-classification metric add formal ut * fix running bug * finetune and pipeline are working with backbone-head * add nli * add missing code * finetune and pipeline are working with backbone-head * Merge branch 'feat/backbone_head' of http://gitlab.alibaba-inc.com/Ali-MaaS/MaaS-lib into feat/backbone_head * add a test repo for pr * remove merge conflicted file * remove merge conflicted file 1 * lint check * import error * none type bug fix * forward input unpacking or dict bug * move head into models, add build_backbone with registry, no base method * merge master * feat: 1. add interleave dataset method 2. support multiple dataset in trainer.build_dataset 3. support 3 sub tasks in sequence_classification task * unfinished * update the task model structure in NLP field * merge master * update by comments * keep the default model id as current on production * unfinished * unfinished * veco can run * Merge remote-tracking branch 'origin/master' into feat/backbone_head * add taskmodel for module management * remove forward_input_is_dict * unfinished * token classification started * update base model structure * move space to backbone * remove 'type' in build_from_cfg method * test update * bug fix * on tesing, mess code * Merge branch 'feat/backbone_head' into feat/refactor_nlp_730 # Conflicts: # modelscope/metrics/builder.py # modelscope/models/__init__.py # modelscope/models/nlp/__init__.py # modelscope/preprocessors/nlp.py # modelscope/trainers/trainer.py # requirements/multi-modal.txt * add missing merge * add sofa source code * refactor * add veco task dataset * add veco task dataset * pre-commit passed * fix bug of log * add some features * merge master * bug fix * refine nlp models * fix the training error * unfinished * refactor pipeline * Merge branch 'feat/backbone_head' into feat/refactor_nlp_730 # Conflicts: # modelscope/metrics/builder.py # modelscope/models/nlp/__init__.py # modelscope/models/nlp/backbones/structbert/modeling_sbert.py # modelscope/models/nlp/palm_v2/palm_for_text_generation.py # modelscope/preprocessors/base.py # modelscope/preprocessors/nlp.py # modelscope/trainers/trainer.py * Merge commit 'ab04ceafc5453ce7daa9aa09e37a55f703072a10' into feat/refactor_nlp_730 # Conflicts: # modelscope/metainfo.py # modelscope/metrics/builder.py # modelscope/models/__init__.py # modelscope/models/base/base_torch_model.py # modelscope/models/nlp/__init__.py # modelscope/models/nlp/backbones/space/model/intent_unified_transformer.py # modelscope/models/nlp/backbones/space/model/model_base.py # modelscope/models/nlp/palm_v2/palm_for_text_generation.py # modelscope/models/nlp/sbert_for_sequence_classification.py # modelscope/models/nlp/sequence_classification.py # modelscope/models/nlp/space/__init__.py # modelscope/models/nlp/space_for_dialog_intent_prediction.py # modelscope/models/nlp/space_for_dialog_modeling.py # modelscope/models/nlp/space_for_dialog_state_tracking.py # modelscope/models/nlp/task_model.py # modelscope/pipelines/nlp/sentiment_classification_pipeline.py # modelscope/preprocessors/base.py # modelscope/preprocessors/nlp.py # modelscope/trainers/trainer.py * revert changes * unify sentnece classification postprocess * revert some changes, move some model files * pipeline first case run through * ws pipeline passed * Merge branch 'feat/refactor_nlp_730' into feat/finetune_sbert_veco * finetune * revert code * revert some code * ws finetune started, only the accuracy is weird * Merge branch 'feat/veco_taskdataset' into feat/finetune_sbert_veco # Conflicts: # modelscope/task_datasets/veco_dataset.py # tests/taskdataset/test_veco_dataset.py * veco+nli finetune started * Merge branch 'master' into feat/finetune_sbert_veco # Conflicts: # modelscope/models/nlp/sbert_for_sequence_classification.py # modelscope/models/nlp/sbert_for_token_classification.py # modelscope/models/nlp/sbert_for_zero_shot_classification.py # modelscope/models/nlp/space/space_for_dialog_intent_prediction.py # modelscope/models/nlp/space/space_for_dialog_modeling.py # modelscope/trainers/trainer.py * add trainer for nlp * trainer: dataset params passed into preprocessor * test passed by nlptrainer * fix some bugs * fix some bugs * add backbone/head subclass * fix regression bugs * fix bug in token-cls finetune * support cfg modification * fix bug * fix bug * update requirements * add some comments and fix some t * add some comments and revert a argument * split to two test files * revert code * fixbug in precessor (cherry picked from commit 7a648d096ef8500c694d3255dabe29e6f4bfc3e5) * fix ut bug * support sbert models * unfinished * Merge branch 'feat/finetune_sbert_veco' into sly_tmp_veco_finetune # Conflicts: # tests/trainers/test_finetune_sequence_classification.py * fixbug in veco * fix bug * fixbug * correct running params * remove useless files * add palm finetuning with cnn_dailymail dataset * copy space model from sofa * Merge branch 'feat/finetune_sbert_veco' of gitlab.alibaba-inc.com:Ali-MaaS/MaaS-lib into feat/finetune_sbert_veco * Merge branch 'master' into feat/finetune_sbert_veco # Conflicts: # modelscope/metrics/__init__.py # modelscope/models/__init__.py # modelscope/models/nlp/__init__.py # modelscope/models/nlp/backbones/__init__.py # modelscope/models/nlp/backbones/structbert/modeling_sbert.py # modelscope/models/nlp/heads/__init__.py # modelscope/models/nlp/masked_language.py # modelscope/models/nlp/palm_v2/palm_for_text_generation.py # modelscope/models/nlp/sbert_for_nli.py # modelscope/models/nlp/sbert_for_sentence_similarity.py # modelscope/models/nlp/sbert_for_sentiment_classification.py # modelscope/models/nlp/sbert_for_sequence_classification.py # modelscope/models/nlp/sbert_for_token_classification.py # modelscope/models/nlp/sbert_for_zero_shot_classification.py # modelscope/models/nlp/sequence_classification.py # modelscope/models/nlp/space/space_for_dialog_intent_prediction.py # modelscope/models/nlp/space/space_for_dialog_modeling.py # modelscope/models/nlp/space/space_for_dialog_state_tracking.py # modelscope/models/nlp/structbert/adv_utils.py # modelscope/models/nlp/structbert/configuration_sbert.py # modelscope/models/nlp/task_models/task_model.py # modelscope/pipelines/__init__.py # modelscope/pipelines/nlp/__init__.py # modelscope/pipelines/nlp/fill_mask_pipeline.py # modelscope/pipelines/nlp/named_entity_recognition_pipeline.py # modelscope/pipelines/nlp/nli_pipeline.py # modelscope/pipelines/nlp/sentence_similarity_pipeline.py # modelscope/pipelines/nlp/sentiment_classification_pipeline.py # modelscope/pipelines/nlp/text_generation_pipeline.py # modelscope/pipelines/nlp/word_segmentation_pipeline.py # modelscope/pipelines/nlp/zero_shot_classification_pipeline.py # modelscope/preprocessors/nlp.py # modelscope/task_datasets/__init__.py # modelscope/trainers/trainer.py # modelscope/trainers/utils/inference.py # modelscope/utils/file_utils.py # requirements/nlp.txt # tests/pipelines/test_nli.py # tests/pipelines/test_sentence_similarity.py # tests/pipelines/test_sentiment_classification.py * fix imports * mark backbone in their own modeling * pre-commit check passed * pre-commit passed, remove roberta model * fix a bug in ast import * skip all finetune uts * fix bugs * pre-commit passed * bug fixed * bug fixed * bug fixed * bug fixed * fix ut bug * fix bug * fix ut bug * fix bug * fix bug * fixbugs * fixbug * revert veco * revert veco because of core dump * fix palm bug * revert veco * revert mistaken code * add a test print * pre-commit check * test exception * add test code * for test * fix bug and test * remove test code * remove useless file * 1. fix some bugs 2. add backbone ut * Merge branch 'master' into feat/finetune_refactor_730 # Conflicts: # modelscope/metainfo.py # modelscope/metrics/sequence_classification_metric.py # modelscope/models/nlp/__init__.py # modelscope/models/nlp/task_models/task_model.py # modelscope/preprocessors/__init__.py # modelscope/preprocessors/nlp.py # modelscope/trainers/trainer.py # modelscope/trainers/utils/inference.py # modelscope/utils/file_utils.py # tests/trainers/test_trainer_with_nlp.py * pre-commit passed * revert files * increase test level * unregister models * fix bugs * fix cr comments * fix bug in backbone-head * add sbert backbone * fix bug * add test for token-cls-metric * pre-commit passed * fix ut comments * revert normal tokenizer to fast tokenizer * Merge branch 'master' into feat/finetune_refactor_730 # Conflicts: # modelscope/models/nlp/__init__.py # modelscope/models/nlp/backbones/__init__.py # modelscope/models/nlp/backbones/structbert/__init__.py # modelscope/models/nlp/masked_language.py # modelscope/models/nlp/palm_v2/palm_for_text_generation.py # modelscope/models/nlp/sbert_for_sequence_classification.py # modelscope/models/nlp/sbert_for_token_classification.py # modelscope/models/nlp/sbert_for_zero_shot_classification.py # modelscope/pipelines/nlp/text_generation_pipeline.py # modelscope/preprocessors/nlp.py # modelscope/trainers/trainer.py # modelscope/trainers/utils/inference.py * fix merge bugs * pre commit passed * fix bug * fix bug * fix bug * fix bug from master * add print * fix ut bug * fix bug * Merge branch 'master' into feat/finetune_refactor_730 * skip task model test
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. # Copyright (c) Alibaba, Inc. and its affiliates.
  2. import unittest
  3. from modelscope.hub.snapshot_download import snapshot_download
  4. from modelscope.models import Model
  5. from modelscope.models.nlp import GPT3ForTextGeneration, PalmForTextGeneration
  6. from modelscope.pipelines import pipeline
  7. from modelscope.pipelines.nlp import TextGenerationPipeline
  8. from modelscope.preprocessors import TextGenerationPreprocessor
  9. from modelscope.utils.constant import Tasks
  10. from modelscope.utils.demo_utils import DemoCompatibilityCheck
  11. from modelscope.utils.test_utils import test_level
  12. class TextGenerationTest(unittest.TestCase, DemoCompatibilityCheck):
  13. def setUp(self) -> None:
  14. self.palm_model_id_zh_base = 'damo/nlp_palm2.0_text-generation_chinese-base'
  15. self.palm_model_id_zh_large = 'damo/nlp_palm2.0_text-generation_chinese-large'
  16. self.palm_model_id_zh_commodity = 'damo/nlp_palm2.0_text-generation_commodity_chinese-base'
  17. self.palm_model_id_zh_weather = 'damo/nlp_palm2.0_text-generation_weather_chinese-base'
  18. self.palm_model_id_en = 'damo/nlp_palm2.0_text-generation_english-base'
  19. self.palm_input_zh = """
  20. 本文总结了十个可穿戴产品的设计原则,而这些原则,同样也是笔者认为是这个行业最吸引人的地方:
  21. 1.为人们解决重复性问题;2.从人开始,而不是从机器开始;3.要引起注意,但不要刻意;4.提升用户能力,而不是取代
  22. """
  23. self.palm_input_commodity = '垃圾桶,双层,可拆卸,加高,加高双层,把手,垃圾桶,内附,万向轮'
  24. self.palm_input_weather = "今日天气类型='浮尘'&空气质量等级='重度污染'&紫外线强度指数='中等'"
  25. self.palm_input_en = """
  26. The Director of Public Prosecutions who let off Lord Janner over alleged child sex abuse started
  27. her career at a legal chambers when the disgraced Labour peer was a top QC there . Alison Saunders ,
  28. 54 , sparked outrage last week when she decided the 86-year-old should not face astring of charges
  29. of paedophilia against nine children because he has dementia . Today , newly-released documents
  30. revealed damning evidence that abuse was covered up by police andsocial workers for more than 20 years .
  31. And now it has emerged Mrs Saunders ' law career got off to a flying start when she secured her
  32. pupillage -- a barrister 's training contract at 1 Garden Court Chambers in London in 1983 .
  33. """
  34. self.gpt3_base_model_id = 'damo/nlp_gpt3_text-generation_chinese-base'
  35. self.gpt3_large_model_id = 'damo/nlp_gpt3_text-generation_chinese-large'
  36. self.gpt3_poetry_large_model_id = 'damo/nlp_gpt3_poetry-generation_chinese-large'
  37. self.gpt3_input = '《故乡》。深蓝的天空中挂着一轮金黄的圆月,下面是海边的沙地,'
  38. self.gpt3_poetry_input = '天生我材必有用,'
  39. def run_pipeline_with_model_instance(self, model_id, input):
  40. model = Model.from_pretrained(model_id)
  41. preprocessor = TextGenerationPreprocessor(
  42. model.model_dir,
  43. model.tokenizer,
  44. first_sequence='sentence',
  45. second_sequence=None)
  46. pipeline_ins = pipeline(
  47. task=Tasks.text_generation, model=model, preprocessor=preprocessor)
  48. print(pipeline_ins(input))
  49. def run_pipeline_with_model_id(self, model_id, input):
  50. pipeline_ins = pipeline(task=Tasks.text_generation, model=model_id)
  51. print(pipeline_ins(input))
  52. @unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
  53. def test_palm_zh_base_with_model_name(self):
  54. self.run_pipeline_with_model_id(self.palm_model_id_zh_base,
  55. self.palm_input_zh)
  56. @unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
  57. def test_palm_en_with_model_name(self):
  58. self.run_pipeline_with_model_id(self.palm_model_id_en,
  59. self.palm_input_en)
  60. @unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
  61. def test_gpt_base_with_model_name(self):
  62. self.run_pipeline_with_model_id(self.gpt3_base_model_id,
  63. self.gpt3_input)
  64. @unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
  65. def test_gpt_large_with_model_name(self):
  66. self.run_pipeline_with_model_id(self.gpt3_large_model_id,
  67. self.gpt3_input)
  68. @unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
  69. def test_palm_zh_large_with_model_name(self):
  70. self.run_pipeline_with_model_id(self.palm_model_id_zh_large,
  71. self.palm_input_zh)
  72. @unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
  73. def test_palm_zh_commodity_with_model_name(self):
  74. self.run_pipeline_with_model_id(self.palm_model_id_zh_commodity,
  75. self.palm_input_commodity)
  76. @unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
  77. def test_palm_zh_weather_with_model_name(self):
  78. self.run_pipeline_with_model_id(self.palm_model_id_zh_weather,
  79. self.palm_input_weather)
  80. @unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
  81. def test_palm_zh_base_with_model_instance(self):
  82. self.run_pipeline_with_model_instance(self.palm_model_id_zh_base,
  83. self.palm_input_zh)
  84. @unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
  85. def test_palm_zh_large_with_model_instance(self):
  86. self.run_pipeline_with_model_instance(self.palm_model_id_zh_large,
  87. self.palm_input_zh)
  88. @unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
  89. def test_palm_zh_commodity_with_model_instance(self):
  90. self.run_pipeline_with_model_instance(self.palm_model_id_zh_commodity,
  91. self.palm_input_commodity)
  92. @unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
  93. def test_palm_zh_weather_with_model_instance(self):
  94. self.run_pipeline_with_model_instance(self.palm_model_id_zh_weather,
  95. self.palm_input_weather)
  96. @unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
  97. def test_palm_en_with_model_instance(self):
  98. self.run_pipeline_with_model_instance(self.palm_model_id_en,
  99. self.palm_input_en)
  100. @unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
  101. def test_gpt_poetry_large_with_model_name(self):
  102. self.run_pipeline_with_model_id(self.gpt3_poetry_large_model_id,
  103. self.gpt3_poetry_input)
  104. @unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
  105. def test_gpt_base_with_model_instance(self):
  106. self.run_pipeline_with_model_instance(self.gpt3_base_model_id,
  107. self.gpt3_input)
  108. @unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
  109. def test_gpt_large_with_model_instance(self):
  110. self.run_pipeline_with_model_instance(self.gpt3_large_model_id,
  111. self.gpt3_input)
  112. @unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
  113. def test_gpt_poetry_large_with_model_instance(self):
  114. self.run_pipeline_with_model_instance(self.gpt3_poetry_large_model_id,
  115. self.gpt3_poetry_input)
  116. @unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
  117. def test_run_palm(self):
  118. for model_id, input in ((self.palm_model_id_zh_base,
  119. self.palm_input_zh), (self.palm_model_id_en,
  120. self.palm_input_en)):
  121. cache_path = snapshot_download(model_id)
  122. model = PalmForTextGeneration.from_pretrained(cache_path)
  123. preprocessor = TextGenerationPreprocessor(
  124. cache_path,
  125. model.tokenizer,
  126. first_sequence='sentence',
  127. second_sequence=None)
  128. pipeline1 = TextGenerationPipeline(model, preprocessor)
  129. pipeline2 = pipeline(
  130. Tasks.text_generation, model=model, preprocessor=preprocessor)
  131. print(
  132. f'pipeline1: {pipeline1(input)}\npipeline2: {pipeline2(input)}'
  133. )
  134. @unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
  135. def test_run_gpt3(self):
  136. cache_path = snapshot_download(self.gpt3_base_model_id)
  137. model = GPT3ForTextGeneration(cache_path)
  138. preprocessor = TextGenerationPreprocessor(
  139. cache_path,
  140. model.tokenizer,
  141. first_sequence='sentence',
  142. second_sequence=None)
  143. pipeline1 = TextGenerationPipeline(model, preprocessor)
  144. pipeline2 = pipeline(
  145. Tasks.text_generation, model=model, preprocessor=preprocessor)
  146. print(
  147. f'pipeline1: {pipeline1(self.gpt3_input)}\npipeline2: {pipeline2(self.gpt3_input)}'
  148. )
  149. @unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
  150. def test_run_with_default_model(self):
  151. pipeline_ins = pipeline(task=Tasks.text_generation)
  152. print(pipeline_ins(self.palm_input_zh))
  153. @unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
  154. def test_bloom(self):
  155. pipe = pipeline(
  156. task=Tasks.text_generation, model='langboat/bloom-1b4-zh')
  157. print(pipe('中国的首都是'))
  158. @unittest.skip("Langboat's checkpoint has not been uploaded to modelhub")
  159. def test_gpt_neo(self):
  160. pipe = pipeline(
  161. task=Tasks.text_generation, model='langboat/mengzi-gpt-neo-base')
  162. print(
  163. pipe(
  164. '我是',
  165. do_sample=True,
  166. top_k=5,
  167. top_p=1,
  168. max_length=20,
  169. repetition_penalty=0.5))
  170. @unittest.skip('demo compatibility test is only enabled on a needed-basis')
  171. def test_demo_compatibility(self):
  172. self.compatibility_check()
  173. if __name__ == '__main__':
  174. unittest.main()