






'''simple docstring'''







import math
from typing import Any, Callable, List, Optional, Tuple, Union

import numpy as np
import torch

from ...models import TaFilmDecoder
from ...schedulers import DDPMScheduler
from ...utils import is_onnx_available, logging, randn_tensor


if is_onnx_available():
							from ..onnx_utils import OnnxRuntimeModel

from ..pipeline_utils import AudioPipelineOutput, DiffusionPipeline
from .continous_encoder import SpectrogramContEncoder
from .notes_encoder import SpectrogramNotesEncoder


_A			:    Dict					  =logging.get_logger(__name__)  # pylint: disable=invalid-name

_A			:    Dict					  =256

class _lowercase			(       _lowercase	):
							a          = ["""melgan"""]




							def __init__(							self:       int    ,							UpperCamelCase__:       SpectrogramNotesEncoder    ,							UpperCamelCase__:       SpectrogramContEncoder    ,							UpperCamelCase__:       TaFilmDecoder    ,							UpperCamelCase__:       DDPMScheduler    ,							UpperCamelCase__:       OnnxRuntimeModel if is_onnx_available() else Any    ,							):
														super().__init__()

														# From MELGAN
														lowerCamelCase__      :			str 						= math.log(1e-5  )  # Matches MelGAN training.
														lowerCamelCase__      :			List[str] 						= 4.0  # Largest value for most examples
														lowerCamelCase__      :			Tuple 						= 128

														self.register_modules(
														    notes_encoder=UpperCamelCase__    ,							continuous_encoder=UpperCamelCase__    ,							decoder=UpperCamelCase__    ,							scheduler=UpperCamelCase__    ,							melgan=UpperCamelCase__    ,							)




							def 	lowerCamelCase_       (							self:       str    ,							UpperCamelCase__:       Union[str, Any]    ,							UpperCamelCase__:       Optional[int]=(-1.0, 1.0)    ,							UpperCamelCase__:       Tuple=False  ):
														lowerCamelCase__      :			Any 						= output_range
														if clip:
																					lowerCamelCase__      :			Union[str, Any] 						= torch.clip(UpperCamelCase__    ,							self.min_value    ,							self.max_value  )
														# Scale to [0, 1].
														lowerCamelCase__      :			int 						= (features - self.min_value) / (self.max_value - self.min_value)
														# Scale to [min_out, max_out].
														return zero_one * (max_out - min_out) + min_out




							def 	lowerCamelCase_       (							self:       str    ,							UpperCamelCase__:       List[Any]    ,							UpperCamelCase__:       int=(-1.0, 1.0)    ,							UpperCamelCase__:       List[str]=False  ):
														lowerCamelCase__      :			str 						= input_range
														lowerCamelCase__      :			Dict 						= torch.clip(UpperCamelCase__    ,							UpperCamelCase__    ,							UpperCamelCase__  ) if clip else outputs
														# Scale to [0, 1].
														lowerCamelCase__      :			List[Any] 						= (outputs - min_out) / (max_out - min_out)
														# Scale to [self.min_value, self.max_value].
														return zero_one * (self.max_value - self.min_value) + self.min_value




							def 	lowerCamelCase_       (							self:       Optional[int]    ,							UpperCamelCase__:       List[Any]    ,							UpperCamelCase__:       Dict    ,							UpperCamelCase__:       Union[str, Any]  ):
														lowerCamelCase__      :			Any 						= input_tokens > 0
														lowerCamelCase__      :			List[str] 						= self.notes_encoder(
														    encoder_input_tokens=UpperCamelCase__    ,							encoder_inputs_mask=UpperCamelCase__  )

														lowerCamelCase__      :			Any 						= self.continuous_encoder(
														    encoder_inputs=UpperCamelCase__    ,							encoder_inputs_mask=UpperCamelCase__  )

														return [(tokens_encoded, tokens_mask), (continuous_encoded, continuous_mask)]




							def 	lowerCamelCase_       (							self:       List[str]    ,							UpperCamelCase__:       Optional[int]    ,							UpperCamelCase__:       int    ,							UpperCamelCase__:       Tuple  ):
														lowerCamelCase__      :			Optional[Any] 						= noise_time
														if not torch.is_tensor(UpperCamelCase__  ):
																					lowerCamelCase__      :			Optional[Any] 						= torch.tensor([timesteps]    ,							dtype=torch.long    ,							device=input_tokens.device  )
														elif torch.is_tensor(UpperCamelCase__  ) and len(timesteps.shape  ) == 0:
																					lowerCamelCase__      :			Tuple 						= timesteps[None].to(input_tokens.device  )

														# broadcast to batch dimension in a way that's compatible with ONNX/Core ML
														lowerCamelCase__      :			List[str] 						= timesteps * torch.ones(input_tokens.shape[0]    ,							dtype=timesteps.dtype    ,							device=timesteps.device  )

														lowerCamelCase__      :			Tuple 						= self.decoder(
														    encodings_and_masks=UpperCamelCase__    ,							decoder_input_tokens=UpperCamelCase__    ,							decoder_noise_time=UpperCamelCase__  )
														return logits




							@torch.no_grad()
							def __call__(							self:       List[str]    ,							UpperCamelCase__:       List[List[int]]    ,							UpperCamelCase__:       Optional[torch.Generator] = None    ,							UpperCamelCase__:       int = 100    ,							UpperCamelCase__:       bool = True    ,							UpperCamelCase__:       str = "numpy"    ,							UpperCamelCase__:       Optional[Callable[[int, int, torch.FloatTensor], None]] = None    ,							UpperCamelCase__:       int = 1    ,							):
														if (callback_steps is None) or (
														    callback_steps is not None and (not isinstance(UpperCamelCase__    ,							UpperCamelCase__  ) or callback_steps <= 0)
														):
																					raise ValueError(
																					    F'''`callback_steps` has to be a positive integer but is {callback_steps} of type'''
																					    F''' {type(UpperCamelCase__  )}.'''  )

														lowerCamelCase__      :			str 						= np.zeros([1, TARGET_FEATURE_LENGTH, self.n_dims]    ,							dtype=np.floataa  )
														lowerCamelCase__      :			Any 						= np.zeros([1, 0, self.n_dims]    ,							np.floataa  )
														lowerCamelCase__      :			List[Any] 						= torch.ones((1, TARGET_FEATURE_LENGTH)    ,							dtype=UpperCamelCase__    ,							device=self.device  )

														for i, encoder_input_tokens in enumerate(UpperCamelCase__  ):
																					if i == 0:
																												lowerCamelCase__      :			Optional[Any] 						= torch.from_numpy(pred_mel[:1].copy()  ).to(
																												    device=self.device    ,							dtype=self.decoder.dtype  )
																												# The first chunk has no previous context.
																												lowerCamelCase__      :			List[Any] 						= torch.zeros((1, TARGET_FEATURE_LENGTH)    ,							dtype=UpperCamelCase__    ,							device=self.device  )
																					else:
																												# The full song pipeline does not feed in a context feature, so the mask
																												# will be all 0s after the feature converter. Because we know we're
																												# feeding in a full context chunk from the previous prediction, set it
																												# to all 1s.
																												lowerCamelCase__      :			Optional[int] 						= ones

																					lowerCamelCase__      :			Optional[int] 						= self.scale_features(
																					    UpperCamelCase__    ,							output_range=[-1.0, 1.0]    ,							clip=UpperCamelCase__  )

																					lowerCamelCase__      :			Optional[Any] 						= self.encode(
																					    input_tokens=torch.IntTensor([encoder_input_tokens]  ).to(device=self.device  )    ,							continuous_inputs=UpperCamelCase__    ,							continuous_mask=UpperCamelCase__    ,							)

																					# Sample encoder_continuous_inputs shaped gaussian noise to begin loop
																					lowerCamelCase__      :			List[str] 						= randn_tensor(
																					    shape=encoder_continuous_inputs.shape    ,							generator=UpperCamelCase__    ,							device=self.device    ,							dtype=self.decoder.dtype    ,							)

																					# set step values
																					self.scheduler.set_timesteps(UpperCamelCase__  )

																					# Denoising diffusion loop
																					for j, t in enumerate(self.progress_bar(self.scheduler.timesteps  )  ):
																												lowerCamelCase__      :			str 						= self.decode(
																												    encodings_and_masks=UpperCamelCase__    ,							input_tokens=UpperCamelCase__    ,							noise_time=t / self.scheduler.config.num_train_timesteps    ,							)

																												# Compute previous output: x_t -> x_t-1
																												lowerCamelCase__      :			Tuple 						= self.scheduler.step(UpperCamelCase__    ,							UpperCamelCase__    ,							UpperCamelCase__    ,							generator=UpperCamelCase__  ).prev_sample

																					lowerCamelCase__      :			Tuple 						= self.scale_to_features(UpperCamelCase__    ,							input_range=[-1.0, 1.0]  )
																					lowerCamelCase__      :			Optional[Any] 						= mel[:1]
																					lowerCamelCase__      :			int 						= mel.cpu().float().numpy()

																					lowerCamelCase__      :			Optional[Any] 						= np.concatenate([full_pred_mel, pred_mel[:1]]    ,							axis=1  )

																					# call the callback, if provided
																					if callback is not None and i % callback_steps == 0:
																												callback(UpperCamelCase__    ,							UpperCamelCase__  )

																					logger.info("""Generated segment"""    ,							UpperCamelCase__  )

														if output_type == "numpy" and not is_onnx_available():
																					raise ValueError(
																					    """Cannot return output in 'np' format if ONNX is not available. Make sure to have ONNX installed or set 'output_type' to 'mel'."""  )
														elif output_type == "numpy" and self.melgan is None:
																					raise ValueError(
																					    """Cannot return output in 'np' format if melgan component is not defined. Make sure to define `self.melgan` or set 'output_type' to 'mel'."""  )

														if output_type == "numpy":
																					lowerCamelCase__      :			Union[str, Any] 						= self.melgan(input_features=full_pred_mel.astype(np.floataa  )  )
														else:
																					lowerCamelCase__      :			str 						= full_pred_mel

														if not return_dict:
																					return (output,)

														return AudioPipelineOutput(audios=UpperCamelCase__  )









'''simple docstring'''







from __future__ import annotations

import os
import tempfile
import unittest

from transformers import ConvBertConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
			import tensorflow as tf

			from transformers import (
			    TFConvBertForMaskedLM,
			    TFConvBertForMultipleChoice,
			    TFConvBertForQuestionAnswering,
			    TFConvBertForSequenceClassification,
			    TFConvBertForTokenClassification,
			    TFConvBertModel,
			)

class _lowercase			:




							def __init__(							self:       int    ,							UpperCamelCase__:       Dict    ,							UpperCamelCase__:       List[str]=13    ,							UpperCamelCase__:       Union[str, Any]=7    ,							UpperCamelCase__:       Union[str, Any]=True    ,							UpperCamelCase__:       List[Any]=True    ,							UpperCamelCase__:       Optional[Any]=True    ,							UpperCamelCase__:       int=True    ,							UpperCamelCase__:       List[Any]=99    ,							UpperCamelCase__:       Tuple=32    ,							UpperCamelCase__:       List[str]=2    ,							UpperCamelCase__:       Optional[Any]=4    ,							UpperCamelCase__:       Optional[int]=37    ,							UpperCamelCase__:       Any="gelu"    ,							UpperCamelCase__:       Any=0.1    ,							UpperCamelCase__:       int=0.1    ,							UpperCamelCase__:       Optional[Any]=512    ,							UpperCamelCase__:       List[str]=16    ,							UpperCamelCase__:       Optional[int]=2    ,							UpperCamelCase__:       Dict=0.02    ,							UpperCamelCase__:       Tuple=3    ,							UpperCamelCase__:       Optional[int]=4    ,							UpperCamelCase__:       Union[str, Any]=None    ,							):
														lowerCamelCase__      :			Dict 						= parent
														lowerCamelCase__      :			Union[str, Any] 						= 13
														lowerCamelCase__      :			Any 						= 7
														lowerCamelCase__      :			int 						= True
														lowerCamelCase__      :			Optional[Any] 						= True
														lowerCamelCase__      :			Dict 						= True
														lowerCamelCase__      :			List[str] 						= True
														lowerCamelCase__      :			str 						= 99
														lowerCamelCase__      :			Dict 						= 384
														lowerCamelCase__      :			Optional[Any] 						= 2
														lowerCamelCase__      :			Optional[int] 						= 4
														lowerCamelCase__      :			Optional[Any] 						= 37
														lowerCamelCase__      :			Union[str, Any] 						= """gelu"""
														lowerCamelCase__      :			int 						= 0.1
														lowerCamelCase__      :			Optional[Any] 						= 0.1
														lowerCamelCase__      :			List[Any] 						= 512
														lowerCamelCase__      :			Optional[Any] 						= 16
														lowerCamelCase__      :			Any 						= 2
														lowerCamelCase__      :			Optional[Any] 						= 0.02
														lowerCamelCase__      :			int 						= 3
														lowerCamelCase__      :			List[str] 						= 4
														lowerCamelCase__      :			Any 						= 128
														lowerCamelCase__      :			List[Any] 						= 2
														lowerCamelCase__      :			Optional[Any] 						= 9
														lowerCamelCase__      :			Any 						= 1
														lowerCamelCase__      :			Optional[int] 						= None




							def 	lowerCamelCase_       (							self:       List[Any]  ):
														lowerCamelCase__      :			Tuple 						= ids_tensor([self.batch_size, self.seq_length]    ,							self.vocab_size  )

														lowerCamelCase__      :			str 						= None
														if self.use_input_mask:
																					lowerCamelCase__      :			int 						= random_attention_mask([self.batch_size, self.seq_length]  )

														lowerCamelCase__      :			List[str] 						= None
														if self.use_token_type_ids:
																					lowerCamelCase__      :			Optional[Any] 						= ids_tensor([self.batch_size, self.seq_length]    ,							self.type_vocab_size  )

														lowerCamelCase__      :			int 						= None
														lowerCamelCase__      :			Optional[Any] 						= None
														lowerCamelCase__      :			Optional[Any] 						= None
														if self.use_labels:
																					lowerCamelCase__      :			List[str] 						= ids_tensor([self.batch_size]    ,							self.type_sequence_label_size  )
																					lowerCamelCase__      :			Optional[Any] 						= ids_tensor([self.batch_size, self.seq_length]    ,							self.num_labels  )
																					lowerCamelCase__      :			Any 						= ids_tensor([self.batch_size]    ,							self.num_choices  )

														lowerCamelCase__      :			List[Any] 						= ConvBertConfig(
														    vocab_size=self.vocab_size    ,							hidden_size=self.hidden_size    ,							num_hidden_layers=self.num_hidden_layers    ,							num_attention_heads=self.num_attention_heads    ,							intermediate_size=self.intermediate_size    ,							hidden_act=self.hidden_act    ,							hidden_dropout_prob=self.hidden_dropout_prob    ,							attention_probs_dropout_prob=self.attention_probs_dropout_prob    ,							max_position_embeddings=self.max_position_embeddings    ,							type_vocab_size=self.type_vocab_size    ,							initializer_range=self.initializer_range    ,							return_dict=UpperCamelCase__    ,							)

														return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels




							def 	lowerCamelCase_       (							self:       Optional[int]    ,							UpperCamelCase__:       Dict    ,							UpperCamelCase__:       List[str]    ,							UpperCamelCase__:       Any    ,							UpperCamelCase__:       Any    ,							UpperCamelCase__:       Any    ,							UpperCamelCase__:       str    ,							UpperCamelCase__:       Any  ):
														lowerCamelCase__      :			List[Any] 						= TFConvBertModel(config=UpperCamelCase__  )
														lowerCamelCase__      :			str 						= {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}

														lowerCamelCase__      :			List[str] 						= [input_ids, input_mask]
														lowerCamelCase__      :			Optional[int] 						= model(UpperCamelCase__  )

														lowerCamelCase__      :			Optional[Any] 						= model(UpperCamelCase__  )

														self.parent.assertEqual(result.last_hidden_state.shape    ,							(self.batch_size, self.seq_length, self.hidden_size)  )




							def 	lowerCamelCase_       (							self:       Any    ,							UpperCamelCase__:       Tuple    ,							UpperCamelCase__:       List[Any]    ,							UpperCamelCase__:       List[Any]    ,							UpperCamelCase__:       List[str]    ,							UpperCamelCase__:       List[Any]    ,							UpperCamelCase__:       Union[str, Any]    ,							UpperCamelCase__:       Tuple  ):
														lowerCamelCase__      :			int 						= TFConvBertForMaskedLM(config=UpperCamelCase__  )
														lowerCamelCase__      :			Tuple 						= {
														    """input_ids""": input_ids,
														    """attention_mask""": input_mask,
														    """token_type_ids""": token_type_ids,
														}
														lowerCamelCase__      :			int 						= model(UpperCamelCase__  )
														self.parent.assertEqual(result.logits.shape    ,							(self.batch_size, self.seq_length, self.vocab_size)  )




							def 	lowerCamelCase_       (							self:       str    ,							UpperCamelCase__:       List[str]    ,							UpperCamelCase__:       Optional[int]    ,							UpperCamelCase__:       List[Any]    ,							UpperCamelCase__:       Optional[int]    ,							UpperCamelCase__:       Any    ,							UpperCamelCase__:       Any    ,							UpperCamelCase__:       Union[str, Any]  ):
														lowerCamelCase__      :			int 						= self.num_labels
														lowerCamelCase__      :			Dict 						= TFConvBertForSequenceClassification(config=UpperCamelCase__  )
														lowerCamelCase__      :			Dict 						= {
														    """input_ids""": input_ids,
														    """attention_mask""": input_mask,
														    """token_type_ids""": token_type_ids,
														}

														lowerCamelCase__      :			Optional[Any] 						= model(UpperCamelCase__  )
														self.parent.assertEqual(result.logits.shape    ,							(self.batch_size, self.num_labels)  )




							def 	lowerCamelCase_       (							self:       Optional[Any]    ,							UpperCamelCase__:       List[str]    ,							UpperCamelCase__:       Tuple    ,							UpperCamelCase__:       Optional[int]    ,							UpperCamelCase__:       List[str]    ,							UpperCamelCase__:       int    ,							UpperCamelCase__:       List[str]    ,							UpperCamelCase__:       Dict  ):
														lowerCamelCase__      :			Optional[int] 						= self.num_choices
														lowerCamelCase__      :			Dict 						= TFConvBertForMultipleChoice(config=UpperCamelCase__  )
														lowerCamelCase__      :			int 						= tf.tile(tf.expand_dims(UpperCamelCase__    ,							1  )    ,							(1, self.num_choices, 1)  )
														lowerCamelCase__      :			List[str] 						= tf.tile(tf.expand_dims(UpperCamelCase__    ,							1  )    ,							(1, self.num_choices, 1)  )
														lowerCamelCase__      :			Any 						= tf.tile(tf.expand_dims(UpperCamelCase__    ,							1  )    ,							(1, self.num_choices, 1)  )
														lowerCamelCase__      :			Tuple 						= {
														    """input_ids""": multiple_choice_inputs_ids,
														    """attention_mask""": multiple_choice_input_mask,
														    """token_type_ids""": multiple_choice_token_type_ids,
														}
														lowerCamelCase__      :			Optional[Any] 						= model(UpperCamelCase__  )
														self.parent.assertEqual(result.logits.shape    ,							(self.batch_size, self.num_choices)  )




							def 	lowerCamelCase_       (							self:       str    ,							UpperCamelCase__:       Any    ,							UpperCamelCase__:       Tuple    ,							UpperCamelCase__:       Optional[int]    ,							UpperCamelCase__:       Any    ,							UpperCamelCase__:       Optional[int]    ,							UpperCamelCase__:       str    ,							UpperCamelCase__:       int  ):
														lowerCamelCase__      :			List[Any] 						= self.num_labels
														lowerCamelCase__      :			List[str] 						= TFConvBertForTokenClassification(config=UpperCamelCase__  )
														lowerCamelCase__      :			Optional[Any] 						= {
														    """input_ids""": input_ids,
														    """attention_mask""": input_mask,
														    """token_type_ids""": token_type_ids,
														}
														lowerCamelCase__      :			Tuple 						= model(UpperCamelCase__  )
														self.parent.assertEqual(result.logits.shape    ,							(self.batch_size, self.seq_length, self.num_labels)  )




							def 	lowerCamelCase_       (							self:       Any    ,							UpperCamelCase__:       List[Any]    ,							UpperCamelCase__:       str    ,							UpperCamelCase__:       Tuple    ,							UpperCamelCase__:       Optional[int]    ,							UpperCamelCase__:       Optional[Any]    ,							UpperCamelCase__:       List[str]    ,							UpperCamelCase__:       Optional[Any]  ):
														lowerCamelCase__      :			Optional[int] 						= TFConvBertForQuestionAnswering(config=UpperCamelCase__  )
														lowerCamelCase__      :			int 						= {
														    """input_ids""": input_ids,
														    """attention_mask""": input_mask,
														    """token_type_ids""": token_type_ids,
														}

														lowerCamelCase__      :			Optional[int] 						= model(UpperCamelCase__  )
														self.parent.assertEqual(result.start_logits.shape    ,							(self.batch_size, self.seq_length)  )
														self.parent.assertEqual(result.end_logits.shape    ,							(self.batch_size, self.seq_length)  )




							def 	lowerCamelCase_       (							self:       Optional[Any]  ):
														lowerCamelCase__      :			str 						= self.prepare_config_and_inputs()
														(
														    (
														    lowerCamelCase__
														)						,	(
														    lowerCamelCase__
														)						,	(
														    lowerCamelCase__
														)						,	(
														    lowerCamelCase__
														)						,	(
														    lowerCamelCase__
														)						,	(
														    lowerCamelCase__
														)						,	(
														    lowerCamelCase__
														)						,	
														)      :			str 						= config_and_inputs
														lowerCamelCase__      :			Optional[Any] 						= {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
														return config, inputs_dict



@require_tf
class _lowercase			(       _lowercase       ,   _lowercase       ,   unittest.TestCase	):
							a          = (
							    (
							        TFConvBertModel,
							        TFConvBertForMaskedLM,
							        TFConvBertForQuestionAnswering,
							        TFConvBertForSequenceClassification,
							        TFConvBertForTokenClassification,
							        TFConvBertForMultipleChoice,
							    )
							    if is_tf_available()
							    else ()
							)
							a          = (
							    {
							        """feature-extraction""": TFConvBertModel,
							        """fill-mask""": TFConvBertForMaskedLM,
							        """question-answering""": TFConvBertForQuestionAnswering,
							        """text-classification""": TFConvBertForSequenceClassification,
							        """token-classification""": TFConvBertForTokenClassification,
							        """zero-shot""": TFConvBertForSequenceClassification,
							    }
							    if is_tf_available()
							    else {}
							)
							a          = False
							a          = False
							a          = False




							def 	lowerCamelCase_       (							self:       str  ):
														lowerCamelCase__      :			Dict 						= TFConvBertModelTester(self  )
														lowerCamelCase__      :			Dict 						= ConfigTester(self    ,							config_class=UpperCamelCase__    ,							hidden_size=37  )




							def 	lowerCamelCase_       (							self:       List[str]  ):
														self.config_tester.run_common_tests()




							def 	lowerCamelCase_       (							self:       Union[str, Any]  ):
														lowerCamelCase__      :			List[str] 						= self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_model(*UpperCamelCase__  )




							def 	lowerCamelCase_       (							self:       Any  ):
														lowerCamelCase__      :			Dict 						= self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_masked_lm(*UpperCamelCase__  )




							def 	lowerCamelCase_       (							self:       Tuple  ):
														lowerCamelCase__      :			int 						= self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_multiple_choice(*UpperCamelCase__  )




							def 	lowerCamelCase_       (							self:       Union[str, Any]  ):
														lowerCamelCase__      :			int 						= self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_question_answering(*UpperCamelCase__  )




							def 	lowerCamelCase_       (							self:       Any  ):
														lowerCamelCase__      :			Union[str, Any] 						= self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_sequence_classification(*UpperCamelCase__  )




							def 	lowerCamelCase_       (							self:       str  ):
														lowerCamelCase__      :			Tuple 						= self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_token_classification(*UpperCamelCase__  )




							@slow
							def 	lowerCamelCase_       (							self:       Optional[Any]  ):
														lowerCamelCase__						,	lowerCamelCase__      :			List[str] 						= self.model_tester.prepare_config_and_inputs_for_common()
														lowerCamelCase__      :			Dict 						= True
														lowerCamelCase__      :			Tuple 						= True

														if hasattr(UpperCamelCase__    ,							"""use_cache"""  ):
																					lowerCamelCase__      :			Union[str, Any] 						= True

														lowerCamelCase__      :			List[str] 						= getattr(self.model_tester    ,							"""encoder_seq_length"""    ,							self.model_tester.seq_length  )
														lowerCamelCase__      :			Tuple 						= getattr(self.model_tester    ,							"""key_length"""    ,							UpperCamelCase__  )

														for model_class in self.all_model_classes:
																					lowerCamelCase__      :			int 						= self._prepare_for_class(UpperCamelCase__    ,							UpperCamelCase__  )
																					lowerCamelCase__      :			List[Any] 						= model_class(UpperCamelCase__  )
																					lowerCamelCase__      :			Dict 						= len(model(UpperCamelCase__  )  )

																					with tempfile.TemporaryDirectory() as tmpdirname:
																												model.save_pretrained(UpperCamelCase__    ,							saved_model=UpperCamelCase__  )
																												lowerCamelCase__      :			int 						= os.path.join(UpperCamelCase__    ,							"""saved_model"""    ,							"""1"""  )
																												lowerCamelCase__      :			List[Any] 						= tf.keras.models.load_model(UpperCamelCase__  )
																												lowerCamelCase__      :			Any 						= model(UpperCamelCase__  )

																												if self.is_encoder_decoder:
																																			lowerCamelCase__      :			Dict 						= outputs["""encoder_hidden_states"""]
																																			lowerCamelCase__      :			Any 						= outputs["""encoder_attentions"""]
																												else:
																																			lowerCamelCase__      :			int 						= outputs["""hidden_states"""]
																																			lowerCamelCase__      :			Optional[int] 						= outputs["""attentions"""]

																												self.assertEqual(len(UpperCamelCase__  )    ,							UpperCamelCase__  )

																												lowerCamelCase__      :			Union[str, Any] 						= getattr(
																												    self.model_tester    ,							"""expected_num_hidden_layers"""    ,							self.model_tester.num_hidden_layers + 1  )

																												self.assertEqual(len(UpperCamelCase__  )    ,							UpperCamelCase__  )
																												self.assertListEqual(
																												    list(output_hidden_states[0].shape[-2:]  )    ,							[self.model_tester.seq_length, self.model_tester.hidden_size]    ,							)

																												self.assertEqual(len(UpperCamelCase__  )    ,							self.model_tester.num_hidden_layers  )
																												self.assertListEqual(
																												    list(output_attentions[0].shape[-3:]  )    ,							[self.model_tester.num_attention_heads / 2, encoder_seq_length, encoder_key_length]    ,							)




							@slow
							def 	lowerCamelCase_       (							self:       int  ):
														lowerCamelCase__      :			Union[str, Any] 						= TFConvBertModel.from_pretrained("""YituTech/conv-bert-base"""  )
														self.assertIsNotNone(UpperCamelCase__  )




							def 	lowerCamelCase_       (							self:       List[str]  ):
														lowerCamelCase__						,	lowerCamelCase__      :			Any 						= self.model_tester.prepare_config_and_inputs_for_common()
														lowerCamelCase__      :			Union[str, Any] 						= True
														lowerCamelCase__      :			int 						= getattr(self.model_tester    ,							"""decoder_seq_length"""    ,							self.model_tester.seq_length  )
														lowerCamelCase__      :			Any 						= getattr(self.model_tester    ,							"""encoder_seq_length"""    ,							self.model_tester.seq_length  )
														lowerCamelCase__      :			Optional[int] 						= getattr(self.model_tester    ,							"""key_length"""    ,							UpperCamelCase__  )
														lowerCamelCase__      :			List[Any] 						= getattr(self.model_tester    ,							"""key_length"""    ,							UpperCamelCase__  )

														def check_decoder_attentions_output(UpperCamelCase__:       Union[str, Any]  ):
																					lowerCamelCase__      :			List[Any] 						= len(UpperCamelCase__  )
																					self.assertEqual(out_len % 2    ,							0  )
																					lowerCamelCase__      :			Any 						= outputs.decoder_attentions
																					self.assertEqual(len(UpperCamelCase__  )    ,							self.model_tester.num_hidden_layers  )
																					self.assertListEqual(
																					    list(decoder_attentions[0].shape[-3:]  )    ,							[self.model_tester.num_attention_heads / 2, decoder_seq_length, decoder_key_length]    ,							)

														def check_encoder_attentions_output(UpperCamelCase__:       List[str]  ):
																					lowerCamelCase__      :			Any 						= [
																					    t.numpy() for t in (outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions)
																					]
																					self.assertEqual(len(UpperCamelCase__  )    ,							self.model_tester.num_hidden_layers  )
																					self.assertListEqual(
																					    list(attentions[0].shape[-3:]  )    ,							[self.model_tester.num_attention_heads / 2, encoder_seq_length, encoder_key_length]    ,							)

														for model_class in self.all_model_classes:
																					lowerCamelCase__      :			int 						= True
																					lowerCamelCase__      :			Any 						= False
																					lowerCamelCase__      :			Union[str, Any] 						= model_class(UpperCamelCase__  )
																					lowerCamelCase__      :			List[str] 						= model(self._prepare_for_class(UpperCamelCase__    ,							UpperCamelCase__  )  )
																					lowerCamelCase__      :			Optional[int] 						= len(UpperCamelCase__  )
																					self.assertEqual(config.output_hidden_states    ,							UpperCamelCase__  )
																					check_encoder_attentions_output(UpperCamelCase__  )

																					if self.is_encoder_decoder:
																												lowerCamelCase__      :			str 						= model_class(UpperCamelCase__  )
																												lowerCamelCase__      :			Tuple 						= model(self._prepare_for_class(UpperCamelCase__    ,							UpperCamelCase__  )  )
																												self.assertEqual(config.output_hidden_states    ,							UpperCamelCase__  )
																												check_decoder_attentions_output(UpperCamelCase__  )

																					# Check that output attentions can also be changed via the config
																					del inputs_dict["output_attentions"]
																					lowerCamelCase__      :			Optional[int] 						= True
																					lowerCamelCase__      :			Dict 						= model_class(UpperCamelCase__  )
																					lowerCamelCase__      :			Union[str, Any] 						= model(self._prepare_for_class(UpperCamelCase__    ,							UpperCamelCase__  )  )
																					self.assertEqual(config.output_hidden_states    ,							UpperCamelCase__  )
																					check_encoder_attentions_output(UpperCamelCase__  )

																					# Check attention is always last and order is fine
																					lowerCamelCase__      :			List[Any] 						= True
																					lowerCamelCase__      :			int 						= True
																					lowerCamelCase__      :			List[Any] 						= model_class(UpperCamelCase__  )
																					lowerCamelCase__      :			List[Any] 						= model(self._prepare_for_class(UpperCamelCase__    ,							UpperCamelCase__  )  )

																					self.assertEqual(out_len + (2 if self.is_encoder_decoder else 1)    ,							len(UpperCamelCase__  )  )
																					self.assertEqual(model.config.output_hidden_states    ,							UpperCamelCase__  )
																					check_encoder_attentions_output(UpperCamelCase__  )



@require_tf
class _lowercase			(       unittest.TestCase	):




							@slow
							def 	lowerCamelCase_       (							self:       List[str]  ):
														lowerCamelCase__      :			Dict 						= TFConvBertModel.from_pretrained("""YituTech/conv-bert-base"""  )
														lowerCamelCase__      :			Optional[Any] 						= tf.constant([[0, 1, 2, 3, 4, 5]]  )
														lowerCamelCase__      :			Optional[Any] 						= model(UpperCamelCase__  )[0]

														lowerCamelCase__      :			Dict 						= [1, 6, 768]
														self.assertEqual(output.shape    ,							UpperCamelCase__  )

														lowerCamelCase__      :			Dict 						= tf.constant(
														    [
														        [
														            [-0.03_475_493, -0.4_686_034, -0.30_638_832],
														            [0.22_637_248, -0.26_988_646, -0.7_423_424],
														            [0.10_324_868, -0.45_013_508, -0.58_280_784],
														        ]
														    ]  )
														tf.debugging.assert_near(output[:, :3, :3]    ,							UpperCamelCase__    ,							atol=1e-4  )

