

'''simple docstring'''







import inspect
import unittest

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

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


if is_torch_available():
      import torch

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


if is_vision_available():
      from PIL import Image

      from transformers import AutoImageProcessor


class 					A__      :



    def __init__(					self		,				UpperCamelCase__		,				UpperCamelCase__=13		,				UpperCamelCase__=32		,				UpperCamelCase__=3		,				UpperCamelCase__=4		,				UpperCamelCase__=[10, 20, 30, 40]		,				UpperCamelCase__=[2, 2, 3, 2]		,				UpperCamelCase__=True		,				UpperCamelCase__=True		,				UpperCamelCase__=37		,				UpperCamelCase__="gelu"		,				UpperCamelCase__=10		,				UpperCamelCase__=0.02		,				UpperCamelCase__=["stage2", "stage3", "stage4"]		,				UpperCamelCase__=[2, 3, 4]		,				UpperCamelCase__=None		,				)						->      Union[str, Any]:





           '''simple docstring'''
           A_						=      parent
           A_						=      batch_size
           A_						=      image_size
           A_						=      num_channels
           A_						=      num_stages
           A_						=      hidden_sizes
           A_						=      depths
           A_						=      is_training
           A_						=      use_labels
           A_						=      intermediate_size
           A_						=      hidden_act
           A_						=      num_labels
           A_						=      initializer_range
           A_						=      out_features
           A_						=      out_indices
           A_						=      scope



    def 					snake_case_			(					self       )						->      Optional[Any]:





           '''simple docstring'''
           A_						=      floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]       )

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

           A_						=      self.get_config()

           return config, pixel_values, labels



    def 					snake_case_			(					self       )						->      str:





           '''simple docstring'''
           return ConvNextVaConfig(
               num_channels=self.num_channels		,				hidden_sizes=self.hidden_sizes		,				depths=self.depths		,				num_stages=self.num_stages		,				hidden_act=self.hidden_act		,				is_decoder=a_		,				initializer_range=self.initializer_range		,				out_features=self.out_features		,				out_indices=self.out_indices		,				num_labels=self.num_labels		,				)



    def 					snake_case_			(					self		,				UpperCamelCase__		,				UpperCamelCase__		,				UpperCamelCase__       )						->      Optional[int]:





           '''simple docstring'''
           A_						=      ConvNextVaModel(config=a_       )
           model.to(a_       )
           model.eval()
           A_						=      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 					snake_case_			(					self		,				UpperCamelCase__		,				UpperCamelCase__		,				UpperCamelCase__       )						->      Optional[int]:





           '''simple docstring'''
           A_						=      ConvNextVaForImageClassification(a_       )
           model.to(a_       )
           model.eval()
           A_						=      model(a_		,				labels=a_       )
           self.parent.assertEqual(result.logits.shape		,				(self.batch_size, self.num_labels)       )



    def 					snake_case_			(					self		,				UpperCamelCase__		,				UpperCamelCase__		,				UpperCamelCase__       )						->      Optional[Any]:





           '''simple docstring'''
           A_						=      ConvNextVaBackbone(config=a_       )
           model.to(a_       )
           model.eval()
           A_						=      model(a_       )

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

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

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

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

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



    def 					snake_case_			(					self       )						->      str:





           '''simple docstring'''
           A_						=      self.prepare_config_and_inputs()
           A_						=      config_and_inputs
           A_						=      {"""pixel_values""": pixel_values}
           return config, inputs_dict







    def 					snake_case_			(					self       )						->      Dict:





           '''simple docstring'''
           A_						=      self.prepare_config_and_inputs()
           A_						=      config_and_inputs
           A_						=      {"""pixel_values""": pixel_values, """labels""": labels}
           return config, inputs_dict


@require_torch
class 					A__      (      __a						,						__a						,						unittest.TestCase			):
    lowercase						=		(
        (
            ConvNextVaModel,
            ConvNextVaForImageClassification,
            ConvNextVaBackbone,
        )
        if is_torch_available()
        else ()
    )
    lowercase						=		(
        {"feature-extraction": ConvNextVaModel, "image-classification": ConvNextVaForImageClassification}
        if is_torch_available()
        else {}
    )

    lowercase						=		False
    lowercase						=		False
    lowercase						=		False
    lowercase						=		False
    lowercase						=		False



    def 					snake_case_			(					self       )						->      List[str]:





           '''simple docstring'''
           A_						=      ConvNextVaModelTester(self       )
           A_						=      ConfigTester(self		,				config_class=a_		,				has_text_modality=a_		,				hidden_size=37       )



    def 					snake_case_			(					self       )						->      List[Any]:





           '''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 					snake_case_			(					self       )						->      str:





           '''simple docstring'''
           return



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





           '''simple docstring'''
           pass



    @unittest.skip(reason="""ConvNextV2 does not support input and output embeddings"""       )
    def 					snake_case_			(					self       )						->      str:





           '''simple docstring'''
           pass



    @unittest.skip(reason="""ConvNextV2 does not use feedforward chunking"""       )
    def 					snake_case_			(					self       )						->      str:





           '''simple docstring'''
           pass



    def 					snake_case_			(					self       )						->      Optional[Any]:





           '''simple docstring'''



           if not self.model_tester.is_training:
                  return

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

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

                  A_						=      model_class(a_       )
                  model.to(a_       )
                  model.train()
                  A_						=      self._prepare_for_class(a_		,				a_		,				return_labels=a_       )
                  A_						=      model(**a_       ).loss
                  loss.backward()



    def 					snake_case_			(					self       )						->      Optional[int]:





           '''simple docstring'''



           if not self.model_tester.is_training:
                  return

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

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

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



    def 					snake_case_			(					self       )						->      Tuple:





           '''simple docstring'''
           A_						=      self.model_tester.prepare_config_and_inputs_for_common()

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

                  A_						=      ["""pixel_values"""]
                  self.assertListEqual(arg_names[:1]		,				a_       )



    def 					snake_case_			(					self       )						->      Tuple:





           '''simple docstring'''
           A_						=      self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*a_       )



    def 					snake_case_			(					self       )						->      Tuple:





           '''simple docstring'''



           def check_hidden_states_output(UpperCamelCase__		,				UpperCamelCase__		,				UpperCamelCase__       ):
                  A_						=      model_class(a_       )
                  model.to(a_       )
                  model.eval()

                  with torch.no_grad():
                         A_						=      model(**self._prepare_for_class(a_		,				a_       )       )

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

                  A_						=      self.model_tester.num_stages
                  self.assertEqual(len(a_       )		,				expected_num_stages + 1       )

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

           A_						=      self.model_tester.prepare_config_and_inputs_for_common()

           for model_class in self.all_model_classes:
                  A_						=      True
                  check_hidden_states_output(a_		,				a_		,				a_       )

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

                  check_hidden_states_output(a_		,				a_		,				a_       )



    def 					snake_case_			(					self       )						->      List[str]:





           '''simple docstring'''
           A_						=      self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_image_classification(*a_       )







    @slow
    def 					snake_case_			(					self       )						->      Optional[Any]:





           '''simple docstring'''



           for model_name in CONVNEXTV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                  A_						=      ConvNextVaModel.from_pretrained(a_       )
                  self.assertIsNotNone(a_       )

def       UpperCAmelCase__    (      )			->			List[str]:
       A_						=      Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""				)
       return image







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



    @cached_property
    def 					snake_case_			(					self       )						->      Any:





           '''simple docstring'''
           return AutoImageProcessor.from_pretrained("""facebook/convnextv2-tiny-1k-224"""       ) if is_vision_available() else None







    @slow
    def 					snake_case_			(					self       )						->      List[Any]:





           '''simple docstring'''
           A_						=      ConvNextVaForImageClassification.from_pretrained("""facebook/convnextv2-tiny-1k-224"""       ).to(a_       )

           A_						=      self.default_image_processor
           A_						=      prepare_img()
           A_						=      preprocessor(images=a_		,				return_tensors="""pt"""       ).to(a_       )

           # forward pass
           with torch.no_grad():
                  A_						=      model(**a_       )

           # verify the logits
           A_						=      torch.Size((1, 1000)       )
           self.assertEqual(outputs.logits.shape		,				a_       )

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






'''simple docstring'''







import tempfile

import torch

from diffusers import IPNDMScheduler

from .test_schedulers import SchedulerCommonTest


class 					A__      (      _snake_case			):
    lowercase						=		(IPNDMScheduler,)
    lowercase						=		(("num_inference_steps", 50),)



    def 					snake_case_			(					self		,				**UpperCamelCase__       )						->      Optional[Any]:





           '''simple docstring'''
           A_						=      {"""num_train_timesteps""": 1000}
           config.update(**UpperCamelCase__       )
           return config



    def 					snake_case_			(					self		,				UpperCamelCase__=0		,				**UpperCamelCase__       )						->      str:





           '''simple docstring'''
           A_						=      dict(self.forward_default_kwargs       )
           A_						=      kwargs.pop("""num_inference_steps"""		,				UpperCamelCase__       )
           A_						=      self.dummy_sample
           A_						=      0.1 * sample
           A_						=      [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05]

           for scheduler_class in self.scheduler_classes:
                  A_						=      self.get_scheduler_config(**UpperCamelCase__       )
                  A_						=      scheduler_class(**UpperCamelCase__       )
                  scheduler.set_timesteps(UpperCamelCase__       )
                  # copy over dummy past residuals
                  A_						=      dummy_past_residuals[:]

                  if time_step is None:
                         A_						=      scheduler.timesteps[len(scheduler.timesteps       ) // 2]

                  with tempfile.TemporaryDirectory() as tmpdirname:
                         scheduler.save_config(UpperCamelCase__       )
                         A_						=      scheduler_class.from_pretrained(UpperCamelCase__       )
                         new_scheduler.set_timesteps(UpperCamelCase__       )
                         # copy over dummy past residuals
                         A_						=      dummy_past_residuals[:]

                  A_						=      scheduler.step(UpperCamelCase__		,				UpperCamelCase__		,				UpperCamelCase__		,				**UpperCamelCase__       ).prev_sample
                  A_						=      new_scheduler.step(UpperCamelCase__		,				UpperCamelCase__		,				UpperCamelCase__		,				**UpperCamelCase__       ).prev_sample

                  assert torch.sum(torch.abs(output - new_output       )       ) < 1e-5, "Scheduler outputs are not identical"

                  A_						=      scheduler.step(UpperCamelCase__		,				UpperCamelCase__		,				UpperCamelCase__		,				**UpperCamelCase__       ).prev_sample
                  A_						=      new_scheduler.step(UpperCamelCase__		,				UpperCamelCase__		,				UpperCamelCase__		,				**UpperCamelCase__       ).prev_sample

                  assert torch.sum(torch.abs(output - new_output       )       ) < 1e-5, "Scheduler outputs are not identical"



    def 					snake_case_			(					self       )						->      Optional[int]:





           '''simple docstring'''
           pass



    def 					snake_case_			(					self		,				UpperCamelCase__=0		,				**UpperCamelCase__       )						->      str:





           '''simple docstring'''
           A_						=      dict(self.forward_default_kwargs       )
           A_						=      kwargs.pop("""num_inference_steps"""		,				UpperCamelCase__       )
           A_						=      self.dummy_sample
           A_						=      0.1 * sample
           A_						=      [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05]

           for scheduler_class in self.scheduler_classes:
                  A_						=      self.get_scheduler_config()
                  A_						=      scheduler_class(**UpperCamelCase__       )
                  scheduler.set_timesteps(UpperCamelCase__       )

                  # copy over dummy past residuals (must be after setting timesteps)
                  A_						=      dummy_past_residuals[:]

                  if time_step is None:
                         A_						=      scheduler.timesteps[len(scheduler.timesteps       ) // 2]

                  with tempfile.TemporaryDirectory() as tmpdirname:
                         scheduler.save_config(UpperCamelCase__       )
                         A_						=      scheduler_class.from_pretrained(UpperCamelCase__       )
                         # copy over dummy past residuals
                         new_scheduler.set_timesteps(UpperCamelCase__       )

                         # copy over dummy past residual (must be after setting timesteps)
                         A_						=      dummy_past_residuals[:]

                  A_						=      scheduler.step(UpperCamelCase__		,				UpperCamelCase__		,				UpperCamelCase__		,				**UpperCamelCase__       ).prev_sample
                  A_						=      new_scheduler.step(UpperCamelCase__		,				UpperCamelCase__		,				UpperCamelCase__		,				**UpperCamelCase__       ).prev_sample

                  assert torch.sum(torch.abs(output - new_output       )       ) < 1e-5, "Scheduler outputs are not identical"

                  A_						=      scheduler.step(UpperCamelCase__		,				UpperCamelCase__		,				UpperCamelCase__		,				**UpperCamelCase__       ).prev_sample
                  A_						=      new_scheduler.step(UpperCamelCase__		,				UpperCamelCase__		,				UpperCamelCase__		,				**UpperCamelCase__       ).prev_sample

                  assert torch.sum(torch.abs(output - new_output       )       ) < 1e-5, "Scheduler outputs are not identical"



    def 					snake_case_			(					self		,				**UpperCamelCase__       )						->      Tuple:





           '''simple docstring'''
           A_						=      self.scheduler_classes[0]
           A_						=      self.get_scheduler_config(**UpperCamelCase__       )
           A_						=      scheduler_class(**UpperCamelCase__       )

           A_						=      10
           A_						=      self.dummy_model()
           A_						=      self.dummy_sample_deter
           scheduler.set_timesteps(UpperCamelCase__       )

           for i, t in enumerate(scheduler.timesteps       ):
                  A_						=      model(UpperCamelCase__		,				UpperCamelCase__       )
                  A_						=      scheduler.step(UpperCamelCase__		,				UpperCamelCase__		,				UpperCamelCase__       ).prev_sample

           for i, t in enumerate(scheduler.timesteps       ):
                  A_						=      model(UpperCamelCase__		,				UpperCamelCase__       )
                  A_						=      scheduler.step(UpperCamelCase__		,				UpperCamelCase__		,				UpperCamelCase__       ).prev_sample

           return sample



    def 					snake_case_			(					self       )						->      Union[str, Any]:





           '''simple docstring'''
           A_						=      dict(self.forward_default_kwargs       )

           A_						=      kwargs.pop("""num_inference_steps"""		,				UpperCamelCase__       )

           for scheduler_class in self.scheduler_classes:
                  A_						=      self.get_scheduler_config()
                  A_						=      scheduler_class(**UpperCamelCase__       )

                  A_						=      self.dummy_sample
                  A_						=      0.1 * sample

                  if num_inference_steps is not None and hasattr(UpperCamelCase__		,				"""set_timesteps"""       ):
                         scheduler.set_timesteps(UpperCamelCase__       )
                  elif num_inference_steps is not None and not hasattr(UpperCamelCase__		,				"""set_timesteps"""       ):
                         A_						=      num_inference_steps

                  # copy over dummy past residuals (must be done after set_timesteps)
                  A_						=      [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05]
                  A_						=      dummy_past_residuals[:]

                  A_						=      scheduler.timesteps[5]
                  A_						=      scheduler.timesteps[6]

                  A_						=      scheduler.step(UpperCamelCase__		,				UpperCamelCase__		,				UpperCamelCase__		,				**UpperCamelCase__       ).prev_sample
                  A_						=      scheduler.step(UpperCamelCase__		,				UpperCamelCase__		,				UpperCamelCase__		,				**UpperCamelCase__       ).prev_sample

                  self.assertEqual(output_a.shape		,				sample.shape       )
                  self.assertEqual(output_a.shape		,				output_a.shape       )

                  A_						=      scheduler.step(UpperCamelCase__		,				UpperCamelCase__		,				UpperCamelCase__		,				**UpperCamelCase__       ).prev_sample
                  A_						=      scheduler.step(UpperCamelCase__		,				UpperCamelCase__		,				UpperCamelCase__		,				**UpperCamelCase__       ).prev_sample

                  self.assertEqual(output_a.shape		,				sample.shape       )
                  self.assertEqual(output_a.shape		,				output_a.shape       )



    def 					snake_case_			(					self       )						->      Any:





           '''simple docstring'''
           for timesteps in [100, 1000]:
                  self.check_over_configs(num_train_timesteps=UpperCamelCase__		,				time_step=UpperCamelCase__       )



    def 					snake_case_			(					self       )						->      Any:





           '''simple docstring'''
           for t, num_inference_steps in zip([1, 5, 10]		,				[10, 50, 100]       ):
                  self.check_over_forward(num_inference_steps=UpperCamelCase__		,				time_step=UpperCamelCase__       )







    def 					snake_case_			(					self       )						->      Dict:





           '''simple docstring'''
           A_						=      self.full_loop()
           A_						=      torch.mean(torch.abs(UpperCamelCase__       )       )

           assert abs(result_mean.item() - 2540529       ) < 10
