loss.py 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. # Copyright (c) 2022 NVIDIA Corporation. All rights reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import paddle
  15. class CrossEntropyLossForSQuAD(paddle.nn.Layer):
  16. """
  17. Loss function for SQuAD
  18. """
  19. def __init__(self):
  20. super().__init__()
  21. def forward(self, y, label):
  22. start_logits, end_logits = y
  23. start_position, end_position = label
  24. start_position = paddle.unsqueeze(start_position, axis=-1)
  25. end_position = paddle.unsqueeze(end_position, axis=-1)
  26. start_loss = paddle.nn.functional.softmax_with_cross_entropy(
  27. logits=start_logits, label=start_position, soft_label=False)
  28. start_loss = paddle.mean(start_loss)
  29. end_loss = paddle.nn.functional.softmax_with_cross_entropy(
  30. logits=end_logits, label=end_position, soft_label=False)
  31. end_loss = paddle.mean(end_loss)
  32. loss = (start_loss + end_loss) / 2
  33. return loss
  34. class BertPretrainingCriterion(paddle.nn.Layer):
  35. """
  36. Loss function for BertPretraining.
  37. Args:
  38. vocab_size(int):
  39. Vocabulary size of `inputs_ids` in `BertModel`.
  40. """
  41. def __init__(self, vocab_size):
  42. super().__init__()
  43. self.loss_fn = paddle.nn.loss.CrossEntropyLoss(ignore_index=-1)
  44. self.vocab_size = vocab_size
  45. def forward(self, prediction_scores, seq_relationship_score,
  46. masked_lm_labels, next_sentence_labels):
  47. """
  48. Args:
  49. prediction_scores(Tensor):
  50. The scores of masked token prediction. Its data type should be float32.
  51. If `masked_positions` is None, its shape is [batch_size, sequence_length, vocab_size].
  52. Otherwise, its shape is [batch_size, mask_token_num, vocab_size]
  53. seq_relationship_score(Tensor):
  54. The scores of next sentence prediction. Its data type should be float32 and
  55. its shape is [batch_size, 2]
  56. masked_lm_labels(Tensor):
  57. The labels of the masked language modeling, its dimensionality is equal to `prediction_scores`.
  58. Its data type should be int64. If `masked_positions` is None, its shape is [batch_size, sequence_length, 1].
  59. Otherwise, its shape is [batch_size, mask_token_num, 1]
  60. next_sentence_labels(Tensor):
  61. The labels of the next sentence prediction task, the dimensionality of `next_sentence_labels`
  62. is equal to `seq_relation_labels`. Its data type should be int64 and
  63. its shape is [batch_size, 1]
  64. masked_lm_scale(Tensor or int):
  65. The scale of masked tokens. Used for the normalization of masked language modeling loss.
  66. If it is a `Tensor`, its data type should be int64 and its shape is equal to `prediction_scores`.
  67. Returns:
  68. Tensor: The pretraining loss, equals to the sum of `masked_lm_loss` plus the mean of `next_sentence_loss`.
  69. Its data type should be float32 and its shape is [1].
  70. """
  71. with paddle.static.amp.fp16_guard():
  72. masked_lm_labels_flat = masked_lm_labels.reshape([-1])
  73. mlm_labels = masked_lm_labels_flat[masked_lm_labels_flat != -1]
  74. masked_lm_loss = self.loss_fn(prediction_scores, mlm_labels)
  75. if next_sentence_labels.ndim == 1:
  76. next_sentence_labels = next_sentence_labels.unsqueeze(axis=-1)
  77. next_sentence_loss = self.loss_fn(seq_relationship_score,
  78. next_sentence_labels)
  79. return masked_lm_loss + next_sentence_loss