Repository: keras-team/keras-nlp

Issue:
RobertaPreprocessor should handle a list of strings as a batch
Once #504 lands, we should mirror changes for Roberta.


Discussion:

Resolving patch:
diff --git a/keras_nlp/models/roberta/roberta_preprocessor.py b/keras_nlp/models/roberta/roberta_preprocessor.py
--- a/keras_nlp/models/roberta/roberta_preprocessor.py
+++ b/keras_nlp/models/roberta/roberta_preprocessor.py
@@ -22,6 +22,10 @@
 
 from keras_nlp.models.roberta.roberta_presets import backbone_presets
 from keras_nlp.models.roberta.roberta_tokenizer import RobertaTokenizer
+from keras_nlp.utils.keras_utils import (
+    convert_inputs_to_list_of_tensor_segments,
+)
+from keras_nlp.utils.keras_utils import pack_x_y_sample_weight
 from keras_nlp.utils.python_utils import classproperty
 from keras_nlp.utils.python_utils import format_docstring
 
@@ -32,17 +36,30 @@ class RobertaPreprocessor(keras.layers.Layer):
 
     This preprocessing layer will do three things:
 
-    - Tokenize any number of inputs using `tokenizer`.
+    - Tokenize any number of input segments using the `tokenizer`.
     - Pack the inputs together with the appropriate `"
"`, `"
"` and
       `"<pad>"` tokens, i.e., adding a single `"
"` at the start of the
       entire sequence, `"

"` at the end of each segment, save the last
       and a `"
"` at the end of the entire sequence.
-    - Construct a dictionary with keys `"token_ids"` and `"padding_mask"` that
-      can be passed directly to a RoBERTa model.
-
-    This layer will accept either a tuple of (possibly batched) inputs, or a
-    single input tensor. If a single tensor is passed, it will be packed
-    equivalently to a tuple with a single element.
+    - Construct a dictionary with keys `"token_ids"`, `"segment_ids"`,
+       `"padding_mask"`, that can be passed directly to a RoBERTa model.
+
+    This layer can be used directly with `tf.data.Dataset.map` to preprocess
+    string data in the `(x, y, sample_weight)` format used by
+    `keras.Model.fit`.
+
+    The call method of this layer accepts three arguments, `x`, `y`, and
+    `sample_weight`. `x` can be a python string or tensor representing a single
+    segment, a list of python strings representing a batch of single segments,
+    or a list of tensors representing multiple segments to be packed together.
+    `y` and `sample_weight` are both optional, can have any format, and will be
+    passed through unaltered.
+
+    Special care should be taken when using `tf.data` to map over an unlabeled
+    tuple of string segments. `tf.data.Dataset.map` will unpack this tuple
+    directly into the call arguments of this layer, rather than forward all
+    argument to `x`. To handle this case, it is recommended to  explicitly call
+    the layer, e.g. `ds.map(lambda seg1, seg2: preprocessor(x=(seg1, seg2)))`.
 
     Args:
         tokenizer: A `keras_nlp.models.RobertaTokenizer` instance.
@@ -87,32 +104,52 @@ class RobertaPreprocessor(keras.layers.Layer):
         sequence_length=20,
     )
 
-    # Tokenize and pack a single sentence directly.
+    # Tokenize and pack a single sentence.
+    sentence = tf.constant(" afternoon sun")
+    preprocessor(sentence)
+    # Same output.
     preprocessor(" afternoon sun")
 
-    # Tokenize and pack multiples sentences directly.
-    preprocessor((" afternoon sun", "refulgent sun"))
+    # Tokenize and a batch of single sentences.
+    sentences = tf.constant(
+        [" afternoon sun", " night moon"]
+    )
+    preprocessor(sentences)
+    # Same output.
+    preprocessor(
+        [" afternoon sun", " night moon"]
+    )
 
-    # Map a dataset to preprocess a single sentence at a time.
-    features = [" afternoon sun", "refulgent sun"]
-    labels = [0, 1]
-    ds = tf.data.Dataset.from_tensor_slices((features, labels))
-    ds = ds.map(
-        lambda x, y: (preprocessor(x), y),
-        num_parallel_calls=tf.data.AUTOTUNE,
+    # Tokenize and pack a sentence pair.
+    first_sentence = tf.constant(" afternoon sun")
+    second_sentence = tf.constant("refulgent sun")
+    preprocessor((first_sentence, second_sentence))
+
+    # Map a dataset to preprocess a single sentence.
+    features = tf.constant(
+        [" afternoon sun", " night moon"]
     )
+    labels = tf.constant([0, 1])
+    ds = tf.data.Dataset.from_tensor_slices((features, labels))
+    ds = ds.map(preprocessor, num_parallel_calls=tf.data.AUTOTUNE)
 
-    # Map a dataset to preprocess multiple sentences.
-    first_sentences = [" afternoon sun", "refulgent sun"]
-    second_sentences = [" night moon", " bright moon"]
-    labels = [1, 1]
+    # Map a dataset to preprocess sentence pairs.
+    first_sentences = tf.constant([" afternoon sun", " night moon"])
+    second_sentences = tf.constant(["refulgent sun", " bright moon"])
+    labels = tf.constant([1, 1])
     ds = tf.data.Dataset.from_tensor_slices(
         (
             (first_sentences, second_sentences), labels
         )
     )
+    ds = ds.map(preprocessor, num_parallel_calls=tf.data.AUTOTUNE)
+
+    # Map a dataset to preprocess unlabeled sentence pairs.
+    ds = tf.data.Dataset.from_tensor_slices((first_sentences, second_sentences))
+    # Watch out for tf.data's default unpacking of tuples here!
+    # Best to invoke the `preprocessor` directly in this case.
     ds = ds.map(
-        lambda x, y: (preprocessor(x), y),
+        lambda s1, s2: preprocessor(x=(s1, s2)),
         num_parallel_calls=tf.data.AUTOTUNE,
     )
     ```
@@ -159,16 +196,15 @@ def from_config(cls, config):
             config["tokenizer"] = keras.layers.deserialize(config["tokenizer"])
         return cls(**config)
 
-    def call(self, inputs):
-        if not isinstance(inputs, (list, tuple)):
-            inputs = [inputs]
-
-        inputs = [self.tokenizer(x) for x in inputs]
-        token_ids = self.packer(inputs)
-        return {
+    def call(self, x, y=None, sample_weight=None):
+        x = convert_inputs_to_list_of_tensor_segments(x)
+        x = [self.tokenizer(segment) for segment in x]
+        token_ids = self.packer(x)
+        x = {
             "token_ids": token_ids,
             "padding_mask": token_ids != self.tokenizer.pad_token_id,
         }
+        return pack_x_y_sample_weight(x, y, sample_weight)
 
     @classproperty
     def presets(cls):


Test patch:
diff --git a/keras_nlp/models/roberta/roberta_preprocessor_test.py b/keras_nlp/models/roberta/roberta_preprocessor_test.py
--- a/keras_nlp/models/roberta/roberta_preprocessor_test.py
+++ b/keras_nlp/models/roberta/roberta_preprocessor_test.py
@@ -54,8 +54,8 @@ def setUp(self):
             sequence_length=12,
         )
 
-    def test_tokenize(self):
-        input_data = [" airplane at airport"]
+    def test_tokenize_strings(self):
+        input_data = " airplane at airport"
 
         output = self.preprocessor(input_data)
         self.assertAllEqual(
@@ -65,15 +65,8 @@ def test_tokenize(self):
             output["padding_mask"], [1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0]
         )
 
-    def test_tokenize_batch(self):
-        input_data = tf.constant(
-            [
-                " airplane at airport",
-                " airplane at airport",
-                " airplane at airport",
-                " airplane at airport",
-            ]
-        )
+    def test_tokenize_list_of_strings(self):
+        input_data = [" airplane at airport"] * 4
 
         output = self.preprocessor(input_data)
         self.assertAllEqual(
@@ -85,9 +78,39 @@ def test_tokenize_batch(self):
             output["padding_mask"], [[1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0]] * 4
         )
 
+    def test_tokenize_labeled_batch(self):
+        x = tf.constant([" airplane at airport"] * 4)
+        y = tf.constant([1] * 4)
+        sw = tf.constant([1.0] * 4)
+        x_out, y_out, sw_out = self.preprocessor(x, y, sw)
+        self.assertAllEqual(
+            x_out["token_ids"], [[0, 3, 4, 5, 3, 6, 2, 1, 1, 1, 1, 1]] * 4
+        )
+        self.assertAllEqual(
+            x_out["padding_mask"], [[1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0]] * 4
+        )
+        self.assertAllEqual(y_out, y)
+        self.assertAllEqual(sw_out, sw)
+
+    def test_tokenize_labeled_dataset(self):
+        x = tf.constant([" airplane at airport"] * 4)
+        y = tf.constant([1] * 4)
+        sw = tf.constant([1.0] * 4)
+        ds = tf.data.Dataset.from_tensor_slices((x, y, sw))
+        ds = ds.map(self.preprocessor)
+        x_out, y_out, sw_out = ds.batch(4).take(1).get_single_element()
+        self.assertAllEqual(
+            x_out["token_ids"], [[0, 3, 4, 5, 3, 6, 2, 1, 1, 1, 1, 1]] * 4
+        )
+        self.assertAllEqual(
+            x_out["padding_mask"], [[1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0]] * 4
+        )
+        self.assertAllEqual(y_out, y)
+        self.assertAllEqual(sw_out, sw)
+
     def test_tokenize_multiple_sentences(self):
-        sentence_one = " airplane at airport"
-        sentence_two = " kohli is the best"
+        sentence_one = tf.constant(" airplane at airport")
+        sentence_two = tf.constant(" kohli is the best")
 
         output = self.preprocessor((sentence_one, sentence_two))
         self.assertAllEqual(
@@ -98,22 +121,8 @@ def test_tokenize_multiple_sentences(self):
         )
 
     def test_tokenize_multiple_batched_sentences(self):
-        sentence_one = tf.constant(
-            [
-                " airplane at airport",
-                " airplane at airport",
-                " airplane at airport",
-                " airplane at airport",
-            ]
-        )
-        sentence_two = tf.constant(
-            [
-                " kohli is the best",
-                " kohli is the best",
-                " kohli is the best",
-                " kohli is the best",
-            ]
-        )
+        sentence_one = tf.constant([" airplane at airport"] * 4)
+        sentence_two = tf.constant([" kohli is the best"] * 4)
 
         output = self.preprocessor((sentence_one, sentence_two))
         self.assertAllEqual(
@@ -124,12 +133,10 @@ def test_tokenize_multiple_batched_sentences(self):
             output["padding_mask"], [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] * 4
         )
 
-    def test_detokenize(self):
-        input_tokens = [0, 3, 4, 5, 3, 6, 2, 1, 1, 1, 1, 1]
-        output = self.preprocessor.tokenizer.detokenize(input_tokens)
-        self.assertEqual(
-            output, "
 airplane at airport
<pad><pad><pad><pad><pad>"
-        )
+    def test_errors_for_2d_list_input(self):
+        ambiguous_input = [["one", "two"], ["three", "four"]]
+        with self.assertRaises(ValueError):
+            self.preprocessor(ambiguous_input)
 
     @parameterized.named_parameters(
         ("save_format_tf", "tf"), ("save_format_h5", "h5")