Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement compute_output_spec() for tokenizers with vocabulary. #1523

Merged
merged 2 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions keras_nlp/tokenizers/byte_pair_tokenizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from typing import Iterable
from typing import List

import keras
import regex as re
import tensorflow as tf

Expand Down Expand Up @@ -605,6 +606,11 @@ def detokenize(self, inputs):
outputs = tf.squeeze(outputs, 0)
return outputs

def compute_output_spec(self, input_spec) -> keras.KerasTensor:
return keras.KerasTensor(
input_spec.shape + (self.sequence_length,), dtype=self.compute_dtype
)

def _transform_bytes(self, tokens):
"""Map token bytes to unicode using `byte2unicode`."""
split_bytes = tf.strings.bytes_split(tokens)
Expand Down
6 changes: 6 additions & 0 deletions keras_nlp/tokenizers/sentence_piece_tokenizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import os
from typing import List

import keras
import tensorflow as tf

from keras_nlp.api_export import keras_nlp_export
Expand Down Expand Up @@ -255,3 +256,8 @@ def detokenize(self, inputs):
if unbatched:
outputs = tf.squeeze(outputs, 0)
return outputs

def compute_output_spec(self, input_spec) -> keras.KerasTensor:
mattdangerw marked this conversation as resolved.
Show resolved Hide resolved
return keras.KerasTensor(
input_spec.shape + (self.sequence_length,), dtype=self.compute_dtype
)
6 changes: 6 additions & 0 deletions keras_nlp/tokenizers/word_piece_tokenizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from typing import Iterable
from typing import List

import keras
import tensorflow as tf

from keras_nlp.api_export import keras_nlp_export
Expand Down Expand Up @@ -528,3 +529,8 @@ def detokenize(self, inputs):
if unbatched:
outputs = tf.squeeze(outputs, 0)
return outputs

def compute_output_spec(self, input_spec) -> keras.KerasTensor:
return keras.KerasTensor(
input_spec.shape + (self.sequence_length,), dtype=self.compute_dtype
)
Loading