예제 #1
0
    def create_and_check_gpt2_model_past(self, config, input_ids, input_mask, head_mask, token_type_ids, *args):
        model = TFGPT2Model(config=config)

        # first forward pass
        outputs = model(input_ids, token_type_ids=token_type_ids, use_cache=True)
        outputs_use_cache_conf = model(input_ids, token_type_ids=token_type_ids)
        outputs_no_past = model(input_ids, token_type_ids=token_type_ids, use_cache=False)

        self.parent.assertTrue(len(outputs) == len(outputs_use_cache_conf))
        self.parent.assertTrue(len(outputs) == len(outputs_no_past) + 1)

        output, past = outputs

        # create hypothetical next token and extent to next_input_ids
        next_tokens = ids_tensor((self.batch_size, 1), config.vocab_size)
        next_token_types = ids_tensor([self.batch_size, 1], self.type_vocab_size)

        # append to next input_ids and token_type_ids
        next_input_ids = tf.concat([input_ids, next_tokens], axis=-1)
        next_token_type_ids = tf.concat([token_type_ids, next_token_types], axis=-1)

        output_from_no_past, _ = model(next_input_ids, token_type_ids=next_token_type_ids)
        output_from_past, _ = model(next_tokens, token_type_ids=next_token_types, past=past)

        # select random slice
        random_slice_idx = int(ids_tensor((1,), shape_list(output_from_past)[-1]))
        output_from_no_past_slice = output_from_no_past[:, -1, random_slice_idx]
        output_from_past_slice = output_from_past[:, 0, random_slice_idx]

        # test that outputs are equal for slice
        tf.debugging.assert_near(output_from_past_slice, output_from_no_past_slice, rtol=1e-6)
예제 #2
0
    def create_and_check_gpt2_model_attention_mask_past(
            self, config, input_ids, input_mask, head_mask, token_type_ids,
            *args):
        model = TFGPT2Model(config=config)

        # create attention mask
        half_seq_length = self.seq_length // 2
        attn_mask_begin = tf.ones((self.batch_size, half_seq_length),
                                  dtype=tf.int32)
        attn_mask_end = tf.zeros(
            (self.batch_size, self.seq_length - half_seq_length),
            dtype=tf.int32)
        attn_mask = tf.concat([attn_mask_begin, attn_mask_end], axis=1)

        # first forward pass
        output, past = model(input_ids, attention_mask=attn_mask).to_tuple()

        # create hypothetical next token and extent to next_input_ids
        next_tokens = ids_tensor((self.batch_size, 1), config.vocab_size)

        # change a random masked slice from input_ids
        random_seq_idx_to_change = ids_tensor(
            (1, ), half_seq_length).numpy() + 1
        random_other_next_tokens = ids_tensor(
            (self.batch_size, self.seq_length), config.vocab_size)
        vector_condition = tf.range(
            self.seq_length) == (self.seq_length - random_seq_idx_to_change)
        condition = tf.transpose(
            tf.broadcast_to(tf.expand_dims(vector_condition, -1),
                            (self.seq_length, self.batch_size)))
        input_ids = tf.where(condition, random_other_next_tokens, input_ids)

        # append to next input_ids and attn_mask
        next_input_ids = tf.concat([input_ids, next_tokens], axis=-1)
        attn_mask = tf.concat([
            attn_mask,
            tf.ones((shape_list(attn_mask)[0], 1), dtype=tf.int32)
        ],
                              axis=1)

        # get two different outputs
        output_from_no_past = model(
            next_input_ids, attention_mask=attn_mask)["last_hidden_state"]
        output_from_past = model(next_tokens,
                                 past=past,
                                 attention_mask=attn_mask)["last_hidden_state"]

        # select random slice
        random_slice_idx = int(
            ids_tensor((1, ),
                       shape_list(output_from_past)[-1]))
        output_from_no_past_slice = output_from_no_past[:, -1,
                                                        random_slice_idx]
        output_from_past_slice = output_from_past[:, 0, random_slice_idx]

        # test that outputs are equal for slice
        tf.debugging.assert_near(output_from_past_slice,
                                 output_from_no_past_slice,
                                 rtol=1e-12)
예제 #3
0
 def test_model_from_pretrained(self):
     cache_dir = "/tmp/transformers_test/"
     for model_name in list(
             TF_gpt2_PRETRAINED_MODEL_ARCHIVE_MAP.keys())[:1]:
         model = TFGPT2Model.from_pretrained(model_name,
                                             cache_dir=cache_dir)
         shutil.rmtree(cache_dir)
         self.assertIsNotNone(model)
    def create_and_check_gpt2_model(self, config, input_ids, input_mask, head_mask, token_type_ids, *args):
        model = TFGPT2Model(config=config)
        inputs = {
            "input_ids": input_ids,
            "attention_mask": input_mask,
            "token_type_ids": token_type_ids,
        }
        result = model(inputs)

        inputs = [input_ids, None, input_mask]  # None is the input for 'past'
        result = model(inputs)

        result = model(input_ids)

        self.parent.assertEqual(result.last_hidden_state.shape, (self.batch_size, self.seq_length, self.hidden_size))
        def create_and_check_gpt2_model(self, config, input_ids, input_mask, head_mask, token_type_ids, *args):
            model = TFGPT2Model(config=config)
            inputs = {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
            sequence_output = model(inputs)[0]

            inputs = [input_ids, None, input_mask]  # None is the input for 'past'
            sequence_output = model(inputs)[0]

            sequence_output = model(input_ids)[0]

            result = {
                "sequence_output": sequence_output.numpy(),
            }
            self.parent.assertListEqual(
                list(result["sequence_output"].shape), [self.batch_size, self.seq_length, self.hidden_size]
            )
    def create_and_check_gpt2_model_past_large_inputs(self, config, input_ids,
                                                      input_mask, head_mask,
                                                      token_type_ids, *args):
        model = TFGPT2Model(config=config)

        # first forward pass
        outputs = model(input_ids,
                        token_type_ids=token_type_ids,
                        use_cache=True)

        output, past = outputs.to_tuple()

        # create hypothetical next token and extent to next_input_ids
        next_tokens = ids_tensor((self.batch_size, 3), config.vocab_size)
        next_token_types = ids_tensor((self.batch_size, 3),
                                      self.type_vocab_size)

        # append to next input_ids and token_type_ids
        next_input_ids = tf.concat([input_ids, next_tokens], axis=-1)
        next_token_type_ids = tf.concat([token_type_ids, next_token_types],
                                        axis=-1)

        output_from_no_past = model(
            next_input_ids,
            token_type_ids=next_token_type_ids)["last_hidden_state"]
        output_from_past = model(next_tokens,
                                 token_type_ids=next_token_types,
                                 past=past)["last_hidden_state"]
        self.parent.assertTrue(
            output_from_past.shape[1] == next_tokens.shape[1])

        # select random slice
        random_slice_idx = int(
            ids_tensor((1, ),
                       shape_list(output_from_past)[-1]))
        output_from_no_past_slice = output_from_no_past[:, -3:,
                                                        random_slice_idx]
        output_from_past_slice = output_from_past[:, :, random_slice_idx]

        # test that outputs are equal for slice
        tf.debugging.assert_near(output_from_past_slice,
                                 output_from_no_past_slice,
                                 rtol=1e-6)
예제 #7
0
 def test_model_from_pretrained(self):
     for model_name in TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         model = TFGPT2Model.from_pretrained(model_name)
         self.assertIsNotNone(model)
 def test_model_from_pretrained(self):
     for model_name in list(
             TF_GPT2_PRETRAINED_MODEL_ARCHIVE_MAP.keys())[:1]:
         model = TFGPT2Model.from_pretrained(model_name,
                                             cache_dir=CACHE_DIR)
         self.assertIsNotNone(model)