

import argparse
import json
import os

import fairseq
import torch
from fairseq.data import Dictionary

from transformers import (
    UniSpeechConfig,
    UniSpeechForCTC,
    UniSpeechForPreTraining,
    WavaVecaFeatureExtractor,
    WavaVecaPhonemeCTCTokenizer,
    WavaVecaProcessor,
    logging,
)


logging.set_verbosity_info()
lowercase			:				str   =     logging.get_logger(__name__)

lowercase			:				Optional[int]   =     {
    "post_extract_proj": "feature_projection.projection",
    "encoder.pos_conv.0": "encoder.pos_conv_embed.conv",
    "self_attn.k_proj": "encoder.layers.*.attention.k_proj",
    "self_attn.v_proj": "encoder.layers.*.attention.v_proj",
    "self_attn.q_proj": "encoder.layers.*.attention.q_proj",
    "self_attn.out_proj": "encoder.layers.*.attention.out_proj",
    "self_attn_layer_norm": "encoder.layers.*.layer_norm",
    "fc1": "encoder.layers.*.feed_forward.intermediate_dense",
    "fc2": "encoder.layers.*.feed_forward.output_dense",
    "final_layer_norm": "encoder.layers.*.final_layer_norm",
    "encoder.layer_norm": "encoder.layer_norm",
    "w2v_model.layer_norm": "feature_projection.layer_norm",
    "quantizer.weight_proj": "quantizer.weight_proj",
    "quantizer.vars": "quantizer.codevectors",
    "project_q": "project_q",
    "final_proj": "project_hid",
    "w2v_encoder.proj": "ctc_proj",
    "mask_emb": "masked_spec_embed",
}
lowercase			:				Tuple   =     [
    "ctc_proj",
    "quantizer.weight_proj",
    "quantizer.codevectors",
    "project_q",
    "project_hid",
]






def        snake_case__		(    lowerCamelCase_     ,				lowerCamelCase_     ,				lowerCamelCase_     ,				lowerCamelCase_     ,				lowerCamelCase_     ,				lowerCamelCase_		):
							for attribute in key.split('''.'''		):
														if is_finetuned:
																					if attribute in ["quantizer", "project_q", "project_hid"]:
																												# those layers are only relevant for pretraining and should be dropped
																												return

																					if attribute == "ctc_proj":
																												# we should rename `ctc_proj` to `lm_head` for fine-tuned phoneme models
																												A      :					Union[str, Any]      		=					'''lm_head'''

														A      :					Dict      		=					getattr(lowerCamelCase_     ,				lowerCamelCase_		)

							if weight_type is not None:
														A      :					Any      		=					getattr(lowerCamelCase_     ,				lowerCamelCase_		).shape
							else:
														A      :					Any      		=					hf_pointer.shape

							assert hf_shape == value.shape, (
							    F'Shape of hf {key + "." + weight_type if weight_type is not None else ""} is {hf_shape}, but should be'
							    F' {value.shape} for {full_name}'
							)

							if weight_type == "weight":
														A      :					Dict      		=					value
							elif weight_type == "weight_g":
														A      :					Tuple      		=					value
							elif weight_type == "weight_v":
														A      :					List[Any]      		=					value
							elif weight_type == "bias":
														A      :					int      		=					value
							else:
														A      :					Optional[Any]      		=					value

							logger.info(F'{key + "." + weight_type if weight_type is not None else ""} was initialized from {full_name}.'		)






def        snake_case__		(    lowerCamelCase_     ,				lowerCamelCase_     ,				lowerCamelCase_		):
							A      :					List[str]      		=					[]
							A      :					Tuple      		=					fairseq_model.state_dict()

							A      :					List[str]      		=					hf_model.unispeech.feature_extractor

							for name, value in fairseq_dict.items():
														A      :					Union[str, Any]      		=					False
														if "conv_layers" in name:
																					load_conv_layer(
																					    lowerCamelCase_     ,				lowerCamelCase_     ,				lowerCamelCase_     ,				lowerCamelCase_     ,				hf_model.config.feat_extract_norm == '''group'''     ,				)
																					A      :					int      		=					True
														else:
																					for key, mapped_key in MAPPING.items():
																												A      :					Tuple      		=					'''unispeech.''' + mapped_key if mapped_key not in TOP_LEVEL_KEYS else mapped_key
																												if key in name or key.split('''w2v_model.'''		)[-1] == name.split('''.'''		)[0]:
																																			A      :					Dict      		=					True
																																			if "*" in mapped_key:
																																										A      :					List[Any]      		=					name.split(lowerCamelCase_		)[0].split('''.'''		)[-2]
																																										A      :					List[Any]      		=					mapped_key.replace('''*'''     ,				lowerCamelCase_		)
																																			if "weight_g" in name:
																																										A      :					List[str]      		=					'''weight_g'''
																																			elif "weight_v" in name:
																																										A      :					Optional[int]      		=					'''weight_v'''
																																			elif "bias" in name:
																																										A      :					List[Any]      		=					'''bias'''
																																			elif "weight" in name:
																																										# TODO: don't match quantizer.weight_proj
																																										A      :					Union[str, Any]      		=					'''weight'''
																																			else:
																																										A      :					Optional[int]      		=					None
																																			set_recursively(lowerCamelCase_     ,				lowerCamelCase_     ,				lowerCamelCase_     ,				lowerCamelCase_     ,				lowerCamelCase_     ,				lowerCamelCase_		)
																												continue
														if not is_used:
																					unused_weights.append(lowerCamelCase_		)

							logger.warning(F'Unused weights: {unused_weights}'		)






def        snake_case__		(    lowerCamelCase_     ,				lowerCamelCase_     ,				lowerCamelCase_     ,				lowerCamelCase_     ,				lowerCamelCase_		):
							A      :					Optional[int]      		=					full_name.split('''conv_layers.'''		)[-1]
							A      :					int      		=					name.split('''.'''		)
							A      :					Any      		=					int(items[0]		)
							A      :					Tuple      		=					int(items[1]		)

							if type_id == 0:
														if "bias" in name:
																					assert value.shape == feature_extractor.conv_layers[layer_id].conv.bias.data.shape, (
																					    F'{full_name} has size {value.shape}, but'
																					    F' {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found.'
																					)
																					A      :					Any      		=					value
																					logger.info(F'Feat extract conv layer {layer_id} was initialized from {full_name}.'		)
														elif "weight" in name:
																					assert value.shape == feature_extractor.conv_layers[layer_id].conv.weight.data.shape, (
																					    F'{full_name} has size {value.shape}, but'
																					    F' {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found.'
																					)
																					A      :					List[Any]      		=					value
																					logger.info(F'Feat extract conv layer {layer_id} was initialized from {full_name}.'		)
							elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm):
														if "bias" in name:
																					assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape, (
																					    F'{full_name} has size {value.shape}, but {feature_extractor[layer_id].layer_norm.bias.data.shape} was'
																					    " found."
																					)
																					A      :					Tuple      		=					value
																					logger.info(F'Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.'		)
														elif "weight" in name:
																					assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape, (
																					    F'{full_name} has size {value.shape}, but'
																					    F' {feature_extractor[layer_id].layer_norm.weight.data.shape} was found.'
																					)
																					A      :					List[Any]      		=					value
																					logger.info(F'Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.'		)
							else:
														unused_weights.append(lowerCamelCase_		)






@torch.no_grad()
def        snake_case__		(    lowerCamelCase_     ,				lowerCamelCase_     ,				lowerCamelCase_=None     ,				lowerCamelCase_=None     ,				lowerCamelCase_=True		):
							if config_path is not None:
														A      :					Dict      		=					UniSpeechConfig.from_pretrained(lowerCamelCase_		)
							else:
														A      :					Union[str, Any]      		=					UniSpeechConfig()

							if is_finetuned:
														if dict_path:
																					A      :					List[Any]      		=					Dictionary.load_from_json(lowerCamelCase_		)

																					# important change bos & pad token id since CTC symbol is <pad> and
																					# not <s> as in fairseq
																					A      :					Tuple      		=					target_dict.pad_index
																					A      :					Tuple      		=					target_dict.bos_index
																					A      :					int      		=					target_dict.eos_index
																					A      :					int      		=					len(target_dict.symbols		)
																					A      :					Optional[int]      		=					os.path.join(lowerCamelCase_     ,				'''vocab.json'''		)
																					if not os.path.isdir(lowerCamelCase_		):
																												logger.error('''--pytorch_dump_folder_path ({}) should be a directory'''.format(lowerCamelCase_		)		)
																												return
																					os.makedirs(lowerCamelCase_     ,				exist_ok=lowerCamelCase_		)
																					A      :					Dict      		=					target_dict.indices

																					# fairseq has the <pad> and <s> switched
																					A      :					Any      		=					42
																					A      :					List[str]      		=					43
																					with open(lowerCamelCase_     ,				'''w'''     ,				encoding='''utf-8'''		) as vocab_handle:
																												json.dump(lowerCamelCase_     ,				lowerCamelCase_		)
																					A      :					Tuple      		=					WavaVecaPhonemeCTCTokenizer(
																					    lowerCamelCase_     ,				unk_token=target_dict.unk_word     ,				pad_token=target_dict.pad_word     ,				bos_token=target_dict.bos_word     ,				eos_token=target_dict.eos_word     ,				word_delimiter_token='''|'''     ,				do_lower_case=lowerCamelCase_     ,				)
																					A      :					Dict      		=					True if config.feat_extract_norm == '''layer''' else False
																					A      :					Optional[int]      		=					WavaVecaFeatureExtractor(
																					    feature_size=1     ,				sampling_rate=16000     ,				padding_value=0     ,				do_normalize=lowerCamelCase_     ,				return_attention_mask=lowerCamelCase_     ,				)
																					A      :					List[str]      		=					WavaVecaProcessor(feature_extractor=lowerCamelCase_     ,				tokenizer=lowerCamelCase_		)
																					processor.save_pretrained(lowerCamelCase_		)

														A      :					Dict      		=					UniSpeechForCTC(lowerCamelCase_		)
							else:
														A      :					Tuple      		=					UniSpeechForPreTraining(lowerCamelCase_		)

							if is_finetuned:
														A		,		A		,		A      :					str      		=					fairseq.checkpoint_utils.load_model_ensemble_and_task(
														    [checkpoint_path]     ,				arg_overrides={'''data''': '''/'''.join(dict_path.split('''/'''		)[:-1]		), '''w2v_path''': checkpoint_path}		)
							else:
														A		,		A		,		A      :					Dict      		=					fairseq.checkpoint_utils.load_model_ensemble_and_task([checkpoint_path]		)

							A      :					List[Any]      		=					model[0].eval()

							recursively_load_weights(lowerCamelCase_     ,				lowerCamelCase_     ,				lowerCamelCase_		)

							hf_unispeech.save_pretrained(lowerCamelCase_		)


if __name__ == "__main__":
		lowercase			:				Optional[int]   =     argparse.ArgumentParser()
		parser.add_argument("--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model.")
		parser.add_argument("--checkpoint_path", default=None, type=str, help="Path to fairseq checkpoint")
		parser.add_argument("--dict_path", default=None, type=str, help="Path to dict of fine-tuned model")
		parser.add_argument("--config_path", default=None, type=str, help="Path to hf config.json of model to convert")
		parser.add_argument(
		    "--not_finetuned", action="store_true", help="Whether the model to convert is a fine-tuned model or not"
		)
		lowercase			:				Any   =     parser.parse_args()
		convert_unispeech_checkpoint(
		    args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.dict_path, not args.not_finetuned
		)









from __future__ import annotations

import unittest

from transformers import FunnelConfig, is_tf_available
from transformers.testing_utils import require_tf

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 (
		    TFFunnelBaseModel,
		    TFFunnelForMaskedLM,
		    TFFunnelForMultipleChoice,
		    TFFunnelForPreTraining,
		    TFFunnelForQuestionAnswering,
		    TFFunnelForSequenceClassification,
		    TFFunnelForTokenClassification,
		    TFFunnelModel,
		)







class 					__lowercase  :





							"""simple docstring"""

							def __init__(					self    ,						__UpperCAmelCase    ,						__UpperCAmelCase=13    ,						__UpperCAmelCase=7    ,						__UpperCAmelCase=True    ,						__UpperCAmelCase=True    ,						__UpperCAmelCase=True    ,						__UpperCAmelCase=True    ,						__UpperCAmelCase=99    ,						__UpperCAmelCase=[1, 1, 2]    ,						__UpperCAmelCase=1    ,						__UpperCAmelCase=32    ,						__UpperCAmelCase=4    ,						__UpperCAmelCase=8    ,						__UpperCAmelCase=37    ,						__UpperCAmelCase="gelu_new"    ,						__UpperCAmelCase=0.1    ,						__UpperCAmelCase=0.1    ,						__UpperCAmelCase=0.0    ,						__UpperCAmelCase=5_12    ,						__UpperCAmelCase=3    ,						__UpperCAmelCase=0.0_2    ,						__UpperCAmelCase=3    ,						__UpperCAmelCase=4    ,						__UpperCAmelCase=None    ,						__UpperCAmelCase=False    ,						)		->       Tuple:
														A      :					Optional[int]      		=					parent
														A      :					int      		=					batch_size
														A      :					Union[str, Any]      		=					seq_length
														A      :					Optional[Any]      		=					is_training
														A      :					int      		=					use_input_mask
														A      :					List[Any]      		=					use_token_type_ids
														A      :					Optional[int]      		=					use_labels
														A      :					List[Any]      		=					vocab_size
														A      :					int      		=					block_sizes
														A      :					Optional[int]      		=					num_decoder_layers
														A      :					int      		=					d_model
														A      :					Dict      		=					n_head
														A      :					Tuple      		=					d_head
														A      :					int      		=					d_inner
														A      :					Tuple      		=					hidden_act
														A      :					Tuple      		=					hidden_dropout
														A      :					Any      		=					attention_dropout
														A      :					Any      		=					activation_dropout
														A      :					List[Any]      		=					max_position_embeddings
														A      :					int      		=					type_vocab_size
														A      :					Dict      		=					2
														A      :					List[str]      		=					num_labels
														A      :					Optional[int]      		=					num_choices
														A      :					int      		=					scope
														A      :					int      		=					initializer_std

														# Used in the tests to check the size of the first attention layer
														A      :					Optional[int]      		=					n_head
														# Used in the tests to check the size of the first hidden state
														A      :					List[Any]      		=					self.d_model
														# Used in the tests to check the number of output hidden states/attentions
														A      :					Dict      		=					sum(self.block_sizes						) + (0 if base else self.num_decoder_layers)
														# FunnelModel adds two hidden layers: input embeddings and the sum of the upsampled encoder hidden state with
														# the last hidden state of the first block (which is the first hidden state of the decoder).
														if not base:
																					A      :					Union[str, Any]      		=					self.num_hidden_layers + 2




							def       snake_case			(					self						)		->       List[Any]:
														A      :					int      		=					ids_tensor([self.batch_size, self.seq_length]    ,						self.vocab_size						)

														A      :					Any      		=					None
														if self.use_input_mask:
																					A      :					List[Any]      		=					random_attention_mask([self.batch_size, self.seq_length]						)

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

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

														A      :					List[str]      		=					FunnelConfig(
														    vocab_size=self.vocab_size    ,						block_sizes=self.block_sizes    ,						num_decoder_layers=self.num_decoder_layers    ,						d_model=self.d_model    ,						n_head=self.n_head    ,						d_head=self.d_head    ,						d_inner=self.d_inner    ,						hidden_act=self.hidden_act    ,						hidden_dropout=self.hidden_dropout    ,						attention_dropout=self.attention_dropout    ,						activation_dropout=self.activation_dropout    ,						max_position_embeddings=self.max_position_embeddings    ,						type_vocab_size=self.type_vocab_size    ,						initializer_std=self.initializer_std    ,						)

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




							def       snake_case			(					self    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						)		->       Optional[int]:
														A      :					str      		=					TFFunnelModel(config=__UpperCAmelCase						)
														A      :					str      		=					{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
														A      :					Tuple      		=					model(__UpperCAmelCase						)

														A      :					str      		=					[input_ids, input_mask]
														A      :					Optional[Any]      		=					model(__UpperCAmelCase						)

														A      :					List[str]      		=					model(__UpperCAmelCase						)
														self.parent.assertEqual(result.last_hidden_state.shape    ,						(self.batch_size, self.seq_length, self.d_model)						)

														A      :					str      		=					False
														A      :					List[str]      		=					TFFunnelModel(config=__UpperCAmelCase						)
														A      :					str      		=					model(__UpperCAmelCase						)
														self.parent.assertEqual(result.last_hidden_state.shape    ,						(self.batch_size, self.seq_length, self.d_model)						)

														A      :					Union[str, Any]      		=					False
														A      :					Tuple      		=					TFFunnelModel(config=__UpperCAmelCase						)
														A      :					Any      		=					model(__UpperCAmelCase						)
														self.parent.assertEqual(result.last_hidden_state.shape    ,						(self.batch_size, self.seq_length, self.d_model)						)




							def       snake_case			(					self    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						)		->       Union[str, Any]:
														A      :					Dict      		=					TFFunnelBaseModel(config=__UpperCAmelCase						)
														A      :					Optional[int]      		=					{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
														A      :					List[str]      		=					model(__UpperCAmelCase						)

														A      :					Union[str, Any]      		=					[input_ids, input_mask]
														A      :					Optional[Any]      		=					model(__UpperCAmelCase						)

														A      :					Union[str, Any]      		=					model(__UpperCAmelCase						)
														self.parent.assertEqual(result.last_hidden_state.shape    ,						(self.batch_size, 2, self.d_model)						)

														A      :					List[Any]      		=					False
														A      :					Union[str, Any]      		=					TFFunnelBaseModel(config=__UpperCAmelCase						)
														A      :					Union[str, Any]      		=					model(__UpperCAmelCase						)
														self.parent.assertEqual(result.last_hidden_state.shape    ,						(self.batch_size, 3, self.d_model)						)

														A      :					int      		=					False
														A      :					Tuple      		=					TFFunnelBaseModel(config=__UpperCAmelCase						)
														A      :					Tuple      		=					model(__UpperCAmelCase						)
														self.parent.assertEqual(result.last_hidden_state.shape    ,						(self.batch_size, 2, self.d_model)						)




							def       snake_case			(					self    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						)		->       List[Any]:
														A      :					List[str]      		=					TFFunnelForPreTraining(config=__UpperCAmelCase						)
														A      :					Any      		=					{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
														A      :					Optional[Any]      		=					model(__UpperCAmelCase						)
														self.parent.assertEqual(result.logits.shape    ,						(self.batch_size, self.seq_length)						)




							def       snake_case			(					self    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						)		->       List[Any]:
														A      :					str      		=					TFFunnelForMaskedLM(config=__UpperCAmelCase						)
														A      :					int      		=					{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
														A      :					str      		=					model(__UpperCAmelCase						)
														self.parent.assertEqual(result.logits.shape    ,						(self.batch_size, self.seq_length, self.vocab_size)						)




							def       snake_case			(					self    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						)		->       Union[str, Any]:
														A      :					Tuple      		=					self.num_labels
														A      :					List[Any]      		=					TFFunnelForSequenceClassification(config=__UpperCAmelCase						)
														A      :					List[str]      		=					{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
														A      :					Any      		=					model(__UpperCAmelCase						)
														self.parent.assertEqual(result.logits.shape    ,						(self.batch_size, self.num_labels)						)




							def       snake_case			(					self    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						)		->       Dict:
														A      :					Union[str, Any]      		=					self.num_choices
														A      :					Tuple      		=					TFFunnelForMultipleChoice(config=__UpperCAmelCase						)
														A      :					Tuple      		=					tf.tile(tf.expand_dims(__UpperCAmelCase    ,						1						)    ,						(1, self.num_choices, 1)						)
														A      :					List[Any]      		=					tf.tile(tf.expand_dims(__UpperCAmelCase    ,						1						)    ,						(1, self.num_choices, 1)						)
														A      :					Any      		=					tf.tile(tf.expand_dims(__UpperCAmelCase    ,						1						)    ,						(1, self.num_choices, 1)						)
														A      :					str      		=					{
														    '''input_ids''': multiple_choice_inputs_ids,
														    '''attention_mask''': multiple_choice_input_mask,
														    '''token_type_ids''': multiple_choice_token_type_ids,
														}
														A      :					Any      		=					model(__UpperCAmelCase						)
														self.parent.assertEqual(result.logits.shape    ,						(self.batch_size, self.num_choices)						)




							def       snake_case			(					self    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						)		->       str:
														A      :					Optional[Any]      		=					self.num_labels
														A      :					List[str]      		=					TFFunnelForTokenClassification(config=__UpperCAmelCase						)
														A      :					int      		=					{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
														A      :					Optional[Any]      		=					model(__UpperCAmelCase						)
														self.parent.assertEqual(result.logits.shape    ,						(self.batch_size, self.seq_length, self.num_labels)						)




							def       snake_case			(					self    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						)		->       Any:
														A      :					Dict      		=					TFFunnelForQuestionAnswering(config=__UpperCAmelCase						)
														A      :					str      		=					{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
														A      :					List[Any]      		=					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       snake_case			(					self						)		->       str:
														A      :					Union[str, Any]      		=					self.prepare_config_and_inputs()
														(
														    (
														    A
														)		,		(
														    A
														)		,		(
														    A
														)		,		(
														    A
														)		,		(
														    A
														)		,		(
														    A
														)		,		(
														    A
														)		,		
														)      :					Optional[Any]      		=					config_and_inputs
														A      :					Union[str, Any]      		=					{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
														return config, inputs_dict







@require_tf
class 					__lowercase  (						_SCREAMING_SNAKE_CASE		,       _SCREAMING_SNAKE_CASE		,       unittest.TestCase				):





							"""simple docstring"""

							UpperCAmelCase_						:		Any								=	(
							    (
							        TFFunnelModel,
							        TFFunnelForMaskedLM,
							        TFFunnelForPreTraining,
							        TFFunnelForQuestionAnswering,
							        TFFunnelForTokenClassification,
							    )
							    if is_tf_available()
							    else ()
							)
							UpperCAmelCase_						:		Optional[Any]								=	(
							    {
							        '''feature-extraction''': (TFFunnelBaseModel, TFFunnelModel),
							        '''fill-mask''': TFFunnelForMaskedLM,
							        '''question-answering''': TFFunnelForQuestionAnswering,
							        '''text-classification''': TFFunnelForSequenceClassification,
							        '''token-classification''': TFFunnelForTokenClassification,
							        '''zero-shot''': TFFunnelForSequenceClassification,
							    }
							    if is_tf_available()
							    else {}
							)
							UpperCAmelCase_						:		Optional[Any]								=	False
							UpperCAmelCase_						:		Optional[Any]								=	False




							def       snake_case			(					self						)		->       str:
														A      :					Dict      		=					TFFunnelModelTester(self						)
														A      :					int      		=					ConfigTester(self    ,						config_class=__UpperCAmelCase						)




							def       snake_case			(					self						)		->       Tuple:
														self.config_tester.run_common_tests()




							def       snake_case			(					self						)		->       List[str]:
														A      :					Tuple      		=					self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_model(*__UpperCAmelCase						)




							def       snake_case			(					self						)		->       Union[str, Any]:
														A      :					Optional[Any]      		=					self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_pretraining(*__UpperCAmelCase						)




							def       snake_case			(					self						)		->       Optional[Any]:
														A      :					List[Any]      		=					self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_masked_lm(*__UpperCAmelCase						)




							def       snake_case			(					self						)		->       Optional[Any]:
														A      :					List[Any]      		=					self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_token_classification(*__UpperCAmelCase						)






							def       snake_case			(					self						)		->       Optional[int]:
														A      :					List[str]      		=					self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_question_answering(*__UpperCAmelCase						)







@require_tf
class 					__lowercase  (						_SCREAMING_SNAKE_CASE		,       unittest.TestCase				):





							"""simple docstring"""

							UpperCAmelCase_						:		Any								=	(
							    (TFFunnelBaseModel, TFFunnelForMultipleChoice, TFFunnelForSequenceClassification) if is_tf_available() else ()
							)
							UpperCAmelCase_						:		str								=	False
							UpperCAmelCase_						:		Union[str, Any]								=	False




							def       snake_case			(					self						)		->       int:
														A      :					Optional[int]      		=					TFFunnelModelTester(self    ,						base=__UpperCAmelCase						)
														A      :					Optional[Any]      		=					ConfigTester(self    ,						config_class=__UpperCAmelCase						)




							def       snake_case			(					self						)		->       str:
														self.config_tester.run_common_tests()




							def       snake_case			(					self						)		->       Tuple:
														A      :					Optional[Any]      		=					self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_base_model(*__UpperCAmelCase						)




							def       snake_case			(					self						)		->       int:
														A      :					Union[str, Any]      		=					self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_sequence_classification(*__UpperCAmelCase						)






							def       snake_case			(					self						)		->       Tuple:
														A      :					List[str]      		=					self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_multiple_choice(*__UpperCAmelCase						)






