|
| 1 | +# Copyright 2022 The KerasNLP Authors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Tests for FNet preprocessor layer.""" |
| 16 | + |
| 17 | +import io |
| 18 | +import os |
| 19 | + |
| 20 | +import sentencepiece |
| 21 | +import tensorflow as tf |
| 22 | +from absl.testing import parameterized |
| 23 | +from tensorflow import keras |
| 24 | + |
| 25 | +from keras_nlp.models.f_net.f_net_preprocessor import FNetPreprocessor |
| 26 | +from keras_nlp.models.f_net.f_net_tokenizer import FNetTokenizer |
| 27 | + |
| 28 | + |
| 29 | +class FNetPreprocessorTest(tf.test.TestCase, parameterized.TestCase): |
| 30 | + def setUp(self): |
| 31 | + bytes_io = io.BytesIO() |
| 32 | + vocab_data = tf.data.Dataset.from_tensor_slices( |
| 33 | + ["the quick brown fox", "the earth is round"] |
| 34 | + ) |
| 35 | + sentencepiece.SentencePieceTrainer.train( |
| 36 | + sentence_iterator=vocab_data.as_numpy_iterator(), |
| 37 | + model_writer=bytes_io, |
| 38 | + vocab_size=10, |
| 39 | + model_type="WORD", |
| 40 | + pad_id=3, |
| 41 | + unk_id=0, |
| 42 | + bos_id=4, |
| 43 | + eos_id=5, |
| 44 | + pad_piece="<pad>", |
| 45 | + unk_piece="<unk>", |
| 46 | + bos_piece="[CLS]", |
| 47 | + eos_piece="[SEP]", |
| 48 | + ) |
| 49 | + self.proto = bytes_io.getvalue() |
| 50 | + |
| 51 | + self.preprocessor = FNetPreprocessor( |
| 52 | + tokenizer=FNetTokenizer(proto=self.proto), |
| 53 | + sequence_length=12, |
| 54 | + ) |
| 55 | + |
| 56 | + def test_tokenize_strings(self): |
| 57 | + input_data = "the quick brown fox" |
| 58 | + output = self.preprocessor(input_data) |
| 59 | + self.assertAllEqual( |
| 60 | + output["token_ids"], [4, 1, 9, 2, 7, 5, 3, 3, 3, 3, 3, 3] |
| 61 | + ) |
| 62 | + self.assertAllEqual( |
| 63 | + output["segment_ids"], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] |
| 64 | + ) |
| 65 | + |
| 66 | + def test_tokenize_list_of_strings(self): |
| 67 | + # We should handle a list of strings as as batch. |
| 68 | + input_data = ["the quick brown fox"] * 4 |
| 69 | + output = self.preprocessor(input_data) |
| 70 | + self.assertAllEqual( |
| 71 | + output["token_ids"], |
| 72 | + [[4, 1, 9, 2, 7, 5, 3, 3, 3, 3, 3, 3]] * 4, |
| 73 | + ) |
| 74 | + self.assertAllEqual( |
| 75 | + output["segment_ids"], [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]] * 4 |
| 76 | + ) |
| 77 | + |
| 78 | + def test_tokenize_labeled_batch(self): |
| 79 | + x = tf.constant(["the quick brown fox"] * 4) |
| 80 | + y = tf.constant([1] * 4) |
| 81 | + sw = tf.constant([1.0] * 4) |
| 82 | + x_out, y_out, sw_out = self.preprocessor(x, y, sw) |
| 83 | + self.assertAllEqual( |
| 84 | + x_out["token_ids"], |
| 85 | + [[4, 1, 9, 2, 7, 5, 3, 3, 3, 3, 3, 3]] * 4, |
| 86 | + ) |
| 87 | + self.assertAllEqual( |
| 88 | + x_out["segment_ids"], [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]] * 4 |
| 89 | + ) |
| 90 | + self.assertAllEqual(y_out, y) |
| 91 | + self.assertAllEqual(sw_out, sw) |
| 92 | + |
| 93 | + def test_tokenize_labeled_dataset(self): |
| 94 | + x = tf.constant(["the quick brown fox"] * 4) |
| 95 | + y = tf.constant([1] * 4) |
| 96 | + sw = tf.constant([1.0] * 4) |
| 97 | + ds = tf.data.Dataset.from_tensor_slices((x, y, sw)) |
| 98 | + ds = ds.map(self.preprocessor) |
| 99 | + x_out, y_out, sw_out = ds.batch(4).take(1).get_single_element() |
| 100 | + self.assertAllEqual( |
| 101 | + x_out["token_ids"], |
| 102 | + [[4, 1, 9, 2, 7, 5, 3, 3, 3, 3, 3, 3]] * 4, |
| 103 | + ) |
| 104 | + self.assertAllEqual( |
| 105 | + x_out["segment_ids"], [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]] * 4 |
| 106 | + ) |
| 107 | + self.assertAllEqual(y_out, y) |
| 108 | + self.assertAllEqual(sw_out, sw) |
| 109 | + |
| 110 | + def test_tokenize_multiple_sentences(self): |
| 111 | + sentence_one = tf.constant("the quick brown fox") |
| 112 | + sentence_two = tf.constant("the earth") |
| 113 | + output = self.preprocessor((sentence_one, sentence_two)) |
| 114 | + self.assertAllEqual( |
| 115 | + output["token_ids"], |
| 116 | + [4, 1, 9, 2, 7, 5, 1, 6, 5, 3, 3, 3], |
| 117 | + ) |
| 118 | + self.assertAllEqual( |
| 119 | + output["segment_ids"], [0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0] |
| 120 | + ) |
| 121 | + |
| 122 | + def test_tokenize_multiple_batched_sentences(self): |
| 123 | + sentence_one = tf.constant(["the quick brown fox"] * 4) |
| 124 | + sentence_two = tf.constant(["the earth"] * 4) |
| 125 | + # The first tuple or list is always interpreted as an enumeration of |
| 126 | + # separate sequences to concatenate. |
| 127 | + output = self.preprocessor((sentence_one, sentence_two)) |
| 128 | + self.assertAllEqual( |
| 129 | + output["token_ids"], |
| 130 | + [[4, 1, 9, 2, 7, 5, 1, 6, 5, 3, 3, 3]] * 4, |
| 131 | + ) |
| 132 | + self.assertAllEqual( |
| 133 | + output["segment_ids"], [[0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0]] * 4 |
| 134 | + ) |
| 135 | + |
| 136 | + def test_errors_for_2d_list_input(self): |
| 137 | + ambiguous_input = [["one", "two"], ["three", "four"]] |
| 138 | + with self.assertRaises(ValueError): |
| 139 | + self.preprocessor(ambiguous_input) |
| 140 | + |
| 141 | + @parameterized.named_parameters( |
| 142 | + ("tf_format", "tf", "model"), |
| 143 | + ("keras_format", "keras_v3", "model.keras"), |
| 144 | + ) |
| 145 | + def test_saved_model(self, save_format, filename): |
| 146 | + input_data = tf.constant(["the quick brown fox"]) |
| 147 | + inputs = keras.Input(dtype="string", shape=()) |
| 148 | + outputs = self.preprocessor(inputs) |
| 149 | + model = keras.Model(inputs, outputs) |
| 150 | + path = os.path.join(self.get_temp_dir(), filename) |
| 151 | + model.save(path, save_format=save_format) |
| 152 | + restored_model = keras.models.load_model(path) |
| 153 | + self.assertAllEqual( |
| 154 | + model(input_data)["token_ids"], |
| 155 | + restored_model(input_data)["token_ids"], |
| 156 | + ) |
0 commit comments