





import unittest

from transformers import (
    MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
    TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
    Pipeline,
    ZeroShotClassificationPipeline,
    pipeline,
)
from transformers.testing_utils import is_pipeline_test, nested_simplify, require_tf, require_torch, slow

from .test_pipelines_common import ANY


# These 2 model types require different inputs than those of the usual text models.
_SCREAMING_SNAKE_CASE      	=		{"""LayoutLMv2Config""", """LayoutLMv3Config"""}
@is_pipeline_test
class SCREAMING_SNAKE_CASE_						(					unittest.TestCase					):

						'''simple docstring'''





						__magic_name__:			Optional[int]       =       MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING
						__magic_name__:			List[str]       =       TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING

						if model_mapping is not None:
												__magic_name__:			str       =       {config: model for config, model in model_mapping.items() if config.__name__ not in _TO_SKIP}
						if tf_model_mapping is not None:
												__magic_name__:			str       =       {
												    config: model for config, model in tf_model_mapping.items() if config.__name__ not in _TO_SKIP
												}






						def UpperCAmelCase_				(      self      :      int		,   _A      :      str		,   _A      :      Dict		,   _A      :      Optional[Any]	)							->    int:







							"""simple docstring"""


							snake_case_		:			Optional[Any]											=	ZeroShotClassificationPipeline(
							    model=_A		,   tokenizer=_A		,   candidate_labels=['polics', 'health']	)
							return classifier, ["Who are you voting for in 2020?", "My stomach hurts."]






						def UpperCAmelCase_				(      self      :      str		,   _A      :      Dict		,   _A      :      Tuple	)							->    Tuple:







							"""simple docstring"""


							snake_case_		:			Optional[int]											=	classifier('Who are you voting for in 2020?'		,   candidate_labels='politics'	)
							self.assertEqual(_A		,   {'sequence': ANY(_A	), 'labels': [ANY(_A	)], 'scores': [ANY(_A	)]}	)

							# No kwarg
							snake_case_		:			int											=	classifier('Who are you voting for in 2020?'		,   ['politics']	)
							self.assertEqual(_A		,   {'sequence': ANY(_A	), 'labels': [ANY(_A	)], 'scores': [ANY(_A	)]}	)

							snake_case_		:			Optional[int]											=	classifier('Who are you voting for in 2020?'		,   candidate_labels=['politics']	)
							self.assertEqual(_A		,   {'sequence': ANY(_A	), 'labels': [ANY(_A	)], 'scores': [ANY(_A	)]}	)

							snake_case_		:			str											=	classifier('Who are you voting for in 2020?'		,   candidate_labels='politics, public health'	)
							self.assertEqual(
							    _A		,   {'sequence': ANY(_A	), 'labels': [ANY(_A	), ANY(_A	)], 'scores': [ANY(_A	), ANY(_A	)]}	)
							self.assertAlmostEqual(sum(nested_simplify(outputs['scores']	)	)		,   1.0	)

							snake_case_		:			Optional[Any]											=	classifier('Who are you voting for in 2020?'		,   candidate_labels=['politics', 'public health']	)
							self.assertEqual(
							    _A		,   {'sequence': ANY(_A	), 'labels': [ANY(_A	), ANY(_A	)], 'scores': [ANY(_A	), ANY(_A	)]}	)
							self.assertAlmostEqual(sum(nested_simplify(outputs['scores']	)	)		,   1.0	)

							snake_case_		:			List[str]											=	classifier(
							    'Who are you voting for in 2020?'		,   candidate_labels='politics'		,   hypothesis_template='This text is about {}'	)
							self.assertEqual(_A		,   {'sequence': ANY(_A	), 'labels': [ANY(_A	)], 'scores': [ANY(_A	)]}	)

							# https://github.com/huggingface/transformers/issues/13846
							snake_case_		:			Any											=	classifier(['I am happy']		,   ['positive', 'negative']	)
							self.assertEqual(
							    _A		,   [
							        {'sequence': ANY(_A	), 'labels': [ANY(_A	), ANY(_A	)], 'scores': [ANY(_A	), ANY(_A	)]}
							        for i in range(1	)
							    ]		,   )
							snake_case_		:			int											=	classifier(['I am happy', 'I am sad']		,   ['positive', 'negative']	)
							self.assertEqual(
							    _A		,   [
							        {'sequence': ANY(_A	), 'labels': [ANY(_A	), ANY(_A	)], 'scores': [ANY(_A	), ANY(_A	)]}
							        for i in range(2	)
							    ]		,   )

							with self.assertRaises(_A	):
								classifier(''		,   candidate_labels='politics'	)

							with self.assertRaises(_A	):
								classifier(_A		,   candidate_labels='politics'	)

							with self.assertRaises(_A	):
								classifier('Who are you voting for in 2020?'		,   candidate_labels=''	)

							with self.assertRaises(_A	):
								classifier('Who are you voting for in 2020?'		,   candidate_labels=_A	)

							with self.assertRaises(_A	):
								classifier(
								    'Who are you voting for in 2020?'		,   candidate_labels='politics'		,   hypothesis_template='Not formatting template'		,   )

							with self.assertRaises(_A	):
								classifier(
								    'Who are you voting for in 2020?'		,   candidate_labels='politics'		,   hypothesis_template=_A		,   )

							self.run_entailment_id(_A	)






						def UpperCAmelCase_				(      self      :      str		,   _A      :      Pipeline	)							->    str:







							"""simple docstring"""


							snake_case_		:			int											=	zero_shot_classifier.model.config
							snake_case_		:			Union[str, Any]											=	config.labelaid
							snake_case_		:			Dict											=	zero_shot_classifier.entailment_id

							snake_case_		:			Optional[Any]											=	{'LABEL_0': 0, 'LABEL_1': 1, 'LABEL_2': 2}
							self.assertEqual(zero_shot_classifier.entailment_id		,   -1	)

							snake_case_		:			List[str]											=	{'entailment': 0, 'neutral': 1, 'contradiction': 2}
							self.assertEqual(zero_shot_classifier.entailment_id		,   0	)

							snake_case_		:			Any											=	{'ENTAIL': 0, 'NON-ENTAIL': 1}
							self.assertEqual(zero_shot_classifier.entailment_id		,   0	)

							snake_case_		:			List[str]											=	{'ENTAIL': 2, 'NEUTRAL': 1, 'CONTR': 0}
							self.assertEqual(zero_shot_classifier.entailment_id		,   2	)

							snake_case_		:			Optional[int]											=	original_labelaid
							self.assertEqual(_A		,   zero_shot_classifier.entailment_id	)






						@require_torch
						def UpperCAmelCase_				(      self      :      int	)							->    List[str]:







							"""simple docstring"""


							snake_case_		:			Dict											=	pipeline(
							    'zero-shot-classification'		,   model='sshleifer/tiny-distilbert-base-cased-distilled-squad'		,   framework='pt'		,   )
							# There was a regression in 4.10 for this
							# Adding a test so we don't make the mistake again.
							# https://github.com/huggingface/transformers/issues/13381#issuecomment-912343499
							zero_shot_classifier(
							    'Who are you voting for in 2020?' * 100		,   candidate_labels=['politics', 'public health', 'science']	)






						@require_torch
						def UpperCAmelCase_				(      self      :      Optional[int]	)							->    Dict:







							"""simple docstring"""


							snake_case_		:			List[str]											=	pipeline(
							    'zero-shot-classification'		,   model='sshleifer/tiny-distilbert-base-cased-distilled-squad'		,   framework='pt'		,   )
							snake_case_		:			Optional[Any]											=	zero_shot_classifier(
							    'Who are you voting for in 2020?'		,   candidate_labels=['politics', 'public health', 'science']	)

							self.assertEqual(
							    nested_simplify(_A	)		,   {
							        'sequence': 'Who are you voting for in 2020?',
							        'labels': ['science', 'public health', 'politics'],
							        'scores': [0.3_3_3, 0.3_3_3, 0.3_3_3],
							    }		,   )






						@require_tf
						def UpperCAmelCase_				(      self      :      List[str]	)							->    Tuple:







							"""simple docstring"""


							snake_case_		:			Optional[int]											=	pipeline(
							    'zero-shot-classification'		,   model='sshleifer/tiny-distilbert-base-cased-distilled-squad'		,   framework='tf'		,   )
							snake_case_		:			Optional[Any]											=	zero_shot_classifier(
							    'Who are you voting for in 2020?'		,   candidate_labels=['politics', 'public health', 'science']	)

							self.assertEqual(
							    nested_simplify(_A	)		,   {
							        'sequence': 'Who are you voting for in 2020?',
							        'labels': ['science', 'public health', 'politics'],
							        'scores': [0.3_3_3, 0.3_3_3, 0.3_3_3],
							    }		,   )






						@slow
						@require_torch
						def UpperCAmelCase_				(      self      :      Union[str, Any]	)							->    Any:







							"""simple docstring"""


							snake_case_		:			Any											=	pipeline('zero-shot-classification'		,   model='roberta-large-mnli'		,   framework='pt'	)
							snake_case_		:			Union[str, Any]											=	zero_shot_classifier(
							    'Who are you voting for in 2020?'		,   candidate_labels=['politics', 'public health', 'science']	)

							self.assertEqual(
							    nested_simplify(_A	)		,   {
							        'sequence': 'Who are you voting for in 2020?',
							        'labels': ['politics', 'public health', 'science'],
							        'scores': [0.9_7_6, 0.0_1_5, 0.0_0_9],
							    }		,   )
							snake_case_		:			Tuple											=	zero_shot_classifier(
							    'The dominant sequence transduction models are based on complex recurrent or convolutional neural networks'
							    ' in an encoder-decoder configuration. The best performing models also connect the encoder and decoder'
							    ' through an attention mechanism. We propose a new simple network architecture, the Transformer, based'
							    ' solely on attention mechanisms, dispensing with recurrence and convolutions entirely. Experiments on two'
							    ' machine translation tasks show these models to be superior in quality while being more parallelizable'
							    ' and requiring significantly less time to train. Our model achieves 28.4 BLEU on the WMT 2014'
							    ' English-to-German translation task, improving over the existing best results, including ensembles by'
							    ' over 2 BLEU. On the WMT 2014 English-to-French translation task, our model establishes a new'
							    ' single-model state-of-the-art BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small'
							    ' fraction of the training costs of the best models from the literature. We show that the Transformer'
							    ' generalizes well to other tasks by applying it successfully to English constituency parsing both with'
							    ' large and limited training data.'		,   candidate_labels=['machine learning', 'statistics', 'translation', 'vision']		,   multi_label=_A		,   )
							self.assertEqual(
							    nested_simplify(_A	)		,   {
							        'sequence': (
							            'The dominant sequence transduction models are based on complex recurrent or convolutional neural'
							            ' networks in an encoder-decoder configuration. The best performing models also connect the'
							            ' encoder and decoder through an attention mechanism. We propose a new simple network'
							            ' architecture, the Transformer, based solely on attention mechanisms, dispensing with recurrence'
							            ' and convolutions entirely. Experiments on two machine translation tasks show these models to be'
							            ' superior in quality while being more parallelizable and requiring significantly less time to'
							            ' train. Our model achieves 28.4 BLEU on the WMT 2014 English-to-German translation task,'
							            ' improving over the existing best results, including ensembles by over 2 BLEU. On the WMT 2014'
							            ' English-to-French translation task, our model establishes a new single-model state-of-the-art'
							            ' BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small fraction of the training'
							            ' costs of the best models from the literature. We show that the Transformer generalizes well to'
							            ' other tasks by applying it successfully to English constituency parsing both with large and'
							            ' limited training data.'
							        ),
							        'labels': ['translation', 'machine learning', 'vision', 'statistics'],
							        'scores': [0.8_1_7, 0.7_1_3, 0.0_1_8, 0.0_1_8],
							    }		,   )






						@slow
						@require_tf
						def UpperCAmelCase_				(      self      :      Dict	)							->    Optional[Any]:







							"""simple docstring"""


							snake_case_		:			Optional[int]											=	pipeline('zero-shot-classification'		,   model='roberta-large-mnli'		,   framework='tf'	)
							snake_case_		:			int											=	zero_shot_classifier(
							    'Who are you voting for in 2020?'		,   candidate_labels=['politics', 'public health', 'science']	)

							self.assertEqual(
							    nested_simplify(_A	)		,   {
							        'sequence': 'Who are you voting for in 2020?',
							        'labels': ['politics', 'public health', 'science'],
							        'scores': [0.9_7_6, 0.0_1_5, 0.0_0_9],
							    }		,   )
							snake_case_		:			Any											=	zero_shot_classifier(
							    'The dominant sequence transduction models are based on complex recurrent or convolutional neural networks'
							    ' in an encoder-decoder configuration. The best performing models also connect the encoder and decoder'
							    ' through an attention mechanism. We propose a new simple network architecture, the Transformer, based'
							    ' solely on attention mechanisms, dispensing with recurrence and convolutions entirely. Experiments on two'
							    ' machine translation tasks show these models to be superior in quality while being more parallelizable'
							    ' and requiring significantly less time to train. Our model achieves 28.4 BLEU on the WMT 2014'
							    ' English-to-German translation task, improving over the existing best results, including ensembles by'
							    ' over 2 BLEU. On the WMT 2014 English-to-French translation task, our model establishes a new'
							    ' single-model state-of-the-art BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small'
							    ' fraction of the training costs of the best models from the literature. We show that the Transformer'
							    ' generalizes well to other tasks by applying it successfully to English constituency parsing both with'
							    ' large and limited training data.'		,   candidate_labels=['machine learning', 'statistics', 'translation', 'vision']		,   multi_label=_A		,   )
							self.assertEqual(
							    nested_simplify(_A	)		,   {
							        'sequence': (
							            'The dominant sequence transduction models are based on complex recurrent or convolutional neural'
							            ' networks in an encoder-decoder configuration. The best performing models also connect the'
							            ' encoder and decoder through an attention mechanism. We propose a new simple network'
							            ' architecture, the Transformer, based solely on attention mechanisms, dispensing with recurrence'
							            ' and convolutions entirely. Experiments on two machine translation tasks show these models to be'
							            ' superior in quality while being more parallelizable and requiring significantly less time to'
							            ' train. Our model achieves 28.4 BLEU on the WMT 2014 English-to-German translation task,'
							            ' improving over the existing best results, including ensembles by over 2 BLEU. On the WMT 2014'
							            ' English-to-French translation task, our model establishes a new single-model state-of-the-art'
							            ' BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small fraction of the training'
							            ' costs of the best models from the literature. We show that the Transformer generalizes well to'
							            ' other tasks by applying it successfully to English constituency parsing both with large and'
							            ' limited training data.'
							        ),
							        'labels': ['translation', 'machine learning', 'vision', 'statistics'],
							        'scores': [0.8_1_7, 0.7_1_3, 0.0_1_8, 0.0_1_8],
							    }		,   )








import argparse

from torch import nn

# transformers_old should correspond to branch `save_old_prophetnet_model_structure` here
# original prophetnet_checkpoints are saved under `patrickvonplaten/..._old` respectively
from transformers_old.modeling_prophetnet import (
    ProphetNetForConditionalGeneration as ProphetNetForConditionalGenerationOld,
)
from transformers_old.modeling_xlm_prophetnet import (
    XLMProphetNetForConditionalGeneration as XLMProphetNetForConditionalGenerationOld,
)

from transformers import ProphetNetForConditionalGeneration, XLMProphetNetForConditionalGeneration, logging


_SCREAMING_SNAKE_CASE      	=		logging.get_logger(__name__)
logging.set_verbosity_info()


def 							SCREAMING_SNAKE_CASE__						(    __a   , __a   ):
	if "xprophetnet" in prophetnet_checkpoint_path:
		snake_case_		:			int											=	XLMProphetNetForConditionalGenerationOld.from_pretrained(__a   )
		snake_case_						,snake_case_		:			List[Any]											=	XLMProphetNetForConditionalGeneration.from_pretrained(
		    __a   , output_loading_info=__a   )
	else:
		snake_case_		:			List[str]											=	ProphetNetForConditionalGenerationOld.from_pretrained(__a   )
		snake_case_						,snake_case_		:			Dict											=	ProphetNetForConditionalGeneration.from_pretrained(
		    __a   , output_loading_info=__a   )

	snake_case_		:			str											=	['key_proj', 'value_proj', 'query_proj']

	snake_case_		:			List[str]											=	{
	    'self_attn': 'ngram_self_attn',
	    'cross_attn': 'encoder_attn',
	    'cross_attn_layer_norm': 'encoder_attn_layer_norm',
	    'feed_forward_layer_norm': 'final_layer_norm',
	    'feed_forward': '',
	    'intermediate': 'fc1',
	    'output': 'fc2',
	    'key_proj': 'k_proj',
	    'query_proj': 'q_proj',
	    'value_proj': 'v_proj',
	    'word_embeddings': 'embed_tokens',
	    'embeddings_layer_norm': 'emb_layer_norm',
	    'relative_pos_embeddings': 'relative_linear',
	    'ngram_embeddings': 'ngram_input_embed',
	    'position_embeddings': 'embed_positions',
	}

	for key in loading_info["missing_keys"]:
		snake_case_		:			Union[str, Any]											=	key.split('.'   )

		if attributes[0] == "lm_head":
			snake_case_		:			Optional[Any]											=	prophet
			snake_case_		:			Any											=	prophet_old
		else:
			snake_case_		:			Optional[int]											=	prophet.prophetnet
			snake_case_		:			str											=	prophet_old.model

		snake_case_		:			Union[str, Any]											=	False
		for attribute in attributes:
			if attribute in mapping:
				snake_case_		:			Optional[Any]											=	mapping[attribute]
				if not hasattr(__a   , __a   ) and len(__a   ) > 0:
					snake_case_		:			List[Any]											=	attribute
			elif hasattr(__a   , __a   ):
				snake_case_		:			List[Any]											=	attribute

			if attribute == "weight":
				assert old_model.weight.shape == model.weight.shape, "Shapes have to match!"
				snake_case_		:			int											=	old_model.weight
				logger.info(f"""{attribute} is initialized."""   )
				snake_case_		:			Optional[int]											=	True
				break
			elif attribute == "bias":
				assert old_model.bias.shape == model.bias.shape, "Shapes have to match!"
				snake_case_		:			List[str]											=	old_model.bias
				logger.info(f"""{attribute} is initialized"""   )
				snake_case_		:			int											=	True
				break
			elif attribute in special_keys and hasattr(__a   , 'in_proj_weight'   ):
				snake_case_		:			Optional[Any]											=	old_model.in_proj_weight.shape[0] // 3
				snake_case_		:			List[Any]											=	getattr(__a   , __a   )
				param.weight.shape == old_model.in_proj_weight[:embed_dim, :].shape, "Shapes have to match"
				param.bias.shape == old_model.in_proj_bias[:embed_dim].shape, "Shapes have to match"
				if attribute == "query_proj":
					snake_case_		:			Tuple											=	nn.Parameter(old_model.in_proj_weight[:embed_dim, :]   )
					snake_case_		:			Optional[int]											=	nn.Parameter(old_model.in_proj_bias[:embed_dim]   )

				elif attribute == "key_proj":
					snake_case_		:			int											=	nn.Parameter(old_model.in_proj_weight[embed_dim : 2 * embed_dim, :]   )
					snake_case_		:			Any											=	nn.Parameter(old_model.in_proj_bias[embed_dim : 2 * embed_dim]   )
				elif attribute == "value_proj":
					snake_case_		:			List[Any]											=	nn.Parameter(old_model.in_proj_weight[2 * embed_dim :, :]   )
					snake_case_		:			str											=	nn.Parameter(old_model.in_proj_bias[2 * embed_dim :]   )
				snake_case_		:			Any											=	True
				break
			elif attribute == "position_embeddings":
				assert (
				    model.position_embeddings.weight.shape[-1] == old_model.embed_positions.weight.shape[-1]
				), "Hidden size has to match"
				assert model.position_embeddings.weight.shape[0] == 5_12, "We want 512 position_embeddings."
				snake_case_		:			Any											=	nn.Parameter(old_model.embed_positions.weight[:5_12, :]   )
				snake_case_		:			List[Any]											=	True
				break

			if attribute.isdigit():
				snake_case_		:			Any											=	model[int(__a   )]
				snake_case_		:			Any											=	old_model[int(__a   )]
			else:
				snake_case_		:			str											=	getattr(__a   , __a   )

				if old_attribute == "":
					snake_case_		:			Tuple											=	old_model
				else:
					if not hasattr(__a   , __a   ):
						raise ValueError(f"""{old_model} does not have {old_attribute}"""   )
					snake_case_		:			List[str]											=	getattr(__a   , __a   )

		if not is_key_init:
			raise ValueError(f"""{key} was not correctly initialized!"""   )

	print(f"""Saving model to {pytorch_dump_folder_path}"""   )
	prophet.save_pretrained(__a   )


if __name__ == "__main__":
						_SCREAMING_SNAKE_CASE      	=		argparse.ArgumentParser()
						# Required parameters
						parser.add_argument(
						    """--prophetnet_checkpoint_path""", default=None, type=str, required=True, help="""Path the official PyTorch dump."""
						)
						parser.add_argument(
						    """--pytorch_dump_folder_path""", default=None, type=str, required=True, help="""Path to the output PyTorch model."""
						)
						_SCREAMING_SNAKE_CASE      	=		parser.parse_args()
						convert_prophetnet_checkpoint_to_pytorch(args.prophetnet_checkpoint_path, args.pytorch_dump_folder_path)

