"docs/vscode:/vscode.git/clone" did not exist on "2ef09ecfb8afb6624aab87afdad9fe72030397af"
Unverified Commit 2e12b907 authored by Patrick von Platen's avatar Patrick von Platen Committed by GitHub
Browse files

TF generate refactor - Greedy Search (#15562)



* TF generate start refactor

* Add tf tests for sample generate

* re-organize

* boom boom

* Apply suggestions from code review

* re-add

* add all code

* make random greedy pass

* make encoder-decoder random work

* further improvements

* delete bogus file

* make gpt2 and t5 tests work

* finish logits tests

* correct logits processors

* correct past / encoder_outputs drama

* refactor some methods

* another fix

* refactor shape_list

* fix more shape list

* import shape
_list

* finish docs

* fix imports

* make style

* correct tf utils

* Fix TFRag as well

* Apply Lysandre's and Sylvais suggestions

* Update tests/test_generation_tf_logits_process.py
Co-authored-by: default avatarMatt <Rocketknight1@users.noreply.github.com>

* Update src/transformers/tf_utils.py
Co-authored-by: default avatarMatt <Rocketknight1@users.noreply.github.com>

* remove cpu according to gante

* correct logit processor
Co-authored-by: default avatarMatt <Rocketknight1@users.noreply.github.com>
parent a3dbbc34
...@@ -34,8 +34,8 @@ from ...modeling_tf_utils import ( ...@@ -34,8 +34,8 @@ from ...modeling_tf_utils import (
get_initializer, get_initializer,
input_processing, input_processing,
keras_serializable, keras_serializable,
shape_list,
) )
from ...tf_utils import shape_list
from ...utils import logging from ...utils import logging
from .configuration_transfo_xl import TransfoXLConfig from .configuration_transfo_xl import TransfoXLConfig
from .modeling_tf_transfo_xl_utilities import TFAdaptiveSoftmaxMask from .modeling_tf_transfo_xl_utilities import TFAdaptiveSoftmaxMask
......
...@@ -20,7 +20,7 @@ ...@@ -20,7 +20,7 @@
import tensorflow as tf import tensorflow as tf
from ...modeling_tf_utils import shape_list from ...tf_utils import shape_list
class TFAdaptiveSoftmaxMask(tf.keras.layers.Layer): class TFAdaptiveSoftmaxMask(tf.keras.layers.Layer):
......
...@@ -30,13 +30,8 @@ from ...file_utils import ( ...@@ -30,13 +30,8 @@ from ...file_utils import (
replace_return_docstrings, replace_return_docstrings,
) )
from ...modeling_tf_outputs import TFBaseModelOutput, TFSeq2SeqLMOutput from ...modeling_tf_outputs import TFBaseModelOutput, TFSeq2SeqLMOutput
from ...modeling_tf_utils import ( from ...modeling_tf_utils import TFCausalLanguageModelingLoss, TFPreTrainedModel, get_initializer, input_processing
TFCausalLanguageModelingLoss, from ...tf_utils import shape_list
TFPreTrainedModel,
get_initializer,
input_processing,
shape_list,
)
from ...utils import logging from ...utils import logging
from ..auto.configuration_auto import AutoConfig from ..auto.configuration_auto import AutoConfig
from ..auto.modeling_tf_auto import TFAutoModel, TFAutoModelForCausalLM from ..auto.modeling_tf_auto import TFAutoModel, TFAutoModelForCausalLM
......
...@@ -32,8 +32,8 @@ from ...modeling_tf_utils import ( ...@@ -32,8 +32,8 @@ from ...modeling_tf_utils import (
get_initializer, get_initializer,
input_processing, input_processing,
keras_serializable, keras_serializable,
shape_list,
) )
from ...tf_utils import shape_list
from ...utils import logging from ...utils import logging
from .configuration_vit import ViTConfig from .configuration_vit import ViTConfig
......
...@@ -30,13 +30,8 @@ from ...file_utils import ( ...@@ -30,13 +30,8 @@ from ...file_utils import (
replace_return_docstrings, replace_return_docstrings,
) )
from ...modeling_tf_outputs import TFBaseModelOutput, TFCausalLMOutput from ...modeling_tf_outputs import TFBaseModelOutput, TFCausalLMOutput
from ...modeling_tf_utils import ( from ...modeling_tf_utils import TFPreTrainedModel, booleans_processing, get_initializer, keras_serializable
TFPreTrainedModel, from ...tf_utils import shape_list
booleans_processing,
get_initializer,
keras_serializable,
shape_list,
)
from ...tokenization_utils_base import BatchEncoding from ...tokenization_utils_base import BatchEncoding
from ...utils import logging from ...utils import logging
from .configuration_wav2vec2 import Wav2Vec2Config from .configuration_wav2vec2 import Wav2Vec2Config
......
...@@ -50,8 +50,8 @@ from ...modeling_tf_utils import ( ...@@ -50,8 +50,8 @@ from ...modeling_tf_utils import (
get_initializer, get_initializer,
input_processing, input_processing,
keras_serializable, keras_serializable,
shape_list,
) )
from ...tf_utils import shape_list
from ...utils import logging from ...utils import logging
from .configuration_xlm import XLMConfig from .configuration_xlm import XLMConfig
......
...@@ -44,8 +44,8 @@ from ...modeling_tf_utils import ( ...@@ -44,8 +44,8 @@ from ...modeling_tf_utils import (
get_initializer, get_initializer,
input_processing, input_processing,
keras_serializable, keras_serializable,
shape_list,
) )
from ...tf_utils import shape_list
from ...utils import logging from ...utils import logging
from .configuration_xlnet import XLNetConfig from .configuration_xlnet import XLNetConfig
......
# Copyright 2022 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import List, Union
import numpy as np
import tensorflow as tf
from .utils import logging
logger = logging.get_logger(__name__)
def set_tensor_by_indices_to_value(tensor: tf.Tensor, indices: tf.Tensor, value: Union[tf.Tensor, int, float]):
# create value_tensor since tensor value assignment is not possible in TF
return tf.where(indices, value, tensor)
def shape_list(tensor: Union[tf.Tensor, np.ndarray]) -> List[int]:
"""
Deal with dynamic shape in tensorflow cleanly.
Args:
tensor (`tf.Tensor` or `np.ndarray`): The tensor we want the shape of.
Returns:
`List[int]`: The shape of the tensor as a list.
"""
if isinstance(tensor, np.ndarray):
return list(tensor.shape)
dynamic = tf.shape(tensor)
if tensor.shape == tf.TensorShape(None):
return dynamic
static = tensor.shape.as_list()
return [dynamic[i] if s is None else s for i, s in enumerate(static)]
...@@ -17,6 +17,48 @@ class TensorFlowBenchmark(metaclass=DummyObject): ...@@ -17,6 +17,48 @@ class TensorFlowBenchmark(metaclass=DummyObject):
requires_backends(self, ["tf"]) requires_backends(self, ["tf"])
class TFLogitsProcessor(metaclass=DummyObject):
_backends = ["tf"]
def __init__(self, *args, **kwargs):
requires_backends(self, ["tf"])
class TFLogitsProcessorList(metaclass=DummyObject):
_backends = ["tf"]
def __init__(self, *args, **kwargs):
requires_backends(self, ["tf"])
class TFMinLengthLogitsProcessor(metaclass=DummyObject):
_backends = ["tf"]
def __init__(self, *args, **kwargs):
requires_backends(self, ["tf"])
class TFNoBadWordsLogitsProcessor(metaclass=DummyObject):
_backends = ["tf"]
def __init__(self, *args, **kwargs):
requires_backends(self, ["tf"])
class TFNoRepeatNGramLogitsProcessor(metaclass=DummyObject):
_backends = ["tf"]
def __init__(self, *args, **kwargs):
requires_backends(self, ["tf"])
class TFRepetitionPenaltyLogitsProcessor(metaclass=DummyObject):
_backends = ["tf"]
def __init__(self, *args, **kwargs):
requires_backends(self, ["tf"])
def tf_top_k_top_p_filtering(*args, **kwargs): def tf_top_k_top_p_filtering(*args, **kwargs):
requires_backends(tf_top_k_top_p_filtering, ["tf"]) requires_backends(tf_top_k_top_p_filtering, ["tf"])
......
...@@ -53,8 +53,8 @@ from ...modeling_tf_utils import ( ...@@ -53,8 +53,8 @@ from ...modeling_tf_utils import (
get_initializer, get_initializer,
input_processing, input_processing,
keras_serializable, keras_serializable,
shape_list,
) )
from ...tf_utils import shape_list
from ...utils import logging from ...utils import logging
from .configuration_{{cookiecutter.lowercase_modelname}} import {{cookiecutter.camelcase_modelname}}Config from .configuration_{{cookiecutter.lowercase_modelname}} import {{cookiecutter.camelcase_modelname}}Config
...@@ -1803,7 +1803,7 @@ from ...modeling_tf_utils import ( ...@@ -1803,7 +1803,7 @@ from ...modeling_tf_utils import (
TFWrappedEmbeddings, TFWrappedEmbeddings,
input_processing, input_processing,
keras_serializable, keras_serializable,
shape_list, ); from ...tf_utils import (shape_list,
) )
from ...utils import logging from ...utils import logging
from .configuration_{{cookiecutter.lowercase_modelname}} import {{cookiecutter.camelcase_modelname}}Config from .configuration_{{cookiecutter.lowercase_modelname}} import {{cookiecutter.camelcase_modelname}}Config
......
# coding=utf-8
# Copyright 2020 The HuggingFace Team Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a clone of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import unittest
from transformers import is_tf_available
from transformers.testing_utils import require_tf
if is_tf_available():
import tensorflow as tf
from transformers.generation_tf_logits_process import (
TFLogitsProcessorList,
TFMinLengthLogitsProcessor,
TFNoBadWordsLogitsProcessor,
TFNoRepeatNGramLogitsProcessor,
TFRepetitionPenaltyLogitsProcessor,
)
from transformers.tf_utils import set_tensor_by_indices_to_value
from .test_modeling_tf_common import ids_tensor
@require_tf
class TFLogitsProcessorTest(unittest.TestCase):
def _get_uniform_logits(self, batch_size: int, length: int):
scores = tf.ones((batch_size, length), dtype=tf.float32) / length
return scores
def test_min_length_dist_processor(self):
vocab_size = 20
batch_size = 4
eos_token_id = 0
min_dist_processor = TFMinLengthLogitsProcessor(min_length=10, eos_token_id=eos_token_id)
# check that min length is applied at length 5
input_ids = ids_tensor((batch_size, 5), vocab_size=20)
scores = self._get_uniform_logits(batch_size, vocab_size)
scores_before_min_length = min_dist_processor(input_ids, scores)
self.assertListEqual(scores_before_min_length[:, eos_token_id].numpy().tolist(), 4 * [-float("inf")])
# check that min length is not applied anymore at length 15
input_ids = ids_tensor((batch_size, 15), vocab_size=20)
scores = self._get_uniform_logits(batch_size, vocab_size)
scores_before_min_length = min_dist_processor(input_ids, scores)
self.assertFalse(tf.math.reduce_any(tf.math.is_inf(scores_before_min_length)).numpy())
def test_repetition_penalty_dist_process(self):
input_ids = tf.constant([[0, 1], [5, 0]], dtype=tf.int32)
vocab_size = 10
scores = self._get_uniform_logits(batch_size=2, length=vocab_size)
mask = tf.cast(tf.constant([[1] + 9 * [0], 10 * [0]]), tf.bool)
scores = set_tensor_by_indices_to_value(scores, mask, -1 / vocab_size)
mask = tf.cast(tf.constant([10 * [0], 5 * [0] + [1] + 4 * [0]]), tf.bool)
scores = set_tensor_by_indices_to_value(scores, mask, 4 / vocab_size)
rep_penalty_proc = TFRepetitionPenaltyLogitsProcessor(penalty=2.0)
scores = rep_penalty_proc(input_ids, tf.identity(scores))
# check that values were correctly changed
self.assertAlmostEqual(scores[0, 0].numpy(), -(1 / vocab_size) * 2)
self.assertAlmostEqual(scores[0, 1].numpy(), (1 / vocab_size) / 2)
self.assertAlmostEqual(scores[1, 0].numpy(), (1 / vocab_size) / 2)
self.assertAlmostEqual(scores[1, 5].numpy(), (4 / vocab_size) / 2)
def test_no_repeat_ngram_dist_processor(self):
vocab_size = 3
batch_size = 2
input_ids = tf.constant([[1, 1, 2, 1], [0, 1, 0, 1]], dtype=tf.int32)
scores = self._get_uniform_logits(batch_size, vocab_size)
no_repeat_proc_2_gram = TFNoRepeatNGramLogitsProcessor(2)
no_repeat_proc_3_gram = TFNoRepeatNGramLogitsProcessor(3)
filtered_scores_2_gram = no_repeat_proc_2_gram(input_ids, tf.identity(scores))
filtered_scores_3_gram = no_repeat_proc_3_gram(input_ids, tf.identity(scores))
# 2-gram would forbid 2nd and 3rd token (1,2) at 1st batch and 1st token (0) at 2nd batch
self.assertListEqual(
tf.math.is_inf(filtered_scores_2_gram).numpy().tolist(), [[False, True, True], [True, False, False]]
)
# 3-gram would forbid no token at 1st batch and 1st token (0) at 2nd batch
self.assertListEqual(
tf.math.is_inf(filtered_scores_3_gram).numpy().tolist(), [[False, False, False], [True, False, False]]
)
def test_no_bad_words_dist_processor(self):
vocab_size = 5
batch_size = 2
eos_token_id = 4
input_ids = tf.constant([[0, 1, 3, 1], [0, 1, 0, 1]], dtype=tf.int32)
bad_word_tokens = [[1], [4], [1, 0], [0, 1, 2], [1, 3, 1, 3]]
scores = self._get_uniform_logits(batch_size, vocab_size)
no_bad_words_dist_proc = TFNoBadWordsLogitsProcessor(bad_words_ids=bad_word_tokens, eos_token_id=eos_token_id)
filtered_scores = no_bad_words_dist_proc(input_ids, tf.identity(scores))
# batch 1: 1st, 2nd, and 4th (0, 1, 3) token are forbidden
# batch 2: 1st, 2nd, and 3rd (0, 1, 2) token are forbidden
self.assertListEqual(
tf.math.is_inf(filtered_scores).numpy().tolist(),
[[True, True, False, True, True], [True, True, True, False, True]],
)
def test_processor_list(self):
batch_size = 4
sequence_length = 10
vocab_size = 15
eos_token_id = 0
# dummy input_ids and scores
input_ids = ids_tensor((batch_size, sequence_length), vocab_size)
input_ids_comp = tf.identity(input_ids)
scores = self._get_uniform_logits(batch_size, vocab_size)
scores_comp = tf.identity(scores)
# instantiate all dist processors
min_dist_proc = TFMinLengthLogitsProcessor(min_length=10, eos_token_id=eos_token_id)
rep_penalty_proc = TFRepetitionPenaltyLogitsProcessor(penalty=2.0)
no_repeat_proc = TFNoRepeatNGramLogitsProcessor(2)
no_bad_words_dist_proc = TFNoBadWordsLogitsProcessor(bad_words_ids=[[1]], eos_token_id=eos_token_id)
# no processor list
scores = min_dist_proc(input_ids, scores)
scores = rep_penalty_proc(input_ids, scores)
scores = no_repeat_proc(input_ids, scores)
scores = no_bad_words_dist_proc(input_ids, scores)
# with processor list
processor = TFLogitsProcessorList(
[
min_dist_proc,
rep_penalty_proc,
no_repeat_proc,
no_bad_words_dist_proc,
]
)
scores_comp = processor(input_ids, scores_comp)
# remove inf
scores = set_tensor_by_indices_to_value(scores, tf.math.is_inf(scores), -1e9)
scores_comp = set_tensor_by_indices_to_value(scores_comp, tf.math.is_inf(scores_comp), -1e9)
# scores should be equal
tf.debugging.assert_near(scores, scores_comp, atol=1e-3)
# input_ids should never be changed
self.assertListEqual(input_ids.numpy().tolist(), input_ids_comp.numpy().tolist())
...@@ -955,7 +955,7 @@ class TFModelTesterMixin: ...@@ -955,7 +955,7 @@ class TFModelTesterMixin:
# Models with non-text inputs won't work here; num_return_sequences = 1 # Models with non-text inputs won't work here; num_return_sequences = 1
self._check_generated_ids(model.generate(do_sample=True, max_length=5)) self._check_generated_ids(model.generate(do_sample=True, max_length=5))
with self.assertRaises(AssertionError): with self.assertRaises(ValueError):
# generating multiple sequences when no beam search generation # generating multiple sequences when no beam search generation
# is not allowed as it would always generate the same sequences # is not allowed as it would always generate the same sequences
model.generate(input_ids, do_sample=False, num_return_sequences=2) model.generate(input_ids, do_sample=False, num_return_sequences=2)
......
...@@ -26,14 +26,15 @@ from .test_modeling_tf_core import TFCoreModelTesterMixin ...@@ -26,14 +26,15 @@ from .test_modeling_tf_core import TFCoreModelTesterMixin
if is_tf_available(): if is_tf_available():
import tensorflow as tf import tensorflow as tf
from transformers import GPT2Tokenizer
from transformers.models.gpt2.modeling_tf_gpt2 import ( from transformers.models.gpt2.modeling_tf_gpt2 import (
TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST, TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST,
TFGPT2DoubleHeadsModel, TFGPT2DoubleHeadsModel,
TFGPT2ForSequenceClassification, TFGPT2ForSequenceClassification,
TFGPT2LMHeadModel, TFGPT2LMHeadModel,
TFGPT2Model, TFGPT2Model,
shape_list,
) )
from transformers.tf_utils import shape_list
class TFGPT2ModelTester: class TFGPT2ModelTester:
...@@ -428,60 +429,53 @@ class TFGPT2ModelTest(TFModelTesterMixin, TFCoreModelTesterMixin, unittest.TestC ...@@ -428,60 +429,53 @@ class TFGPT2ModelTest(TFModelTesterMixin, TFCoreModelTesterMixin, unittest.TestC
@require_tf @require_tf
class TFGPT2ModelLanguageGenerationTest(unittest.TestCase): class TFGPT2ModelLanguageGenerationTest(unittest.TestCase):
@slow @slow
def test_lm_generate_gpt2(self): def test_lm_generate_distilgpt2(self):
model = TFGPT2LMHeadModel.from_pretrained("gpt2") model = TFGPT2LMHeadModel.from_pretrained("distilgpt2")
input_ids = tf.convert_to_tensor([[464, 3290]], dtype=tf.int32) # The dog input_ids = tf.convert_to_tensor([[464, 1893]], dtype=tf.int32) # The president
expected_output_ids = [
464, # The president of the United States, and the president of the United Kingdom, have been in the White
3290, # fmt: off
373, expected_output_ids = [464, 1893, 286, 262, 1578, 1829, 11, 290, 262, 1893, 286, 262, 1578, 7526, 11, 423, 587, 287, 262, 2635]
1043, # fmt: on
287,
257,
2214,
1474,
262,
16246,
286,
2688,
290,
2688,
27262,
13,
198,
198,
464,
3290,
] # The dog was found in a field near the intersection of West and West Streets.\n\nThe dog
output_ids = model.generate(input_ids, do_sample=False) output_ids = model.generate(input_ids, do_sample=False)
self.assertListEqual(output_ids[0].numpy().tolist(), expected_output_ids) self.assertListEqual(output_ids[0].numpy().tolist(), expected_output_ids)
@slow @slow
def test_lm_generate_distilgpt2(self): def test_lm_generate_distilgpt2_batch_special(self):
model = TFGPT2LMHeadModel.from_pretrained("distilgpt2") model = TFGPT2LMHeadModel.from_pretrained("distilgpt2")
input_ids = tf.convert_to_tensor([[464, 1893]], dtype=tf.int32) # The president tokenizer = GPT2Tokenizer.from_pretrained("distilgpt2")
expected_output_ids = [
464, tokenizer.pad_token = tokenizer.eos_token
1893, tokenizer.padding_side = "left"
286,
262, sentences = ["Today is a beautiful day and", "Yesterday was"]
1578, input_ids = tokenizer(sentences, return_tensors="tf", padding=True).input_ids
1829,
11, generation_kwargs = {
290, "bad_words_ids": [tokenizer("is").input_ids, tokenizer("angry about").input_ids],
262, "no_repeat_ngram_size": 2,
1893, "do_sample": False,
286, "repetition_penalty": 1.3,
262, }
1578,
7526, output_ids = model.generate(input_ids, **generation_kwargs)
11,
423, output_strings = tokenizer.batch_decode(output_ids, skip_special_tokens=True)
587, expected_output_string = [
287, "Today is a beautiful day and I am so happy to be able take part in this amazing event.",
262, "Yesterday was a very busy day for the first time since I started writing this post",
2635, ]
] # The president of the United States, and the president of the United Kingdom, have been in the White self.assertListEqual(output_strings, expected_output_string)
@slow
def test_lm_generate_gpt2(self):
model = TFGPT2LMHeadModel.from_pretrained("gpt2")
input_ids = tf.convert_to_tensor([[464, 3290]], dtype=tf.int32) # The dog
# The dog was found in a field near the intersection of West and West Streets.\n\nThe dog
# fmt: off
expected_output_ids = [464, 3290, 373, 1043, 287, 257, 2214, 1474, 262, 16246, 286, 2688, 290, 2688, 27262, 13, 198, 198, 464, 3290]
# fmt: on
output_ids = model.generate(input_ids, do_sample=False) output_ids = model.generate(input_ids, do_sample=False)
self.assertListEqual(output_ids[0].numpy().tolist(), expected_output_ids) self.assertListEqual(output_ids[0].numpy().tolist(), expected_output_ids)
...@@ -36,14 +36,7 @@ if is_tf_available(): ...@@ -36,14 +36,7 @@ if is_tf_available():
TFLongformerModel, TFLongformerModel,
TFLongformerSelfAttention, TFLongformerSelfAttention,
) )
from transformers.tf_utils import shape_list
def shape_list(x):
"""
copied from transformers.modeling_tf_utils
"""
static = x.shape.as_list()
dynamic = tf.shape(x)
return [dynamic[i] if s is None else s for i, s in enumerate(static)]
class TFLongformerModelTester: class TFLongformerModelTester:
......
...@@ -474,7 +474,7 @@ class TFSpeech2TextModelTest(TFModelTesterMixin, unittest.TestCase): ...@@ -474,7 +474,7 @@ class TFSpeech2TextModelTest(TFModelTesterMixin, unittest.TestCase):
# num_return_sequences = 1 # num_return_sequences = 1
self._check_generated_ids(model.generate(input_features, do_sample=True)) self._check_generated_ids(model.generate(input_features, do_sample=True))
with self.assertRaises(AssertionError): with self.assertRaises(ValueError):
# generating multiple sequences when no beam search generation # generating multiple sequences when no beam search generation
# is not allowed as it would always generate the same sequences # is not allowed as it would always generate the same sequences
model.generate(input_features, do_sample=False, num_return_sequences=2) model.generate(input_features, do_sample=False, num_return_sequences=2)
......
...@@ -453,6 +453,34 @@ class TFT5EncoderOnlyModelTest(TFModelTesterMixin, unittest.TestCase): ...@@ -453,6 +453,34 @@ class TFT5EncoderOnlyModelTest(TFModelTesterMixin, unittest.TestCase):
pass pass
@require_tf
@require_sentencepiece
@require_tokenizers
class TFT5GenerationIntegrationTests(unittest.TestCase):
@slow
def test_greedy_generate(self):
model = TFT5ForConditionalGeneration.from_pretrained("t5-small")
tokenizer = T5Tokenizer.from_pretrained("t5-small")
sentences = ["Yesterday, my name was", "Today is a beautiful day and"]
input_ids = tokenizer(sentences, return_tensors="tf", padding=True).input_ids
generation_kwargs = {
"bad_words_ids": [tokenizer("my").input_ids, tokenizer("ein schöner").input_ids],
"no_repeat_ngram_size": 3,
"do_sample": False,
"repetition_penalty": 2.2,
}
output_ids = model.generate(input_ids, **generation_kwargs)
output_strings = tokenizer.batch_decode(output_ids, skip_special_tokens=True)
expected_output_string = ["Yesterday, my name was", "Heute ist ein schöne Tag und"]
self.assertListEqual(expected_output_string, output_strings)
@require_tf @require_tf
@require_sentencepiece @require_sentencepiece
@require_tokenizers @require_tokenizers
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment