

'''simple docstring'''



import torch

from diffusers import DDIMScheduler

from .test_schedulers import SchedulerCommonTest

class 			SCREAMING_SNAKE_CASE					(							lowerCamelCase_					):






      '''simple docstring'''






      SCREAMING_SNAKE_CASE__ :				Dict    =				(DDIMScheduler,)
      SCREAMING_SNAKE_CASE__ :				Tuple    =				(("""eta""", 0.0), ("""num_inference_steps""", 50))




      def 	__UpperCAmelCase			(					self :		str     ,					**snake_case :		List[Any]   ):



          """simple docstring"""





          _snake_case : Dict												=		{
              "num_train_timesteps": 1000,
              "beta_start": 0.0001,
              "beta_end": 0.02,
              "beta_schedule": "linear",
              "clip_sample": True,
          }

          config.update(**snake_case   )
          return config




      def 	__UpperCAmelCase			(					self :		List[str]     ,					**snake_case :		Dict   ):



          """simple docstring"""





          _snake_case : Any												=		self.scheduler_classes[0]
          _snake_case : str												=		self.get_scheduler_config(**snake_case   )
          _snake_case : int												=		scheduler_class(**snake_case   )

          _snake_case : int												=		10, 0.0

          _snake_case : int												=		self.dummy_model()
          _snake_case : Optional[Any]												=		self.dummy_sample_deter

          scheduler.set_timesteps(snake_case   )

          for t in scheduler.timesteps:
              _snake_case : str												=		model(snake_case     ,					snake_case   )
              _snake_case : Union[str, Any]												=		scheduler.step(snake_case     ,					snake_case     ,					snake_case     ,					snake_case   ).prev_sample

          return sample




      def 	__UpperCAmelCase			(					self :		Any   ):



          """simple docstring"""





          for timesteps in [100, 500, 1000]:
              self.check_over_configs(num_train_timesteps=snake_case   )




      def 	__UpperCAmelCase			(					self :		int   ):



          """simple docstring"""





          for steps_offset in [0, 1]:
              self.check_over_configs(steps_offset=snake_case   )

          _snake_case : str												=		self.scheduler_classes[0]
          _snake_case : str												=		self.get_scheduler_config(steps_offset=1   )
          _snake_case : List[str]												=		scheduler_class(**snake_case   )
          scheduler.set_timesteps(5   )
          assert torch.equal(scheduler.timesteps     ,					torch.LongTensor([801, 601, 401, 201, 1]   )   )




      def 	__UpperCAmelCase			(					self :		int   ):



          """simple docstring"""





          for beta_start, beta_end in zip([0.0001, 0.001, 0.01, 0.1]     ,					[0.002, 0.02, 0.2, 2]   ):
              self.check_over_configs(beta_start=snake_case     ,					beta_end=snake_case   )




      def 	__UpperCAmelCase			(					self :		Tuple   ):



          """simple docstring"""





          for schedule in ["linear", "squaredcos_cap_v2"]:
              self.check_over_configs(beta_schedule=snake_case   )




      def 	__UpperCAmelCase			(					self :		Union[str, Any]   ):



          """simple docstring"""





          for prediction_type in ["epsilon", "v_prediction"]:
              self.check_over_configs(prediction_type=snake_case   )




      def 	__UpperCAmelCase			(					self :		int   ):



          """simple docstring"""





          for clip_sample in [True, False]:
              self.check_over_configs(clip_sample=snake_case   )




      def 	__UpperCAmelCase			(					self :		Optional[Any]   ):



          """simple docstring"""





          for timestep_spacing in ["trailing", "leading"]:
              self.check_over_configs(timestep_spacing=snake_case   )




      def 	__UpperCAmelCase			(					self :		List[str]   ):



          """simple docstring"""





          for rescale_betas_zero_snr in [True, False]:
              self.check_over_configs(rescale_betas_zero_snr=snake_case   )




      def 	__UpperCAmelCase			(					self :		Any   ):



          """simple docstring"""





          self.check_over_configs(thresholding=snake_case   )
          for threshold in [0.5, 1.0, 2.0]:
              for prediction_type in ["epsilon", "v_prediction"]:
                  self.check_over_configs(
                      thresholding=snake_case     ,					prediction_type=snake_case     ,					sample_max_value=snake_case     ,					)




      def 	__UpperCAmelCase			(					self :		str   ):



          """simple docstring"""





          for t in [1, 10, 49]:
              self.check_over_forward(time_step=snake_case   )




      def 	__UpperCAmelCase			(					self :		Optional[Any]   ):



          """simple docstring"""





          for t, num_inference_steps in zip([1, 10, 50]     ,					[10, 50, 500]   ):
              self.check_over_forward(time_step=snake_case     ,					num_inference_steps=snake_case   )




      def 	__UpperCAmelCase			(					self :		List[str]   ):



          """simple docstring"""





          for t, eta in zip([1, 10, 49]     ,					[0.0, 0.5, 1.0]   ):
              self.check_over_forward(time_step=snake_case     ,					eta=snake_case   )




      def 	__UpperCAmelCase			(					self :		str   ):



          """simple docstring"""





          _snake_case : Dict												=		self.scheduler_classes[0]
          _snake_case : Dict												=		self.get_scheduler_config()
          _snake_case : Union[str, Any]												=		scheduler_class(**snake_case   )

          assert torch.sum(torch.abs(scheduler._get_variance(0     ,					0   ) - 0.0   )   ) < 1e-5
          assert torch.sum(torch.abs(scheduler._get_variance(420     ,					400   ) - 0.1_4771   )   ) < 1e-5
          assert torch.sum(torch.abs(scheduler._get_variance(980     ,					960   ) - 0.3_2460   )   ) < 1e-5
          assert torch.sum(torch.abs(scheduler._get_variance(0     ,					0   ) - 0.0   )   ) < 1e-5
          assert torch.sum(torch.abs(scheduler._get_variance(487     ,					486   ) - 0.0_0979   )   ) < 1e-5
          assert torch.sum(torch.abs(scheduler._get_variance(999     ,					998   ) - 0.02   )   ) < 1e-5




      def 	__UpperCAmelCase			(					self :		List[Any]   ):



          """simple docstring"""





          _snake_case : Tuple												=		self.full_loop()

          _snake_case : Any												=		torch.sum(torch.abs(snake_case   )   )
          _snake_case : Dict												=		torch.mean(torch.abs(snake_case   )   )

          assert abs(result_sum.item() - 172.0067   ) < 1e-2
          assert abs(result_mean.item() - 0.22_3967   ) < 1e-3




      def 	__UpperCAmelCase			(					self :		Optional[int]   ):



          """simple docstring"""





          _snake_case : Optional[int]												=		self.full_loop(prediction_type='v_prediction'   )

          _snake_case : Tuple												=		torch.sum(torch.abs(snake_case   )   )
          _snake_case : List[str]												=		torch.mean(torch.abs(snake_case   )   )

          assert abs(result_sum.item() - 52.5302   ) < 1e-2
          assert abs(result_mean.item() - 0.0684   ) < 1e-3




      def 	__UpperCAmelCase			(					self :		Any   ):



          """simple docstring"""





          _snake_case : Any												=		self.full_loop(set_alpha_to_one=snake_case     ,					beta_start=0.01   )
          _snake_case : Any												=		torch.sum(torch.abs(snake_case   )   )
          _snake_case : List[str]												=		torch.mean(torch.abs(snake_case   )   )

          assert abs(result_sum.item() - 149.8295   ) < 1e-2
          assert abs(result_mean.item() - 0.1951   ) < 1e-3




      def 	__UpperCAmelCase			(					self :		Optional[Any]   ):



          """simple docstring"""





          _snake_case : List[str]												=		self.full_loop(set_alpha_to_one=snake_case     ,					beta_start=0.01   )
          _snake_case : List[Any]												=		torch.sum(torch.abs(snake_case   )   )
          _snake_case : int												=		torch.mean(torch.abs(snake_case   )   )

          assert abs(result_sum.item() - 149.0784   ) < 1e-2
          assert abs(result_mean.item() - 0.1941   ) < 1e-3






'''simple docstring'''



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 			SCREAMING_SNAKE_CASE					:






      '''simple docstring'''






      def __init__(					self :		Optional[int]     ,					snake_case :		Union[str, Any]     ,					snake_case :		Optional[int]=13     ,					snake_case :		Union[str, Any]=32     ,					snake_case :		List[Any]=3     ,					snake_case :		int=4     ,					snake_case :		List[Any]=[10, 20, 30, 40]     ,					snake_case :		List[str]=[2, 2, 3, 2]     ,					snake_case :		List[str]=True     ,					snake_case :		Optional[int]=True     ,					snake_case :		Union[str, Any]=37     ,					snake_case :		Any="gelu"     ,					snake_case :		Any=10     ,					snake_case :		Optional[Any]=0.02     ,					snake_case :		List[str]=3     ,					snake_case :		Any=None     ,					):



          """simple docstring"""





          _snake_case : Optional[Any]												=		parent
          _snake_case : Any												=		batch_size
          _snake_case : int												=		image_size
          _snake_case : int												=		num_channels
          _snake_case : int												=		num_stages
          _snake_case : Optional[int]												=		hidden_sizes
          _snake_case : Tuple												=		depths
          _snake_case : Optional[int]												=		is_training
          _snake_case : Tuple												=		use_labels
          _snake_case : str												=		intermediate_size
          _snake_case : Any												=		hidden_act
          _snake_case : Union[str, Any]												=		type_sequence_label_size
          _snake_case : Tuple												=		initializer_range
          _snake_case : List[Any]												=		scope




      def 	__UpperCAmelCase			(					self :		List[str]   ):



          """simple docstring"""





          _snake_case : str												=		floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]   )

          _snake_case : Any												=		None
          if self.use_labels:
              _snake_case : str												=		ids_tensor([self.batch_size]     ,					self.type_sequence_label_size   )

          _snake_case : Any												=		self.get_config()

          return config, pixel_values, labels




      def 	__UpperCAmelCase			(					self :		Union[str, Any]   ):



          """simple docstring"""





          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=snake_case     ,					initializer_range=self.initializer_range     ,					)




      def 	__UpperCAmelCase			(					self :		int     ,					snake_case :		Optional[Any]     ,					snake_case :		str     ,					snake_case :		Optional[int]   ):



          """simple docstring"""





          _snake_case : Optional[int]												=		TFConvNextModel(config=snake_case   )
          _snake_case : Tuple												=		model(snake_case     ,					training=snake_case   )
          # 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 	__UpperCAmelCase			(					self :		Any     ,					snake_case :		int     ,					snake_case :		List[str]     ,					snake_case :		List[Any]   ):



          """simple docstring"""





          _snake_case : int												=		self.type_sequence_label_size
          _snake_case : str												=		TFConvNextForImageClassification(snake_case   )
          _snake_case : Any												=		model(snake_case     ,					labels=snake_case     ,					training=snake_case   )
          self.parent.assertEqual(result.logits.shape     ,					(self.batch_size, self.type_sequence_label_size)   )




      def 	__UpperCAmelCase			(					self :		Any   ):



          """simple docstring"""





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



@require_tf
class 			SCREAMING_SNAKE_CASE					(							lowercase_      ,lowercase_      ,unittest.TestCase					):






      '''simple docstring'''






      SCREAMING_SNAKE_CASE__ :				List[Any]    =				(TFConvNextModel, TFConvNextForImageClassification) if is_tf_available() else ()
      SCREAMING_SNAKE_CASE__ :				Tuple    =				(
          {'''feature-extraction''': TFConvNextModel, '''image-classification''': TFConvNextForImageClassification}
          if is_tf_available()
          else {}
      )

      SCREAMING_SNAKE_CASE__ :				Optional[int]    =				False
      SCREAMING_SNAKE_CASE__ :				str    =				False
      SCREAMING_SNAKE_CASE__ :				Optional[Any]    =				False
      SCREAMING_SNAKE_CASE__ :				str    =				False
      SCREAMING_SNAKE_CASE__ :				Any    =				False




      def 	__UpperCAmelCase			(					self :		List[str]   ):



          """simple docstring"""





          _snake_case : Optional[int]												=		TFConvNextModelTester(self   )
          _snake_case : Any												=		ConfigTester(
              self     ,					config_class=snake_case     ,					has_text_modality=snake_case     ,					hidden_size=37     ,					)




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



          """simple docstring"""





          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 	__UpperCAmelCase			(					self :		Optional[Any]   ):



          """simple docstring"""





          super().test_keras_fit()




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



          """simple docstring"""





          pass




      def 	__UpperCAmelCase			(					self :		Optional[int]   ):



          """simple docstring"""





          _snake_case					,							_snake_case : Optional[Any]												=		self.model_tester.prepare_config_and_inputs_for_common()

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

              _snake_case : str												=		['pixel_values']
              self.assertListEqual(arg_names[:1]     ,					snake_case   )




      def 	__UpperCAmelCase			(					self :		Optional[int]   ):



          """simple docstring"""





          _snake_case : Optional[Any]												=		self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*snake_case   )




      @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 	__UpperCAmelCase			(					self :		Dict   ):



          """simple docstring"""





          super().test_dataset_conversion()




      def 	__UpperCAmelCase			(					self :		List[Any]   ):



          """simple docstring"""





          def check_hidden_states_output(snake_case :		Union[str, Any]     ,					snake_case :		str     ,					snake_case :		Dict   ):
              _snake_case : Any												=		model_class(snake_case   )

              _snake_case : Tuple												=		model(**self._prepare_for_class(snake_case     ,					snake_case   )   )
              _snake_case : Tuple												=		outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

              _snake_case : Optional[Any]												=		self.model_tester.num_stages
              self.assertEqual(len(snake_case   )     ,					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]     ,					)

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

          for model_class in self.all_model_classes:
              _snake_case : int												=		True
              check_hidden_states_output(snake_case     ,					snake_case     ,					snake_case   )

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

              check_hidden_states_output(snake_case     ,					snake_case     ,					snake_case   )




      def 	__UpperCAmelCase			(					self :		Optional[int]   ):



          """simple docstring"""





          _snake_case					,							_snake_case : Any												=		self.model_tester.prepare_config_and_inputs_for_common()

          def check_equivalence(snake_case :		Any     ,					snake_case :		Tuple     ,					snake_case :		List[str]     ,					snake_case :		Dict={}   ):
              _snake_case : Union[str, Any]												=		model(snake_case     ,					return_dict=snake_case     ,					**snake_case   )
              _snake_case : Dict												=		model(snake_case     ,					return_dict=snake_case     ,					**snake_case   ).to_tuple()

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

                  recursive_check(snake_case     ,					snake_case   )

          for model_class in self.all_model_classes:
              _snake_case : Any												=		model_class(snake_case   )

              _snake_case : List[str]												=		self._prepare_for_class(snake_case     ,					snake_case   )
              _snake_case : Optional[int]												=		self._prepare_for_class(snake_case     ,					snake_case   )
              check_equivalence(snake_case     ,					snake_case     ,					snake_case   )

              _snake_case : Optional[int]												=		self._prepare_for_class(snake_case     ,					snake_case     ,					return_labels=snake_case   )
              _snake_case : Union[str, Any]												=		self._prepare_for_class(snake_case     ,					snake_case     ,					return_labels=snake_case   )
              check_equivalence(snake_case     ,					snake_case     ,					snake_case   )

              _snake_case : Any												=		self._prepare_for_class(snake_case     ,					snake_case   )
              _snake_case : Optional[Any]												=		self._prepare_for_class(snake_case     ,					snake_case   )
              check_equivalence(snake_case     ,					snake_case     ,					snake_case     ,					{'output_hidden_states': True}   )

              _snake_case : str												=		self._prepare_for_class(snake_case     ,					snake_case     ,					return_labels=snake_case   )
              _snake_case : Any												=		self._prepare_for_class(snake_case     ,					snake_case     ,					return_labels=snake_case   )
              check_equivalence(snake_case     ,					snake_case     ,					snake_case     ,					{'output_hidden_states': True}   )




      def 	__UpperCAmelCase			(					self :		str   ):



          """simple docstring"""





          _snake_case : List[str]												=		self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_image_classification(*snake_case   )




      @slow
      def 	__UpperCAmelCase			(					self :		str   ):



          """simple docstring"""





          _snake_case : Optional[Any]												=		TFConvNextModel.from_pretrained('facebook/convnext-tiny-224'   )
          self.assertIsNotNone(snake_case   )



def   lowerCamelCase__       (      )		->    int:



    """simple docstring"""







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




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






      '''simple docstring'''






      @cached_property
      def 	__UpperCAmelCase			(					self :		Optional[int]   ):



          """simple docstring"""





          return ConvNextImageProcessor.from_pretrained('facebook/convnext-tiny-224'   ) if is_vision_available() else None




      @slow
      def 	__UpperCAmelCase			(					self :		int   ):



          """simple docstring"""





          _snake_case : List[Any]												=		TFConvNextForImageClassification.from_pretrained('facebook/convnext-tiny-224'   )

          _snake_case : Optional[int]												=		self.default_image_processor
          _snake_case : Dict												=		prepare_img()
          _snake_case : str												=		image_processor(images=snake_case     ,					return_tensors='tf'   )

          # forward pass
          _snake_case : Optional[int]												=		model(**snake_case   )

          # verify the logits
          _snake_case : List[Any]												=		tf.TensorShape((1, 1000)   )
          self.assertEqual(outputs.logits.shape     ,					snake_case   )

          _snake_case : Union[str, Any]												=		tf.constant([-0.0260, -0.4739, 0.1911]   )

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



