Skip to content

Commit 9fd1f56

Browse files
authored
[No merge] TF integration testing (#7621)
* stash * TF Integration testing for ELECTRA, BERT, Longformer * Trigger slow tests * Apply suggestions from code review
1 parent 8fe6629 commit 9fd1f56

5 files changed

+65
-3
lines changed

src/transformers/modeling_electra.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ def __init__(self, config):
514514
def forward(self, discriminator_hidden_states):
515515
hidden_states = self.dense(discriminator_hidden_states)
516516
hidden_states = get_activation(self.config.hidden_act)(hidden_states)
517-
logits = self.dense_prediction(hidden_states).squeeze()
517+
logits = self.dense_prediction(hidden_states).squeeze(-1)
518518

519519
return logits
520520

src/transformers/modeling_tf_electra.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ def __init__(self, config, **kwargs):
425425
def call(self, discriminator_hidden_states, training=False):
426426
hidden_states = self.dense(discriminator_hidden_states)
427427
hidden_states = get_tf_activation(self.config.hidden_act)(hidden_states)
428-
logits = tf.squeeze(self.dense_prediction(hidden_states))
428+
logits = tf.squeeze(self.dense_prediction(hidden_states), -1)
429429

430430
return logits
431431

tests/test_modeling_tf_bert.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import unittest
1818

1919
from transformers import BertConfig, is_tf_available
20-
from transformers.testing_utils import require_tf
20+
from transformers.testing_utils import require_tf, slow
2121

2222
from .test_configuration_common import ConfigTester
2323
from .test_modeling_tf_common import TFModelTesterMixin, ids_tensor
@@ -328,3 +328,27 @@ def test_custom_load_tf_weights(self):
328328
self.assertEqual(sorted(output_loading_info["unexpected_keys"]), ["mlm___cls", "nsp___cls"])
329329
for layer in output_loading_info["missing_keys"]:
330330
self.assertTrue(layer.split("_")[0] in ["dropout", "classifier"])
331+
332+
333+
class TFBertModelIntegrationTest(unittest.TestCase):
334+
@slow
335+
def test_inference_masked_lm(self):
336+
model = TFBertForPreTraining.from_pretrained("lysandre/tiny-bert-random")
337+
input_ids = tf.constant([[0, 1, 2, 3, 4, 5]])
338+
output = model(input_ids)[0]
339+
340+
expected_shape = [1, 6, 10]
341+
self.assertEqual(output.shape, expected_shape)
342+
343+
print(output[:, :3, :3])
344+
345+
expected_slice = tf.constant(
346+
[
347+
[
348+
[0.03706957, 0.10124919, 0.03616843],
349+
[-0.06099961, 0.02266058, 0.00601412],
350+
[-0.06066202, 0.05684517, 0.02038802],
351+
]
352+
]
353+
)
354+
tf.debugging.assert_near(output[:, :3, :3], expected_slice, atol=1e-4)

tests/test_modeling_tf_electra.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,3 +248,19 @@ def test_model_from_pretrained(self):
248248
for model_name in ["google/electra-small-discriminator"]:
249249
model = TFElectraModel.from_pretrained(model_name)
250250
self.assertIsNotNone(model)
251+
252+
253+
class TFElectraModelIntegrationTest(unittest.TestCase):
254+
@slow
255+
def test_inference_masked_lm(self):
256+
model = TFElectraForPreTraining.from_pretrained("lysandre/tiny-electra-random")
257+
input_ids = tf.constant([[0, 1, 2, 3, 4, 5]])
258+
output = model(input_ids)[0]
259+
260+
expected_shape = [1, 6]
261+
self.assertEqual(output.shape, expected_shape)
262+
263+
print(output[:, :3])
264+
265+
expected_slice = tf.constant([[-0.24651965, 0.8835437, 1.823782]])
266+
tf.debugging.assert_near(output[:, :3], expected_slice, atol=1e-4)

tests/test_modeling_tf_longformer.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,3 +622,25 @@ def test_inference_masked_lm_long(self):
622622
tf.debugging.assert_near(tf.reduce_mean(loss), expected_loss, rtol=1e-4)
623623
tf.debugging.assert_near(tf.reduce_sum(prediction_scores), expected_prediction_scores_sum, rtol=1e-4)
624624
tf.debugging.assert_near(tf.reduce_mean(prediction_scores), expected_prediction_scores_mean, rtol=1e-4)
625+
626+
@slow
627+
def test_inference_masked_lm(self):
628+
model = TFLongformerForMaskedLM.from_pretrained("lysandre/tiny-longformer-random")
629+
input_ids = tf.constant([[0, 1, 2, 3, 4, 5]])
630+
output = model(input_ids)[0]
631+
632+
expected_shape = [1, 6, 10]
633+
self.assertEqual(output.shape, expected_shape)
634+
635+
print(output[:, :3, :3])
636+
637+
expected_slice = tf.constant(
638+
[
639+
[
640+
[-0.04926379, 0.0367098, 0.02099686],
641+
[0.03940692, 0.01547744, -0.01448723],
642+
[0.03495252, -0.05900355, -0.01675752],
643+
]
644+
]
645+
)
646+
tf.debugging.assert_near(output[:, :3, :3], expected_slice, atol=1e-4)

0 commit comments

Comments
 (0)