"docs/en/output_files.md" did not exist on "cf704253f01908f5702650b71da7f65dc5d044e0"
test_modeling_tf_auto.py 3.72 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

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

24

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


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

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

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

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

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

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

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

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

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

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

Julien Chaumond's avatar
Julien Chaumond committed
98
99
    def test_from_pretrained_identifier(self):
        logging.basicConfig(level=logging.INFO)
100
        model = TFAutoModelWithLMHead.from_pretrained(SMALL_MODEL_IDENTIFIER)
Julien Chaumond's avatar
Julien Chaumond committed
101
        self.assertIsInstance(model, TFBertForMaskedLM)