|
| 1 | +# coding=utf-8 |
| 2 | +# Copyright 2022 The HuggingFace Team Inc. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a clone of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +import tempfile |
| 17 | +import unittest |
| 18 | + |
| 19 | +from transformers import is_tf_available |
| 20 | +from transformers.testing_utils import require_tf, slow |
| 21 | + |
| 22 | + |
| 23 | +if is_tf_available(): |
| 24 | + import tensorflow as tf |
| 25 | + |
| 26 | + from transformers import AutoTokenizer, TFAutoModelForCausalLM, TFAutoModelForSeq2SeqLM, tf_top_k_top_p_filtering |
| 27 | + |
| 28 | + |
| 29 | +@require_tf |
| 30 | +class UtilsFunctionsTest(unittest.TestCase): |
| 31 | + |
| 32 | + # tests whether the top_k_top_p_filtering function behaves as expected |
| 33 | + def test_top_k_top_p_filtering(self): |
| 34 | + logits = tf.convert_to_tensor( |
| 35 | + [ |
| 36 | + [ |
| 37 | + 8.2220991, # 3rd highest value; idx. 0 |
| 38 | + -0.5620044, |
| 39 | + 5.23229752, |
| 40 | + 4.0386393, |
| 41 | + -6.8798378, |
| 42 | + -0.54785802, |
| 43 | + -3.2012153, |
| 44 | + 2.92777176, |
| 45 | + 1.88171953, |
| 46 | + 7.35341276, # 5th highest value; idx. 9 |
| 47 | + 8.43207833, # 2nd highest value; idx. 10 |
| 48 | + -9.85711836, |
| 49 | + -5.96209236, |
| 50 | + -1.13039161, |
| 51 | + -7.1115294, |
| 52 | + -0.8369633, |
| 53 | + -5.3186408, |
| 54 | + 7.06427407, |
| 55 | + 0.81369344, |
| 56 | + -0.82023817, |
| 57 | + -5.9179796, |
| 58 | + 0.58813443, |
| 59 | + -6.99778438, |
| 60 | + 4.71551189, |
| 61 | + -0.18771637, |
| 62 | + 7.44020759, # 4th highest value; idx. 25 |
| 63 | + 9.38450987, # 1st highest value; idx. 26 |
| 64 | + 2.12662941, |
| 65 | + -9.32562038, |
| 66 | + 2.35652522, |
| 67 | + ], # cummulative prob of 5 highest values <= 0.6 |
| 68 | + [ |
| 69 | + 0.58425518, |
| 70 | + 4.53139238, |
| 71 | + -5.57510464, |
| 72 | + -6.28030699, |
| 73 | + -7.19529503, |
| 74 | + -4.02122551, |
| 75 | + 1.39337037, |
| 76 | + -6.06707057, |
| 77 | + 1.59480517, |
| 78 | + -9.643119, |
| 79 | + 0.03907799, |
| 80 | + 0.67231762, |
| 81 | + -8.88206726, |
| 82 | + 6.27115922, # 4th highest value; idx. 13 |
| 83 | + 2.28520723, |
| 84 | + 4.82767506, |
| 85 | + 4.30421368, |
| 86 | + 8.8275313, # 2nd highest value; idx. 17 |
| 87 | + 5.44029958, # 5th highest value; idx. 18 |
| 88 | + -4.4735794, |
| 89 | + 7.38579536, # 3rd highest value; idx. 20 |
| 90 | + -2.91051663, |
| 91 | + 2.61946077, |
| 92 | + -2.5674762, |
| 93 | + -9.48959302, |
| 94 | + -4.02922645, |
| 95 | + -1.35416918, |
| 96 | + 9.67702323, # 1st highest value; idx. 27 |
| 97 | + -5.89478553, |
| 98 | + 1.85370467, |
| 99 | + ], # cummulative prob of 5 highest values <= 0.6 |
| 100 | + ], |
| 101 | + dtype=tf.float32, |
| 102 | + ) |
| 103 | + |
| 104 | + non_inf_expected_idx = tf.convert_to_tensor( |
| 105 | + [[0, 0], [0, 9], [0, 10], [0, 25], [0, 26], [1, 13], [1, 17], [1, 18], [1, 20], [1, 27]], |
| 106 | + dtype=tf.int32, |
| 107 | + ) # expected non filtered idx as noted above |
| 108 | + |
| 109 | + non_inf_expected_output = tf.convert_to_tensor( |
| 110 | + [8.222099, 7.3534126, 8.432078, 7.4402075, 9.38451, 6.271159, 8.827531, 5.4402995, 7.3857956, 9.677023], |
| 111 | + dtype=tf.float32, |
| 112 | + ) # expected non filtered values as noted above |
| 113 | + |
| 114 | + output = tf_top_k_top_p_filtering(logits, top_k=10, top_p=0.6, min_tokens_to_keep=4) |
| 115 | + |
| 116 | + non_inf_output = output[output != -float("inf")] |
| 117 | + non_inf_idx = tf.cast( |
| 118 | + tf.where(tf.not_equal(output, tf.constant(-float("inf"), dtype=tf.float32))), |
| 119 | + dtype=tf.int32, |
| 120 | + ) |
| 121 | + |
| 122 | + tf.debugging.assert_near(non_inf_output, non_inf_expected_output, rtol=1e-12) |
| 123 | + tf.debugging.assert_equal(non_inf_idx, non_inf_expected_idx) |
| 124 | + |
| 125 | + |
| 126 | +@require_tf |
| 127 | +class TFGenerationIntegrationTests(unittest.TestCase): |
| 128 | + @slow |
| 129 | + def test_generate_tf_function_export(self): |
| 130 | + test_model = TFAutoModelForCausalLM.from_pretrained("hf-internal-testing/tiny-random-gpt2") |
| 131 | + max_length = 2 |
| 132 | + |
| 133 | + class DummyModel(tf.Module): |
| 134 | + def __init__(self, model): |
| 135 | + super(DummyModel, self).__init__() |
| 136 | + self.model = model |
| 137 | + |
| 138 | + @tf.function( |
| 139 | + input_signature=( |
| 140 | + tf.TensorSpec((None, max_length), tf.int32, name="input_ids"), |
| 141 | + tf.TensorSpec((None, max_length), tf.int32, name="attention_mask"), |
| 142 | + ), |
| 143 | + jit_compile=True, |
| 144 | + ) |
| 145 | + def serving(self, input_ids, attention_mask): |
| 146 | + outputs = self.model.generate( |
| 147 | + input_ids=input_ids, |
| 148 | + attention_mask=attention_mask, |
| 149 | + max_new_tokens=max_length, |
| 150 | + return_dict_in_generate=True, |
| 151 | + ) |
| 152 | + return {"sequences": outputs["sequences"]} |
| 153 | + |
| 154 | + dummy_input_ids = [[2, 0], [102, 103]] |
| 155 | + dummy_attention_masks = [[1, 0], [1, 1]] |
| 156 | + dummy_model = DummyModel(model=test_model) |
| 157 | + with tempfile.TemporaryDirectory() as tmp_dir: |
| 158 | + tf.saved_model.save(dummy_model, tmp_dir, signatures={"serving_default": dummy_model.serving}) |
| 159 | + serving_func = tf.saved_model.load(tmp_dir).signatures["serving_default"] |
| 160 | + for batch_size in range(1, len(dummy_input_ids) + 1): |
| 161 | + inputs = { |
| 162 | + "input_ids": tf.constant(dummy_input_ids[:batch_size]), |
| 163 | + "attention_mask": tf.constant(dummy_attention_masks[:batch_size]), |
| 164 | + } |
| 165 | + tf_func_outputs = serving_func(**inputs)["sequences"] |
| 166 | + tf_model_outputs = test_model.generate(**inputs, max_new_tokens=max_length) |
| 167 | + tf.debugging.assert_equal(tf_func_outputs, tf_model_outputs) |
| 168 | + |
| 169 | + def test_validate_generation_inputs(self): |
| 170 | + tokenizer = AutoTokenizer.from_pretrained("hf-internal-testing/tiny-random-t5") |
| 171 | + model = TFAutoModelForSeq2SeqLM.from_pretrained("hf-internal-testing/tiny-random-t5") |
| 172 | + |
| 173 | + encoder_input_str = "Hello world" |
| 174 | + input_ids = tokenizer(encoder_input_str, return_tensors="tf").input_ids |
| 175 | + |
| 176 | + # typos are quickly detected (the correct argument is `do_sample`) |
| 177 | + with self.assertRaisesRegex(ValueError, "do_samples"): |
| 178 | + model.generate(input_ids, do_samples=True) |
| 179 | + |
| 180 | + # arbitrary arguments that will not be used anywhere are also not accepted |
| 181 | + with self.assertRaisesRegex(ValueError, "foo"): |
| 182 | + fake_model_kwargs = {"foo": "bar"} |
| 183 | + model.generate(input_ids, **fake_model_kwargs) |
0 commit comments