nlp_outputs.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. from dataclasses import dataclass
  2. from typing import List, Optional, Tuple, Union
  3. import numpy as np
  4. from modelscope.outputs.outputs import ModelOutputBase
  5. Tensor = Union['torch.Tensor', 'tf.Tensor']
  6. @dataclass
  7. class BackboneModelOutput(ModelOutputBase):
  8. """The output class for text classification models.
  9. Args:
  10. last_hidden_state (`Tensor`, *optional*): Sequence of hidden-states at
  11. the output of the last layer of the model.
  12. pooler_output (`Tensor`, *optional*) The tensor of the pooled hidden state.
  13. hidden_states (`Tensor`, *optional*) Hidden-states of the model at
  14. the output of each layer plus the optional initial embedding outputs.
  15. """
  16. last_hidden_state: Tensor = None
  17. pooler_output: Tensor = None
  18. hidden_states: Tensor = None
  19. @dataclass
  20. class AttentionBackboneModelOutput(BackboneModelOutput):
  21. """The output class for backbones of attention based models.
  22. Args:
  23. attentions (`tuple(torch.FloatTensor)`, *optional*, returned when
  24. `output_attentions=True` is passed or when
  25. `config.output_attentions=True`):
  26. Tuple of `torch.FloatTensor` (one for each layer) of shape
  27. `(batch_size, num_heads, sequence_length, sequence_length)`.
  28. Attentions weights after the attention softmax, used to compute the
  29. weighted average in the self-attention heads.
  30. cross_attentions (`tuple(torch.FloatTensor)`, *optional*, returned when
  31. `output_attentions=True` and `config.add_cross_attention=True` is passed
  32. or when `config.output_attentions=True`):
  33. Tuple of `torch.FloatTensor` (one for each layer) of shape
  34. `(batch_size, num_heads, sequence_length, sequence_length)`.
  35. Attentions weights of the decoder's cross-attention layer, after the
  36. attention softmax, used to compute the weighted average in the
  37. cross-attention heads.
  38. past_key_values (`tuple(tuple(torch.FloatTensor))`, *optional*, returned
  39. when `use_cache=True` is passed or when `config.use_cache=True`):
  40. Tuple of `tuple(torch.FloatTensor)` of length `config.n_layers`,
  41. with each tuple having 2 tensors of shape `(batch_size, num_heads,
  42. sequence_length, embed_size_per_head)`) and optionally if
  43. `config.is_encoder_decoder=True` 2 additional tensors of shape
  44. `(batch_size, num_heads, encoder_sequence_length,
  45. embed_size_per_head)`.
  46. Contains pre-computed hidden-states (key and values in the
  47. self-attention blocks and optionally if
  48. `config.is_encoder_decoder=True` in the cross-attention blocks) that
  49. can be used (see `past_key_values` input) to speed up sequential
  50. decoding.
  51. """
  52. attentions: Tensor = None
  53. past_key_values: Tensor = None
  54. cross_attentions: Tensor = None
  55. @dataclass
  56. class Seq2SeqModelOutput(ModelOutputBase):
  57. """
  58. Base class for model encoder's outputs that also contains : pre-computed
  59. hidden states that can speed up sequential decoding.
  60. Args:
  61. last_hidden_state (`torch.FloatTensor` of shape `(batch_size,
  62. sequence_length, hidden_size)`):
  63. Sequence of hidden-states at the output of the last layer of the
  64. decoder of the model.
  65. If `past_key_values` is used only the last hidden-state of the
  66. sequences of shape `(batch_size, 1, hidden_size)` is output.
  67. past_key_values (`tuple(tuple(torch.FloatTensor))`, *optional*, returned
  68. when `use_cache=True` is passed or when `config.use_cache=True`):
  69. Tuple of `tuple(torch.FloatTensor)` of length `config.n_layers`,
  70. with each tuple having 2 tensors of shape `(batch_size, num_heads,
  71. sequence_length, embed_size_per_head)`) and 2 additional tensors of
  72. shape `(batch_size, num_heads, encoder_sequence_length,
  73. embed_size_per_head)`.
  74. Contains pre-computed hidden-states (key and values in the
  75. self-attention blocks and in the cross-attention blocks) that can be
  76. used (see `past_key_values` input) to speed up sequential decoding.
  77. decoder_hidden_states (`tuple(torch.FloatTensor)`, *optional*, returned
  78. when `output_hidden_states=True` is passed or when
  79. `config.output_hidden_states=True`):
  80. Tuple of `torch.FloatTensor` (one for the output of the embeddings,
  81. if the model has an embedding layer, + one for the output of each
  82. layer) of shape `(batch_size, sequence_length, hidden_size)`.
  83. Hidden-states of the decoder at the output of each layer plus the
  84. optional initial embedding outputs.
  85. decoder_attentions (`tuple(torch.FloatTensor)`, *optional*, returned
  86. when `output_attentions=True` is passed or when
  87. `config.output_attentions=True`):
  88. Tuple of `torch.FloatTensor` (one for each layer) of shape
  89. `(batch_size, num_heads, sequence_length, sequence_length)`.
  90. Attentions weights of the decoder, after the attention softmax, used
  91. to compute the weighted average in the self-attention heads.
  92. cross_attentions (`tuple(torch.FloatTensor)`, *optional*, returned when
  93. `output_attentions=True` is passed or when
  94. `config.output_attentions=True`):
  95. Tuple of `torch.FloatTensor` (one for each layer) of shape
  96. `(batch_size, num_heads, sequence_length, sequence_length)`.
  97. Attentions weights of the decoder's cross-attention layer, after the
  98. attention softmax, used to compute the weighted average in the
  99. cross-attention heads.
  100. encoder_last_hidden_state (`torch.FloatTensor` of shape `(batch_size,
  101. sequence_length, hidden_size)`, *optional*):
  102. Sequence of hidden-states at the output of the last layer of the
  103. encoder of the model.
  104. encoder_hidden_states (`tuple(torch.FloatTensor)`, *optional*, returned
  105. when `output_hidden_states=True` is passed or when
  106. `config.output_hidden_states=True`):
  107. Tuple of `torch.FloatTensor` (one for the output of the embeddings,
  108. if the model has an embedding layer, + one for the output of each
  109. layer) of shape `(batch_size, sequence_length, hidden_size)`.
  110. Hidden-states of the encoder at the output of each layer plus the
  111. optional initial embedding outputs.
  112. encoder_attentions (`tuple(torch.FloatTensor)`, *optional*, returned
  113. when `output_attentions=True` is passed or when
  114. `config.output_attentions=True`):
  115. Tuple of `torch.FloatTensor` (one for each layer) of shape
  116. `(batch_size, num_heads, sequence_length, sequence_length)`.
  117. Attentions weights of the encoder, after the attention softmax, used
  118. to compute the weighted average in the self-attention heads.
  119. """
  120. last_hidden_state: Tensor = None
  121. past_key_values: Optional[Tuple[Tuple[Tensor]]] = None
  122. decoder_hidden_states: Optional[Tuple[Tensor]] = None
  123. decoder_attentions: Optional[Tuple[Tensor]] = None
  124. cross_attentions: Optional[Tuple[Tensor]] = None
  125. encoder_last_hidden_state: Optional[Tensor] = None
  126. encoder_hidden_states: Optional[Tuple[Tensor]] = None
  127. encoder_attentions: Optional[Tuple[Tensor]] = None
  128. @dataclass
  129. class FaqQuestionAnsweringOutput(ModelOutputBase):
  130. """The output class for faq QA models.
  131. """
  132. scores: Tensor = None
  133. labels: Tensor = None
  134. loss: Tensor = None
  135. logits: Tensor = None
  136. @dataclass
  137. class FeatureExtractionOutput(ModelOutputBase):
  138. """The output class for feature extraction models.
  139. """
  140. text_embedding: Tensor = None
  141. @dataclass
  142. class FillMaskModelOutput(ModelOutputBase):
  143. """The output class for fill mask models.
  144. Args:
  145. logits (`Tensor`): The logits output of the model.
  146. loss (`Tensor`, *optional*) The loss of the model, available when training.
  147. input_ids (`Tensor`, *optional*) The input id tensor fed into the model.
  148. hidden_states (`Tensor`, *optional*) Hidden-states of the model at the
  149. output of each layer plus the optional initial embedding outputs.
  150. """
  151. logits: Tensor = None
  152. loss: Tensor = None
  153. input_ids: Tensor = None
  154. hidden_states: Tensor = None
  155. @dataclass
  156. class AttentionFillMaskModelOutput(FillMaskModelOutput):
  157. """The output class for the fill mask and attention based models.
  158. Args:
  159. attentions (`tuple(Tensor)`, *optional* Attentions weights after the
  160. attention softmax, used to compute the weighted average in the
  161. self-attention heads.
  162. """
  163. attentions: Tensor = None
  164. @dataclass
  165. class InformationExtractionOutput(ModelOutputBase):
  166. """The output class for information extraction models.
  167. """
  168. spo_list: np.ndarray = None
  169. @dataclass
  170. class Seq2SeqLMOutput(ModelOutputBase):
  171. """
  172. Base class for sequence-to-sequence language models outputs.
  173. Args:
  174. loss (`torch.FloatTensor` of shape `(1,)`, *optional*, returned when
  175. `labels` is provided):
  176. Language modeling loss.
  177. logits (`torch.FloatTensor` of shape `(batch_size, sequence_length,
  178. config.vocab_size)`):
  179. Prediction scores of the language modeling head (scores for each
  180. vocabulary token before SoftMax).
  181. past_key_values (`tuple(tuple(torch.FloatTensor))`, *optional*, returned
  182. when `use_cache=True` is passed or when `config.use_cache=True`):
  183. Tuple of `tuple(torch.FloatTensor)` of length `config.n_layers`,
  184. with each tuple having 2 tensors of shape `(batch_size, num_heads,
  185. sequence_length, embed_size_per_head)`) and 2 additional tensors of
  186. shape `(batch_size, num_heads, encoder_sequence_length,
  187. embed_size_per_head)`.
  188. Contains pre-computed hidden-states (key and values in the
  189. self-attention blocks and in the cross-attention blocks) that can be
  190. used (see `past_key_values` input) to speed up sequential decoding.
  191. decoder_hidden_states (`tuple(torch.FloatTensor)`, *optional*, returned
  192. when `output_hidden_states=True` is passed or when
  193. `config.output_hidden_states=True`):
  194. Tuple of `torch.FloatTensor` (one for the output of the embeddings,
  195. if the model has an embedding layer, + one for the output of each
  196. layer) of shape `(batch_size, sequence_length, hidden_size)`.
  197. Hidden-states of the decoder at the output of each layer plus the
  198. initial embedding outputs.
  199. decoder_attentions (`tuple(torch.FloatTensor)`, *optional*, returned
  200. when `output_attentions=True` is passed or when
  201. `config.output_attentions=True`):
  202. Tuple of `torch.FloatTensor` (one for each layer) of shape
  203. `(batch_size, num_heads, sequence_length, sequence_length)`.
  204. Attentions weights of the decoder, after the attention softmax, used
  205. to compute the weighted average in the self-attention heads.
  206. cross_attentions (`tuple(torch.FloatTensor)`, *optional*, returned when
  207. `output_attentions=True` is passed or when
  208. `config.output_attentions=True`):
  209. Tuple of `torch.FloatTensor` (one for each layer) of shape
  210. `(batch_size, num_heads, sequence_length, sequence_length)`.
  211. Attentions weights of the decoder's cross-attention layer, after the
  212. attention softmax, used to compute the weighted average in the
  213. cross-attention heads.
  214. encoder_last_hidden_state (`torch.FloatTensor` of shape `(batch_size,
  215. sequence_length, hidden_size)`, *optional*):
  216. Sequence of hidden-states at the output of the last layer of the
  217. encoder of the model.
  218. encoder_hidden_states (`tuple(torch.FloatTensor)`, *optional*, returned
  219. when `output_hidden_states=True` is passed or when
  220. `config.output_hidden_states=True`):
  221. Tuple of `torch.FloatTensor` (one for the output of the embeddings,
  222. if the model has an embedding layer, + one for the output of each
  223. layer) of shape `(batch_size, sequence_length, hidden_size)`.
  224. Hidden-states of the encoder at the output of each layer plus the
  225. initial embedding outputs.
  226. encoder_attentions (`tuple(torch.FloatTensor)`, *optional*, returned
  227. when `output_attentions=True` is passed or when
  228. `config.output_attentions=True`):
  229. Tuple of `torch.FloatTensor` (one for each layer) of shape
  230. `(batch_size, num_heads, sequence_length, sequence_length)`.
  231. Attentions weights of the encoder, after the attention softmax, used
  232. to compute the weighted average in the self-attention heads.
  233. """
  234. loss: Optional[Tensor] = None
  235. logits: Tensor = None
  236. past_key_values: Optional[Tuple[Tuple[Tensor]]] = None
  237. decoder_hidden_states: Optional[Tuple[Tensor]] = None
  238. decoder_attentions: Optional[Tuple[Tensor]] = None
  239. cross_attentions: Optional[Tuple[Tensor]] = None
  240. encoder_last_hidden_state: Optional[Tensor] = None
  241. encoder_hidden_states: Optional[Tuple[Tensor]] = None
  242. encoder_attentions: Optional[Tuple[Tensor]] = None
  243. @dataclass
  244. class TextClassificationModelOutput(ModelOutputBase):
  245. """The output class for text classification models.
  246. Args:
  247. logits (`Tensor`): The logits output of the model. loss (`Tensor`,
  248. *optional*) The loss of the model, available when training.
  249. hidden_states (`Tensor`, *optional*) Hidden-states of the model at the
  250. output of each layer plus the optional initial embedding outputs.
  251. """
  252. logits: Tensor = None
  253. loss: Tensor = None
  254. @dataclass
  255. class AttentionTextClassificationModelOutput(TextClassificationModelOutput):
  256. """The output class for backbones of attention based models.
  257. Args:
  258. attentions (`tuple(Tensor)`, *optional* Attentions weights after the
  259. attention softmax, used to compute the weighted average in the
  260. self-attention heads.
  261. """
  262. attentions: Tensor = None
  263. hidden_states: Tensor = None
  264. past_key_values: Tensor = None
  265. @dataclass
  266. class TextErrorCorrectionOutput(ModelOutputBase):
  267. """The output class for information extraction models.
  268. """
  269. predictions: List = None
  270. @dataclass
  271. class WordAlignmentOutput(ModelOutputBase):
  272. """The output class for word alignment models.
  273. """
  274. predictions: List = None
  275. @dataclass
  276. class TextGenerationModelOutput(ModelOutputBase):
  277. """The output class for text generation models.
  278. Args:
  279. logits (`Tensor`): The logits output of the model. loss (`Tensor`,
  280. *optional*) The loss of the model, available when training.
  281. hidden_states (`Tensor`, *optional*) Hidden-states of the model at the
  282. output of each layer plus the optional initial embedding outputs.
  283. """
  284. logits: Tensor = None
  285. loss: Tensor = None
  286. @dataclass
  287. class AttentionTextGenerationModelOutput(TextGenerationModelOutput):
  288. """The output class for text generation of attention based models.
  289. Args:
  290. logits (`Tensor`): The logits output of the model. loss (`Tensor`,
  291. *optional*) The loss of the model, available when training.
  292. hidden_states (`Tensor`, *optional*) Hidden-states of the model at the
  293. output of each layer plus the optional initial embedding outputs.
  294. """
  295. attentions: Tensor = None
  296. hidden_states: Tensor = None
  297. past_key_values: Tensor = None
  298. @dataclass
  299. class TokenGeneratorOutput(ModelOutputBase):
  300. """
  301. The output class for generate method of text generation models.
  302. Args:
  303. sequences (`torch.LongTensor` of shape `(batch_size*num_return_sequences, sequence_length)`):
  304. The generated sequences. The second dimension (sequence_length) is either equal to `max_length` or shorter
  305. if all batches finished early due to the `eos_token_id`.
  306. scores (`tuple(torch.FloatTensor)` *optional*, returned when `output_scores=True`
  307. is passed or when `config.output_scores=True`):
  308. Processed prediction scores of the language modeling head (scores for each vocabulary token before SoftMax)
  309. at each generation step. Tuple of `torch.FloatTensor` with up to `max_new_tokens` elements (one element for
  310. each generated token), with each tensor of shape `(batch_size*num_return_sequences, config.vocab_size)`.
  311. attentions (`tuple(tuple(torch.FloatTensor))`, *optional*, returned when `output_attentions=True`
  312. is passed or `config.output_attentions=True`):
  313. Tuple (one element for each generated token) of tuples (one element for each layer of the decoder) of
  314. `torch.FloatTensor` of shape `(num_return_sequences*batch_size, num_heads, generated_length,
  315. sequence_length)`.
  316. hidden_states (`tuple(tuple(torch.FloatTensor))`, *optional*, returned when `output_hidden_states=True`
  317. is passed or when `config.output_hidden_states=True`):
  318. Tuple (one element for each generated token) of tuples (one element for each layer of the decoder) of
  319. `torch.FloatTensor` of shape `(num_return_sequences*batch_size, generated_length, hidden_size)`.
  320. """
  321. sequences: Tensor = None
  322. scores: Optional[Tuple[Tensor]] = None
  323. attentions: Optional[Tuple[Tuple[Tensor]]] = None
  324. hidden_states: Optional[Tuple[Tuple[Tensor]]] = None
  325. @dataclass
  326. class TokenClassificationModelOutput(ModelOutputBase):
  327. """The output class for token classification models.
  328. logits (`Tensor`): The logits output of the model.
  329. loss (`Tensor`, *optional*) The loss of the model, available when training.
  330. predictions: A PyTorch tensor of the best tag sequence for each batch of shape
  331. (nbest, batch_size, seq_length)
  332. offset_mapping (:obj:`torch.FloatTensor` of shape :obj:`(batch_size,
  333. sequence_length)`, `optional`):
  334. Indices of positions of each input sequence tokens in the sentence.
  335. Selected in the range ``[0, sequence_length - 1]``.
  336. """
  337. logits: Tensor = None
  338. loss: Tensor = None
  339. offset_mapping: Tensor = None
  340. predictions: Tensor = None
  341. label_mask: Tensor = None
  342. @dataclass
  343. class AttentionTokenClassificationModelOutput(TokenClassificationModelOutput):
  344. """The output class for backbones of attention based models.
  345. Args:
  346. attentions (`tuple(Tensor)`, *optional* Attentions weights after the attention softmax,
  347. used to compute the weighted average in the self-attention heads.
  348. """
  349. attentions: Tensor = None
  350. hidden_states: Tensor = None
  351. @dataclass
  352. class DialogueUserSatisfactionEstimationModelOutput(ModelOutputBase):
  353. """The output class for user satisfaction estimation.
  354. Args:
  355. logits (`Tensor`): The logits output of the model.
  356. """
  357. logits: Tensor = None
  358. @dataclass
  359. class SentencEmbeddingModelOutput(ModelOutputBase):
  360. """The output class for text classification models.
  361. Args:
  362. query_embs (`Tensor`, *optional*): The tensor of the query embeddings.
  363. doc_embs (`Tensor`, *optional*) Then tensor of the doc embeddings.
  364. loss (`torch.FloatTensor` of shape `(1,)`, *optional*): Sentence Embedding modeling loss.
  365. """
  366. query_embeddings: Tensor = None
  367. doc_embeddings: Tensor = None
  368. loss: Tensor = None
  369. @dataclass
  370. class TranslationEvaluationOutput(ModelOutputBase):
  371. """The output class for translation evaluation models.
  372. """
  373. score: Tensor = None
  374. loss: Tensor = None
  375. input_format: List[str] = None
  376. @dataclass
  377. class MachineReadingComprehensionOutput(ModelOutputBase):
  378. """The output class for machine reading comprehension models.
  379. Args:
  380. loss (`Tensor`, *optional*): The training loss of the current batch
  381. match_loss (`Tensor`, *optinal*): The match loss of the current batch
  382. span_logits (`Tensor`): The logits of the span matrix output by the model
  383. hidden_states (`Tuple[Tensor]`, *optinal*): The hidden states output by the model
  384. attentions (`Tuple[Tensor]`, *optinal*): The attention scores output by the model
  385. input_ids (`Tensor`): The token ids of the input sentence
  386. """
  387. loss: Optional[Tensor] = None
  388. match_loss: Optional[Tensor] = None
  389. span_logits: Tensor = None
  390. hidden_states: Optional[Tuple[Tensor]] = None
  391. attentions: Optional[Tuple[Tensor]] = None
  392. input_ids: Tensor = None