fill_mask.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # Copyright 2019 Facebook AI Research and the HuggingFace Inc. team.
  2. # Copyright (c) 2018, NVIDIA CORPORATION.
  3. # Copyright 2021-2022 The Alibaba DAMO NLP Team Authors.
  4. # All rights reserved.
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License");
  7. # you may not use this file except in compliance with the License.
  8. # You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. from transformers import RobertaForMaskedLM
  18. from modelscope.metainfo import Models
  19. from modelscope.models import Model, TorchModel
  20. from modelscope.models.builder import MODELS
  21. from modelscope.outputs import AttentionFillMaskModelOutput
  22. from modelscope.utils.constant import Tasks
  23. from .configuration import VecoConfig
  24. @MODELS.register_module(Tasks.fill_mask, module_name=Models.veco)
  25. class VecoForMaskedLM(TorchModel, RobertaForMaskedLM):
  26. """Veco Model transformer with a masked language model head on top (a linear layer on top of the
  27. pooled output).
  28. This model inherits from [`PreTrainedModel`]. Check the superclass documentation for the generic
  29. methods the library implements for all its model (such as downloading or saving, resizing the input embeddings,
  30. pruning heads etc.)
  31. This model is also a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module)
  32. subclass. Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to
  33. general usage and behavior.
  34. Preprocessor:
  35. This is the fill_mask model of StructBERT, the preprocessor of this model
  36. is `modelscope.preprocessors.FillMaskTransformersPreprocessor`.
  37. Parameters:
  38. config ([`VecoConfig`]): Model configuration class with all the parameters of the
  39. model. Initializing with a config file does not load the weights associated with the model, only the
  40. configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model
  41. weights.
  42. This class overrides [`RobertaForMaskedLM`]. Please check the superclass for the
  43. appropriate documentation alongside usage examples.
  44. """
  45. config_class = VecoConfig
  46. def __init__(self, config, **kwargs):
  47. super().__init__(config.name_or_path, **kwargs)
  48. super(Model, self).__init__(config)
  49. def forward(self, *args, **kwargs):
  50. """
  51. Returns:
  52. Returns `modelscope.outputs.AttentionFillMaskModelOutput`
  53. Examples:
  54. >>> from modelscope.models import Model
  55. >>> from modelscope.preprocessors import Preprocessor
  56. >>> model = Model.from_pretrained('damo/nlp_veco_fill-mask-large')
  57. >>> preprocessor = Preprocessor.from_pretrained('damo/nlp_veco_fill-mask-large')
  58. >>> # Call the model, return some tensors
  59. >>> print(model(**preprocessor('你师父差得动你,你师父可<mask>不动我。')))
  60. >>> # Call the pipeline
  61. >>> from modelscope.pipelines import pipeline
  62. >>> pipeline_ins = pipeline('fill-mask', model=model, preprocessor=preprocessor)
  63. >>> print(pipeline_ins('你师父差得动你,你师父可<mask>不动我。'))
  64. """
  65. kwargs['return_dict'] = True
  66. outputs = super(Model, self).forward(*args, **kwargs)
  67. return AttentionFillMaskModelOutput(
  68. loss=outputs.loss,
  69. logits=outputs.logits,
  70. hidden_states=outputs.hidden_states,
  71. attentions=outputs.attentions,
  72. input_ids=kwargs['input_ids'],
  73. )
  74. @classmethod
  75. def _instantiate(cls, **kwargs):
  76. model_dir = kwargs.pop('model_dir', None)
  77. if model_dir is None:
  78. ponet_config = VecoConfig(**kwargs)
  79. model = cls(ponet_config)
  80. else:
  81. model = super(
  82. Model,
  83. cls).from_pretrained(pretrained_model_name_or_path=model_dir)
  84. return model