



import inspect
import unittest

from transformers import ConvNextVaConfig
from transformers.models.auto import get_values
from transformers.models.auto.modeling_auto import MODEL_FOR_BACKBONE_MAPPING_NAMES, MODEL_MAPPING_NAMES
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
   import torch

   from transformers import ConvNextVaBackbone, ConvNextVaForImageClassification, ConvNextVaModel
   from transformers.models.convnextva.modeling_convnextva import CONVNEXTV2_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
   from PIL import Image

   from transformers import AutoImageProcessor


class 	lowerCamelCase__	:



 """simple docstring"""

 def __init__(    self					:					Optional[int]			,						__lowerCAmelCase					:					Optional[int]			,						__lowerCAmelCase					:					Tuple=13			,						__lowerCAmelCase					:					List[str]=32			,						__lowerCAmelCase					:					Optional[int]=3			,						__lowerCAmelCase					:					Optional[int]=4			,						__lowerCAmelCase					:					List[Any]=[10, 20, 30, 40]			,						__lowerCAmelCase					:					str=[2, 2, 3, 2]			,						__lowerCAmelCase					:					Optional[Any]=True			,						__lowerCAmelCase					:					str=True			,						__lowerCAmelCase					:					Optional[Any]=37			,						__lowerCAmelCase					:					Dict="gelu"			,						__lowerCAmelCase					:					List[Any]=10			,						__lowerCAmelCase					:					int=0.02			,						__lowerCAmelCase					:					Any=["stage2", "stage3", "stage4"]			,						__lowerCAmelCase					:					List[Any]=[2, 3, 4]			,						__lowerCAmelCase					:					List[Any]=None			,						)   ->	Optional[int]:
    _A      =       parent
    _A      =       batch_size
    _A      =       image_size
    _A      =       num_channels
    _A      =       num_stages
    _A      =       hidden_sizes
    _A      =       depths
    _A      =       is_training
    _A      =       use_labels
    _A      =       intermediate_size
    _A      =       hidden_act
    _A      =       num_labels
    _A      =       initializer_range
    _A      =       out_features
    _A      =       out_indices
    _A      =       scope

 def     snake_case_      (    self					:					Any      )   ->	List[Any]:
    _A      =       floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]      )

    _A      =       None
    if self.use_labels:
       _A      =       ids_tensor([self.batch_size]			,						self.num_labels      )

    _A      =       self.get_config()

    return config, pixel_values, labels

 def     snake_case_      (    self					:					Tuple      )   ->	List[str]:
    return ConvNextVaConfig(
        num_channels=self.num_channels			,						hidden_sizes=self.hidden_sizes			,						depths=self.depths			,						num_stages=self.num_stages			,						hidden_act=self.hidden_act			,						is_decoder=__lowerCAmelCase			,						initializer_range=self.initializer_range			,						out_features=self.out_features			,						out_indices=self.out_indices			,						num_labels=self.num_labels			,						)

 def     snake_case_      (    self					:					str			,						__lowerCAmelCase					:					Any			,						__lowerCAmelCase					:					int			,						__lowerCAmelCase					:					int      )   ->	List[str]:
    _A      =       ConvNextVaModel(config=__lowerCAmelCase      )
    model.to(__lowerCAmelCase      )
    model.eval()
    _A      =       model(__lowerCAmelCase      )
    # expected last hidden states: B, C, H // 32, W // 32
    self.parent.assertEqual(
        result.last_hidden_state.shape			,						(self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32)			,						)

 def     snake_case_      (    self					:					List[Any]			,						__lowerCAmelCase					:					str			,						__lowerCAmelCase					:					int			,						__lowerCAmelCase					:					Any      )   ->	Optional[Any]:
    _A      =       ConvNextVaForImageClassification(__lowerCAmelCase      )
    model.to(__lowerCAmelCase      )
    model.eval()
    _A      =       model(__lowerCAmelCase			,						labels=__lowerCAmelCase      )
    self.parent.assertEqual(result.logits.shape			,						(self.batch_size, self.num_labels)      )

 def     snake_case_      (    self					:					str			,						__lowerCAmelCase					:					int			,						__lowerCAmelCase					:					List[str]			,						__lowerCAmelCase					:					Dict      )   ->	int:
    _A      =       ConvNextVaBackbone(config=__lowerCAmelCase      )
    model.to(__lowerCAmelCase      )
    model.eval()
    _A      =       model(__lowerCAmelCase      )

    # verify hidden states
    self.parent.assertEqual(len(result.feature_maps      )			,						len(config.out_features      )      )
    self.parent.assertListEqual(list(result.feature_maps[0].shape      )			,						[self.batch_size, self.hidden_sizes[1], 4, 4]      )

    # verify channels
    self.parent.assertEqual(len(model.channels      )			,						len(config.out_features      )      )
    self.parent.assertListEqual(model.channels			,						config.hidden_sizes[1:]      )

    # verify backbone works with out_features=None
    _A      =       None
    _A      =       ConvNextVaBackbone(config=__lowerCAmelCase      )
    model.to(__lowerCAmelCase      )
    model.eval()
    _A      =       model(__lowerCAmelCase      )

    # verify feature maps
    self.parent.assertEqual(len(result.feature_maps      )			,						1      )
    self.parent.assertListEqual(list(result.feature_maps[0].shape      )			,						[self.batch_size, self.hidden_sizes[-1], 1, 1]      )

    # verify channels
    self.parent.assertEqual(len(model.channels      )			,						1      )
    self.parent.assertListEqual(model.channels			,						[config.hidden_sizes[-1]]      )

 def     snake_case_      (    self					:					Any      )   ->	Union[str, Any]:
    _A      =       self.prepare_config_and_inputs()
    _A  ,    _A  ,    _A      =       config_and_inputs
    _A      =       {'''pixel_values''': pixel_values}
    return config, inputs_dict

 def     snake_case_      (    self					:					Optional[int]      )   ->	Optional[Any]:
    _A      =       self.prepare_config_and_inputs()
    _A  ,    _A  ,    _A      =       config_and_inputs
    _A      =       {'''pixel_values''': pixel_values, '''labels''': labels}
    return config, inputs_dict


@require_torch
class 	lowerCamelCase__	(       _A							,  _A							,  unittest.TestCase):



 """simple docstring"""

 a__					: Optional[int] =  (
     (
         ConvNextVaModel,
         ConvNextVaForImageClassification,
         ConvNextVaBackbone,
     )
     if is_torch_available()
     else ()
 )
 a__					: Tuple =  (
     {"feature-extraction": ConvNextVaModel, "image-classification": ConvNextVaForImageClassification}
     if is_torch_available()
     else {}
 )

 a__					: Optional[Any] =  False
 a__					: Any =  False
 a__					: Dict =  False
 a__					: Optional[Any] =  False
 a__					: Optional[Any] =  False

 def     snake_case_      (    self					:					Optional[int]      )   ->	List[str]:
    _A      =       ConvNextVaModelTester(self      )
    _A      =       ConfigTester(self			,						config_class=__lowerCAmelCase			,						has_text_modality=__lowerCAmelCase			,						hidden_size=37      )

 def     snake_case_      (    self					:					Optional[Any]      )   ->	List[Any]:
    self.create_and_test_config_common_properties()
    self.config_tester.create_and_test_config_to_json_string()
    self.config_tester.create_and_test_config_to_json_file()
    self.config_tester.create_and_test_config_from_and_save_pretrained()
    self.config_tester.create_and_test_config_with_num_labels()
    self.config_tester.check_config_can_be_init_without_params()
    self.config_tester.check_config_arguments_init()

 def     snake_case_      (    self					:					Any      )   ->	str:
    return

 @unittest.skip(reason='''ConvNextV2 does not use inputs_embeds'''      )
 def     snake_case_      (    self					:					Any      )   ->	Any:
    pass

 @unittest.skip(reason='''ConvNextV2 does not support input and output embeddings'''      )
 def     snake_case_      (    self					:					List[Any]      )   ->	Optional[int]:
    pass

 @unittest.skip(reason='''ConvNextV2 does not use feedforward chunking'''      )
 def     snake_case_      (    self					:					Union[str, Any]      )   ->	Tuple:
    pass

 def     snake_case_      (    self					:					int      )   ->	List[Any]:
    if not self.model_tester.is_training:
       return

    for model_class in self.all_model_classes:
       _A  ,    _A      =       self.model_tester.prepare_config_and_inputs_with_labels()
       _A      =       True

       if model_class.__name__ in [
           *get_values(__lowerCAmelCase      ),
           *get_values(__lowerCAmelCase      ),
       ]:
          continue

       _A      =       model_class(__lowerCAmelCase      )
       model.to(__lowerCAmelCase      )
       model.train()
       _A      =       self._prepare_for_class(__lowerCAmelCase			,						__lowerCAmelCase			,						return_labels=__lowerCAmelCase      )
       _A      =       model(**__lowerCAmelCase      ).loss
       loss.backward()

 def     snake_case_      (    self					:					List[str]      )   ->	Any:
    if not self.model_tester.is_training:
       return

    for model_class in self.all_model_classes:
       _A  ,    _A      =       self.model_tester.prepare_config_and_inputs_with_labels()
       _A      =       False
       _A      =       True

       if (
           model_class.__name__
           in [*get_values(__lowerCAmelCase      ), *get_values(__lowerCAmelCase      )]
           or not model_class.supports_gradient_checkpointing
       ):
          continue

       _A      =       model_class(__lowerCAmelCase      )
       model.to(__lowerCAmelCase      )
       model.gradient_checkpointing_enable()
       model.train()
       _A      =       self._prepare_for_class(__lowerCAmelCase			,						__lowerCAmelCase			,						return_labels=__lowerCAmelCase      )
       _A      =       model(**__lowerCAmelCase      ).loss
       loss.backward()

 def     snake_case_      (    self					:					Union[str, Any]      )   ->	str:
    _A  ,    _A      =       self.model_tester.prepare_config_and_inputs_for_common()

    for model_class in self.all_model_classes:
       _A      =       model_class(__lowerCAmelCase      )
       _A      =       inspect.signature(model.forward      )
       # signature.parameters is an OrderedDict => so arg_names order is deterministic
       _A      =       [*signature.parameters.keys()]

       _A      =       ['''pixel_values''']
       self.assertListEqual(arg_names[:1]			,						__lowerCAmelCase      )

 def     snake_case_      (    self					:					str      )   ->	List[Any]:
    _A      =       self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_model(*__lowerCAmelCase      )

 def     snake_case_      (    self					:					Tuple      )   ->	Any:
    def check_hidden_states_output(__lowerCAmelCase					:					Tuple			,						__lowerCAmelCase					:					int			,						__lowerCAmelCase					:					List[str]      ):
       _A      =       model_class(__lowerCAmelCase      )
       model.to(__lowerCAmelCase      )
       model.eval()

       with torch.no_grad():
          _A      =       model(**self._prepare_for_class(__lowerCAmelCase			,						__lowerCAmelCase      )      )

       _A      =       outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

       _A      =       self.model_tester.num_stages
       self.assertEqual(len(__lowerCAmelCase      )			,						expected_num_stages + 1      )

       # ConvNextV2's feature maps are of shape (batch_size, num_channels, height, width)
       self.assertListEqual(
           list(hidden_states[0].shape[-2:]      )			,						[self.model_tester.image_size // 4, self.model_tester.image_size // 4]			,						)

    _A  ,    _A      =       self.model_tester.prepare_config_and_inputs_for_common()

    for model_class in self.all_model_classes:
       _A      =       True
       check_hidden_states_output(__lowerCAmelCase			,						__lowerCAmelCase			,						__lowerCAmelCase      )

       # check that output_hidden_states also work using config
       del inputs_dict["output_hidden_states"]
       _A      =       True

       check_hidden_states_output(__lowerCAmelCase			,						__lowerCAmelCase			,						__lowerCAmelCase      )

 def     snake_case_      (    self					:					Optional[int]      )   ->	List[Any]:
    _A      =       self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_image_classification(*__lowerCAmelCase      )

 @slow
 def     snake_case_      (    self					:					List[str]      )   ->	Union[str, Any]:
    for model_name in CONVNEXTV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       _A      =       ConvNextVaModel.from_pretrained(__lowerCAmelCase      )
       self.assertIsNotNone(__lowerCAmelCase      )

def 	SCREAMING_SNAKE_CASE_							(	)  ->     Tuple:
   _A      =       Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''				)
   return image


@require_torch
@require_vision
class 	lowerCamelCase__	(       unittest.TestCase):



 """simple docstring"""

 @cached_property
 def     snake_case_      (    self					:					List[str]      )   ->	Dict:
    return AutoImageProcessor.from_pretrained('''facebook/convnextv2-tiny-1k-224'''      ) if is_vision_available() else None

 @slow
 def     snake_case_      (    self					:					Tuple      )   ->	int:
    _A      =       ConvNextVaForImageClassification.from_pretrained('''facebook/convnextv2-tiny-1k-224'''      ).to(__lowerCAmelCase      )

    _A      =       self.default_image_processor
    _A      =       prepare_img()
    _A      =       preprocessor(images=__lowerCAmelCase			,						return_tensors='''pt'''      ).to(__lowerCAmelCase      )

    # forward pass
    with torch.no_grad():
       _A      =       model(**__lowerCAmelCase      )

    # verify the logits
    _A      =       torch.Size((1, 10_00)      )
    self.assertEqual(outputs.logits.shape			,						__lowerCAmelCase      )

    _A      =       torch.tensor([0.9996, 0.1966, -0.4386]      ).to(__lowerCAmelCase      )
    self.assertTrue(torch.allclose(outputs.logits[0, :3]			,						__lowerCAmelCase			,						atol=1E-4      )      )











import argparse
from typing import Dict

import tensorflow as tf
import torch
from tqdm import tqdm

from transformers import BigBirdPegasusConfig, BigBirdPegasusForConditionalGeneration


A__      :     Union[str, Any]       =					[
    # tf -> hf
    ('/', '.'),
    ('layer_', 'layers.'),
    ('kernel', 'weight'),
    ('beta', 'bias'),
    ('gamma', 'weight'),
    ('pegasus', 'model'),
]
A__      :     Optional[Any]       =					[
    ('.output.dense', '.fc2'),
    ('intermediate.LayerNorm', 'final_layer_norm'),
    ('intermediate.dense', 'fc1'),
]

A__      :     List[str]       =					(
    INIT_COMMON
    + [
        ('attention.self.LayerNorm', 'self_attn_layer_norm'),
        ('attention.output.dense', 'self_attn.out_proj'),
        ('attention.self', 'self_attn'),
        ('attention.encdec.LayerNorm', 'encoder_attn_layer_norm'),
        ('attention.encdec_output.dense', 'encoder_attn.out_proj'),
        ('attention.encdec', 'encoder_attn'),
        ('key', 'k_proj'),
        ('value', 'v_proj'),
        ('query', 'q_proj'),
        ('decoder.LayerNorm', 'decoder.layernorm_embedding'),
    ]
    + END_COMMON
)

A__      :     List[str]       =					(
    INIT_COMMON
    + [
        ('embeddings.word_embeddings', 'shared.weight'),
        ('embeddings.position_embeddings', 'embed_positions.weight'),
        ('attention.self.LayerNorm', 'self_attn_layer_norm'),
        ('attention.output.dense', 'self_attn.output'),
        ('attention.self', 'self_attn.self'),
        ('encoder.LayerNorm', 'encoder.layernorm_embedding'),
    ]
    + END_COMMON
)

A__      :     Dict       =					[
    'encdec/key/bias',
    'encdec/query/bias',
    'encdec/value/bias',
    'self/key/bias',
    'self/query/bias',
    'self/value/bias',
    'encdec_output/dense/bias',
    'attention/output/dense/bias',
]






def     a     (		lowerCamelCase_						,  lowerCamelCase_							):






				'''simple docstring'''
				for tf_name, hf_name in patterns:
								lowercase__  					=     k.replace(lowerCamelCase_						,  lowerCamelCase_							)
				return k






def     a     (		lowerCamelCase_						,  lowerCamelCase_							):






				'''simple docstring'''
				lowercase__  					=     BigBirdPegasusConfig(**lowerCamelCase_							)
				lowercase__  					=     BigBirdPegasusForConditionalGeneration(lowerCamelCase_							)
				lowercase__  					=     torch_model.state_dict()
				lowercase__  					=     {}

				# separating decoder weights
				lowercase__  					=     {k: tf_weights[k] for k in tf_weights if k.startswith('''pegasus/decoder'''							)}
				lowercase__  					=     {k: tf_weights[k] for k in tf_weights if not k.startswith('''pegasus/decoder'''							)}

				for k, v in tqdm(decoder_weights.items()						,  '''tf -> hf conversion'''							):
								lowercase__  					=     [k.endswith(lowerCamelCase_							) for ending in KEYS_TO_IGNORE]
								if any(lowerCamelCase_							):
												continue
								lowercase__  					=     DECODER_PATTERNS
								lowercase__  					=     rename_state_dict_key(lowerCamelCase_						,  lowerCamelCase_							)
								if new_k not in state_dict:
												raise ValueError(F"""could not find new key {new_k} in state dict. (converted from {k})"""							)
								if any(True if i in k else False for i in ['''dense''', '''query''', '''key''', '''value''']							):
												lowercase__  					=     v.T
								lowercase__  					=     torch.from_numpy(lowerCamelCase_							)
								assert v.shape == state_dict[new_k].shape, F"""{new_k}, {k}, {v.shape}, {state_dict[new_k].shape}"""

				for k, v in tqdm(remaining_weights.items()						,  '''tf -> hf conversion'''							):
								lowercase__  					=     [k.endswith(lowerCamelCase_							) for ending in KEYS_TO_IGNORE]
								if any(lowerCamelCase_							):
												continue
								lowercase__  					=     REMAINING_PATTERNS
								lowercase__  					=     rename_state_dict_key(lowerCamelCase_						,  lowerCamelCase_							)
								if new_k not in state_dict and k != "pegasus/embeddings/position_embeddings":
												raise ValueError(F"""could not find new key {new_k} in state dict. (converted from {k})"""							)
								if any(True if i in k else False for i in ['''dense''', '''query''', '''key''', '''value''']							):
												lowercase__  					=     v.T
								lowercase__  					=     torch.from_numpy(lowerCamelCase_							)
								if k != "pegasus/embeddings/position_embeddings":
												assert v.shape == state_dict[new_k].shape, F"""{new_k}, {k}, {v.shape}, {state_dict[new_k].shape}"""

				lowercase__  					=     mapping['''model.embed_positions.weight''']
				lowercase__  					=     mapping.pop('''model.embed_positions.weight'''							)
				lowercase__					,		lowercase__  					=     torch_model.load_state_dict(lowerCamelCase_						,  strict=lowerCamelCase_							)
				lowercase__  					=     [
				    k
				    for k in missing
				    if k
				    not in [
				        '''final_logits_bias''',
				        '''model.encoder.embed_tokens.weight''',
				        '''model.decoder.embed_tokens.weight''',
				        '''lm_head.weight''',
				    ]
				]
				assert unexpected_missing == [], F"""no matches found for the following torch keys {unexpected_missing}"""
				assert extra == [], F"""no matches found for the following tf keys {extra}"""
				return torch_model






def     a     (		lowerCamelCase_							):






				'''simple docstring'''
				lowercase__  					=     tf.train.list_variables(lowerCamelCase_							)
				lowercase__  					=     {}
				lowercase__  					=     ['''global_step''']
				for name, shape in tqdm(lowerCamelCase_						,  desc='''converting tf checkpoint to dict'''							):
								lowercase__  					=     any(pat in name for pat in ignore_name							)
								if skip_key:
												continue
								lowercase__  					=     tf.train.load_variable(lowerCamelCase_						,  lowerCamelCase_							)
								lowercase__  					=     array
				return tf_weights






def     a     (		lowerCamelCase_						,  lowerCamelCase_						,  lowerCamelCase_							):






				'''simple docstring'''
				lowercase__  					=     get_tf_weights_as_numpy(lowerCamelCase_							)
				lowercase__  					=     convert_bigbird_pegasus(lowerCamelCase_						,  lowerCamelCase_							)
				torch_model.save_pretrained(lowerCamelCase_							)


if __name__ == "__main__":
							A__      :     Optional[Any]       =					argparse.ArgumentParser()
							parser.add_argument('--tf_ckpt_path', type=str, help='passed to tf.train.list_variables')
							parser.add_argument('--save_dir', default=None, type=str, help='Path to the output PyTorch model.')
							A__      :     Tuple       =					parser.parse_args()
							A__      :     int       =					{}
							convert_bigbird_pegasus_ckpt_to_pytorch(args.tf_ckpt_path, args.save_dir, config_update=config_update)




