

"""simple docstring"""






from __future__ import annotations

import inspect
import unittest

import numpy as np

from transformers import ResNetConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

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


if is_tf_available():
 import tensorflow as tf

 from transformers import TFResNetForImageClassification, TFResNetModel
 from transformers.models.resnet.modeling_tf_resnet import TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
 from PIL import Image

 from transformers import AutoImageProcessor







class 	__UpperCamelCase    :

  def __init__(		self    ,_A    ,_A=3    ,_A=32    ,_A=3    ,_A=10    ,_A=[10, 20, 30, 40]    ,_A=[1, 1, 2, 1]    ,_A=True    ,_A=True    ,_A="relu"    ,_A=3    ,_A=None    ,):
      '''simple docstring'''

      _lowerCAmelCase       :   Union[str, Any]  =    parent
      _lowerCAmelCase       :   int  =    batch_size
      _lowerCAmelCase       :   int  =    image_size
      _lowerCAmelCase       :   List[str]  =    num_channels
      _lowerCAmelCase       :   Optional[int]  =    embeddings_size
      _lowerCAmelCase       :   Optional[int]  =    hidden_sizes
      _lowerCAmelCase       :   str  =    depths
      _lowerCAmelCase       :   str  =    is_training
      _lowerCAmelCase       :   int  =    use_labels
      _lowerCAmelCase       :   Optional[int]  =    hidden_act
      _lowerCAmelCase       :   Optional[int]  =    num_labels
      _lowerCAmelCase       :   Dict  =    scope
      _lowerCAmelCase       :   Union[str, Any]  =    len(_A      )

  def __lowerCamelCase		(		self      ):
      '''simple docstring'''

      _lowerCAmelCase       :   Optional[int]  =    floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]      )

      _lowerCAmelCase       :   Optional[Any]  =    None
      if self.use_labels:
          _lowerCAmelCase       :   List[Any]  =    ids_tensor([self.batch_size]    ,self.num_labels      )

      _lowerCAmelCase       :   Union[str, Any]  =    self.get_config()

      return config, pixel_values, labels

  def __lowerCamelCase		(		self      ):
      '''simple docstring'''

      return ResNetConfig(
          num_channels=self.num_channels    ,embeddings_size=self.embeddings_size    ,hidden_sizes=self.hidden_sizes    ,depths=self.depths    ,hidden_act=self.hidden_act    ,num_labels=self.num_labels    ,image_size=self.image_size    ,)

  def __lowerCamelCase		(		self    ,_A    ,_A    ,_A      ):
      '''simple docstring'''

      _lowerCAmelCase       :   Optional[Any]  =    TFResNetModel(config=_A      )
      _lowerCAmelCase       :   List[str]  =    model(_A      )
      # 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 __lowerCamelCase		(		self    ,_A    ,_A    ,_A      ):
      '''simple docstring'''

      _lowerCAmelCase       :   Any  =    self.num_labels
      _lowerCAmelCase       :   Dict  =    TFResNetForImageClassification(_A      )
      _lowerCAmelCase       :   int  =    model(_A    ,labels=_A      )
      self.parent.assertEqual(result.logits.shape    ,(self.batch_size, self.num_labels)      )







  def __lowerCamelCase		(		self      ):
      '''simple docstring'''

      _lowerCAmelCase       :   Union[str, Any]  =    self.prepare_config_and_inputs()
      _lowerCAmelCase,					_lowerCAmelCase,					_lowerCAmelCase       :   Tuple  =    config_and_inputs
      _lowerCAmelCase       :   Any  =    {'pixel_values': pixel_values}
      return config, inputs_dict







@require_tf
class 	__UpperCamelCase    (    a__							,							a__							,							unittest.TestCase						):
  _UpperCAmelCase          =		(TFResNetModel, TFResNetForImageClassification) if is_tf_available() else ()
  _UpperCAmelCase          =		(
      {"feature-extraction": TFResNetModel, "image-classification": TFResNetForImageClassification}
      if is_tf_available()
      else {}
  )

  _UpperCAmelCase          =		False
  _UpperCAmelCase          =		False
  _UpperCAmelCase          =		False
  _UpperCAmelCase          =		False
  _UpperCAmelCase          =		False

  def __lowerCamelCase		(		self      ):
      '''simple docstring'''

      _lowerCAmelCase       :   List[str]  =    TFResNetModelTester(self      )
      _lowerCAmelCase       :   List[str]  =    ConfigTester(self    ,config_class=_A    ,has_text_modality=_A      )

  def __lowerCamelCase		(		self      ):
      '''simple docstring'''

      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 __lowerCamelCase		(		self      ):
      '''simple docstring'''

      return

  @unittest.skip(reason='ResNet does not use inputs_embeds'      )
  def __lowerCamelCase		(		self      ):
      '''simple docstring'''

      pass

  @unittest.skip(reason='ResNet does not support input and output embeddings'      )
  def __lowerCamelCase		(		self      ):
      '''simple docstring'''

      pass

  def __lowerCamelCase		(		self      ):
      '''simple docstring'''

      _lowerCAmelCase,					_lowerCAmelCase       :   Optional[int]  =    self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes:
          _lowerCAmelCase       :   int  =    model_class(_A      )
          _lowerCAmelCase       :   Union[str, Any]  =    inspect.signature(model.call      )
          # signature.parameters is an OrderedDict => so arg_names order is deterministic
          _lowerCAmelCase       :   Any  =    [*signature.parameters.keys()]

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

  def __lowerCamelCase		(		self      ):
      '''simple docstring'''

      _lowerCAmelCase       :   Dict  =    self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*_A      )

  def __lowerCamelCase		(		self      ):
      '''simple docstring'''

      def check_hidden_states_output(_A    ,_A    ,_A      ):
          _lowerCAmelCase       :   int  =    model_class(_A      )
          _lowerCAmelCase       :   int  =    model(**self._prepare_for_class(_A    ,_A      )      )

          _lowerCAmelCase       :   Dict  =    outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

          _lowerCAmelCase       :   int  =    self.model_tester.num_stages
          self.assertEqual(len(_A      )    ,expected_num_stages + 1      )

          # ResNet'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]    ,)

      _lowerCAmelCase,					_lowerCAmelCase       :   List[str]  =    self.model_tester.prepare_config_and_inputs_for_common()
      _lowerCAmelCase       :   Any  =    ['basic', 'bottleneck']
      for model_class in self.all_model_classes:
          for layer_type in layers_type:
              _lowerCAmelCase       :   Optional[int]  =    layer_type
              _lowerCAmelCase       :   Tuple  =    True
              check_hidden_states_output(_A    ,_A    ,_A      )

              # check that output_hidden_states also work using config
              del inputs_dict["output_hidden_states"]
              _lowerCAmelCase       :   Any  =    True

              check_hidden_states_output(_A    ,_A    ,_A      )

  def __lowerCamelCase		(		self      ):
      '''simple docstring'''

      _lowerCAmelCase       :   str  =    self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_image_classification(*_A      )







  @slow
  def __lowerCamelCase		(		self      ):
      '''simple docstring'''

      for model_name in TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
          _lowerCAmelCase       :   Optional[Any]  =    TFResNetModel.from_pretrained(_A      )
          self.assertIsNotNone(_A      )





def 		lowerCamelCase__		(					):

    '''simple docstring'''



    _lowerCAmelCase       :   Tuple  =    Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'   )
    return image







@require_tf
@require_vision
class 	__UpperCamelCase    (    unittest.TestCase						):

  @cached_property
  def __lowerCamelCase		(		self      ):
      '''simple docstring'''

      return (
          AutoImageProcessor.from_pretrained(TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]      )
          if is_vision_available()
          else None
      )







  @slow
  def __lowerCamelCase		(		self      ):
      '''simple docstring'''

      _lowerCAmelCase       :   str  =    TFResNetForImageClassification.from_pretrained(TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]      )

      _lowerCAmelCase       :   Tuple  =    self.default_image_processor
      _lowerCAmelCase       :   Optional[Any]  =    prepare_img()
      _lowerCAmelCase       :   int  =    image_processor(images=_A    ,return_tensors='tf'      )

      # forward pass
      _lowerCAmelCase       :   int  =    model(**_A      )

      # verify the logits
      _lowerCAmelCase       :   Optional[Any]  =    tf.TensorShape((1, 1000)      )
      self.assertEqual(outputs.logits.shape    ,_A      )

      _lowerCAmelCase       :   Any  =    tf.constant([-1_1.1_0_6_9, -9.7_8_7_7, -8.3_7_7_7]      )

      self.assertTrue(np.allclose(outputs.logits[0, :3].numpy()    ,_A    ,atol=1E-4      )      )








"""simple docstring"""






import math

import torch
from torch import nn

from ..configuration_utils import ConfigMixin, register_to_config
from .attention_processor import Attention
from .embeddings import get_timestep_embedding
from .modeling_utils import ModelMixin







class 	__UpperCamelCase    (    a__							,							a__						):

  @register_to_config
  def __init__(		self    ,_A = 128    ,_A = 256    ,_A = 2_0_0_0.0    ,_A = 768    ,_A = 12    ,_A = 12    ,_A = 64    ,_A = 2048    ,_A = 0.1    ,):
      '''simple docstring'''

      super().__init__()

      _lowerCAmelCase       :   int  =    nn.Sequential(
          nn.Linear(_A    ,d_model * 4    ,bias=_A      )    ,nn.SiLU()    ,nn.Linear(d_model * 4    ,d_model * 4    ,bias=_A      )    ,nn.SiLU()    ,)

      _lowerCAmelCase       :   Any  =    nn.Embedding(_A    ,_A      )
      _lowerCAmelCase       :   Tuple  =    False

      _lowerCAmelCase       :   Union[str, Any]  =    nn.Linear(_A    ,_A    ,bias=_A      )

      _lowerCAmelCase       :   int  =    nn.Dropout(p=_A      )

      _lowerCAmelCase       :   int  =    nn.ModuleList()
      for lyr_num in range(_A      ):
          # FiLM conditional T5 decoder
          _lowerCAmelCase       :   Any  =    DecoderLayer(d_model=_A    ,d_kv=_A    ,num_heads=_A    ,d_ff=_A    ,dropout_rate=_A      )
          self.decoders.append(_A      )

      _lowerCAmelCase       :   Optional[Any]  =    TaLayerNorm(_A      )

      _lowerCAmelCase       :   List[str]  =    nn.Dropout(p=_A      )
      _lowerCAmelCase       :   Optional[Any]  =    nn.Linear(_A    ,_A    ,bias=_A      )

  def __lowerCamelCase		(		self    ,_A    ,_A      ):
      '''simple docstring'''

      _lowerCAmelCase       :   Dict  =    torch.mul(query_input.unsqueeze(-1      )    ,key_input.unsqueeze(-2      )      )
      return mask.unsqueeze(-3      )







  def __lowerCamelCase		(		self    ,_A    ,_A    ,_A      ):
      '''simple docstring'''

      _lowerCAmelCase,					_lowerCAmelCase,					_lowerCAmelCase       :   Dict  =    decoder_input_tokens.shape
      assert decoder_noise_time.shape == (batch,)

      # decoder_noise_time is in [0, 1), so rescale to expected timing range.
      _lowerCAmelCase       :   Any  =    get_timestep_embedding(
          decoder_noise_time * self.config.max_decoder_noise_time    ,embedding_dim=self.config.d_model    ,max_period=self.config.max_decoder_noise_time    ,).to(dtype=self.dtype      )

      _lowerCAmelCase       :   Union[str, Any]  =    self.conditioning_emb(_A      ).unsqueeze(1      )

      assert conditioning_emb.shape == (batch, 1, self.config.d_model * 4)

      _lowerCAmelCase       :   str  =    decoder_input_tokens.shape[1]

      # If we want to use relative positions for audio context, we can just offset
      # this sequence by the length of encodings_and_masks.
      _lowerCAmelCase       :   Union[str, Any]  =    torch.broadcast_to(
          torch.arange(_A    ,device=decoder_input_tokens.device      )    ,(batch, seq_length)    ,)

      _lowerCAmelCase       :   Any  =    self.position_encoding(_A      )

      _lowerCAmelCase       :   str  =    self.continuous_inputs_projection(_A      )
      inputs += position_encodings
      _lowerCAmelCase       :   int  =    self.dropout(_A      )

      # decoder: No padding present.
      _lowerCAmelCase       :   Union[str, Any]  =    torch.ones(
          decoder_input_tokens.shape[:2]    ,device=decoder_input_tokens.device    ,dtype=inputs.dtype      )

      # Translate encoding masks to encoder-decoder masks.
      _lowerCAmelCase       :   Optional[Any]  =    [(x, self.encoder_decoder_mask(_A    ,_A      )) for x, y in encodings_and_masks]

      # cross attend style: concat encodings
      _lowerCAmelCase       :   Dict  =    torch.cat([x[0] for x in encodings_and_encdec_masks]    ,dim=1      )
      _lowerCAmelCase       :   Tuple  =    torch.cat([x[1] for x in encodings_and_encdec_masks]    ,dim=-1      )

      for lyr in self.decoders:
          _lowerCAmelCase       :   Tuple  =    lyr(
              _A    ,conditioning_emb=_A    ,encoder_hidden_states=_A    ,encoder_attention_mask=_A    ,)[0]

      _lowerCAmelCase       :   Any  =    self.decoder_norm(_A      )
      _lowerCAmelCase       :   List[Any]  =    self.post_dropout(_A      )

      _lowerCAmelCase       :   int  =    self.spec_out(_A      )
      return spec_out







class 	__UpperCamelCase    (    nn.Module						):

  def __init__(		self    ,_A    ,_A    ,_A    ,_A    ,_A    ,_A=1E-6      ):
      '''simple docstring'''

      super().__init__()
      _lowerCAmelCase       :   Optional[Any]  =    nn.ModuleList()

      # cond self attention: layer 0
      self.layer.append(
          TaLayerSelfAttentionCond(d_model=_A    ,d_kv=_A    ,num_heads=_A    ,dropout_rate=_A      )      )

      # cross attention: layer 1
      self.layer.append(
          TaLayerCrossAttention(
              d_model=_A    ,d_kv=_A    ,num_heads=_A    ,dropout_rate=_A    ,layer_norm_epsilon=_A    ,)      )

      # Film Cond MLP + dropout: last layer
      self.layer.append(
          TaLayerFFCond(d_model=_A    ,d_ff=_A    ,dropout_rate=_A    ,layer_norm_epsilon=_A      )      )







  def __lowerCamelCase		(		self    ,_A    ,_A=None    ,_A=None    ,_A=None    ,_A=None    ,_A=None    ,):
      '''simple docstring'''

      _lowerCAmelCase       :   Any  =    self.layer[0](
          _A    ,conditioning_emb=_A    ,attention_mask=_A    ,)

      if encoder_hidden_states is not None:
          _lowerCAmelCase       :   Any  =    torch.where(encoder_attention_mask > 0    ,0    ,-1E10      ).to(
              encoder_hidden_states.dtype      )

          _lowerCAmelCase       :   str  =    self.layer[1](
              _A    ,key_value_states=_A    ,attention_mask=_A    ,)

      # Apply Film Conditional Feed Forward layer
      _lowerCAmelCase       :   Optional[Any]  =    self.layer[-1](_A    ,_A      )

      return (hidden_states,)







class 	__UpperCamelCase    (    nn.Module						):

  def __init__(		self    ,_A    ,_A    ,_A    ,_A      ):
      '''simple docstring'''

      super().__init__()
      _lowerCAmelCase       :   Union[str, Any]  =    TaLayerNorm(_A      )
      _lowerCAmelCase       :   Any  =    TaFiLMLayer(in_features=d_model * 4    ,out_features=_A      )
      _lowerCAmelCase       :   Dict  =    Attention(query_dim=_A    ,heads=_A    ,dim_head=_A    ,out_bias=_A    ,scale_qk=_A      )
      _lowerCAmelCase       :   Tuple  =    nn.Dropout(_A      )







  def __lowerCamelCase		(		self    ,_A    ,_A=None    ,_A=None    ,):
      '''simple docstring'''

      _lowerCAmelCase       :   int  =    self.layer_norm(_A      )

      if conditioning_emb is not None:
          _lowerCAmelCase       :   Union[str, Any]  =    self.FiLMLayer(_A    ,_A      )

      # Self-attention block
      _lowerCAmelCase       :   Union[str, Any]  =    self.attention(_A      )

      _lowerCAmelCase       :   Optional[Any]  =    hidden_states + self.dropout(_A      )

      return hidden_states







class 	__UpperCamelCase    (    nn.Module						):

  def __init__(		self    ,_A    ,_A    ,_A    ,_A    ,_A      ):
      '''simple docstring'''

      super().__init__()
      _lowerCAmelCase       :   List[str]  =    Attention(query_dim=_A    ,heads=_A    ,dim_head=_A    ,out_bias=_A    ,scale_qk=_A      )
      _lowerCAmelCase       :   Optional[int]  =    TaLayerNorm(_A    ,eps=_A      )
      _lowerCAmelCase       :   Tuple  =    nn.Dropout(_A      )







  def __lowerCamelCase		(		self    ,_A    ,_A=None    ,_A=None    ,):
      '''simple docstring'''

      _lowerCAmelCase       :   Union[str, Any]  =    self.layer_norm(_A      )
      _lowerCAmelCase       :   str  =    self.attention(
          _A    ,encoder_hidden_states=_A    ,attention_mask=attention_mask.squeeze(1      )    ,)
      _lowerCAmelCase       :   Any  =    hidden_states + self.dropout(_A      )
      return layer_output







class 	__UpperCamelCase    (    nn.Module						):

  def __init__(		self    ,_A    ,_A    ,_A    ,_A      ):
      '''simple docstring'''

      super().__init__()
      _lowerCAmelCase       :   Optional[int]  =    TaDenseGatedActDense(d_model=_A    ,d_ff=_A    ,dropout_rate=_A      )
      _lowerCAmelCase       :   Tuple  =    TaFiLMLayer(in_features=d_model * 4    ,out_features=_A      )
      _lowerCAmelCase       :   Any  =    TaLayerNorm(_A    ,eps=_A      )
      _lowerCAmelCase       :   Union[str, Any]  =    nn.Dropout(_A      )







  def __lowerCamelCase		(		self    ,_A    ,_A=None      ):
      '''simple docstring'''

      _lowerCAmelCase       :   int  =    self.layer_norm(_A      )
      if conditioning_emb is not None:
          _lowerCAmelCase       :   Union[str, Any]  =    self.film(_A    ,_A      )

      _lowerCAmelCase       :   str  =    self.DenseReluDense(_A      )
      _lowerCAmelCase       :   Tuple  =    hidden_states + self.dropout(_A      )
      return hidden_states







class 	__UpperCamelCase    (    nn.Module						):

  def __init__(		self    ,_A    ,_A    ,_A      ):
      '''simple docstring'''

      super().__init__()
      _lowerCAmelCase       :   Union[str, Any]  =    nn.Linear(_A    ,_A    ,bias=_A      )
      _lowerCAmelCase       :   Any  =    nn.Linear(_A    ,_A    ,bias=_A      )
      _lowerCAmelCase       :   Union[str, Any]  =    nn.Linear(_A    ,_A    ,bias=_A      )
      _lowerCAmelCase       :   Union[str, Any]  =    nn.Dropout(_A      )
      _lowerCAmelCase       :   int  =    NewGELUActivation()







  def __lowerCamelCase		(		self    ,_A      ):
      '''simple docstring'''

      _lowerCAmelCase       :   Tuple  =    self.act(self.wi_a(_A      )      )
      _lowerCAmelCase       :   Optional[int]  =    self.wi_a(_A      )
      _lowerCAmelCase       :   Union[str, Any]  =    hidden_gelu * hidden_linear
      _lowerCAmelCase       :   Dict  =    self.dropout(_A      )

      _lowerCAmelCase       :   Dict  =    self.wo(_A      )
      return hidden_states







class 	__UpperCamelCase    (    nn.Module						):

  def __init__(		self    ,_A    ,_A=1E-6      ):
      '''simple docstring'''

      super().__init__()
      _lowerCAmelCase       :   Union[str, Any]  =    nn.Parameter(torch.ones(_A      )      )
      _lowerCAmelCase       :   Optional[int]  =    eps







  def __lowerCamelCase		(		self    ,_A      ):
      '''simple docstring'''

      _lowerCAmelCase       :   Optional[int]  =    hidden_states.to(torch.floataa      ).pow(2      ).mean(-1    ,keepdim=_A      )
      _lowerCAmelCase       :   List[Any]  =    hidden_states * torch.rsqrt(variance + self.variance_epsilon      )

      # convert into half-precision if necessary
      if self.weight.dtype in [torch.floataa, torch.bfloataa]:
          _lowerCAmelCase       :   Optional[int]  =    hidden_states.to(self.weight.dtype      )

      return self.weight * hidden_states







class 	__UpperCamelCase    (    nn.Module						):

  def __lowerCamelCase		(		self    ,_A      ):
      '''simple docstring'''

      return 0.5 * input * (1.0 + torch.tanh(math.sqrt(2.0 / math.pi      ) * (input + 0.0_4_4_7_1_5 * torch.pow(_A    ,3.0      ))      ))







class 	__UpperCamelCase    (    nn.Module						):

  def __init__(		self    ,_A    ,_A      ):
      '''simple docstring'''

      super().__init__()
      _lowerCAmelCase       :   List[str]  =    nn.Linear(_A    ,out_features * 2    ,bias=_A      )







  def __lowerCamelCase		(		self    ,_A    ,_A      ):
      '''simple docstring'''

      _lowerCAmelCase       :   List[Any]  =    self.scale_bias(_A      )
      _lowerCAmelCase,					_lowerCAmelCase       :   List[Any]  =    torch.chunk(_A    ,2    ,-1      )
      _lowerCAmelCase       :   List[Any]  =    x * (1 + scale) + shift
      return x





