test_modeling_tf_auto.py 4.99 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
17

import logging
Aymeric Augustin's avatar
Aymeric Augustin committed
18
import unittest
thomwolf's avatar
thomwolf committed
19

20
from transformers import is_tf_available
21

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

24

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


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

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

thomwolf's avatar
thomwolf committed
51
        logging.basicConfig(level=logging.INFO)
52
        # for model_name in list(TF_BERT_PRETRAINED_MODEL_ARCHIVE_MAP.keys())[:1]:
53
        for model_name in ["bert-base-uncased"]:
54
            config = AutoConfig.from_pretrained(model_name)
thomwolf's avatar
thomwolf committed
55
56
57
            self.assertIsNotNone(config)
            self.assertIsInstance(config, BertConfig)

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

thomwolf's avatar
thomwolf committed
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
    @slow
    def test_model_for_pretraining_from_pretrained(self):
        import h5py

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

        logging.basicConfig(level=logging.INFO)
        # for model_name in list(TF_BERT_PRETRAINED_MODEL_ARCHIVE_MAP.keys())[:1]:
        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)

79
    @slow
thomwolf's avatar
thomwolf committed
80
81
    def test_lmhead_model_from_pretrained(self):
        logging.basicConfig(level=logging.INFO)
82
        # for model_name in list(TF_BERT_PRETRAINED_MODEL_ARCHIVE_MAP.keys())[:1]:
83
        for model_name in ["bert-base-uncased"]:
84
            config = AutoConfig.from_pretrained(model_name)
thomwolf's avatar
thomwolf committed
85
86
87
            self.assertIsNotNone(config)
            self.assertIsInstance(config, BertConfig)

88
            model = TFAutoModelWithLMHead.from_pretrained(model_name)
thomwolf's avatar
thomwolf committed
89
90
91
            self.assertIsNotNone(model)
            self.assertIsInstance(model, TFBertForMaskedLM)

92
    @slow
thomwolf's avatar
thomwolf committed
93
94
    def test_sequence_classification_model_from_pretrained(self):
        logging.basicConfig(level=logging.INFO)
95
        # for model_name in list(TF_BERT_PRETRAINED_MODEL_ARCHIVE_MAP.keys())[:1]:
96
        for model_name in ["bert-base-uncased"]:
97
            config = AutoConfig.from_pretrained(model_name)
thomwolf's avatar
thomwolf committed
98
99
100
            self.assertIsNotNone(config)
            self.assertIsInstance(config, BertConfig)

101
            model = TFAutoModelForSequenceClassification.from_pretrained(model_name)
thomwolf's avatar
thomwolf committed
102
103
104
            self.assertIsNotNone(model)
            self.assertIsInstance(model, TFBertForSequenceClassification)

105
    @slow
thomwolf's avatar
thomwolf committed
106
107
    def test_question_answering_model_from_pretrained(self):
        logging.basicConfig(level=logging.INFO)
108
        # for model_name in list(TF_BERT_PRETRAINED_MODEL_ARCHIVE_MAP.keys())[:1]:
109
        for model_name in ["bert-base-uncased"]:
110
            config = AutoConfig.from_pretrained(model_name)
thomwolf's avatar
thomwolf committed
111
112
113
            self.assertIsNotNone(config)
            self.assertIsInstance(config, BertConfig)

114
            model = TFAutoModelForQuestionAnswering.from_pretrained(model_name)
thomwolf's avatar
thomwolf committed
115
116
117
            self.assertIsNotNone(model)
            self.assertIsInstance(model, TFBertForQuestionAnswering)

Julien Chaumond's avatar
Julien Chaumond committed
118
119
    def test_from_pretrained_identifier(self):
        logging.basicConfig(level=logging.INFO)
120
        model = TFAutoModelWithLMHead.from_pretrained(SMALL_MODEL_IDENTIFIER)
Julien Chaumond's avatar
Julien Chaumond committed
121
        self.assertIsInstance(model, TFBertForMaskedLM)
Julien Chaumond's avatar
Julien Chaumond committed
122
123
        self.assertEqual(model.num_parameters(), 14830)
        self.assertEqual(model.num_parameters(only_trainable=True), 14830)
Julien Chaumond's avatar
Julien Chaumond committed
124
125
126
127
128
129
130

    def test_from_identifier_from_model_type(self):
        logging.basicConfig(level=logging.INFO)
        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)