

import unittest
from unittest.util import safe_repr

from transformers import AutoTokenizer, RwkvConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
       import torch

       from transformers import (
           RWKV_PRETRAINED_MODEL_ARCHIVE_LIST,
           RwkvForCausalLM,
           RwkvModel,
       )
       from transformers.pytorch_utils import is_torch_greater_or_equal_than_2_0
else:
       UpperCamelCase__					: Any								=      False



class  __snake_case					:

  def __init__(   self  ,						_A  ,						_A=14  ,						_A=7  ,						_A=True  ,						_A=False  ,						_A=True  ,						_A=True  ,						_A=True  ,						_A=99  ,						_A=32  ,						_A=5  ,						_A=37  ,						_A="gelu"  ,						_A=0.1  ,						_A=0.1  ,						_A=512  ,						_A=16  ,						_A=2  ,						_A=3  ,						_A=4  ,						_A=None  ,						):
      SCREAMING_SNAKE_CASE_													=  parent
      SCREAMING_SNAKE_CASE_													=  batch_size
      SCREAMING_SNAKE_CASE_													=  seq_length
      SCREAMING_SNAKE_CASE_													=  is_training
      SCREAMING_SNAKE_CASE_													=  use_token_type_ids
      SCREAMING_SNAKE_CASE_													=  use_input_mask
      SCREAMING_SNAKE_CASE_													=  use_labels
      SCREAMING_SNAKE_CASE_													=  use_mc_token_ids
      SCREAMING_SNAKE_CASE_													=  vocab_size
      SCREAMING_SNAKE_CASE_													=  hidden_size
      SCREAMING_SNAKE_CASE_													=  num_hidden_layers
      SCREAMING_SNAKE_CASE_													=  intermediate_size
      SCREAMING_SNAKE_CASE_													=  hidden_act
      SCREAMING_SNAKE_CASE_													=  hidden_dropout_prob
      SCREAMING_SNAKE_CASE_													=  attention_probs_dropout_prob
      SCREAMING_SNAKE_CASE_													=  max_position_embeddings
      SCREAMING_SNAKE_CASE_													=  type_vocab_size
      SCREAMING_SNAKE_CASE_													=  type_sequence_label_size
      SCREAMING_SNAKE_CASE_													=  num_labels
      SCREAMING_SNAKE_CASE_													=  num_choices
      SCREAMING_SNAKE_CASE_													=  scope
      SCREAMING_SNAKE_CASE_													=  vocab_size - 1
      SCREAMING_SNAKE_CASE_													=  vocab_size - 1
      SCREAMING_SNAKE_CASE_													=  vocab_size - 1

  def   lowerCAmelCase__       (   self):
      return RwkvConfig.from_pretrained('sgugger/rwkv-4-pile-7b')

  def   lowerCAmelCase__       (   self  ,						_A=False  ,						_A=False  ,						_A=False):
      SCREAMING_SNAKE_CASE_													=  ids_tensor([self.batch_size, self.seq_length]  ,						self.vocab_size)

      SCREAMING_SNAKE_CASE_													=  None
      if self.use_input_mask:
          SCREAMING_SNAKE_CASE_													=  random_attention_mask([self.batch_size, self.seq_length])

      SCREAMING_SNAKE_CASE_													=  None
      if self.use_token_type_ids:
          SCREAMING_SNAKE_CASE_													=  ids_tensor([self.batch_size, self.seq_length]  ,						self.type_vocab_size)

      SCREAMING_SNAKE_CASE_													=  None
      if self.use_mc_token_ids:
          SCREAMING_SNAKE_CASE_													=  ids_tensor([self.batch_size, self.num_choices]  ,						self.seq_length)

      SCREAMING_SNAKE_CASE_													=  None
      SCREAMING_SNAKE_CASE_													=  None
      SCREAMING_SNAKE_CASE_													=  None
      if self.use_labels:
          SCREAMING_SNAKE_CASE_													=  ids_tensor([self.batch_size]  ,						self.type_sequence_label_size)
          SCREAMING_SNAKE_CASE_													=  ids_tensor([self.batch_size, self.seq_length]  ,						self.num_labels)
          SCREAMING_SNAKE_CASE_													=  ids_tensor([self.batch_size]  ,						self.num_choices)

      SCREAMING_SNAKE_CASE_													=  self.get_config(
          gradient_checkpointing=_A  ,						scale_attn_by_inverse_layer_idx=_A  ,						reorder_and_upcast_attn=_A  ,						)

      return (
          config,
          input_ids,
          input_mask,
          None,
          token_type_ids,
          mc_token_ids,
          sequence_labels,
          token_labels,
          choice_labels,
      )

  def   lowerCAmelCase__       (   self  ,						_A=False  ,						_A=False  ,						_A=False):
      return RwkvConfig(
          vocab_size=self.vocab_size  ,						hidden_size=self.hidden_size  ,						num_hidden_layers=self.num_hidden_layers  ,						intermediate_size=self.intermediate_size  ,						activation_function=self.hidden_act  ,						resid_pdrop=self.hidden_dropout_prob  ,						attn_pdrop=self.attention_probs_dropout_prob  ,						n_positions=self.max_position_embeddings  ,						type_vocab_size=self.type_vocab_size  ,						use_cache=_A  ,						bos_token_id=self.bos_token_id  ,						eos_token_id=self.eos_token_id  ,						pad_token_id=self.pad_token_id  ,						gradient_checkpointing=_A  ,						scale_attn_by_inverse_layer_idx=_A  ,						reorder_and_upcast_attn=_A  ,						)

  def   lowerCAmelCase__       (   self):
      SCREAMING_SNAKE_CASE_													=  self.get_config()
      SCREAMING_SNAKE_CASE_													=  300
      return config

  def   lowerCAmelCase__       (   self):
      (
          (
          SCREAMING_SNAKE_CASE_
      )							,     (
          SCREAMING_SNAKE_CASE_
      )							,     (
          SCREAMING_SNAKE_CASE_
      )							,     (
          SCREAMING_SNAKE_CASE_
      )							,     (
          SCREAMING_SNAKE_CASE_
      )							,     (
          SCREAMING_SNAKE_CASE_
      )							,     (
          SCREAMING_SNAKE_CASE_
      )							,     (
          SCREAMING_SNAKE_CASE_
      )							,     (
          SCREAMING_SNAKE_CASE_
      )							,     
      )													=  self.prepare_config_and_inputs()

      SCREAMING_SNAKE_CASE_													=  floats_tensor([self.batch_size, self.seq_length, self.hidden_size])
      SCREAMING_SNAKE_CASE_													=  ids_tensor([self.batch_size, self.seq_length]  ,						vocab_size=2)

      return (
          config,
          input_ids,
          input_mask,
          head_mask,
          token_type_ids,
          sequence_labels,
          token_labels,
          choice_labels,
          encoder_hidden_states,
          encoder_attention_mask,
      )

  def   lowerCAmelCase__       (   self  ,						_A  ,						_A  ,						_A  ,						_A  ,						_A  ,						*_A):
      SCREAMING_SNAKE_CASE_													=  True
      SCREAMING_SNAKE_CASE_													=  RwkvModel(config=_A)
      model.to(_A)
      model.eval()

      SCREAMING_SNAKE_CASE_													=  model(_A)

      self.parent.assertEqual(result.last_hidden_state.shape  ,						(self.batch_size, self.seq_length, self.hidden_size))
      self.parent.assertEqual(len(result.hidden_states)  ,						config.num_hidden_layers + 1)

  def   lowerCAmelCase__       (   self  ,						_A  ,						_A  ,						_A  ,						_A  ,						_A  ,						*_A):
      SCREAMING_SNAKE_CASE_													=  RwkvForCausalLM(_A)
      model.to(_A)
      model.eval()

      SCREAMING_SNAKE_CASE_													=  model(_A  ,						labels=_A)
      self.parent.assertEqual(result.loss.shape  ,						())
      self.parent.assertEqual(result.logits.shape  ,						(self.batch_size, self.seq_length, self.vocab_size))

  def   lowerCAmelCase__       (   self  ,						_A  ,						_A  ,						_A  ,						_A  ,						_A  ,						*_A):
      SCREAMING_SNAKE_CASE_													=  RwkvModel(config=_A)
      model.to(_A)
      model.eval()

      SCREAMING_SNAKE_CASE_													=  model(_A)
      SCREAMING_SNAKE_CASE_													=  outputs.last_hidden_state

      SCREAMING_SNAKE_CASE_													=  model(input_ids[:, :2])
      SCREAMING_SNAKE_CASE_													=  outputs.last_hidden_state

      # Using the state computed on the first inputs, we will get the same output
      SCREAMING_SNAKE_CASE_													=  model(input_ids[:, 2:]  ,						state=outputs.state)
      SCREAMING_SNAKE_CASE_													=  outputs.last_hidden_state

      self.parent.assertTrue(torch.allclose(torch.cat([output_one, output_two]  ,						dim=1)  ,						_A  ,						atol=1E-5))

  def   lowerCAmelCase__       (   self  ,						_A  ,						_A  ,						_A  ,						_A  ,						_A  ,						*_A  ,						_A=False):
      SCREAMING_SNAKE_CASE_													=  RwkvForCausalLM(_A)
      model.to(_A)
      if gradient_checkpointing:
          model.gradient_checkpointing_enable()

      SCREAMING_SNAKE_CASE_													=  model(_A  ,						labels=_A)
      self.parent.assertEqual(result.loss.shape  ,						())
      self.parent.assertEqual(result.logits.shape  ,						(self.batch_size, self.seq_length, self.vocab_size))
      result.loss.backward()


  def   lowerCAmelCase__       (   self):
      SCREAMING_SNAKE_CASE_													=  self.prepare_config_and_inputs()

      (
          (
          SCREAMING_SNAKE_CASE_
      )							,     (
          SCREAMING_SNAKE_CASE_
      )							,     (
          SCREAMING_SNAKE_CASE_
      )							,     (
          SCREAMING_SNAKE_CASE_
      )							,     (
          SCREAMING_SNAKE_CASE_
      )							,     (
          SCREAMING_SNAKE_CASE_
      )							,     (
          SCREAMING_SNAKE_CASE_
      )							,     (
          SCREAMING_SNAKE_CASE_
      )							,     (
          SCREAMING_SNAKE_CASE_
      )							,     
      )													=  config_and_inputs

      SCREAMING_SNAKE_CASE_													=  {'input_ids': input_ids}

      return config, inputs_dict






@unittest.skipIf(
    not is_torch_greater_or_equal_than_2_0       ,	reason='See https://github.com/huggingface/transformers/pull/24204'    )
@require_torch
class  __snake_case					(      lowerCAmelCase__       ,	lowerCAmelCase__       ,	lowerCAmelCase__       ,	unittest.TestCase    ):
  __lowerCAmelCase       :		Any  	=					(RwkvModel, RwkvForCausalLM) if is_torch_available() else ()
  __lowerCAmelCase       :		Dict  	=					(
      {'feature-extraction': RwkvModel, 'text-generation': RwkvForCausalLM} if is_torch_available() else {}
  )
  # all_generative_model_classes = (RwkvForCausalLM,) if is_torch_available() else ()
  __lowerCAmelCase       :		str  	=					False
  __lowerCAmelCase       :		Union[str, Any]  	=					False
  __lowerCAmelCase       :		List[str]  	=					False
  __lowerCAmelCase       :		str  	=					False
  __lowerCAmelCase       :		List[Any]  	=					False  # Rwkv does not support head masking

  def   lowerCAmelCase__       (   self):
      SCREAMING_SNAKE_CASE_													=  RwkvModelTester(self)
      SCREAMING_SNAKE_CASE_													=  ConfigTester(
          self  ,						config_class=_A  ,						n_embd=37  ,						common_properties=['hidden_size', 'num_hidden_layers'])

  def   lowerCAmelCase__       (   self  ,						_A  ,						_A  ,						_A=None):
      if isinstance(_A  ,						torch.Tensor):
          SCREAMING_SNAKE_CASE_							,     SCREAMING_SNAKE_CASE_													=  member.max().item(), member.min().item()
      elif isinstance(_A  ,						_A) or isinstance(_A  ,						_A):
          SCREAMING_SNAKE_CASE_							,     SCREAMING_SNAKE_CASE_													=  max(_A), min(_A)

      if not isinstance(_A  ,						_A):
          raise TypeError('container should be a list or tuple')
      elif len(_A) != 2:
          raise ValueError('container should have 2 elements')

      SCREAMING_SNAKE_CASE_							,     SCREAMING_SNAKE_CASE_													=  container

      SCREAMING_SNAKE_CASE_													=  (min_value >= expected_min) and (max_value <= expected_max)

      if not is_inside_interval:
          SCREAMING_SNAKE_CASE_													=  '%s not found in %s' % (safe_repr(_A), safe_repr(_A))
          self.fail(self._formatMessage(_A  ,						_A))

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

  def   lowerCAmelCase__       (   self):
      SCREAMING_SNAKE_CASE_													=  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_rwkv_model(*_A)

  def   lowerCAmelCase__       (   self):
      SCREAMING_SNAKE_CASE_													=  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_causl_lm(*_A)

  def   lowerCAmelCase__       (   self):
      SCREAMING_SNAKE_CASE_													=  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_state_equivalency(*_A)

  def   lowerCAmelCase__       (   self):
      SCREAMING_SNAKE_CASE_							,     SCREAMING_SNAKE_CASE_													=  self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes:
          SCREAMING_SNAKE_CASE_													=  model_class(config=_A)
          for name, param in model.named_parameters():
              if "time_decay" in name:
                  if param.requires_grad:
                      self.assertTrue(param.data.max().item() == 3.0)
                      self.assertTrue(param.data.min().item() == -5.0)
              elif "time_first" in name:
                  if param.requires_grad:
                      # check if it's a ones like
                      self.assertTrue(torch.allclose(param.data  ,						torch.ones_like(param.data)  ,						atol=1E-5  ,						rtol=1E-5))
              elif any(x in name for x in ['time_mix_key', 'time_mix_receptance']):
                  if param.requires_grad:
                      self.assertInterval(
                          param.data  ,						[0.0, 1.0]  ,						msg=f"""Parameter {name} of model {model_class} seems not properly initialized"""  ,						)
              elif "time_mix_value" in name:
                  if param.requires_grad:
                      self.assertInterval(
                          param.data  ,						[0.0, 1.3]  ,						msg=f"""Parameter {name} of model {model_class} seems not properly initialized"""  ,						)

  def   lowerCAmelCase__       (   self):
      SCREAMING_SNAKE_CASE_							,     SCREAMING_SNAKE_CASE_													=  self.model_tester.prepare_config_and_inputs_for_common()
      SCREAMING_SNAKE_CASE_													=  True

      SCREAMING_SNAKE_CASE_													=  getattr(self.model_tester  ,						'seq_length'  ,						_A)

      for model_class in self.all_model_classes:
          SCREAMING_SNAKE_CASE_													=  True
          SCREAMING_SNAKE_CASE_													=  False
          SCREAMING_SNAKE_CASE_													=  True
          SCREAMING_SNAKE_CASE_													=  model_class(_A)
          model.to(_A)
          model.eval()

          SCREAMING_SNAKE_CASE_													=  self._prepare_for_class(_A  ,						_A)
          SCREAMING_SNAKE_CASE_													=  inputs['input_ids'].shape[0]
          with torch.no_grad():
              SCREAMING_SNAKE_CASE_													=  model(**_A)
          SCREAMING_SNAKE_CASE_													=  outputs.attentions
          self.assertEqual(len(_A)  ,						self.model_tester.num_hidden_layers)

          # check that output_attentions also work using config
          del inputs_dict["output_attentions"]
          SCREAMING_SNAKE_CASE_													=  True
          SCREAMING_SNAKE_CASE_													=  model_class(_A)
          model.to(_A)
          model.eval()

          SCREAMING_SNAKE_CASE_													=  self._prepare_for_class(_A  ,						_A)
          SCREAMING_SNAKE_CASE_													=  inputs['input_ids'].shape[0]
          with torch.no_grad():
              SCREAMING_SNAKE_CASE_													=  model(**_A)
          SCREAMING_SNAKE_CASE_													=  outputs.attentions
          self.assertEqual(len(_A)  ,						self.model_tester.num_hidden_layers)

          self.assertListEqual(
              list(attentions[0].shape[-3:])  ,						[batch_size, seq_len, config.hidden_size]  ,						)
          SCREAMING_SNAKE_CASE_													=  len(_A)

          # Check attention is always last and order is fine
          SCREAMING_SNAKE_CASE_													=  True
          SCREAMING_SNAKE_CASE_													=  True
          SCREAMING_SNAKE_CASE_													=  model_class(_A)
          model.to(_A)
          model.eval()

          SCREAMING_SNAKE_CASE_													=  self._prepare_for_class(_A  ,						_A)
          SCREAMING_SNAKE_CASE_													=  inputs['input_ids'].shape[0]
          with torch.no_grad():
              SCREAMING_SNAKE_CASE_													=  model(**_A)

          SCREAMING_SNAKE_CASE_													=  1
          self.assertEqual(out_len + added_hidden_states  ,						len(_A))

          SCREAMING_SNAKE_CASE_													=  outputs.attentions

          self.assertEqual(len(_A)  ,						self.model_tester.num_hidden_layers)
          self.assertListEqual(
              list(self_attentions[0].shape[-3:])  ,						[batch_size, seq_len, config.hidden_size]  ,						)


  @slow
  def   lowerCAmelCase__       (   self):
      for model_name in RWKV_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
          SCREAMING_SNAKE_CASE_													=  RwkvModel.from_pretrained(_A)
          self.assertIsNotNone(_A)






@unittest.skipIf(
    not is_torch_greater_or_equal_than_2_0       ,	reason='See https://github.com/huggingface/transformers/pull/24204'    )
@slow
class  __snake_case					(      unittest.TestCase    ):

  def   lowerCAmelCase__       (   self):
      SCREAMING_SNAKE_CASE_													=  'RWKV/rwkv-4-169m-pile'
      SCREAMING_SNAKE_CASE_													=  AutoTokenizer.from_pretrained(self.model_id)

  def   lowerCAmelCase__       (   self):
      SCREAMING_SNAKE_CASE_													=  'Hello my name is Jasmine and I am a newbie to the'
      SCREAMING_SNAKE_CASE_													=  RwkvForCausalLM.from_pretrained(self.model_id).to(_A)

      SCREAMING_SNAKE_CASE_													=  self.tokenizer('Hello my name is'  ,						return_tensors='pt').input_ids.to(_A)
      SCREAMING_SNAKE_CASE_													=  model.generate(_A  ,						max_new_tokens=10)
      SCREAMING_SNAKE_CASE_													=  self.tokenizer.decode(output[0].tolist())

      self.assertEqual(_A  ,						_A)


  def   lowerCAmelCase__       (   self):
      SCREAMING_SNAKE_CASE_													=  'Hello my name is Jasmine and I am a newbie to the'

      SCREAMING_SNAKE_CASE_													=  self.tokenizer('Hello my name is'  ,						return_tensors='pt').input_ids.to(_A)
      SCREAMING_SNAKE_CASE_													=  RwkvForCausalLM.from_pretrained(self.model_id  ,						torch_dtype=torch.bfloataa).to(_A)

      SCREAMING_SNAKE_CASE_													=  model.generate(_A  ,						max_new_tokens=10)
      SCREAMING_SNAKE_CASE_													=  self.tokenizer.decode(output[0].tolist())

      self.assertEqual(_A  ,						_A)





from __future__ import annotations

import inspect
import unittest
from typing import List, Tuple

from transformers import ConvNextConfig
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 TFConvNextForImageClassification, TFConvNextModel


if is_vision_available():
       from PIL import Image

       from transformers import ConvNextImageProcessor



class  __snake_case					:

  def __init__(   self  ,						_A  ,						_A=13  ,						_A=32  ,						_A=3  ,						_A=4  ,						_A=[10, 20, 30, 40]  ,						_A=[2, 2, 3, 2]  ,						_A=True  ,						_A=True  ,						_A=37  ,						_A="gelu"  ,						_A=10  ,						_A=0.0_2  ,						_A=3  ,						_A=None  ,						):
      SCREAMING_SNAKE_CASE_													=  parent
      SCREAMING_SNAKE_CASE_													=  batch_size
      SCREAMING_SNAKE_CASE_													=  image_size
      SCREAMING_SNAKE_CASE_													=  num_channels
      SCREAMING_SNAKE_CASE_													=  num_stages
      SCREAMING_SNAKE_CASE_													=  hidden_sizes
      SCREAMING_SNAKE_CASE_													=  depths
      SCREAMING_SNAKE_CASE_													=  is_training
      SCREAMING_SNAKE_CASE_													=  use_labels
      SCREAMING_SNAKE_CASE_													=  intermediate_size
      SCREAMING_SNAKE_CASE_													=  hidden_act
      SCREAMING_SNAKE_CASE_													=  type_sequence_label_size
      SCREAMING_SNAKE_CASE_													=  initializer_range
      SCREAMING_SNAKE_CASE_													=  scope

  def   lowerCAmelCase__       (   self):
      SCREAMING_SNAKE_CASE_													=  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

      SCREAMING_SNAKE_CASE_													=  None
      if self.use_labels:
          SCREAMING_SNAKE_CASE_													=  ids_tensor([self.batch_size]  ,						self.type_sequence_label_size)

      SCREAMING_SNAKE_CASE_													=  self.get_config()

      return config, pixel_values, labels

  def   lowerCAmelCase__       (   self):
      return ConvNextConfig(
          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=_A  ,						initializer_range=self.initializer_range  ,						)

  def   lowerCAmelCase__       (   self  ,						_A  ,						_A  ,						_A):
      SCREAMING_SNAKE_CASE_													=  TFConvNextModel(config=_A)
      SCREAMING_SNAKE_CASE_													=  model(_A  ,						training=_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):
      SCREAMING_SNAKE_CASE_													=  self.type_sequence_label_size
      SCREAMING_SNAKE_CASE_													=  TFConvNextForImageClassification(_A)
      SCREAMING_SNAKE_CASE_													=  model(_A  ,						labels=_A  ,						training=_A)
      self.parent.assertEqual(result.logits.shape  ,						(self.batch_size, self.type_sequence_label_size))


  def   lowerCAmelCase__       (   self):
      SCREAMING_SNAKE_CASE_													=  self.prepare_config_and_inputs()
      SCREAMING_SNAKE_CASE_							,     SCREAMING_SNAKE_CASE_							,     SCREAMING_SNAKE_CASE_													=  config_and_inputs
      SCREAMING_SNAKE_CASE_													=  {'pixel_values': pixel_values}
      return config, inputs_dict






@require_tf
class  __snake_case					(      lowerCAmelCase__       ,	lowerCAmelCase__       ,	unittest.TestCase    ):
  __lowerCAmelCase       :		str  	=					(TFConvNextModel, TFConvNextForImageClassification) if is_tf_available() else ()
  __lowerCAmelCase       :		List[str]  	=					(
      {'feature-extraction': TFConvNextModel, 'image-classification': TFConvNextForImageClassification}
      if is_tf_available()
      else {}
  )

  __lowerCAmelCase       :		Union[str, Any]  	=					False
  __lowerCAmelCase       :		Dict  	=					False
  __lowerCAmelCase       :		Optional[int]  	=					False
  __lowerCAmelCase       :		Optional[Any]  	=					False
  __lowerCAmelCase       :		List[Any]  	=					False

  def   lowerCAmelCase__       (   self):
      SCREAMING_SNAKE_CASE_													=  TFConvNextModelTester(self)
      SCREAMING_SNAKE_CASE_													=  ConfigTester(
          self  ,						config_class=_A  ,						has_text_modality=_A  ,						hidden_size=37  ,						)

  @unittest.skip(reason='ConvNext does not use inputs_embeds')
  def   lowerCAmelCase__       (   self):
      pass

  @unittest.skipIf(
      not is_tf_available() or len(tf.config.list_physical_devices('GPU')) == 0  ,						reason='TF does not support backprop for grouped convolutions on CPU.'  ,						)
  @slow
  def   lowerCAmelCase__       (   self):
      super().test_keras_fit()

  @unittest.skip(reason='ConvNext does not support input and output embeddings')
  def   lowerCAmelCase__       (   self):
      pass

  def   lowerCAmelCase__       (   self):
      SCREAMING_SNAKE_CASE_							,     SCREAMING_SNAKE_CASE_													=  self.model_tester.prepare_config_and_inputs_for_common()

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

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

  def   lowerCAmelCase__       (   self):
      SCREAMING_SNAKE_CASE_													=  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*_A)

  @unittest.skipIf(
      not is_tf_available() or len(tf.config.list_physical_devices('GPU')) == 0  ,						reason='TF does not support backprop for grouped convolutions on CPU.'  ,						)
  def   lowerCAmelCase__       (   self):
      super().test_dataset_conversion()

  def   lowerCAmelCase__       (   self):
      def check_hidden_states_output(_A  ,						_A  ,						_A):
          SCREAMING_SNAKE_CASE_													=  model_class(_A)

          SCREAMING_SNAKE_CASE_													=  model(**self._prepare_for_class(_A  ,						_A))
          SCREAMING_SNAKE_CASE_													=  outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

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

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

      SCREAMING_SNAKE_CASE_							,     SCREAMING_SNAKE_CASE_													=  self.model_tester.prepare_config_and_inputs_for_common()

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

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

          check_hidden_states_output(_A  ,						_A  ,						_A)

  def   lowerCAmelCase__       (   self):
      SCREAMING_SNAKE_CASE_							,     SCREAMING_SNAKE_CASE_													=  self.model_tester.prepare_config_and_inputs_for_common()

      def check_equivalence(_A  ,						_A  ,						_A  ,						_A={}):
          SCREAMING_SNAKE_CASE_													=  model(_A  ,						return_dict=_A  ,						**_A)
          SCREAMING_SNAKE_CASE_													=  model(_A  ,						return_dict=_A  ,						**_A).to_tuple()

          def recursive_check(_A  ,						_A):
              if isinstance(_A  ,						(List, Tuple)):
                  for tuple_iterable_value, dict_iterable_value in zip(_A  ,						_A):
                      recursive_check(_A  ,						_A)
              elif tuple_object is None:
                  return
              else:
                  self.assertTrue(
                      all(tf.equal(_A  ,						_A))  ,						msg=(
                          'Tuple and dict output are not equal. Difference:'
                          f""" {tf.math.reduce_max(tf.abs(tuple_object - dict_object))}"""
                      )  ,						)

              recursive_check(_A  ,						_A)

      for model_class in self.all_model_classes:
          SCREAMING_SNAKE_CASE_													=  model_class(_A)

          SCREAMING_SNAKE_CASE_													=  self._prepare_for_class(_A  ,						_A)
          SCREAMING_SNAKE_CASE_													=  self._prepare_for_class(_A  ,						_A)
          check_equivalence(_A  ,						_A  ,						_A)

          SCREAMING_SNAKE_CASE_													=  self._prepare_for_class(_A  ,						_A  ,						return_labels=_A)
          SCREAMING_SNAKE_CASE_													=  self._prepare_for_class(_A  ,						_A  ,						return_labels=_A)
          check_equivalence(_A  ,						_A  ,						_A)

          SCREAMING_SNAKE_CASE_													=  self._prepare_for_class(_A  ,						_A)
          SCREAMING_SNAKE_CASE_													=  self._prepare_for_class(_A  ,						_A)
          check_equivalence(_A  ,						_A  ,						_A  ,						{'output_hidden_states': True})

          SCREAMING_SNAKE_CASE_													=  self._prepare_for_class(_A  ,						_A  ,						return_labels=_A)
          SCREAMING_SNAKE_CASE_													=  self._prepare_for_class(_A  ,						_A  ,						return_labels=_A)
          check_equivalence(_A  ,						_A  ,						_A  ,						{'output_hidden_states': True})

  def   lowerCAmelCase__       (   self):
      SCREAMING_SNAKE_CASE_													=  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_image_classification(*_A)


  @slow
  def   lowerCAmelCase__       (   self):
      SCREAMING_SNAKE_CASE_													=  TFConvNextModel.from_pretrained('facebook/convnext-tiny-224')
      self.assertIsNotNone(_A)






def    _UpperCAmelCase    (					):





    """simple docstring"""


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





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

  @cached_property
  def   lowerCAmelCase__       (   self):
      return ConvNextImageProcessor.from_pretrained('facebook/convnext-tiny-224') if is_vision_available() else None


  @slow
  def   lowerCAmelCase__       (   self):
      SCREAMING_SNAKE_CASE_													=  TFConvNextForImageClassification.from_pretrained('facebook/convnext-tiny-224')

      SCREAMING_SNAKE_CASE_													=  self.default_image_processor
      SCREAMING_SNAKE_CASE_													=  prepare_img()
      SCREAMING_SNAKE_CASE_													=  image_processor(images=_A  ,						return_tensors='tf')

      # forward pass
      SCREAMING_SNAKE_CASE_													=  model(**_A)

      # verify the logits
      SCREAMING_SNAKE_CASE_													=  tf.TensorShape((1, 1000))
      self.assertEqual(outputs.logits.shape  ,						_A)

      SCREAMING_SNAKE_CASE_													=  tf.constant([-0.0_2_6_0, -0.4_7_3_9, 0.1_9_1_1])

      tf.debugging.assert_near(outputs.logits[0, :3]  ,						_A  ,						atol=1E-4)


