test_modeling_tf_auto.py 4.27 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
31
32
    from transformers import (
        AutoConfig,
        BertConfig,
        TFAutoModel,
        TFBertModel,
        TFAutoModelWithLMHead,
        TFBertForMaskedLM,
Julien Chaumond's avatar
Julien Chaumond committed
33
        TFRobertaForMaskedLM,
34
35
36
37
38
        TFAutoModelForSequenceClassification,
        TFBertForSequenceClassification,
        TFAutoModelForQuestionAnswering,
        TFBertForQuestionAnswering,
    )
thomwolf's avatar
thomwolf committed
39
40


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

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

thomwolf's avatar
thomwolf committed
49
        logging.basicConfig(level=logging.INFO)
50
        # for model_name in list(TF_BERT_PRETRAINED_MODEL_ARCHIVE_MAP.keys())[: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)

60
    @slow
thomwolf's avatar
thomwolf committed
61
62
    def test_lmhead_model_from_pretrained(self):
        logging.basicConfig(level=logging.INFO)
63
        # for model_name in list(TF_BERT_PRETRAINED_MODEL_ARCHIVE_MAP.keys())[:1]:
64
        for model_name in ["bert-base-uncased"]:
65
            config = AutoConfig.from_pretrained(model_name)
thomwolf's avatar
thomwolf committed
66
67
68
            self.assertIsNotNone(config)
            self.assertIsInstance(config, BertConfig)

69
            model = TFAutoModelWithLMHead.from_pretrained(model_name)
thomwolf's avatar
thomwolf committed
70
71
72
            self.assertIsNotNone(model)
            self.assertIsInstance(model, TFBertForMaskedLM)

73
    @slow
thomwolf's avatar
thomwolf committed
74
75
    def test_sequence_classification_model_from_pretrained(self):
        logging.basicConfig(level=logging.INFO)
76
        # for model_name in list(TF_BERT_PRETRAINED_MODEL_ARCHIVE_MAP.keys())[:1]:
77
        for model_name in ["bert-base-uncased"]:
78
            config = AutoConfig.from_pretrained(model_name)
thomwolf's avatar
thomwolf committed
79
80
81
            self.assertIsNotNone(config)
            self.assertIsInstance(config, BertConfig)

82
            model = TFAutoModelForSequenceClassification.from_pretrained(model_name)
thomwolf's avatar
thomwolf committed
83
84
85
            self.assertIsNotNone(model)
            self.assertIsInstance(model, TFBertForSequenceClassification)

86
    @slow
thomwolf's avatar
thomwolf committed
87
88
    def test_question_answering_model_from_pretrained(self):
        logging.basicConfig(level=logging.INFO)
89
        # for model_name in list(TF_BERT_PRETRAINED_MODEL_ARCHIVE_MAP.keys())[:1]:
90
        for model_name in ["bert-base-uncased"]:
91
            config = AutoConfig.from_pretrained(model_name)
thomwolf's avatar
thomwolf committed
92
93
94
            self.assertIsNotNone(config)
            self.assertIsInstance(config, BertConfig)

95
            model = TFAutoModelForQuestionAnswering.from_pretrained(model_name)
thomwolf's avatar
thomwolf committed
96
97
98
            self.assertIsNotNone(model)
            self.assertIsInstance(model, TFBertForQuestionAnswering)

Julien Chaumond's avatar
Julien Chaumond committed
99
100
    def test_from_pretrained_identifier(self):
        logging.basicConfig(level=logging.INFO)
101
        model = TFAutoModelWithLMHead.from_pretrained(SMALL_MODEL_IDENTIFIER)
Julien Chaumond's avatar
Julien Chaumond committed
102
        self.assertIsInstance(model, TFBertForMaskedLM)
Julien Chaumond's avatar
Julien Chaumond committed
103
104
        self.assertEqual(model.num_parameters(), 14830)
        self.assertEqual(model.num_parameters(only_trainable=True), 14830)
Julien Chaumond's avatar
Julien Chaumond committed
105
106
107
108
109
110
111

    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)