code: 
  
  
  
  
  
  import unittest
  
  from transformers import (
      MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING,
      TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING,
      PreTrainedTokenizer,
      is_vision_available,
  )
  from transformers.pipelines import ImageClassificationPipeline, pipeline
  from transformers.testing_utils import (
      is_pipeline_test,
      nested_simplify,
      require_tf,
      require_torch,
      require_torch_or_tf,
      require_vision,
      slow,
  )
  
  from .test_pipelines_common import ANY
  
  
  if is_vision_available():
       from PIL import Image
  else:
  
  
  
  
  
  
  
       class        __lowerCAmelCase :
  
  
  
  
  
  
  
             @staticmethod
             def      _lowercase						(					*lowerCAmelCase__	,   **lowerCAmelCase__							)     ->		str:
  
  
  
               '''simple docstring'''
  
  
  
  
               pass
  
  
  
  
  
  
  
  @is_pipeline_test
  @require_torch_or_tf
  @require_vision
  class        __lowerCAmelCase (					unittest.TestCase):
        _lowercase       :      Tuple        =    MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING
        _lowercase       :      Optional[Any]        =    TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING
  
  
  
  
  
  
  
        def      _lowercase						(					self	,   lowerCAmelCase__	,   lowerCAmelCase__	,   lowerCAmelCase__							)     ->		Optional[Any]:
  
  
  
          '''simple docstring'''
  
  
  
  
          a__			:      Dict            =ImageClassificationPipeline(model=lowerCAmelCase__	,   image_processor=lowerCAmelCase__	,   top_k=2							)
          a__			:      List[Any]            =[
              Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"							),
              "http://images.cocodataset.org/val2017/000000039769.jpg",
          ]
          return image_classifier, examples
  
  
  
  
  
  
  
        def      _lowercase						(					self	,   lowerCAmelCase__	,   lowerCAmelCase__							)     ->		Optional[int]:
  
  
  
          '''simple docstring'''
  
  
  
  
          a__			:      int            =image_classifier("./tests/fixtures/tests_samples/COCO/000000039769.png"							)
  
          self.assertEqual(
              lowerCAmelCase__	,   [
                  {"score": ANY(lowerCAmelCase__							), "label": ANY(lowerCAmelCase__							)},
                  {"score": ANY(lowerCAmelCase__							), "label": ANY(lowerCAmelCase__							)},
              ]	,   )
  
          import datasets
  
          a__			:      Any            =datasets.load_dataset("hf-internal-testing/fixtures_image_utils"	,   "image"	,   split="test"							)
  
          # Accepts URL + PIL.Image + lists
          a__			:      str            =image_classifier(
              [
                  Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"							),
                  "http://images.cocodataset.org/val2017/000000039769.jpg",
                  # RGBA
                  dataset[0]["file"],
                  # LA
                  dataset[1]["file"],
                  # L
                  dataset[2]["file"],
              ]							)
          self.assertEqual(
              lowerCAmelCase__	,   [
                  [
                      {"score": ANY(lowerCAmelCase__							), "label": ANY(lowerCAmelCase__							)},
                      {"score": ANY(lowerCAmelCase__							), "label": ANY(lowerCAmelCase__							)},
                  ],
                  [
                      {"score": ANY(lowerCAmelCase__							), "label": ANY(lowerCAmelCase__							)},
                      {"score": ANY(lowerCAmelCase__							), "label": ANY(lowerCAmelCase__							)},
                  ],
                  [
                      {"score": ANY(lowerCAmelCase__							), "label": ANY(lowerCAmelCase__							)},
                      {"score": ANY(lowerCAmelCase__							), "label": ANY(lowerCAmelCase__							)},
                  ],
                  [
                      {"score": ANY(lowerCAmelCase__							), "label": ANY(lowerCAmelCase__							)},
                      {"score": ANY(lowerCAmelCase__							), "label": ANY(lowerCAmelCase__							)},
                  ],
                  [
                      {"score": ANY(lowerCAmelCase__							), "label": ANY(lowerCAmelCase__							)},
                      {"score": ANY(lowerCAmelCase__							), "label": ANY(lowerCAmelCase__							)},
                  ],
              ]	,   )
  
  
  
  
  
  
  
        @require_torch
        def      _lowercase						(					self							)     ->		int:
  
  
  
          '''simple docstring'''
  
  
  
  
          a__			:      Optional[int]            ="hf-internal-testing/tiny-random-vit"
          a__			:      Optional[Any]            =pipeline("image-classification"	,   model=lowerCAmelCase__							)
  
          a__			:      Optional[int]            =image_classifier("http://images.cocodataset.org/val2017/000000039769.jpg"							)
          self.assertEqual(
              nested_simplify(lowerCAmelCase__	,   decimals=4							)	,   [{"label": "LABEL_1", "score": 0.5_74}, {"label": "LABEL_0", "score": 0.4_26}]	,   )
  
          a__			:      Optional[Any]            =image_classifier(
              [
                  "http://images.cocodataset.org/val2017/000000039769.jpg",
                  "http://images.cocodataset.org/val2017/000000039769.jpg",
              ]	,   top_k=2	,   )
          self.assertEqual(
              nested_simplify(lowerCAmelCase__	,   decimals=4							)	,   [
                  [{"label": "LABEL_1", "score": 0.5_74}, {"label": "LABEL_0", "score": 0.4_26}],
                  [{"label": "LABEL_1", "score": 0.5_74}, {"label": "LABEL_0", "score": 0.4_26}],
              ]	,   )
  
  
  
  
  
  
  
        @require_tf
        def      _lowercase						(					self							)     ->		Optional[Any]:
  
  
  
          '''simple docstring'''
  
  
  
  
          a__			:      str            ="hf-internal-testing/tiny-random-vit"
          a__			:      Tuple            =pipeline("image-classification"	,   model=lowerCAmelCase__	,   framework="tf"							)
  
          a__			:      Any            =image_classifier("http://images.cocodataset.org/val2017/000000039769.jpg"							)
          self.assertEqual(
              nested_simplify(lowerCAmelCase__	,   decimals=4							)	,   [{"label": "LABEL_1", "score": 0.5_74}, {"label": "LABEL_0", "score": 0.4_26}]	,   )
  
          a__			:      Any            =image_classifier(
              [
                  "http://images.cocodataset.org/val2017/000000039769.jpg",
                  "http://images.cocodataset.org/val2017/000000039769.jpg",
              ]	,   top_k=2	,   )
          self.assertEqual(
              nested_simplify(lowerCAmelCase__	,   decimals=4							)	,   [
                  [{"label": "LABEL_1", "score": 0.5_74}, {"label": "LABEL_0", "score": 0.4_26}],
                  [{"label": "LABEL_1", "score": 0.5_74}, {"label": "LABEL_0", "score": 0.4_26}],
              ]	,   )
  
  
  
  
  
  
  
        def      _lowercase						(					self							)     ->		Optional[Any]:
  
  
  
          '''simple docstring'''
  
  
  
  
          a__			:      Union[str, Any]            =PreTrainedTokenizer()
  
          # Assert that the pipeline can be initialized with a feature extractor that is not in any mapping
          a__			:      List[Any]            =pipeline(
              "image-classification"	,   model="hf-internal-testing/tiny-random-vit"	,   tokenizer=lowerCAmelCase__							)
  
          self.assertIs(image_classifier.tokenizer	,   lowerCAmelCase__							)
  
  
  
  
  
  
  
        @slow
        @require_torch
        def      _lowercase						(					self							)     ->		Optional[Any]:
  
  
  
          '''simple docstring'''
  
  
  
  
          a__			:      List[str]            =pipeline("image-classification"	,   model="deepmind/vision-perceiver-conv"							)
          a__			:      Union[str, Any]            =image_classifier("http://images.cocodataset.org/val2017/000000039769.jpg"							)
          self.assertEqual(
              nested_simplify(lowerCAmelCase__	,   decimals=4							)	,   [
                  {"score": 0.43_85, "label": "tabby, tabby cat"},
                  {"score": 0.3_21, "label": "tiger cat"},
                  {"score": 0.05_02, "label": "Egyptian cat"},
                  {"score": 0.01_37, "label": "crib, cot"},
                  {"score": 0.0_07, "label": "radiator"},
              ]	,   )
  
          a__			:      int            =pipeline("image-classification"	,   model="deepmind/vision-perceiver-fourier"							)
          a__			:      Dict            =image_classifier("http://images.cocodataset.org/val2017/000000039769.jpg"							)
          self.assertEqual(
              nested_simplify(lowerCAmelCase__	,   decimals=4							)	,   [
                  {"score": 0.56_58, "label": "tabby, tabby cat"},
                  {"score": 0.13_09, "label": "tiger cat"},
                  {"score": 0.07_22, "label": "Egyptian cat"},
                  {"score": 0.07_07, "label": "remote control, remote"},
                  {"score": 0.00_82, "label": "computer keyboard, keypad"},
              ]	,   )
  
          a__			:      Union[str, Any]            =pipeline("image-classification"	,   model="deepmind/vision-perceiver-learned"							)
          a__			:      Optional[Any]            =image_classifier("http://images.cocodataset.org/val2017/000000039769.jpg"							)
          self.assertEqual(
              nested_simplify(lowerCAmelCase__	,   decimals=4							)	,   [
                  {"score": 0.30_22, "label": "tabby, tabby cat"},
                  {"score": 0.23_62, "label": "Egyptian cat"},
                  {"score": 0.18_56, "label": "tiger cat"},
                  {"score": 0.03_24, "label": "remote control, remote"},
                  {"score": 0.00_96, "label": "quilt, comforter, comfort, puff"},
              ]	,   )
  
  
  
  
  
  
  
style_context: 
  
  
  
  
  
  import tempfile
  import unittest
  
  import torch
  
  from diffusers import UNetaDConditionModel
  from diffusers.training_utils import EMAModel
  from diffusers.utils.testing_utils import enable_full_determinism, skip_mps, torch_device
  
  
  enable_full_determinism()
  
  
  
  
  
  
  
  class        __lowerCAmelCase (					unittest.TestCase):
        _lowercase       :      str        =    """hf-internal-testing/tiny-stable-diffusion-pipe"""
        _lowercase       :      Optional[int]        =    1
        _lowercase       :      List[str]        =    77
        _lowercase       :      int        =    32
        _lowercase       :      Dict        =    4
        _lowercase       :      str        =    64
        _lowercase       :      Optional[Any]        =    torch.manual_seed(0)
  
  
  
  
  
  
  
        def      _lowercase						(					self	,   lowerCAmelCase__=0.99_99							)     ->		Tuple:
  
  
  
          '''simple docstring'''
  
  
  
  
          a__			:      int            =UNetaDConditionModel.from_pretrained(self.model_id	,   subfolder="unet"							)
          a__			:      Any            =unet.to(lowerCAmelCase__							)
          a__			:      int            =EMAModel(unet.parameters()	,   decay=lowerCAmelCase__	,   model_cls=lowerCAmelCase__	,   model_config=unet.config							)
          return unet, ema_unet
  
  
  
  
  
  
  
        def      _lowercase						(					self							)     ->		Dict:
  
  
  
          '''simple docstring'''
  
  
  
  
          a__			:      str            =torch.randn(
              self.batch_size	,   self.num_in_channels	,   self.latent_height	,   self.latent_width	,   generator=self.generator							).to(lowerCAmelCase__							)
          a__			:      Any            =torch.randint(0	,   1_0_0_0	,   size=(self.batch_size,)	,   generator=self.generator							).to(lowerCAmelCase__							)
          a__			:      Tuple            =torch.randn(
              self.batch_size	,   self.prompt_length	,   self.text_encoder_hidden_dim	,   generator=self.generator							).to(lowerCAmelCase__							)
          return noisy_latents, timesteps, encoder_hidden_states
  
  
  
  
  
  
  
        def      _lowercase						(					self	,   lowerCAmelCase__							)     ->		Any:
  
  
  
          '''simple docstring'''
  
  
  
  
          a__			:      Any            ={}
          for k, param in unet.state_dict().items():
            a__			:      Tuple            =torch.randn_like(lowerCAmelCase__							) + (param * torch.randn_like(lowerCAmelCase__							))
            updated_state_dict.update({k: updated_param}							)
          unet.load_state_dict(lowerCAmelCase__							)
          return unet
  
  
  
  
  
  
  
        def      _lowercase						(					self							)     ->		Tuple:
  
  
  
          '''simple docstring'''
  
  
  
  
          a__      ,							a__			:      List[Any]            =self.get_models()
          # Take the first (hypothetical) EMA step.
          ema_unet.step(unet.parameters()							)
          assert ema_unet.optimization_step == 1
  
          # Take two more.
          for _ in range(2							):
            ema_unet.step(unet.parameters()							)
          assert ema_unet.optimization_step == 3
  
  
  
  
  
  
  
        def      _lowercase						(					self							)     ->		List[str]:
  
  
  
          '''simple docstring'''
  
  
  
  
          a__      ,							a__			:      Any            =self.get_models()
          # Since the `unet` is not being updated (i.e., backprop'd)
          # there won't be any difference between the `params` of `unet`
          # and `ema_unet` even if we call `ema_unet.step(unet.parameters())`.
          ema_unet.step(unet.parameters()							)
          a__			:      str            =list(unet.parameters()							)
          for s_param, param in zip(ema_unet.shadow_params	,   lowerCAmelCase__							):
            assert torch.allclose(lowerCAmelCase__	,   lowerCAmelCase__							)
  
          # The above holds true even if we call `ema.step()` multiple times since
          # `unet` params are still not being updated.
          for _ in range(4							):
            ema_unet.step(unet.parameters()							)
          for s_param, param in zip(ema_unet.shadow_params	,   lowerCAmelCase__							):
            assert torch.allclose(lowerCAmelCase__	,   lowerCAmelCase__							)
  
  
  
  
  
  
  
        def      _lowercase						(					self							)     ->		List[Any]:
  
  
  
          '''simple docstring'''
  
  
  
  
          a__      ,							a__			:      Optional[Any]            =self.get_models()
          # Here we simulate the parameter updates for `unet`. Since there might
          # be some parameters which are initialized to zero we take extra care to
          # initialize their values to something non-zero before the multiplication.
          a__			:      List[str]            =self.simulate_backprop(lowerCAmelCase__							)
  
          # Take the EMA step.
          ema_unet.step(unet_pseudo_updated_step_one.parameters()							)
  
          # Now the EMA'd parameters won't be equal to the original model parameters.
          a__			:      Optional[int]            =list(unet_pseudo_updated_step_one.parameters()							)
          for s_param, param in zip(ema_unet.shadow_params	,   lowerCAmelCase__							):
            assert ~torch.allclose(lowerCAmelCase__	,   lowerCAmelCase__							)
  
          # Ensure this is the case when we take multiple EMA steps.
          for _ in range(4							):
            ema_unet.step(unet.parameters()							)
          for s_param, param in zip(ema_unet.shadow_params	,   lowerCAmelCase__							):
            assert ~torch.allclose(lowerCAmelCase__	,   lowerCAmelCase__							)
  
  
  
  
  
  
  
        def      _lowercase						(					self							)     ->		Union[str, Any]:
  
  
  
          '''simple docstring'''
  
  
  
  
          a__      ,							a__			:      Dict            =self.get_models()
  
          # First backprop + EMA
          a__			:      Tuple            =self.simulate_backprop(lowerCAmelCase__							)
          ema_unet.step(unet_step_one.parameters()							)
          a__			:      Dict            =ema_unet.shadow_params
  
          # Second backprop + EMA
          a__			:      str            =self.simulate_backprop(lowerCAmelCase__							)
          ema_unet.step(unet_step_two.parameters()							)
          a__			:      List[str]            =ema_unet.shadow_params
  
          for step_one, step_two in zip(lowerCAmelCase__	,   lowerCAmelCase__							):
            assert ~torch.allclose(lowerCAmelCase__	,   lowerCAmelCase__							)
  
  
  
  
  
  
  
        def      _lowercase						(					self							)     ->		List[str]:
  
  
  
          '''simple docstring'''
  
  
  
  
          a__      ,							a__			:      int            =self.get_models(decay=0.0							)
          a__			:      Optional[int]            =self.simulate_backprop(lowerCAmelCase__							)
          ema_unet.step(unet_step_one.parameters()							)
          a__			:      List[Any]            =ema_unet.shadow_params
  
          a__			:      Tuple            =self.simulate_backprop(lowerCAmelCase__							)
          ema_unet.step(unet_step_two.parameters()							)
          a__			:      List[Any]            =ema_unet.shadow_params
  
          for step_one, step_two in zip(lowerCAmelCase__	,   lowerCAmelCase__							):
            assert torch.allclose(lowerCAmelCase__	,   lowerCAmelCase__							)
  
  
  
  
  
  
  
        @skip_mps
        def      _lowercase						(					self							)     ->		Optional[Any]:
  
  
  
          '''simple docstring'''
  
  
  
  
          a__      ,							a__			:      Union[str, Any]            =self.get_models()
          a__      ,							a__      ,							a__			:      Tuple            =self.get_dummy_inputs()
  
          with tempfile.TemporaryDirectory() as tmpdir:
            ema_unet.save_pretrained(lowerCAmelCase__							)
            a__			:      List[str]            =UNetaDConditionModel.from_pretrained(lowerCAmelCase__	,   model_cls=lowerCAmelCase__							)
            a__			:      Tuple            =loaded_unet.to(unet.device							)
  
          # Since no EMA step has been performed the outputs should match.
          a__			:      Optional[Any]            =unet(lowerCAmelCase__	,   lowerCAmelCase__	,   lowerCAmelCase__							).sample
          a__			:      Optional[int]            =loaded_unet(lowerCAmelCase__	,   lowerCAmelCase__	,   lowerCAmelCase__							).sample
  
          assert torch.allclose(lowerCAmelCase__	,   lowerCAmelCase__	,   atol=1E-4							)
  
  
  
  
  
  
  