






from __future__ import annotations

import unittest

from transformers import BlenderbotConfig, BlenderbotTokenizer, is_tf_available
from transformers.testing_utils import require_tf, require_tokenizers, slow
from transformers.utils import cached_property

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


if is_tf_available():
       import tensorflow as tf

       from transformers import TFAutoModelForSeqaSeqLM, TFBlenderbotForConditionalGeneration, TFBlenderbotModel
@require_tf
class        a       :



      """simple docstring"""


      a    :					Any							=      BlenderbotConfig
      a    :					Union[str, Any]							=      {}
      a    :					Optional[Any]							=      'gelu'
      def __init__( self   :     Any						, __lowercase   :     str						, __lowercase   :     str=13						, __lowercase   :     Tuple=7						, __lowercase   :     Optional[Any]=True						, __lowercase   :     List[Any]=False						, __lowercase   :     int=99						, __lowercase   :     str=32						, __lowercase   :     Optional[int]=2						, __lowercase   :     List[str]=4						, __lowercase   :     Optional[Any]=37						, __lowercase   :     Any=0.1						, __lowercase   :     Optional[int]=0.1						, __lowercase   :     Dict=20						, __lowercase   :     Tuple=2						, __lowercase   :     Optional[Any]=1						, __lowercase   :     Optional[int]=0						, )				-> int:
        __UpperCAmelCase				:      List[str]	 =    parent
        __UpperCAmelCase				:      List[Any]	 =    batch_size
        __UpperCAmelCase				:      str	 =    seq_length
        __UpperCAmelCase				:      Dict	 =    is_training
        __UpperCAmelCase				:      Optional[Any]	 =    use_labels
        __UpperCAmelCase				:      str	 =    vocab_size
        __UpperCAmelCase				:      List[Any]	 =    hidden_size
        __UpperCAmelCase				:      int	 =    num_hidden_layers
        __UpperCAmelCase				:      Dict	 =    num_attention_heads
        __UpperCAmelCase				:      Optional[int]	 =    intermediate_size

        __UpperCAmelCase				:      Union[str, Any]	 =    hidden_dropout_prob
        __UpperCAmelCase				:      Tuple	 =    attention_probs_dropout_prob
        __UpperCAmelCase				:      str	 =    max_position_embeddings
        __UpperCAmelCase				:      Any	 =    eos_token_id
        __UpperCAmelCase				:      Optional[Any]	 =    pad_token_id
        __UpperCAmelCase				:      str	 =    bos_token_id
      def       UpperCAmelCase ( self   :     str   )				-> int:
        __UpperCAmelCase				:      str	 =    ids_tensor([self.batch_size, self.seq_length - 1]						, self.vocab_size   )
        __UpperCAmelCase				:      List[str]	 =    tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size   )						, 1   )
        __UpperCAmelCase				:      Dict	 =    tf.concat([input_ids, eos_tensor]						, axis=1   )

        __UpperCAmelCase				:      int	 =    ids_tensor([self.batch_size, self.seq_length]						, self.vocab_size   )

        __UpperCAmelCase				:      str	 =    self.config_cls(
            vocab_size=self.vocab_size						, d_model=self.hidden_size						, encoder_layers=self.num_hidden_layers						, decoder_layers=self.num_hidden_layers						, encoder_attention_heads=self.num_attention_heads						, decoder_attention_heads=self.num_attention_heads						, encoder_ffn_dim=self.intermediate_size						, decoder_ffn_dim=self.intermediate_size						, dropout=self.hidden_dropout_prob						, attention_dropout=self.attention_probs_dropout_prob						, max_position_embeddings=self.max_position_embeddings						, eos_token_ids=[2]						, bos_token_id=self.bos_token_id						, pad_token_id=self.pad_token_id						, decoder_start_token_id=self.pad_token_id						, **self.config_updates						, )
        __UpperCAmelCase				:      int	 =    prepare_blenderbot_inputs_dict(a__						, a__						, a__   )
        return config, inputs_dict




      def       UpperCAmelCase ( self   :     Optional[int]						, __lowercase   :     str						, __lowercase   :     List[str]   )				-> Any:
        __UpperCAmelCase				:      List[str]	 =    TFBlenderbotModel(config=a__   ).get_decoder()
        __UpperCAmelCase				:      Dict	 =    inputs_dict["""input_ids"""]

        __UpperCAmelCase				:      Optional[int]	 =    input_ids[:1, :]
        __UpperCAmelCase				:      List[str]	 =    inputs_dict["""attention_mask"""][:1, :]
        __UpperCAmelCase				:      List[Any]	 =    inputs_dict["""head_mask"""]
        __UpperCAmelCase				:      Optional[int]	 =    1

        # first forward pass
        __UpperCAmelCase				:      List[str]	 =    model(a__						, attention_mask=a__						, head_mask=a__						, use_cache=a__   )

        __UpperCAmelCase ,					__UpperCAmelCase				:      str	 =    outputs.to_tuple()

        # create hypothetical next token and extent to next_input_ids
        __UpperCAmelCase				:      str	 =    ids_tensor((self.batch_size, 3)						, config.vocab_size   )
        __UpperCAmelCase				:      str	 =    tf.cast(ids_tensor((self.batch_size, 3)						, 2   )						, tf.inta   )

        # append to next input_ids and
        __UpperCAmelCase				:      Any	 =    tf.concat([input_ids, next_tokens]						, axis=-1   )
        __UpperCAmelCase				:      int	 =    tf.concat([attention_mask, next_attn_mask]						, axis=-1   )

        __UpperCAmelCase				:      Optional[int]	 =    model(a__						, attention_mask=a__   )[0]
        __UpperCAmelCase				:      int	 =    model(a__						, attention_mask=a__						, past_key_values=a__   )[0]

        self.parent.assertEqual(next_tokens.shape[1]						, output_from_past.shape[1]   )

        # select random slice
        __UpperCAmelCase				:      Optional[Any]	 =    int(ids_tensor((1,)						, output_from_past.shape[-1]   )   )
        __UpperCAmelCase				:      str	 =    output_from_no_past[:, -3:, random_slice_idx]
        __UpperCAmelCase				:      List[str]	 =    output_from_past[:, :, random_slice_idx]

        # test that outputs are equal for slice
        tf.debugging.assert_near(a__						, a__						, rtol=1e-3   )






def 							lowerCamelCase__ (       __lowerCamelCase     :	Optional[int]    ,						__lowerCamelCase     :	Any    ,						__lowerCamelCase     :	Any    ,						__lowerCamelCase     :	Dict=None    ,						__lowerCamelCase     :	Optional[Any]=None    ,						__lowerCamelCase     :	Union[str, Any]=None    ,						__lowerCamelCase     :	Optional[Any]=None    ,						__lowerCamelCase     :	Optional[Any]=None    ,						):







  if attention_mask is None:
    __UpperCAmelCase				:      Any	 =    tf.cast(tf.math.not_equal(UpperCamelCase__    ,						config.pad_token_id						)    ,						tf.inta						)
  if decoder_attention_mask is None:
    __UpperCAmelCase				:      List[Any]	 =    tf.concat(
        [
            tf.ones(decoder_input_ids[:, :1].shape    ,						dtype=tf.inta						),
            tf.cast(tf.math.not_equal(decoder_input_ids[:, 1:]    ,						config.pad_token_id						)    ,						tf.inta						),
        ]    ,						axis=-1    ,						)
  if head_mask is None:
    __UpperCAmelCase				:      List[Any]	 =    tf.ones((config.encoder_layers, config.encoder_attention_heads)						)
  if decoder_head_mask is None:
    __UpperCAmelCase				:      Optional[int]	 =    tf.ones((config.decoder_layers, config.decoder_attention_heads)						)
  if cross_attn_head_mask is None:
    __UpperCAmelCase				:      int	 =    tf.ones((config.decoder_layers, config.decoder_attention_heads)						)
  return {
      "input_ids": input_ids,
      "decoder_input_ids": decoder_input_ids,
      "attention_mask": attention_mask,
      "decoder_attention_mask": decoder_attention_mask,
      "head_mask": head_mask,
      "decoder_head_mask": decoder_head_mask,
      "cross_attn_head_mask": cross_attn_head_mask,
  }
@require_tf
class        a       (					__snake_case				,			__snake_case				,			unittest.TestCase  ):



      """simple docstring"""


      a    :					Any							=      (TFBlenderbotForConditionalGeneration, TFBlenderbotModel) if is_tf_available() else ()
      a    :					List[str]							=      (TFBlenderbotForConditionalGeneration,) if is_tf_available() else ()
      a    :					int							=      (
          {
              'conversational': TFBlenderbotForConditionalGeneration,
              'feature-extraction': TFBlenderbotModel,
              'summarization': TFBlenderbotForConditionalGeneration,
              'text2text-generation': TFBlenderbotForConditionalGeneration,
              'translation': TFBlenderbotForConditionalGeneration,
          }
          if is_tf_available()
          else {}
      )
      a    :					Tuple							=      True
      a    :					str							=      False
      a    :					Dict							=      False
      def       UpperCAmelCase ( self   :     int   )				-> Union[str, Any]:
        __UpperCAmelCase				:      List[str]	 =    TFBlenderbotModelTester(self   )
        __UpperCAmelCase				:      Dict	 =    ConfigTester(self						, config_class=a__   )
      def       UpperCAmelCase ( self   :     Optional[Any]   )				-> Dict:
        self.config_tester.run_common_tests()




      def       UpperCAmelCase ( self   :     List[str]   )				-> str:
        __UpperCAmelCase				:      str	 =    self.model_tester.prepare_config_and_inputs_for_common()
        self.model_tester.check_decoder_model_past_large_inputs(*a__   )


@require_tokenizers
@require_tf
class        a       (					unittest.TestCase  ):



      """simple docstring"""


      a    :					Dict							=      ['My friends are cool but they eat too many carbs.']
      a    :					Dict							=      'facebook/blenderbot-400M-distill'
      @cached_property
      def       UpperCAmelCase ( self   :     Dict   )				-> List[Any]:
        return BlenderbotTokenizer.from_pretrained(self.model_name   )
      @cached_property
      def       UpperCAmelCase ( self   :     int   )				-> Any:
        __UpperCAmelCase				:      str	 =    TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name   )
        return model




      @slow
      def       UpperCAmelCase ( self   :     Optional[int]   )				-> List[Any]:
        __UpperCAmelCase				:      int	 =    self.tokenizer(self.src_text						, return_tensors="""tf"""   )
        __UpperCAmelCase				:      Tuple	 =    self.model.generate(
            model_inputs.input_ids						, )
        __UpperCAmelCase				:      int	 =    self.tokenizer.batch_decode(generated_ids.numpy()						, skip_special_tokens=a__   )[0]
        assert (
            generated_words
            == " That's unfortunate. Are they trying to lose weight or are they just trying to be healthier?"
        )






import argparse
import json

import requests
import torch
from huggingface_hub import hf_hub_download
from PIL import Image

from transformers import ConvNextConfig, SegformerImageProcessor, UperNetConfig, UperNetForSemanticSegmentation





def 	_lowerCAmelCase					( UpperCamelCase__:  Optional[Any]    )			-> Optional[int]:





	"""simple docstring"""







	A             =					3_84
	if "tiny" in model_name:
		A             =					[3, 3, 9, 3]
		A             =					[96, 1_92, 3_84, 7_68]
	if "small" in model_name:
		A             =					[3, 3, 27, 3]
		A             =					[96, 1_92, 3_84, 7_68]
	if "base" in model_name:
		A             =					[3, 3, 27, 3]
		A             =					[1_28, 2_56, 5_12, 10_24]
		A             =					5_12
	if "large" in model_name:
		A             =					[3, 3, 27, 3]
		A             =					[1_92, 3_84, 7_68, 15_36]
		A             =					7_68
	if "xlarge" in model_name:
		A             =					[3, 3, 27, 3]
		A             =					[2_56, 5_12, 10_24, 20_48]
		A             =					10_24

	# set label information
	A             =					1_50
	A             =					"""huggingface/label-files"""
	A             =					"""ade20k-id2label.json"""
	A             =					json.load(open(hf_hub_download(UpperCamelCase__				,       UpperCamelCase__				,       repo_type="""dataset"""    )				,       """r"""    )    )
	A             =					{int(UpperCamelCase__    ): v for k, v in idalabel.items()}
	A             =					{v: k for k, v in idalabel.items()}

	A             =					ConvNextConfig(
	    depths=UpperCamelCase__				,       hidden_sizes=UpperCamelCase__				,       out_features=["""stage1""", """stage2""", """stage3""", """stage4"""]    )
	A             =					UperNetConfig(
	    backbone_config=UpperCamelCase__				,       auxiliary_in_channels=UpperCamelCase__				,       num_labels=UpperCamelCase__				,       idalabel=UpperCamelCase__				,       labelaid=UpperCamelCase__				,       )

	return config





def 	_lowerCAmelCase					( UpperCamelCase__:  int    )			-> Dict:





	"""simple docstring"""







	A             =					[]

	# fmt: off
	# stem
	rename_keys.append(("""backbone.downsample_layers.0.0.weight""", """backbone.embeddings.patch_embeddings.weight""")    )
	rename_keys.append(("""backbone.downsample_layers.0.0.bias""", """backbone.embeddings.patch_embeddings.bias""")    )
	rename_keys.append(("""backbone.downsample_layers.0.1.weight""", """backbone.embeddings.layernorm.weight""")    )
	rename_keys.append(("""backbone.downsample_layers.0.1.bias""", """backbone.embeddings.layernorm.bias""")    )
	# stages
	for i in range(len(config.backbone_config.depths    )    ):
		for j in range(config.backbone_config.depths[i]    ):
			rename_keys.append((f'backbone.stages.{i}.{j}.gamma', f'backbone.encoder.stages.{i}.layers.{j}.layer_scale_parameter')    )
			rename_keys.append((f'backbone.stages.{i}.{j}.depthwise_conv.weight', f'backbone.encoder.stages.{i}.layers.{j}.dwconv.weight')    )
			rename_keys.append((f'backbone.stages.{i}.{j}.depthwise_conv.bias', f'backbone.encoder.stages.{i}.layers.{j}.dwconv.bias')    )
			rename_keys.append((f'backbone.stages.{i}.{j}.norm.weight', f'backbone.encoder.stages.{i}.layers.{j}.layernorm.weight')    )
			rename_keys.append((f'backbone.stages.{i}.{j}.norm.bias', f'backbone.encoder.stages.{i}.layers.{j}.layernorm.bias')    )
			rename_keys.append((f'backbone.stages.{i}.{j}.pointwise_conv1.weight', f'backbone.encoder.stages.{i}.layers.{j}.pwconv1.weight')    )
			rename_keys.append((f'backbone.stages.{i}.{j}.pointwise_conv1.bias', f'backbone.encoder.stages.{i}.layers.{j}.pwconv1.bias')    )
			rename_keys.append((f'backbone.stages.{i}.{j}.pointwise_conv2.weight', f'backbone.encoder.stages.{i}.layers.{j}.pwconv2.weight')    )
			rename_keys.append((f'backbone.stages.{i}.{j}.pointwise_conv2.bias', f'backbone.encoder.stages.{i}.layers.{j}.pwconv2.bias')    )
		if i > 0:
			rename_keys.append((f'backbone.downsample_layers.{i}.0.weight', f'backbone.encoder.stages.{i}.downsampling_layer.0.weight')    )
			rename_keys.append((f'backbone.downsample_layers.{i}.0.bias', f'backbone.encoder.stages.{i}.downsampling_layer.0.bias')    )
			rename_keys.append((f'backbone.downsample_layers.{i}.1.weight', f'backbone.encoder.stages.{i}.downsampling_layer.1.weight')    )
			rename_keys.append((f'backbone.downsample_layers.{i}.1.bias', f'backbone.encoder.stages.{i}.downsampling_layer.1.bias')    )

		rename_keys.append((f'backbone.norm{i}.weight', f'backbone.hidden_states_norms.stage{i+1}.weight')    )
		rename_keys.append((f'backbone.norm{i}.bias', f'backbone.hidden_states_norms.stage{i+1}.bias')    )

	# decode head
	rename_keys.extend(
	    [
	        ("""decode_head.conv_seg.weight""", """decode_head.classifier.weight"""),
	        ("""decode_head.conv_seg.bias""", """decode_head.classifier.bias"""),
	        ("""auxiliary_head.conv_seg.weight""", """auxiliary_head.classifier.weight"""),
	        ("""auxiliary_head.conv_seg.bias""", """auxiliary_head.classifier.bias"""),
	    ]    )
	# fmt: on

	return rename_keys





def 	_lowerCAmelCase					( UpperCamelCase__:  Tuple				,       UpperCamelCase__:  List[str]				,       UpperCamelCase__:  int    )			-> Optional[Any]:





	"""simple docstring"""







	A             =					dct.pop(UpperCamelCase__    )
	A             =					val





def 	_lowerCAmelCase					( UpperCamelCase__:  Optional[int]				,       UpperCamelCase__:  Any				,       UpperCamelCase__:  int    )			-> List[Any]:





	"""simple docstring"""







	A             =					{
	    """upernet-convnext-tiny""": """https://download.openmmlab.com/mmsegmentation/v0.5/convnext/upernet_convnext_tiny_fp16_512x512_160k_ade20k/upernet_convnext_tiny_fp16_512x512_160k_ade20k_20220227_124553-cad485de.pth""",
	    """upernet-convnext-small""": """https://download.openmmlab.com/mmsegmentation/v0.5/convnext/upernet_convnext_small_fp16_512x512_160k_ade20k/upernet_convnext_small_fp16_512x512_160k_ade20k_20220227_131208-1b1e394f.pth""",
	    """upernet-convnext-base""": """https://download.openmmlab.com/mmsegmentation/v0.5/convnext/upernet_convnext_base_fp16_512x512_160k_ade20k/upernet_convnext_base_fp16_512x512_160k_ade20k_20220227_181227-02a24fc6.pth""",
	    """upernet-convnext-large""": """https://download.openmmlab.com/mmsegmentation/v0.5/convnext/upernet_convnext_large_fp16_640x640_160k_ade20k/upernet_convnext_large_fp16_640x640_160k_ade20k_20220226_040532-e57aa54d.pth""",
	    """upernet-convnext-xlarge""": """https://download.openmmlab.com/mmsegmentation/v0.5/convnext/upernet_convnext_xlarge_fp16_640x640_160k_ade20k/upernet_convnext_xlarge_fp16_640x640_160k_ade20k_20220226_080344-95fc38c2.pth""",
	}
	A             =					model_name_to_url[model_name]
	A             =					torch.hub.load_state_dict_from_url(UpperCamelCase__				,       map_location="""cpu"""    )["""state_dict"""]

	A             =					get_upernet_config(UpperCamelCase__    )
	A             =					UperNetForSemanticSegmentation(UpperCamelCase__    )
	model.eval()

	# replace "bn" => "batch_norm"
	for key in state_dict.copy().keys():
		A             =					state_dict.pop(UpperCamelCase__    )
		if "bn" in key:
			A             =					key.replace("""bn"""				,       """batch_norm"""    )
		A             =					val

	# rename keys
	A             =					create_rename_keys(UpperCamelCase__    )
	for src, dest in rename_keys:
		rename_key(UpperCamelCase__				,       UpperCamelCase__				,       UpperCamelCase__    )

	model.load_state_dict(UpperCamelCase__    )

	# verify on image
	A             =					"""https://huggingface.co/datasets/hf-internal-testing/fixtures_ade20k/resolve/main/ADE_val_00000001.jpg"""
	A             =					Image.open(requests.get(UpperCamelCase__				,       stream=UpperCamelCase__    ).raw    ).convert("""RGB"""    )

	A             =					SegformerImageProcessor()
	A             =					processor(UpperCamelCase__				,       return_tensors="""pt"""    ).pixel_values

	with torch.no_grad():
		A             =					model(UpperCamelCase__    )

	if model_name == "upernet-convnext-tiny":
		A             =					torch.tensor(
		    [[-8.81_10, -8.81_10, -8.65_21], [-8.81_10, -8.81_10, -8.65_21], [-8.77_46, -8.77_46, -8.61_30]]    )
	elif model_name == "upernet-convnext-small":
		A             =					torch.tensor(
		    [[-8.82_36, -8.82_36, -8.67_71], [-8.82_36, -8.82_36, -8.67_71], [-8.76_38, -8.76_38, -8.62_40]]    )
	elif model_name == "upernet-convnext-base":
		A             =					torch.tensor(
		    [[-8.85_58, -8.85_58, -8.69_05], [-8.85_58, -8.85_58, -8.69_05], [-8.76_69, -8.76_69, -8.60_21]]    )
	elif model_name == "upernet-convnext-large":
		A             =					torch.tensor(
		    [[-8.66_60, -8.66_60, -8.62_10], [-8.66_60, -8.66_60, -8.62_10], [-8.63_10, -8.63_10, -8.59_64]]    )
	elif model_name == "upernet-convnext-xlarge":
		A             =					torch.tensor(
		    [[-8.49_80, -8.49_80, -8.39_77], [-8.49_80, -8.49_80, -8.39_77], [-8.43_79, -8.43_79, -8.34_12]]    )
	print("""Logits:"""				,       outputs.logits[0, 0, :3, :3]    )
	assert torch.allclose(outputs.logits[0, 0, :3, :3]				,       UpperCamelCase__				,       atol=1e-4    )
	print("""Looks ok!"""    )

	if pytorch_dump_folder_path is not None:
		print(f'Saving model {model_name} to {pytorch_dump_folder_path}'    )
		model.save_pretrained(UpperCamelCase__    )
		print(f'Saving processor to {pytorch_dump_folder_path}'    )
		processor.save_pretrained(UpperCamelCase__    )

	if push_to_hub:
		print(f'Pushing model and processor for {model_name} to hub'    )
		model.push_to_hub(f'openmmlab/{model_name}'    )
		processor.push_to_hub(f'openmmlab/{model_name}'    )


if __name__ == "__main__":
				_lowercase							:   List[str]						=						argparse.ArgumentParser()
				# Required parameters
				parser.add_argument(
				    "--model_name",
				    default="upernet-convnext-tiny",
				    type=str,
				    choices=[f'''upernet-convnext-{size}''' for size in ["tiny", "small", "base", "large", "xlarge"]],
				    help="Name of the ConvNext UperNet model you'd like to convert.",
				)
				parser.add_argument(
				    "--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model directory."
				)
				parser.add_argument(
				    "--push_to_hub", action="store_true", help="Whether or not to push the converted model to the 🤗 hub."
				)

				_lowercase							:   str						=						parser.parse_args()
				convert_upernet_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)



