From f5df9cbba90a40384bd011599f4bbfb009a4e6af Mon Sep 17 00:00:00 2001 From: Matt Date: Tue, 9 May 2023 15:26:30 +0100 Subject: [PATCH 01/49] Let's try autodetecting serving sigs --- src/transformers/modeling_tf_utils.py | 39 +++++++++++++-------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/src/transformers/modeling_tf_utils.py b/src/transformers/modeling_tf_utils.py index c2b0485b5f4c..e6b9ea52177f 100644 --- a/src/transformers/modeling_tf_utils.py +++ b/src/transformers/modeling_tf_utils.py @@ -1137,6 +1137,7 @@ def __init__(self, config, *inputs, **kwargs): self.config = config self.name_or_path = config.name_or_path self.generation_config = GenerationConfig.from_model_config(config) if self.can_generate() else None + self.serving = tf.function(self.eager_serving, input_signature=self.get_serving_input_signature()) # Set the serving spec quickly to ensure that Keras doesn't use the specific dummy input shapes as the spec self._set_save_spec(self.serving.input_signature[0]) @@ -1201,26 +1202,24 @@ def eager_serving(self, inputs): return self.serving_output(output) - @tf.function( - input_signature=[ - { - "input_ids": tf.TensorSpec((None, None), tf.int32, name="input_ids"), - "attention_mask": tf.TensorSpec((None, None), tf.int32, name="attention_mask"), - "token_type_ids": tf.TensorSpec((None, None), tf.int32, name="token_type_ids"), - } - ] - ) - def serving(self, inputs): - """ - Method used for serving the model. - - Args: - inputs (`Dict[str, tf.Tensor]`): - The input of the saved model as a dictionary of tensors. - """ - output = self.call(inputs) - - return self.serving_output(output) + def get_serving_input_signature(self): + model_inputs = list(dict(inspect.signature(self.call).parameters).keys()) + sig = {} + if "input_ids" in model_inputs: + for input_name in ("input_ids", "attention_mask", "token_type_ids"): + if input_name in model_inputs: + sig[input_name] = tf.TensorSpec((None, None), tf.int32, name=input_name) + if "pixel_values" in model_inputs: + pixel_values_shape = [None, None, None, None] + if hasattr(self.config, "vision_config"): + vision_config = self.config.vision_config + else: + vision_config = self.config + pixel_values_shape[1] = vision_config.get("num_channels", None) + if hasattr(vision_config, "image_size"): + pixel_values_shape[2] = pixel_values_shape[3] = vision_config.image_size + sig["pixel_values"] = tf.TensorSpec(pixel_values_shape, tf.float32, name="pixel_values") + return [sig] def serving_output(self, output): """ From e3338c077cc724dd385355f8083d9bb39f709534 Mon Sep 17 00:00:00 2001 From: Matt Date: Tue, 9 May 2023 15:33:48 +0100 Subject: [PATCH 02/49] Don't clobber existing sigs --- src/transformers/modeling_tf_utils.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/transformers/modeling_tf_utils.py b/src/transformers/modeling_tf_utils.py index e6b9ea52177f..9b48e7cf4b9c 100644 --- a/src/transformers/modeling_tf_utils.py +++ b/src/transformers/modeling_tf_utils.py @@ -1137,7 +1137,8 @@ def __init__(self, config, *inputs, **kwargs): self.config = config self.name_or_path = config.name_or_path self.generation_config = GenerationConfig.from_model_config(config) if self.can_generate() else None - self.serving = tf.function(self.eager_serving, input_signature=self.get_serving_input_signature()) + if not hasattr(self, "serving"): # Don't overwrite existing serving signatures + self.serving = tf.function(self.eager_serving, input_signature=self.get_serving_input_signature()) # Set the serving spec quickly to ensure that Keras doesn't use the specific dummy input shapes as the spec self._set_save_spec(self.serving.input_signature[0]) From 459e2c6d5e9653295c6c6d61fc08274c3f9c9cb0 Mon Sep 17 00:00:00 2001 From: Matt Date: Tue, 9 May 2023 15:55:06 +0100 Subject: [PATCH 03/49] Change shapes for multiplechoice models --- src/transformers/modeling_tf_utils.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/transformers/modeling_tf_utils.py b/src/transformers/modeling_tf_utils.py index 9b48e7cf4b9c..8b86f00d29f1 100644 --- a/src/transformers/modeling_tf_utils.py +++ b/src/transformers/modeling_tf_utils.py @@ -1206,10 +1206,14 @@ def eager_serving(self, inputs): def get_serving_input_signature(self): model_inputs = list(dict(inspect.signature(self.call).parameters).keys()) sig = {} + if self.__name__.endswith("ForMultipleChoice"): + text_dims = 3 + else: + text_dims = 2 if "input_ids" in model_inputs: for input_name in ("input_ids", "attention_mask", "token_type_ids"): if input_name in model_inputs: - sig[input_name] = tf.TensorSpec((None, None), tf.int32, name=input_name) + sig[input_name] = tf.TensorSpec([None] * text_dims, tf.int32, name=input_name) if "pixel_values" in model_inputs: pixel_values_shape = [None, None, None, None] if hasattr(self.config, "vision_config"): From 507ec1facb55fe5a2161d0693576b3806d89949b Mon Sep 17 00:00:00 2001 From: Matt Date: Tue, 9 May 2023 16:11:03 +0100 Subject: [PATCH 04/49] Make default dummy inputs smarter too --- src/transformers/modeling_tf_utils.py | 28 +++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/src/transformers/modeling_tf_utils.py b/src/transformers/modeling_tf_utils.py index 8b86f00d29f1..ff724efbbe34 100644 --- a/src/transformers/modeling_tf_utils.py +++ b/src/transformers/modeling_tf_utils.py @@ -43,6 +43,7 @@ from .tf_utils import expand_1d, load_attributes_from_hdf5_group, save_attributes_to_hdf5_group, shape_list from .utils import ( DUMMY_INPUTS, + MULTIPLE_CHOICE_DUMMY_INPUTS, SAFE_WEIGHTS_INDEX_NAME, SAFE_WEIGHTS_NAME, TF2_WEIGHTS_INDEX_NAME, @@ -1114,9 +1115,28 @@ def dummy_inputs(self) -> Dict[str, tf.Tensor]: Returns: `Dict[str, tf.Tensor]`: The dummy inputs. """ - return { - "input_ids": tf.constant(DUMMY_INPUTS, dtype=tf.int32), - } + dummy_inputs = {} + rng = np.random.default_rng(42) + serving_sig = self.get_serving_input_signature() + if self.main_input_name == "input_ids" and serving_sig[0]["input_ids"].shape.rank == 2: + dummy_inputs["input_ids"] = DUMMY_INPUTS + elif self.main_input_name == "input_ids" and serving_sig[0]["input_ids"].shape.rank == 3: + dummy_inputs["input_ids"] = MULTIPLE_CHOICE_DUMMY_INPUTS + elif self.main_input_name == "pixel_values": + image_shape = serving_sig[0]["pixel_values"].shape.as_list() + if image_shape[0] is None: + image_shape[0] = 3 # matches DUMMY_INPUTS + if None in image_shape[1:]: + raise NotImplementedError( + f"Could not fully infer input tensor shape, dummy inputs must be defined manually for {self.__name__}" + ) + VISION_DUMMY_INPUTS = rng.random(image_shape).astype(np.float32) + dummy_inputs["pixel_values"] = tf.constant(VISION_DUMMY_INPUTS, dtype=tf.float32) + else: + raise NotImplementedError( + "Could not fully infer input shapes, dummy inputs must be defined manually for {self.__name__}" + ) + return dummy_inputs @property def framework(self) -> str: @@ -1203,7 +1223,7 @@ def eager_serving(self, inputs): return self.serving_output(output) - def get_serving_input_signature(self): + def get_serving_input_signature(self) -> List[Dict[str, tf.TensorSpec]]: model_inputs = list(dict(inspect.signature(self.call).parameters).keys()) sig = {} if self.__name__.endswith("ForMultipleChoice"): From 6435f3be4b1750d1a7ed081fad60afd5b33ee438 Mon Sep 17 00:00:00 2001 From: Matt Date: Tue, 9 May 2023 16:15:17 +0100 Subject: [PATCH 05/49] Fix missing f-string --- src/transformers/modeling_tf_utils.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/transformers/modeling_tf_utils.py b/src/transformers/modeling_tf_utils.py index ff724efbbe34..b7310c9e5abb 100644 --- a/src/transformers/modeling_tf_utils.py +++ b/src/transformers/modeling_tf_utils.py @@ -1116,7 +1116,7 @@ def dummy_inputs(self) -> Dict[str, tf.Tensor]: `Dict[str, tf.Tensor]`: The dummy inputs. """ dummy_inputs = {} - rng = np.random.default_rng(42) + serving_sig = self.get_serving_input_signature() if self.main_input_name == "input_ids" and serving_sig[0]["input_ids"].shape.rank == 2: dummy_inputs["input_ids"] = DUMMY_INPUTS @@ -1130,11 +1130,12 @@ def dummy_inputs(self) -> Dict[str, tf.Tensor]: raise NotImplementedError( f"Could not fully infer input tensor shape, dummy inputs must be defined manually for {self.__name__}" ) + rng = np.random.default_rng(42) VISION_DUMMY_INPUTS = rng.random(image_shape).astype(np.float32) dummy_inputs["pixel_values"] = tf.constant(VISION_DUMMY_INPUTS, dtype=tf.float32) else: raise NotImplementedError( - "Could not fully infer input shapes, dummy inputs must be defined manually for {self.__name__}" + f"Could not fully infer input shapes, dummy inputs must be defined manually for {self.__name__}" ) return dummy_inputs From f379e89eba53945e68218ce6e39739abb9585fc6 Mon Sep 17 00:00:00 2001 From: Matt Date: Tue, 9 May 2023 16:34:14 +0100 Subject: [PATCH 06/49] Let's YOLO a serving output too --- src/transformers/modeling_tf_utils.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/transformers/modeling_tf_utils.py b/src/transformers/modeling_tf_utils.py index b7310c9e5abb..b98d93e2da3c 100644 --- a/src/transformers/modeling_tf_utils.py +++ b/src/transformers/modeling_tf_utils.py @@ -1249,13 +1249,10 @@ def get_serving_input_signature(self) -> List[Dict[str, tf.TensorSpec]]: def serving_output(self, output): """ - Prepare the output of the saved model. Each model must implement this function. - - Args: - output ([`TFBaseModelOutput`]): - The output returned by the model. + Prepare the output of the saved model. Can be overridden if specific serving modifications are required. """ - raise NotImplementedError + + return output def can_generate(self) -> bool: """ From 0ed9bc0a52c8a01dd9a754ef830a14b23b3bb10a Mon Sep 17 00:00:00 2001 From: Matt Date: Tue, 9 May 2023 17:00:23 +0100 Subject: [PATCH 07/49] Read __class__.__name__ properly --- src/transformers/modeling_tf_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transformers/modeling_tf_utils.py b/src/transformers/modeling_tf_utils.py index b98d93e2da3c..45e66a8687df 100644 --- a/src/transformers/modeling_tf_utils.py +++ b/src/transformers/modeling_tf_utils.py @@ -1227,7 +1227,7 @@ def eager_serving(self, inputs): def get_serving_input_signature(self) -> List[Dict[str, tf.TensorSpec]]: model_inputs = list(dict(inspect.signature(self.call).parameters).keys()) sig = {} - if self.__name__.endswith("ForMultipleChoice"): + if self.__class__.__name__.endswith("ForMultipleChoice"): text_dims = 3 else: text_dims = 2 From b060e7cc138e14f74464aab055990b367bdc9c51 Mon Sep 17 00:00:00 2001 From: Matt Date: Tue, 9 May 2023 17:22:54 +0100 Subject: [PATCH 08/49] Don't just pass naked lists in there and expect it to be okay --- src/transformers/modeling_tf_utils.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/transformers/modeling_tf_utils.py b/src/transformers/modeling_tf_utils.py index 45e66a8687df..7005b508de81 100644 --- a/src/transformers/modeling_tf_utils.py +++ b/src/transformers/modeling_tf_utils.py @@ -1118,10 +1118,11 @@ def dummy_inputs(self) -> Dict[str, tf.Tensor]: dummy_inputs = {} serving_sig = self.get_serving_input_signature() + breakpoint() if self.main_input_name == "input_ids" and serving_sig[0]["input_ids"].shape.rank == 2: - dummy_inputs["input_ids"] = DUMMY_INPUTS + dummy_inputs["input_ids"] = tf.constant(DUMMY_INPUTS, dtype=tf.int32) elif self.main_input_name == "input_ids" and serving_sig[0]["input_ids"].shape.rank == 3: - dummy_inputs["input_ids"] = MULTIPLE_CHOICE_DUMMY_INPUTS + dummy_inputs["input_ids"] = tf.constant(MULTIPLE_CHOICE_DUMMY_INPUTS, dtype=tf.int32) elif self.main_input_name == "pixel_values": image_shape = serving_sig[0]["pixel_values"].shape.as_list() if image_shape[0] is None: From 9d8fd7d61e4a6d20f98af75e28bd7d245aa946b5 Mon Sep 17 00:00:00 2001 From: Matt Date: Wed, 10 May 2023 13:11:05 +0100 Subject: [PATCH 09/49] Code cleanup --- src/transformers/modeling_tf_utils.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/transformers/modeling_tf_utils.py b/src/transformers/modeling_tf_utils.py index 7005b508de81..ad058e2b98b2 100644 --- a/src/transformers/modeling_tf_utils.py +++ b/src/transformers/modeling_tf_utils.py @@ -1118,7 +1118,6 @@ def dummy_inputs(self) -> Dict[str, tf.Tensor]: dummy_inputs = {} serving_sig = self.get_serving_input_signature() - breakpoint() if self.main_input_name == "input_ids" and serving_sig[0]["input_ids"].shape.rank == 2: dummy_inputs["input_ids"] = tf.constant(DUMMY_INPUTS, dtype=tf.int32) elif self.main_input_name == "input_ids" and serving_sig[0]["input_ids"].shape.rank == 3: From e27e4909dcf7a0ed94b6065655a64744704443a8 Mon Sep 17 00:00:00 2001 From: Matt Date: Wed, 10 May 2023 14:08:24 +0100 Subject: [PATCH 10/49] Update default serving sig --- src/transformers/modeling_tf_utils.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/transformers/modeling_tf_utils.py b/src/transformers/modeling_tf_utils.py index ad058e2b98b2..13a435bc08a9 100644 --- a/src/transformers/modeling_tf_utils.py +++ b/src/transformers/modeling_tf_utils.py @@ -1241,7 +1241,8 @@ def get_serving_input_signature(self) -> List[Dict[str, tf.TensorSpec]]: vision_config = self.config.vision_config else: vision_config = self.config - pixel_values_shape[1] = vision_config.get("num_channels", None) + if hasattr(vision_config, "num_channels"): + pixel_values_shape[1] = vision_config.num_channels if hasattr(vision_config, "image_size"): pixel_values_shape[2] = pixel_values_shape[3] = vision_config.image_size sig["pixel_values"] = tf.TensorSpec(pixel_values_shape, tf.float32, name="pixel_values") From db4f20b1b6d20d9a2f1ce0da0e1c9bbc3f50bebe Mon Sep 17 00:00:00 2001 From: Matt Date: Wed, 10 May 2023 14:31:26 +0100 Subject: [PATCH 11/49] Clearer error messages --- src/transformers/modeling_tf_utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/transformers/modeling_tf_utils.py b/src/transformers/modeling_tf_utils.py index 13a435bc08a9..aabf9f838569 100644 --- a/src/transformers/modeling_tf_utils.py +++ b/src/transformers/modeling_tf_utils.py @@ -1128,14 +1128,14 @@ def dummy_inputs(self) -> Dict[str, tf.Tensor]: image_shape[0] = 3 # matches DUMMY_INPUTS if None in image_shape[1:]: raise NotImplementedError( - f"Could not fully infer input tensor shape, dummy inputs must be defined manually for {self.__name__}" + f"Could not fully infer input tensor shape; dummy inputs or serving sig must be defined manually for {self.__class__.__name__}" ) rng = np.random.default_rng(42) VISION_DUMMY_INPUTS = rng.random(image_shape).astype(np.float32) dummy_inputs["pixel_values"] = tf.constant(VISION_DUMMY_INPUTS, dtype=tf.float32) else: raise NotImplementedError( - f"Could not fully infer input shapes, dummy inputs must be defined manually for {self.__name__}" + f"Could not fully infer input shapes, dummy inputs must be defined manually for {self.__class__.__name__}" ) return dummy_inputs From 8543596ffee851e50d0a8d43eafca4cf5d14839b Mon Sep 17 00:00:00 2001 From: Matt Date: Wed, 10 May 2023 15:02:56 +0100 Subject: [PATCH 12/49] Further updates to the default serving output --- src/transformers/modeling_tf_utils.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/transformers/modeling_tf_utils.py b/src/transformers/modeling_tf_utils.py index aabf9f838569..f9c06df74d1d 100644 --- a/src/transformers/modeling_tf_utils.py +++ b/src/transformers/modeling_tf_utils.py @@ -1252,6 +1252,17 @@ def serving_output(self, output): """ Prepare the output of the saved model. Can be overridden if specific serving modifications are required. """ + if isinstance(output, ModelOutput): + if 'hidden_states' in output: + try: + output.hidden_states = tf.convert_to_tensor(output.hidden_states) + except ValueError: + pass # Layers may not have the same dimensions + if 'attentions' in output: + try: + output.attentions = tf.convert_to_tensor(output.attentions) + except ValueError: + pass # Layers may not have the same dimensions return output From b5f3b31078e88f83163fe16a80aaeb3c63c7d45d Mon Sep 17 00:00:00 2001 From: Matt Date: Wed, 10 May 2023 15:27:07 +0100 Subject: [PATCH 13/49] make fixup --- src/transformers/modeling_tf_utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/transformers/modeling_tf_utils.py b/src/transformers/modeling_tf_utils.py index f9c06df74d1d..f6632a351e84 100644 --- a/src/transformers/modeling_tf_utils.py +++ b/src/transformers/modeling_tf_utils.py @@ -1253,12 +1253,12 @@ def serving_output(self, output): Prepare the output of the saved model. Can be overridden if specific serving modifications are required. """ if isinstance(output, ModelOutput): - if 'hidden_states' in output: + if "hidden_states" in output: try: output.hidden_states = tf.convert_to_tensor(output.hidden_states) except ValueError: pass # Layers may not have the same dimensions - if 'attentions' in output: + if "attentions" in output: try: output.attentions = tf.convert_to_tensor(output.attentions) except ValueError: From 79f4b50e7469bcb1963d13bfc0e1fe2a290022f2 Mon Sep 17 00:00:00 2001 From: Matt Date: Wed, 10 May 2023 16:19:59 +0100 Subject: [PATCH 14/49] Update the serving output a bit more --- src/transformers/modeling_tf_utils.py | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/transformers/modeling_tf_utils.py b/src/transformers/modeling_tf_utils.py index f6632a351e84..cfa0ea4c1719 100644 --- a/src/transformers/modeling_tf_utils.py +++ b/src/transformers/modeling_tf_utils.py @@ -1252,18 +1252,21 @@ def serving_output(self, output): """ Prepare the output of the saved model. Can be overridden if specific serving modifications are required. """ + config_variables = { + "hidden_states": "output_hidden_states", + "attentions": "output_attentions", + "past_key_values": "use_cache", + } if isinstance(output, ModelOutput): - if "hidden_states" in output: - try: - output.hidden_states = tf.convert_to_tensor(output.hidden_states) - except ValueError: - pass # Layers may not have the same dimensions - if "attentions" in output: - try: - output.attentions = tf.convert_to_tensor(output.attentions) - except ValueError: - pass # Layers may not have the same dimensions - + for key, config_var in config_variables.items(): + if key in output: + if not getattr(self.config, config_var, False): + output[key] = None + elif output[key] is not None: + try: + output[key] = tf.convert_to_tensor(output[key]) + except ValueError: + pass # Layers may not have the same dimensions return output def can_generate(self) -> bool: From e0ec348fa78cc172d5cba210538531a6118d8c0d Mon Sep 17 00:00:00 2001 From: Matt Date: Fri, 12 May 2023 14:29:04 +0100 Subject: [PATCH 15/49] Cleanups and renames, raise errors appropriately when we can't infer inputs --- src/transformers/modeling_tf_utils.py | 28 +++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/src/transformers/modeling_tf_utils.py b/src/transformers/modeling_tf_utils.py index cfa0ea4c1719..434e384ac2d6 100644 --- a/src/transformers/modeling_tf_utils.py +++ b/src/transformers/modeling_tf_utils.py @@ -1131,8 +1131,8 @@ def dummy_inputs(self) -> Dict[str, tf.Tensor]: f"Could not fully infer input tensor shape; dummy inputs or serving sig must be defined manually for {self.__class__.__name__}" ) rng = np.random.default_rng(42) - VISION_DUMMY_INPUTS = rng.random(image_shape).astype(np.float32) - dummy_inputs["pixel_values"] = tf.constant(VISION_DUMMY_INPUTS, dtype=tf.float32) + vision_dummy_inputs = rng.random(image_shape).astype(np.float32) + dummy_inputs["pixel_values"] = tf.constant(vision_dummy_inputs, dtype=tf.float32) else: raise NotImplementedError( f"Could not fully infer input shapes, dummy inputs must be defined manually for {self.__class__.__name__}" @@ -1159,7 +1159,7 @@ def __init__(self, config, *inputs, **kwargs): self.name_or_path = config.name_or_path self.generation_config = GenerationConfig.from_model_config(config) if self.can_generate() else None if not hasattr(self, "serving"): # Don't overwrite existing serving signatures - self.serving = tf.function(self.eager_serving, input_signature=self.get_serving_input_signature()) + self.serving = tf.function(self.eager_serving, input_signature=self.input_signature) # Set the serving spec quickly to ensure that Keras doesn't use the specific dummy input shapes as the spec self._set_save_spec(self.serving.input_signature[0]) @@ -1224,7 +1224,13 @@ def eager_serving(self, inputs): return self.serving_output(output) - def get_serving_input_signature(self) -> List[Dict[str, tf.TensorSpec]]: + @property + def input_signature(self) -> List[Dict[str, tf.TensorSpec]]: + """ + This property should return a dict mapping input names to tf.TensorSpec objects, representing the expected + shape and dtype for model inputs. It is used for both serving and for generating the dummy inputs used to build + the model. + """ model_inputs = list(dict(inspect.signature(self.call).parameters).keys()) sig = {} if self.__class__.__name__.endswith("ForMultipleChoice"): @@ -1243,9 +1249,19 @@ def get_serving_input_signature(self) -> List[Dict[str, tf.TensorSpec]]: vision_config = self.config if hasattr(vision_config, "num_channels"): pixel_values_shape[1] = vision_config.num_channels + else: + raise NotImplementedError( + "Could not infer number of channels from config, please override input_signature to specify input shapes." + ) if hasattr(vision_config, "image_size"): pixel_values_shape[2] = pixel_values_shape[3] = vision_config.image_size + else: + raise NotImplementedError( + "Could not infer input image shape from config, please override input_signature to specify input shapes." + ) sig["pixel_values"] = tf.TensorSpec(pixel_values_shape, tf.float32, name="pixel_values") + if "input_features" in model_inputs: + raise NotImplementedError("Audio models need a manually defined input_signature") return [sig] def serving_output(self, output): @@ -1254,7 +1270,11 @@ def serving_output(self, output): """ config_variables = { "hidden_states": "output_hidden_states", + "encoder_hidden_states": "output_hidden_states", + "decoder_hidden_states": "output_hidden_states", "attentions": "output_attentions", + "encoder_attentions": "output_attentions", + "decoder_attentions": "output_attentions", "past_key_values": "use_cache", } if isinstance(output, ModelOutput): From 831d56d0511dbb8e8d227576d39c3b051cd5dd41 Mon Sep 17 00:00:00 2001 From: Matt Date: Fri, 12 May 2023 14:34:19 +0100 Subject: [PATCH 16/49] More renames --- src/transformers/modeling_tf_utils.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/transformers/modeling_tf_utils.py b/src/transformers/modeling_tf_utils.py index 434e384ac2d6..10fb80a41f66 100644 --- a/src/transformers/modeling_tf_utils.py +++ b/src/transformers/modeling_tf_utils.py @@ -1117,13 +1117,13 @@ def dummy_inputs(self) -> Dict[str, tf.Tensor]: """ dummy_inputs = {} - serving_sig = self.get_serving_input_signature() - if self.main_input_name == "input_ids" and serving_sig[0]["input_ids"].shape.rank == 2: + input_sig = self.input_signature + if self.main_input_name == "input_ids" and input_sig["input_ids"].shape.rank == 2: dummy_inputs["input_ids"] = tf.constant(DUMMY_INPUTS, dtype=tf.int32) - elif self.main_input_name == "input_ids" and serving_sig[0]["input_ids"].shape.rank == 3: + elif self.main_input_name == "input_ids" and input_sig["input_ids"].shape.rank == 3: dummy_inputs["input_ids"] = tf.constant(MULTIPLE_CHOICE_DUMMY_INPUTS, dtype=tf.int32) elif self.main_input_name == "pixel_values": - image_shape = serving_sig[0]["pixel_values"].shape.as_list() + image_shape = input_sig["pixel_values"].shape.as_list() if image_shape[0] is None: image_shape[0] = 3 # matches DUMMY_INPUTS if None in image_shape[1:]: @@ -1159,7 +1159,7 @@ def __init__(self, config, *inputs, **kwargs): self.name_or_path = config.name_or_path self.generation_config = GenerationConfig.from_model_config(config) if self.can_generate() else None if not hasattr(self, "serving"): # Don't overwrite existing serving signatures - self.serving = tf.function(self.eager_serving, input_signature=self.input_signature) + self.serving = tf.function(self.eager_serving, input_signature=[self.input_signature]) # Set the serving spec quickly to ensure that Keras doesn't use the specific dummy input shapes as the spec self._set_save_spec(self.serving.input_signature[0]) @@ -1225,7 +1225,7 @@ def eager_serving(self, inputs): return self.serving_output(output) @property - def input_signature(self) -> List[Dict[str, tf.TensorSpec]]: + def input_signature(self) -> Dict[str, tf.TensorSpec]: """ This property should return a dict mapping input names to tf.TensorSpec objects, representing the expected shape and dtype for model inputs. It is used for both serving and for generating the dummy inputs used to build @@ -1262,7 +1262,7 @@ def input_signature(self) -> List[Dict[str, tf.TensorSpec]]: sig["pixel_values"] = tf.TensorSpec(pixel_values_shape, tf.float32, name="pixel_values") if "input_features" in model_inputs: raise NotImplementedError("Audio models need a manually defined input_signature") - return [sig] + return sig def serving_output(self, output): """ From 967073056dddd22822c5f83e24a9173a973c6657 Mon Sep 17 00:00:00 2001 From: Matt Date: Fri, 12 May 2023 14:36:32 +0100 Subject: [PATCH 17/49] we're building in a functional context again, yolo --- src/transformers/modeling_tf_utils.py | 28 ++++----------------------- 1 file changed, 4 insertions(+), 24 deletions(-) diff --git a/src/transformers/modeling_tf_utils.py b/src/transformers/modeling_tf_utils.py index 10fb80a41f66..21effe1f2e4f 100644 --- a/src/transformers/modeling_tf_utils.py +++ b/src/transformers/modeling_tf_utils.py @@ -42,8 +42,6 @@ from .generation import GenerationConfig, TFGenerationMixin from .tf_utils import expand_1d, load_attributes_from_hdf5_group, save_attributes_to_hdf5_group, shape_list from .utils import ( - DUMMY_INPUTS, - MULTIPLE_CHOICE_DUMMY_INPUTS, SAFE_WEIGHTS_INDEX_NAME, SAFE_WEIGHTS_NAME, TF2_WEIGHTS_INDEX_NAME, @@ -1115,29 +1113,11 @@ def dummy_inputs(self) -> Dict[str, tf.Tensor]: Returns: `Dict[str, tf.Tensor]`: The dummy inputs. """ - dummy_inputs = {} - input_sig = self.input_signature - if self.main_input_name == "input_ids" and input_sig["input_ids"].shape.rank == 2: - dummy_inputs["input_ids"] = tf.constant(DUMMY_INPUTS, dtype=tf.int32) - elif self.main_input_name == "input_ids" and input_sig["input_ids"].shape.rank == 3: - dummy_inputs["input_ids"] = tf.constant(MULTIPLE_CHOICE_DUMMY_INPUTS, dtype=tf.int32) - elif self.main_input_name == "pixel_values": - image_shape = input_sig["pixel_values"].shape.as_list() - if image_shape[0] is None: - image_shape[0] = 3 # matches DUMMY_INPUTS - if None in image_shape[1:]: - raise NotImplementedError( - f"Could not fully infer input tensor shape; dummy inputs or serving sig must be defined manually for {self.__class__.__name__}" - ) - rng = np.random.default_rng(42) - vision_dummy_inputs = rng.random(image_shape).astype(np.float32) - dummy_inputs["pixel_values"] = tf.constant(vision_dummy_inputs, dtype=tf.float32) - else: - raise NotImplementedError( - f"Could not fully infer input shapes, dummy inputs must be defined manually for {self.__class__.__name__}" - ) - return dummy_inputs + return { + key: tf.keras.Input(shape=tensor.shape[1:], dtype=tensor.dtype, name=key) + for key, tensor in input_sig.items() + } @property def framework(self) -> str: From 7da8c8fcc53e62645a12dc003d4308dd51ee1962 Mon Sep 17 00:00:00 2001 From: Matt Date: Fri, 12 May 2023 14:49:19 +0100 Subject: [PATCH 18/49] import DUMMY_INPUTS from the right place --- src/transformers/modeling_tf_utils.py | 2 ++ src/transformers/models/bart/modeling_tf_bart.py | 2 +- src/transformers/models/blenderbot/modeling_tf_blenderbot.py | 2 +- .../models/blenderbot_small/modeling_tf_blenderbot_small.py | 2 +- src/transformers/models/blip/modeling_tf_blip.py | 2 +- src/transformers/models/blip/modeling_tf_blip_text.py | 1 - src/transformers/models/clip/modeling_tf_clip.py | 2 +- src/transformers/models/marian/modeling_tf_marian.py | 2 +- src/transformers/models/mbart/modeling_tf_mbart.py | 2 +- src/transformers/models/opt/modeling_tf_opt.py | 2 +- src/transformers/models/pegasus/modeling_tf_pegasus.py | 2 +- 11 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/transformers/modeling_tf_utils.py b/src/transformers/modeling_tf_utils.py index 21effe1f2e4f..7d85c535d759 100644 --- a/src/transformers/modeling_tf_utils.py +++ b/src/transformers/modeling_tf_utils.py @@ -1114,6 +1114,8 @@ def dummy_inputs(self) -> Dict[str, tf.Tensor]: `Dict[str, tf.Tensor]`: The dummy inputs. """ input_sig = self.input_signature + if not all(tensor_spec.shape[0] is None for tensor_spec in self.input_signature.values()): + raise ValueError("The first dimension of the input tensors should be None.") return { key: tf.keras.Input(shape=tensor.shape[1:], dtype=tensor.dtype, name=key) for key, tensor in input_sig.items() diff --git a/src/transformers/models/bart/modeling_tf_bart.py b/src/transformers/models/bart/modeling_tf_bart.py index 5690e022adaa..91e01574ad48 100644 --- a/src/transformers/models/bart/modeling_tf_bart.py +++ b/src/transformers/models/bart/modeling_tf_bart.py @@ -34,7 +34,6 @@ # Public API from ...modeling_tf_utils import ( - DUMMY_INPUTS, TFCausalLanguageModelingLoss, TFModelInputType, TFPreTrainedModel, @@ -44,6 +43,7 @@ ) from ...tf_utils import check_embeddings_within_bounds, shape_list, stable_softmax from ...utils import ( + DUMMY_INPUTS, ContextManagers, add_code_sample_docstrings, add_end_docstrings, diff --git a/src/transformers/models/blenderbot/modeling_tf_blenderbot.py b/src/transformers/models/blenderbot/modeling_tf_blenderbot.py index 66f00d89f897..92baa551c7ac 100644 --- a/src/transformers/models/blenderbot/modeling_tf_blenderbot.py +++ b/src/transformers/models/blenderbot/modeling_tf_blenderbot.py @@ -34,7 +34,6 @@ # Public API from ...modeling_tf_utils import ( - DUMMY_INPUTS, TFCausalLanguageModelingLoss, TFPreTrainedModel, keras_serializable, @@ -42,6 +41,7 @@ ) from ...tf_utils import check_embeddings_within_bounds, shape_list, stable_softmax from ...utils import ( + DUMMY_INPUTS, ContextManagers, add_code_sample_docstrings, add_end_docstrings, diff --git a/src/transformers/models/blenderbot_small/modeling_tf_blenderbot_small.py b/src/transformers/models/blenderbot_small/modeling_tf_blenderbot_small.py index 541024470d10..04a4811bd57a 100644 --- a/src/transformers/models/blenderbot_small/modeling_tf_blenderbot_small.py +++ b/src/transformers/models/blenderbot_small/modeling_tf_blenderbot_small.py @@ -33,7 +33,6 @@ # Public API from ...modeling_tf_utils import ( - DUMMY_INPUTS, TFCausalLanguageModelingLoss, TFPreTrainedModel, keras_serializable, @@ -41,6 +40,7 @@ ) from ...tf_utils import check_embeddings_within_bounds, shape_list, stable_softmax from ...utils import ( + DUMMY_INPUTS, ContextManagers, add_code_sample_docstrings, add_end_docstrings, diff --git a/src/transformers/models/blip/modeling_tf_blip.py b/src/transformers/models/blip/modeling_tf_blip.py index 95269e4351d9..b1ad900f2498 100644 --- a/src/transformers/models/blip/modeling_tf_blip.py +++ b/src/transformers/models/blip/modeling_tf_blip.py @@ -23,7 +23,6 @@ from ...modeling_tf_outputs import TFBaseModelOutput, TFBaseModelOutputWithPooling from ...modeling_tf_utils import ( - DUMMY_INPUTS, TFPreTrainedModel, get_initializer, get_tf_activation, @@ -33,6 +32,7 @@ ) from ...tf_utils import check_embeddings_within_bounds, stable_softmax from ...utils import ( + DUMMY_INPUTS, ModelOutput, add_start_docstrings, add_start_docstrings_to_model_forward, diff --git a/src/transformers/models/blip/modeling_tf_blip_text.py b/src/transformers/models/blip/modeling_tf_blip_text.py index bff81223375c..ec0ee2c43c86 100644 --- a/src/transformers/models/blip/modeling_tf_blip_text.py +++ b/src/transformers/models/blip/modeling_tf_blip_text.py @@ -27,7 +27,6 @@ TFCausalLMOutputWithCrossAttentions, ) from ...modeling_tf_utils import ( - DUMMY_INPUTS, TFPreTrainedModel, get_initializer, get_tf_activation, diff --git a/src/transformers/models/clip/modeling_tf_clip.py b/src/transformers/models/clip/modeling_tf_clip.py index 9b7976f41366..0ac496c5b7fc 100644 --- a/src/transformers/models/clip/modeling_tf_clip.py +++ b/src/transformers/models/clip/modeling_tf_clip.py @@ -29,7 +29,6 @@ # Public API from ...modeling_tf_utils import ( - DUMMY_INPUTS, TFModelInputType, TFPreTrainedModel, get_initializer, @@ -38,6 +37,7 @@ ) from ...tf_utils import check_embeddings_within_bounds, shape_list, stable_softmax from ...utils import ( + DUMMY_INPUTS, ModelOutput, add_start_docstrings, add_start_docstrings_to_model_forward, diff --git a/src/transformers/models/marian/modeling_tf_marian.py b/src/transformers/models/marian/modeling_tf_marian.py index 208e9b8335d7..bdd6defd0a97 100644 --- a/src/transformers/models/marian/modeling_tf_marian.py +++ b/src/transformers/models/marian/modeling_tf_marian.py @@ -33,7 +33,6 @@ # Public API from ...modeling_tf_utils import ( - DUMMY_INPUTS, TFCausalLanguageModelingLoss, TFPreTrainedModel, keras_serializable, @@ -41,6 +40,7 @@ ) from ...tf_utils import check_embeddings_within_bounds, shape_list, stable_softmax from ...utils import ( + DUMMY_INPUTS, ContextManagers, add_code_sample_docstrings, add_end_docstrings, diff --git a/src/transformers/models/mbart/modeling_tf_mbart.py b/src/transformers/models/mbart/modeling_tf_mbart.py index 293c564141b3..af88634d97a7 100644 --- a/src/transformers/models/mbart/modeling_tf_mbart.py +++ b/src/transformers/models/mbart/modeling_tf_mbart.py @@ -32,7 +32,6 @@ # Public API from ...modeling_tf_utils import ( - DUMMY_INPUTS, TFCausalLanguageModelingLoss, TFModelInputType, TFPreTrainedModel, @@ -41,6 +40,7 @@ ) from ...tf_utils import check_embeddings_within_bounds, shape_list, stable_softmax from ...utils import ( + DUMMY_INPUTS, ContextManagers, add_code_sample_docstrings, add_end_docstrings, diff --git a/src/transformers/models/opt/modeling_tf_opt.py b/src/transformers/models/opt/modeling_tf_opt.py index 227e56fdef55..a4e016c90899 100644 --- a/src/transformers/models/opt/modeling_tf_opt.py +++ b/src/transformers/models/opt/modeling_tf_opt.py @@ -27,7 +27,6 @@ # Public API from ...modeling_tf_utils import ( - DUMMY_INPUTS, TFCausalLanguageModelingLoss, TFModelInputType, TFPreTrainedModel, @@ -37,6 +36,7 @@ ) from ...tf_utils import check_embeddings_within_bounds, shape_list, stable_softmax from ...utils import ( + DUMMY_INPUTS, add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, diff --git a/src/transformers/models/pegasus/modeling_tf_pegasus.py b/src/transformers/models/pegasus/modeling_tf_pegasus.py index 7de1542ebe47..976bcc3aebae 100644 --- a/src/transformers/models/pegasus/modeling_tf_pegasus.py +++ b/src/transformers/models/pegasus/modeling_tf_pegasus.py @@ -33,7 +33,6 @@ # Public API from ...modeling_tf_utils import ( - DUMMY_INPUTS, TFCausalLanguageModelingLoss, TFModelInputType, TFPreTrainedModel, @@ -42,6 +41,7 @@ ) from ...tf_utils import check_embeddings_within_bounds, shape_list, stable_softmax from ...utils import ( + DUMMY_INPUTS, ContextManagers, add_code_sample_docstrings, add_end_docstrings, From 1b0f3804e5ec27fc5b4e73761e70f22bea34a14b Mon Sep 17 00:00:00 2001 From: Matt Date: Fri, 12 May 2023 14:53:46 +0100 Subject: [PATCH 19/49] import DUMMY_INPUTS from the right place --- src/transformers/models/groupvit/modeling_tf_groupvit.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transformers/models/groupvit/modeling_tf_groupvit.py b/src/transformers/models/groupvit/modeling_tf_groupvit.py index 718884720980..e51194bd14ae 100644 --- a/src/transformers/models/groupvit/modeling_tf_groupvit.py +++ b/src/transformers/models/groupvit/modeling_tf_groupvit.py @@ -28,7 +28,6 @@ from ...activations_tf import get_tf_activation from ...modeling_tf_outputs import TFBaseModelOutput, TFBaseModelOutputWithPooling from ...modeling_tf_utils import ( - DUMMY_INPUTS, TFModelInputType, TFPreTrainedModel, get_initializer, @@ -37,6 +36,7 @@ ) from ...tf_utils import check_embeddings_within_bounds, shape_list, stable_softmax from ...utils import ( + DUMMY_INPUTS, ModelOutput, add_start_docstrings, add_start_docstrings_to_model_forward, From b867d7d5eb8cd9c84cd56b6151f64c63007c6902 Mon Sep 17 00:00:00 2001 From: Matt Date: Fri, 12 May 2023 15:06:45 +0100 Subject: [PATCH 20/49] Support cross-attention in the dummies --- src/transformers/modeling_tf_utils.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/transformers/modeling_tf_utils.py b/src/transformers/modeling_tf_utils.py index 7d85c535d759..aa719aa77a9c 100644 --- a/src/transformers/modeling_tf_utils.py +++ b/src/transformers/modeling_tf_utils.py @@ -1116,10 +1116,24 @@ def dummy_inputs(self) -> Dict[str, tf.Tensor]: input_sig = self.input_signature if not all(tensor_spec.shape[0] is None for tensor_spec in self.input_signature.values()): raise ValueError("The first dimension of the input tensors should be None.") - return { + dummies = { key: tf.keras.Input(shape=tensor.shape[1:], dtype=tensor.dtype, name=key) for key, tensor in input_sig.items() } + if self.config.add_cross_attention: + if ( + "encoder_hidden_states" not in dummies + and "encoder_hidden_states" in inspect.signature(self.call).parameters + and self.main_input_name == "input_ids" + ): + dummies["encoder_hidden_states"] = tf.keras.Input( + shape=(None, self.config.hidden_size), dtype=tf.float32, name="encoder_hidden_states" + ) + else: + raise NotImplementedError( + "Model has cross-attention but we couldn't infer the shape for the encoder hidden states. Please manually override dummy_inputs!" + ) + return dummies @property def framework(self) -> str: From 5a3bb9cf37ac10934fe2f34ae6a48f63e871f9ad Mon Sep 17 00:00:00 2001 From: Matt Date: Fri, 12 May 2023 15:10:23 +0100 Subject: [PATCH 21/49] Support cross-attention in the dummies --- src/transformers/modeling_tf_utils.py | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/src/transformers/modeling_tf_utils.py b/src/transformers/modeling_tf_utils.py index aa719aa77a9c..1bbab03ab8db 100644 --- a/src/transformers/modeling_tf_utils.py +++ b/src/transformers/modeling_tf_utils.py @@ -1120,19 +1120,16 @@ def dummy_inputs(self) -> Dict[str, tf.Tensor]: key: tf.keras.Input(shape=tensor.shape[1:], dtype=tensor.dtype, name=key) for key, tensor in input_sig.items() } - if self.config.add_cross_attention: - if ( - "encoder_hidden_states" not in dummies - and "encoder_hidden_states" in inspect.signature(self.call).parameters - and self.main_input_name == "input_ids" - ): - dummies["encoder_hidden_states"] = tf.keras.Input( - shape=(None, self.config.hidden_size), dtype=tf.float32, name="encoder_hidden_states" - ) - else: - raise NotImplementedError( - "Model has cross-attention but we couldn't infer the shape for the encoder hidden states. Please manually override dummy_inputs!" - ) + if self.config.add_cross_attention and "encoder_hidden_states" in inspect.signature(self.call).parameters: + if "encoder_hidden_states" not in dummies: + if self.main_input_name == "input_ids": + dummies["encoder_hidden_states"] = tf.keras.Input( + shape=(None, self.config.hidden_size), dtype=tf.float32, name="encoder_hidden_states" + ) + else: + raise NotImplementedError( + "Model has cross-attention but we couldn't infer the shape for the encoder hidden states. Please manually override dummy_inputs!" + ) return dummies @property From 526821c510ab69a1fc9f216e96a465ce3e4c2851 Mon Sep 17 00:00:00 2001 From: Matt Date: Fri, 12 May 2023 15:51:10 +0100 Subject: [PATCH 22/49] Complete removal of dummy/serving overrides in BERT --- src/transformers/modeling_tf_utils.py | 18 +-- .../models/bert/modeling_tf_bert.py | 113 ------------------ tests/test_modeling_tf_common.py | 5 +- 3 files changed, 14 insertions(+), 122 deletions(-) diff --git a/src/transformers/modeling_tf_utils.py b/src/transformers/modeling_tf_utils.py index 1bbab03ab8db..40e74448ec88 100644 --- a/src/transformers/modeling_tf_utils.py +++ b/src/transformers/modeling_tf_utils.py @@ -1113,18 +1113,15 @@ def dummy_inputs(self) -> Dict[str, tf.Tensor]: Returns: `Dict[str, tf.Tensor]`: The dummy inputs. """ - input_sig = self.input_signature - if not all(tensor_spec.shape[0] is None for tensor_spec in self.input_signature.values()): - raise ValueError("The first dimension of the input tensors should be None.") - dummies = { - key: tf.keras.Input(shape=tensor.shape[1:], dtype=tensor.dtype, name=key) - for key, tensor in input_sig.items() - } + dummies = {} + for key, spec in self.input_signature.items(): + # 2 is the most correct arbitrary size. I will not be taking questions + dummies[key] = tf.ones(shape=[dim if dim is not None else 2 for dim in spec.shape], dtype=spec.dtype) if self.config.add_cross_attention and "encoder_hidden_states" in inspect.signature(self.call).parameters: if "encoder_hidden_states" not in dummies: if self.main_input_name == "input_ids": dummies["encoder_hidden_states"] = tf.keras.Input( - shape=(None, self.config.hidden_size), dtype=tf.float32, name="encoder_hidden_states" + shape=(2, self.config.hidden_size), dtype=tf.float32, name="encoder_hidden_states" ) else: raise NotImplementedError( @@ -1280,6 +1277,11 @@ def serving_output(self, output): output[key] = tf.convert_to_tensor(output[key]) except ValueError: pass # Layers may not have the same dimensions + if "cross_attentions" in output: + if not (self.config.output_attentions and self.config.add_cross_attention): + output["cross_attentions"] = None + if output["cross_attentions"] is not None: + output["cross_attentions"] = tf.convert_to_tensor(output["cross_attentions"]) return output def can_generate(self) -> bool: diff --git a/src/transformers/models/bert/modeling_tf_bert.py b/src/transformers/models/bert/modeling_tf_bert.py index df78d03a0074..b8f1945e0845 100644 --- a/src/transformers/models/bert/modeling_tf_bert.py +++ b/src/transformers/models/bert/modeling_tf_bert.py @@ -54,8 +54,6 @@ ) from ...tf_utils import check_embeddings_within_bounds, shape_list, stable_softmax from ...utils import ( - DUMMY_INPUTS, - MULTIPLE_CHOICE_DUMMY_INPUTS, ModelOutput, add_code_sample_docstrings, add_start_docstrings, @@ -903,24 +901,6 @@ class TFBertPreTrainedModel(TFPreTrainedModel): config_class = BertConfig base_model_prefix = "bert" - @property - def dummy_inputs(self): - """ - Dummy inputs to build the network. - - Returns: - `Dict[str, tf.Tensor]`: The dummy inputs. - """ - dummy = {"input_ids": tf.constant(DUMMY_INPUTS, dtype=tf.int32)} - # Add `encoder_hidden_states` to make the cross-attention layers' weights initialized - if self.config.add_cross_attention: - batch_size, seq_len = tf.constant(DUMMY_INPUTS).shape - shape = (batch_size, seq_len) + (self.config.hidden_size,) - h = tf.random.uniform(shape=shape) - dummy["encoder_hidden_states"] = h - - return dummy - @dataclass class TFBertForPreTrainingOutput(ModelOutput): @@ -1123,26 +1103,6 @@ def call( ) return outputs - def serving_output( - self, output: TFBaseModelOutputWithPoolingAndCrossAttentions - ) -> TFBaseModelOutputWithPoolingAndCrossAttentions: - output_cache = self.config.use_cache and self.config.is_decoder - pkv = tf.convert_to_tensor(output.past_key_values) if output_cache else None - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - cross_attns = tf.convert_to_tensor(output.cross_attentions) if output.cross_attentions is not None else None - if not (self.config.output_attentions and self.config.add_cross_attention): - cross_attns = None - - return TFBaseModelOutputWithPoolingAndCrossAttentions( - last_hidden_state=output.last_hidden_state, - pooler_output=output.pooler_output, - past_key_values=pkv, - hidden_states=hs, - attentions=attns, - cross_attentions=cross_attns, - ) - @add_start_docstrings( """ @@ -1255,17 +1215,6 @@ def call( attentions=outputs.attentions, ) - def serving_output(self, output: TFBertForPreTrainingOutput) -> TFBertForPreTrainingOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFBertForPreTrainingOutput( - prediction_logits=output.prediction_logits, - seq_relationship_logits=output.seq_relationship_logits, - hidden_states=hs, - attentions=attns, - ) - @add_start_docstrings("""Bert Model with a `language modeling` head on top.""", BERT_START_DOCSTRING) class TFBertForMaskedLM(TFBertPreTrainedModel, TFMaskedLanguageModelingLoss): @@ -1352,12 +1301,6 @@ def call( attentions=outputs.attentions, ) - def serving_output(self, output: TFMaskedLMOutput) -> TFMaskedLMOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFMaskedLMOutput(logits=output.logits, hidden_states=hs, attentions=attns) - class TFBertLMHeadModel(TFBertPreTrainedModel, TFCausalLanguageModelingLoss): # names with a '.' represents the authorized unexpected/missing layers when a TF model is loaded from a PT model @@ -1578,12 +1521,6 @@ def call( attentions=outputs.attentions, ) - def serving_output(self, output: TFNextSentencePredictorOutput) -> TFNextSentencePredictorOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFNextSentencePredictorOutput(logits=output.logits, hidden_states=hs, attentions=attns) - @add_start_docstrings( """ @@ -1670,12 +1607,6 @@ def call( attentions=outputs.attentions, ) - def serving_output(self, output: TFSequenceClassifierOutput) -> TFSequenceClassifierOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFSequenceClassifierOutput(logits=output.logits, hidden_states=hs, attentions=attns) - @add_start_docstrings( """ @@ -1698,16 +1629,6 @@ def __init__(self, config: BertConfig, *inputs, **kwargs): units=1, kernel_initializer=get_initializer(config.initializer_range), name="classifier" ) - @property - def dummy_inputs(self) -> Dict[str, tf.Tensor]: - """ - Dummy inputs to build the network. - - Returns: - tf.Tensor with dummy inputs - """ - return {"input_ids": tf.constant(MULTIPLE_CHOICE_DUMMY_INPUTS, dtype=tf.int32)} - @unpack_inputs @add_start_docstrings_to_model_forward(BERT_INPUTS_DOCSTRING.format("batch_size, num_choices, sequence_length")) @add_code_sample_docstrings( @@ -1785,26 +1706,6 @@ def call( attentions=outputs.attentions, ) - @tf.function( - input_signature=[ - { - "input_ids": tf.TensorSpec((None, None, None), tf.int32, name="input_ids"), - "attention_mask": tf.TensorSpec((None, None, None), tf.int32, name="attention_mask"), - "token_type_ids": tf.TensorSpec((None, None, None), tf.int32, name="token_type_ids"), - } - ] - ) - def serving(self, inputs: Dict[str, tf.Tensor]) -> TFMultipleChoiceModelOutput: - output = self.call(input_ids=inputs) - - return self.serving_output(output) - - def serving_output(self, output: TFMultipleChoiceModelOutput) -> TFMultipleChoiceModelOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFMultipleChoiceModelOutput(logits=output.logits, hidden_states=hs, attentions=attns) - @add_start_docstrings( """ @@ -1895,12 +1796,6 @@ def call( attentions=outputs.attentions, ) - def serving_output(self, output: TFTokenClassifierOutput) -> TFTokenClassifierOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFTokenClassifierOutput(logits=output.logits, hidden_states=hs, attentions=attns) - @add_start_docstrings( """ @@ -2002,11 +1897,3 @@ def call( hidden_states=outputs.hidden_states, attentions=outputs.attentions, ) - - def serving_output(self, output: TFQuestionAnsweringModelOutput) -> TFQuestionAnsweringModelOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFQuestionAnsweringModelOutput( - start_logits=output.start_logits, end_logits=output.end_logits, hidden_states=hs, attentions=attns - ) diff --git a/tests/test_modeling_tf_common.py b/tests/test_modeling_tf_common.py index 02d5077e233c..69363686837b 100644 --- a/tests/test_modeling_tf_common.py +++ b/tests/test_modeling_tf_common.py @@ -1677,7 +1677,10 @@ def test_int_support(self): # After testing that the model accepts all int inputs, confirm that its dummies are int32 for key, tensor in model.dummy_inputs.items(): - self.assertTrue(isinstance(tensor, tf.Tensor), "Dummy inputs should be tf.Tensor!") + self.assertTrue( + isinstance(tensor, tf.Tensor) or tf.keras.backend.is_keras_tensor(tensor), + "Dummy inputs should be tf.Tensor!", + ) if tensor.dtype.is_integer: self.assertTrue(tensor.dtype == tf.int32, "Integer dummy inputs should be tf.int32!") From 77917558e6270daed8e658f18b7dbc3ad10d0b84 Mon Sep 17 00:00:00 2001 From: Matt Date: Fri, 12 May 2023 15:52:44 +0100 Subject: [PATCH 23/49] Complete removal of dummy/serving overrides in RoBERTa --- .../models/roberta/modeling_tf_roberta.py | 129 ------------------ 1 file changed, 129 deletions(-) diff --git a/src/transformers/models/roberta/modeling_tf_roberta.py b/src/transformers/models/roberta/modeling_tf_roberta.py index 585c4d31ad0d..9b6c491d2761 100644 --- a/src/transformers/models/roberta/modeling_tf_roberta.py +++ b/src/transformers/models/roberta/modeling_tf_roberta.py @@ -51,8 +51,6 @@ ) from ...tf_utils import check_embeddings_within_bounds, shape_list, stable_softmax from ...utils import ( - DUMMY_INPUTS, - MULTIPLE_CHOICE_DUMMY_INPUTS, add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, @@ -777,38 +775,6 @@ class TFRobertaPreTrainedModel(TFPreTrainedModel): config_class = RobertaConfig base_model_prefix = "roberta" - @property - # Copied from transformers.models.bert.modeling_tf_bert.TFBertPreTrainedModel.dummy_inputs - def dummy_inputs(self): - """ - Dummy inputs to build the network. - - Returns: - `Dict[str, tf.Tensor]`: The dummy inputs. - """ - dummy = {"input_ids": tf.constant(DUMMY_INPUTS, dtype=tf.int32)} - # Add `encoder_hidden_states` to make the cross-attention layers' weights initialized - if self.config.add_cross_attention: - batch_size, seq_len = tf.constant(DUMMY_INPUTS).shape - shape = (batch_size, seq_len) + (self.config.hidden_size,) - h = tf.random.uniform(shape=shape) - dummy["encoder_hidden_states"] = h - - return dummy - - @tf.function( - input_signature=[ - { - "input_ids": tf.TensorSpec((None, None), tf.int32, name="input_ids"), - "attention_mask": tf.TensorSpec((None, None), tf.int32, name="attention_mask"), - } - ] - ) - def serving(self, inputs): - output = self.call(inputs) - - return self.serving_output(output) - ROBERTA_START_DOCSTRING = r""" @@ -980,27 +946,6 @@ def call( return outputs - # Copied from transformers.models.bert.modeling_tf_bert.TFBertModel.serving_output - def serving_output( - self, output: TFBaseModelOutputWithPoolingAndCrossAttentions - ) -> TFBaseModelOutputWithPoolingAndCrossAttentions: - output_cache = self.config.use_cache and self.config.is_decoder - pkv = tf.convert_to_tensor(output.past_key_values) if output_cache else None - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - cross_attns = tf.convert_to_tensor(output.cross_attentions) if output.cross_attentions is not None else None - if not (self.config.output_attentions and self.config.add_cross_attention): - cross_attns = None - - return TFBaseModelOutputWithPoolingAndCrossAttentions( - last_hidden_state=output.last_hidden_state, - pooler_output=output.pooler_output, - past_key_values=pkv, - hidden_states=hs, - attentions=attns, - cross_attentions=cross_attns, - ) - class TFRobertaLMHead(tf.keras.layers.Layer): """Roberta Head for masked language modeling.""" @@ -1131,13 +1076,6 @@ def call( attentions=outputs.attentions, ) - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForMaskedLM.serving_output - def serving_output(self, output: TFMaskedLMOutput) -> TFMaskedLMOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFMaskedLMOutput(logits=output.logits, hidden_states=hs, attentions=attns) - class TFRobertaForCausalLM(TFRobertaPreTrainedModel, TFCausalLanguageModelingLoss): # names with a '.' represents the authorized unexpected/missing layers when a TF model is loaded from a PT model @@ -1260,20 +1198,6 @@ def call( cross_attentions=outputs.cross_attentions, ) - # Copied from transformers.models.bert.modeling_tf_bert.TFBertLMHeadModel.serving_output - def serving_output(self, output: TFCausalLMOutputWithCrossAttentions) -> TFCausalLMOutputWithCrossAttentions: - output_cache = self.config.use_cache and self.config.is_decoder - pkv = tf.convert_to_tensor(output.past_key_values) if output_cache else None - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - cross_attns = tf.convert_to_tensor(output.cross_attentions) if output.cross_attentions is not None else None - if not (self.config.output_attentions and self.config.add_cross_attention): - cross_attns = None - - return TFCausalLMOutputWithCrossAttentions( - logits=output.logits, past_key_values=pkv, hidden_states=hs, attentions=attns, cross_attentions=cross_attns - ) - class TFRobertaClassificationHead(tf.keras.layers.Layer): """Head for sentence-level classification tasks.""" @@ -1378,13 +1302,6 @@ def call( attentions=outputs.attentions, ) - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForSequenceClassification.serving_output - def serving_output(self, output: TFSequenceClassifierOutput) -> TFSequenceClassifierOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFSequenceClassifierOutput(logits=output.logits, hidden_states=hs, attentions=attns) - @add_start_docstrings( """ @@ -1407,16 +1324,6 @@ def __init__(self, config, *inputs, **kwargs): 1, kernel_initializer=get_initializer(config.initializer_range), name="classifier" ) - @property - def dummy_inputs(self): - """ - Dummy inputs to build the network. - - Returns: - tf.Tensor with dummy inputs - """ - return {"input_ids": tf.constant(MULTIPLE_CHOICE_DUMMY_INPUTS, dtype=tf.int32)} - @unpack_inputs @add_start_docstrings_to_model_forward(ROBERTA_INPUTS_DOCSTRING.format("batch_size, num_choices, sequence_length")) @add_code_sample_docstrings( @@ -1485,26 +1392,6 @@ def call( attentions=outputs.attentions, ) - @tf.function( - input_signature=[ - { - "input_ids": tf.TensorSpec((None, None, None), tf.int32, name="input_ids"), - "attention_mask": tf.TensorSpec((None, None, None), tf.int32, name="attention_mask"), - } - ] - ) - def serving(self, inputs): - output = self.call(inputs) - - return self.serving_output(output) - - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForMultipleChoice.serving_output - def serving_output(self, output: TFMultipleChoiceModelOutput) -> TFMultipleChoiceModelOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFMultipleChoiceModelOutput(logits=output.logits, hidden_states=hs, attentions=attns) - @add_start_docstrings( """ @@ -1588,13 +1475,6 @@ def call( attentions=outputs.attentions, ) - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForTokenClassification.serving_output - def serving_output(self, output: TFTokenClassifierOutput) -> TFTokenClassifierOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFTokenClassifierOutput(logits=output.logits, hidden_states=hs, attentions=attns) - @add_start_docstrings( """ @@ -1686,12 +1566,3 @@ def call( hidden_states=outputs.hidden_states, attentions=outputs.attentions, ) - - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForQuestionAnswering.serving_output - def serving_output(self, output: TFQuestionAnsweringModelOutput) -> TFQuestionAnsweringModelOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFQuestionAnsweringModelOutput( - start_logits=output.start_logits, end_logits=output.end_logits, hidden_states=hs, attentions=attns - ) From ef2191a0aed36c0d0238adb4fc681b9f75a13ea6 Mon Sep 17 00:00:00 2001 From: Matt Date: Fri, 12 May 2023 16:25:02 +0100 Subject: [PATCH 24/49] Obliterate lots and lots of serving sig and dummy overrides --- src/transformers/modeling_tf_utils.py | 2 + .../models/albert/modeling_tf_albert.py | 85 ---------- .../models/camembert/modeling_tf_camembert.py | 129 ---------------- .../models/convbert/modeling_tf_convbert.py | 64 -------- .../models/ctrl/modeling_tf_ctrl.py | 23 --- .../distilbert/modeling_tf_distilbert.py | 80 ---------- .../models/electra/modeling_tf_electra.py | 108 +------------ .../models/esm/modeling_tf_esm.py | 93 ----------- .../models/flaubert/modeling_tf_flaubert.py | 58 ------- .../models/layoutlm/modeling_tf_layoutlm.py | 47 ------ .../layoutlmv3/modeling_tf_layoutlmv3.py | 70 +-------- .../mobilebert/modeling_tf_mobilebert.py | 94 +----------- .../models/mpnet/modeling_tf_mpnet.py | 85 ---------- .../models/openai/modeling_tf_openai.py | 61 +------- .../models/rembert/modeling_tf_rembert.py | 111 -------------- .../modeling_tf_roberta_prelayernorm.py | 129 ---------------- src/transformers/models/t5/modeling_tf_t5.py | 74 +-------- .../models/xlm/modeling_tf_xlm.py | 58 ------- .../xlm_roberta/modeling_tf_xlm_roberta.py | 129 ---------------- ...tf_{{cookiecutter.lowercase_modelname}}.py | 145 ------------------ 20 files changed, 22 insertions(+), 1623 deletions(-) diff --git a/src/transformers/modeling_tf_utils.py b/src/transformers/modeling_tf_utils.py index 40e74448ec88..52760d9e6c68 100644 --- a/src/transformers/modeling_tf_utils.py +++ b/src/transformers/modeling_tf_utils.py @@ -1245,6 +1245,8 @@ def input_signature(self) -> Dict[str, tf.TensorSpec]: ) if hasattr(vision_config, "image_size"): pixel_values_shape[2] = pixel_values_shape[3] = vision_config.image_size + elif hasattr(vision_config, "input_size"): + pixel_values_shape[2] = pixel_values_shape[3] = vision_config.input_size else: raise NotImplementedError( "Could not infer input image shape from config, please override input_signature to specify input shapes." diff --git a/src/transformers/models/albert/modeling_tf_albert.py b/src/transformers/models/albert/modeling_tf_albert.py index 57e2414e720d..ad35b6182a4e 100644 --- a/src/transformers/models/albert/modeling_tf_albert.py +++ b/src/transformers/models/albert/modeling_tf_albert.py @@ -49,7 +49,6 @@ ) from ...tf_utils import check_embeddings_within_bounds, shape_list, stable_softmax from ...utils import ( - MULTIPLE_CHOICE_DUMMY_INPUTS, ModelOutput, add_code_sample_docstrings, add_start_docstrings, @@ -826,17 +825,6 @@ def call( return outputs - def serving_output(self, output: TFBaseModelOutputWithPooling) -> TFBaseModelOutputWithPooling: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFBaseModelOutputWithPooling( - last_hidden_state=output.last_hidden_state, - pooler_output=output.pooler_output, - hidden_states=hs, - attentions=attns, - ) - @add_start_docstrings( """ @@ -933,17 +921,6 @@ def call( attentions=outputs.attentions, ) - def serving_output(self, output: TFAlbertForPreTrainingOutput) -> TFAlbertForPreTrainingOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFAlbertForPreTrainingOutput( - prediction_logits=output.prediction_logits, - sop_logits=output.sop_logits, - hidden_states=hs, - attentions=attns, - ) - class TFAlbertSOPHead(tf.keras.layers.Layer): def __init__(self, config: AlbertConfig, **kwargs): @@ -1058,13 +1035,6 @@ def call( attentions=outputs.attentions, ) - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForMaskedLM.serving_output - def serving_output(self, output: TFMaskedLMOutput) -> TFMaskedLMOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFMaskedLMOutput(logits=output.logits, hidden_states=hs, attentions=attns) - @add_start_docstrings( """ @@ -1147,13 +1117,6 @@ def call( attentions=outputs.attentions, ) - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForSequenceClassification.serving_output - def serving_output(self, output: TFSequenceClassifierOutput) -> TFSequenceClassifierOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFSequenceClassifierOutput(logits=output.logits, hidden_states=hs, attentions=attns) - @add_start_docstrings( """ @@ -1237,13 +1200,6 @@ def call( attentions=outputs.attentions, ) - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForTokenClassification.serving_output - def serving_output(self, output: TFTokenClassifierOutput) -> TFTokenClassifierOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFTokenClassifierOutput(logits=output.logits, hidden_states=hs, attentions=attns) - @add_start_docstrings( """ @@ -1339,15 +1295,6 @@ def call( attentions=outputs.attentions, ) - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForQuestionAnswering.serving_output - def serving_output(self, output: TFQuestionAnsweringModelOutput) -> TFQuestionAnsweringModelOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFQuestionAnsweringModelOutput( - start_logits=output.start_logits, end_logits=output.end_logits, hidden_states=hs, attentions=attns - ) - @add_start_docstrings( """ @@ -1370,16 +1317,6 @@ def __init__(self, config: AlbertConfig, *inputs, **kwargs): units=1, kernel_initializer=get_initializer(config.initializer_range), name="classifier" ) - @property - def dummy_inputs(self): - """ - Dummy inputs to build the network. - - Returns: - tf.Tensor with dummy inputs - """ - return {"input_ids": tf.constant(MULTIPLE_CHOICE_DUMMY_INPUTS, dtype=tf.int32)} - @unpack_inputs @add_start_docstrings_to_model_forward(ALBERT_INPUTS_DOCSTRING.format("batch_size, num_choices, sequence_length")) @add_code_sample_docstrings( @@ -1457,25 +1394,3 @@ def call( hidden_states=outputs.hidden_states, attentions=outputs.attentions, ) - - @tf.function( - input_signature=[ - { - "input_ids": tf.TensorSpec((None, None, None), tf.int32, name="input_ids"), - "attention_mask": tf.TensorSpec((None, None, None), tf.int32, name="attention_mask"), - "token_type_ids": tf.TensorSpec((None, None, None), tf.int32, name="token_type_ids"), - } - ] - ) - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForMultipleChoice.serving - def serving(self, inputs: Dict[str, tf.Tensor]) -> TFMultipleChoiceModelOutput: - output = self.call(input_ids=inputs) - - return self.serving_output(output) - - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForMultipleChoice.serving_output - def serving_output(self, output: TFMultipleChoiceModelOutput) -> TFMultipleChoiceModelOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFMultipleChoiceModelOutput(logits=output.logits, hidden_states=hs, attentions=attns) diff --git a/src/transformers/models/camembert/modeling_tf_camembert.py b/src/transformers/models/camembert/modeling_tf_camembert.py index 980462f4be7c..8def74a5b304 100644 --- a/src/transformers/models/camembert/modeling_tf_camembert.py +++ b/src/transformers/models/camembert/modeling_tf_camembert.py @@ -51,8 +51,6 @@ ) from ...tf_utils import check_embeddings_within_bounds, shape_list, stable_softmax from ...utils import ( - DUMMY_INPUTS, - MULTIPLE_CHOICE_DUMMY_INPUTS, add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, @@ -873,38 +871,6 @@ class TFCamembertPreTrainedModel(TFPreTrainedModel): config_class = CamembertConfig base_model_prefix = "roberta" - @property - # Copied from transformers.models.bert.modeling_tf_bert.TFBertPreTrainedModel.dummy_inputs - def dummy_inputs(self): - """ - Dummy inputs to build the network. - - Returns: - `Dict[str, tf.Tensor]`: The dummy inputs. - """ - dummy = {"input_ids": tf.constant(DUMMY_INPUTS, dtype=tf.int32)} - # Add `encoder_hidden_states` to make the cross-attention layers' weights initialized - if self.config.add_cross_attention: - batch_size, seq_len = tf.constant(DUMMY_INPUTS).shape - shape = (batch_size, seq_len) + (self.config.hidden_size,) - h = tf.random.uniform(shape=shape) - dummy["encoder_hidden_states"] = h - - return dummy - - @tf.function( - input_signature=[ - { - "input_ids": tf.TensorSpec((None, None), tf.int32, name="input_ids"), - "attention_mask": tf.TensorSpec((None, None), tf.int32, name="attention_mask"), - } - ] - ) - def serving(self, inputs): - output = self.call(inputs) - - return self.serving_output(output) - @add_start_docstrings( "The bare CamemBERT Model transformer outputting raw hidden-states without any specific head on top.", @@ -979,27 +945,6 @@ def call( return outputs - # Copied from transformers.models.bert.modeling_tf_bert.TFBertModel.serving_output - def serving_output( - self, output: TFBaseModelOutputWithPoolingAndCrossAttentions - ) -> TFBaseModelOutputWithPoolingAndCrossAttentions: - output_cache = self.config.use_cache and self.config.is_decoder - pkv = tf.convert_to_tensor(output.past_key_values) if output_cache else None - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - cross_attns = tf.convert_to_tensor(output.cross_attentions) if output.cross_attentions is not None else None - if not (self.config.output_attentions and self.config.add_cross_attention): - cross_attns = None - - return TFBaseModelOutputWithPoolingAndCrossAttentions( - last_hidden_state=output.last_hidden_state, - pooler_output=output.pooler_output, - past_key_values=pkv, - hidden_states=hs, - attentions=attns, - cross_attentions=cross_attns, - ) - # Copied from transformers.models.roberta.modeling_tf_roberta.TFRobertaLMHead with Roberta->Camembert class TFCamembertLMHead(tf.keras.layers.Layer): @@ -1135,13 +1080,6 @@ def call( attentions=outputs.attentions, ) - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForMaskedLM.serving_output - def serving_output(self, output: TFMaskedLMOutput) -> TFMaskedLMOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFMaskedLMOutput(logits=output.logits, hidden_states=hs, attentions=attns) - # Copied from transformers.models.roberta.modeling_tf_roberta.TFRobertaClassificationHead class TFCamembertClassificationHead(tf.keras.layers.Layer): @@ -1248,13 +1186,6 @@ def call( attentions=outputs.attentions, ) - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForSequenceClassification.serving_output - def serving_output(self, output: TFSequenceClassifierOutput) -> TFSequenceClassifierOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFSequenceClassifierOutput(logits=output.logits, hidden_states=hs, attentions=attns) - @add_start_docstrings( """ @@ -1339,13 +1270,6 @@ def call( attentions=outputs.attentions, ) - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForTokenClassification.serving_output - def serving_output(self, output: TFTokenClassifierOutput) -> TFTokenClassifierOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFTokenClassifierOutput(logits=output.logits, hidden_states=hs, attentions=attns) - @add_start_docstrings( """ @@ -1369,16 +1293,6 @@ def __init__(self, config, *inputs, **kwargs): 1, kernel_initializer=get_initializer(config.initializer_range), name="classifier" ) - @property - def dummy_inputs(self): - """ - Dummy inputs to build the network. - - Returns: - tf.Tensor with dummy inputs - """ - return {"input_ids": tf.constant(MULTIPLE_CHOICE_DUMMY_INPUTS, dtype=tf.int32)} - @unpack_inputs @add_start_docstrings_to_model_forward( CAMEMBERT_INPUTS_DOCSTRING.format("batch_size, num_choices, sequence_length") @@ -1449,26 +1363,6 @@ def call( attentions=outputs.attentions, ) - @tf.function( - input_signature=[ - { - "input_ids": tf.TensorSpec((None, None, None), tf.int32, name="input_ids"), - "attention_mask": tf.TensorSpec((None, None, None), tf.int32, name="attention_mask"), - } - ] - ) - def serving(self, inputs): - output = self.call(inputs) - - return self.serving_output(output) - - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForMultipleChoice.serving_output - def serving_output(self, output: TFMultipleChoiceModelOutput) -> TFMultipleChoiceModelOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFMultipleChoiceModelOutput(logits=output.logits, hidden_states=hs, attentions=attns) - @add_start_docstrings( """ @@ -1562,15 +1456,6 @@ def call( attentions=outputs.attentions, ) - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForQuestionAnswering.serving_output - def serving_output(self, output: TFQuestionAnsweringModelOutput) -> TFQuestionAnsweringModelOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFQuestionAnsweringModelOutput( - start_logits=output.start_logits, end_logits=output.end_logits, hidden_states=hs, attentions=attns - ) - @add_start_docstrings( """CamemBERT Model with a `language modeling` head on top for CLM fine-tuning.""", CAMEMBERT_START_DOCSTRING @@ -1696,17 +1581,3 @@ def call( attentions=outputs.attentions, cross_attentions=outputs.cross_attentions, ) - - # Copied from transformers.models.bert.modeling_tf_bert.TFBertLMHeadModel.serving_output - def serving_output(self, output: TFCausalLMOutputWithCrossAttentions) -> TFCausalLMOutputWithCrossAttentions: - output_cache = self.config.use_cache and self.config.is_decoder - pkv = tf.convert_to_tensor(output.past_key_values) if output_cache else None - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - cross_attns = tf.convert_to_tensor(output.cross_attentions) if output.cross_attentions is not None else None - if not (self.config.output_attentions and self.config.add_cross_attention): - cross_attns = None - - return TFCausalLMOutputWithCrossAttentions( - logits=output.logits, past_key_values=pkv, hidden_states=hs, attentions=attns, cross_attentions=cross_attns - ) diff --git a/src/transformers/models/convbert/modeling_tf_convbert.py b/src/transformers/models/convbert/modeling_tf_convbert.py index 19c2d700dc06..9b2bf2383bb7 100644 --- a/src/transformers/models/convbert/modeling_tf_convbert.py +++ b/src/transformers/models/convbert/modeling_tf_convbert.py @@ -46,7 +46,6 @@ ) from ...tf_utils import check_embeddings_within_bounds, shape_list, stable_softmax from ...utils import ( - MULTIPLE_CHOICE_DUMMY_INPUTS, add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, @@ -770,12 +769,6 @@ def call( return outputs - def serving_output(self, output): - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFBaseModelOutput(last_hidden_state=output.last_hidden_state, hidden_states=hs, attentions=attns) - class TFConvBertMaskedLMHead(tf.keras.layers.Layer): def __init__(self, config, input_embeddings, **kwargs): @@ -907,13 +900,6 @@ def call( attentions=generator_hidden_states.attentions, ) - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForMaskedLM.serving_output - def serving_output(self, output): - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFMaskedLMOutput(logits=output.logits, hidden_states=hs, attentions=attns) - class TFConvBertClassificationHead(tf.keras.layers.Layer): """Head for sentence-level classification tasks.""" @@ -1012,12 +998,6 @@ def call( attentions=outputs.attentions, ) - def serving_output(self, output): - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFSequenceClassifierOutput(logits=output.logits, hidden_states=hs, attentions=attns) - @add_start_docstrings( """ @@ -1038,16 +1018,6 @@ def __init__(self, config, *inputs, **kwargs): 1, kernel_initializer=get_initializer(config.initializer_range), name="classifier" ) - @property - def dummy_inputs(self): - """ - Dummy inputs to build the network. - - Returns: - tf.Tensor with dummy inputs - """ - return {"input_ids": tf.convert_to_tensor(MULTIPLE_CHOICE_DUMMY_INPUTS, dtype=tf.int32)} - @unpack_inputs @add_start_docstrings_to_model_forward( CONVBERT_INPUTS_DOCSTRING.format("batch_size, num_choices, sequence_length") @@ -1121,26 +1091,6 @@ def call( attentions=outputs.attentions, ) - @tf.function( - input_signature=[ - { - "input_ids": tf.TensorSpec((None, None, None), tf.int32, name="input_ids"), - "attention_mask": tf.TensorSpec((None, None, None), tf.int32, name="attention_mask"), - "token_type_ids": tf.TensorSpec((None, None, None), tf.int32, name="token_type_ids"), - } - ] - ) - def serving(self, inputs): - output = self.call(inputs) - - return self.serving_output(output) - - def serving_output(self, output): - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFMultipleChoiceModelOutput(logits=output.logits, hidden_states=hs, attentions=attns) - @add_start_docstrings( """ @@ -1216,12 +1166,6 @@ def call( attentions=outputs.attentions, ) - def serving_output(self, output): - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFTokenClassifierOutput(logits=output.logits, hidden_states=hs, attentions=attns) - @add_start_docstrings( """ @@ -1307,11 +1251,3 @@ def call( hidden_states=outputs.hidden_states, attentions=outputs.attentions, ) - - def serving_output(self, output): - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFQuestionAnsweringModelOutput( - start_logits=output.start_logits, end_logits=output.end_logits, hidden_states=hs, attentions=attns - ) diff --git a/src/transformers/models/ctrl/modeling_tf_ctrl.py b/src/transformers/models/ctrl/modeling_tf_ctrl.py index cddfd4a9e352..4dd9e7392507 100644 --- a/src/transformers/models/ctrl/modeling_tf_ctrl.py +++ b/src/transformers/models/ctrl/modeling_tf_ctrl.py @@ -564,15 +564,6 @@ def call( ) return outputs - def serving_output(self, output): - pkv = tf.convert_to_tensor(output.past_key_values) if self.config.use_cache else None - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFBaseModelOutputWithPast( - last_hidden_state=output.last_hidden_state, past_key_values=pkv, hidden_states=hs, attentions=attns - ) - class TFCTRLLMHead(tf.keras.layers.Layer): def __init__(self, config, input_embeddings, **kwargs): @@ -705,13 +696,6 @@ def call( attentions=transformer_outputs.attentions, ) - def serving_output(self, output): - pkv = tf.convert_to_tensor(output.past_key_values) if self.config.use_cache else None - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFCausalLMOutputWithPast(logits=output.logits, past_key_values=pkv, hidden_states=hs, attentions=attns) - @add_start_docstrings( """ @@ -839,10 +823,3 @@ def call( hidden_states=transformer_outputs.hidden_states, attentions=transformer_outputs.attentions, ) - - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForSequenceClassification.serving_output - def serving_output(self, output: TFSequenceClassifierOutput) -> TFSequenceClassifierOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFSequenceClassifierOutput(logits=output.logits, hidden_states=hs, attentions=attns) diff --git a/src/transformers/models/distilbert/modeling_tf_distilbert.py b/src/transformers/models/distilbert/modeling_tf_distilbert.py index 85a98c2a77fb..6b0e1b0f3feb 100644 --- a/src/transformers/models/distilbert/modeling_tf_distilbert.py +++ b/src/transformers/models/distilbert/modeling_tf_distilbert.py @@ -48,7 +48,6 @@ ) from ...tf_utils import check_embeddings_within_bounds, shape_list, stable_softmax from ...utils import ( - MULTIPLE_CHOICE_DUMMY_INPUTS, add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, @@ -424,19 +423,6 @@ class TFDistilBertPreTrainedModel(TFPreTrainedModel): config_class = DistilBertConfig base_model_prefix = "distilbert" - @tf.function( - input_signature=[ - { - "input_ids": tf.TensorSpec((None, None), tf.int32, name="input_ids"), - "attention_mask": tf.TensorSpec((None, None), tf.int32, name="attention_mask"), - } - ] - ) - def serving(self, inputs): - output = self.call(inputs) - - return self.serving_output(output) - DISTILBERT_START_DOCSTRING = r""" @@ -562,12 +548,6 @@ def call( ) return outputs - def serving_output(self, output): - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFBaseModelOutput(last_hidden_state=output.last_hidden_state, hidden_states=hs, attentions=attns) - class TFDistilBertLMHead(tf.keras.layers.Layer): def __init__(self, config, input_embeddings, **kwargs): @@ -687,13 +667,6 @@ def call( attentions=distilbert_output.attentions, ) - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForMaskedLM.serving_output - def serving_output(self, output: TFMaskedLMOutput) -> TFMaskedLMOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFMaskedLMOutput(logits=output.logits, hidden_states=hs, attentions=attns) - @add_start_docstrings( """ @@ -773,13 +746,6 @@ def call( attentions=distilbert_output.attentions, ) - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForSequenceClassification.serving_output - def serving_output(self, output: TFSequenceClassifierOutput) -> TFSequenceClassifierOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFSequenceClassifierOutput(logits=output.logits, hidden_states=hs, attentions=attns) - @add_start_docstrings( """ @@ -848,13 +814,6 @@ def call( attentions=outputs.attentions, ) - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForTokenClassification.serving_output - def serving_output(self, output: TFTokenClassifierOutput) -> TFTokenClassifierOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFTokenClassifierOutput(logits=output.logits, hidden_states=hs, attentions=attns) - @add_start_docstrings( """ @@ -879,16 +838,6 @@ def __init__(self, config, *inputs, **kwargs): 1, kernel_initializer=get_initializer(config.initializer_range), name="classifier" ) - @property - def dummy_inputs(self): - """ - Dummy inputs to build the network. - - Returns: - tf.Tensor with dummy inputs - """ - return {"input_ids": tf.constant(MULTIPLE_CHOICE_DUMMY_INPUTS, dtype=tf.int32)} - @unpack_inputs @add_start_docstrings_to_model_forward( DISTILBERT_INPUTS_DOCSTRING.format("batch_size, num_choices, sequence_length") @@ -959,26 +908,6 @@ def call( attentions=distilbert_output.attentions, ) - @tf.function( - input_signature=[ - { - "input_ids": tf.TensorSpec((None, None, None), tf.int32, name="input_ids"), - "attention_mask": tf.TensorSpec((None, None, None), tf.int32, name="attention_mask"), - } - ] - ) - def serving(self, inputs): - output = self.call(inputs) - - return self.serving_output(output) - - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForMultipleChoice.serving_output - def serving_output(self, output: TFMultipleChoiceModelOutput) -> TFMultipleChoiceModelOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFMultipleChoiceModelOutput(logits=output.logits, hidden_states=hs, attentions=attns) - @add_start_docstrings( """ @@ -1062,12 +991,3 @@ def call( hidden_states=distilbert_output.hidden_states, attentions=distilbert_output.attentions, ) - - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForQuestionAnswering.serving_output - def serving_output(self, output: TFQuestionAnsweringModelOutput) -> TFQuestionAnsweringModelOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFQuestionAnsweringModelOutput( - start_logits=output.start_logits, end_logits=output.end_logits, hidden_states=hs, attentions=attns - ) diff --git a/src/transformers/models/electra/modeling_tf_electra.py b/src/transformers/models/electra/modeling_tf_electra.py index 7602d43cc0ca..41c64eed369d 100644 --- a/src/transformers/models/electra/modeling_tf_electra.py +++ b/src/transformers/models/electra/modeling_tf_electra.py @@ -20,7 +20,7 @@ import math import warnings from dataclasses import dataclass -from typing import Dict, Optional, Tuple, Union +from typing import Optional, Tuple, Union import numpy as np import tensorflow as tf @@ -49,8 +49,6 @@ ) from ...tf_utils import check_embeddings_within_bounds, shape_list, stable_softmax from ...utils import ( - DUMMY_INPUTS, - MULTIPLE_CHOICE_DUMMY_INPUTS, ModelOutput, add_code_sample_docstrings, add_start_docstrings, @@ -596,25 +594,6 @@ class TFElectraPreTrainedModel(TFPreTrainedModel): _keys_to_ignore_on_load_unexpected = [r"generator_lm_head.weight"] _keys_to_ignore_on_load_missing = [r"dropout"] - @property - # Copied from transformers.models.bert.modeling_tf_bert.TFBertPreTrainedModel.dummy_inputs - def dummy_inputs(self): - """ - Dummy inputs to build the network. - - Returns: - `Dict[str, tf.Tensor]`: The dummy inputs. - """ - dummy = {"input_ids": tf.constant(DUMMY_INPUTS, dtype=tf.int32)} - # Add `encoder_hidden_states` to make the cross-attention layers' weights initialized - if self.config.add_cross_attention: - batch_size, seq_len = tf.constant(DUMMY_INPUTS).shape - shape = (batch_size, seq_len) + (self.config.hidden_size,) - h = tf.random.uniform(shape=shape) - dummy["encoder_hidden_states"] = h - - return dummy - @keras_serializable class TFElectraMainLayer(tf.keras.layers.Layer): @@ -998,23 +977,6 @@ def call( return outputs - def serving_output(self, output): - output_cache = self.config.use_cache and self.config.is_decoder - pkv = tf.convert_to_tensor(output.past_key_values) if output_cache else None - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - cross_attns = tf.convert_to_tensor(output.cross_attentions) if output.cross_attentions is not None else None - if not (self.config.output_attentions and self.config.add_cross_attention): - cross_attns = None - - return TFBaseModelOutputWithPastAndCrossAttentions( - last_hidden_state=output.last_hidden_state, - past_key_values=pkv, - hidden_states=hs, - attentions=attns, - cross_attentions=cross_attns, - ) - @add_start_docstrings( """ @@ -1087,12 +1049,6 @@ def call( attentions=discriminator_hidden_states.attentions, ) - def serving_output(self, output): - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFElectraForPreTrainingOutput(logits=output.logits, hidden_states=hs, attentions=attns) - class TFElectraMaskedLMHead(tf.keras.layers.Layer): def __init__(self, config, input_embeddings, **kwargs): @@ -1221,13 +1177,6 @@ def call( attentions=generator_hidden_states.attentions, ) - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForMaskedLM.serving_output - def serving_output(self, output: TFMaskedLMOutput) -> TFMaskedLMOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFMaskedLMOutput(logits=output.logits, hidden_states=hs, attentions=attns) - class TFElectraClassificationHead(tf.keras.layers.Layer): """Head for sentence-level classification tasks.""" @@ -1329,13 +1278,6 @@ def call( attentions=outputs.attentions, ) - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForSequenceClassification.serving_output - def serving_output(self, output: TFSequenceClassifierOutput) -> TFSequenceClassifierOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFSequenceClassifierOutput(logits=output.logits, hidden_states=hs, attentions=attns) - @add_start_docstrings( """ @@ -1356,16 +1298,6 @@ def __init__(self, config, *inputs, **kwargs): 1, kernel_initializer=get_initializer(config.initializer_range), name="classifier" ) - @property - def dummy_inputs(self): - """ - Dummy inputs to build the network. - - Returns: - tf.Tensor with dummy inputs - """ - return {"input_ids": tf.constant(MULTIPLE_CHOICE_DUMMY_INPUTS, dtype=tf.int32)} - @unpack_inputs @add_start_docstrings_to_model_forward(ELECTRA_INPUTS_DOCSTRING.format("batch_size, num_choices, sequence_length")) @add_code_sample_docstrings( @@ -1438,28 +1370,6 @@ def call( attentions=outputs.attentions, ) - @tf.function( - input_signature=[ - { - "input_ids": tf.TensorSpec((None, None, None), tf.int32, name="input_ids"), - "attention_mask": tf.TensorSpec((None, None, None), tf.int32, name="attention_mask"), - "token_type_ids": tf.TensorSpec((None, None, None), tf.int32, name="token_type_ids"), - } - ] - ) - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForMultipleChoice.serving - def serving(self, inputs: Dict[str, tf.Tensor]): - output = self.call(input_ids=inputs) - - return self.serving_output(output) - - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForMultipleChoice.serving_output - def serving_output(self, output: TFMultipleChoiceModelOutput) -> TFMultipleChoiceModelOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFMultipleChoiceModelOutput(logits=output.logits, hidden_states=hs, attentions=attns) - @add_start_docstrings( """ @@ -1538,13 +1448,6 @@ def call( attentions=discriminator_hidden_states.attentions, ) - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForTokenClassification.serving_output - def serving_output(self, output: TFTokenClassifierOutput) -> TFTokenClassifierOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFTokenClassifierOutput(logits=output.logits, hidden_states=hs, attentions=attns) - @add_start_docstrings( """ @@ -1638,12 +1541,3 @@ def call( hidden_states=discriminator_hidden_states.hidden_states, attentions=discriminator_hidden_states.attentions, ) - - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForQuestionAnswering.serving_output - def serving_output(self, output: TFQuestionAnsweringModelOutput) -> TFQuestionAnsweringModelOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFQuestionAnsweringModelOutput( - start_logits=output.start_logits, end_logits=output.end_logits, hidden_states=hs, attentions=attns - ) diff --git a/src/transformers/models/esm/modeling_tf_esm.py b/src/transformers/models/esm/modeling_tf_esm.py index df4ea54f83bc..126473ee529a 100644 --- a/src/transformers/models/esm/modeling_tf_esm.py +++ b/src/transformers/models/esm/modeling_tf_esm.py @@ -1038,39 +1038,6 @@ def call( ) return outputs - @tf.function( - input_signature=[ - { - "input_ids": tf.TensorSpec((None, None), tf.int32, name="input_ids"), - "attention_mask": tf.TensorSpec((None, None), tf.int32, name="attention_mask"), - } - ] - ) - def serving(self, inputs): - output = self.call(inputs) - - return self.serving_output(output) - - def serving_output( - self, output: TFBaseModelOutputWithPoolingAndCrossAttentions - ) -> TFBaseModelOutputWithPoolingAndCrossAttentions: - output_cache = self.config.use_cache and self.config.is_decoder - pkv = tf.convert_to_tensor(output.past_key_values) if output_cache else None - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - cross_attns = tf.convert_to_tensor(output.cross_attentions) if output.cross_attentions is not None else None - if not (self.config.output_attentions and self.config.add_cross_attention): - cross_attns = None - - return TFBaseModelOutputWithPoolingAndCrossAttentions( - last_hidden_state=output.last_hidden_state, - pooler_output=output.pooler_output, - past_key_values=pkv, - hidden_states=hs, - attentions=attns, - cross_attentions=cross_attns, - ) - def predict_contacts(self, tokens, attention_mask): return self.esm.predict_contacts(tokens, attention_mask) @@ -1170,26 +1137,6 @@ def call( attentions=outputs.attentions, ) - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForMaskedLM.serving_output - def serving_output(self, output: TFMaskedLMOutput) -> TFMaskedLMOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFMaskedLMOutput(logits=output.logits, hidden_states=hs, attentions=attns) - - @tf.function( - input_signature=[ - { - "input_ids": tf.TensorSpec((None, None), tf.int32, name="input_ids"), - "attention_mask": tf.TensorSpec((None, None), tf.int32, name="attention_mask"), - } - ] - ) - def serving(self, inputs): - output = self.call(inputs) - - return self.serving_output(output) - def predict_contacts(self, tokens, attention_mask): return self.esm.predict_contacts(tokens, attention_mask) @@ -1310,26 +1257,6 @@ def call( attentions=outputs.attentions, ) - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForSequenceClassification.serving_output - def serving_output(self, output: TFSequenceClassifierOutput) -> TFSequenceClassifierOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFSequenceClassifierOutput(logits=output.logits, hidden_states=hs, attentions=attns) - - @tf.function( - input_signature=[ - { - "input_ids": tf.TensorSpec((None, None, None), tf.int32, name="input_ids"), - "attention_mask": tf.TensorSpec((None, None, None), tf.int32, name="attention_mask"), - } - ] - ) - def serving(self, inputs): - output = self.call(inputs) - - return self.serving_output(output) - @add_start_docstrings( """ @@ -1406,26 +1333,6 @@ def call( attentions=outputs.attentions, ) - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForTokenClassification.serving_output - def serving_output(self, output: TFTokenClassifierOutput) -> TFTokenClassifierOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFTokenClassifierOutput(logits=output.logits, hidden_states=hs, attentions=attns) - - @tf.function( - input_signature=[ - { - "input_ids": tf.TensorSpec((None, None), tf.int32, name="input_ids"), - "attention_mask": tf.TensorSpec((None, None), tf.int32, name="attention_mask"), - } - ] - ) - def serving(self, inputs): - output = self.call(inputs) - - return self.serving_output(output) - class TFEsmClassificationHead(Layer): """Head for sentence-level classification tasks.""" diff --git a/src/transformers/models/flaubert/modeling_tf_flaubert.py b/src/transformers/models/flaubert/modeling_tf_flaubert.py index 7f93caebb000..068119d35f17 100644 --- a/src/transformers/models/flaubert/modeling_tf_flaubert.py +++ b/src/transformers/models/flaubert/modeling_tf_flaubert.py @@ -290,13 +290,6 @@ def call( return outputs - # Copied from transformers.models.distilbert.modeling_tf_distilbert.TFDistilBertModel.serving_output - def serving_output(self, output): - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFBaseModelOutput(last_hidden_state=output.last_hidden_state, hidden_states=hs, attentions=attns) - # Copied from transformers.models.xlm.modeling_tf_xlm.TFXLMMultiHeadAttention with XLM->Flaubert class TFFlaubertMultiHeadAttention(tf.keras.layers.Layer): @@ -845,12 +838,6 @@ def call( logits=outputs, hidden_states=transformer_outputs.hidden_states, attentions=transformer_outputs.attentions ) - def serving_output(self, output): - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFFlaubertWithLMHeadModelOutput(logits=output.logits, hidden_states=hs, attentions=attns) - @add_start_docstrings( """ @@ -930,13 +917,6 @@ def call( attentions=transformer_outputs.attentions, ) - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForSequenceClassification.serving_output - def serving_output(self, output: TFSequenceClassifierOutput) -> TFSequenceClassifierOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFSequenceClassifierOutput(logits=output.logits, hidden_states=hs, attentions=attns) - @add_start_docstrings( """ @@ -1029,15 +1009,6 @@ def call( attentions=transformer_outputs.attentions, ) - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForQuestionAnswering.serving_output - def serving_output(self, output: TFQuestionAnsweringModelOutput) -> TFQuestionAnsweringModelOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFQuestionAnsweringModelOutput( - start_logits=output.start_logits, end_logits=output.end_logits, hidden_states=hs, attentions=attns - ) - @add_start_docstrings( """ @@ -1119,13 +1090,6 @@ def call( attentions=transformer_outputs.attentions, ) - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForTokenClassification.serving_output - def serving_output(self, output: TFTokenClassifierOutput) -> TFTokenClassifierOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFTokenClassifierOutput(logits=output.logits, hidden_states=hs, attentions=attns) - @add_start_docstrings( """ @@ -1247,25 +1211,3 @@ def call( hidden_states=transformer_outputs.hidden_states, attentions=transformer_outputs.attentions, ) - - @tf.function( - input_signature=[ - { - "input_ids": tf.TensorSpec((None, None, None), tf.int32, name="input_ids"), - "attention_mask": tf.TensorSpec((None, None, None), tf.int32, name="attention_mask"), - "token_type_ids": tf.TensorSpec((None, None, None), tf.int32, name="token_type_ids"), - } - ] - ) - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForMultipleChoice.serving - def serving(self, inputs: Dict[str, tf.Tensor]): - output = self.call(input_ids=inputs) - - return self.serving_output(output) - - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForMultipleChoice.serving_output - def serving_output(self, output: TFMultipleChoiceModelOutput) -> TFMultipleChoiceModelOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFMultipleChoiceModelOutput(logits=output.logits, hidden_states=hs, attentions=attns) diff --git a/src/transformers/models/layoutlm/modeling_tf_layoutlm.py b/src/transformers/models/layoutlm/modeling_tf_layoutlm.py index 67128e0c1338..c75660946859 100644 --- a/src/transformers/models/layoutlm/modeling_tf_layoutlm.py +++ b/src/transformers/models/layoutlm/modeling_tf_layoutlm.py @@ -986,27 +986,6 @@ def call( return outputs - # Copied from transformers.models.bert.modeling_tf_bert.TFBertModel.serving_output - def serving_output( - self, output: TFBaseModelOutputWithPoolingAndCrossAttentions - ) -> TFBaseModelOutputWithPoolingAndCrossAttentions: - output_cache = self.config.use_cache and self.config.is_decoder - pkv = tf.convert_to_tensor(output.past_key_values) if output_cache else None - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - cross_attns = tf.convert_to_tensor(output.cross_attentions) if output.cross_attentions is not None else None - if not (self.config.output_attentions and self.config.add_cross_attention): - cross_attns = None - - return TFBaseModelOutputWithPoolingAndCrossAttentions( - last_hidden_state=output.last_hidden_state, - pooler_output=output.pooler_output, - past_key_values=pkv, - hidden_states=hs, - attentions=attns, - cross_attentions=cross_attns, - ) - @add_start_docstrings("""LayoutLM Model with a `language modeling` head on top.""", LAYOUTLM_START_DOCSTRING) class TFLayoutLMForMaskedLM(TFLayoutLMPreTrainedModel, TFMaskedLanguageModelingLoss): @@ -1128,12 +1107,6 @@ def call( attentions=outputs.attentions, ) - def serving_output(self, output: TFMaskedLMOutput) -> TFMaskedLMOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFMaskedLMOutput(logits=output.logits, hidden_states=hs, attentions=attns) - @add_start_docstrings( """ @@ -1252,12 +1225,6 @@ def call( attentions=outputs.attentions, ) - def serving_output(self, output: TFSequenceClassifierOutput) -> TFSequenceClassifierOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFSequenceClassifierOutput(logits=output.logits, hidden_states=hs, attentions=attns) - @add_start_docstrings( """ @@ -1380,12 +1347,6 @@ def call( attentions=outputs.attentions, ) - def serving_output(self, output: TFTokenClassifierOutput) -> TFTokenClassifierOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFTokenClassifierOutput(logits=output.logits, hidden_states=hs, attentions=attns) - @add_start_docstrings( """ @@ -1524,11 +1485,3 @@ def call( hidden_states=outputs.hidden_states, attentions=outputs.attentions, ) - - def serving_output(self, output: TFTokenClassifierOutput) -> TFTokenClassifierOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFQuestionAnsweringModelOutput( - start_logits=output.start_logits, end_logits=output.end_logits, hidden_states=hs, attentions=attns - ) diff --git a/src/transformers/models/layoutlmv3/modeling_tf_layoutlmv3.py b/src/transformers/models/layoutlmv3/modeling_tf_layoutlmv3.py index 67377c5baf8a..feba69eafc2a 100644 --- a/src/transformers/models/layoutlmv3/modeling_tf_layoutlmv3.py +++ b/src/transformers/models/layoutlmv3/modeling_tf_layoutlmv3.py @@ -19,7 +19,7 @@ import collections import math -from typing import Dict, List, Optional, Tuple, Union +from typing import List, Optional, Tuple, Union import tensorflow as tf @@ -980,37 +980,10 @@ class TFLayoutLMv3PreTrainedModel(TFPreTrainedModel): base_model_prefix = "layoutlmv3" @property - def dummy_inputs(self) -> Dict[str, tf.Tensor]: - size = self.config.input_size - image_shape = (2, self.config.num_channels, size, size) - pixel_values = tf.random.uniform(shape=image_shape, minval=-1, maxval=1) - return { - "input_ids": tf.constant(_DUMMY_INPUT_IDS, dtype=tf.int32), - "bbox": tf.constant(_DUMMY_BBOX, dtype=tf.int32), - "pixel_values": pixel_values, - } - - @tf.function( - input_signature=[ - { - "input_ids": tf.TensorSpec((None, None), tf.int32, name="input_ids"), - "bbox": tf.TensorSpec((None, None, 4), tf.int32, name="bbox"), - "pixel_values": tf.TensorSpec((None, None, None, None), tf.float32, name="pixel_values"), - "attention_mask": tf.TensorSpec((None, None), tf.int32, name="attention_mask"), - } - ] - ) - def serving(self, inputs): - """ - Method used for serving the model. - - Args: - inputs (`Dict[str, tf.Tensor]`): - The input of the saved model as a dictionary of tensors. - """ - output = self.call(inputs) - - return self.serving_output(output) + def input_signature(self): + sig = super().input_signature + sig["bbox"] = tf.TensorSpec((None, None, 4), tf.int32, name="bbox") + return sig LAYOUTLMV3_START_DOCSTRING = r""" @@ -1207,16 +1180,6 @@ def call( return outputs - def serving_output(self, output: TFBaseModelOutput) -> TFBaseModelOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFBaseModelOutput( - last_hidden_state=output.last_hidden_state, - hidden_states=hs, - attentions=attns, - ) - class TFLayoutLMv3ClassificationHead(tf.keras.layers.Layer): """ @@ -1354,13 +1317,6 @@ def call( attentions=outputs.attentions, ) - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForSequenceClassification.serving_output - def serving_output(self, output: TFSequenceClassifierOutput) -> TFSequenceClassifierOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFSequenceClassifierOutput(logits=output.logits, hidden_states=hs, attentions=attns) - @add_start_docstrings( """ @@ -1484,13 +1440,6 @@ def call( attentions=outputs.attentions, ) - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForTokenClassification.serving_output - def serving_output(self, output: TFTokenClassifierOutput) -> TFTokenClassifierOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFTokenClassifierOutput(logits=output.logits, hidden_states=hs, attentions=attns) - @add_start_docstrings( """ @@ -1618,12 +1567,3 @@ def call( hidden_states=outputs.hidden_states, attentions=outputs.attentions, ) - - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForQuestionAnswering.serving_output - def serving_output(self, output: TFQuestionAnsweringModelOutput) -> TFQuestionAnsweringModelOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFQuestionAnsweringModelOutput( - start_logits=output.start_logits, end_logits=output.end_logits, hidden_states=hs, attentions=attns - ) diff --git a/src/transformers/models/mobilebert/modeling_tf_mobilebert.py b/src/transformers/models/mobilebert/modeling_tf_mobilebert.py index eddb339074a3..c454a8b35db1 100644 --- a/src/transformers/models/mobilebert/modeling_tf_mobilebert.py +++ b/src/transformers/models/mobilebert/modeling_tf_mobilebert.py @@ -20,7 +20,7 @@ import warnings from dataclasses import dataclass -from typing import Dict, Optional, Tuple, Union +from typing import Optional, Tuple, Union import numpy as np import tensorflow as tf @@ -51,7 +51,6 @@ ) from ...tf_utils import check_embeddings_within_bounds, shape_list, stable_softmax from ...utils import ( - MULTIPLE_CHOICE_DUMMY_INPUTS, ModelOutput, add_code_sample_docstrings, add_start_docstrings, @@ -998,17 +997,6 @@ def call( return outputs - def serving_output(self, output: TFBaseModelOutputWithPooling) -> TFBaseModelOutputWithPooling: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFBaseModelOutputWithPooling( - last_hidden_state=output.last_hidden_state, - pooler_output=output.pooler_output, - hidden_states=hs, - attentions=attns, - ) - @add_start_docstrings( """ @@ -1099,17 +1087,6 @@ def call( attentions=outputs.attentions, ) - def serving_output(self, output): - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFMobileBertForPreTrainingOutput( - prediction_logits=output.prediction_logits, - seq_relationship_logits=output.seq_relationship_logits, - hidden_states=hs, - attentions=attns, - ) - @add_start_docstrings("""MobileBert Model with a `language modeling` head on top.""", MOBILEBERT_START_DOCSTRING) class TFMobileBertForMaskedLM(TFMobileBertPreTrainedModel, TFMaskedLanguageModelingLoss): @@ -1190,13 +1167,6 @@ def call( attentions=outputs.attentions, ) - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForMaskedLM.serving_output - def serving_output(self, output: TFMaskedLMOutput) -> TFMaskedLMOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFMaskedLMOutput(logits=output.logits, hidden_states=hs, attentions=attns) - class TFMobileBertOnlyNSPHead(tf.keras.layers.Layer): def __init__(self, config, **kwargs): @@ -1289,13 +1259,6 @@ def call( attentions=outputs.attentions, ) - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForNextSentencePrediction.serving_output - def serving_output(self, output: TFNextSentencePredictorOutput) -> TFNextSentencePredictorOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFNextSentencePredictorOutput(logits=output.logits, hidden_states=hs, attentions=attns) - @add_start_docstrings( """ @@ -1386,13 +1349,6 @@ def call( attentions=outputs.attentions, ) - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForSequenceClassification.serving_output - def serving_output(self, output: TFSequenceClassifierOutput) -> TFSequenceClassifierOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFSequenceClassifierOutput(logits=output.logits, hidden_states=hs, attentions=attns) - @add_start_docstrings( """ @@ -1492,15 +1448,6 @@ def call( attentions=outputs.attentions, ) - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForQuestionAnswering.serving_output - def serving_output(self, output: TFQuestionAnsweringModelOutput) -> TFQuestionAnsweringModelOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFQuestionAnsweringModelOutput( - start_logits=output.start_logits, end_logits=output.end_logits, hidden_states=hs, attentions=attns - ) - @add_start_docstrings( """ @@ -1528,16 +1475,6 @@ def __init__(self, config, *inputs, **kwargs): 1, kernel_initializer=get_initializer(config.initializer_range), name="classifier" ) - @property - def dummy_inputs(self): - """ - Dummy inputs to build the network. - - Returns: - tf.Tensor with dummy inputs - """ - return {"input_ids": tf.constant(MULTIPLE_CHOICE_DUMMY_INPUTS, dtype=tf.int32)} - @unpack_inputs @add_start_docstrings_to_model_forward( MOBILEBERT_INPUTS_DOCSTRING.format("batch_size, num_choices, sequence_length") @@ -1612,28 +1549,6 @@ def call( attentions=outputs.attentions, ) - @tf.function( - input_signature=[ - { - "input_ids": tf.TensorSpec((None, None, None), tf.int32, name="input_ids"), - "attention_mask": tf.TensorSpec((None, None, None), tf.int32, name="attention_mask"), - "token_type_ids": tf.TensorSpec((None, None, None), tf.int32, name="token_type_ids"), - } - ] - ) - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForMultipleChoice.serving - def serving(self, inputs: Dict[str, tf.Tensor]): - output = self.call(input_ids=inputs) - - return self.serving_output(output) - - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForMultipleChoice.serving_output - def serving_output(self, output: TFMultipleChoiceModelOutput) -> TFMultipleChoiceModelOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFMultipleChoiceModelOutput(logits=output.logits, hidden_states=hs, attentions=attns) - @add_start_docstrings( """ @@ -1722,10 +1637,3 @@ def call( hidden_states=outputs.hidden_states, attentions=outputs.attentions, ) - - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForTokenClassification.serving_output - def serving_output(self, output: TFTokenClassifierOutput) -> TFTokenClassifierOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFTokenClassifierOutput(logits=output.logits, hidden_states=hs, attentions=attns) diff --git a/src/transformers/models/mpnet/modeling_tf_mpnet.py b/src/transformers/models/mpnet/modeling_tf_mpnet.py index 2f4178d6cfc9..2982899340d2 100644 --- a/src/transformers/models/mpnet/modeling_tf_mpnet.py +++ b/src/transformers/models/mpnet/modeling_tf_mpnet.py @@ -49,7 +49,6 @@ ) from ...tf_utils import check_embeddings_within_bounds, shape_list, stable_softmax from ...utils import ( - MULTIPLE_CHOICE_DUMMY_INPUTS, add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, @@ -77,19 +76,6 @@ class TFMPNetPreTrainedModel(TFPreTrainedModel): config_class = MPNetConfig base_model_prefix = "mpnet" - @tf.function( - input_signature=[ - { - "input_ids": tf.TensorSpec((None, None), tf.int32, name="input_ids"), - "attention_mask": tf.TensorSpec((None, None), tf.int32, name="attention_mask"), - } - ] - ) - def serving(self, inputs): - output = self.call(inputs) - - return self.serving_output(output) - class TFMPNetEmbeddings(tf.keras.layers.Layer): """Construct the embeddings from word, position embeddings.""" @@ -707,17 +693,6 @@ def call( ) return outputs - def serving_output(self, output: TFBaseModelOutputWithPooling) -> TFBaseModelOutputWithPooling: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFBaseModelOutputWithPooling( - last_hidden_state=output.last_hidden_state, - pooler_output=output.pooler_output, - hidden_states=hs, - attentions=attns, - ) - class TFMPNetLMHead(tf.keras.layers.Layer): """MPNet head for masked and permuted language modeling""" @@ -841,13 +816,6 @@ def call( attentions=outputs.attentions, ) - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForMaskedLM.serving_output - def serving_output(self, output: TFMaskedLMOutput) -> TFMaskedLMOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFMaskedLMOutput(logits=output.logits, hidden_states=hs, attentions=attns) - class TFMPNetClassificationHead(tf.keras.layers.Layer): """Head for sentence-level classification tasks.""" @@ -945,13 +913,6 @@ def call( attentions=outputs.attentions, ) - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForSequenceClassification.serving_output - def serving_output(self, output: TFSequenceClassifierOutput) -> TFSequenceClassifierOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFSequenceClassifierOutput(logits=output.logits, hidden_states=hs, attentions=attns) - @add_start_docstrings( """ @@ -970,16 +931,6 @@ def __init__(self, config, *inputs, **kwargs): 1, kernel_initializer=get_initializer(config.initializer_range), name="classifier" ) - @property - def dummy_inputs(self): - """ - Dummy inputs to build the network. - - Returns: - tf.Tensor with dummy inputs - """ - return {"input_ids": tf.constant(MULTIPLE_CHOICE_DUMMY_INPUTS, dtype=tf.int32)} - @unpack_inputs @add_start_docstrings_to_model_forward(MPNET_INPUTS_DOCSTRING.format("batch_size, num_choices, sequence_length")) @add_code_sample_docstrings( @@ -1048,26 +999,6 @@ def call( attentions=outputs.attentions, ) - @tf.function( - input_signature=[ - { - "input_ids": tf.TensorSpec((None, None, None), tf.int32, name="input_ids"), - "attention_mask": tf.TensorSpec((None, None, None), tf.int32, name="attention_mask"), - } - ] - ) - def serving(self, inputs): - output = self.call(inputs) - - return self.serving_output(output) - - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForMultipleChoice.serving_output - def serving_output(self, output: TFMultipleChoiceModelOutput) -> TFMultipleChoiceModelOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFMultipleChoiceModelOutput(logits=output.logits, hidden_states=hs, attentions=attns) - @add_start_docstrings( """ @@ -1142,13 +1073,6 @@ def call( attentions=outputs.attentions, ) - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForTokenClassification.serving_output - def serving_output(self, output: TFTokenClassifierOutput) -> TFTokenClassifierOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFTokenClassifierOutput(logits=output.logits, hidden_states=hs, attentions=attns) - @add_start_docstrings( """ @@ -1235,12 +1159,3 @@ def call( hidden_states=outputs.hidden_states, attentions=outputs.attentions, ) - - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForQuestionAnswering.serving_output - def serving_output(self, output: TFQuestionAnsweringModelOutput) -> TFQuestionAnsweringModelOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFQuestionAnsweringModelOutput( - start_logits=output.start_logits, end_logits=output.end_logits, hidden_states=hs, attentions=attns - ) diff --git a/src/transformers/models/openai/modeling_tf_openai.py b/src/transformers/models/openai/modeling_tf_openai.py index 3f8967241946..70b7f6c05efb 100644 --- a/src/transformers/models/openai/modeling_tf_openai.py +++ b/src/transformers/models/openai/modeling_tf_openai.py @@ -357,19 +357,6 @@ class TFOpenAIGPTPreTrainedModel(TFPreTrainedModel): config_class = OpenAIGPTConfig base_model_prefix = "transformer" - @tf.function( - input_signature=[ - { - "input_ids": tf.TensorSpec((None, None), tf.int32, name="input_ids"), - "attention_mask": tf.TensorSpec((None, None), tf.int32, name="attention_mask"), - } - ] - ) - def serving(self, inputs): - output = self.call(inputs) - - return self.serving_output(output) - @dataclass class TFOpenAIGPTDoubleHeadsModelOutput(ModelOutput): @@ -541,13 +528,6 @@ def call( ) return outputs - # Copied from transformers.models.distilbert.modeling_tf_distilbert.TFDistilBertModel.serving_output - def serving_output(self, output): - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFBaseModelOutput(last_hidden_state=output.last_hidden_state, hidden_states=hs, attentions=attns) - @add_start_docstrings( """ @@ -630,12 +610,6 @@ def call( attentions=transformer_outputs.attentions, ) - def serving_output(self, output: TFCausalLMOutput) -> TFCausalLMOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFCausalLMOutput(logits=output.logits, hidden_states=hs, attentions=attns) - def prepare_inputs_for_generation(self, inputs, **kwargs): return {"input_ids": inputs} @@ -752,27 +726,13 @@ def call( attentions=transformer_outputs.attentions, ) - @tf.function( - input_signature=[ - { - "input_ids": tf.TensorSpec((None, None, None), tf.int32, name="input_ids"), - "attention_mask": tf.TensorSpec((None, None, None), tf.int32, name="attention_mask"), - "mc_token_ids": tf.TensorSpec((None, None), tf.int32, name="token_type_ids"), - } - ] - ) - def serving(self, inputs): - output = self.call(inputs) - - return self.serving_output(output) - - def serving_output(self, output): - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFOpenAIGPTDoubleHeadsModelOutput( - logits=output.logits, mc_logits=output.mc_logits, hidden_states=hs, attentions=attns - ) + @property + def input_signature(self): + return { + "input_ids": tf.TensorSpec((None, None, None), tf.int32, name="input_ids"), + "attention_mask": tf.TensorSpec((None, None, None), tf.int32, name="attention_mask"), + "mc_token_ids": tf.TensorSpec((None, None), tf.int32, name="token_type_ids"), + } @add_start_docstrings( @@ -894,10 +854,3 @@ def call( hidden_states=transformer_outputs.hidden_states, attentions=transformer_outputs.attentions, ) - - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForSequenceClassification.serving_output - def serving_output(self, output: TFSequenceClassifierOutput) -> TFSequenceClassifierOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFSequenceClassifierOutput(logits=output.logits, hidden_states=hs, attentions=attns) diff --git a/src/transformers/models/rembert/modeling_tf_rembert.py b/src/transformers/models/rembert/modeling_tf_rembert.py index 097bd977a4a1..1595fd8118de 100644 --- a/src/transformers/models/rembert/modeling_tf_rembert.py +++ b/src/transformers/models/rembert/modeling_tf_rembert.py @@ -49,8 +49,6 @@ ) from ...tf_utils import check_embeddings_within_bounds, shape_list, stable_softmax from ...utils import ( - DUMMY_INPUTS, - MULTIPLE_CHOICE_DUMMY_INPUTS, add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, @@ -812,24 +810,6 @@ class TFRemBertPreTrainedModel(TFPreTrainedModel): config_class = RemBertConfig base_model_prefix = "rembert" - @property - def dummy_inputs(self): - """ - Dummy inputs to build the network. - - Returns: - `Dict[str, tf.Tensor]`: The dummy inputs. - """ - dummy = {"input_ids": tf.constant(DUMMY_INPUTS, dtype=tf.int32)} - # Add `encoder_hidden_states` to make the cross-attention layers' weights initialized - if self.config.add_cross_attention: - batch_size, seq_len = tf.constant(DUMMY_INPUTS).shape - shape = (batch_size, seq_len) + (self.config.hidden_size,) - h = tf.random.uniform(shape=shape) - dummy["encoder_hidden_states"] = h - - return dummy - REMBERT_START_DOCSTRING = r""" @@ -1002,27 +982,6 @@ def call( return outputs - # Copied from transformers.models.bert.modeling_tf_bert.TFBertModel.serving_output - def serving_output( - self, output: TFBaseModelOutputWithPoolingAndCrossAttentions - ) -> TFBaseModelOutputWithPoolingAndCrossAttentions: - output_cache = self.config.use_cache and self.config.is_decoder - pkv = tf.convert_to_tensor(output.past_key_values) if output_cache else None - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - cross_attns = tf.convert_to_tensor(output.cross_attentions) if output.cross_attentions is not None else None - if not (self.config.output_attentions and self.config.add_cross_attention): - cross_attns = None - - return TFBaseModelOutputWithPoolingAndCrossAttentions( - last_hidden_state=output.last_hidden_state, - pooler_output=output.pooler_output, - past_key_values=pkv, - hidden_states=hs, - attentions=attns, - cross_attentions=cross_attns, - ) - @add_start_docstrings("""RemBERT Model with a `language modeling` head on top.""", REMBERT_START_DOCSTRING) class TFRemBertForMaskedLM(TFRemBertPreTrainedModel, TFMaskedLanguageModelingLoss): @@ -1095,12 +1054,6 @@ def call( attentions=outputs.attentions, ) - def serving_output(self, output: TFMaskedLMOutput) -> TFMaskedLMOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFMaskedLMOutput(logits=output.logits, hidden_states=hs, attentions=attns) - @add_start_docstrings( """RemBERT Model with a `language modeling` head on top for CLM fine-tuning.""", REMBERT_START_DOCSTRING @@ -1217,20 +1170,6 @@ def call( cross_attentions=outputs.cross_attentions, ) - # Copied from transformers.models.bert.modeling_tf_bert.TFBertLMHeadModel.serving_output - def serving_output(self, output: TFCausalLMOutputWithCrossAttentions) -> TFCausalLMOutputWithCrossAttentions: - output_cache = self.config.use_cache and self.config.is_decoder - pkv = tf.convert_to_tensor(output.past_key_values) if output_cache else None - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - cross_attns = tf.convert_to_tensor(output.cross_attentions) if output.cross_attentions is not None else None - if not (self.config.output_attentions and self.config.add_cross_attention): - cross_attns = None - - return TFCausalLMOutputWithCrossAttentions( - logits=output.logits, past_key_values=pkv, hidden_states=hs, attentions=attns, cross_attentions=cross_attns - ) - @add_start_docstrings( """ @@ -1307,12 +1246,6 @@ def call( attentions=outputs.attentions, ) - def serving_output(self, output: TFSequenceClassifierOutput) -> TFSequenceClassifierOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFSequenceClassifierOutput(logits=output.logits, hidden_states=hs, attentions=attns) - @add_start_docstrings( """ @@ -1331,16 +1264,6 @@ def __init__(self, config: RemBertConfig, *inputs, **kwargs): units=1, kernel_initializer=get_initializer(config.initializer_range), name="classifier" ) - @property - def dummy_inputs(self) -> Dict[str, tf.Tensor]: - """ - Dummy inputs to build the network. - - Returns: - tf.Tensor with dummy inputs - """ - return {"input_ids": tf.constant(MULTIPLE_CHOICE_DUMMY_INPUTS, dtype=tf.int32)} - @unpack_inputs @add_start_docstrings_to_model_forward(REMBERT_INPUTS_DOCSTRING.format("batch_size, num_choices, sequence_length")) @add_code_sample_docstrings( @@ -1419,26 +1342,6 @@ def call( attentions=outputs.attentions, ) - @tf.function( - input_signature=[ - { - "input_ids": tf.TensorSpec((None, None, None), tf.int32, name="input_ids"), - "attention_mask": tf.TensorSpec((None, None, None), tf.int32, name="attention_mask"), - "token_type_ids": tf.TensorSpec((None, None, None), tf.int32, name="token_type_ids"), - } - ] - ) - def serving(self, inputs: Dict[str, tf.Tensor]) -> TFMultipleChoiceModelOutput: - output = self.call(input_ids=inputs) - - return self.serving_output(output) - - def serving_output(self, output: TFMultipleChoiceModelOutput) -> TFMultipleChoiceModelOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFMultipleChoiceModelOutput(logits=output.logits, hidden_states=hs, attentions=attns) - @add_start_docstrings( """ @@ -1512,12 +1415,6 @@ def call( attentions=outputs.attentions, ) - def serving_output(self, output: TFTokenClassifierOutput) -> TFTokenClassifierOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFTokenClassifierOutput(logits=output.logits, hidden_states=hs, attentions=attns) - @add_start_docstrings( """ @@ -1604,11 +1501,3 @@ def call( hidden_states=outputs.hidden_states, attentions=outputs.attentions, ) - - def serving_output(self, output: TFQuestionAnsweringModelOutput) -> TFQuestionAnsweringModelOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFQuestionAnsweringModelOutput( - start_logits=output.start_logits, end_logits=output.end_logits, hidden_states=hs, attentions=attns - ) diff --git a/src/transformers/models/roberta_prelayernorm/modeling_tf_roberta_prelayernorm.py b/src/transformers/models/roberta_prelayernorm/modeling_tf_roberta_prelayernorm.py index 80a834ad5854..2f98a5f5d0cf 100644 --- a/src/transformers/models/roberta_prelayernorm/modeling_tf_roberta_prelayernorm.py +++ b/src/transformers/models/roberta_prelayernorm/modeling_tf_roberta_prelayernorm.py @@ -51,8 +51,6 @@ ) from ...tf_utils import check_embeddings_within_bounds, shape_list, stable_softmax from ...utils import ( - DUMMY_INPUTS, - MULTIPLE_CHOICE_DUMMY_INPUTS, add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, @@ -778,38 +776,6 @@ class TFRobertaPreLayerNormPreTrainedModel(TFPreTrainedModel): config_class = RobertaPreLayerNormConfig base_model_prefix = "roberta_prelayernorm" - @property - # Copied from transformers.models.bert.modeling_tf_bert.TFBertPreTrainedModel.dummy_inputs - def dummy_inputs(self): - """ - Dummy inputs to build the network. - - Returns: - `Dict[str, tf.Tensor]`: The dummy inputs. - """ - dummy = {"input_ids": tf.constant(DUMMY_INPUTS, dtype=tf.int32)} - # Add `encoder_hidden_states` to make the cross-attention layers' weights initialized - if self.config.add_cross_attention: - batch_size, seq_len = tf.constant(DUMMY_INPUTS).shape - shape = (batch_size, seq_len) + (self.config.hidden_size,) - h = tf.random.uniform(shape=shape) - dummy["encoder_hidden_states"] = h - - return dummy - - @tf.function( - input_signature=[ - { - "input_ids": tf.TensorSpec((None, None), tf.int32, name="input_ids"), - "attention_mask": tf.TensorSpec((None, None), tf.int32, name="attention_mask"), - } - ] - ) - def serving(self, inputs): - output = self.call(inputs) - - return self.serving_output(output) - ROBERTA_PRELAYERNORM_START_DOCSTRING = r""" @@ -982,27 +948,6 @@ def call( return outputs - # Copied from transformers.models.bert.modeling_tf_bert.TFBertModel.serving_output - def serving_output( - self, output: TFBaseModelOutputWithPoolingAndCrossAttentions - ) -> TFBaseModelOutputWithPoolingAndCrossAttentions: - output_cache = self.config.use_cache and self.config.is_decoder - pkv = tf.convert_to_tensor(output.past_key_values) if output_cache else None - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - cross_attns = tf.convert_to_tensor(output.cross_attentions) if output.cross_attentions is not None else None - if not (self.config.output_attentions and self.config.add_cross_attention): - cross_attns = None - - return TFBaseModelOutputWithPoolingAndCrossAttentions( - last_hidden_state=output.last_hidden_state, - pooler_output=output.pooler_output, - past_key_values=pkv, - hidden_states=hs, - attentions=attns, - cross_attentions=cross_attns, - ) - # Copied from transformers.models.roberta.modeling_tf_roberta.TFRobertaLMHead with Roberta->RobertaPreLayerNorm class TFRobertaPreLayerNormLMHead(tf.keras.layers.Layer): @@ -1140,13 +1085,6 @@ def call( attentions=outputs.attentions, ) - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForMaskedLM.serving_output - def serving_output(self, output: TFMaskedLMOutput) -> TFMaskedLMOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFMaskedLMOutput(logits=output.logits, hidden_states=hs, attentions=attns) - # Copied from transformers.models.roberta.modeling_tf_roberta.TFRobertaForCausalLM with ROBERTA->ROBERTA_PRELAYERNORM,Roberta->RobertaPreLayerNorm,roberta->roberta_prelayernorm class TFRobertaPreLayerNormForCausalLM(TFRobertaPreLayerNormPreTrainedModel, TFCausalLanguageModelingLoss): @@ -1276,20 +1214,6 @@ def call( cross_attentions=outputs.cross_attentions, ) - # Copied from transformers.models.bert.modeling_tf_bert.TFBertLMHeadModel.serving_output - def serving_output(self, output: TFCausalLMOutputWithCrossAttentions) -> TFCausalLMOutputWithCrossAttentions: - output_cache = self.config.use_cache and self.config.is_decoder - pkv = tf.convert_to_tensor(output.past_key_values) if output_cache else None - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - cross_attns = tf.convert_to_tensor(output.cross_attentions) if output.cross_attentions is not None else None - if not (self.config.output_attentions and self.config.add_cross_attention): - cross_attns = None - - return TFCausalLMOutputWithCrossAttentions( - logits=output.logits, past_key_values=pkv, hidden_states=hs, attentions=attns, cross_attentions=cross_attns - ) - # Copied from transformers.models.roberta.modeling_tf_roberta.TFRobertaClassificationHead with Roberta->RobertaPreLayerNorm class TFRobertaPreLayerNormClassificationHead(tf.keras.layers.Layer): @@ -1398,13 +1322,6 @@ def call( attentions=outputs.attentions, ) - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForSequenceClassification.serving_output - def serving_output(self, output: TFSequenceClassifierOutput) -> TFSequenceClassifierOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFSequenceClassifierOutput(logits=output.logits, hidden_states=hs, attentions=attns) - @add_start_docstrings( """ @@ -1428,16 +1345,6 @@ def __init__(self, config, *inputs, **kwargs): 1, kernel_initializer=get_initializer(config.initializer_range), name="classifier" ) - @property - def dummy_inputs(self): - """ - Dummy inputs to build the network. - - Returns: - tf.Tensor with dummy inputs - """ - return {"input_ids": tf.constant(MULTIPLE_CHOICE_DUMMY_INPUTS, dtype=tf.int32)} - @unpack_inputs @add_start_docstrings_to_model_forward( ROBERTA_PRELAYERNORM_INPUTS_DOCSTRING.format("batch_size, num_choices, sequence_length") @@ -1508,26 +1415,6 @@ def call( attentions=outputs.attentions, ) - @tf.function( - input_signature=[ - { - "input_ids": tf.TensorSpec((None, None, None), tf.int32, name="input_ids"), - "attention_mask": tf.TensorSpec((None, None, None), tf.int32, name="attention_mask"), - } - ] - ) - def serving(self, inputs): - output = self.call(inputs) - - return self.serving_output(output) - - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForMultipleChoice.serving_output - def serving_output(self, output: TFMultipleChoiceModelOutput) -> TFMultipleChoiceModelOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFMultipleChoiceModelOutput(logits=output.logits, hidden_states=hs, attentions=attns) - @add_start_docstrings( """ @@ -1612,13 +1499,6 @@ def call( attentions=outputs.attentions, ) - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForTokenClassification.serving_output - def serving_output(self, output: TFTokenClassifierOutput) -> TFTokenClassifierOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFTokenClassifierOutput(logits=output.logits, hidden_states=hs, attentions=attns) - @add_start_docstrings( """ @@ -1711,12 +1591,3 @@ def call( hidden_states=outputs.hidden_states, attentions=outputs.attentions, ) - - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForQuestionAnswering.serving_output - def serving_output(self, output: TFQuestionAnsweringModelOutput) -> TFQuestionAnsweringModelOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFQuestionAnsweringModelOutput( - start_logits=output.start_logits, end_logits=output.end_logits, hidden_states=hs, attentions=attns - ) diff --git a/src/transformers/models/t5/modeling_tf_t5.py b/src/transformers/models/t5/modeling_tf_t5.py index 012f0c41b017..c1c44b2c2ccd 100644 --- a/src/transformers/models/t5/modeling_tf_t5.py +++ b/src/transformers/models/t5/modeling_tf_t5.py @@ -45,8 +45,6 @@ ) from ...tf_utils import check_embeddings_within_bounds, shape_list, stable_softmax from ...utils import ( - DUMMY_INPUTS, - DUMMY_MASK, ContextManagers, add_start_docstrings, add_start_docstrings_to_model_forward, @@ -868,30 +866,13 @@ class TFT5PreTrainedModel(TFPreTrainedModel): _keys_to_ignore_on_load_unexpected = [r"decoder\Wblock[\W_0]+layer[\W_1]+EncDecAttention\Wrelative_attention_bias"] @property - def dummy_inputs(self): - inputs = tf.constant(DUMMY_INPUTS, dtype=tf.int32) - input_mask = tf.constant(DUMMY_MASK, dtype=tf.int32) - dummy_inputs = { - "input_ids": inputs, - "decoder_input_ids": inputs, - "decoder_attention_mask": input_mask, + def input_signature(self): + return { + "input_ids": tf.TensorSpec((None, None), tf.int32, name="input_ids"), + "attention_mask": tf.TensorSpec((None, None), tf.int32, name="attention_mask"), + "decoder_input_ids": tf.TensorSpec((None, None), tf.int32, name="decoder_input_ids"), + "decoder_attention_mask": tf.TensorSpec((None, None), tf.int32, name="decoder_attention_mask"), } - return dummy_inputs - - @tf.function( - input_signature=[ - { - "input_ids": tf.TensorSpec((None, None), tf.int32, name="input_ids"), - "attention_mask": tf.TensorSpec((None, None), tf.int32, name="attention_mask"), - "decoder_input_ids": tf.TensorSpec((None, None), tf.int32, name="decoder_input_ids"), - "decoder_attention_mask": tf.TensorSpec((None, None), tf.int32, name="decoder_attention_mask"), - } - ] - ) - def serving(self, inputs): - output = self.call(inputs) - - return self.serving_output(output) def get_input_embeddings(self): return self.shared @@ -1249,25 +1230,6 @@ def call( encoder_attentions=encoder_outputs.attentions, ) - def serving_output(self, output): - pkv = tf.convert_to_tensor(output.past_key_values[1:]) if self.config.use_cache else None - dec_hs = tf.convert_to_tensor(output.decoder_hidden_states) if self.config.output_hidden_states else None - dec_attns = tf.convert_to_tensor(output.decoder_attentions) if self.config.output_attentions else None - cross_attns = tf.convert_to_tensor(output.cross_attentions) if self.config.output_attentions else None - enc_hs = tf.convert_to_tensor(output.encoder_hidden_states) if self.config.output_hidden_states else None - enc_attns = tf.convert_to_tensor(output.encoder_attentions) if self.config.output_attentions else None - - return TFSeq2SeqModelOutput( - last_hidden_state=output.last_hidden_state, - past_key_values=pkv, - decoder_hidden_states=dec_hs, - decoder_attentions=dec_attns, - encoder_last_hidden_state=output.encoder_last_hidden_state, - cross_attentions=cross_attns, - encoder_hidden_states=enc_hs, - encoder_attentions=enc_attns, - ) - @add_start_docstrings("""T5 Model with a `language modeling` head on top.""", T5_START_DOCSTRING) class TFT5ForConditionalGeneration(TFT5PreTrainedModel, TFCausalLanguageModelingLoss): @@ -1539,10 +1501,6 @@ def __init__(self, config, *inputs, **kwargs): encoder_config.use_cache = False self.encoder = TFT5MainLayer(encoder_config, self.shared, name="encoder") - @property - def dummy_inputs(self): - return {"input_ids": tf.constant(DUMMY_INPUTS, dtype=tf.int32)} - def get_encoder(self): return self.encoder @@ -1600,23 +1558,3 @@ def call( hidden_states=encoder_outputs.hidden_states, attentions=encoder_outputs.attentions, ) - - @tf.function( - input_signature=[ - { - "input_ids": tf.TensorSpec((None, None), tf.int32, name="input_ids"), - "attention_mask": tf.TensorSpec((None, None), tf.int32, name="attention_mask"), - } - ] - ) - def serving(self, inputs): - output = self.call(inputs) - - return self.serving_output(output) - - # Copied from transformers.models.distilbert.modeling_tf_distilbert.TFDistilBertModel.serving_output - def serving_output(self, output): - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFBaseModelOutput(last_hidden_state=output.last_hidden_state, hidden_states=hs, attentions=attns) diff --git a/src/transformers/models/xlm/modeling_tf_xlm.py b/src/transformers/models/xlm/modeling_tf_xlm.py index 1815b27c8595..80a214280cb6 100644 --- a/src/transformers/models/xlm/modeling_tf_xlm.py +++ b/src/transformers/models/xlm/modeling_tf_xlm.py @@ -732,13 +732,6 @@ def call( return outputs - # Copied from transformers.models.distilbert.modeling_tf_distilbert.TFDistilBertModel.serving_output - def serving_output(self, output): - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFBaseModelOutput(last_hidden_state=output.last_hidden_state, hidden_states=hs, attentions=attns) - class TFXLMPredLayer(tf.keras.layers.Layer): """ @@ -876,12 +869,6 @@ def call( logits=outputs, hidden_states=transformer_outputs.hidden_states, attentions=transformer_outputs.attentions ) - def serving_output(self, output): - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFXLMWithLMHeadModelOutput(logits=output.logits, hidden_states=hs, attentions=attns) - @add_start_docstrings( """ @@ -960,13 +947,6 @@ def call( attentions=transformer_outputs.attentions, ) - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForSequenceClassification.serving_output - def serving_output(self, output: TFSequenceClassifierOutput) -> TFSequenceClassifierOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFSequenceClassifierOutput(logits=output.logits, hidden_states=hs, attentions=attns) - @add_start_docstrings( """ @@ -1086,28 +1066,6 @@ def call( attentions=transformer_outputs.attentions, ) - @tf.function( - input_signature=[ - { - "input_ids": tf.TensorSpec((None, None, None), tf.int32, name="input_ids"), - "attention_mask": tf.TensorSpec((None, None, None), tf.int32, name="attention_mask"), - "token_type_ids": tf.TensorSpec((None, None, None), tf.int32, name="token_type_ids"), - } - ] - ) - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForMultipleChoice.serving - def serving(self, inputs: Dict[str, tf.Tensor]): - output = self.call(input_ids=inputs) - - return self.serving_output(output) - - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForMultipleChoice.serving_output - def serving_output(self, output: TFMultipleChoiceModelOutput) -> TFMultipleChoiceModelOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFMultipleChoiceModelOutput(logits=output.logits, hidden_states=hs, attentions=attns) - @add_start_docstrings( """ @@ -1188,13 +1146,6 @@ def call( attentions=transformer_outputs.attentions, ) - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForTokenClassification.serving_output - def serving_output(self, output: TFTokenClassifierOutput) -> TFTokenClassifierOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFTokenClassifierOutput(logits=output.logits, hidden_states=hs, attentions=attns) - @add_start_docstrings( """ @@ -1285,12 +1236,3 @@ def call( hidden_states=transformer_outputs.hidden_states, attentions=transformer_outputs.attentions, ) - - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForQuestionAnswering.serving_output - def serving_output(self, output: TFQuestionAnsweringModelOutput) -> TFQuestionAnsweringModelOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFQuestionAnsweringModelOutput( - start_logits=output.start_logits, end_logits=output.end_logits, hidden_states=hs, attentions=attns - ) diff --git a/src/transformers/models/xlm_roberta/modeling_tf_xlm_roberta.py b/src/transformers/models/xlm_roberta/modeling_tf_xlm_roberta.py index ae2bae7d7aa7..65f3be9e2f27 100644 --- a/src/transformers/models/xlm_roberta/modeling_tf_xlm_roberta.py +++ b/src/transformers/models/xlm_roberta/modeling_tf_xlm_roberta.py @@ -51,8 +51,6 @@ ) from ...tf_utils import check_embeddings_within_bounds, shape_list, stable_softmax from ...utils import ( - DUMMY_INPUTS, - MULTIPLE_CHOICE_DUMMY_INPUTS, add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, @@ -868,38 +866,6 @@ class TFXLMRobertaPreTrainedModel(TFPreTrainedModel): config_class = XLMRobertaConfig base_model_prefix = "roberta" - @property - # Copied from transformers.models.bert.modeling_tf_bert.TFBertPreTrainedModel.dummy_inputs - def dummy_inputs(self): - """ - Dummy inputs to build the network. - - Returns: - `Dict[str, tf.Tensor]`: The dummy inputs. - """ - dummy = {"input_ids": tf.constant(DUMMY_INPUTS, dtype=tf.int32)} - # Add `encoder_hidden_states` to make the cross-attention layers' weights initialized - if self.config.add_cross_attention: - batch_size, seq_len = tf.constant(DUMMY_INPUTS).shape - shape = (batch_size, seq_len) + (self.config.hidden_size,) - h = tf.random.uniform(shape=shape) - dummy["encoder_hidden_states"] = h - - return dummy - - @tf.function( - input_signature=[ - { - "input_ids": tf.TensorSpec((None, None), tf.int32, name="input_ids"), - "attention_mask": tf.TensorSpec((None, None), tf.int32, name="attention_mask"), - } - ] - ) - def serving(self, inputs): - output = self.call(inputs) - - return self.serving_output(output) - @add_start_docstrings( "The bare XLM RoBERTa Model transformer outputting raw hidden-states without any specific head on top.", @@ -974,27 +940,6 @@ def call( return outputs - # Copied from transformers.models.bert.modeling_tf_bert.TFBertModel.serving_output - def serving_output( - self, output: TFBaseModelOutputWithPoolingAndCrossAttentions - ) -> TFBaseModelOutputWithPoolingAndCrossAttentions: - output_cache = self.config.use_cache and self.config.is_decoder - pkv = tf.convert_to_tensor(output.past_key_values) if output_cache else None - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - cross_attns = tf.convert_to_tensor(output.cross_attentions) if output.cross_attentions is not None else None - if not (self.config.output_attentions and self.config.add_cross_attention): - cross_attns = None - - return TFBaseModelOutputWithPoolingAndCrossAttentions( - last_hidden_state=output.last_hidden_state, - pooler_output=output.pooler_output, - past_key_values=pkv, - hidden_states=hs, - attentions=attns, - cross_attentions=cross_attns, - ) - # Copied from transformers.models.roberta.modeling_tf_roberta.TFRobertaLMHead with Roberta->XLMRoberta class TFXLMRobertaLMHead(tf.keras.layers.Layer): @@ -1127,13 +1072,6 @@ def call( attentions=outputs.attentions, ) - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForMaskedLM.serving_output - def serving_output(self, output: TFMaskedLMOutput) -> TFMaskedLMOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFMaskedLMOutput(logits=output.logits, hidden_states=hs, attentions=attns) - @add_start_docstrings( "XLM-RoBERTa Model with a `language modeling` head on top for CLM fine-tuning.", @@ -1261,20 +1199,6 @@ def call( cross_attentions=outputs.cross_attentions, ) - # Copied from transformers.models.bert.modeling_tf_bert.TFBertLMHeadModel.serving_output - def serving_output(self, output: TFCausalLMOutputWithCrossAttentions) -> TFCausalLMOutputWithCrossAttentions: - output_cache = self.config.use_cache and self.config.is_decoder - pkv = tf.convert_to_tensor(output.past_key_values) if output_cache else None - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - cross_attns = tf.convert_to_tensor(output.cross_attentions) if output.cross_attentions is not None else None - if not (self.config.output_attentions and self.config.add_cross_attention): - cross_attns = None - - return TFCausalLMOutputWithCrossAttentions( - logits=output.logits, past_key_values=pkv, hidden_states=hs, attentions=attns, cross_attentions=cross_attns - ) - # Copied from transformers.models.roberta.modeling_tf_roberta.TFRobertaClassificationHead with Roberta->XLMRoberta class TFXLMRobertaClassificationHead(tf.keras.layers.Layer): @@ -1381,13 +1305,6 @@ def call( attentions=outputs.attentions, ) - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForSequenceClassification.serving_output - def serving_output(self, output: TFSequenceClassifierOutput) -> TFSequenceClassifierOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFSequenceClassifierOutput(logits=output.logits, hidden_states=hs, attentions=attns) - @add_start_docstrings( """ @@ -1411,16 +1328,6 @@ def __init__(self, config, *inputs, **kwargs): 1, kernel_initializer=get_initializer(config.initializer_range), name="classifier" ) - @property - def dummy_inputs(self): - """ - Dummy inputs to build the network. - - Returns: - tf.Tensor with dummy inputs - """ - return {"input_ids": tf.constant(MULTIPLE_CHOICE_DUMMY_INPUTS, dtype=tf.int32)} - @unpack_inputs @add_start_docstrings_to_model_forward( XLM_ROBERTA_INPUTS_DOCSTRING.format("batch_size, num_choices, sequence_length") @@ -1491,26 +1398,6 @@ def call( attentions=outputs.attentions, ) - @tf.function( - input_signature=[ - { - "input_ids": tf.TensorSpec((None, None, None), tf.int32, name="input_ids"), - "attention_mask": tf.TensorSpec((None, None, None), tf.int32, name="attention_mask"), - } - ] - ) - def serving(self, inputs): - output = self.call(inputs) - - return self.serving_output(output) - - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForMultipleChoice.serving_output - def serving_output(self, output: TFMultipleChoiceModelOutput) -> TFMultipleChoiceModelOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFMultipleChoiceModelOutput(logits=output.logits, hidden_states=hs, attentions=attns) - @add_start_docstrings( """ @@ -1595,13 +1482,6 @@ def call( attentions=outputs.attentions, ) - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForTokenClassification.serving_output - def serving_output(self, output: TFTokenClassifierOutput) -> TFTokenClassifierOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFTokenClassifierOutput(logits=output.logits, hidden_states=hs, attentions=attns) - @add_start_docstrings( """ @@ -1694,12 +1574,3 @@ def call( hidden_states=outputs.hidden_states, attentions=outputs.attentions, ) - - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForQuestionAnswering.serving_output - def serving_output(self, output: TFQuestionAnsweringModelOutput) -> TFQuestionAnsweringModelOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFQuestionAnsweringModelOutput( - start_logits=output.start_logits, end_logits=output.end_logits, hidden_states=hs, attentions=attns - ) diff --git a/templates/adding_a_new_model/cookiecutter-template-{{cookiecutter.modelname}}/modeling_tf_{{cookiecutter.lowercase_modelname}}.py b/templates/adding_a_new_model/cookiecutter-template-{{cookiecutter.modelname}}/modeling_tf_{{cookiecutter.lowercase_modelname}}.py index 80e2d8ed1e09..a0da6fc492f6 100644 --- a/templates/adding_a_new_model/cookiecutter-template-{{cookiecutter.modelname}}/modeling_tf_{{cookiecutter.lowercase_modelname}}.py +++ b/templates/adding_a_new_model/cookiecutter-template-{{cookiecutter.modelname}}/modeling_tf_{{cookiecutter.lowercase_modelname}}.py @@ -803,23 +803,6 @@ class TF{{cookiecutter.camelcase_modelname}}PreTrainedModel(TFPreTrainedModel): config_class = {{cookiecutter.camelcase_modelname}}Config base_model_prefix = "{{cookiecutter.lowercase_modelname}}" - @property - def dummy_inputs(self): - """ - Dummy inputs to build the network. - - Returns: - `Dict[str, tf.Tensor]`: The dummy inputs. - """ - dummy = {"input_ids": tf.constant(DUMMY_INPUTS, dtype=tf.int64)} - # Add `encoder_hidden_states` to make the cross-attention layers' weights initialized - if self.config.add_cross_attention: - batch_size, seq_len = tf.constant(DUMMY_INPUTS).shape - shape = (batch_size, seq_len) + (self.config.hidden_size,) - h = tf.random.uniform(shape=shape) - dummy["encoder_hidden_states"] = h - - return dummy {{cookiecutter.uppercase_modelname}}_START_DOCSTRING = r""" @@ -991,24 +974,6 @@ def call( return outputs - def serving_output( - self, output: TFBaseModelOutputWithPastAndCrossAttentions - ) -> TFBaseModelOutputWithPastAndCrossAttentions: - output_cache = self.config.use_cache and self.config.is_decoder - pkv = tf.convert_to_tensor(output.past_key_values) if output_cache else None - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - cross_attns = tf.convert_to_tensor(output.cross_attentions) if output.cross_attentions is not None else None - if not (self.config.output_attentions and self.config.add_cross_attention): - cross_attns = None - - return TFBaseModelOutputWithPastAndCrossAttentions( - last_hidden_state=output.last_hidden_state, - past_key_values=pkv, - hidden_states=hs, - attentions=attns, - cross_attentions=cross_attns, - ) @add_start_docstrings("""{{cookiecutter.modelname}} Model with a `language modeling` head on top. """, {{cookiecutter.uppercase_modelname}}_START_DOCSTRING) @@ -1084,13 +1049,6 @@ def call( attentions=outputs.attentions, ) - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForMaskedLM.serving_output - def serving_output(self, output: TFMaskedLMOutput) -> TFMaskedLMOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFMaskedLMOutput(logits=output.logits, hidden_states=hs, attentions=attns) - @add_start_docstrings( """{{cookiecutter.modelname}} Model with a `language modeling` head on top for CLM fine-tuning. """, {{cookiecutter.uppercase_modelname}}_START_DOCSTRING @@ -1206,19 +1164,6 @@ def call( cross_attentions=outputs.cross_attentions, ) - # Copied from transformers.models.bert.modeling_tf_bert.TFBertLMHeadModel.serving_output - def serving_output(self, output: TFCausalLMOutputWithCrossAttentions) -> TFCausalLMOutputWithCrossAttentions: - output_cache = self.config.use_cache and self.config.is_decoder - pkv = tf.convert_to_tensor(output.past_key_values) if output_cache else None - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - cross_attns = tf.convert_to_tensor(output.cross_attentions) if output.cross_attentions is not None else None - if not (self.config.output_attentions and self.config.add_cross_attention): - cross_attns = None - - return TFCausalLMOutputWithCrossAttentions( - logits=output.logits, past_key_values=pkv, hidden_states=hs, attentions=attns, cross_attentions=cross_attns - ) class TF{{cookiecutter.camelcase_modelname}}ClassificationHead(tf.keras.layers.Layer): @@ -1318,13 +1263,6 @@ def call( attentions=outputs.attentions, ) - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForSequenceClassification.serving_output - def serving_output(self, output: TFSequenceClassifierOutput) -> TFSequenceClassifierOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFSequenceClassifierOutput(logits=output.logits, hidden_states=hs, attentions=attns) - @add_start_docstrings( """{{cookiecutter.modelname}} Model with a multiple choice classification head on top (a linear layer on top of @@ -1343,16 +1281,6 @@ def __init__(self, config: {{cookiecutter.camelcase_modelname}}Config, *inputs, units=1, kernel_initializer=get_initializer(config.initializer_range), name="classifier" ) - @property - def dummy_inputs(self) -> Dict[str, tf.Tensor]: - """ - Dummy inputs to build the network. - - Returns: - tf.Tensor with dummy inputs - """ - return {"input_ids": tf.constant(MULTIPLE_CHOICE_DUMMY_INPUTS, dtype=tf.int64)} - @unpack_inputs @add_start_docstrings_to_model_forward({{cookiecutter.uppercase_modelname}}_INPUTS_DOCSTRING.format("batch_size, num_choices, sequence_length")) @add_code_sample_docstrings( @@ -1441,24 +1369,6 @@ def call( attentions=outputs.attentions, ) - @tf.function(input_signature=[{ - "input_ids": tf.TensorSpec((None, None, None), tf.int32, name="input_ids"), - "attention_mask": tf.TensorSpec((None, None, None), tf.int32, name="attention_mask"), - "token_type_ids": tf.TensorSpec((None, None, None), tf.int32, name="token_type_ids"), - }]) - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForMultipleChoice.serving - def serving(self, inputs: Dict[str, tf.Tensor]) -> TFMultipleChoiceModelOutput: - output = self.call(input_ids=inputs) - - return self.serving_output(output) - - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForMultipleChoice.serving_output - def serving_output(self, output: TFMultipleChoiceModelOutput) -> TFMultipleChoiceModelOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFMultipleChoiceModelOutput(logits=output.logits, hidden_states=hs, attentions=attns) - @add_start_docstrings( """{{cookiecutter.modelname}} Model with a token classification head on top (a linear layer on top of @@ -1532,13 +1442,6 @@ def call( attentions=outputs.attentions, ) - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForTokenClassification.serving_output - def serving_output(self, output: TFTokenClassifierOutput) -> TFTokenClassifierOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFTokenClassifierOutput(logits=output.logits, hidden_states=hs, attentions=attns) - @add_start_docstrings( """{{cookiecutter.modelname}} Model with a span classification head on top for extractive question-answering tasks like SQuAD (a linear @@ -1625,14 +1528,6 @@ def call( attentions=outputs.attentions, ) - # Copied from transformers.models.bert.modeling_tf_bert.TFBertForQuestionAnswering.serving_output - def serving_output(self, output: TFQuestionAnsweringModelOutput) -> TFQuestionAnsweringModelOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFQuestionAnsweringModelOutput( - start_logits=output.start_logits, end_logits=output.end_logits, hidden_states=hs, attentions=attns - ) {% else %} import random @@ -2777,26 +2672,6 @@ def call( return outputs - # Copied from transformers.models.bart.modeling_tf_bart.TFBartModel.serving_output - def serving_output(self, output): - pkv = tf.tuple(output.past_key_values)[1] if self.config.use_cache else None - dec_hs = tf.convert_to_tensor(output.decoder_hidden_states) if self.config.output_hidden_states else None - dec_attns = tf.convert_to_tensor(output.decoder_attentions) if self.config.output_attentions else None - cross_attns = tf.convert_to_tensor(output.cross_attentions) if self.config.output_attentions else None - enc_hs = tf.convert_to_tensor(output.encoder_hidden_states) if self.config.output_hidden_states else None - enc_attns = tf.convert_to_tensor(output.encoder_attentions) if self.config.output_attentions else None - - return TFSeq2SeqModelOutput( - last_hidden_state=output.last_hidden_state, - past_key_values=pkv, - decoder_hidden_states=dec_hs, - decoder_attentions=dec_attns, - cross_attentions=cross_attns, - encoder_last_hidden_state=output.encoder_last_hidden_state, - encoder_hidden_states=enc_hs, - encoder_attentions=enc_attns, - ) - # Copied from transformers.models.bart.modeling_tf_bart.BiasLayer class BiasLayer(tf.keras.layers.Layer): @@ -2944,26 +2819,6 @@ def call( encoder_attentions=outputs.encoder_attentions, # 2 of e out ) - # Copied from transformers.models.bart.modeling_tf_bart.TFBartForConditionalGeneration.serving_output - def serving_output(self, output): - pkv = tf.tuple(output.past_key_values)[1] if self.config.use_cache else None - dec_hs = tf.convert_to_tensor(output.decoder_hidden_states) if self.config.output_hidden_states else None - dec_attns = tf.convert_to_tensor(output.decoder_attentions) if self.config.output_attentions else None - cross_attns = tf.convert_to_tensor(output.cross_attentions) if self.config.output_attentions else None - enc_hs = tf.convert_to_tensor(output.encoder_hidden_states) if self.config.output_hidden_states else None - enc_attns = tf.convert_to_tensor(output.encoder_attentions) if self.config.output_attentions else None - - return TFSeq2SeqLMOutput( - logits=output.logits, - past_key_values=pkv, - decoder_hidden_states=dec_hs, - decoder_attentions=dec_attns, - cross_attentions=cross_attns, - encoder_last_hidden_state=output.encoder_last_hidden_state, - encoder_hidden_states=enc_hs, - encoder_attentions=enc_attns, - ) - def prepare_inputs_for_generation( self, decoder_input_ids, From e43ae84f1dd81171d36f758fd90acfc0cd749b63 Mon Sep 17 00:00:00 2001 From: Matt Date: Fri, 12 May 2023 17:31:54 +0100 Subject: [PATCH 25/49] merge type hint changes --- src/transformers/modeling_tf_utils.py | 40 ++- .../models/bart/modeling_tf_bart.py | 41 +--- .../models/bert/modeling_tf_bert.py | 13 - .../blenderbot/modeling_tf_blenderbot.py | 33 +-- .../modeling_tf_blenderbot_small.py | 32 +-- .../models/blip/modeling_tf_blip.py | 229 ++---------------- .../models/blip/modeling_tf_blip_text.py | 67 +---- .../models/clip/modeling_tf_clip.py | 107 +------- .../models/convnext/modeling_tf_convnext.py | 49 ---- .../models/cvt/modeling_tf_cvt.py | 41 +--- .../data2vec/modeling_tf_data2vec_vision.py | 55 +---- .../models/deberta/modeling_tf_deberta.py | 32 --- .../deberta_v2/modeling_tf_deberta_v2.py | 32 --- .../models/deit/modeling_tf_deit.py | 73 +----- .../models/dpr/modeling_tf_dpr.py | 37 --- .../modeling_tf_encoder_decoder.py | 44 +--- .../models/funnel/modeling_tf_funnel.py | 88 +------ .../models/gpt2/modeling_tf_gpt2.py | 110 +-------- .../models/gptj/modeling_tf_gptj.py | 61 ----- .../models/groupvit/modeling_tf_groupvit.py | 117 +-------- .../models/hubert/modeling_tf_hubert.py | 40 +-- .../models/led/modeling_tf_led.py | 32 +-- .../longformer/modeling_tf_longformer.py | 110 +-------- .../models/lxmert/modeling_tf_lxmert.py | 102 +++----- .../models/marian/modeling_tf_marian.py | 32 +-- .../models/mbart/modeling_tf_mbart.py | 32 +-- .../models/mobilevit/modeling_tf_mobilevit.py | 49 ---- .../models/opt/modeling_tf_opt.py | 24 -- .../models/pegasus/modeling_tf_pegasus.py | 32 +-- .../models/regnet/modeling_tf_regnet.py | 45 +--- .../models/resnet/modeling_tf_resnet.py | 36 +-- .../models/roformer/modeling_tf_roformer.py | 70 ------ .../models/segformer/modeling_tf_segformer.py | 52 +--- .../modeling_tf_speech_to_text.py | 48 +--- .../models/swin/modeling_tf_swin.py | 51 ---- .../models/tapas/modeling_tf_tapas.py | 53 +--- .../transfo_xl/modeling_tf_transfo_xl.py | 42 ---- .../modeling_tf_vision_encoder_decoder.py | 35 +-- .../models/vit/modeling_tf_vit.py | 51 +--- .../models/vit_mae/modeling_tf_vit_mae.py | 56 +---- .../models/wav2vec2/modeling_tf_wav2vec2.py | 67 +---- .../models/whisper/modeling_tf_whisper.py | 19 +- .../models/xglm/modeling_tf_xglm.py | 64 +---- .../models/xlnet/modeling_tf_xlnet.py | 77 ------ 44 files changed, 197 insertions(+), 2323 deletions(-) diff --git a/src/transformers/modeling_tf_utils.py b/src/transformers/modeling_tf_utils.py index 52760d9e6c68..14bb8818cb8a 100644 --- a/src/transformers/modeling_tf_utils.py +++ b/src/transformers/modeling_tf_utils.py @@ -1260,30 +1260,24 @@ def serving_output(self, output): """ Prepare the output of the saved model. Can be overridden if specific serving modifications are required. """ - config_variables = { - "hidden_states": "output_hidden_states", - "encoder_hidden_states": "output_hidden_states", - "decoder_hidden_states": "output_hidden_states", - "attentions": "output_attentions", - "encoder_attentions": "output_attentions", - "decoder_attentions": "output_attentions", - "past_key_values": "use_cache", - } if isinstance(output, ModelOutput): - for key, config_var in config_variables.items(): - if key in output: - if not getattr(self.config, config_var, False): - output[key] = None - elif output[key] is not None: - try: - output[key] = tf.convert_to_tensor(output[key]) - except ValueError: - pass # Layers may not have the same dimensions - if "cross_attentions" in output: - if not (self.config.output_attentions and self.config.add_cross_attention): - output["cross_attentions"] = None - if output["cross_attentions"] is not None: - output["cross_attentions"] = tf.convert_to_tensor(output["cross_attentions"]) + for key in output.keys(): + if key.endswith("hidden_states") and not getattr(self.config, "output_hidden_states", False): + output[key] = None + elif key.endswith("attentions") and not getattr(self.config, "output_attentions", False): + output[key] = None + elif key == "past_key_values" and not getattr(self.config, "use_cache", False): + output[key] = None + elif key == "cross_attentions" and not ( + getattr(self.config, "output_attentions", False) + and getattr(self.config, "add_cross_attention", False) + ): + output[key] = None + if output[key] is not None: + try: + output[key] = tf.convert_to_tensor(output[key]) + except ValueError: + pass # Layers may not have the same dimensions return output def can_generate(self) -> bool: diff --git a/src/transformers/models/bart/modeling_tf_bart.py b/src/transformers/models/bart/modeling_tf_bart.py index 91e01574ad48..d1b3c728a4db 100644 --- a/src/transformers/models/bart/modeling_tf_bart.py +++ b/src/transformers/models/bart/modeling_tf_bart.py @@ -43,7 +43,6 @@ ) from ...tf_utils import check_embeddings_within_bounds, shape_list, stable_softmax from ...utils import ( - DUMMY_INPUTS, ContextManagers, add_code_sample_docstrings, add_end_docstrings, @@ -486,31 +485,13 @@ class TFBartPretrainedModel(TFPreTrainedModel): base_model_prefix = "model" @property - def dummy_inputs(self): - pad_token = 1 - input_ids = tf.convert_to_tensor(DUMMY_INPUTS, dtype=tf.int32) - decoder_input_ids = tf.convert_to_tensor(DUMMY_INPUTS, dtype=tf.int32) - dummy_inputs = { - "decoder_input_ids": decoder_input_ids, - "attention_mask": tf.cast(input_ids != pad_token, tf.int32), - "input_ids": input_ids, + def input_signature(self): + return { + "input_ids": tf.TensorSpec((None, None), tf.int32, name="input_ids"), + "attention_mask": tf.TensorSpec((None, None), tf.int32, name="attention_mask"), + "decoder_input_ids": tf.TensorSpec((None, None), tf.int32, name="decoder_input_ids"), + "decoder_attention_mask": tf.TensorSpec((None, None), tf.int32, name="decoder_attention_mask"), } - return dummy_inputs - - @tf.function( - input_signature=[ - { - "input_ids": tf.TensorSpec((None, None), tf.int32, name="input_ids"), - "attention_mask": tf.TensorSpec((None, None), tf.int32, name="attention_mask"), - "decoder_input_ids": tf.TensorSpec((None, None), tf.int32, name="decoder_input_ids"), - "decoder_attention_mask": tf.TensorSpec((None, None), tf.int32, name="decoder_attention_mask"), - } - ] - ) - def serving(self, inputs): - output = self.call(inputs) - - return self.serving_output(output) BART_START_DOCSTRING = r""" @@ -1461,16 +1442,6 @@ def prepare_decoder_input_ids_from_labels(self, labels: tf.Tensor): BART_START_DOCSTRING, ) class TFBartForSequenceClassification(TFBartPretrainedModel, TFSequenceClassificationLoss): - @property - def dummy_inputs(self): - pad_token = self.config.pad_token_id - input_ids = tf.constant([[0, 6, 10, 4, 2], [0, 8, 12, 2, pad_token]]) - dummy_inputs = { - "attention_mask": tf.cast(tf.math.not_equal(input_ids, (pad_token)), dtype=tf.int32), - "input_ids": input_ids, - } - return dummy_inputs - def __init__(self, config: BartConfig, load_weight_prefix=None, *inputs, **kwargs): super().__init__(config, *inputs, **kwargs) self.model = TFBartMainLayer(config, load_weight_prefix=load_weight_prefix, name="model") diff --git a/src/transformers/models/bert/modeling_tf_bert.py b/src/transformers/models/bert/modeling_tf_bert.py index b8f1945e0845..fd0a07b415f4 100644 --- a/src/transformers/models/bert/modeling_tf_bert.py +++ b/src/transformers/models/bert/modeling_tf_bert.py @@ -1426,19 +1426,6 @@ def call( cross_attentions=outputs.cross_attentions, ) - def serving_output(self, output: TFCausalLMOutputWithCrossAttentions) -> TFCausalLMOutputWithCrossAttentions: - output_cache = self.config.use_cache and self.config.is_decoder - pkv = tf.convert_to_tensor(output.past_key_values) if output_cache else None - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - cross_attns = tf.convert_to_tensor(output.cross_attentions) if output.cross_attentions is not None else None - if not (self.config.output_attentions and self.config.add_cross_attention): - cross_attns = None - - return TFCausalLMOutputWithCrossAttentions( - logits=output.logits, past_key_values=pkv, hidden_states=hs, attentions=attns, cross_attentions=cross_attns - ) - @add_start_docstrings( """Bert Model with a `next sentence prediction (classification)` head on top.""", diff --git a/src/transformers/models/blenderbot/modeling_tf_blenderbot.py b/src/transformers/models/blenderbot/modeling_tf_blenderbot.py index 92baa551c7ac..cd31b151dc21 100644 --- a/src/transformers/models/blenderbot/modeling_tf_blenderbot.py +++ b/src/transformers/models/blenderbot/modeling_tf_blenderbot.py @@ -41,7 +41,6 @@ ) from ...tf_utils import check_embeddings_within_bounds, shape_list, stable_softmax from ...utils import ( - DUMMY_INPUTS, ContextManagers, add_code_sample_docstrings, add_end_docstrings, @@ -464,33 +463,13 @@ class TFBlenderbotPreTrainedModel(TFPreTrainedModel): config_class = BlenderbotConfig base_model_prefix = "model" - @property - def dummy_inputs(self): - pad_token = 1 - input_ids = tf.convert_to_tensor(DUMMY_INPUTS, dtype=tf.int32) - decoder_input_ids = tf.convert_to_tensor(DUMMY_INPUTS, dtype=tf.int32) - dummy_inputs = { - "decoder_input_ids": decoder_input_ids, - "attention_mask": tf.cast(input_ids != pad_token, tf.int32), - "input_ids": input_ids, + def input_signature(self): + return { + "input_ids": tf.TensorSpec((None, None), tf.int32, name="input_ids"), + "attention_mask": tf.TensorSpec((None, None), tf.int32, name="attention_mask"), + "decoder_input_ids": tf.TensorSpec((None, None), tf.int32, name="decoder_input_ids"), + "decoder_attention_mask": tf.TensorSpec((None, None), tf.int32, name="decoder_attention_mask"), } - return dummy_inputs - - @tf.function( - input_signature=[ - { - "input_ids": tf.TensorSpec((None, None), tf.int32, name="input_ids"), - "attention_mask": tf.TensorSpec((None, None), tf.int32, name="attention_mask"), - "decoder_input_ids": tf.TensorSpec((None, None), tf.int32, name="decoder_input_ids"), - "decoder_attention_mask": tf.TensorSpec((None, None), tf.int32, name="decoder_attention_mask"), - } - ] - ) - # Copied from transformers.models.bart.modeling_tf_bart.TFBartPretrainedModel.serving - def serving(self, inputs): - output = self.call(inputs) - - return self.serving_output(output) BLENDERBOT_START_DOCSTRING = r""" diff --git a/src/transformers/models/blenderbot_small/modeling_tf_blenderbot_small.py b/src/transformers/models/blenderbot_small/modeling_tf_blenderbot_small.py index 04a4811bd57a..8f898a72ee9a 100644 --- a/src/transformers/models/blenderbot_small/modeling_tf_blenderbot_small.py +++ b/src/transformers/models/blenderbot_small/modeling_tf_blenderbot_small.py @@ -40,7 +40,6 @@ ) from ...tf_utils import check_embeddings_within_bounds, shape_list, stable_softmax from ...utils import ( - DUMMY_INPUTS, ContextManagers, add_code_sample_docstrings, add_end_docstrings, @@ -465,32 +464,13 @@ class TFBlenderbotSmallPreTrainedModel(TFPreTrainedModel): base_model_prefix = "model" @property - def dummy_inputs(self): - pad_token = 1 - input_ids = tf.convert_to_tensor(DUMMY_INPUTS, dtype=tf.int32) - decoder_input_ids = tf.convert_to_tensor(DUMMY_INPUTS, dtype=tf.int32) - dummy_inputs = { - "decoder_input_ids": decoder_input_ids, - "attention_mask": tf.cast(input_ids != pad_token, tf.int32), - "input_ids": input_ids, + def input_signature(self): + return { + "input_ids": tf.TensorSpec((None, None), tf.int32, name="input_ids"), + "attention_mask": tf.TensorSpec((None, None), tf.int32, name="attention_mask"), + "decoder_input_ids": tf.TensorSpec((None, None), tf.int32, name="decoder_input_ids"), + "decoder_attention_mask": tf.TensorSpec((None, None), tf.int32, name="decoder_attention_mask"), } - return dummy_inputs - - @tf.function( - input_signature=[ - { - "input_ids": tf.TensorSpec((None, None), tf.int32, name="input_ids"), - "attention_mask": tf.TensorSpec((None, None), tf.int32, name="attention_mask"), - "decoder_input_ids": tf.TensorSpec((None, None), tf.int32, name="decoder_input_ids"), - "decoder_attention_mask": tf.TensorSpec((None, None), tf.int32, name="decoder_attention_mask"), - } - ] - ) - # Copied from transformers.models.bart.modeling_tf_bart.TFBartPretrainedModel.serving - def serving(self, inputs): - output = self.call(inputs) - - return self.serving_output(output) BLENDERBOT_SMALL_START_DOCSTRING = r""" diff --git a/src/transformers/models/blip/modeling_tf_blip.py b/src/transformers/models/blip/modeling_tf_blip.py index b1ad900f2498..df2840b12abf 100644 --- a/src/transformers/models/blip/modeling_tf_blip.py +++ b/src/transformers/models/blip/modeling_tf_blip.py @@ -17,7 +17,7 @@ from __future__ import annotations from dataclasses import dataclass -from typing import Any, Dict, Optional, Tuple, Union +from typing import Any, Optional, Tuple, Union import tensorflow as tf @@ -32,7 +32,6 @@ ) from ...tf_utils import check_embeddings_within_bounds, stable_softmax from ...utils import ( - DUMMY_INPUTS, ModelOutput, add_start_docstrings, add_start_docstrings_to_model_forward, @@ -648,49 +647,6 @@ def __init__(self, config: BlipVisionConfig, *args, **kwargs): self.encoder = TFBlipEncoder(config, name="encoder") self.post_layernorm = tf.keras.layers.LayerNormalization(epsilon=config.layer_norm_eps, name="post_layernorm") - @property - def dummy_inputs(self) -> Dict[str, tf.Tensor]: - """ - Dummy inputs to build the network. - - Returns: - `Dict[str, tf.Tensor]`: The dummy inputs. - """ - VISION_DUMMY_INPUTS = tf.random.uniform( - shape=(len(DUMMY_INPUTS), 3, self.config.image_size, self.config.image_size), dtype=tf.float32 - ) - return {"pixel_values": VISION_DUMMY_INPUTS} - - @tf.function( - input_signature=[ - { - "pixel_values": tf.TensorSpec((None, None, None, None), tf.float32, name="pixel_values"), - } - ] - ) - def serving(self, inputs: Dict[str, tf.Tensor]) -> TFBaseModelOutputWithPooling: - """ - Method used for serving the model. - - Args: - inputs (`Dict[str, tf.Tensor]`): - The input of the saved model as a dictionary of tensors. - """ - output = self.call(inputs) - - return self.serving_output(output) - - def serving_output(self, output: TFBaseModelOutputWithPooling) -> TFBaseModelOutputWithPooling: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFBaseModelOutputWithPooling( - last_hidden_state=output.last_hidden_state, - pooler_output=output.pooler_output, - hidden_states=hs, - attentions=attns, - ) - @unpack_inputs @add_start_docstrings_to_model_forward(BLIP_VISION_INPUTS_DOCSTRING) @replace_return_docstrings(output_type=TFBaseModelOutputWithPooling, config_class=BlipVisionConfig) @@ -881,52 +837,6 @@ def __init__(self, config: BlipConfig, *inputs, **kwargs): self.blip = TFBlipMainLayer(config, name="blip") - @property - def dummy_inputs(self) -> Dict[str, tf.Tensor]: - """ - Dummy inputs to build the network. - - Returns: - `Dict[str, tf.Tensor]`: The dummy inputs. - """ - VISION_DUMMY_INPUTS = tf.random.uniform( - shape=(len(DUMMY_INPUTS), 3, self.config.vision_config.image_size, self.config.vision_config.image_size), - dtype=tf.float32, - ) - return { - "input_ids": tf.constant(DUMMY_INPUTS, dtype=tf.int32), - "pixel_values": VISION_DUMMY_INPUTS, - } - - @tf.function( - input_signature=[ - { - "input_ids": tf.TensorSpec((None, None), tf.int32, name="input_ids"), - "pixel_values": tf.TensorSpec((None, None, None, None), tf.float32, name="pixel_values"), - "attention_mask": tf.TensorSpec((None, None), tf.int32, name="attention_mask"), - } - ] - ) - def serving(self, inputs: Dict[str, tf.Tensor]) -> TFBlipOutput: - """ - Method used for serving the model. - - Args: - inputs (`Dict[str, tf.Tensor]`): - The input of the saved model as a dictionary of tensors. - """ - output = self.call(inputs) - - return self.serving_output(output) - - def serving_output(self, output: TFBlipOutput) -> TFBlipOutput: - return TFBlipOutput( - logits_per_image=output.logits_per_image, - logits_per_text=output.logits_per_text, - text_embeds=output.text_embeds, - image_embeds=output.image_embeds, - ) - @unpack_inputs @add_start_docstrings_to_model_forward(BLIP_INPUTS_DOCSTRING) @replace_return_docstrings(output_type=TFBlipOutput, config_class=BlipConfig) @@ -1082,48 +992,6 @@ def __init__(self, config: BlipConfig, *args, **kwargs): def get_input_embeddings(self) -> tf.keras.layers.Layer: return self.vision_model.embeddings.patch_embedding - @property - def dummy_inputs(self): - input_ids = tf.constant(DUMMY_INPUTS, dtype=tf.int32) - VISION_DUMMY_INPUTS = tf.random.uniform( - shape=(len(DUMMY_INPUTS), 3, self.config.vision_config.image_size, self.config.vision_config.image_size), - dtype=tf.float32, - ) - return {"input_ids": input_ids, "pixel_values": VISION_DUMMY_INPUTS} - - @tf.function( - input_signature=[ - { - "pixel_values": tf.TensorSpec((None, None, None, None), tf.float32, name="pixel_values"), - "input_ids": tf.TensorSpec((None, None), tf.int32, name="input_ids"), - } - ] - ) - def serving(self, inputs: Dict[str, tf.Tensor]) -> TFBaseModelOutputWithPooling: - """ - Method used for serving the model. - - Args: - inputs (`Dict[str, tf.Tensor]`): - The input of the saved model as a dictionary of tensors. - """ - output = self.call(inputs) - - return self.serving_output(output) - - def serving_output( - self, output: TFBlipForConditionalGenerationModelOutput - ) -> TFBlipForConditionalGenerationModelOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFBlipForConditionalGenerationModelOutput( - last_hidden_state=output.last_hidden_state, - image_embeds=output.image_embeds, - hidden_states=hs, - attentions=attns, - ) - @unpack_inputs @add_start_docstrings_to_model_forward(BLIP_VISION_INPUTS_DOCSTRING) @replace_return_docstrings(output_type=TFBlipForConditionalGenerationModelOutput, config_class=BlipConfig) @@ -1297,46 +1165,30 @@ def __init__(self, config: BlipConfig, *args, **kwargs): def get_input_embeddings(self) -> tf.keras.layers.Layer: return self.vision_model.embeddings.patch_embedding - @property - def dummy_inputs(self): - input_ids = tf.constant(DUMMY_INPUTS, dtype=tf.int32) - VISION_DUMMY_INPUTS = tf.random.uniform( - shape=(len(DUMMY_INPUTS), 3, self.config.vision_config.image_size, self.config.vision_config.image_size), - dtype=tf.float32, - ) - return {"input_ids": input_ids, "pixel_values": VISION_DUMMY_INPUTS, "decoder_input_ids": input_ids} - - @tf.function( - input_signature=[ - { - "pixel_values": tf.TensorSpec((None, None, None, None), tf.float32, name="pixel_values"), - "input_ids": tf.TensorSpec((None, None), tf.int32, name="input_ids"), - } - ] - ) - def serving(self, inputs: Dict[str, tf.Tensor]) -> TFBaseModelOutputWithPooling: - """ - Method used for serving the model. - - Args: - inputs (`Dict[str, tf.Tensor]`): - The input of the saved model as a dictionary of tensors. - """ - output = self.call(inputs) + # Adapted from transformers.models.t5.modeling_tf_t5.TFT5PreTrainedModel._shift_right + def _shift_right(self, input_ids): + decoder_start_token_id = self.decoder_start_token_id + pad_token_id = self.decoder_pad_token_id - return self.serving_output(output) + if decoder_start_token_id is None or pad_token_id is None: + raise ValueError("decoder_start_token_id and pad_token_id must be defined!") - def serving_output(self, output: TFBlipTextVisionModelOutput) -> TFBlipTextVisionModelOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None + start_tokens = tf.fill((shape_list(input_ids)[0], 1), decoder_start_token_id) + start_tokens = tf.cast(start_tokens, input_ids.dtype) # Ensure compatible dtypes for concatenation + shifted_input_ids = tf.concat([start_tokens, input_ids[:, :-1]], -1) - return TFBlipTextVisionModelOutput( - image_embeds=output.image_embeds, - last_hidden_state=output.last_hidden_state, - hidden_states=hs, - attentions=attns, + # replace possible -100 values in labels by `pad_token_id` + shifted_input_ids = tf.where( + shifted_input_ids == -100, + tf.cast(tf.fill(shape_list(shifted_input_ids), pad_token_id), shifted_input_ids.dtype), + shifted_input_ids, ) + # "Verify that `labels` has only positive values and -100" + tf.debugging.assert_greater_equal(shifted_input_ids, tf.constant(0, dtype=shifted_input_ids.dtype)) + + return shifted_input_ids + @unpack_inputs @add_start_docstrings_to_model_forward(BLIP_VISION_INPUTS_DOCSTRING) @replace_return_docstrings(output_type=TFBlipTextVisionModelOutput, config_class=BlipVisionConfig) @@ -1579,47 +1431,6 @@ def __init__(self, config: BlipConfig, *args, **kwargs): def get_input_embeddings(self) -> tf.keras.layers.Layer: return self.vision_model.embeddings.patch_embedding - @property - def dummy_inputs(self): - input_ids = tf.constant(DUMMY_INPUTS, dtype=tf.int32) - VISION_DUMMY_INPUTS = tf.random.uniform( - shape=(len(DUMMY_INPUTS), 3, self.config.vision_config.image_size, self.config.vision_config.image_size), - dtype=tf.float32, - ) - return {"input_ids": input_ids, "pixel_values": VISION_DUMMY_INPUTS} - - @tf.function( - input_signature=[ - { - "pixel_values": tf.TensorSpec((None, None, None, None), tf.float32, name="pixel_values"), - "input_ids": tf.TensorSpec((None, None), tf.int32, name="input_ids"), - } - ] - ) - def serving(self, inputs: Dict[str, tf.Tensor]) -> TFBaseModelOutputWithPooling: - """ - Method used for serving the model. - - Args: - inputs (`Dict[str, tf.Tensor]`): - The input of the saved model as a dictionary of tensors. - """ - output = self.call(inputs) - - return self.serving_output(output) - - def serving_output(self, output: TFBlipImageTextMatchingModelOutput) -> TFBlipImageTextMatchingModelOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFBlipImageTextMatchingModelOutput( - itm_score=output.itm_score, - last_hidden_state=hs, - hidden_states=output.hidden_states, - attentions=attns, - question_embeds=output.question_embeds, - ) - @unpack_inputs @add_start_docstrings_to_model_forward(BLIP_VISION_INPUTS_DOCSTRING) @replace_return_docstrings(output_type=TFBlipImageTextMatchingModelOutput, config_class=BlipVisionConfig) diff --git a/src/transformers/models/blip/modeling_tf_blip_text.py b/src/transformers/models/blip/modeling_tf_blip_text.py index ec0ee2c43c86..19ebdac62e22 100644 --- a/src/transformers/models/blip/modeling_tf_blip_text.py +++ b/src/transformers/models/blip/modeling_tf_blip_text.py @@ -17,7 +17,7 @@ from __future__ import annotations import math -from typing import Dict, Optional, Tuple +from typing import Optional, Tuple import tensorflow as tf @@ -592,31 +592,6 @@ def __init__(self, config, add_pooling_layer=True, name=None, **kwargs): self.encoder = TFBlipTextEncoder(config, name="encoder") self.pooler = TFBlipTextPooler(config, name="pooler") if add_pooling_layer else None - @tf.function( - input_signature=[ - { - "input_ids": tf.TensorSpec((None, None), tf.int32, name="input_ids"), - "attention_mask": tf.TensorSpec((None, None), tf.int32, name="attention_mask"), - } - ] - ) - def serving(self, inputs: Dict[str, tf.Tensor]) -> TFBaseModelOutputWithPoolingAndCrossAttentions: - output = self.call(inputs) - return self.serving_output(output) - - def serving_output( - self, output: TFBaseModelOutputWithPoolingAndCrossAttentions - ) -> TFBaseModelOutputWithPoolingAndCrossAttentions: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFBaseModelOutputWithPoolingAndCrossAttentions( - last_hidden_state=output.last_hidden_state, - pooler_output=output.pooler_output, - hidden_states=hs, - attentions=attns, - ) - def get_input_embeddings(self): return self.embeddings.word_embeddings @@ -843,46 +818,6 @@ def get_output_embeddings(self): def set_output_embeddings(self, new_embeddings): self.cls.predictions.decoder = new_embeddings - @property - def dummy_inputs(self) -> Dict[str, tf.Tensor]: - """ - Dummy inputs to build the network. - - Returns: - `Dict[str, tf.Tensor]`: The dummy inputs. - """ - return {"input_ids": tf.constant(DUMMY_INPUTS, dtype=tf.int32)} - - @tf.function( - input_signature=[ - { - "input_ids": tf.TensorSpec((None, None), tf.int32, name="input_ids"), - } - ] - ) - def serving(self, inputs: Dict[str, tf.Tensor]) -> TFCausalLMOutputWithCrossAttentions: - """ - Method used for serving the model. - - Args: - inputs (`Dict[str, tf.Tensor]`): - The input of the saved model as a dictionary of tensors. - """ - output = self.call(inputs) - - return self.serving_output(output) - - def serving_output(self, output: TFCausalLMOutputWithCrossAttentions) -> TFCausalLMOutputWithCrossAttentions: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFCausalLMOutputWithCrossAttentions( - logits=output.logits, - cross_attentions=output.cross_attentions, - hidden_states=hs, - attentions=attns, - ) - @add_start_docstrings_to_model_forward(BLIP_TEXT_INPUTS_DOCSTRING) @unpack_inputs def call( diff --git a/src/transformers/models/clip/modeling_tf_clip.py b/src/transformers/models/clip/modeling_tf_clip.py index 0ac496c5b7fc..778f1ed2c92e 100644 --- a/src/transformers/models/clip/modeling_tf_clip.py +++ b/src/transformers/models/clip/modeling_tf_clip.py @@ -19,7 +19,7 @@ import math from dataclasses import dataclass -from typing import Any, Dict, Optional, Tuple, Union +from typing import Any, Optional, Tuple, Union import numpy as np import tensorflow as tf @@ -37,7 +37,6 @@ ) from ...tf_utils import check_embeddings_within_bounds, shape_list, stable_softmax from ...utils import ( - DUMMY_INPUTS, ModelOutput, add_start_docstrings, add_start_docstrings_to_model_forward, @@ -1090,29 +1089,6 @@ def call( return outputs - @tf.function( - input_signature=[ - { - "input_ids": tf.TensorSpec((None, None), tf.int32, name="input_ids"), - "attention_mask": tf.TensorSpec((None, None), tf.int32, name="attention_mask"), - } - ] - ) - def serving(self, inputs: Dict[str, tf.Tensor]) -> TFBaseModelOutputWithPooling: - output = self.call(inputs) - return self.serving_output(output) - - def serving_output(self, output: TFBaseModelOutputWithPooling) -> TFBaseModelOutputWithPooling: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFBaseModelOutputWithPooling( - last_hidden_state=output.last_hidden_state, - pooler_output=output.pooler_output, - hidden_states=hs, - attentions=attns, - ) - class TFCLIPVisionModel(TFCLIPPreTrainedModel): config_class = CLIPVisionConfig @@ -1123,38 +1099,6 @@ def __init__(self, config: CLIPVisionConfig, *inputs, **kwargs): self.clip = TFCLIPVisionMainLayer(config, name="clip") - @property - def dummy_inputs(self) -> Dict[str, tf.Tensor]: - """ - Dummy inputs to build the network. - - Returns: - `Dict[str, tf.Tensor]`: The dummy inputs. - """ - VISION_DUMMY_INPUTS = tf.random.uniform( - shape=(len(DUMMY_INPUTS), 3, self.config.image_size, self.config.image_size), dtype=tf.float32 - ) - return {"pixel_values": VISION_DUMMY_INPUTS} - - @tf.function( - input_signature=[ - { - "pixel_values": tf.TensorSpec((None, None, None, None), tf.float32, name="pixel_values"), - } - ] - ) - def serving(self, inputs: Dict[str, tf.Tensor]) -> TFBaseModelOutputWithPooling: - """ - Method used for serving the model. - - Args: - inputs (`Dict[str, tf.Tensor]`): - The input of the saved model as a dictionary of tensors. - """ - output = self.call(inputs) - - return self.serving_output(output) - @unpack_inputs @add_start_docstrings_to_model_forward(CLIP_VISION_INPUTS_DOCSTRING) @replace_return_docstrings(output_type=TFBaseModelOutputWithPooling, config_class=CLIPVisionConfig) @@ -1199,17 +1143,6 @@ def call( return outputs - def serving_output(self, output: TFBaseModelOutputWithPooling) -> TFBaseModelOutputWithPooling: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFBaseModelOutputWithPooling( - last_hidden_state=output.last_hidden_state, - pooler_output=output.pooler_output, - hidden_states=hs, - attentions=attns, - ) - @add_start_docstrings(CLIP_START_DOCSTRING) class TFCLIPModel(TFCLIPPreTrainedModel): @@ -1220,44 +1153,6 @@ def __init__(self, config: CLIPConfig, *inputs, **kwargs): self.clip = TFCLIPMainLayer(config, name="clip") - @property - def dummy_inputs(self) -> Dict[str, tf.Tensor]: - """ - Dummy inputs to build the network. - - Returns: - `Dict[str, tf.Tensor]`: The dummy inputs. - """ - VISION_DUMMY_INPUTS = tf.random.uniform( - shape=(len(DUMMY_INPUTS), 3, self.config.vision_config.image_size, self.config.vision_config.image_size), - dtype=tf.float32, - ) - return { - "input_ids": tf.constant(DUMMY_INPUTS, dtype=tf.int32), - "pixel_values": VISION_DUMMY_INPUTS, - } - - @tf.function( - input_signature=[ - { - "input_ids": tf.TensorSpec((None, None), tf.int32, name="input_ids"), - "pixel_values": tf.TensorSpec((None, None, None, None), tf.float32, name="pixel_values"), - "attention_mask": tf.TensorSpec((None, None), tf.int32, name="attention_mask"), - } - ] - ) - def serving(self, inputs: Dict[str, tf.Tensor]) -> TFCLIPOutput: - """ - Method used for serving the model. - - Args: - inputs (`Dict[str, tf.Tensor]`): - The input of the saved model as a dictionary of tensors. - """ - output = self.call(inputs) - - return self.serving_output(output) - @unpack_inputs @add_start_docstrings_to_model_forward(CLIP_TEXT_INPUTS_DOCSTRING.format("batch_size, sequence_length")) def get_text_features( diff --git a/src/transformers/models/convnext/modeling_tf_convnext.py b/src/transformers/models/convnext/modeling_tf_convnext.py index f258abe24cbb..895d71663140 100644 --- a/src/transformers/models/convnext/modeling_tf_convnext.py +++ b/src/transformers/models/convnext/modeling_tf_convnext.py @@ -351,43 +351,6 @@ class TFConvNextPreTrainedModel(TFPreTrainedModel): base_model_prefix = "convnext" main_input_name = "pixel_values" - @property - def dummy_inputs(self) -> Dict[str, tf.Tensor]: - """ - Dummy inputs to build the network. - - Returns: - `Dict[str, tf.Tensor]`: The dummy inputs. - """ - VISION_DUMMY_INPUTS = tf.random.uniform( - shape=( - 3, - self.config.num_channels, - self.config.image_size, - self.config.image_size, - ), - dtype=tf.float32, - ) - return {"pixel_values": tf.constant(VISION_DUMMY_INPUTS)} - - @tf.function( - input_signature=[ - { - "pixel_values": tf.TensorSpec((None, None, None, None), tf.float32, name="pixel_values"), - } - ] - ) - def serving(self, inputs): - """ - Method used for serving the model. - - Args: - inputs (`Dict[str, tf.Tensor]`): - The input of the saved model as a dictionary of tensors. - """ - output = self.call(inputs) - return self.serving_output(output) - CONVNEXT_START_DOCSTRING = r""" This model inherits from [`TFPreTrainedModel`]. Check the superclass documentation for the generic methods the @@ -509,14 +472,6 @@ def call( hidden_states=outputs.hidden_states, ) - def serving_output(self, output: TFBaseModelOutputWithPooling) -> TFBaseModelOutputWithPooling: - # hidden_states not converted to Tensor with tf.convert_to_tensor as they are all of different dimensions - return TFBaseModelOutputWithPooling( - last_hidden_state=output.last_hidden_state, - pooler_output=output.pooler_output, - hidden_states=output.hidden_states, - ) - @add_start_docstrings( """ @@ -609,7 +564,3 @@ def call( logits=logits, hidden_states=outputs.hidden_states, ) - - def serving_output(self, output: TFSequenceClassifierOutput) -> TFSequenceClassifierOutput: - # hidden_states not converted to Tensor with tf.convert_to_tensor as they are all of different dimensions - return TFSequenceClassifierOutput(logits=output.logits, hidden_states=output.hidden_states) diff --git a/src/transformers/models/cvt/modeling_tf_cvt.py b/src/transformers/models/cvt/modeling_tf_cvt.py index 3c80f53bfaf2..80e15a196f85 100644 --- a/src/transformers/models/cvt/modeling_tf_cvt.py +++ b/src/transformers/models/cvt/modeling_tf_cvt.py @@ -19,7 +19,7 @@ import collections.abc from dataclasses import dataclass -from typing import Dict, Optional, Tuple, Union +from typing import Optional, Tuple, Union import tensorflow as tf @@ -707,35 +707,6 @@ class TFCvtPreTrainedModel(TFPreTrainedModel): base_model_prefix = "cvt" main_input_name = "pixel_values" - @property - def dummy_inputs(self) -> Dict[str, tf.Tensor]: - """ - Dummy inputs to build the network. - - Returns: - `Dict[str, tf.Tensor]`: The dummy inputs. - """ - VISION_DUMMY_INPUTS = tf.random.uniform(shape=(3, self.config.num_channels, 224, 224), dtype=tf.float32) - return {"pixel_values": tf.constant(VISION_DUMMY_INPUTS)} - - @tf.function( - input_signature=[ - { - "pixel_values": tf.TensorSpec((None, None, None, None), tf.float32, name="pixel_values"), - } - ] - ) - def serving(self, inputs): - """ - Method used for serving the model. - - Args: - inputs (`Dict[str, tf.Tensor]`): - The input of the saved model as a dictionary of tensors. - """ - output = self.call(inputs) - return self.serving_output(output) - TFCVT_START_DOCSTRING = r""" @@ -844,13 +815,6 @@ def call( hidden_states=outputs.hidden_states, ) - def serving_output(self, output: TFBaseModelOutputWithCLSToken) -> TFBaseModelOutputWithCLSToken: - return TFBaseModelOutputWithCLSToken( - last_hidden_state=output.last_hidden_state, - cls_token_value=output.cls_token_value, - hidden_states=output.hidden_states, - ) - @add_start_docstrings( """ @@ -945,6 +909,3 @@ def call( return ((loss,) + output) if loss is not None else output return TFImageClassifierOutputWithNoAttention(loss=loss, logits=logits, hidden_states=outputs.hidden_states) - - def serving_output(self, output: TFImageClassifierOutputWithNoAttention) -> TFImageClassifierOutputWithNoAttention: - return TFImageClassifierOutputWithNoAttention(logits=output.logits, hidden_states=output.hidden_states) diff --git a/src/transformers/models/data2vec/modeling_tf_data2vec_vision.py b/src/transformers/models/data2vec/modeling_tf_data2vec_vision.py index 1085d6e48d6c..8ebb8c68ff8d 100644 --- a/src/transformers/models/data2vec/modeling_tf_data2vec_vision.py +++ b/src/transformers/models/data2vec/modeling_tf_data2vec_vision.py @@ -20,7 +20,7 @@ import collections.abc import math from dataclasses import dataclass -from typing import Dict, List, Optional, Tuple, Union +from typing import List, Optional, Tuple, Union import numpy as np import tensorflow as tf @@ -774,36 +774,6 @@ class TFData2VecVisionPreTrainedModel(TFPreTrainedModel): main_input_name = "pixel_values" _keys_to_ignore_on_load_unexpected = [r"relative_position_index"] - @property - def dummy_inputs(self) -> Dict[str, tf.Tensor]: - """ - Dummy inputs to build the network. Returns: - `Dict[str, tf.Tensor]`: The dummy inputs. - """ - VISION_DUMMY_INPUTS = tf.random.uniform( - shape=(3, self.config.num_channels, self.config.image_size, self.config.image_size), - dtype=tf.float32, - ) - return {"pixel_values": tf.constant(VISION_DUMMY_INPUTS)} - - @tf.function( - input_signature=[ - { - "pixel_values": tf.TensorSpec((None, None, None, None), tf.float32, name="pixel_values"), - } - ] - ) - def serving(self, inputs): - """ - Method used for serving the model. - - Args: - inputs (`Dict[str, tf.Tensor]`): - The input of the saved model as a dictionary of tensors. - """ - output = self.call(inputs) - return self.serving_output(output) - DATA2VEC_VISION_START_DOCSTRING = r""" This model inherits from [`TFPreTrainedModel`]. Check the superclass documentation for the generic methods the @@ -926,17 +896,6 @@ def call( return outputs - def serving_output(self, output: TFData2VecVisionModelOutputWithPooling) -> TFData2VecVisionModelOutputWithPooling: - hidden_states = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attentions = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFData2VecVisionModelOutputWithPooling( - last_hidden_state=output.last_hidden_state, - pooler_output=output.pooler_output, - hidden_states=hidden_states, - attentions=attentions, - ) - @add_start_docstrings( """ @@ -1009,12 +968,6 @@ def call( attentions=outputs.attentions, ) - def serving_output(self, output: TFSequenceClassifierOutput) -> TFSequenceClassifierOutput: - hidden_states = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attentions = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFSequenceClassifierOutput(logits=output.logits, hidden_states=hidden_states, attentions=attentions) - class TFData2VecVisionConvModule(tf.keras.layers.Layer): """ @@ -1475,9 +1428,3 @@ def reshape_features(x): hidden_states=outputs.hidden_states if output_hidden_states else None, attentions=outputs.attentions, ) - - def serving_output(self, output: TFSemanticSegmenterOutput) -> TFSemanticSegmenterOutput: - hidden_states = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attentions = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFSemanticSegmenterOutput(logits=output.logits, hidden_states=hidden_states, attentions=attentions) diff --git a/src/transformers/models/deberta/modeling_tf_deberta.py b/src/transformers/models/deberta/modeling_tf_deberta.py index 7a045426185e..57e6ea8b1e9b 100644 --- a/src/transformers/models/deberta/modeling_tf_deberta.py +++ b/src/transformers/models/deberta/modeling_tf_deberta.py @@ -1118,12 +1118,6 @@ def call( return outputs - def serving_output(self, output: TFBaseModelOutput) -> TFBaseModelOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFBaseModelOutput(last_hidden_state=output.last_hidden_state, hidden_states=hs, attentions=attns) - @add_start_docstrings("""DeBERTa Model with a `language modeling` head on top.""", DEBERTA_START_DOCSTRING) class TFDebertaForMaskedLM(TFDebertaPreTrainedModel, TFMaskedLanguageModelingLoss): @@ -1194,12 +1188,6 @@ def call( attentions=outputs.attentions, ) - def serving_output(self, output: TFMaskedLMOutput) -> TFMaskedLMOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFMaskedLMOutput(logits=output.logits, hidden_states=hs, attentions=attns) - @add_start_docstrings( """ @@ -1281,12 +1269,6 @@ def call( attentions=outputs.attentions, ) - def serving_output(self, output: TFSequenceClassifierOutput) -> TFSequenceClassifierOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFSequenceClassifierOutput(logits=output.logits, hidden_states=hs, attentions=attns) - @add_start_docstrings( """ @@ -1358,12 +1340,6 @@ def call( attentions=outputs.attentions, ) - def serving_output(self, output: TFTokenClassifierOutput) -> TFTokenClassifierOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFTokenClassifierOutput(logits=output.logits, hidden_states=hs, attentions=attns) - @add_start_docstrings( """ @@ -1448,11 +1424,3 @@ def call( hidden_states=outputs.hidden_states, attentions=outputs.attentions, ) - - def serving_output(self, output: TFQuestionAnsweringModelOutput) -> TFQuestionAnsweringModelOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFQuestionAnsweringModelOutput( - start_logits=output.start_logits, end_logits=output.end_logits, hidden_states=hs, attentions=attns - ) diff --git a/src/transformers/models/deberta_v2/modeling_tf_deberta_v2.py b/src/transformers/models/deberta_v2/modeling_tf_deberta_v2.py index 82b0a30c5a50..1075cc855a02 100644 --- a/src/transformers/models/deberta_v2/modeling_tf_deberta_v2.py +++ b/src/transformers/models/deberta_v2/modeling_tf_deberta_v2.py @@ -1212,12 +1212,6 @@ def call( return outputs - def serving_output(self, output: TFBaseModelOutput) -> TFBaseModelOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFBaseModelOutput(last_hidden_state=output.last_hidden_state, hidden_states=hs, attentions=attns) - @add_start_docstrings("""DeBERTa Model with a `language modeling` head on top.""", DEBERTA_START_DOCSTRING) # Copied from transformers.models.deberta.modeling_tf_deberta.TFDebertaForMaskedLM with Deberta->DebertaV2 @@ -1289,12 +1283,6 @@ def call( attentions=outputs.attentions, ) - def serving_output(self, output: TFMaskedLMOutput) -> TFMaskedLMOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFMaskedLMOutput(logits=output.logits, hidden_states=hs, attentions=attns) - @add_start_docstrings( """ @@ -1377,12 +1365,6 @@ def call( attentions=outputs.attentions, ) - def serving_output(self, output: TFSequenceClassifierOutput) -> TFSequenceClassifierOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFSequenceClassifierOutput(logits=output.logits, hidden_states=hs, attentions=attns) - @add_start_docstrings( """ @@ -1455,12 +1437,6 @@ def call( attentions=outputs.attentions, ) - def serving_output(self, output: TFTokenClassifierOutput) -> TFTokenClassifierOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFTokenClassifierOutput(logits=output.logits, hidden_states=hs, attentions=attns) - @add_start_docstrings( """ @@ -1546,11 +1522,3 @@ def call( hidden_states=outputs.hidden_states, attentions=outputs.attentions, ) - - def serving_output(self, output: TFQuestionAnsweringModelOutput) -> TFQuestionAnsweringModelOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFQuestionAnsweringModelOutput( - start_logits=output.start_logits, end_logits=output.end_logits, hidden_states=hs, attentions=attns - ) diff --git a/src/transformers/models/deit/modeling_tf_deit.py b/src/transformers/models/deit/modeling_tf_deit.py index 131939f5bcfa..efd25788b033 100644 --- a/src/transformers/models/deit/modeling_tf_deit.py +++ b/src/transformers/models/deit/modeling_tf_deit.py @@ -20,7 +20,7 @@ import collections.abc import math from dataclasses import dataclass -from typing import Dict, Optional, Tuple, Union +from typing import Optional, Tuple, Union import tensorflow as tf @@ -568,38 +568,6 @@ class TFDeiTPreTrainedModel(TFPreTrainedModel): base_model_prefix = "deit" main_input_name = "pixel_values" - @property - def dummy_inputs(self) -> Dict[str, tf.Tensor]: - """ - Dummy inputs to build the network. - - Returns: - `Dict[str, tf.Tensor]`: The dummy inputs. - """ - VISION_DUMMY_INPUTS = tf.random.uniform( - shape=(3, self.config.num_channels, self.config.image_size, self.config.image_size), dtype=tf.float32 - ) - return {"pixel_values": tf.constant(VISION_DUMMY_INPUTS)} - - @tf.function( - input_signature=[ - { - "pixel_values": tf.TensorSpec((None, None, None, None), tf.float32, name="pixel_values"), - } - ] - ) - def serving(self, inputs): - """ - Method used for serving the model. - - Args: - inputs (`Dict[str, tf.Tensor]`): - The input of the saved model as a dictionary of tensors. - """ - output = self.call(inputs) - - return self.serving_output(output) - DEIT_START_DOCSTRING = r""" This model is a TensorFlow @@ -679,17 +647,6 @@ def call( ) return outputs - def serving_output(self, output: TFBaseModelOutputWithPooling) -> TFBaseModelOutputWithPooling: - hidden_states = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attentions = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFBaseModelOutputWithPooling( - last_hidden_state=output.last_hidden_state, - pooler_output=output.pooler_output, - hidden_states=hidden_states, - attentions=attentions, - ) - # Copied from transformers.models.vit.modeling_tf_vit.TFViTPooler with ViT->DeiT class TFDeiTPooler(tf.keras.layers.Layer): @@ -865,14 +822,6 @@ def call( attentions=outputs.attentions, ) - def serving_output(self, output: TFMaskedImageModelingOutput) -> TFMaskedImageModelingOutput: - hidden_states = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attentions = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFMaskedImageModelingOutput( - reconstruction=output.reconstruction, hidden_states=hidden_states, attentions=attentions - ) - @add_start_docstrings( """ @@ -970,12 +919,6 @@ def call( attentions=outputs.attentions, ) - def serving_output(self, output: TFImageClassifierOutput) -> TFImageClassifierOutput: - hidden_states = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attentions = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFImageClassifierOutput(logits=output.logits, hidden_states=hidden_states, attentions=attentions) - @add_start_docstrings( """ @@ -1055,17 +998,3 @@ def call( hidden_states=outputs.hidden_states, attentions=outputs.attentions, ) - - def serving_output( - self, output: TFDeiTForImageClassificationWithTeacherOutput - ) -> TFDeiTForImageClassificationWithTeacherOutput: - hidden_states = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attentions = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFDeiTForImageClassificationWithTeacherOutput( - logits=output.logits, - cls_logits=output.cls_logits, - distillation_logits=output.distillation_logits, - hidden_states=hidden_states, - attentions=attentions, - ) diff --git a/src/transformers/models/dpr/modeling_tf_dpr.py b/src/transformers/models/dpr/modeling_tf_dpr.py index 008e6a39fdc9..759e22c8c71c 100644 --- a/src/transformers/models/dpr/modeling_tf_dpr.py +++ b/src/transformers/models/dpr/modeling_tf_dpr.py @@ -372,19 +372,6 @@ class TFDPRPretrainedReader(TFPreTrainedModel): config_class = DPRConfig base_model_prefix = "reader" - @tf.function( - input_signature=[ - { - "input_ids": tf.TensorSpec((None, None), tf.int32, name="input_ids"), - "attention_mask": tf.TensorSpec((None, None), tf.int32, name="attention_mask"), - } - ] - ) - def serving(self, inputs): - output = self.call(inputs) - - return self.serving_output(output) - ############### # Actual Models @@ -612,12 +599,6 @@ def call( pooler_output=outputs.pooler_output, hidden_states=outputs.hidden_states, attentions=outputs.attentions ) - def serving_output(self, output): - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFDPRContextEncoderOutput(pooler_output=output.pooler_output, hidden_states=hs, attentions=attns) - @add_start_docstrings( "The bare DPRQuestionEncoder transformer outputting pooler outputs as question representations.", @@ -698,12 +679,6 @@ def call( pooler_output=outputs.pooler_output, hidden_states=outputs.hidden_states, attentions=outputs.attentions ) - def serving_output(self, output): - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFDPRQuestionEncoderOutput(pooler_output=output.pooler_output, hidden_states=hs, attentions=attns) - @add_start_docstrings( "The bare DPRReader transformer outputting span predictions.", @@ -777,15 +752,3 @@ def call( return_dict=return_dict, training=training, ) - - def serving_output(self, output): - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFDPRReaderOutput( - start_logits=output.start_logits, - end_logits=output.end_logits, - relevance_logits=output.relevance_logits, - hidden_states=hs, - attentions=attns, - ) diff --git a/src/transformers/models/encoder_decoder/modeling_tf_encoder_decoder.py b/src/transformers/models/encoder_decoder/modeling_tf_encoder_decoder.py index f5cd5e445aac..a1a46ed569bc 100644 --- a/src/transformers/models/encoder_decoder/modeling_tf_encoder_decoder.py +++ b/src/transformers/models/encoder_decoder/modeling_tf_encoder_decoder.py @@ -36,7 +36,6 @@ ) from ...tf_utils import shape_list from ...utils import ( - DUMMY_INPUTS, ModelOutput, add_start_docstrings, add_start_docstrings_to_model_forward, @@ -277,17 +276,11 @@ def __init__( ) @property - def dummy_inputs(self): - """ - Dummy inputs to build the network. - - Returns: - `Dict[str, tf.Tensor]`: The dummy inputs. - """ - # Add `decoder_input_ids` because `self.decoder` requires it. - input_ids = tf.constant(DUMMY_INPUTS, dtype=tf.int32) - dummy = {"input_ids": input_ids, "decoder_input_ids": input_ids} - return dummy + def input_signature(self): + return { + "input_ids": tf.TensorSpec([None, None], dtype=tf.int32), + "decoder_input_ids": tf.TensorSpec([None, None], dtype=tf.int32), + } def get_encoder(self): return self.encoder @@ -642,33 +635,6 @@ def call( encoder_attentions=encoder_outputs.attentions, ) - def serving_output(self, output): - pkv = tf.tuple(output.past_key_values)[1] if self.config.decoder.use_cache else None - dec_hs = ( - tf.convert_to_tensor(output.decoder_hidden_states) if self.config.decoder.output_hidden_states else None - ) - dec_attns = tf.convert_to_tensor(output.decoder_attentions) if self.config.decoder.output_attentions else None - enc_hs = ( - tf.convert_to_tensor(output.encoder_hidden_states) if self.config.encoder.output_hidden_states else None - ) - enc_attns = tf.convert_to_tensor(output.encoder_attentions) if self.config.encoder.output_attentions else None - cross_attns = ( - tf.convert_to_tensor(output.cross_attentions) - if self.config.decoder.output_attentions and output.cross_attentions is not None - else None - ) - - return TFSeq2SeqLMOutput( - logits=output.logits, - past_key_values=pkv, - decoder_hidden_states=dec_hs, - decoder_attentions=dec_attns, - encoder_last_hidden_state=output.encoder_last_hidden_state, - encoder_hidden_states=enc_hs, - encoder_attentions=enc_attns, - cross_attentions=cross_attns, - ) - def prepare_inputs_for_generation( self, input_ids, past_key_values=None, attention_mask=None, use_cache=None, encoder_outputs=None, **kwargs ): diff --git a/src/transformers/models/funnel/modeling_tf_funnel.py b/src/transformers/models/funnel/modeling_tf_funnel.py index fa077d612d21..522add5a8c1f 100644 --- a/src/transformers/models/funnel/modeling_tf_funnel.py +++ b/src/transformers/models/funnel/modeling_tf_funnel.py @@ -19,7 +19,7 @@ import warnings from dataclasses import dataclass -from typing import Dict, Optional, Tuple, Union +from typing import Optional, Tuple, Union import numpy as np import tensorflow as tf @@ -47,7 +47,6 @@ ) from ...tf_utils import check_embeddings_within_bounds, shape_list, stable_softmax from ...utils import ( - MULTIPLE_CHOICE_DUMMY_INPUTS, ModelOutput, add_code_sample_docstrings, add_start_docstrings, @@ -1133,15 +1132,6 @@ def call( training=training, ) - def serving_output(self, output): - # hidden_states and attentions not converted to Tensor with tf.convert_to_tensor as they are all of - # different dimensions - return TFBaseModelOutput( - last_hidden_state=output.last_hidden_state, - hidden_states=output.hidden_states, - attentions=output.attentions, - ) - @add_start_docstrings( "The bare Funnel Transformer Model transformer outputting raw hidden-states without any specific head on top.", @@ -1181,15 +1171,6 @@ def call( training=training, ) - def serving_output(self, output): - # hidden_states and attentions not converted to Tensor with tf.convert_to_tensor as they are all of - # different dimensions - return TFBaseModelOutput( - last_hidden_state=output.last_hidden_state, - hidden_states=output.hidden_states, - attentions=output.attentions, - ) - @add_start_docstrings( """ @@ -1256,13 +1237,6 @@ def call( attentions=discriminator_hidden_states.attentions, ) - def serving_output(self, output): - # hidden_states and attentions not converted to Tensor with tf.convert_to_tensor as they are all of - # different dimensions - return TFFunnelForPreTrainingOutput( - logits=output.logits, hidden_states=output.hidden_states, attentions=output.attentions - ) - @add_start_docstrings("""Funnel Model with a `language modeling` head on top.""", FUNNEL_START_DOCSTRING) class TFFunnelForMaskedLM(TFFunnelPreTrainedModel, TFMaskedLanguageModelingLoss): @@ -1330,11 +1304,6 @@ def call( attentions=outputs.attentions, ) - def serving_output(self, output: TFMaskedLMOutput) -> TFMaskedLMOutput: - # hidden_states and attentions not converted to Tensor with tf.convert_to_tensor as they are all of - # different dimensions - return TFMaskedLMOutput(logits=output.logits, hidden_states=output.hidden_states, attentions=output.attentions) - @add_start_docstrings( """ @@ -1403,13 +1372,6 @@ def call( attentions=outputs.attentions, ) - def serving_output(self, output: TFSequenceClassifierOutput) -> TFSequenceClassifierOutput: - # hidden_states and attentions not converted to Tensor with tf.convert_to_tensor as they are all of - # different dimensions - return TFSequenceClassifierOutput( - logits=output.logits, hidden_states=output.hidden_states, attentions=output.attentions - ) - @add_start_docstrings( """ @@ -1425,16 +1387,6 @@ def __init__(self, config: FunnelConfig, *inputs, **kwargs) -> None: self.funnel = TFFunnelBaseLayer(config, name="funnel") self.classifier = TFFunnelClassificationHead(config, 1, name="classifier") - @property - def dummy_inputs(self): - """ - Dummy inputs to build the network. - - Returns: - tf.Tensor with dummy inputs - """ - return {"input_ids": tf.constant(MULTIPLE_CHOICE_DUMMY_INPUTS, dtype=tf.int32)} - @unpack_inputs @add_start_docstrings_to_model_forward(FUNNEL_INPUTS_DOCSTRING.format("batch_size, num_choices, sequence_length")) @add_code_sample_docstrings( @@ -1504,27 +1456,6 @@ def call( attentions=outputs.attentions, ) - @tf.function( - input_signature=[ - { - "input_ids": tf.TensorSpec((None, None), tf.int32, name="input_ids"), - "attention_mask": tf.TensorSpec((None, None), tf.float32, name="attention_mask"), - "token_type_ids": tf.TensorSpec((None, None), tf.int32, name="token_type_ids"), - } - ] - ) - def serving(self, inputs: Dict[str, tf.Tensor]) -> TFMultipleChoiceModelOutput: - output = self.call(input_ids=inputs) - - return self.serving_output(output=output) - - def serving_output(self, output: TFMultipleChoiceModelOutput) -> TFMultipleChoiceModelOutput: - # hidden_states and attentions not converted to Tensor with tf.convert_to_tensor as they are all of - # different dimensions - return TFMultipleChoiceModelOutput( - logits=output.logits, hidden_states=output.hidden_states, attentions=output.attentions - ) - @add_start_docstrings( """ @@ -1595,13 +1526,6 @@ def call( attentions=outputs.attentions, ) - def serving_output(self, output: TFTokenClassifierOutput) -> TFTokenClassifierOutput: - # hidden_states and attentions not converted to Tensor with tf.convert_to_tensor as they are all of - # different dimensions - return TFTokenClassifierOutput( - logits=output.logits, hidden_states=output.hidden_states, attentions=output.attentions - ) - @add_start_docstrings( """ @@ -1684,13 +1608,3 @@ def call( hidden_states=outputs.hidden_states, attentions=outputs.attentions, ) - - def serving_output(self, output: TFQuestionAnsweringModelOutput) -> TFQuestionAnsweringModelOutput: - # hidden_states and attentions not converted to Tensor with tf.convert_to_tensor as they are all of - # different dimensions - return TFQuestionAnsweringModelOutput( - start_logits=output.start_logits, - end_logits=output.end_logits, - hidden_states=output.hidden_states, - attentions=output.attentions, - ) diff --git a/src/transformers/models/gpt2/modeling_tf_gpt2.py b/src/transformers/models/gpt2/modeling_tf_gpt2.py index 6b7476b71bba..ab6bc07947cc 100644 --- a/src/transformers/models/gpt2/modeling_tf_gpt2.py +++ b/src/transformers/models/gpt2/modeling_tf_gpt2.py @@ -42,7 +42,6 @@ ) from ...tf_utils import check_embeddings_within_bounds, shape_list, stable_softmax from ...utils import ( - DUMMY_INPUTS, ModelOutput, add_code_sample_docstrings, add_start_docstrings, @@ -522,37 +521,6 @@ class TFGPT2PreTrainedModel(TFPreTrainedModel): # names with a '.' represents the authorized unexpected/missing layers when a TF model is loaded from a PT model _keys_to_ignore_on_load_unexpected = [r"h.\d+.attn.bias", r"h.\d+.crossattention.bias"] - @property - def dummy_inputs(self): - """ - Dummy inputs to build the network. - - Returns: - `Dict[str, tf.Tensor]`: The dummy inputs. - """ - dummy = {"input_ids": tf.constant(DUMMY_INPUTS, dtype=tf.int32)} - # Add `encoder_hidden_states` to make the cross-attention layers' weights initialized - if self.config.add_cross_attention: - batch_size, seq_len = tf.constant(DUMMY_INPUTS).shape - shape = (batch_size, seq_len) + (self.config.hidden_size,) - h = tf.random.uniform(shape=shape) - dummy["encoder_hidden_states"] = h - - return dummy - - @tf.function( - input_signature=[ - { - "input_ids": tf.TensorSpec((None, None), tf.int32, name="input_ids"), - "attention_mask": tf.TensorSpec((None, None), tf.int32, name="attention_mask"), - } - ] - ) - def serving(self, inputs): - output = self.call(inputs) - - return self.serving_output(output) - @dataclass class TFGPT2DoubleHeadsModelOutput(ModelOutput): @@ -773,26 +741,6 @@ def call( return outputs - def serving_output(self, output): - pkv = tf.convert_to_tensor(output.past_key_values) if self.config.use_cache else None - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - cross_attns = ( - tf.convert_to_tensor(output.cross_attentions) - if self.config.output_attentions - and self.config.add_cross_attention - and output.cross_attentions is not None - else None - ) - - return TFBaseModelOutputWithPastAndCrossAttentions( - last_hidden_state=output.last_hidden_state, - past_key_values=pkv, - hidden_states=hs, - attentions=attns, - cross_attentions=cross_attns, - ) - @add_start_docstrings( """ @@ -925,22 +873,6 @@ def call( cross_attentions=transformer_outputs.cross_attentions, ) - def serving_output(self, output): - pkv = tf.convert_to_tensor(output.past_key_values) if self.config.use_cache else None - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - cross_attns = ( - tf.convert_to_tensor(output.cross_attentions) - if self.config.output_attentions - and self.config.add_cross_attention - and output.cross_attentions is not None - else None - ) - - return TFCausalLMOutputWithCrossAttentions( - logits=output.logits, past_key_values=pkv, hidden_states=hs, attentions=attns, cross_attentions=cross_attns - ) - @add_start_docstrings( """ @@ -1062,32 +994,13 @@ def call( attentions=transformer_outputs.attentions, ) - @tf.function( - input_signature=[ - { - "input_ids": tf.TensorSpec((None, None, None), tf.int32, name="input_ids"), - "attention_mask": tf.TensorSpec((None, None, None), tf.int32, name="attention_mask"), - "mc_token_ids": tf.TensorSpec((None, None), tf.int32, name="mc_token_ids"), - } - ] - ) - def serving(self, inputs): - output = self.call(inputs) - - return self.serving_output(output) - - def serving_output(self, output): - pkv = tf.convert_to_tensor(output.past_key_values) if self.config.use_cache else None - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFGPT2DoubleHeadsModelOutput( - logits=output.logits, - mc_logits=output.mc_logits, - past_key_values=pkv, - hidden_states=hs, - attentions=attns, - ) + @property + def input_signature(self): + return { + "input_ids": tf.TensorSpec((None, None, None), tf.int32, name="input_ids"), + "attention_mask": tf.TensorSpec((None, None, None), tf.int32, name="attention_mask"), + "mc_token_ids": tf.TensorSpec((None, None), tf.int32, name="mc_token_ids"), + } @add_start_docstrings( @@ -1210,12 +1123,3 @@ def call( hidden_states=transformer_outputs.hidden_states, attentions=transformer_outputs.attentions, ) - - def serving_output(self, output): - pkv = tf.convert_to_tensor(output.past_key_values) if self.config.use_cache else None - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFSequenceClassifierOutputWithPast( - logits=output.logits, past_key_values=pkv, hidden_states=hs, attentions=attns - ) diff --git a/src/transformers/models/gptj/modeling_tf_gptj.py b/src/transformers/models/gptj/modeling_tf_gptj.py index 09e4330eb182..bbcdf3bd240a 100644 --- a/src/transformers/models/gptj/modeling_tf_gptj.py +++ b/src/transformers/models/gptj/modeling_tf_gptj.py @@ -23,7 +23,6 @@ from ...activations_tf import get_tf_activation from ...file_utils import ( - DUMMY_INPUTS, add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, @@ -513,30 +512,6 @@ class TFGPTJPreTrainedModel(TFPreTrainedModel): # names with a '.' represents the authorized unexpected/missing layers when a TF model is loaded from a PT model _keys_to_ignore_on_load_unexpected = [r"h.\d+.attn.bias"] - @property - def dummy_inputs(self): - """ - Dummy inputs to build the network. - - Returns: - `Dict[str, tf.Tensor]`: The dummy inputs. - """ - dummy = {"input_ids": tf.constant(DUMMY_INPUTS, dtype=tf.int32)} - return dummy - - @tf.function( - input_signature=[ - { - "input_ids": tf.TensorSpec((None, None), tf.int32, name="input_ids"), - "attention_mask": tf.TensorSpec((None, None), tf.int32, name="attention_mask"), - } - ] - ) - def serving(self, inputs): - output = self.call(inputs) - - return self.serving_output(output) - GPTJ_START_DOCSTRING = r""" @@ -697,18 +672,6 @@ def call( return outputs - def serving_output(self, output): - pkv = tf.convert_to_tensor(output.past_key_values) if self.config.use_cache else None - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFBaseModelOutputWithPast( - last_hidden_state=output.last_hidden_state, - past_key_values=pkv, - hidden_states=hs, - attentions=attns, - ) - @add_start_docstrings( """ @@ -821,13 +784,6 @@ def call( attentions=transformer_outputs.attentions, ) - def serving_output(self, output): - pkv = tf.convert_to_tensor(output.past_key_values) if self.config.use_cache else None - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFCausalLMOutputWithPast(logits=output.logits, past_key_values=pkv, hidden_states=hs, attentions=attns) - @add_start_docstrings( """ @@ -952,15 +908,6 @@ def call( attentions=transformer_outputs.attentions, ) - def serving_output(self, output): - pkv = tf.convert_to_tensor(output.past_key_values) if self.config.use_cache else None - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFSequenceClassifierOutputWithPast( - logits=output.logits, past_key_values=pkv, hidden_states=hs, attentions=attns - ) - @add_start_docstrings( """ @@ -1051,11 +998,3 @@ def call( hidden_states=transformer_outputs.hidden_states, attentions=transformer_outputs.attentions, ) - - def serving_output(self, output: TFQuestionAnsweringModelOutput) -> TFQuestionAnsweringModelOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFQuestionAnsweringModelOutput( - start_logits=output.start_logits, end_logits=output.end_logits, hidden_states=hs, attentions=attns - ) diff --git a/src/transformers/models/groupvit/modeling_tf_groupvit.py b/src/transformers/models/groupvit/modeling_tf_groupvit.py index e51194bd14ae..5c989356a5de 100644 --- a/src/transformers/models/groupvit/modeling_tf_groupvit.py +++ b/src/transformers/models/groupvit/modeling_tf_groupvit.py @@ -20,7 +20,7 @@ import collections.abc import math from dataclasses import dataclass -from typing import Any, Dict, Optional, Tuple, Union +from typing import Any, Optional, Tuple, Union import numpy as np import tensorflow as tf @@ -36,7 +36,6 @@ ) from ...tf_utils import check_embeddings_within_bounds, shape_list, stable_softmax from ...utils import ( - DUMMY_INPUTS, ModelOutput, add_start_docstrings, add_start_docstrings_to_model_forward, @@ -1608,30 +1607,6 @@ def __init__(self, config: GroupViTTextConfig, *inputs, **kwargs): self.groupvit = TFGroupViTTextMainLayer(config, name="groupvit") - @property - def dummy_inputs(self) -> Dict[str, tf.Tensor]: - """ - Dummy inputs to build the network. - - Returns: - `Dict[str, tf.Tensor]`: The dummy inputs. - """ - return { - "input_ids": tf.constant(DUMMY_INPUTS, dtype=tf.int32), - } - - @tf.function( - input_signature=[ - { - "input_ids": tf.TensorSpec((None, None), tf.int32, name="input_ids"), - "attention_mask": tf.TensorSpec((None, None), tf.int32, name="attention_mask"), - } - ] - ) - def serving(self, inputs: Dict[str, tf.Tensor]) -> TFBaseModelOutputWithPooling: - output = self.call(inputs) - return self.serving_output(output) - @unpack_inputs @add_start_docstrings_to_model_forward(GROUPVIT_TEXT_INPUTS_DOCSTRING.format("batch_size, sequence_length")) @replace_return_docstrings(output_type=TFBaseModelOutputWithPooling, config_class=GroupViTTextConfig) @@ -1675,17 +1650,6 @@ def call( return outputs - def serving_output(self, output: TFBaseModelOutputWithPooling) -> TFBaseModelOutputWithPooling: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFBaseModelOutputWithPooling( - last_hidden_state=output.last_hidden_state, - pooler_output=output.pooler_output, - hidden_states=hs, - attentions=attns, - ) - class TFGroupViTVisionModel(TFGroupViTPreTrainedModel): config_class = GroupViTVisionConfig @@ -1696,38 +1660,6 @@ def __init__(self, config: GroupViTVisionConfig, *inputs, **kwargs): self.groupvit = TFGroupViTVisionMainLayer(config, name="groupvit") - @property - def dummy_inputs(self) -> Dict[str, tf.Tensor]: - """ - Dummy inputs to build the network. - - Returns: - `Dict[str, tf.Tensor]`: The dummy inputs. - """ - VISION_DUMMY_INPUTS = tf.random.uniform( - shape=(len(DUMMY_INPUTS), 3, self.config.image_size, self.config.image_size), dtype=tf.float32 - ) - return {"pixel_values": VISION_DUMMY_INPUTS} - - @tf.function( - input_signature=[ - { - "pixel_values": tf.TensorSpec((None, None, None, None), tf.float32, name="pixel_values"), - } - ] - ) - def serving(self, inputs: Dict[str, tf.Tensor]) -> TFBaseModelOutputWithPooling: - """ - Method used for serving the model. - - Args: - inputs (`Dict[str, tf.Tensor]`): - The input of the saved model as a dictionary of tensors. - """ - output = self.call(inputs) - - return self.serving_output(output) - @unpack_inputs @add_start_docstrings_to_model_forward(GROUPVIT_VISION_INPUTS_DOCSTRING) @replace_return_docstrings(output_type=TFBaseModelOutputWithPooling, config_class=GroupViTVisionConfig) @@ -1772,15 +1704,6 @@ def call( return outputs - def serving_output(self, output: TFBaseModelOutputWithPooling) -> TFBaseModelOutputWithPooling: - # hidden_states and attentions not converted to Tensor with tf.convert_to_tensor as they are all of different dimensions - return TFBaseModelOutputWithPooling( - last_hidden_state=output.last_hidden_state, - pooler_output=output.pooler_output, - hidden_states=output.hidden_states, - attentions=output.attentions, - ) - @add_start_docstrings(GROUPVIT_START_DOCSTRING) class TFGroupViTModel(TFGroupViTPreTrainedModel): @@ -1791,44 +1714,6 @@ def __init__(self, config: GroupViTConfig, *inputs, **kwargs): self.groupvit = TFGroupViTMainLayer(config, name="groupvit") - @property - def dummy_inputs(self) -> Dict[str, tf.Tensor]: - """ - Dummy inputs to build the network. - - Returns: - `Dict[str, tf.Tensor]`: The dummy inputs. - """ - VISION_DUMMY_INPUTS = tf.random.uniform( - shape=(len(DUMMY_INPUTS), 3, self.config.vision_config.image_size, self.config.vision_config.image_size), - dtype=tf.float32, - ) - return { - "input_ids": tf.constant(DUMMY_INPUTS, dtype=tf.int32), - "pixel_values": VISION_DUMMY_INPUTS, - } - - @tf.function( - input_signature=[ - { - "input_ids": tf.TensorSpec((None, None), tf.int32, name="input_ids"), - "pixel_values": tf.TensorSpec((None, None, None, None), tf.float64, name="pixel_values"), - "attention_mask": tf.TensorSpec((None, None), tf.int32, name="attention_mask"), - } - ] - ) - def serving(self, inputs: Dict[str, tf.Tensor]) -> TFGroupViTModelOutput: - """ - Method used for serving the model. - - Args: - inputs (`Dict[str, tf.Tensor]`): - The input of the saved model as a dictionary of tensors. - """ - output = self.call(inputs) - - return self.serving_output(output) - @unpack_inputs @add_start_docstrings_to_model_forward(GROUPVIT_TEXT_INPUTS_DOCSTRING.format("batch_size, sequence_length")) def get_text_features( diff --git a/src/transformers/models/hubert/modeling_tf_hubert.py b/src/transformers/models/hubert/modeling_tf_hubert.py index fd1f17edfb3a..c237616bf2a4 100644 --- a/src/transformers/models/hubert/modeling_tf_hubert.py +++ b/src/transformers/models/hubert/modeling_tf_hubert.py @@ -17,7 +17,7 @@ from __future__ import annotations import warnings -from typing import Any, Dict, Optional, Tuple, Union +from typing import Any, Optional, Tuple, Union import numpy as np import tensorflow as tf @@ -1157,14 +1157,12 @@ class TFHubertPreTrainedModel(TFPreTrainedModel): main_input_name = "input_values" @property - def dummy_inputs(self) -> Dict[str, tf.Tensor]: - pad_token = 0.0 - input_values = tf.convert_to_tensor(np.random.rand(1, 16000), tf.float32) - dummy_inputs = { - "input_values": input_values, - "attention_mask": tf.cast(tf.not_equal(input_values, pad_token), tf.float32), + def input_signature(self): + return { + "input_values": tf.TensorSpec((None, 16000), tf.float32, name="input_values"), + "attention_mask": tf.TensorSpec((None, None), tf.int32, name="attention_mask"), + "token_type_ids": tf.TensorSpec((None, None), tf.int32, name="token_type_ids"), } - return dummy_inputs def __init__(self, config, *inputs, **kwargs): super().__init__(config, *inputs, **kwargs) @@ -1173,20 +1171,6 @@ def __init__(self, config, *inputs, **kwargs): "to train/fine-tine this model, you need a GPU or a TPU" ) - @tf.function( - input_signature=[ - { - "input_values": tf.TensorSpec((None, None), tf.float32, name="input_values"), - "attention_mask": tf.TensorSpec((None, None), tf.int32, name="attention_mask"), - "token_type_ids": tf.TensorSpec((None, None), tf.int32, name="token_type_ids"), - } - ] - ) - def serving(self, inputs): - output = self.call(input_values=inputs, training=False) - - return self.serving_output(output) - HUBERT_START_DOCSTRING = r""" @@ -1359,13 +1343,6 @@ def call( return outputs - def serving_output(self, output): - hidden_states = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attentions = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - return TFBaseModelOutput( - last_hidden_state=output.last_hidden_state, hidden_states=hidden_states, attentions=attentions - ) - @add_start_docstrings( """TFHubert Model with a `language modeling` head on top for Connectionist Temporal Classification (CTC).""", @@ -1518,8 +1495,3 @@ def call( hidden_states=outputs.hidden_states, attentions=outputs.attentions, ) - - def serving_output(self, output: TFCausalLMOutput) -> TFCausalLMOutput: - hidden_states = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attentions = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - return TFCausalLMOutput(logits=output.logits, hidden_states=hidden_states, attentions=attentions) diff --git a/src/transformers/models/led/modeling_tf_led.py b/src/transformers/models/led/modeling_tf_led.py index 4e815da33d9e..988107236df3 100644 --- a/src/transformers/models/led/modeling_tf_led.py +++ b/src/transformers/models/led/modeling_tf_led.py @@ -1323,33 +1323,13 @@ class TFLEDPreTrainedModel(TFPreTrainedModel): base_model_prefix = "led" @property - def dummy_inputs(self): - input_ids = tf.convert_to_tensor([[7, 6, 0, 0, 1], [1, 2, 3, 0, 0]], dtype=tf.int32) - # make sure global layers are initialized - attention_mask = tf.convert_to_tensor([[1, 1, 0, 0, 1], [1, 1, 1, 0, 0]], dtype=tf.int32) - global_attention_mask = tf.convert_to_tensor([[0, 0, 0, 0, 1], [0, 0, 1, 0, 0]], dtype=tf.int32) - dummy_inputs = { - "input_ids": input_ids, - "attention_mask": attention_mask, - "global_attention_mask": global_attention_mask, - "decoder_input_ids": input_ids, + def input_signature(self): + return { + "input_ids": tf.TensorSpec((None, None), tf.int32, name="input_ids"), + "attention_mask": tf.TensorSpec((None, None), tf.int32, name="attention_mask"), + "decoder_input_ids": tf.TensorSpec((None, None), tf.int32, name="decoder_input_ids"), + "decoder_attention_mask": tf.TensorSpec((None, None), tf.int32, name="decoder_attention_mask"), } - return dummy_inputs - - @tf.function( - input_signature=[ - { - "input_ids": tf.TensorSpec((None, None), tf.int32, name="input_ids"), - "attention_mask": tf.TensorSpec((None, None), tf.int32, name="attention_mask"), - "decoder_input_ids": tf.TensorSpec((None, None), tf.int32, name="decoder_input_ids"), - "decoder_attention_mask": tf.TensorSpec((None, None), tf.int32, name="decoder_attention_mask"), - } - ] - ) - def serving(self, inputs): - output = self.call(inputs) - - return self.serving_output(output) @dataclass diff --git a/src/transformers/models/longformer/modeling_tf_longformer.py b/src/transformers/models/longformer/modeling_tf_longformer.py index b5adb2c803e9..b9e471fda48d 100644 --- a/src/transformers/models/longformer/modeling_tf_longformer.py +++ b/src/transformers/models/longformer/modeling_tf_longformer.py @@ -39,7 +39,6 @@ ) from ...tf_utils import check_embeddings_within_bounds, shape_list, stable_softmax from ...utils import ( - MULTIPLE_CHOICE_DUMMY_INPUTS, ModelOutput, add_code_sample_docstrings, add_start_docstrings, @@ -1874,32 +1873,13 @@ class TFLongformerPreTrainedModel(TFPreTrainedModel): base_model_prefix = "longformer" @property - def dummy_inputs(self): - input_ids = tf.convert_to_tensor([[7, 6, 0, 0, 1], [1, 2, 3, 0, 0], [0, 0, 0, 4, 5]], dtype=tf.int32) - # make sure global layers are initialized - attention_mask = tf.convert_to_tensor([[1, 1, 0, 0, 1], [1, 1, 1, 0, 0], [1, 0, 0, 1, 1]], dtype=tf.int32) - global_attention_mask = tf.convert_to_tensor( - [[0, 0, 0, 0, 1], [0, 0, 1, 0, 0], [0, 0, 0, 0, 1]], dtype=tf.int32 - ) + def input_signature(self): return { - "input_ids": input_ids, - "attention_mask": attention_mask, - "global_attention_mask": global_attention_mask, + "input_ids": tf.TensorSpec((None, None), tf.int32, name="input_ids"), + "attention_mask": tf.TensorSpec((None, None), tf.int32, name="attention_mask"), + "global_attention_mask": tf.TensorSpec((None, None), tf.int32, name="global_attention_mask"), } - @tf.function( - input_signature=[ - { - "input_ids": tf.TensorSpec((None, None), tf.int32, name="input_ids"), - "attention_mask": tf.TensorSpec((None, None), tf.int32, name="attention_mask"), - } - ] - ) - def serving(self, inputs): - output = self.call(inputs) - - return self.serving_output(output) - LONGFORMER_START_DOCSTRING = r""" @@ -2069,19 +2049,6 @@ def call( return outputs - def serving_output(self, output): - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - g_attns = tf.convert_to_tensor(output.global_attentions) if self.config.output_attentions else None - - return TFLongformerBaseModelOutputWithPooling( - last_hidden_state=output.last_hidden_state, - pooler_output=output.pooler_output, - hidden_states=hs, - attentions=attns, - global_attentions=g_attns, - ) - @add_start_docstrings( """Longformer Model with a `language modeling` head on top.""", @@ -2166,15 +2133,6 @@ def call( global_attentions=outputs.global_attentions, ) - def serving_output(self, output): - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - g_attns = tf.convert_to_tensor(output.global_attentions) if self.config.output_attentions else None - - return TFLongformerMaskedLMOutput( - logits=output.logits, hidden_states=hs, attentions=attns, global_attentions=g_attns - ) - @add_start_docstrings( """ @@ -2305,19 +2263,6 @@ def call( global_attentions=outputs.global_attentions, ) - def serving_output(self, output): - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - g_attns = tf.convert_to_tensor(output.global_attentions) if self.config.output_attentions else None - - return TFLongformerQuestionAnsweringModelOutput( - start_logits=output.start_logits, - end_logits=output.end_logits, - hidden_states=hs, - attentions=attns, - global_attentions=g_attns, - ) - class TFLongformerClassificationHead(tf.keras.layers.Layer): """Head for sentence-level classification tasks.""" @@ -2446,15 +2391,6 @@ def call( global_attentions=outputs.global_attentions, ) - def serving_output(self, output): - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - g_attns = tf.convert_to_tensor(output.global_attentions) if self.config.output_attentions else None - - return TFLongformerSequenceClassifierOutput( - logits=output.logits, hidden_states=hs, attentions=attns, global_attentions=g_attns - ) - @add_start_docstrings( """ @@ -2476,13 +2412,6 @@ def __init__(self, config, *inputs, **kwargs): 1, kernel_initializer=get_initializer(config.initializer_range), name="classifier" ) - @property - def dummy_inputs(self): - input_ids = tf.convert_to_tensor(MULTIPLE_CHOICE_DUMMY_INPUTS, dtype=tf.int32) - # make sure global layers are initialized - global_attention_mask = tf.convert_to_tensor([[[0, 0, 0, 1], [0, 0, 0, 1]]] * 2, dtype=tf.int32) - return {"input_ids": input_ids, "global_attention_mask": global_attention_mask} - @unpack_inputs @add_start_docstrings_to_model_forward( LONGFORMER_INPUTS_DOCSTRING.format("batch_size, num_choices, sequence_length") @@ -2568,28 +2497,6 @@ def call( global_attentions=outputs.global_attentions, ) - @tf.function( - input_signature=[ - { - "input_ids": tf.TensorSpec((None, None, None), tf.int32, name="input_ids"), - "attention_mask": tf.TensorSpec((None, None, None), tf.int32, name="attention_mask"), - } - ] - ) - def serving(self, inputs): - output = self.call(inputs) - - return self.serving_output(output) - - def serving_output(self, output): - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - g_attns = tf.convert_to_tensor(output.global_attentions) if self.config.output_attentions else None - - return TFLongformerMultipleChoiceModelOutput( - logits=output.logits, hidden_states=hs, attentions=attns, global_attentions=g_attns - ) - @add_start_docstrings( """ @@ -2669,12 +2576,3 @@ def call( attentions=outputs.attentions, global_attentions=outputs.global_attentions, ) - - def serving_output(self, output): - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - g_attns = tf.convert_to_tensor(output.global_attentions) if self.config.output_attentions else None - - return TFLongformerTokenClassifierOutput( - logits=output.logits, hidden_states=hs, attentions=attns, global_attentions=g_attns - ) diff --git a/src/transformers/models/lxmert/modeling_tf_lxmert.py b/src/transformers/models/lxmert/modeling_tf_lxmert.py index 59bc18591405..0b54702d761d 100644 --- a/src/transformers/models/lxmert/modeling_tf_lxmert.py +++ b/src/transformers/models/lxmert/modeling_tf_lxmert.py @@ -636,26 +636,6 @@ def call( class TFLxmertMainLayer(tf.keras.layers.Layer): config_class = LxmertConfig - @property - def dummy_inputs(self): - """ - Dummy inputs to build the network. - - Returns: - tf.Tensor with dummy inputs - """ - batch_size = 2 - num_visual_features = 10 - input_ids = tf.constant([[3, 5, 6], [2, 3, 4]], dtype=tf.int32) - visual_feats = tf.random.uniform((batch_size, num_visual_features, self.config.visual_feat_dim)) - visual_pos = tf.random.uniform((batch_size, num_visual_features, 4)) - - return { - "input_ids": input_ids, - "visual_feats": visual_feats, - "visual_pos": visual_pos, - } - def __init__(self, config, **kwargs): super().__init__(**kwargs) @@ -802,25 +782,35 @@ class TFLxmertPreTrainedModel(TFPreTrainedModel): base_model_prefix = "lxmert" @property - def dummy_inputs(self) -> Dict[str, tf.Tensor]: - return getattr(self, self.base_model_prefix).dummy_inputs - - @tf.function( - input_signature=[ - { - "input_ids": tf.TensorSpec((None, None), tf.int32, name="input_ids"), - "attention_mask": tf.TensorSpec((None, None), tf.int32, name="attention_mask"), - "visual_feats": tf.TensorSpec((None, None, None), tf.float32, name="visual_feats"), - "visual_pos": tf.TensorSpec((None, None, None), tf.float32, name="visual_pos"), - "visual_attention_mask": tf.TensorSpec((None, None), tf.int32, name="visual_attention_mask"), - "token_type_ids": tf.TensorSpec((None, None), tf.int32, name="token_type_ids"), - } - ] - ) - def serving(self, inputs): - output = self.call(inputs) + def dummy_inputs(self): + """ + Dummy inputs to build the network. - return self.serving_output(output) + Returns: + tf.Tensor with dummy inputs + """ + batch_size = 2 + num_visual_features = 10 + input_ids = tf.constant([[3, 5, 6], [2, 3, 4]], dtype=tf.int32) + visual_feats = tf.random.uniform((batch_size, num_visual_features, self.config.visual_feat_dim)) + visual_pos = tf.random.uniform((batch_size, num_visual_features, 4)) + + return { + "input_ids": input_ids, + "visual_feats": visual_feats, + "visual_pos": visual_pos, + } + + @property + def input_signature(self): + return { + "input_ids": tf.TensorSpec((None, None), tf.int32, name="input_ids"), + "attention_mask": tf.TensorSpec((None, None), tf.int32, name="attention_mask"), + "visual_feats": tf.TensorSpec((None, None, self.config.visual_feat_dim), tf.float32, name="visual_feats"), + "visual_pos": tf.TensorSpec((None, None, 4), tf.float32, name="visual_pos"), + "visual_attention_mask": tf.TensorSpec((None, None), tf.int32, name="visual_attention_mask"), + "token_type_ids": tf.TensorSpec((None, None), tf.int32, name="token_type_ids"), + } LXMERT_START_DOCSTRING = r""" @@ -976,24 +966,6 @@ def call( return outputs - def serving_output(self, output): - l_hs = tf.convert_to_tensor(output.language_hidden_states) if self.config.output_hidden_states else None - v_hs = tf.convert_to_tensor(output.vision_hidden_states) if self.config.output_hidden_states else None - l_attns = tf.convert_to_tensor(output.language_attentions) if self.config.output_attentions else None - v_attns = tf.convert_to_tensor(output.vision_attentions) if self.config.output_attentions else None - c_enc_attns = tf.convert_to_tensor(output.cross_encoder_attentions) if self.config.output_attentions else None - - return TFLxmertModelOutput( - pooled_output=output.pooled_output, - language_output=output.language_output, - vision_output=output.vision_output, - language_hidden_states=l_hs, - vision_hidden_states=v_hs, - language_attentions=l_attns, - vision_attentions=v_attns, - cross_encoder_attentions=c_enc_attns, - ) - class TFLxmertPooler(tf.keras.layers.Layer): def __init__(self, config, **kwargs): @@ -1415,21 +1387,3 @@ def call( vision_attentions=lxmert_output.vision_attentions, cross_encoder_attentions=lxmert_output.cross_encoder_attentions, ) - - def serving_output(self, output): - l_hs = tf.convert_to_tensor(output.language_hidden_states) if self.config.output_hidden_states else None - v_hs = tf.convert_to_tensor(output.vision_hidden_states) if self.config.output_hidden_states else None - l_attns = tf.convert_to_tensor(output.language_attentions) if self.config.output_attentions else None - v_attns = tf.convert_to_tensor(output.vision_attentions) if self.config.output_attentions else None - c_enc_attns = tf.convert_to_tensor(output.cross_encoder_attentions) if self.config.output_attentions else None - - return TFLxmertForPreTrainingOutput( - prediction_logits=output.prediction_logits, - cross_relationship_score=output.cross_relationship_score, - question_answering_score=output.question_answering_score, - language_hidden_states=l_hs, - vision_hidden_states=v_hs, - language_attentions=l_attns, - vision_attentions=v_attns, - cross_encoder_attentions=c_enc_attns, - ) diff --git a/src/transformers/models/marian/modeling_tf_marian.py b/src/transformers/models/marian/modeling_tf_marian.py index bdd6defd0a97..85a81e48b002 100644 --- a/src/transformers/models/marian/modeling_tf_marian.py +++ b/src/transformers/models/marian/modeling_tf_marian.py @@ -40,7 +40,6 @@ ) from ...tf_utils import check_embeddings_within_bounds, shape_list, stable_softmax from ...utils import ( - DUMMY_INPUTS, ContextManagers, add_code_sample_docstrings, add_end_docstrings, @@ -502,32 +501,13 @@ class TFMarianPreTrainedModel(TFPreTrainedModel): base_model_prefix = "model" @property - def dummy_inputs(self): - pad_token = 1 - input_ids = tf.cast(tf.convert_to_tensor(DUMMY_INPUTS), tf.int32) - decoder_input_ids = tf.cast(tf.convert_to_tensor(DUMMY_INPUTS), tf.int32) - dummy_inputs = { - "decoder_input_ids": decoder_input_ids, - "attention_mask": tf.cast(input_ids != pad_token, tf.int32), - "input_ids": input_ids, + def input_signature(self): + return { + "input_ids": tf.TensorSpec((None, None), tf.int32, name="input_ids"), + "attention_mask": tf.TensorSpec((None, None), tf.int32, name="attention_mask"), + "decoder_input_ids": tf.TensorSpec((None, None), tf.int32, name="decoder_input_ids"), + "decoder_attention_mask": tf.TensorSpec((None, None), tf.int32, name="decoder_attention_mask"), } - return dummy_inputs - - @tf.function( - input_signature=[ - { - "input_ids": tf.TensorSpec((None, None), tf.int32, name="input_ids"), - "attention_mask": tf.TensorSpec((None, None), tf.int32, name="attention_mask"), - "decoder_input_ids": tf.TensorSpec((None, None), tf.int32, name="decoder_input_ids"), - "decoder_attention_mask": tf.TensorSpec((None, None), tf.int32, name="decoder_attention_mask"), - } - ] - ) - # Copied from transformers.models.bart.modeling_tf_bart.TFBartPretrainedModel.serving - def serving(self, inputs): - output = self.call(inputs) - - return self.serving_output(output) MARIAN_START_DOCSTRING = r""" diff --git a/src/transformers/models/mbart/modeling_tf_mbart.py b/src/transformers/models/mbart/modeling_tf_mbart.py index af88634d97a7..f1a05d8f735e 100644 --- a/src/transformers/models/mbart/modeling_tf_mbart.py +++ b/src/transformers/models/mbart/modeling_tf_mbart.py @@ -40,7 +40,6 @@ ) from ...tf_utils import check_embeddings_within_bounds, shape_list, stable_softmax from ...utils import ( - DUMMY_INPUTS, ContextManagers, add_code_sample_docstrings, add_end_docstrings, @@ -469,32 +468,13 @@ class TFMBartPreTrainedModel(TFPreTrainedModel): base_model_prefix = "model" @property - def dummy_inputs(self): - pad_token = 1 - input_ids = tf.cast(tf.convert_to_tensor(DUMMY_INPUTS), tf.int32) - decoder_input_ids = tf.cast(tf.convert_to_tensor(DUMMY_INPUTS), tf.int32) - dummy_inputs = { - "decoder_input_ids": decoder_input_ids, - "attention_mask": tf.cast(input_ids != pad_token, tf.int32), - "input_ids": input_ids, + def input_signature(self): + return { + "input_ids": tf.TensorSpec((None, None), tf.int32, name="input_ids"), + "attention_mask": tf.TensorSpec((None, None), tf.int32, name="attention_mask"), + "decoder_input_ids": tf.TensorSpec((None, None), tf.int32, name="decoder_input_ids"), + "decoder_attention_mask": tf.TensorSpec((None, None), tf.int32, name="decoder_attention_mask"), } - return dummy_inputs - - @tf.function( - input_signature=[ - { - "input_ids": tf.TensorSpec((None, None), tf.int32, name="input_ids"), - "attention_mask": tf.TensorSpec((None, None), tf.int32, name="attention_mask"), - "decoder_input_ids": tf.TensorSpec((None, None), tf.int32, name="decoder_input_ids"), - "decoder_attention_mask": tf.TensorSpec((None, None), tf.int32, name="decoder_attention_mask"), - } - ] - ) - # Copied from transformers.models.bart.modeling_tf_bart.TFBartPretrainedModel.serving - def serving(self, inputs): - output = self.call(inputs) - - return self.serving_output(output) MBART_START_DOCSTRING = r""" diff --git a/src/transformers/models/mobilevit/modeling_tf_mobilevit.py b/src/transformers/models/mobilevit/modeling_tf_mobilevit.py index 879c642800fe..4d48ce72725c 100644 --- a/src/transformers/models/mobilevit/modeling_tf_mobilevit.py +++ b/src/transformers/models/mobilevit/modeling_tf_mobilevit.py @@ -735,38 +735,6 @@ class TFMobileViTPreTrainedModel(TFPreTrainedModel): base_model_prefix = "mobilevit" main_input_name = "pixel_values" - @property - def dummy_inputs(self) -> Dict[str, tf.Tensor]: - """ - Dummy inputs to build the network. - - Returns: - `Dict[str, tf.Tensor]`: The dummy inputs. - """ - VISION_DUMMY_INPUTS = tf.random.uniform( - shape=(3, self.config.num_channels, self.config.image_size, self.config.image_size), - dtype=tf.float32, - ) - return {"pixel_values": tf.constant(VISION_DUMMY_INPUTS)} - - @tf.function( - input_signature=[ - { - "pixel_values": tf.TensorSpec((None, None, None, None), tf.float32, name="pixel_values"), - } - ] - ) - def serving(self, inputs): - """ - Method used for serving the model. - - Args: - inputs (`Dict[str, tf.Tensor]`): - The input of the saved model as a dictionary of tensors. - """ - output = self.call(inputs) - return self.serving_output(output) - MOBILEVIT_START_DOCSTRING = r""" This model inherits from [`TFPreTrainedModel`]. Check the superclass documentation for the generic methods the @@ -856,14 +824,6 @@ def call( output = self.mobilevit(pixel_values, output_hidden_states, return_dict, training=training) return output - def serving_output(self, output: TFBaseModelOutputWithPooling) -> TFBaseModelOutputWithPooling: - # hidden_states not converted to Tensor with tf.convert_to_tensor as they are all of different dimensions - return TFBaseModelOutputWithPooling( - last_hidden_state=output.last_hidden_state, - pooler_output=output.pooler_output, - hidden_states=output.hidden_states, - ) - @add_start_docstrings( """ @@ -924,10 +884,6 @@ def call( return TFImageClassifierOutputWithNoAttention(loss=loss, logits=logits, hidden_states=outputs.hidden_states) - def serving_output(self, output: TFImageClassifierOutputWithNoAttention) -> TFImageClassifierOutputWithNoAttention: - # hidden_states and attention not converted to Tensor with tf.convert_to_tensor as they are all of different dimensions - return TFImageClassifierOutputWithNoAttention(logits=output.logits, hidden_states=output.hidden_states) - class TFMobileViTASPPPooling(tf.keras.layers.Layer): def __init__(self, config: MobileViTConfig, out_channels: int, **kwargs) -> None: @@ -1157,8 +1113,3 @@ def call( logits=logits, hidden_states=outputs.hidden_states if output_hidden_states else None, ) - - def serving_output( - self, output: TFSemanticSegmenterOutputWithNoAttention - ) -> TFSemanticSegmenterOutputWithNoAttention: - return TFSemanticSegmenterOutputWithNoAttention(logits=output.logits, hidden_states=output.hidden_states) diff --git a/src/transformers/models/opt/modeling_tf_opt.py b/src/transformers/models/opt/modeling_tf_opt.py index a4e016c90899..5f7dd22369b8 100644 --- a/src/transformers/models/opt/modeling_tf_opt.py +++ b/src/transformers/models/opt/modeling_tf_opt.py @@ -36,7 +36,6 @@ ) from ...tf_utils import check_embeddings_within_bounds, shape_list, stable_softmax from ...utils import ( - DUMMY_INPUTS, add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, @@ -413,29 +412,6 @@ class TFOPTPreTrainedModel(TFPreTrainedModel): config_class = OPTConfig base_model_prefix = "model" - @property - def dummy_inputs(self): - pad_token = 1 - input_ids = tf.convert_to_tensor(DUMMY_INPUTS, dtype=tf.int32) - dummy_inputs = { - "attention_mask": tf.cast(input_ids != pad_token, tf.int32), - "input_ids": input_ids, - } - return dummy_inputs - - @tf.function( - input_signature=[ - { - "input_ids": tf.TensorSpec((None, None), tf.int32, name="input_ids"), - "attention_mask": tf.TensorSpec((None, None), tf.int32, name="attention_mask"), - } - ] - ) - def serving(self, inputs): - output = self.call(inputs) - - return self.serving_output(output) - OPT_INPUTS_DOCSTRING = r""" Args: diff --git a/src/transformers/models/pegasus/modeling_tf_pegasus.py b/src/transformers/models/pegasus/modeling_tf_pegasus.py index 976bcc3aebae..aaf3ccd584b7 100644 --- a/src/transformers/models/pegasus/modeling_tf_pegasus.py +++ b/src/transformers/models/pegasus/modeling_tf_pegasus.py @@ -41,7 +41,6 @@ ) from ...tf_utils import check_embeddings_within_bounds, shape_list, stable_softmax from ...utils import ( - DUMMY_INPUTS, ContextManagers, add_code_sample_docstrings, add_end_docstrings, @@ -504,32 +503,13 @@ class TFPegasusPreTrainedModel(TFPreTrainedModel): base_model_prefix = "model" @property - def dummy_inputs(self): - pad_token = 1 - input_ids = tf.convert_to_tensor(DUMMY_INPUTS, dtype=tf.int32) - decoder_input_ids = tf.convert_to_tensor(DUMMY_INPUTS, dtype=tf.int32) - dummy_inputs = { - "decoder_input_ids": decoder_input_ids, - "attention_mask": tf.cast(input_ids != pad_token, tf.int32), - "input_ids": input_ids, + def input_signature(self): + return { + "input_ids": tf.TensorSpec((None, None), tf.int32, name="input_ids"), + "attention_mask": tf.TensorSpec((None, None), tf.int32, name="attention_mask"), + "decoder_input_ids": tf.TensorSpec((None, None), tf.int32, name="decoder_input_ids"), + "decoder_attention_mask": tf.TensorSpec((None, None), tf.int32, name="decoder_attention_mask"), } - return dummy_inputs - - @tf.function( - input_signature=[ - { - "input_ids": tf.TensorSpec((None, None), tf.int32, name="input_ids"), - "attention_mask": tf.TensorSpec((None, None), tf.int32, name="attention_mask"), - "decoder_input_ids": tf.TensorSpec((None, None), tf.int32, name="decoder_input_ids"), - "decoder_attention_mask": tf.TensorSpec((None, None), tf.int32, name="decoder_attention_mask"), - } - ] - ) - # Copied from transformers.models.bart.modeling_tf_bart.TFBartPretrainedModel.serving - def serving(self, inputs): - output = self.call(inputs) - - return self.serving_output(output) PEGASUS_START_DOCSTRING = r""" diff --git a/src/transformers/models/regnet/modeling_tf_regnet.py b/src/transformers/models/regnet/modeling_tf_regnet.py index 2c3a1ac42e50..254d49a9f1ef 100644 --- a/src/transformers/models/regnet/modeling_tf_regnet.py +++ b/src/transformers/models/regnet/modeling_tf_regnet.py @@ -14,7 +14,7 @@ # limitations under the License. """ TensorFlow RegNet model.""" -from typing import Dict, Optional, Tuple, Union +from typing import Optional, Tuple, Union import tensorflow as tf @@ -345,33 +345,8 @@ class TFRegNetPreTrainedModel(TFPreTrainedModel): main_input_name = "pixel_values" @property - def dummy_inputs(self) -> Dict[str, tf.Tensor]: - """ - Dummy inputs to build the network. - - Returns: - `Dict[str, tf.Tensor]`: The dummy inputs. - """ - VISION_DUMMY_INPUTS = tf.random.uniform(shape=(3, self.config.num_channels, 224, 224), dtype=tf.float32) - return {"pixel_values": tf.constant(VISION_DUMMY_INPUTS)} - - @tf.function( - input_signature=[ - { - "pixel_values": tf.TensorSpec((None, None, None, None), tf.float32, name="pixel_values"), - } - ] - ) - def serving(self, inputs): - """ - Method used for serving the model. - - Args: - inputs (`Dict[str, tf.Tensor]`): - The input of the saved model as a dictionary of tensors. - """ - output = self.call(inputs) - return self.serving_output(output) + def input_signature(self): + return {"pixel_values": tf.TensorSpec(shape=(None, self.config.num_channels, 224, 224), dtype=tf.float32)} REGNET_START_DOCSTRING = r""" @@ -443,16 +418,6 @@ def call( hidden_states=outputs.hidden_states, ) - def serving_output( - self, output: TFBaseModelOutputWithPoolingAndNoAttention - ) -> TFBaseModelOutputWithPoolingAndNoAttention: - # hidden_states not converted to Tensor with tf.convert_to_tensor as they are all of different dimensions - return TFBaseModelOutputWithPoolingAndNoAttention( - last_hidden_state=output.last_hidden_state, - pooler_output=output.pooler_output, - hidden_states=output.hidden_states, - ) - @add_start_docstrings( """ @@ -514,7 +479,3 @@ def call( return ((loss,) + output) if loss is not None else output return TFSequenceClassifierOutput(loss=loss, logits=logits, hidden_states=outputs.hidden_states) - - def serving_output(self, output: TFSequenceClassifierOutput) -> TFSequenceClassifierOutput: - # hidden_states not converted to Tensor with tf.convert_to_tensor as they are all of different dimensions - return TFSequenceClassifierOutput(logits=output.logits, hidden_states=output.hidden_states) diff --git a/src/transformers/models/resnet/modeling_tf_resnet.py b/src/transformers/models/resnet/modeling_tf_resnet.py index bb6035adf2df..4ff1b119d428 100644 --- a/src/transformers/models/resnet/modeling_tf_resnet.py +++ b/src/transformers/models/resnet/modeling_tf_resnet.py @@ -14,7 +14,7 @@ # limitations under the License. """ TensorFlow ResNet model.""" -from typing import Dict, Optional, Tuple, Union +from typing import Optional, Tuple, Union import tensorflow as tf @@ -276,24 +276,8 @@ class TFResNetPreTrainedModel(TFPreTrainedModel): main_input_name = "pixel_values" @property - def dummy_inputs(self) -> Dict[str, tf.Tensor]: - """ - Dummy inputs to build the network. Returns: - `Dict[str, tf.Tensor]`: The dummy inputs. - """ - VISION_DUMMY_INPUTS = tf.random.uniform(shape=(3, self.config.num_channels, 224, 224), dtype=tf.float32) - return {"pixel_values": tf.constant(VISION_DUMMY_INPUTS)} - - @tf.function( - input_signature=[ - { - "pixel_values": tf.TensorSpec((None, None, None, None), tf.float32, name="pixel_values"), - } - ] - ) - def serving(self, inputs): - output = self.call(inputs) - return self.serving_output(output) + def input_signature(self): + return {"pixel_values": tf.TensorSpec(shape=(None, self.config.num_channels, 224, 224), dtype=tf.float32)} RESNET_START_DOCSTRING = r""" @@ -419,16 +403,6 @@ def call( ) return resnet_outputs - def serving_output( - self, output: TFBaseModelOutputWithPoolingAndNoAttention - ) -> TFBaseModelOutputWithPoolingAndNoAttention: - # hidden_states not converted to Tensor with tf.convert_to_tensor as they are all of different dimensions - return TFBaseModelOutputWithPoolingAndNoAttention( - last_hidden_state=output.last_hidden_state, - pooler_output=output.pooler_output, - hidden_states=output.hidden_states, - ) - @add_start_docstrings( """ @@ -492,7 +466,3 @@ def call( return (loss,) + output if loss is not None else output return TFImageClassifierOutputWithNoAttention(loss=loss, logits=logits, hidden_states=outputs.hidden_states) - - def serving_output(self, output: TFImageClassifierOutputWithNoAttention) -> TFImageClassifierOutputWithNoAttention: - # hidden_states not converted to Tensor with tf.convert_to_tensor as they are all of different dimensions - return TFImageClassifierOutputWithNoAttention(logits=output.logits, hidden_states=output.hidden_states) diff --git a/src/transformers/models/roformer/modeling_tf_roformer.py b/src/transformers/models/roformer/modeling_tf_roformer.py index 50b57571461d..f6067f9237f4 100644 --- a/src/transformers/models/roformer/modeling_tf_roformer.py +++ b/src/transformers/models/roformer/modeling_tf_roformer.py @@ -50,7 +50,6 @@ ) from ...tf_utils import check_embeddings_within_bounds, shape_list, stable_softmax from ...utils import ( - MULTIPLE_CHOICE_DUMMY_INPUTS, add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, @@ -835,12 +834,6 @@ def call( return outputs - def serving_output(self, output: TFBaseModelOutput) -> TFBaseModelOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFBaseModelOutput(last_hidden_state=output.last_hidden_state, hidden_states=hs, attentions=attns) - @add_start_docstrings("""RoFormer Model with a `language modeling` head on top.""", ROFORMER_START_DOCSTRING) class TFRoFormerForMaskedLM(TFRoFormerPreTrainedModel, TFMaskedLanguageModelingLoss): @@ -911,12 +904,6 @@ def call( attentions=outputs.attentions, ) - def serving_output(self, output: TFMaskedLMOutput) -> TFMaskedLMOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFMaskedLMOutput(logits=output.logits, hidden_states=hs, attentions=attns) - @add_start_docstrings( """RoFormer Model with a `language modeling` head on top for CLM fine-tuning.""", ROFORMER_START_DOCSTRING @@ -990,12 +977,6 @@ def call( attentions=outputs.attentions, ) - def serving_output(self, output: TFCausalLMOutput) -> TFCausalLMOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFCausalLMOutput(logits=output.logits, hidden_states=hs, attentions=attns) - class TFRoFormerClassificationHead(tf.keras.layers.Layer): """Head for sentence-level classification tasks.""" @@ -1094,12 +1075,6 @@ def call( attentions=outputs.attentions, ) - def serving_output(self, output: TFSequenceClassifierOutput) -> TFSequenceClassifierOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFSequenceClassifierOutput(logits=output.logits, hidden_states=hs, attentions=attns) - @add_start_docstrings( """ @@ -1118,17 +1093,6 @@ def __init__(self, config: RoFormerConfig, *inputs, **kwargs): units=1, kernel_initializer=get_initializer(config.initializer_range), name="classifier" ) - @property - def dummy_inputs(self) -> Dict[str, tf.Tensor]: - """ - Dummy inputs to build the network. - - - Returns: - tf.Tensor with dummy inputs - """ - return {"input_ids": tf.constant(MULTIPLE_CHOICE_DUMMY_INPUTS, dtype=tf.int32)} - @unpack_inputs @add_start_docstrings_to_model_forward( ROFORMER_INPUTS_DOCSTRING.format("batch_size, num_choices, sequence_length") @@ -1203,26 +1167,6 @@ def call( attentions=outputs.attentions, ) - @tf.function( - input_signature=[ - { - "input_ids": tf.TensorSpec((None, None, None), tf.int32, name="input_ids"), - "attention_mask": tf.TensorSpec((None, None, None), tf.int32, name="attention_mask"), - "token_type_ids": tf.TensorSpec((None, None, None), tf.int32, name="token_type_ids"), - } - ] - ) - def serving(self, inputs: Dict[str, tf.Tensor]) -> TFMultipleChoiceModelOutput: - output = self.call(input_ids=inputs) - - return self.serving_output(output) - - def serving_output(self, output: TFMultipleChoiceModelOutput) -> TFMultipleChoiceModelOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFMultipleChoiceModelOutput(logits=output.logits, hidden_states=hs, attentions=attns) - @add_start_docstrings( """ @@ -1294,12 +1238,6 @@ def call( attentions=outputs.attentions, ) - def serving_output(self, output: TFTokenClassifierOutput) -> TFTokenClassifierOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFTokenClassifierOutput(logits=output.logits, hidden_states=hs, attentions=attns) - @add_start_docstrings( """ @@ -1383,11 +1321,3 @@ def call( hidden_states=outputs.hidden_states, attentions=outputs.attentions, ) - - def serving_output(self, output: TFQuestionAnsweringModelOutput) -> TFQuestionAnsweringModelOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFQuestionAnsweringModelOutput( - start_logits=output.start_logits, end_logits=output.end_logits, hidden_states=hs, attentions=attns - ) diff --git a/src/transformers/models/segformer/modeling_tf_segformer.py b/src/transformers/models/segformer/modeling_tf_segformer.py index 47b7ce8e8c5c..b3090135afc2 100644 --- a/src/transformers/models/segformer/modeling_tf_segformer.py +++ b/src/transformers/models/segformer/modeling_tf_segformer.py @@ -18,7 +18,7 @@ from __future__ import annotations import math -from typing import Dict, Optional, Tuple, Union +from typing import Optional, Tuple, Union import tensorflow as tf @@ -521,34 +521,8 @@ class TFSegformerPreTrainedModel(TFPreTrainedModel): main_input_name = "pixel_values" @property - def dummy_inputs(self) -> Dict[str, tf.Tensor]: - """ - Dummy inputs to build the network. - - Returns: - `Dict[str, tf.Tensor]`: The dummy inputs. - """ - VISION_DUMMY_INPUTS = tf.random.uniform(shape=(3, self.config.num_channels, 512, 512), dtype=tf.float32) - return {"pixel_values": tf.constant(VISION_DUMMY_INPUTS)} - - @tf.function( - input_signature=[ - { - "pixel_values": tf.TensorSpec((None, None, None, None), tf.float32, name="pixel_values"), - } - ] - ) - def serving(self, inputs): - """ - Method used for serving the model. - - Args: - inputs (`Dict[str, tf.Tensor]`): - The input of the saved model as a dictionary of tensors. - """ - output = self.call(inputs) - - return self.serving_output(output) + def input_signature(self): + return {"pixel_values": tf.TensorSpec(shape=(None, self.config.num_channels, 512, 512), dtype=tf.float32)} SEGFORMER_START_DOCSTRING = r""" @@ -631,14 +605,6 @@ def call( ) return outputs - def serving_output(self, output: TFBaseModelOutput) -> TFBaseModelOutput: - # hidden_states and attention not converted to Tensor with tf.convert_to_tensor as they are all of different dimensions - return TFBaseModelOutput( - last_hidden_state=output.last_hidden_state, - hidden_states=output.hidden_states, - attentions=output.attentions, - ) - @add_start_docstrings( """ @@ -702,12 +668,6 @@ def call( loss=loss, logits=logits, hidden_states=outputs.hidden_states, attentions=outputs.attentions ) - def serving_output(self, output: TFSequenceClassifierOutput) -> TFSequenceClassifierOutput: - # hidden_states and attention not converted to Tensor with tf.convert_to_tensor as they are all of different dimensions - return TFSequenceClassifierOutput( - logits=output.logits, hidden_states=output.hidden_states, attentions=output.attentions - ) - class TFSegformerMLP(tf.keras.layers.Layer): """ @@ -892,9 +852,3 @@ def call( hidden_states=outputs.hidden_states if output_hidden_states else None, attentions=outputs.attentions, ) - - def serving_output(self, output: TFSemanticSegmenterOutput) -> TFSemanticSegmenterOutput: - # hidden_states and attention not converted to Tensor with tf.convert_to_tensor as they are all of different dimensions - return TFSemanticSegmenterOutput( - logits=output.logits, hidden_states=output.hidden_states, attentions=output.attentions - ) diff --git a/src/transformers/models/speech_to_text/modeling_tf_speech_to_text.py b/src/transformers/models/speech_to_text/modeling_tf_speech_to_text.py index 3651506894c7..59caabffab9c 100755 --- a/src/transformers/models/speech_to_text/modeling_tf_speech_to_text.py +++ b/src/transformers/models/speech_to_text/modeling_tf_speech_to_text.py @@ -18,7 +18,7 @@ from __future__ import annotations import random -from typing import Dict, Optional, Tuple, Union +from typing import Optional, Tuple, Union import numpy as np import tensorflow as tf @@ -563,26 +563,6 @@ class TFSpeech2TextPreTrainedModel(TFPreTrainedModel): base_model_prefix = "model" main_input_name = "input_features" - # Overwritten property due to different expected input shape and type - @property - def dummy_inputs(self) -> Dict[str, tf.Tensor]: - """ - Dummy inputs to build the network. - - Returns: - `Dict[str, tf.Tensor]`: The dummy inputs. - """ - return { - self.main_input_name: tf.random.uniform( - [ - 1, - random.randint(1, self.config.max_source_positions), # time - self.config.input_feat_per_channel * self.config.input_channels, # input channels - ] - ), - "decoder_input_ids": tf.constant([[2, 3]], dtype=tf.int32), - } - def _get_feat_extract_output_lengths(self, input_lengths: tf.Tensor): """ Computes the output length of the convolutional layers @@ -592,20 +572,18 @@ def _get_feat_extract_output_lengths(self, input_lengths: tf.Tensor): return input_lengths - @tf.function( - input_signature=[ - { - "input_features": tf.TensorSpec((None, None, None), tf.float32, name="input_features"), - "attention_mask": tf.TensorSpec((None, None), tf.int32, name="attention_mask"), - "decoder_input_ids": tf.TensorSpec((None, None), tf.int32, name="decoder_input_ids"), - "decoder_attention_mask": tf.TensorSpec((None, None), tf.int32, name="decoder_attention_mask"), - } - ] - ) - def serving(self, inputs): - output = self.call(inputs) - - return self.serving_output(output) + @property + def input_signature(self): + return { + "input_features": tf.TensorSpec( + (None, None, self.config.input_feat_per_channel * self.config.input_channels), + tf.float32, + name="input_features", + ), + "attention_mask": tf.TensorSpec((None, None), tf.int32, name="attention_mask"), + "decoder_input_ids": tf.TensorSpec((None, None), tf.int32, name="decoder_input_ids"), + "decoder_attention_mask": tf.TensorSpec((None, None), tf.int32, name="decoder_attention_mask"), + } SPEECH_TO_TEXT_START_DOCSTRING = r""" diff --git a/src/transformers/models/swin/modeling_tf_swin.py b/src/transformers/models/swin/modeling_tf_swin.py index f75bf230c0ad..02ec39edb0fe 100644 --- a/src/transformers/models/swin/modeling_tf_swin.py +++ b/src/transformers/models/swin/modeling_tf_swin.py @@ -957,29 +957,6 @@ def _set_gradient_checkpointing(self, module, value=False) -> None: if isinstance(module, TFSwinEncoder): module.gradient_checkpointing = value - @property - def dummy_inputs(self) -> Dict[str, tf.Tensor]: - """ - Dummy inputs to build the network. Returns: - `Dict[str, tf.Tensor]`: The dummy inputs. - """ - VISION_DUMMY_INPUTS = tf.random.uniform( - shape=(3, self.config.num_channels, self.config.image_size, self.config.image_size), - dtype=tf.float32, - ) - return {"pixel_values": tf.constant(VISION_DUMMY_INPUTS)} - - @tf.function( - input_signature=[ - { - "pixel_values": tf.TensorSpec((None, None, None, None), tf.float32, name="pixel_values"), - } - ] - ) - def serving(self, inputs): - output = self.call(inputs) - return self.serving_output(output) - SWIN_START_DOCSTRING = r""" This model is a Tensorflow @@ -1245,16 +1222,6 @@ def call( return swin_outputs - def serving_output(self, output: TFSwinModelOutput) -> TFSwinModelOutput: - # hidden_states and attentions not converted to Tensor with tf.convert_to_tensor as they are all of different dimensions - return TFSwinModelOutput( - last_hidden_state=output.last_hidden_state, - pooler_output=output.pooler_output, - hidden_states=output.hidden_states, - attentions=output.attentions, - reshaped_hidden_states=output.reshaped_hidden_states, - ) - class TFSwinPixelShuffle(tf.keras.layers.Layer): """TF layer implementation of torch.nn.PixelShuffle""" @@ -1410,15 +1377,6 @@ def call( reshaped_hidden_states=outputs.reshaped_hidden_states, ) - def serving_output(self, output: TFSwinMaskedImageModelingOutput) -> TFSwinMaskedImageModelingOutput: - # hidden_states and attentions not converted to Tensor with tf.convert_to_tensor as they are all of different dimensions - return TFSwinMaskedImageModelingOutput( - reconstruction=output.reconstruction, - hidden_states=output.hidden_states, - attentions=output.attentions, - reshaped_hidden_states=output.reshaped_hidden_states, - ) - @add_start_docstrings( """ @@ -1493,12 +1451,3 @@ def call( attentions=outputs.attentions, reshaped_hidden_states=outputs.reshaped_hidden_states, ) - - def serving_output(self, output: TFSwinImageClassifierOutput) -> TFSwinImageClassifierOutput: - # hidden_states and attentions not converted to Tensor with tf.convert_to_tensor as they are all of different dimensions - return TFSwinImageClassifierOutput( - logits=output.logits, - hidden_states=output.hidden_states, - attentions=output.attentions, - reshaped_hidden_states=output.reshaped_hidden_states, - ) diff --git a/src/transformers/models/tapas/modeling_tf_tapas.py b/src/transformers/models/tapas/modeling_tf_tapas.py index b17fddc32720..62e77a6678de 100644 --- a/src/transformers/models/tapas/modeling_tf_tapas.py +++ b/src/transformers/models/tapas/modeling_tf_tapas.py @@ -862,18 +862,13 @@ class TFTapasPreTrainedModel(TFPreTrainedModel): config_class = TapasConfig base_model_prefix = "tapas" - @tf.function( - input_signature=[ - { - "input_ids": tf.TensorSpec((None, None), tf.int32, name="input_ids"), - "attention_mask": tf.TensorSpec((None, None), tf.float32, name="attention_mask"), - "token_type_ids": tf.TensorSpec((None, None, None), tf.int32, name="token_type_ids"), - } - ] - ) - def serving(self, inputs): - output = self.call(inputs) - return self.serving_output(output) + @property + def input_signature(self): + return { + "input_ids": tf.TensorSpec((None, None), tf.int32, name="input_ids"), + "attention_mask": tf.TensorSpec((None, None), tf.float32, name="attention_mask"), + "token_type_ids": tf.TensorSpec((None, None, 7), tf.int32, name="token_type_ids"), + } TAPAS_START_DOCSTRING = r""" @@ -1038,17 +1033,6 @@ def call( return outputs - def serving_output(self, output: TFBaseModelOutputWithPooling) -> TFBaseModelOutputWithPooling: - hidden_states = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attentions = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFBaseModelOutputWithPooling( - last_hidden_state=output.last_hidden_state, - pooler_output=output.pooler_output, - hidden_states=hidden_states, - attentions=attentions, - ) - @add_start_docstrings("""Tapas Model with a `language modeling` head on top.""", TAPAS_START_DOCSTRING) class TFTapasForMaskedLM(TFTapasPreTrainedModel, TFMaskedLanguageModelingLoss): @@ -1145,12 +1129,6 @@ def call( attentions=outputs.attentions, ) - def serving_output(self, output: TFMaskedLMOutput) -> TFMaskedLMOutput: - hidden_states = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attentions = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFMaskedLMOutput(logits=output.logits, hidden_states=hidden_states, attentions=attentions) - class TFTapasComputeTokenLogits(tf.keras.layers.Layer): def __init__(self, config: TapasConfig, **kwargs): @@ -1574,17 +1552,6 @@ def call( attentions=outputs.attentions, ) - def serving_output(self, output: TFTableQuestionAnsweringOutput) -> TFTableQuestionAnsweringOutput: - hidden_states = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attentions = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFTableQuestionAnsweringOutput( - logits=output.logits, - logits_aggregation=output.logits_aggregation, - hidden_states=hidden_states, - attentions=attentions, - ) - @add_start_docstrings( """ @@ -1687,12 +1654,6 @@ def call( attentions=outputs.attentions, ) - def serving_output(self, output: TFSequenceClassifierOutput) -> TFSequenceClassifierOutput: - hidden_states = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attentions = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFSequenceClassifierOutput(logits=output.logits, hidden_states=hidden_states, attentions=attentions) - """ TAPAS utilities.""" diff --git a/src/transformers/models/transfo_xl/modeling_tf_transfo_xl.py b/src/transformers/models/transfo_xl/modeling_tf_transfo_xl.py index decf18b8a7a0..2ef67426f87c 100644 --- a/src/transformers/models/transfo_xl/modeling_tf_transfo_xl.py +++ b/src/transformers/models/transfo_xl/modeling_tf_transfo_xl.py @@ -684,18 +684,6 @@ class TFTransfoXLPreTrainedModel(TFPreTrainedModel): config_class = TransfoXLConfig base_model_prefix = "transformer" - @tf.function( - input_signature=[ - { - "input_ids": tf.TensorSpec((None, None), tf.int32, name="input_ids"), - } - ] - ) - def serving(self, inputs): - output = self.call(inputs) - - return self.serving_output(output) - @dataclass class TFTransfoXLModelOutput(ModelOutput): @@ -916,17 +904,6 @@ def call( return outputs - def serving_output(self, output): - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFTransfoXLModelOutput( - last_hidden_state=output.last_hidden_state, - mems=tf.convert_to_tensor(output.mems), - hidden_states=hs, - attentions=attns, - ) - @add_start_docstrings( """ @@ -1015,17 +992,6 @@ def call( attentions=transformer_outputs.attentions, ) - def serving_output(self, output): - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFTransfoXLLMHeadModelOutput( - prediction_scores=output.prediction_scores, - mems=tf.convert_to_tensor(output.mems), - hidden_states=hs, - attentions=attns, - ) - def prepare_inputs_for_generation(self, input_ids, past_key_values=None, **model_kwargs): inputs = {} @@ -1157,11 +1123,3 @@ def call( hidden_states=transformer_outputs.hidden_states, attentions=transformer_outputs.attentions, ) - - def serving_output(self, output): - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFTransfoXLSequenceClassifierOutputWithPast( - logits=output.logits, mems=tf.convert_to_tensor(output.mems), hidden_states=hs, attentions=attns - ) diff --git a/src/transformers/models/vision_encoder_decoder/modeling_tf_vision_encoder_decoder.py b/src/transformers/models/vision_encoder_decoder/modeling_tf_vision_encoder_decoder.py index ad39a0ae82ba..eac67b4172bb 100644 --- a/src/transformers/models/vision_encoder_decoder/modeling_tf_vision_encoder_decoder.py +++ b/src/transformers/models/vision_encoder_decoder/modeling_tf_vision_encoder_decoder.py @@ -29,7 +29,6 @@ from ...modeling_tf_utils import TFCausalLanguageModelingLoss, TFPreTrainedModel, get_initializer, unpack_inputs from ...tf_utils import shape_list from ...utils import ( - DUMMY_INPUTS, ModelOutput, add_start_docstrings, add_start_docstrings_to_model_forward, @@ -254,29 +253,19 @@ def __init__( ) @property - def dummy_inputs(self): - """ - Dummy inputs to build the network. - - Returns: - `Dict[str, tf.Tensor]`: The dummy inputs. - """ - decoder_input_ids = tf.constant(DUMMY_INPUTS, dtype=tf.int32) - batch_size, seq_len = decoder_input_ids.shape - - VISION_DUMMY_INPUTS = tf.random.uniform( - shape=( - batch_size, - self.config.encoder.num_channels, - self.config.encoder.image_size, - self.config.encoder.image_size, + def input_signature(self): + return { + "pixel_values": tf.TensorSpec( + shape=( + None, + self.config.encoder.num_channels, + self.config.encoder.input_size, + self.config.encoder.input_size, + ), + dtype=tf.float32, ), - dtype=tf.float32, - ) - pixel_values = tf.constant(VISION_DUMMY_INPUTS) - # Add `decoder_input_ids` because `self.decoder` requires it. - dummy = {"pixel_values": pixel_values, "decoder_input_ids": decoder_input_ids} - return dummy + "decoder_input_ids": tf.TensorSpec(shape=(None, None), dtype=tf.int32, name="decoder_input_ids"), + } def get_encoder(self): return self.encoder diff --git a/src/transformers/models/vit/modeling_tf_vit.py b/src/transformers/models/vit/modeling_tf_vit.py index 6a07719c916c..727db8dfc6c0 100644 --- a/src/transformers/models/vit/modeling_tf_vit.py +++ b/src/transformers/models/vit/modeling_tf_vit.py @@ -19,7 +19,7 @@ import collections.abc import math -from typing import Dict, Optional, Tuple, Union +from typing import Optional, Tuple, Union import numpy as np import tensorflow as tf @@ -550,38 +550,6 @@ class TFViTPreTrainedModel(TFPreTrainedModel): base_model_prefix = "vit" main_input_name = "pixel_values" - @property - def dummy_inputs(self) -> Dict[str, tf.Tensor]: - """ - Dummy inputs to build the network. - - Returns: - `Dict[str, tf.Tensor]`: The dummy inputs. - """ - VISION_DUMMY_INPUTS = tf.random.uniform( - shape=(3, self.config.num_channels, self.config.image_size, self.config.image_size), dtype=tf.float32 - ) - return {"pixel_values": tf.constant(VISION_DUMMY_INPUTS)} - - @tf.function( - input_signature=[ - { - "pixel_values": tf.TensorSpec((None, None, None, None), tf.float32, name="pixel_values"), - } - ] - ) - def serving(self, inputs): - """ - Method used for serving the model. - - Args: - inputs (`Dict[str, tf.Tensor]`): - The input of the saved model as a dictionary of tensors. - """ - output = self.call(inputs) - - return self.serving_output(output) - VIT_START_DOCSTRING = r""" @@ -697,17 +665,6 @@ def call( return outputs - def serving_output(self, output: TFBaseModelOutputWithPooling) -> TFBaseModelOutputWithPooling: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFBaseModelOutputWithPooling( - last_hidden_state=output.last_hidden_state, - pooler_output=output.pooler_output, - hidden_states=hs, - attentions=attns, - ) - class TFViTPooler(tf.keras.layers.Layer): def __init__(self, config: ViTConfig, **kwargs): @@ -807,9 +764,3 @@ def call( hidden_states=outputs.hidden_states, attentions=outputs.attentions, ) - - def serving_output(self, output: TFSequenceClassifierOutput) -> TFSequenceClassifierOutput: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFSequenceClassifierOutput(logits=output.logits, hidden_states=hs, attentions=attns) diff --git a/src/transformers/models/vit_mae/modeling_tf_vit_mae.py b/src/transformers/models/vit_mae/modeling_tf_vit_mae.py index 5f5c1a6830d6..e7d7770bcf26 100644 --- a/src/transformers/models/vit_mae/modeling_tf_vit_mae.py +++ b/src/transformers/models/vit_mae/modeling_tf_vit_mae.py @@ -21,7 +21,7 @@ import math from copy import deepcopy from dataclasses import dataclass -from typing import Dict, Optional, Tuple, Union +from typing import Optional, Tuple, Union import numpy as np import tensorflow as tf @@ -698,36 +698,6 @@ class TFViTMAEPreTrainedModel(TFPreTrainedModel): base_model_prefix = "vit" main_input_name = "pixel_values" - @property - def dummy_inputs(self) -> Dict[str, tf.Tensor]: - """ - Dummy inputs to build the network. Returns: - `Dict[str, tf.Tensor]`: The dummy inputs. - """ - VISION_DUMMY_INPUTS = tf.random.uniform( - shape=(3, self.config.num_channels, self.config.image_size, self.config.image_size), - dtype=tf.float32, - ) - return {"pixel_values": tf.constant(VISION_DUMMY_INPUTS)} - - @tf.function( - input_signature=[ - { - "pixel_values": tf.TensorSpec((None, None, None, None), tf.float32, name="pixel_values"), - } - ] - ) - def serving(self, inputs): - """ - Method used for serving the model. - - Args: - inputs (`Dict[str, tf.Tensor]`): - The input of the saved model as a dictionary of tensors. - """ - output = self.call(inputs) - return self.serving_output(output) - VIT_MAE_START_DOCSTRING = r""" This model inherits from [`TFPreTrainedModel`]. Check the superclass documentation for the generic methods the @@ -859,18 +829,6 @@ def call( return outputs - def serving_output(self, output: TFViTMAEModelOutput) -> TFViTMAEModelOutput: - hidden_states = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attentions = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFViTMAEModelOutput( - last_hidden_state=output.last_hidden_state, - mask=output.mask, - ids_restore=output.ids_restore, - hidden_states=hidden_states, - attentions=attentions, - ) - class TFViTMAEDecoder(tf.keras.layers.Layer): def __init__(self, config, num_patches, **kwargs): @@ -1173,15 +1131,3 @@ def call( hidden_states=outputs.hidden_states, attentions=outputs.attentions, ) - - def serving_output(self, output: TFViTMAEForPreTrainingOutput) -> TFViTMAEForPreTrainingOutput: - hidden_states = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attentions = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFViTMAEForPreTrainingOutput( - logits=output.logits, - mask=output.mask, - ids_restore=output.ids_restore, - hidden_states=hidden_states, - attentions=attentions, - ) diff --git a/src/transformers/models/wav2vec2/modeling_tf_wav2vec2.py b/src/transformers/models/wav2vec2/modeling_tf_wav2vec2.py index 3ee16127b323..ef90908b0c5c 100644 --- a/src/transformers/models/wav2vec2/modeling_tf_wav2vec2.py +++ b/src/transformers/models/wav2vec2/modeling_tf_wav2vec2.py @@ -19,7 +19,7 @@ import warnings from dataclasses import dataclass -from typing import Any, Dict, Optional, Tuple, Union +from typing import Any, Optional, Tuple, Union import numpy as np import tensorflow as tf @@ -1185,14 +1185,11 @@ class TFWav2Vec2PreTrainedModel(TFPreTrainedModel): main_input_name = "input_values" @property - def dummy_inputs(self) -> Dict[str, tf.Tensor]: - pad_token = 0.0 - input_values = tf.convert_to_tensor(np.random.rand(1, 16000), tf.float32) - dummy_inputs = { - "input_values": input_values, - "attention_mask": tf.cast(tf.not_equal(input_values, pad_token), tf.float32), + def input_signature(self): + return { + "input_values": tf.TensorSpec((None, None), tf.float32, name="input_values"), + "attention_mask": tf.TensorSpec((None, None), tf.float32, name="attention_mask"), } - return dummy_inputs def __init__(self, config, *inputs, **kwargs): super().__init__(config, *inputs, **kwargs) @@ -1201,20 +1198,6 @@ def __init__(self, config, *inputs, **kwargs): "to train/fine-tine this model, you need a GPU or a TPU" ) - @tf.function( - input_signature=[ - { - "input_values": tf.TensorSpec((None, None), tf.float32, name="input_values"), - "attention_mask": tf.TensorSpec((None, None), tf.int32, name="attention_mask"), - "token_type_ids": tf.TensorSpec((None, None), tf.int32, name="token_type_ids"), - } - ] - ) - def serving(self, inputs): - output = self.call(input_values=inputs, training=False) - - return self.serving_output(output) - def _get_feat_extract_output_lengths(self, input_lengths, add_adapter=None): """ Computes the output length of the convolutional layers @@ -1427,17 +1410,6 @@ def call( return outputs - def serving_output(self, output): - hidden_states = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attentions = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFWav2Vec2BaseModelOutput( - last_hidden_state=output.last_hidden_state, - extract_features=output.extract_features, - hidden_states=hidden_states, - attentions=attentions, - ) - @add_start_docstrings( """TFWav2Vec2 Model with a `language modeling` head on top for Connectionist Temporal Classification (CTC).""", @@ -1591,11 +1563,6 @@ def call( attentions=outputs.attentions, ) - def serving_output(self, output: TFCausalLMOutput) -> TFCausalLMOutput: - hidden_states = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attentions = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - return TFCausalLMOutput(logits=output.logits, hidden_states=hidden_states, attentions=attentions) - class TFWav2Vec2ForSequenceClassification(TFWav2Vec2PreTrainedModel): def __init__(self, config): @@ -1693,27 +1660,3 @@ def call( hidden_states=outputs.hidden_states, attentions=outputs.attentions, ) - - def serving_output(self, output): - hidden_states = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attentions = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFSequenceClassifierOutput( - logits=output.logits, - hidden_states=hidden_states, - attentions=attentions, - ) - - @tf.function( - input_signature=[ - { - "input_values": tf.TensorSpec((None, None), tf.float32, name="input_values"), - "attention_mask": tf.TensorSpec((None, None), tf.int32, name="attention_mask"), - "token_type_ids": tf.TensorSpec((None, None), tf.int32, name="token_type_ids"), - } - ] - ) - def serving(self, inputs): - output = self.call(input_values=inputs) - - return self.serving_output(output) diff --git a/src/transformers/models/whisper/modeling_tf_whisper.py b/src/transformers/models/whisper/modeling_tf_whisper.py index 11168df3f9ca..b8cd87f67ef0 100644 --- a/src/transformers/models/whisper/modeling_tf_whisper.py +++ b/src/transformers/models/whisper/modeling_tf_whisper.py @@ -486,18 +486,13 @@ def dummy_inputs(self) -> Dict[str, tf.Tensor]: "decoder_input_ids": tf.constant([[2, 3]], dtype=tf.int32), } - @tf.function( - input_signature=[ - { - "input_features": tf.TensorSpec((None, None, None), tf.float32, name="input_features"), - "decoder_input_ids": tf.TensorSpec((None, None), tf.int32, name="decoder_input_ids"), - "decoder_attention_mask": tf.TensorSpec((None, None), tf.int32, name="decoder_attention_mask"), - } - ] - ) - def serving(self, inputs): - output = self.call(inputs) - return self.serving_output(output) + @property + def input_signature(self): + return { + "input_features": tf.TensorSpec((None, self.config.num_mel_bins, None), tf.float32, name="input_features"), + "decoder_input_ids": tf.TensorSpec((None, None), tf.int32, name="decoder_input_ids"), + "decoder_attention_mask": tf.TensorSpec((None, None), tf.int32, name="decoder_attention_mask"), + } WHISPER_START_DOCSTRING = r""" diff --git a/src/transformers/models/xglm/modeling_tf_xglm.py b/src/transformers/models/xglm/modeling_tf_xglm.py index 236720ae49df..217a02b53f9b 100644 --- a/src/transformers/models/xglm/modeling_tf_xglm.py +++ b/src/transformers/models/xglm/modeling_tf_xglm.py @@ -28,7 +28,6 @@ # Public API from ...file_utils import ( - DUMMY_INPUTS, add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, @@ -621,27 +620,13 @@ class TFXGLMPreTrainedModel(TFPreTrainedModel): base_model_prefix = "model" @property - def dummy_inputs(self): - pad_token = 1 - input_ids = tf.cast(tf.convert_to_tensor(DUMMY_INPUTS), tf.int32) - dummy_inputs = { - "input_ids": input_ids, - "attention_mask": tf.cast(input_ids != pad_token, tf.int32), + def input_signature(self): + return { + "input_ids": tf.TensorSpec((None, None), tf.int32, name="input_ids"), + "attention_mask": tf.TensorSpec((None, None), tf.int32, name="attention_mask"), + "decoder_input_ids": tf.TensorSpec((None, None), tf.int32, name="decoder_input_ids"), + "decoder_attention_mask": tf.TensorSpec((None, None), tf.int32, name="decoder_attention_mask"), } - return dummy_inputs - - @tf.function( - input_signature=[ - { - "input_ids": tf.TensorSpec((None, None), tf.int32, name="input_ids"), - "attention_mask": tf.TensorSpec((None, None), tf.int32, name="attention_mask"), - } - ] - ) - def serving(self, inputs): - output = self.call(inputs) - - return self.serving_output(output) XGLM_START_DOCSTRING = r""" @@ -821,24 +806,6 @@ def call( return outputs - def serving_output(self, output): - pkv = tf.convert_to_tensor(output.past_key_values) if self.config.use_cache else None - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - cross_attns = ( - tf.convert_to_tensor(output.cross_attentions) - if self.config.output_attentions and self.config.add_cross_attention - else None - ) - - return TFBaseModelOutputWithPastAndCrossAttentions( - last_hidden_state=output.hidden_states, - past_key_values=pkv, - hidden_states=hs, - attentions=attns, - cross_attentions=cross_attns, - ) - @add_start_docstrings( """ @@ -971,22 +938,3 @@ def call( attentions=outputs.attentions, cross_attentions=outputs.cross_attentions, ) - - def serving_output(self, output): - pkv = tf.convert_to_tensor(output.past_key_values) if self.config.use_cache else None - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - cross_attns = ( - tf.convert_to_tensor(output.cross_attentions) - if self.config.output_attentions and self.config.add_cross_attention - else None - ) - - return TFCausalLMOutputWithCrossAttentions( - loss=output.loss, - logits=output.logits, - past_key_values=pkv, - hidden_states=hs, - attentions=attns, - cross_attentions=cross_attns, - ) diff --git a/src/transformers/models/xlnet/modeling_tf_xlnet.py b/src/transformers/models/xlnet/modeling_tf_xlnet.py index 1d8a6692c090..c5f3805ec987 100644 --- a/src/transformers/models/xlnet/modeling_tf_xlnet.py +++ b/src/transformers/models/xlnet/modeling_tf_xlnet.py @@ -44,7 +44,6 @@ ) from ...tf_utils import check_embeddings_within_bounds, shape_list, stable_softmax from ...utils import ( - MULTIPLE_CHOICE_DUMMY_INPUTS, ModelOutput, add_code_sample_docstrings, add_start_docstrings, @@ -1177,15 +1176,6 @@ def call( return outputs - def serving_output(self, output): - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - mems = tf.convert_to_tensor(output.mems) if output.mems is not None else None - - return TFXLNetModelOutput( - last_hidden_state=output.last_hidden_state, mems=mems, hidden_states=hs, attentions=attns - ) - @add_start_docstrings( """ @@ -1345,13 +1335,6 @@ def call( attentions=transformer_outputs.attentions, ) - def serving_output(self, output): - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - mems = tf.convert_to_tensor(output.mems) if output.mems is not None else None - - return TFXLNetLMHeadModelOutput(logits=output.logits, mems=mems, hidden_states=hs, attentions=attns) - @add_start_docstrings( """ @@ -1439,15 +1422,6 @@ def call( attentions=transformer_outputs.attentions, ) - def serving_output(self, output): - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - mems = tf.convert_to_tensor(output.mems) if output.mems is not None else None - - return TFXLNetForSequenceClassificationOutput( - logits=output.logits, mems=mems, hidden_states=hs, attentions=attns - ) - @add_start_docstrings( """ @@ -1468,16 +1442,6 @@ def __init__(self, config, *inputs, **kwargs): 1, kernel_initializer=get_initializer(config.initializer_range), name="logits_proj" ) - @property - def dummy_inputs(self): - """ - Dummy inputs to build the network. - - Returns: - tf.Tensor with dummy inputs - """ - return {"input_ids": tf.constant(MULTIPLE_CHOICE_DUMMY_INPUTS, dtype=tf.int32)} - @unpack_inputs @add_start_docstrings_to_model_forward(XLNET_INPUTS_DOCSTRING.format("batch_size, num_choices, sequence_length")) @add_code_sample_docstrings( @@ -1559,27 +1523,6 @@ def call( attentions=transformer_outputs.attentions, ) - @tf.function( - input_signature=[ - { - "input_ids": tf.TensorSpec((None, None, None), tf.int32, name="input_ids"), - "attention_mask": tf.TensorSpec((None, None, None), tf.int32, name="attention_mask"), - "token_type_ids": tf.TensorSpec((None, None, None), tf.int32, name="token_type_ids"), - } - ] - ) - def serving(self, inputs): - output = self.call(inputs) - - return self.serving_output(output) - - def serving_output(self, output): - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - mems = tf.convert_to_tensor(output.mems) if output.mems is not None else None - - return TFXLNetForMultipleChoiceOutput(logits=output.logits, mems=mems, hidden_states=hs, attentions=attns) - @add_start_docstrings( """ @@ -1660,13 +1603,6 @@ def call( attentions=transformer_outputs.attentions, ) - def serving_output(self, output): - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - mems = tf.convert_to_tensor(output.mems) if output.mems is not None else None - - return TFXLNetForTokenClassificationOutput(logits=output.logits, mems=mems, hidden_states=hs, attentions=attns) - @add_start_docstrings( """ @@ -1760,16 +1696,3 @@ def call( hidden_states=transformer_outputs.hidden_states, attentions=transformer_outputs.attentions, ) - - def serving_output(self, output): - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - mems = tf.convert_to_tensor(output.mems) if output.mems is not None else None - - return TFXLNetForQuestionAnsweringSimpleOutput( - start_logits=output.start_logits, - end_logits=output.end_logits, - mems=mems, - hidden_states=hs, - attentions=attns, - ) From 2cded64f057d91fe2469376e995762f637296e79 Mon Sep 17 00:00:00 2001 From: Matt Date: Fri, 12 May 2023 17:43:17 +0100 Subject: [PATCH 26/49] Fix for token_type_ids with vocab_size 1 --- src/transformers/modeling_tf_utils.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/transformers/modeling_tf_utils.py b/src/transformers/modeling_tf_utils.py index 14bb8818cb8a..0d46c36897c4 100644 --- a/src/transformers/modeling_tf_utils.py +++ b/src/transformers/modeling_tf_utils.py @@ -1117,6 +1117,9 @@ def dummy_inputs(self) -> Dict[str, tf.Tensor]: for key, spec in self.input_signature.items(): # 2 is the most correct arbitrary size. I will not be taking questions dummies[key] = tf.ones(shape=[dim if dim is not None else 2 for dim in spec.shape], dtype=spec.dtype) + if key == "token_type_ids": + # Some models have token_type_ids but with a vocab_size of 1 + dummies[key] = tf.zeros_like(dummies[key]) if self.config.add_cross_attention and "encoder_hidden_states" in inspect.signature(self.call).parameters: if "encoder_hidden_states" not in dummies: if self.main_input_name == "input_ids": From ddea02eb679d80fd0d65cc76e9c0616c864c1cab Mon Sep 17 00:00:00 2001 From: Matt Date: Fri, 12 May 2023 18:20:12 +0100 Subject: [PATCH 27/49] Add missing property decorator --- src/transformers/models/blenderbot/modeling_tf_blenderbot.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/transformers/models/blenderbot/modeling_tf_blenderbot.py b/src/transformers/models/blenderbot/modeling_tf_blenderbot.py index cd31b151dc21..893d97d99ab4 100644 --- a/src/transformers/models/blenderbot/modeling_tf_blenderbot.py +++ b/src/transformers/models/blenderbot/modeling_tf_blenderbot.py @@ -463,6 +463,7 @@ class TFBlenderbotPreTrainedModel(TFPreTrainedModel): config_class = BlenderbotConfig base_model_prefix = "model" + @property def input_signature(self): return { "input_ids": tf.TensorSpec((None, None), tf.int32, name="input_ids"), From 26f4f67e2201c21223ff34f80785abf3e42f97d7 Mon Sep 17 00:00:00 2001 From: Matt Date: Mon, 15 May 2023 18:37:46 +0100 Subject: [PATCH 28/49] Fix T5 and hopefully some models that take conv inputs --- src/transformers/modeling_tf_utils.py | 9 +++++++-- src/transformers/models/t5/modeling_tf_t5.py | 14 ++++++++------ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/transformers/modeling_tf_utils.py b/src/transformers/modeling_tf_utils.py index 0d46c36897c4..95401df3bd21 100644 --- a/src/transformers/modeling_tf_utils.py +++ b/src/transformers/modeling_tf_utils.py @@ -1115,8 +1115,8 @@ def dummy_inputs(self) -> Dict[str, tf.Tensor]: """ dummies = {} for key, spec in self.input_signature.items(): - # 2 is the most correct arbitrary size. I will not be taking questions - dummies[key] = tf.ones(shape=[dim if dim is not None else 2 for dim in spec.shape], dtype=spec.dtype) + # 3 is the most correct arbitrary size. I will not be taking questions + dummies[key] = tf.ones(shape=[dim if dim is not None else 3 for dim in spec.shape], dtype=spec.dtype) if key == "token_type_ids": # Some models have token_type_ids but with a vocab_size of 1 dummies[key] = tf.zeros_like(dummies[key]) @@ -1259,6 +1259,11 @@ def input_signature(self) -> Dict[str, tf.TensorSpec]: raise NotImplementedError("Audio models need a manually defined input_signature") return sig + def _prune_signature(self, signature): + """Keeps only the keys of a given input signature that are valid for this model.""" + model_inputs = list(dict(inspect.signature(self.call).parameters).keys()) + return {key: val for key, val in signature.items() if key in model_inputs} + def serving_output(self, output): """ Prepare the output of the saved model. Can be overridden if specific serving modifications are required. diff --git a/src/transformers/models/t5/modeling_tf_t5.py b/src/transformers/models/t5/modeling_tf_t5.py index c1c44b2c2ccd..18bf09ba7514 100644 --- a/src/transformers/models/t5/modeling_tf_t5.py +++ b/src/transformers/models/t5/modeling_tf_t5.py @@ -867,12 +867,14 @@ class TFT5PreTrainedModel(TFPreTrainedModel): @property def input_signature(self): - return { - "input_ids": tf.TensorSpec((None, None), tf.int32, name="input_ids"), - "attention_mask": tf.TensorSpec((None, None), tf.int32, name="attention_mask"), - "decoder_input_ids": tf.TensorSpec((None, None), tf.int32, name="decoder_input_ids"), - "decoder_attention_mask": tf.TensorSpec((None, None), tf.int32, name="decoder_attention_mask"), - } + return self._prune_signature( + { + "input_ids": tf.TensorSpec((None, None), tf.int32, name="input_ids"), + "attention_mask": tf.TensorSpec((None, None), tf.int32, name="attention_mask"), + "decoder_input_ids": tf.TensorSpec((None, None), tf.int32, name="decoder_input_ids"), + "decoder_attention_mask": tf.TensorSpec((None, None), tf.int32, name="decoder_attention_mask"), + } + ) def get_input_embeddings(self): return self.shared From f61f6d389c27b4c2678195a35675c12b1670b911 Mon Sep 17 00:00:00 2001 From: Matt Date: Mon, 15 May 2023 18:55:43 +0100 Subject: [PATCH 29/49] More signature pruning --- src/transformers/modeling_tf_utils.py | 7 +++++-- src/transformers/models/t5/modeling_tf_t5.py | 14 ++++++-------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/transformers/modeling_tf_utils.py b/src/transformers/modeling_tf_utils.py index 95401df3bd21..6ac439f36840 100644 --- a/src/transformers/modeling_tf_utils.py +++ b/src/transformers/modeling_tf_utils.py @@ -1114,7 +1114,8 @@ def dummy_inputs(self) -> Dict[str, tf.Tensor]: `Dict[str, tf.Tensor]`: The dummy inputs. """ dummies = {} - for key, spec in self.input_signature.items(): + sig = self._prune_signature(self.input_signature) + for key, spec in sig.items(): # 3 is the most correct arbitrary size. I will not be taking questions dummies[key] = tf.ones(shape=[dim if dim is not None else 3 for dim in spec.shape], dtype=spec.dtype) if key == "token_type_ids": @@ -1152,7 +1153,9 @@ def __init__(self, config, *inputs, **kwargs): self.name_or_path = config.name_or_path self.generation_config = GenerationConfig.from_model_config(config) if self.can_generate() else None if not hasattr(self, "serving"): # Don't overwrite existing serving signatures - self.serving = tf.function(self.eager_serving, input_signature=[self.input_signature]) + self.serving = tf.function( + self.eager_serving, input_signature=[self._prune_signature(self.input_signature)] + ) # Set the serving spec quickly to ensure that Keras doesn't use the specific dummy input shapes as the spec self._set_save_spec(self.serving.input_signature[0]) diff --git a/src/transformers/models/t5/modeling_tf_t5.py b/src/transformers/models/t5/modeling_tf_t5.py index 18bf09ba7514..49669a823419 100644 --- a/src/transformers/models/t5/modeling_tf_t5.py +++ b/src/transformers/models/t5/modeling_tf_t5.py @@ -867,14 +867,12 @@ class TFT5PreTrainedModel(TFPreTrainedModel): @property def input_signature(self): - return self._prune_signature( - { - "input_ids": tf.TensorSpec((None, None), tf.int32, name="input_ids"), - "attention_mask": tf.TensorSpec((None, None), tf.int32, name="attention_mask"), - "decoder_input_ids": tf.TensorSpec((None, None), tf.int32, name="decoder_input_ids"), - "decoder_attention_mask": tf.TensorSpec((None, None), tf.int32, name="decoder_attention_mask"), - } - ) + { + "input_ids": tf.TensorSpec((None, None), tf.int32, name="input_ids"), + "attention_mask": tf.TensorSpec((None, None), tf.int32, name="attention_mask"), + "decoder_input_ids": tf.TensorSpec((None, None), tf.int32, name="decoder_input_ids"), + "decoder_attention_mask": tf.TensorSpec((None, None), tf.int32, name="decoder_attention_mask"), + } def get_input_embeddings(self): return self.shared From 4328bf79029881dabe4ac0030d735a7bcb4ed4df Mon Sep 17 00:00:00 2001 From: Matt Date: Mon, 15 May 2023 19:21:24 +0100 Subject: [PATCH 30/49] Fix T5's signature --- src/transformers/models/t5/modeling_tf_t5.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transformers/models/t5/modeling_tf_t5.py b/src/transformers/models/t5/modeling_tf_t5.py index 49669a823419..c1c44b2c2ccd 100644 --- a/src/transformers/models/t5/modeling_tf_t5.py +++ b/src/transformers/models/t5/modeling_tf_t5.py @@ -867,7 +867,7 @@ class TFT5PreTrainedModel(TFPreTrainedModel): @property def input_signature(self): - { + return { "input_ids": tf.TensorSpec((None, None), tf.int32, name="input_ids"), "attention_mask": tf.TensorSpec((None, None), tf.int32, name="attention_mask"), "decoder_input_ids": tf.TensorSpec((None, None), tf.int32, name="decoder_input_ids"), From 2b4baf4be9b42b1d78e6c78e967edd8a192f3e54 Mon Sep 17 00:00:00 2001 From: Matt Date: Mon, 15 May 2023 19:27:47 +0100 Subject: [PATCH 31/49] Fix Wav2Vec2 signature --- src/transformers/models/wav2vec2/modeling_tf_wav2vec2.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/transformers/models/wav2vec2/modeling_tf_wav2vec2.py b/src/transformers/models/wav2vec2/modeling_tf_wav2vec2.py index ef90908b0c5c..39e1539e70a7 100644 --- a/src/transformers/models/wav2vec2/modeling_tf_wav2vec2.py +++ b/src/transformers/models/wav2vec2/modeling_tf_wav2vec2.py @@ -1191,6 +1191,13 @@ def input_signature(self): "attention_mask": tf.TensorSpec((None, None), tf.float32, name="attention_mask"), } + @property + def dummy_inputs(self): + return { + "input_values": tf.random.uniform(shape=(1, 16000), dtype=tf.float32), + "attention_mask": tf.ones(shape=(1, 16000), dtype=tf.float32), + } + def __init__(self, config, *inputs, **kwargs): super().__init__(config, *inputs, **kwargs) logger.warning( From bb731385ceace7eaa84585424c04db96ee5179ab Mon Sep 17 00:00:00 2001 From: Matt Date: Mon, 15 May 2023 19:36:19 +0100 Subject: [PATCH 32/49] Fix LongformerForMultipleChoice input signature --- .../models/longformer/modeling_tf_longformer.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/transformers/models/longformer/modeling_tf_longformer.py b/src/transformers/models/longformer/modeling_tf_longformer.py index b9e471fda48d..a3a31beca80a 100644 --- a/src/transformers/models/longformer/modeling_tf_longformer.py +++ b/src/transformers/models/longformer/modeling_tf_longformer.py @@ -2412,6 +2412,14 @@ def __init__(self, config, *inputs, **kwargs): 1, kernel_initializer=get_initializer(config.initializer_range), name="classifier" ) + @property + def input_signature(self): + return { + "input_ids": tf.TensorSpec((None, None, None), tf.int32, name="input_ids"), + "attention_mask": tf.TensorSpec((None, None, None), tf.int32, name="attention_mask"), + "global_attention_mask": tf.TensorSpec((None, None, None), tf.int32, name="global_attention_mask"), + } + @unpack_inputs @add_start_docstrings_to_model_forward( LONGFORMER_INPUTS_DOCSTRING.format("batch_size, num_choices, sequence_length") From 74e991d4a974f4b43c9348a02ed8e226aa436133 Mon Sep 17 00:00:00 2001 From: Matt Date: Mon, 15 May 2023 20:04:17 +0100 Subject: [PATCH 33/49] Fix BLIP and LED --- src/transformers/models/blip/modeling_tf_blip.py | 8 +++++++- .../models/blip/modeling_tf_blip_text.py | 13 +++++++++++++ src/transformers/models/led/modeling_tf_led.py | 1 + 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/transformers/models/blip/modeling_tf_blip.py b/src/transformers/models/blip/modeling_tf_blip.py index df2840b12abf..4c2f2735188b 100644 --- a/src/transformers/models/blip/modeling_tf_blip.py +++ b/src/transformers/models/blip/modeling_tf_blip.py @@ -1189,6 +1189,12 @@ def _shift_right(self, input_ids): return shifted_input_ids + @property + def input_signature(self): + base_sig = super().input_signature + base_sig["decoder_input_ids"] = base_sig["input_ids"] + return base_sig + @unpack_inputs @add_start_docstrings_to_model_forward(BLIP_VISION_INPUTS_DOCSTRING) @replace_return_docstrings(output_type=TFBlipTextVisionModelOutput, config_class=BlipVisionConfig) @@ -1241,7 +1247,7 @@ def call( ```""" if labels is None and decoder_input_ids is None: raise ValueError( - "Either `decoder_input_ids` or `labels` should be passed when calling `forward` with" + "Either `decoder_input_ids` or `labels` should be passed when calling" " `TFBlipForQuestionAnswering`. if you are training the model make sure that `labels` is passed, if you" " are using the model for inference make sure that `decoder_input_ids` is passed or call `generate`" ) diff --git a/src/transformers/models/blip/modeling_tf_blip_text.py b/src/transformers/models/blip/modeling_tf_blip_text.py index 19ebdac62e22..630bb5a98d64 100644 --- a/src/transformers/models/blip/modeling_tf_blip_text.py +++ b/src/transformers/models/blip/modeling_tf_blip_text.py @@ -800,6 +800,19 @@ def call( cross_attentions=encoder_outputs.cross_attentions, ) + def serving_output( + self, output: TFBaseModelOutputWithPoolingAndCrossAttentions + ) -> TFBaseModelOutputWithPoolingAndCrossAttentions: + hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None + attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None + + return TFBaseModelOutputWithPoolingAndCrossAttentions( + last_hidden_state=output.last_hidden_state, + pooler_output=output.pooler_output, + hidden_states=hs, + attentions=attns, + ) + # Adapted from https://github.com/salesforce/BLIP/blob/main/models/med.py#L811 class TFBlipTextLMHeadModel(TFBlipTextPreTrainedModel): diff --git a/src/transformers/models/led/modeling_tf_led.py b/src/transformers/models/led/modeling_tf_led.py index 988107236df3..cb69e15b54c3 100644 --- a/src/transformers/models/led/modeling_tf_led.py +++ b/src/transformers/models/led/modeling_tf_led.py @@ -1329,6 +1329,7 @@ def input_signature(self): "attention_mask": tf.TensorSpec((None, None), tf.int32, name="attention_mask"), "decoder_input_ids": tf.TensorSpec((None, None), tf.int32, name="decoder_input_ids"), "decoder_attention_mask": tf.TensorSpec((None, None), tf.int32, name="decoder_attention_mask"), + "global_attention_mask": tf.TensorSpec((None, None), tf.int32, name="global_attention_mask"), } From 38dbc86faa1cec8b5fc2013728a2b8227af1ee04 Mon Sep 17 00:00:00 2001 From: Matt Date: Tue, 16 May 2023 17:07:07 +0100 Subject: [PATCH 34/49] Better default serving output error handling --- src/transformers/modeling_tf_utils.py | 4 +- .../models/funnel/modeling_tf_funnel.py | 61 +++++++++++++++++++ 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/src/transformers/modeling_tf_utils.py b/src/transformers/modeling_tf_utils.py index 6ac439f36840..0bccaa8ce58b 100644 --- a/src/transformers/modeling_tf_utils.py +++ b/src/transformers/modeling_tf_utils.py @@ -1284,10 +1284,10 @@ def serving_output(self, output): and getattr(self.config, "add_cross_attention", False) ): output[key] = None - if output[key] is not None: + if isinstance(output[key], (tuple, list)): try: output[key] = tf.convert_to_tensor(output[key]) - except ValueError: + except (ValueError, tf.errors.InvalidArgumentError): pass # Layers may not have the same dimensions return output diff --git a/src/transformers/models/funnel/modeling_tf_funnel.py b/src/transformers/models/funnel/modeling_tf_funnel.py index 522add5a8c1f..9c472674cf65 100644 --- a/src/transformers/models/funnel/modeling_tf_funnel.py +++ b/src/transformers/models/funnel/modeling_tf_funnel.py @@ -1132,6 +1132,15 @@ def call( training=training, ) + def serving_output(self, output): + # hidden_states and attentions not converted to Tensor with tf.convert_to_tensor as they are all of + # different dimensions + return TFBaseModelOutput( + last_hidden_state=output.last_hidden_state, + hidden_states=output.hidden_states, + attentions=output.attentions, + ) + @add_start_docstrings( "The bare Funnel Transformer Model transformer outputting raw hidden-states without any specific head on top.", @@ -1171,6 +1180,15 @@ def call( training=training, ) + def serving_output(self, output): + # hidden_states and attentions not converted to Tensor with tf.convert_to_tensor as they are all of + # different dimensions + return TFBaseModelOutput( + last_hidden_state=output.last_hidden_state, + hidden_states=output.hidden_states, + attentions=output.attentions, + ) + @add_start_docstrings( """ @@ -1237,6 +1255,13 @@ def call( attentions=discriminator_hidden_states.attentions, ) + def serving_output(self, output): + # hidden_states and attentions not converted to Tensor with tf.convert_to_tensor as they are all of + # different dimensions + return TFFunnelForPreTrainingOutput( + logits=output.logits, hidden_states=output.hidden_states, attentions=output.attentions + ) + @add_start_docstrings("""Funnel Model with a `language modeling` head on top.""", FUNNEL_START_DOCSTRING) class TFFunnelForMaskedLM(TFFunnelPreTrainedModel, TFMaskedLanguageModelingLoss): @@ -1304,6 +1329,11 @@ def call( attentions=outputs.attentions, ) + def serving_output(self, output: TFMaskedLMOutput) -> TFMaskedLMOutput: + # hidden_states and attentions not converted to Tensor with tf.convert_to_tensor as they are all of + # different dimensions + return TFMaskedLMOutput(logits=output.logits, hidden_states=output.hidden_states, attentions=output.attentions) + @add_start_docstrings( """ @@ -1372,6 +1402,13 @@ def call( attentions=outputs.attentions, ) + def serving_output(self, output: TFSequenceClassifierOutput) -> TFSequenceClassifierOutput: + # hidden_states and attentions not converted to Tensor with tf.convert_to_tensor as they are all of + # different dimensions + return TFSequenceClassifierOutput( + logits=output.logits, hidden_states=output.hidden_states, attentions=output.attentions + ) + @add_start_docstrings( """ @@ -1456,6 +1493,13 @@ def call( attentions=outputs.attentions, ) + def serving_output(self, output: TFMultipleChoiceModelOutput) -> TFMultipleChoiceModelOutput: + # hidden_states and attentions not converted to Tensor with tf.convert_to_tensor as they are all of + # different dimensions + return TFMultipleChoiceModelOutput( + logits=output.logits, hidden_states=output.hidden_states, attentions=output.attentions + ) + @add_start_docstrings( """ @@ -1526,6 +1570,13 @@ def call( attentions=outputs.attentions, ) + def serving_output(self, output: TFTokenClassifierOutput) -> TFTokenClassifierOutput: + # hidden_states and attentions not converted to Tensor with tf.convert_to_tensor as they are all of + # different dimensions + return TFTokenClassifierOutput( + logits=output.logits, hidden_states=output.hidden_states, attentions=output.attentions + ) + @add_start_docstrings( """ @@ -1608,3 +1659,13 @@ def call( hidden_states=outputs.hidden_states, attentions=outputs.attentions, ) + + def serving_output(self, output: TFQuestionAnsweringModelOutput) -> TFQuestionAnsweringModelOutput: + # hidden_states and attentions not converted to Tensor with tf.convert_to_tensor as they are all of + # different dimensions + return TFQuestionAnsweringModelOutput( + start_logits=output.start_logits, + end_logits=output.end_logits, + hidden_states=output.hidden_states, + attentions=output.attentions, + ) From 057454fa4f48078240ea687b4a92cb3e67cb2aa5 Mon Sep 17 00:00:00 2001 From: Matt Date: Wed, 17 May 2023 17:32:20 +0100 Subject: [PATCH 35/49] Fix BART dummies --- src/transformers/models/bart/modeling_tf_bart.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/transformers/models/bart/modeling_tf_bart.py b/src/transformers/models/bart/modeling_tf_bart.py index d1b3c728a4db..bf89d0fcf533 100644 --- a/src/transformers/models/bart/modeling_tf_bart.py +++ b/src/transformers/models/bart/modeling_tf_bart.py @@ -493,6 +493,15 @@ def input_signature(self): "decoder_attention_mask": tf.TensorSpec((None, None), tf.int32, name="decoder_attention_mask"), } + @property + def dummy_inputs(self): + dummy_inputs = super().dummy_inputs + # Dummy inputs should not contain the default val of 1 + # as this is the padding token and some assertions check it + dummy_inputs["input_ids"] = dummy_inputs["input_ids"] * 2 + dummy_inputs["decoder_input_ids"] = dummy_inputs["decoder_input_ids"] * 2 + return dummy_inputs + BART_START_DOCSTRING = r""" This model inherits from [`TFPreTrainedModel`]. Check the superclass documentation for the generic methods the From 692921eed7f9cdfbee6aecb1ee4704d73c31a9af Mon Sep 17 00:00:00 2001 From: Matt Date: Wed, 17 May 2023 18:27:19 +0100 Subject: [PATCH 36/49] Fix dummies for cross-attention, esp encoder-decoder models --- src/transformers/modeling_tf_utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/transformers/modeling_tf_utils.py b/src/transformers/modeling_tf_utils.py index 0bccaa8ce58b..4d4f42e1200c 100644 --- a/src/transformers/modeling_tf_utils.py +++ b/src/transformers/modeling_tf_utils.py @@ -1124,8 +1124,8 @@ def dummy_inputs(self) -> Dict[str, tf.Tensor]: if self.config.add_cross_attention and "encoder_hidden_states" in inspect.signature(self.call).parameters: if "encoder_hidden_states" not in dummies: if self.main_input_name == "input_ids": - dummies["encoder_hidden_states"] = tf.keras.Input( - shape=(2, self.config.hidden_size), dtype=tf.float32, name="encoder_hidden_states" + dummies["encoder_hidden_states"] = tf.ones( + shape=(3, 3, self.config.hidden_size), dtype=tf.float32, name="encoder_hidden_states" ) else: raise NotImplementedError( From 1b90dad93eb1443c316439428f5ece9124365869 Mon Sep 17 00:00:00 2001 From: Matt Date: Wed, 17 May 2023 19:06:13 +0100 Subject: [PATCH 37/49] Fix visionencoderdecoder signature --- .../modeling_tf_vision_encoder_decoder.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/transformers/models/vision_encoder_decoder/modeling_tf_vision_encoder_decoder.py b/src/transformers/models/vision_encoder_decoder/modeling_tf_vision_encoder_decoder.py index eac67b4172bb..9667c529b564 100644 --- a/src/transformers/models/vision_encoder_decoder/modeling_tf_vision_encoder_decoder.py +++ b/src/transformers/models/vision_encoder_decoder/modeling_tf_vision_encoder_decoder.py @@ -254,13 +254,20 @@ def __init__( @property def input_signature(self): + vision_config = self.config.encoder + if hasattr(vision_config, "vision_config"): + vision_config = vision_config.vision_config + if hasattr(vision_config, "image_size"): + image_size = vision_config.image_size + else: + image_size = vision_config.input_size return { "pixel_values": tf.TensorSpec( shape=( None, - self.config.encoder.num_channels, - self.config.encoder.input_size, - self.config.encoder.input_size, + vision_config.num_channels, + image_size, + image_size, ), dtype=tf.float32, ), From 3bfe4c33338b0542432a02a1f9309c00a54e076a Mon Sep 17 00:00:00 2001 From: Matt Date: Thu, 18 May 2023 14:07:20 +0100 Subject: [PATCH 38/49] Fix BLIP serving output --- .../models/blip/modeling_tf_blip.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/transformers/models/blip/modeling_tf_blip.py b/src/transformers/models/blip/modeling_tf_blip.py index 4c2f2735188b..3b1e6e2b1c18 100644 --- a/src/transformers/models/blip/modeling_tf_blip.py +++ b/src/transformers/models/blip/modeling_tf_blip.py @@ -647,6 +647,17 @@ def __init__(self, config: BlipVisionConfig, *args, **kwargs): self.encoder = TFBlipEncoder(config, name="encoder") self.post_layernorm = tf.keras.layers.LayerNormalization(epsilon=config.layer_norm_eps, name="post_layernorm") + def serving_output(self, output: TFBaseModelOutputWithPooling) -> TFBaseModelOutputWithPooling: + hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None + attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None + + return TFBaseModelOutputWithPooling( + last_hidden_state=output.last_hidden_state, + pooler_output=output.pooler_output, + hidden_states=hs, + attentions=attns, + ) + @unpack_inputs @add_start_docstrings_to_model_forward(BLIP_VISION_INPUTS_DOCSTRING) @replace_return_docstrings(output_type=TFBaseModelOutputWithPooling, config_class=BlipVisionConfig) @@ -837,6 +848,14 @@ def __init__(self, config: BlipConfig, *inputs, **kwargs): self.blip = TFBlipMainLayer(config, name="blip") + def serving_output(self, output: TFBlipOutput) -> TFBlipOutput: + return TFBlipOutput( + logits_per_image=output.logits_per_image, + logits_per_text=output.logits_per_text, + text_embeds=output.text_embeds, + image_embeds=output.image_embeds, + ) + @unpack_inputs @add_start_docstrings_to_model_forward(BLIP_INPUTS_DOCSTRING) @replace_return_docstrings(output_type=TFBlipOutput, config_class=BlipConfig) From 0efc89be3be981f3015a7375da75167ca2b312ca Mon Sep 17 00:00:00 2001 From: Matt Date: Thu, 18 May 2023 14:13:03 +0100 Subject: [PATCH 39/49] Small tweak to BART dummies --- src/transformers/models/bart/modeling_tf_bart.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/transformers/models/bart/modeling_tf_bart.py b/src/transformers/models/bart/modeling_tf_bart.py index bf89d0fcf533..170c8de9b3a7 100644 --- a/src/transformers/models/bart/modeling_tf_bart.py +++ b/src/transformers/models/bart/modeling_tf_bart.py @@ -499,7 +499,8 @@ def dummy_inputs(self): # Dummy inputs should not contain the default val of 1 # as this is the padding token and some assertions check it dummy_inputs["input_ids"] = dummy_inputs["input_ids"] * 2 - dummy_inputs["decoder_input_ids"] = dummy_inputs["decoder_input_ids"] * 2 + if "decoder_input_ids" in dummy_inputs: + dummy_inputs["decoder_input_ids"] = dummy_inputs["decoder_input_ids"] * 2 return dummy_inputs From 1ff2c38161b0c619467becfa51cee91e079529d1 Mon Sep 17 00:00:00 2001 From: Matt Date: Thu, 18 May 2023 14:17:38 +0100 Subject: [PATCH 40/49] Cleanup the ugly parameter inspection line that I used in a few places --- src/transformers/modeling_tf_utils.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/transformers/modeling_tf_utils.py b/src/transformers/modeling_tf_utils.py index 4d4f42e1200c..fc9dc2500d99 100644 --- a/src/transformers/modeling_tf_utils.py +++ b/src/transformers/modeling_tf_utils.py @@ -1227,7 +1227,7 @@ def input_signature(self) -> Dict[str, tf.TensorSpec]: shape and dtype for model inputs. It is used for both serving and for generating the dummy inputs used to build the model. """ - model_inputs = list(dict(inspect.signature(self.call).parameters).keys()) + model_inputs = list(inspect.signature(self.call).parameters) sig = {} if self.__class__.__name__.endswith("ForMultipleChoice"): text_dims = 3 @@ -1264,7 +1264,8 @@ def input_signature(self) -> Dict[str, tf.TensorSpec]: def _prune_signature(self, signature): """Keeps only the keys of a given input signature that are valid for this model.""" - model_inputs = list(dict(inspect.signature(self.call).parameters).keys()) + breakpoint() + model_inputs = list(inspect.signature(self.call).parameters) return {key: val for key, val in signature.items() if key in model_inputs} def serving_output(self, output): @@ -1443,7 +1444,7 @@ def prepare_tf_dataset( if not isinstance(dataset, datasets.Dataset): raise TypeError("Dataset argument should be a datasets.Dataset!") - model_inputs = list(dict(inspect.signature(self.call).parameters).keys()) + model_inputs = list(inspect.signature(self.call).parameters) model_labels = find_labels(self.__class__) if "cols_to_retain" in list(inspect.signature(dataset._get_output_signature).parameters.keys()): output_signature, _ = dataset._get_output_signature( @@ -1555,7 +1556,7 @@ def compute_loss(self, *args, **kwargs): return self.hf_compute_loss(*args, **kwargs) def get_label_to_output_name_mapping(self): - arg_names = list(dict(inspect.signature(self.call).parameters).keys()) + arg_names = list(inspect.signature(self.call).parameters) if self._label_to_output_map is not None: return self._label_to_output_map elif "start_positions" in arg_names: @@ -1578,7 +1579,7 @@ def train_step(self, data): """ # We hardcode the most common renamings; models with weirder names can set `self._label_to_output_map` - arg_names = list(dict(inspect.signature(self.call).parameters).keys()) + arg_names = list(inspect.signature(self.call).parameters) label_kwargs = find_labels(self.__class__) label_to_output = self.get_label_to_output_name_mapping() output_to_label = {val: key for key, val in label_to_output.items()} @@ -1685,7 +1686,7 @@ def test_step(self, data): that they are available to the model during the forward pass. """ # We hardcode the most common renamings; models with weirder names can set `self._label_to_output_map` - arg_names = list(dict(inspect.signature(self.call).parameters).keys()) + arg_names = list(inspect.signature(self.call).parameters) label_kwargs = find_labels(self.__class__) label_to_output = self.get_label_to_output_name_mapping() output_to_label = {val: key for key, val in label_to_output.items()} @@ -1704,7 +1705,7 @@ def test_step(self, data): # When using a dummy loss, we ensure that separate labels are copied to the correct model arguments, # if those keys are not already present in the input dict if self._using_dummy_loss and y is not None: - arg_names = list(dict(inspect.signature(self.call).parameters).keys()) + arg_names = list(inspect.signature(self.call).parameters) # If y is a tensor and the model only has one label-like input, map y to that input if len(label_kwargs) == 1 and isinstance(y, tf.Tensor): if isinstance(x, tf.Tensor): From 257f3de7eacc91a4c84c6c4a5ff89dbd593c3ce3 Mon Sep 17 00:00:00 2001 From: Matt Date: Thu, 18 May 2023 14:28:51 +0100 Subject: [PATCH 41/49] committed a breakpoint again --- src/transformers/modeling_tf_utils.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/transformers/modeling_tf_utils.py b/src/transformers/modeling_tf_utils.py index fc9dc2500d99..12f96bdfa583 100644 --- a/src/transformers/modeling_tf_utils.py +++ b/src/transformers/modeling_tf_utils.py @@ -1264,7 +1264,6 @@ def input_signature(self) -> Dict[str, tf.TensorSpec]: def _prune_signature(self, signature): """Keeps only the keys of a given input signature that are valid for this model.""" - breakpoint() model_inputs = list(inspect.signature(self.call).parameters) return {key: val for key, val in signature.items() if key in model_inputs} From 599ce590f8aff3bba91518bbcbae795a0ae62b26 Mon Sep 17 00:00:00 2001 From: Matt Date: Thu, 18 May 2023 17:01:44 +0100 Subject: [PATCH 42/49] Move the text_dims check --- src/transformers/modeling_tf_utils.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/transformers/modeling_tf_utils.py b/src/transformers/modeling_tf_utils.py index 12f96bdfa583..4fe162f251a3 100644 --- a/src/transformers/modeling_tf_utils.py +++ b/src/transformers/modeling_tf_utils.py @@ -1229,11 +1229,11 @@ def input_signature(self) -> Dict[str, tf.TensorSpec]: """ model_inputs = list(inspect.signature(self.call).parameters) sig = {} - if self.__class__.__name__.endswith("ForMultipleChoice"): - text_dims = 3 - else: - text_dims = 2 if "input_ids" in model_inputs: + if self.__class__.__name__.endswith("ForMultipleChoice"): + text_dims = 3 + else: + text_dims = 2 for input_name in ("input_ids", "attention_mask", "token_type_ids"): if input_name in model_inputs: sig[input_name] = tf.TensorSpec([None] * text_dims, tf.int32, name=input_name) From 1a9d3ddcf75557ac33d83f4a33f6b90db2ad7569 Mon Sep 17 00:00:00 2001 From: Matt Date: Thu, 18 May 2023 17:04:09 +0100 Subject: [PATCH 43/49] Remove blip_text serving_output --- .../models/blip/modeling_tf_blip_text.py | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/src/transformers/models/blip/modeling_tf_blip_text.py b/src/transformers/models/blip/modeling_tf_blip_text.py index 630bb5a98d64..19ebdac62e22 100644 --- a/src/transformers/models/blip/modeling_tf_blip_text.py +++ b/src/transformers/models/blip/modeling_tf_blip_text.py @@ -800,19 +800,6 @@ def call( cross_attentions=encoder_outputs.cross_attentions, ) - def serving_output( - self, output: TFBaseModelOutputWithPoolingAndCrossAttentions - ) -> TFBaseModelOutputWithPoolingAndCrossAttentions: - hs = tf.convert_to_tensor(output.hidden_states) if self.config.output_hidden_states else None - attns = tf.convert_to_tensor(output.attentions) if self.config.output_attentions else None - - return TFBaseModelOutputWithPoolingAndCrossAttentions( - last_hidden_state=output.last_hidden_state, - pooler_output=output.pooler_output, - hidden_states=hs, - attentions=attns, - ) - # Adapted from https://github.com/salesforce/BLIP/blob/main/models/med.py#L811 class TFBlipTextLMHeadModel(TFBlipTextPreTrainedModel): From f348dbe15c8ca970c74d9df5121ef1e6efbdf3ea Mon Sep 17 00:00:00 2001 From: Matt Date: Thu, 18 May 2023 17:07:23 +0100 Subject: [PATCH 44/49] Add decoder_input_ids to the default input sig --- src/transformers/modeling_tf_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transformers/modeling_tf_utils.py b/src/transformers/modeling_tf_utils.py index 4fe162f251a3..b04c2bfb6fc1 100644 --- a/src/transformers/modeling_tf_utils.py +++ b/src/transformers/modeling_tf_utils.py @@ -1234,7 +1234,7 @@ def input_signature(self) -> Dict[str, tf.TensorSpec]: text_dims = 3 else: text_dims = 2 - for input_name in ("input_ids", "attention_mask", "token_type_ids"): + for input_name in ("input_ids", "attention_mask", "token_type_ids", "decoder_input_ids"): if input_name in model_inputs: sig[input_name] = tf.TensorSpec([None] * text_dims, tf.int32, name=input_name) if "pixel_values" in model_inputs: From cd887ef223eff34728b594ca19e8b494c37fd07b Mon Sep 17 00:00:00 2001 From: Matt Date: Thu, 18 May 2023 17:46:51 +0100 Subject: [PATCH 45/49] Remove all the manual overrides for encoder-decoder model signatures --- src/transformers/modeling_tf_utils.py | 8 +++++++- src/transformers/models/bart/modeling_tf_bart.py | 9 --------- .../models/blenderbot/modeling_tf_blenderbot.py | 9 --------- .../blenderbot_small/modeling_tf_blenderbot_small.py | 9 --------- src/transformers/models/blip/modeling_tf_blip.py | 6 ------ .../encoder_decoder/modeling_tf_encoder_decoder.py | 7 ------- src/transformers/models/marian/modeling_tf_marian.py | 9 --------- src/transformers/models/mbart/modeling_tf_mbart.py | 9 --------- src/transformers/models/pegasus/modeling_tf_pegasus.py | 9 --------- src/transformers/models/t5/modeling_tf_t5.py | 9 --------- src/transformers/models/xglm/modeling_tf_xglm.py | 9 --------- 11 files changed, 7 insertions(+), 86 deletions(-) diff --git a/src/transformers/modeling_tf_utils.py b/src/transformers/modeling_tf_utils.py index b04c2bfb6fc1..f6b5008fe88e 100644 --- a/src/transformers/modeling_tf_utils.py +++ b/src/transformers/modeling_tf_utils.py @@ -1234,7 +1234,13 @@ def input_signature(self) -> Dict[str, tf.TensorSpec]: text_dims = 3 else: text_dims = 2 - for input_name in ("input_ids", "attention_mask", "token_type_ids", "decoder_input_ids"): + for input_name in ( + "input_ids", + "attention_mask", + "token_type_ids", + "decoder_input_ids", + "decoder_attention_mask", + ): if input_name in model_inputs: sig[input_name] = tf.TensorSpec([None] * text_dims, tf.int32, name=input_name) if "pixel_values" in model_inputs: diff --git a/src/transformers/models/bart/modeling_tf_bart.py b/src/transformers/models/bart/modeling_tf_bart.py index 170c8de9b3a7..e2555381f4bd 100644 --- a/src/transformers/models/bart/modeling_tf_bart.py +++ b/src/transformers/models/bart/modeling_tf_bart.py @@ -484,15 +484,6 @@ class TFBartPretrainedModel(TFPreTrainedModel): config_class = BartConfig base_model_prefix = "model" - @property - def input_signature(self): - return { - "input_ids": tf.TensorSpec((None, None), tf.int32, name="input_ids"), - "attention_mask": tf.TensorSpec((None, None), tf.int32, name="attention_mask"), - "decoder_input_ids": tf.TensorSpec((None, None), tf.int32, name="decoder_input_ids"), - "decoder_attention_mask": tf.TensorSpec((None, None), tf.int32, name="decoder_attention_mask"), - } - @property def dummy_inputs(self): dummy_inputs = super().dummy_inputs diff --git a/src/transformers/models/blenderbot/modeling_tf_blenderbot.py b/src/transformers/models/blenderbot/modeling_tf_blenderbot.py index 893d97d99ab4..d0e745503705 100644 --- a/src/transformers/models/blenderbot/modeling_tf_blenderbot.py +++ b/src/transformers/models/blenderbot/modeling_tf_blenderbot.py @@ -463,15 +463,6 @@ class TFBlenderbotPreTrainedModel(TFPreTrainedModel): config_class = BlenderbotConfig base_model_prefix = "model" - @property - def input_signature(self): - return { - "input_ids": tf.TensorSpec((None, None), tf.int32, name="input_ids"), - "attention_mask": tf.TensorSpec((None, None), tf.int32, name="attention_mask"), - "decoder_input_ids": tf.TensorSpec((None, None), tf.int32, name="decoder_input_ids"), - "decoder_attention_mask": tf.TensorSpec((None, None), tf.int32, name="decoder_attention_mask"), - } - BLENDERBOT_START_DOCSTRING = r""" This model inherits from [`TFPreTrainedModel`]. Check the superclass documentation for the generic methods the diff --git a/src/transformers/models/blenderbot_small/modeling_tf_blenderbot_small.py b/src/transformers/models/blenderbot_small/modeling_tf_blenderbot_small.py index 8f898a72ee9a..2e8d2e11cae7 100644 --- a/src/transformers/models/blenderbot_small/modeling_tf_blenderbot_small.py +++ b/src/transformers/models/blenderbot_small/modeling_tf_blenderbot_small.py @@ -463,15 +463,6 @@ class TFBlenderbotSmallPreTrainedModel(TFPreTrainedModel): config_class = BlenderbotSmallConfig base_model_prefix = "model" - @property - def input_signature(self): - return { - "input_ids": tf.TensorSpec((None, None), tf.int32, name="input_ids"), - "attention_mask": tf.TensorSpec((None, None), tf.int32, name="attention_mask"), - "decoder_input_ids": tf.TensorSpec((None, None), tf.int32, name="decoder_input_ids"), - "decoder_attention_mask": tf.TensorSpec((None, None), tf.int32, name="decoder_attention_mask"), - } - BLENDERBOT_SMALL_START_DOCSTRING = r""" This model inherits from [`TFPreTrainedModel`]. Check the superclass documentation for the generic methods the diff --git a/src/transformers/models/blip/modeling_tf_blip.py b/src/transformers/models/blip/modeling_tf_blip.py index 3b1e6e2b1c18..428151ea9a3c 100644 --- a/src/transformers/models/blip/modeling_tf_blip.py +++ b/src/transformers/models/blip/modeling_tf_blip.py @@ -1208,12 +1208,6 @@ def _shift_right(self, input_ids): return shifted_input_ids - @property - def input_signature(self): - base_sig = super().input_signature - base_sig["decoder_input_ids"] = base_sig["input_ids"] - return base_sig - @unpack_inputs @add_start_docstrings_to_model_forward(BLIP_VISION_INPUTS_DOCSTRING) @replace_return_docstrings(output_type=TFBlipTextVisionModelOutput, config_class=BlipVisionConfig) diff --git a/src/transformers/models/encoder_decoder/modeling_tf_encoder_decoder.py b/src/transformers/models/encoder_decoder/modeling_tf_encoder_decoder.py index a1a46ed569bc..19fc47546b0f 100644 --- a/src/transformers/models/encoder_decoder/modeling_tf_encoder_decoder.py +++ b/src/transformers/models/encoder_decoder/modeling_tf_encoder_decoder.py @@ -275,13 +275,6 @@ def __init__( "following discussion on GitHub: https://github.com/huggingface/transformers/issues/23350" ) - @property - def input_signature(self): - return { - "input_ids": tf.TensorSpec([None, None], dtype=tf.int32), - "decoder_input_ids": tf.TensorSpec([None, None], dtype=tf.int32), - } - def get_encoder(self): return self.encoder diff --git a/src/transformers/models/marian/modeling_tf_marian.py b/src/transformers/models/marian/modeling_tf_marian.py index 85a81e48b002..9632ddeaac8f 100644 --- a/src/transformers/models/marian/modeling_tf_marian.py +++ b/src/transformers/models/marian/modeling_tf_marian.py @@ -500,15 +500,6 @@ class TFMarianPreTrainedModel(TFPreTrainedModel): config_class = MarianConfig base_model_prefix = "model" - @property - def input_signature(self): - return { - "input_ids": tf.TensorSpec((None, None), tf.int32, name="input_ids"), - "attention_mask": tf.TensorSpec((None, None), tf.int32, name="attention_mask"), - "decoder_input_ids": tf.TensorSpec((None, None), tf.int32, name="decoder_input_ids"), - "decoder_attention_mask": tf.TensorSpec((None, None), tf.int32, name="decoder_attention_mask"), - } - MARIAN_START_DOCSTRING = r""" This model inherits from [`TFPreTrainedModel`]. Check the superclass documentation for the generic methods the diff --git a/src/transformers/models/mbart/modeling_tf_mbart.py b/src/transformers/models/mbart/modeling_tf_mbart.py index f1a05d8f735e..b0e2d141f4fa 100644 --- a/src/transformers/models/mbart/modeling_tf_mbart.py +++ b/src/transformers/models/mbart/modeling_tf_mbart.py @@ -467,15 +467,6 @@ class TFMBartPreTrainedModel(TFPreTrainedModel): config_class = MBartConfig base_model_prefix = "model" - @property - def input_signature(self): - return { - "input_ids": tf.TensorSpec((None, None), tf.int32, name="input_ids"), - "attention_mask": tf.TensorSpec((None, None), tf.int32, name="attention_mask"), - "decoder_input_ids": tf.TensorSpec((None, None), tf.int32, name="decoder_input_ids"), - "decoder_attention_mask": tf.TensorSpec((None, None), tf.int32, name="decoder_attention_mask"), - } - MBART_START_DOCSTRING = r""" This model inherits from [`TFPreTrainedModel`]. Check the superclass documentation for the generic methods the diff --git a/src/transformers/models/pegasus/modeling_tf_pegasus.py b/src/transformers/models/pegasus/modeling_tf_pegasus.py index aaf3ccd584b7..15c87b938bfa 100644 --- a/src/transformers/models/pegasus/modeling_tf_pegasus.py +++ b/src/transformers/models/pegasus/modeling_tf_pegasus.py @@ -502,15 +502,6 @@ class TFPegasusPreTrainedModel(TFPreTrainedModel): config_class = PegasusConfig base_model_prefix = "model" - @property - def input_signature(self): - return { - "input_ids": tf.TensorSpec((None, None), tf.int32, name="input_ids"), - "attention_mask": tf.TensorSpec((None, None), tf.int32, name="attention_mask"), - "decoder_input_ids": tf.TensorSpec((None, None), tf.int32, name="decoder_input_ids"), - "decoder_attention_mask": tf.TensorSpec((None, None), tf.int32, name="decoder_attention_mask"), - } - PEGASUS_START_DOCSTRING = r""" This model inherits from [`TFPreTrainedModel`]. Check the superclass documentation for the generic methods the diff --git a/src/transformers/models/t5/modeling_tf_t5.py b/src/transformers/models/t5/modeling_tf_t5.py index c1c44b2c2ccd..daef8bfb7fdd 100644 --- a/src/transformers/models/t5/modeling_tf_t5.py +++ b/src/transformers/models/t5/modeling_tf_t5.py @@ -865,15 +865,6 @@ class TFT5PreTrainedModel(TFPreTrainedModel): # names with a '.' represents the authorized unexpected/missing layers when a TF model is loaded from a PT model _keys_to_ignore_on_load_unexpected = [r"decoder\Wblock[\W_0]+layer[\W_1]+EncDecAttention\Wrelative_attention_bias"] - @property - def input_signature(self): - return { - "input_ids": tf.TensorSpec((None, None), tf.int32, name="input_ids"), - "attention_mask": tf.TensorSpec((None, None), tf.int32, name="attention_mask"), - "decoder_input_ids": tf.TensorSpec((None, None), tf.int32, name="decoder_input_ids"), - "decoder_attention_mask": tf.TensorSpec((None, None), tf.int32, name="decoder_attention_mask"), - } - def get_input_embeddings(self): return self.shared diff --git a/src/transformers/models/xglm/modeling_tf_xglm.py b/src/transformers/models/xglm/modeling_tf_xglm.py index 217a02b53f9b..6cc9db021cf9 100644 --- a/src/transformers/models/xglm/modeling_tf_xglm.py +++ b/src/transformers/models/xglm/modeling_tf_xglm.py @@ -619,15 +619,6 @@ class TFXGLMPreTrainedModel(TFPreTrainedModel): config_class = XGLMConfig base_model_prefix = "model" - @property - def input_signature(self): - return { - "input_ids": tf.TensorSpec((None, None), tf.int32, name="input_ids"), - "attention_mask": tf.TensorSpec((None, None), tf.int32, name="attention_mask"), - "decoder_input_ids": tf.TensorSpec((None, None), tf.int32, name="decoder_input_ids"), - "decoder_attention_mask": tf.TensorSpec((None, None), tf.int32, name="decoder_attention_mask"), - } - XGLM_START_DOCSTRING = r""" This model inherits from [`TFPreTrainedModel`]. Check the superclass documentation for the generic methods the From 60a41fa907084b7ce0ac80763f9ec5c943752073 Mon Sep 17 00:00:00 2001 From: Matt Date: Wed, 24 May 2023 16:03:43 +0100 Subject: [PATCH 46/49] Tweak longformer/led input sigs --- src/transformers/models/led/modeling_tf_led.py | 10 +++------- .../models/longformer/modeling_tf_longformer.py | 8 +++----- 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/src/transformers/models/led/modeling_tf_led.py b/src/transformers/models/led/modeling_tf_led.py index cb69e15b54c3..6e962ea4934e 100644 --- a/src/transformers/models/led/modeling_tf_led.py +++ b/src/transformers/models/led/modeling_tf_led.py @@ -1324,13 +1324,9 @@ class TFLEDPreTrainedModel(TFPreTrainedModel): @property def input_signature(self): - return { - "input_ids": tf.TensorSpec((None, None), tf.int32, name="input_ids"), - "attention_mask": tf.TensorSpec((None, None), tf.int32, name="attention_mask"), - "decoder_input_ids": tf.TensorSpec((None, None), tf.int32, name="decoder_input_ids"), - "decoder_attention_mask": tf.TensorSpec((None, None), tf.int32, name="decoder_attention_mask"), - "global_attention_mask": tf.TensorSpec((None, None), tf.int32, name="global_attention_mask"), - } + sig = super().input_signature + sig["global_attention_mask"] = tf.TensorSpec((None, None), tf.int32, name="global_attention_mask") + return sig @dataclass diff --git a/src/transformers/models/longformer/modeling_tf_longformer.py b/src/transformers/models/longformer/modeling_tf_longformer.py index a3a31beca80a..60cee2a83e89 100644 --- a/src/transformers/models/longformer/modeling_tf_longformer.py +++ b/src/transformers/models/longformer/modeling_tf_longformer.py @@ -1874,11 +1874,9 @@ class TFLongformerPreTrainedModel(TFPreTrainedModel): @property def input_signature(self): - return { - "input_ids": tf.TensorSpec((None, None), tf.int32, name="input_ids"), - "attention_mask": tf.TensorSpec((None, None), tf.int32, name="attention_mask"), - "global_attention_mask": tf.TensorSpec((None, None), tf.int32, name="global_attention_mask"), - } + sig = super().input_signature + sig["global_attention_mask"] = tf.TensorSpec((None, None), tf.int32, name="global_attention_mask") + return sig LONGFORMER_START_DOCSTRING = r""" From 05f7584dc3e6e83d31d2e9d71c68fd88ba775ed9 Mon Sep 17 00:00:00 2001 From: Matt Date: Wed, 24 May 2023 16:05:17 +0100 Subject: [PATCH 47/49] Tweak default serving output --- src/transformers/modeling_tf_utils.py | 37 ++++++++++++++------------- 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/src/transformers/modeling_tf_utils.py b/src/transformers/modeling_tf_utils.py index f6b5008fe88e..38a4e8cc1051 100644 --- a/src/transformers/modeling_tf_utils.py +++ b/src/transformers/modeling_tf_utils.py @@ -1277,24 +1277,25 @@ def serving_output(self, output): """ Prepare the output of the saved model. Can be overridden if specific serving modifications are required. """ - if isinstance(output, ModelOutput): - for key in output.keys(): - if key.endswith("hidden_states") and not getattr(self.config, "output_hidden_states", False): - output[key] = None - elif key.endswith("attentions") and not getattr(self.config, "output_attentions", False): - output[key] = None - elif key == "past_key_values" and not getattr(self.config, "use_cache", False): - output[key] = None - elif key == "cross_attentions" and not ( - getattr(self.config, "output_attentions", False) - and getattr(self.config, "add_cross_attention", False) - ): - output[key] = None - if isinstance(output[key], (tuple, list)): - try: - output[key] = tf.convert_to_tensor(output[key]) - except (ValueError, tf.errors.InvalidArgumentError): - pass # Layers may not have the same dimensions + if not isinstance(output, ModelOutput): + return output + for key in output.keys(): + if key.endswith("hidden_states") and not getattr(self.config, "output_hidden_states", False): + output[key] = None + elif key.endswith("attentions") and not getattr(self.config, "output_attentions", False): + output[key] = None + elif key == "past_key_values" and not getattr(self.config, "use_cache", False): + output[key] = None + elif key == "cross_attentions" and not ( + getattr(self.config, "output_attentions", False) + and getattr(self.config, "add_cross_attention", False) + ): + output[key] = None + if isinstance(output[key], (tuple, list)): + try: + output[key] = tf.convert_to_tensor(output[key]) + except (ValueError, tf.errors.InvalidArgumentError): + pass # Layers may not have the same dimensions return output def can_generate(self) -> bool: From 52e5b6bd4aa9107de093e1be57ab12eeb3fbf36b Mon Sep 17 00:00:00 2001 From: Matt Date: Wed, 24 May 2023 16:05:47 +0100 Subject: [PATCH 48/49] output.keys() -> output --- src/transformers/modeling_tf_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transformers/modeling_tf_utils.py b/src/transformers/modeling_tf_utils.py index 38a4e8cc1051..cd8e69564487 100644 --- a/src/transformers/modeling_tf_utils.py +++ b/src/transformers/modeling_tf_utils.py @@ -1279,7 +1279,7 @@ def serving_output(self, output): """ if not isinstance(output, ModelOutput): return output - for key in output.keys(): + for key in output: if key.endswith("hidden_states") and not getattr(self.config, "output_hidden_states", False): output[key] = None elif key.endswith("attentions") and not getattr(self.config, "output_attentions", False): From 99da5217361381d7824527348b8e645047a6e674 Mon Sep 17 00:00:00 2001 From: Matt Date: Wed, 24 May 2023 16:17:21 +0100 Subject: [PATCH 49/49] make fixup --- src/transformers/modeling_tf_utils.py | 3 +-- src/transformers/models/convnext/modeling_tf_convnext.py | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/transformers/modeling_tf_utils.py b/src/transformers/modeling_tf_utils.py index cd8e69564487..bac575e249df 100644 --- a/src/transformers/modeling_tf_utils.py +++ b/src/transformers/modeling_tf_utils.py @@ -1287,8 +1287,7 @@ def serving_output(self, output): elif key == "past_key_values" and not getattr(self.config, "use_cache", False): output[key] = None elif key == "cross_attentions" and not ( - getattr(self.config, "output_attentions", False) - and getattr(self.config, "add_cross_attention", False) + getattr(self.config, "output_attentions", False) and getattr(self.config, "add_cross_attention", False) ): output[key] = None if isinstance(output[key], (tuple, list)): diff --git a/src/transformers/models/convnext/modeling_tf_convnext.py b/src/transformers/models/convnext/modeling_tf_convnext.py index 895d71663140..23a77a928ecc 100644 --- a/src/transformers/models/convnext/modeling_tf_convnext.py +++ b/src/transformers/models/convnext/modeling_tf_convnext.py @@ -17,7 +17,7 @@ from __future__ import annotations -from typing import Dict, Optional, Tuple, Union +from typing import Optional, Tuple, Union import numpy as np import tensorflow as tf