Exemplo n.º 1
0
    def test_splinter_pretraining_loss(self):
        model = SplinterForPreTraining.from_pretrained(
            "tau/splinter-base-qass")

        # Input: "[CLS] [QUESTION] was born in [QUESTION] . Brad returned to the United Kingdom later . [SEP]"
        # Output should be the spans "Brad" and "the United Kingdom"
        input_ids = torch.tensor([
            [
                101, 104, 1108, 1255, 1107, 104, 119, 7796, 1608, 1106, 1103,
                1244, 2325, 1224, 119, 102
            ],
            [
                101, 104, 1108, 1255, 1107, 104, 119, 7796, 1608, 1106, 1103,
                1244, 2325, 1224, 119, 102
            ],
        ])
        start_positions = torch.tensor([[7, 10], [7, 10]], dtype=torch.long)
        end_positions = torch.tensor([[7, 12], [7, 12]], dtype=torch.long)
        question_positions = torch.tensor([[1, 5], [1, 5]], dtype=torch.long)
        output = model(
            input_ids,
            start_positions=start_positions,
            end_positions=end_positions,
            question_positions=question_positions,
        )
        self.assertAlmostEqual(output.loss.item(), 0.0024, 4)
Exemplo n.º 2
0
 def create_and_check_for_pretraining(
     self,
     config,
     input_ids,
     token_type_ids,
     input_mask,
     start_positions,
     end_positions,
     question_positions,
 ):
     model = SplinterForPreTraining(config=config)
     model.to(torch_device)
     model.eval()
     result = model(
         input_ids,
         attention_mask=input_mask,
         token_type_ids=token_type_ids,
         start_positions=start_positions,
         end_positions=end_positions,
         question_positions=question_positions,
     )
     self.parent.assertEqual(
         result.start_logits.shape,
         (self.batch_size, self.num_questions, self.seq_length))
     self.parent.assertEqual(
         result.end_logits.shape,
         (self.batch_size, self.num_questions, self.seq_length))
Exemplo n.º 3
0
    def test_splinter_pretraining_prepare_question_positions(self):
        model = SplinterForPreTraining.from_pretrained("tau/splinter-base-qass")

        input_ids = torch.tensor(
            [
                [101, 104, 1, 2, 104, 3, 4, 102],
                [101, 1, 104, 2, 104, 3, 104, 102],
                [101, 1, 2, 104, 104, 3, 4, 102],
                [101, 1, 2, 3, 4, 5, 104, 102],
            ]
        )
        question_positions = torch.tensor([[1, 4, 0], [2, 4, 6], [3, 4, 0], [6, 0, 0]], dtype=torch.long)
        output_without_positions = model(input_ids)
        output_with_positions = model(input_ids, question_positions=question_positions)
        self.assertTrue((output_without_positions.start_logits == output_with_positions.start_logits).all())
        self.assertTrue((output_without_positions.end_logits == output_with_positions.end_logits).all())
Exemplo n.º 4
0
    def test_splinter_pretraining_loss_requires_question_positions(self):
        model = SplinterForPreTraining.from_pretrained("tau/splinter-base-qass")

        # Input: "[CLS] [QUESTION] was born in [QUESTION] . Brad returned to the United Kingdom later . [SEP]"
        # Output should be the spans "Brad" and "the United Kingdom"
        input_ids = torch.tensor(
            [[101, 104, 1108, 1255, 1107, 104, 119, 7796, 1608, 1106, 1103, 1244, 2325, 1224, 119, 102]]
        )
        start_positions = torch.tensor([[7, 10]], dtype=torch.long)
        end_positions = torch.tensor([7, 12], dtype=torch.long)
        with self.assertRaises(TypeError):
            model(
                input_ids,
                start_positions=start_positions,
                end_positions=end_positions,
            )
Exemplo n.º 5
0
    def test_splinter_pretraining(self):
        model = SplinterForPreTraining.from_pretrained("tau/splinter-base-qass")

        # Input: "[CLS] [QUESTION] was born in [QUESTION] . Brad returned to the United Kingdom later . [SEP]"
        # Output should be the spans "Brad" and "the United Kingdom"
        input_ids = torch.tensor(
            [[101, 104, 1108, 1255, 1107, 104, 119, 7796, 1608, 1106, 1103, 1244, 2325, 1224, 119, 102]]
        )
        question_positions = torch.tensor([[1, 5]], dtype=torch.long)
        output = model(input_ids, question_positions=question_positions)

        expected_shape = torch.Size((1, 2, 16))
        self.assertEqual(output.start_logits.shape, expected_shape)
        self.assertEqual(output.end_logits.shape, expected_shape)

        self.assertEqual(torch.argmax(output.start_logits[0, 0]), 7)
        self.assertEqual(torch.argmax(output.end_logits[0, 0]), 7)
        self.assertEqual(torch.argmax(output.start_logits[0, 1]), 10)
        self.assertEqual(torch.argmax(output.end_logits[0, 1]), 12)
Exemplo n.º 6
0
    def test_splinter_pretraining_loss_with_padding(self):
        model = SplinterForPreTraining.from_pretrained(
            "tau/splinter-base-qass")

        # Input: "[CLS] [QUESTION] was born in [QUESTION] . Brad returned to the United Kingdom later . [SEP]"
        # Output should be the spans "Brad" and "the United Kingdom"
        input_ids = torch.tensor([
            [
                101, 104, 1108, 1255, 1107, 104, 119, 7796, 1608, 1106, 1103,
                1244, 2325, 1224, 119, 102
            ],
        ])
        start_positions = torch.tensor([[7, 10]], dtype=torch.long)
        end_positions = torch.tensor([7, 12], dtype=torch.long)
        question_positions = torch.tensor([[1, 5]], dtype=torch.long)
        start_positions_with_padding = torch.tensor([[7, 10, 0]],
                                                    dtype=torch.long)
        end_positions_with_padding = torch.tensor([7, 12, 0], dtype=torch.long)
        question_positions_with_padding = torch.tensor([[1, 5, 0]],
                                                       dtype=torch.long)
        output = model(
            input_ids,
            start_positions=start_positions,
            end_positions=end_positions,
            question_positions=question_positions,
        )
        output_with_padding = model(
            input_ids,
            start_positions=start_positions_with_padding,
            end_positions=end_positions_with_padding,
            question_positions=question_positions_with_padding,
        )

        self.assertAlmostEqual(output.loss.item(),
                               output_with_padding.loss.item(), 4)

        # Note that the original code uses 0 to denote padded question tokens
        # and their start and end positions. As the pad_token_id of the model's
        # config is used for the losse's ignore_index in SplinterForPreTraining,
        # we add this test to ensure anybody making changes to the default
        # value of the config, will be aware of the implication.
        self.assertEqual(model.config.pad_token_id, 0)