Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 36 additions & 50 deletions keras_nlp/models/albert/albert_backbone_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,78 +25,63 @@

class AlbertBackboneTest(tf.test.TestCase, parameterized.TestCase):
def setUp(self):
self.model = AlbertBackbone(
vocabulary_size=1000,
self.backbone = AlbertBackbone(
vocabulary_size=10,
num_layers=2,
num_heads=2,
num_groups=1,
num_inner_repetitions=1,
embedding_dim=16,
hidden_dim=64,
intermediate_dim=128,
max_sequence_length=128,
hidden_dim=2,
intermediate_dim=4,
max_sequence_length=5,
)
self.batch_size = 8
self.input_batch = {
"token_ids": tf.ones(
(self.batch_size, self.model.max_sequence_length), dtype="int32"
),
"segment_ids": tf.ones(
(self.batch_size, self.model.max_sequence_length), dtype="int32"
),
"padding_mask": tf.ones(
(self.batch_size, self.model.max_sequence_length), dtype="int32"
),
"token_ids": tf.ones((2, 5), dtype="int32"),
"segment_ids": tf.ones((2, 5), dtype="int32"),
"padding_mask": tf.ones((2, 5), dtype="int32"),
}

self.input_dataset = tf.data.Dataset.from_tensor_slices(
self.input_batch
).batch(2)

def test_valid_call_albert(self):
self.model(self.input_batch)
self.backbone(self.input_batch)

def test_name(self):
# Check default name passed through
self.assertRegexpMatches(self.model.name, "albert_backbone")
self.assertRegexpMatches(self.backbone.name, "albert_backbone")

def test_variable_sequence_length_call_albert(self):
for seq_length in (25, 50, 75):
for seq_length in (2, 3, 4):
input_data = {
"token_ids": tf.ones(
(self.batch_size, seq_length), dtype="int32"
),
"segment_ids": tf.ones(
(self.batch_size, seq_length), dtype="int32"
),
"padding_mask": tf.ones(
(self.batch_size, seq_length), dtype="int32"
),
"token_ids": tf.ones((2, seq_length), dtype="int32"),
"segment_ids": tf.ones((2, seq_length), dtype="int32"),
"padding_mask": tf.ones((2, seq_length), dtype="int32"),
}
self.model(input_data)
self.backbone(input_data)

@parameterized.named_parameters(
("jit_compile_false", False), ("jit_compile_true", True)
)
def test_compile(self, jit_compile):
self.model.compile(jit_compile=jit_compile)
self.model.predict(self.input_batch)
def test_predict(self):
self.backbone.predict(self.input_batch)
self.backbone.predict(self.input_dataset)

@parameterized.named_parameters(
("jit_compile_false", False), ("jit_compile_true", True)
)
def test_compile_batched_ds(self, jit_compile):
self.model.compile(jit_compile=jit_compile)
self.model.predict(self.input_dataset)
def test_serialization(self):
new_backbone = keras.utils.deserialize_keras_object(
keras.utils.serialize_keras_object(self.backbone)
)
self.assertEqual(new_backbone.get_config(), self.backbone.get_config())

def test_error_for_invalid_num_groups(self):
with self.assertRaises(ValueError):
self.model = AlbertBackbone(
vocabulary_size=1000,
vocabulary_size=10,
num_layers=3,
num_heads=2,
num_groups=2,
num_inner_repetitions=1,
embedding_dim=16,
embedding_dim=4,
hidden_dim=64,
intermediate_dim=128,
)
Expand All @@ -105,10 +90,11 @@ def test_error_for_invalid_num_groups(self):
("tf_format", "tf", "model"),
("keras_format", "keras_v3", "model.keras"),
)
@pytest.mark.large
def test_saved_model(self, save_format, filename):
model_output = self.model(self.input_batch)
model_output = self.backbone(self.input_batch)
save_path = os.path.join(self.get_temp_dir(), filename)
self.model.save(save_path, save_format=save_format)
self.backbone.save(save_path, save_format=save_format)
restored_model = keras.models.load_model(save_path)

# Check we got the real object back.
Expand All @@ -126,16 +112,16 @@ def test_saved_model(self, save_format, filename):
class AlbertBackboneTPUTest(tf.test.TestCase, parameterized.TestCase):
def setUp(self):
with self.tpu_strategy.scope():
self.model = AlbertBackbone(
vocabulary_size=1000,
self.backbone = AlbertBackbone(
vocabulary_size=10,
num_layers=2,
num_heads=2,
num_groups=1,
num_inner_repetitions=1,
embedding_dim=16,
hidden_dim=64,
intermediate_dim=128,
max_sequence_length=128,
hidden_dim=2,
intermediate_dim=2,
max_sequence_length=4,
)

self.input_batch = {
Expand All @@ -148,5 +134,5 @@ def setUp(self):
).batch(2)

def test_predict(self):
self.model.compile()
self.model.predict(self.input_dataset)
self.backbone.compile()
self.backbone.predict(self.input_dataset)
71 changes: 29 additions & 42 deletions keras_nlp/models/albert/albert_classifier_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import io
import os

import pytest
import sentencepiece
import tensorflow as tf
from absl.testing import parameterized
Expand All @@ -29,16 +30,7 @@

class AlbertClassifierTest(tf.test.TestCase, parameterized.TestCase):
def setUp(self):
self.backbone = AlbertBackbone(
vocabulary_size=1000,
num_layers=2,
num_heads=2,
embedding_dim=8,
hidden_dim=64,
intermediate_dim=128,
max_sequence_length=128,
name="encoder",
)
# Setup model

bytes_io = io.BytesIO()
vocab_data = tf.data.Dataset.from_tensor_slices(
Expand All @@ -65,8 +57,18 @@ def setUp(self):

self.preprocessor = AlbertPreprocessor(
tokenizer=tokenizer,
sequence_length=8,
sequence_length=5,
)
self.backbone = AlbertBackbone(
vocabulary_size=self.preprocessor.tokenizer.vocabulary_size(),
num_layers=2,
num_heads=2,
embedding_dim=2,
hidden_dim=2,
intermediate_dim=4,
max_sequence_length=self.preprocessor.packer.sequence_length,
)

self.classifier = AlbertClassifier(
self.backbone,
4,
Expand Down Expand Up @@ -95,54 +97,39 @@ def setUp(self):
def test_valid_call_classifier(self):
self.classifier(self.preprocessed_batch)

@parameterized.named_parameters(
("jit_compile_false", False), ("jit_compile_true", True)
)
def test_albert_classifier_predict(self, jit_compile):
self.classifier.compile(jit_compile=jit_compile)
def test_classifier_predict(self):
self.classifier.predict(self.raw_batch)
self.classifier.preprocessor = None
self.classifier.predict(self.preprocessed_batch)

@parameterized.named_parameters(
("jit_compile_false", False), ("jit_compile_true", True)
)
def test_albert_classifier_predict_no_preprocessing(self, jit_compile):
self.classifier_no_preprocessing.compile(jit_compile=jit_compile)
self.classifier_no_preprocessing.predict(self.preprocessed_batch)

def test_albert_classifier_fit_default_compile(self):
def test_classifier_fit(self):
self.classifier.fit(self.raw_dataset)
self.classifier.preprocessor = None
self.classifier.fit(self.preprocessed_dataset)

@parameterized.named_parameters(
("jit_compile_false", False), ("jit_compile_true", True)
)
def test_albert_classifier_fit(self, jit_compile):
self.classifier.compile(
loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
jit_compile=jit_compile,
)
def test_distilbert_classifier_fit_default_compile(self):
self.classifier.fit(self.raw_dataset)

@parameterized.named_parameters(
("jit_compile_false", False), ("jit_compile_true", True)
)
def test_albert_classifier_fit_no_preprocessing(self, jit_compile):
self.classifier_no_preprocessing.compile(
loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
jit_compile=jit_compile,
def test_serialization(self):
config = keras.utils.serialize_keras_object(self.classifier)
new_classifier = keras.utils.deserialize_keras_object(config)
self.assertEqual(
new_classifier.get_config(),
self.classifier.get_config(),
)
self.classifier_no_preprocessing.fit(self.preprocessed_dataset)

@parameterized.named_parameters(
("tf_format", "tf", "model"),
("keras_format", "keras_v3", "model.keras"),
)
def test_saved_model(self, save_format, filename):
@pytest.mark.large
def test_saving_model(self, save_format, filename):
model_output = self.classifier.predict(self.raw_batch)
save_path = os.path.join(self.get_temp_dir(), filename)
self.classifier.save(save_path, save_format=save_format)
restored_model = keras.models.load_model(save_path)

# Check we got the real object back.
# Check we got the real object back
self.assertIsInstance(restored_model, AlbertClassifier)

# Check that output matches.
Expand Down
10 changes: 10 additions & 0 deletions keras_nlp/models/albert/albert_masked_lm_preprocessor_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import io
import os

import pytest
import sentencepiece
import tensorflow as tf
from absl.testing import parameterized
Expand Down Expand Up @@ -142,10 +143,19 @@ def test_no_masking_zero_rate(self):
self.assertAllEqual(y, [0, 0, 0, 0])
self.assertAllEqual(sw, [0.0, 0.0, 0.0, 0.0])

def test_serialization(self):
config = keras.utils.serialize_keras_object(self.preprocessor)
new_preprocessor = keras.utils.deserialize_keras_object(config)
self.assertEqual(
new_preprocessor.get_config(),
self.preprocessor.get_config(),
)

@parameterized.named_parameters(
("tf_format", "tf", "model"),
("keras_format", "keras_v3", "model.keras"),
)
@pytest.mark.large
def test_saved_model(self, save_format, filename):
input_data = tf.constant(["the quick brown fox"])

Expand Down
Loading