test_modeling_tf_auto.py 4.59 KB
Newer Older
thomwolf's avatar
thomwolf committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# coding=utf-8
# Copyright 2018 The Google AI Language Team Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
Aymeric Augustin's avatar
Aymeric Augustin committed
15

thomwolf's avatar
thomwolf committed
16

Aymeric Augustin's avatar
Aymeric Augustin committed
17
import unittest
thomwolf's avatar
thomwolf committed
18

19
from transformers import is_tf_available
20

Julien Chaumond's avatar
Julien Chaumond committed
21
from .utils import DUMMY_UNKWOWN_IDENTIFIER, SMALL_MODEL_IDENTIFIER, require_tf, slow
Aymeric Augustin's avatar
Aymeric Augustin committed
22

23

24
if is_tf_available():
25
26
27
28
29
    from transformers import (
        AutoConfig,
        BertConfig,
        TFAutoModel,
        TFBertModel,
thomwolf's avatar
thomwolf committed
30
31
        TFAutoModelForPreTraining,
        TFBertForPreTraining,
32
33
        TFAutoModelWithLMHead,
        TFBertForMaskedLM,
Julien Chaumond's avatar
Julien Chaumond committed
34
        TFRobertaForMaskedLM,
35
36
37
38
39
        TFAutoModelForSequenceClassification,
        TFBertForSequenceClassification,
        TFAutoModelForQuestionAnswering,
        TFBertForQuestionAnswering,
    )
thomwolf's avatar
thomwolf committed
40
41


42
@require_tf
thomwolf's avatar
thomwolf committed
43
class TFAutoModelTest(unittest.TestCase):
44
    @slow
thomwolf's avatar
thomwolf committed
45
    def test_model_from_pretrained(self):
thomwolf's avatar
thomwolf committed
46
        import h5py
47

thomwolf's avatar
thomwolf committed
48
49
        self.assertTrue(h5py.version.hdf5_version.startswith("1.10"))

50
        # for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
51
        for model_name in ["bert-base-uncased"]:
52
            config = AutoConfig.from_pretrained(model_name)
thomwolf's avatar
thomwolf committed
53
54
55
            self.assertIsNotNone(config)
            self.assertIsInstance(config, BertConfig)

56
            model = TFAutoModel.from_pretrained(model_name)
thomwolf's avatar
thomwolf committed
57
58
59
            self.assertIsNotNone(model)
            self.assertIsInstance(model, TFBertModel)

thomwolf's avatar
thomwolf committed
60
61
62
63
64
65
    @slow
    def test_model_for_pretraining_from_pretrained(self):
        import h5py

        self.assertTrue(h5py.version.hdf5_version.startswith("1.10"))

66
        # for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
thomwolf's avatar
thomwolf committed
67
68
69
70
71
72
73
74
75
        for model_name in ["bert-base-uncased"]:
            config = AutoConfig.from_pretrained(model_name)
            self.assertIsNotNone(config)
            self.assertIsInstance(config, BertConfig)

            model = TFAutoModelForPreTraining.from_pretrained(model_name)
            self.assertIsNotNone(model)
            self.assertIsInstance(model, TFBertForPreTraining)

76
    @slow
thomwolf's avatar
thomwolf committed
77
    def test_lmhead_model_from_pretrained(self):
78
        # for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
79
        for model_name in ["bert-base-uncased"]:
80
            config = AutoConfig.from_pretrained(model_name)
thomwolf's avatar
thomwolf committed
81
82
83
            self.assertIsNotNone(config)
            self.assertIsInstance(config, BertConfig)

84
            model = TFAutoModelWithLMHead.from_pretrained(model_name)
thomwolf's avatar
thomwolf committed
85
86
87
            self.assertIsNotNone(model)
            self.assertIsInstance(model, TFBertForMaskedLM)

88
    @slow
thomwolf's avatar
thomwolf committed
89
    def test_sequence_classification_model_from_pretrained(self):
90
        # for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
91
        for model_name in ["bert-base-uncased"]:
92
            config = AutoConfig.from_pretrained(model_name)
thomwolf's avatar
thomwolf committed
93
94
95
            self.assertIsNotNone(config)
            self.assertIsInstance(config, BertConfig)

96
            model = TFAutoModelForSequenceClassification.from_pretrained(model_name)
thomwolf's avatar
thomwolf committed
97
98
99
            self.assertIsNotNone(model)
            self.assertIsInstance(model, TFBertForSequenceClassification)

100
    @slow
thomwolf's avatar
thomwolf committed
101
    def test_question_answering_model_from_pretrained(self):
102
        # for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
103
        for model_name in ["bert-base-uncased"]:
104
            config = AutoConfig.from_pretrained(model_name)
thomwolf's avatar
thomwolf committed
105
106
107
            self.assertIsNotNone(config)
            self.assertIsInstance(config, BertConfig)

108
            model = TFAutoModelForQuestionAnswering.from_pretrained(model_name)
thomwolf's avatar
thomwolf committed
109
110
111
            self.assertIsNotNone(model)
            self.assertIsInstance(model, TFBertForQuestionAnswering)

Julien Chaumond's avatar
Julien Chaumond committed
112
    def test_from_pretrained_identifier(self):
113
        model = TFAutoModelWithLMHead.from_pretrained(SMALL_MODEL_IDENTIFIER)
Julien Chaumond's avatar
Julien Chaumond committed
114
        self.assertIsInstance(model, TFBertForMaskedLM)
Julien Chaumond's avatar
Julien Chaumond committed
115
116
        self.assertEqual(model.num_parameters(), 14830)
        self.assertEqual(model.num_parameters(only_trainable=True), 14830)
Julien Chaumond's avatar
Julien Chaumond committed
117
118
119
120
121
122

    def test_from_identifier_from_model_type(self):
        model = TFAutoModelWithLMHead.from_pretrained(DUMMY_UNKWOWN_IDENTIFIER)
        self.assertIsInstance(model, TFRobertaForMaskedLM)
        self.assertEqual(model.num_parameters(), 14830)
        self.assertEqual(model.num_parameters(only_trainable=True), 14830)