



from __future__ import annotations

import unittest

import numpy as np

from transformers import OPTConfig, is_tf_available
from transformers.testing_utils import require_sentencepiece, require_tf, slow

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 GPTaTokenizer, TFOPTForCausalLM, TFOPTModel




def      __UpperCamelCase     (  _lowerCAmelCase				,					_lowerCAmelCase				,					_lowerCAmelCase=None				,					_lowerCAmelCase=None		)	->  Union[str, Any]:



    """simple docstring"""






    if attention_mask is None:
        A			:	Optional[Any]										=       tf.cast(tf.math.not_equal(__snake_case				,					config.pad_token_id		)				,					tf.inta		)
    return {"input_ids": input_ids, "attention_mask": attention_mask}






@require_tf
class SCREAMING_SNAKE_CASE__					:




    '''simple docstring'''






    __lowerCamelCase      :			List[Any] =				OPTConfig
    __lowerCamelCase      :			List[str] =				{}
    __lowerCamelCase      :			Optional[Any] =				"gelu"






    def __init__(	self,		lowerCamelCase__,		lowerCamelCase__=13,		lowerCamelCase__=7,		lowerCamelCase__=True,		lowerCamelCase__=False,		lowerCamelCase__=99,		lowerCamelCase__=16,		lowerCamelCase__=2,		lowerCamelCase__=4,		lowerCamelCase__=4,		lowerCamelCase__="gelu",		lowerCamelCase__=0.1,		lowerCamelCase__=0.1,		lowerCamelCase__=20,		lowerCamelCase__=2,		lowerCamelCase__=1,		lowerCamelCase__=0,		lowerCamelCase__=16,		lowerCamelCase__=16,		):
        A			:	Optional[Any]										=       parent
        A			:	List[str]										=       batch_size
        A			:	List[Any]										=       seq_length
        A			:	Union[str, Any]										=       is_training
        A			:	Union[str, Any]										=       use_labels
        A			:	Union[str, Any]										=       vocab_size
        A			:	List[str]										=       hidden_size
        A			:	List[str]										=       num_hidden_layers
        A			:	List[Any]										=       num_attention_heads
        A			:	List[Any]										=       intermediate_size
        A			:	Optional[Any]										=       hidden_act
        A			:	List[Any]										=       hidden_dropout_prob
        A			:	Optional[int]										=       attention_probs_dropout_prob
        A			:	Optional[int]										=       max_position_embeddings
        A			:	int										=       eos_token_id
        A			:	Optional[int]										=       pad_token_id
        A			:	Tuple										=       bos_token_id
        A			:	Optional[Any]										=       embed_dim
        A			:	Optional[Any]										=       word_embed_proj_dim
        A			:	Optional[int]										=       False






    def       _lowerCAmelCase (	self		):
        A			:	Dict										=       ids_tensor([self.batch_size, self.seq_length - 1],		self.vocab_size		)
        A			:	List[str]										=       tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size		),		1		)
        A			:	List[Any]										=       tf.concat([input_ids, eos_tensor],		axis=1		)

        A			:	str										=       self.config_cls(
            vocab_size=self.vocab_size,		hidden_size=self.hidden_size,		num_hidden_layers=self.num_hidden_layers,		num_attention_heads=self.num_attention_heads,		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_id=self.eos_token_id,		bos_token_id=self.bos_token_id,		pad_token_id=self.pad_token_id,		embed_dim=self.embed_dim,		word_embed_proj_dim=self.word_embed_proj_dim,		is_encoder_decoder=__a,		**self.config_updates,		)
        A			:	List[str]										=       prepare_opt_inputs_dict(__a,		__a		)
        return config, inputs_dict






    def       _lowerCAmelCase (	self,		lowerCamelCase__,		lowerCamelCase__		):
        A			:	int										=       TFOPTModel(config=__a		)
        A			:	Optional[Any]										=       inputs_dict["""input_ids"""]

        A			:	List[Any]										=       input_ids[:1, :]
        A			:	Tuple										=       inputs_dict["""attention_mask"""][:1, :]
        A			:	Any										=       1

        # first forward pass
        A			:	Optional[Any]										=       model(__a,		attention_mask=__a,		use_cache=__a		)

        A					,							A			:	Tuple										=       outputs.to_tuple()

        # create hypothetical next token and extent to next_input_ids
        A			:	Union[str, Any]										=       ids_tensor((self.batch_size, 3),		config.vocab_size		)
        A			:	Optional[Any]										=       tf.cast(ids_tensor((self.batch_size, 3),		2		),		tf.inta		)

        # append to next input_ids and
        A			:	Optional[int]										=       tf.concat([input_ids, next_tokens],		axis=-1		)
        A			:	List[str]										=       tf.concat([attention_mask, next_attn_mask],		axis=-1		)

        A			:	str										=       model(__a,		attention_mask=__a		)[0]
        A			:	Dict										=       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
        A			:	List[Any]										=       int(ids_tensor((1,),		output_from_past.shape[-1]		)		)
        A			:	str										=       output_from_no_past[:, -3:, random_slice_idx]
        A			:	Union[str, Any]										=       output_from_past[:, :, random_slice_idx]

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






@require_tf
class SCREAMING_SNAKE_CASE__					(		SCREAMING_SNAKE_CASE__			,						SCREAMING_SNAKE_CASE__			,						unittest.TestCase   ):




    '''simple docstring'''






    __lowerCamelCase      :			int =				(TFOPTModel, TFOPTForCausalLM) if is_tf_available() else ()
    __lowerCamelCase      :			Any =				(TFOPTForCausalLM,) if is_tf_available() else ()
    __lowerCamelCase      :			Union[str, Any] =				(
        {"feature-extraction": TFOPTModel, "text-generation": TFOPTForCausalLM} if is_tf_available() else {}
    )
    __lowerCamelCase      :			List[str] =				False
    __lowerCamelCase      :			Dict =				False
    __lowerCamelCase      :			List[Any] =				False
    __lowerCamelCase      :			List[str] =				10






    def       _lowerCAmelCase (	self		):
        A			:	List[str]										=       TFOPTModelTester(self		)
        A			:	Union[str, Any]										=       ConfigTester(self,		config_class=__a		)






    def       _lowerCAmelCase (	self		):
        self.config_tester.run_common_tests()






    def       _lowerCAmelCase (	self		):
        A			:	Optional[int]										=       self.model_tester.prepare_config_and_inputs_for_common()
        self.model_tester.check_decoder_model_past_large_inputs(*__a		)






    def       _lowerCAmelCase (	self		):
        A					,							A			:	Any										=       self.model_tester.prepare_config_and_inputs_for_common()

        def _get_word_embedding_weight(lowerCamelCase__,		lowerCamelCase__		):
            if hasattr(__a,		"""weight"""		):
                return embedding_layer.weight
            else:
                # Here we build the word embeddings weights if not exists.
                # And then we retry to get the attribute once built.
                model.build()
                if hasattr(__a,		"""weight"""		):
                    return embedding_layer.weight
                else:
                    return None

        for model_class in self.all_model_classes:
            for size in [config.vocab_size - 10, config.vocab_size + 10]:
                # build the embeddings
                A			:	Any										=       model_class(config=__a		)
                A			:	List[Any]										=       _get_word_embedding_weight(__a,		model.get_input_embeddings()		)
                A			:	Dict										=       _get_word_embedding_weight(__a,		model.get_output_embeddings()		)

                # reshape the embeddings
                model.resize_token_embeddings(__a		)
                A			:	Tuple										=       _get_word_embedding_weight(__a,		model.get_input_embeddings()		)
                A			:	int										=       _get_word_embedding_weight(__a,		model.get_output_embeddings()		)

                # check that the resized embeddings size matches the desired size.
                A			:	Optional[Any]										=       size if size is not None else config.vocab_size

                self.assertEqual(new_input_embeddings.shape[0],		__a		)

                # check that weights remain the same after resizing
                A			:	Any										=       True
                for pa, pa in zip(old_input_embeddings.value(),		new_input_embeddings.value()		):
                    if tf.math.reduce_sum(tf.math.abs(pa - pa		)		) > 0:
                        A			:	Any										=       False
                self.assertTrue(__a		)

                if old_output_embeddings is not None and new_output_embeddings is not None:
                    self.assertEqual(new_output_embeddings.shape[0],		__a		)

                    A			:	Union[str, Any]										=       True
                    for pa, pa in zip(old_output_embeddings.value(),		new_output_embeddings.value()		):
                        if tf.math.reduce_sum(tf.math.abs(pa - pa		)		) > 0:
                            A			:	Union[str, Any]										=       False
                    self.assertTrue(__a		)




def      __UpperCamelCase     (  _lowerCAmelCase		)	->  Optional[int]:



    """simple docstring"""






    return tf.constant(__snake_case				,					dtype=tf.intaa		)






@require_tf
class SCREAMING_SNAKE_CASE__					(		unittest.TestCase   ):




    '''simple docstring'''






    __lowerCamelCase      :			str =				99






    def       _lowerCAmelCase (	self		):
        A			:	int										=       tf.ones((4, 1),		dtype=tf.intaa		) * 2
        A			:	Dict										=       tf.concat([ids_tensor((4, 6),		self.vocab_size - 3		) + 3, eos_column_vector],		axis=1		)
        A			:	Tuple										=       input_ids.shape[0]
        A			:	Optional[int]										=       OPTConfig(
            vocab_size=self.vocab_size,		hidden_size=24,		num_hidden_layers=2,		num_attention_heads=2,		ffn_dim=32,		max_position_embeddings=48,		eos_token_id=2,		pad_token_id=1,		bos_token_id=0,		)
        return config, input_ids, batch_size






@require_sentencepiece
@require_tf
class SCREAMING_SNAKE_CASE__					(		unittest.TestCase   ):




    '''simple docstring'''






    @slow
    def       _lowerCAmelCase (	self		):
        A			:	Any										=       TFOPTModel.from_pretrained("""facebook/opt-350m"""		)
        A			:	List[Any]										=       _long_tensor([[0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 2]]		)
        A			:	List[Any]										=       tf.not_equal(__a,		model.config.pad_token_id		)
        with tf.GradientTape():
            A			:	Optional[Any]										=       model(input_ids=__a,		attention_mask=__a		).last_hidden_state
        A			:	Dict										=       (1, 11, 512)
        self.assertEqual(output.shape,		__a		)
        A			:	int										=       tf.constant(
            [[-0.2873, -1.9218, -0.3033], [-1.2710, -0.1338, -0.1902], [0.4095, 0.1214, -1.3121]]		)
        self.assertTrue(np.allclose(output[:, :3, :3],		__a,		atol=4e-3		)		)

        A			:	Dict										=       tf.function(__a,		jit_compile=__a		)
        A			:	str										=       xla_generate(__a,		__a		)[0]
        self.assertTrue(np.allclose(output[:, :3, :3],		__a,		atol=4e-2		)		)






@require_tf
@slow
class SCREAMING_SNAKE_CASE__					(		unittest.TestCase   ):




    '''simple docstring'''






    def       _lowerCAmelCase (	self		):
        super().setUp()
        A			:	Union[str, Any]										=       """facebook/opt-350m"""






    def       _lowerCAmelCase (	self		):
        A			:	str										=       TFOPTForCausalLM.from_pretrained(self.path_model		)
        A			:	Optional[int]										=       GPTaTokenizer.from_pretrained(self.path_model		)

        A			:	Any										=       [
            """Today is a beautiful day and I want to""",
            """In the city of""",
            """Paris is the capital of France and""",
            """Computers and mobile phones have taken""",
        ]
        # verify that prompt without BOS token is identical to Metaseq -> add_special_tokens=False
        A			:	Optional[int]										=       tokenizer(__a,		return_tensors="""tf""",		padding=__a,		add_special_tokens=__a		)
        A			:	List[Any]										=       tf.math.reduce_mean(model(inputs.input_ids,		attention_mask=inputs.attention_mask		)[0],		axis=-1		)
        A			:	str										=       tf.constant(
            [
                [1.3851, -13.8923, -10.5229, -10.7533, -0.2309, -10.2384, -0.5365, -9.0947, -5.1670],
                [-4.7073, -10.6276, -3.9415, -21.5242, -0.2822, -0.2822, -0.2822, -0.2822, -0.2822],
                [0.6247, -3.4229, -8.9179, -1.4297, -14.1650, 1.4146, -9.0218, -0.2703, -0.2703],
                [6.4783, -1.9913, -10.7926, -2.3336, 1.5092, -0.9974, -6.8213, 1.3477, 1.3477],
            ]		)
        self.assertTrue(np.allclose(__a,		__a,		atol=1e-4		)		)

        A			:	List[str]										=       tf.function(__a,		jit_compile=__a		)
        A			:	List[str]										=       tf.math.reduce_mean(xla_generate(inputs.input_ids,		attention_mask=inputs.attention_mask		)[0],		axis=-1		)
        self.assertTrue(np.allclose(__a,		__a,		atol=1e-4		)		)






@require_tf
@slow
class SCREAMING_SNAKE_CASE__					(		unittest.TestCase   ):




    '''simple docstring'''






    @property
    def       _lowerCAmelCase (	self		):
        return [
            "Today is a beautiful day and I want",
            "In the city of",
            "Paris is the capital of France and",
            "Computers and mobile phones have taken",
        ]






    def       _lowerCAmelCase (	self		):
        A			:	Optional[int]										=       """facebook/opt-125m"""

        A			:	Optional[Any]										=       [
            """Today is a beautiful day and I want to""",
            """In the city of New York, the city""",
            """Paris is the capital of France and the capital""",
            """Computers and mobile phones have taken over the""",
        ]

        A			:	Optional[int]										=       []
        A			:	Union[str, Any]										=       GPTaTokenizer.from_pretrained(__a		)
        A			:	Optional[Any]										=       TFOPTForCausalLM.from_pretrained(__a		)

        for prompt in self.prompts:
            A			:	Optional[Any]										=       tokenizer(__a,		return_tensors="""tf"""		).input_ids

            A			:	int										=       model.generate(__a,		max_length=10		)

            A			:	str										=       tokenizer.batch_decode(__a,		skip_special_tokens=__a		)
            predicted_outputs += generated_string

        self.assertListEqual(__a,		__a		)






    def       _lowerCAmelCase (	self		):
        A			:	Optional[Any]										=       """facebook/opt-350m"""

        A			:	Any										=       GPTaTokenizer.from_pretrained(__a		)
        A			:	Optional[int]										=       TFOPTForCausalLM.from_pretrained(__a		)

        A			:	List[Any]										=       """left"""

        # use different length sentences to test batching
        A			:	List[Any]										=       [
            """Hello, my dog is a little""",
            """Today, I""",
        ]

        A			:	str										=       tokenizer(__a,		return_tensors="""tf""",		padding=__a		)
        A			:	Optional[int]										=       inputs["""input_ids"""]

        A			:	Optional[int]										=       model.generate(input_ids=__a,		attention_mask=inputs["""attention_mask"""]		)

        A			:	Tuple										=       tokenizer(sentences[0],		return_tensors="""tf"""		).input_ids
        A			:	Optional[Any]										=       model.generate(input_ids=__a		)

        A			:	Union[str, Any]										=       inputs_non_padded.shape[-1] - tf.math.reduce_sum(
            tf.cast(inputs["""attention_mask"""][-1],		tf.intaa		)		)
        A			:	Optional[Any]										=       tokenizer(sentences[1],		return_tensors="""tf"""		).input_ids
        A			:	int										=       model.generate(input_ids=__a,		max_length=model.config.max_length - num_paddings		)

        A			:	Optional[Any]										=       tokenizer.batch_decode(__a,		skip_special_tokens=__a		)
        A			:	str										=       tokenizer.decode(output_non_padded[0],		skip_special_tokens=__a		)
        A			:	Optional[int]										=       tokenizer.decode(output_padded[0],		skip_special_tokens=__a		)

        A			:	List[Any]										=       [
            """Hello, my dog is a little bit of a dork.\nI\'m a little bit""",
            """Today, I was in the middle of a conversation with a friend about the""",
        ]
        self.assertListEqual(__a,		__a		)
        self.assertListEqual(__a,		[non_padded_sentence, padded_sentence]		)






    def       _lowerCAmelCase (	self		):
        A			:	List[str]										=       """facebook/opt-350m"""

        A			:	Any										=       [
            """Today is a beautiful day and I want to""",
            """In the city of San Francisco, the city""",
            """Paris is the capital of France and the capital""",
            """Computers and mobile phones have taken over the""",
        ]

        A			:	str										=       []
        A			:	Optional[int]										=       GPTaTokenizer.from_pretrained(__a		)
        A			:	Tuple										=       TFOPTForCausalLM.from_pretrained(__a		)

        for prompt in self.prompts:
            A			:	Optional[int]										=       tokenizer(__a,		return_tensors="""tf"""		).input_ids

            A			:	str										=       model.generate(__a,		max_length=10		)

            A			:	List[str]										=       tokenizer.batch_decode(__a,		skip_special_tokens=__a		)
            predicted_outputs += generated_string

        self.assertListEqual(__a,		__a		)







import copy
from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging
from ..auto import CONFIG_MAPPING


SCREAMING_SNAKE_CASE_:Optional[Any]					=						logging.get_logger(__name__)

SCREAMING_SNAKE_CASE_:Union[str, Any]					=						{
    """microsoft/conditional-detr-resnet-50""": (
        """https://huggingface.co/microsoft/conditional-detr-resnet-50/resolve/main/config.json"""
    ),
}






class SCREAMING_SNAKE_CASE__					(		SCREAMING_SNAKE_CASE__   ):




    '''simple docstring'''






    __lowerCamelCase      :			Dict =				"conditional_detr"
    __lowerCamelCase      :			str =				["past_key_values"]
    __lowerCamelCase      :			str =				{
        "hidden_size": "d_model",
        "num_attention_heads": "encoder_attention_heads",
    }






    def __init__(	self,		lowerCamelCase__=True,		lowerCamelCase__=None,		lowerCamelCase__=3,		lowerCamelCase__=300,		lowerCamelCase__=6,		lowerCamelCase__=2048,		lowerCamelCase__=8,		lowerCamelCase__=6,		lowerCamelCase__=2048,		lowerCamelCase__=8,		lowerCamelCase__=0.0,		lowerCamelCase__=0.0,		lowerCamelCase__=True,		lowerCamelCase__="relu",		lowerCamelCase__=256,		lowerCamelCase__=0.1,		lowerCamelCase__=0.0,		lowerCamelCase__=0.0,		lowerCamelCase__=0.02,		lowerCamelCase__=1.0,		lowerCamelCase__=False,		lowerCamelCase__="sine",		lowerCamelCase__="resnet50",		lowerCamelCase__=True,		lowerCamelCase__=False,		lowerCamelCase__=2,		lowerCamelCase__=5,		lowerCamelCase__=2,		lowerCamelCase__=1,		lowerCamelCase__=1,		lowerCamelCase__=2,		lowerCamelCase__=5,		lowerCamelCase__=2,		lowerCamelCase__=0.25,		**lowerCamelCase__,		):
        if backbone_config is not None and use_timm_backbone:
            raise ValueError("""You can't specify both `backbone_config` and `use_timm_backbone`."""		)

        if not use_timm_backbone:
            if backbone_config is None:
                logger.info("""`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone."""		)
                A			:	List[Any]										=       CONFIG_MAPPING["""resnet"""](out_features=["""stage4"""]		)
            elif isinstance(lowerCamelCase__,		lowerCamelCase__		):
                A			:	Any										=       backbone_config.get("""model_type"""		)
                A			:	Optional[Any]										=       CONFIG_MAPPING[backbone_model_type]
                A			:	Tuple										=       config_class.from_dict(lowerCamelCase__		)

        A			:	Dict										=       use_timm_backbone
        A			:	int										=       backbone_config
        A			:	Union[str, Any]										=       num_channels
        A			:	Optional[Any]										=       num_queries
        A			:	Union[str, Any]										=       d_model
        A			:	str										=       encoder_ffn_dim
        A			:	List[Any]										=       encoder_layers
        A			:	Tuple										=       encoder_attention_heads
        A			:	Union[str, Any]										=       decoder_ffn_dim
        A			:	Tuple										=       decoder_layers
        A			:	int										=       decoder_attention_heads
        A			:	Union[str, Any]										=       dropout
        A			:	List[str]										=       attention_dropout
        A			:	Optional[int]										=       activation_dropout
        A			:	Optional[Any]										=       activation_function
        A			:	Any										=       init_std
        A			:	List[Any]										=       init_xavier_std
        A			:	Any										=       encoder_layerdrop
        A			:	List[str]										=       decoder_layerdrop
        A			:	int										=       encoder_layers
        A			:	Union[str, Any]										=       auxiliary_loss
        A			:	Union[str, Any]										=       position_embedding_type
        A			:	Tuple										=       backbone
        A			:	Dict										=       use_pretrained_backbone
        A			:	int										=       dilation
        # Hungarian matcher
        A			:	List[Any]										=       class_cost
        A			:	List[Any]										=       bbox_cost
        A			:	int										=       giou_cost
        # Loss coefficients
        A			:	List[Any]										=       mask_loss_coefficient
        A			:	Any										=       dice_loss_coefficient
        A			:	int										=       cls_loss_coefficient
        A			:	Tuple										=       bbox_loss_coefficient
        A			:	List[Any]										=       giou_loss_coefficient
        A			:	int										=       focal_alpha
        super().__init__(is_encoder_decoder=lowerCamelCase__,		**lowerCamelCase__		)






    @property
    def       _lowerCAmelCase (	self		):
        return self.encoder_attention_heads






    @property
    def       _lowerCAmelCase (	self		):
        return self.d_model






    def       _lowerCAmelCase (	self		):
        A			:	Dict										=       copy.deepcopy(self.__dict__		)
        if self.backbone_config is not None:
            A			:	List[Any]										=       self.backbone_config.to_dict()
        A			:	List[str]										=       self.__class__.model_type
        return output






class SCREAMING_SNAKE_CASE__					(		SCREAMING_SNAKE_CASE__   ):




    '''simple docstring'''






    __lowerCamelCase      :			Tuple =				version.parse("1.11"   )






    @property
    def       _lowerCAmelCase (	self		):
        return OrderedDict(
            [
                ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}),
                ("""pixel_mask""", {0: """batch"""}),
            ]		)






    @property
    def       _lowerCAmelCase (	self		):
        return 1e-5






    @property
    def       _lowerCAmelCase (	self		):
        return 12


