





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

            @staticmethod
            def lowercase       (						*_SCREAMING_SNAKE_CASE:       str    ,      **_SCREAMING_SNAKE_CASE:       Optional[int]						)				->       str:




                   """simple docstring"""


                   pass



@is_pipeline_test
@require_torch_or_tf
@require_vision
class 							_UpperCamelCase    (		unittest.TestCase   ):
       _UpperCamelCase :    int   			=      MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING
       _UpperCamelCase :    Optional[Any]   			=      TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING

       def lowercase       (						self:       List[str]    ,      _SCREAMING_SNAKE_CASE:       str    ,      _SCREAMING_SNAKE_CASE:       List[Any]    ,      _SCREAMING_SNAKE_CASE:       Dict						)				->       List[str]:




              """simple docstring"""


              UpperCamelCase_      				=     ImageClassificationPipeline(model=_SCREAMING_SNAKE_CASE    ,      image_processor=_SCREAMING_SNAKE_CASE    ,      top_k=2						)
              UpperCamelCase_      				=     [
                  Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"						),
                  "http://images.cocodataset.org/val2017/000000039769.jpg",
              ]
              return image_classifier, examples

       def lowercase       (						self:       Any    ,      _SCREAMING_SNAKE_CASE:       str    ,      _SCREAMING_SNAKE_CASE:       List[Any]						)				->       Union[str, Any]:




              """simple docstring"""


              UpperCamelCase_      				=     image_classifier("./tests/fixtures/tests_samples/COCO/000000039769.png"						)

              self.assertEqual(
                  _SCREAMING_SNAKE_CASE    ,      [
                      {"score": ANY(_SCREAMING_SNAKE_CASE						), "label": ANY(_SCREAMING_SNAKE_CASE						)},
                      {"score": ANY(_SCREAMING_SNAKE_CASE						), "label": ANY(_SCREAMING_SNAKE_CASE						)},
                  ]    ,      )

              import datasets

              UpperCamelCase_      				=     datasets.load_dataset("hf-internal-testing/fixtures_image_utils"    ,      "image"    ,      split="test"						)

              # Accepts URL + PIL.Image + lists
              UpperCamelCase_      				=     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(
                  _SCREAMING_SNAKE_CASE    ,      [
                      [
                          {"score": ANY(_SCREAMING_SNAKE_CASE						), "label": ANY(_SCREAMING_SNAKE_CASE						)},
                          {"score": ANY(_SCREAMING_SNAKE_CASE						), "label": ANY(_SCREAMING_SNAKE_CASE						)},
                      ],
                      [
                          {"score": ANY(_SCREAMING_SNAKE_CASE						), "label": ANY(_SCREAMING_SNAKE_CASE						)},
                          {"score": ANY(_SCREAMING_SNAKE_CASE						), "label": ANY(_SCREAMING_SNAKE_CASE						)},
                      ],
                      [
                          {"score": ANY(_SCREAMING_SNAKE_CASE						), "label": ANY(_SCREAMING_SNAKE_CASE						)},
                          {"score": ANY(_SCREAMING_SNAKE_CASE						), "label": ANY(_SCREAMING_SNAKE_CASE						)},
                      ],
                      [
                          {"score": ANY(_SCREAMING_SNAKE_CASE						), "label": ANY(_SCREAMING_SNAKE_CASE						)},
                          {"score": ANY(_SCREAMING_SNAKE_CASE						), "label": ANY(_SCREAMING_SNAKE_CASE						)},
                      ],
                      [
                          {"score": ANY(_SCREAMING_SNAKE_CASE						), "label": ANY(_SCREAMING_SNAKE_CASE						)},
                          {"score": ANY(_SCREAMING_SNAKE_CASE						), "label": ANY(_SCREAMING_SNAKE_CASE						)},
                      ],
                  ]    ,      )

       @require_torch
       def lowercase       (						self:       Union[str, Any]						)				->       List[Any]:




              """simple docstring"""


              UpperCamelCase_      				=     "hf-internal-testing/tiny-random-vit"
              UpperCamelCase_      				=     pipeline("image-classification"    ,      model=_SCREAMING_SNAKE_CASE						)

              UpperCamelCase_      				=     image_classifier("http://images.cocodataset.org/val2017/000000039769.jpg"						)
              self.assertEqual(
                  nested_simplify(_SCREAMING_SNAKE_CASE    ,      decimals=4						)    ,      [{"label": "LABEL_1", "score": 0.5_74}, {"label": "LABEL_0", "score": 0.4_26}]    ,      )

              UpperCamelCase_      				=     image_classifier(
                  [
                      "http://images.cocodataset.org/val2017/000000039769.jpg",
                      "http://images.cocodataset.org/val2017/000000039769.jpg",
                  ]    ,      top_k=2    ,      )
              self.assertEqual(
                  nested_simplify(_SCREAMING_SNAKE_CASE    ,      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:       Dict						)				->       str:




              """simple docstring"""


              UpperCamelCase_      				=     "hf-internal-testing/tiny-random-vit"
              UpperCamelCase_      				=     pipeline("image-classification"    ,      model=_SCREAMING_SNAKE_CASE    ,      framework="tf"						)

              UpperCamelCase_      				=     image_classifier("http://images.cocodataset.org/val2017/000000039769.jpg"						)
              self.assertEqual(
                  nested_simplify(_SCREAMING_SNAKE_CASE    ,      decimals=4						)    ,      [{"label": "LABEL_1", "score": 0.5_74}, {"label": "LABEL_0", "score": 0.4_26}]    ,      )

              UpperCamelCase_      				=     image_classifier(
                  [
                      "http://images.cocodataset.org/val2017/000000039769.jpg",
                      "http://images.cocodataset.org/val2017/000000039769.jpg",
                  ]    ,      top_k=2    ,      )
              self.assertEqual(
                  nested_simplify(_SCREAMING_SNAKE_CASE    ,      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:       int						)				->       str:




              """simple docstring"""


              UpperCamelCase_      				=     PreTrainedTokenizer()

              # Assert that the pipeline can be initialized with a feature extractor that is not in any mapping
              UpperCamelCase_      				=     pipeline(
                  "image-classification"    ,      model="hf-internal-testing/tiny-random-vit"    ,      tokenizer=_SCREAMING_SNAKE_CASE						)

              self.assertIs(image_classifier.tokenizer    ,      _SCREAMING_SNAKE_CASE						)



       @slow
       @require_torch
       def lowercase       (						self:       Union[str, Any]						)				->       Optional[int]:




              """simple docstring"""


              UpperCamelCase_      				=     pipeline("image-classification"    ,      model="deepmind/vision-perceiver-conv"						)
              UpperCamelCase_      				=     image_classifier("http://images.cocodataset.org/val2017/000000039769.jpg"						)
              self.assertEqual(
                  nested_simplify(_SCREAMING_SNAKE_CASE    ,      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"},
                  ]    ,      )

              UpperCamelCase_      				=     pipeline("image-classification"    ,      model="deepmind/vision-perceiver-fourier"						)
              UpperCamelCase_      				=     image_classifier("http://images.cocodataset.org/val2017/000000039769.jpg"						)
              self.assertEqual(
                  nested_simplify(_SCREAMING_SNAKE_CASE    ,      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"},
                  ]    ,      )

              UpperCamelCase_      				=     pipeline("image-classification"    ,      model="deepmind/vision-perceiver-learned"						)
              UpperCamelCase_      				=     image_classifier("http://images.cocodataset.org/val2017/000000039769.jpg"						)
              self.assertEqual(
                  nested_simplify(_SCREAMING_SNAKE_CASE    ,      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"},
                  ]    ,      )








import unittest
from functools import partial

import torch

from accelerate import Accelerator, debug_launcher
from accelerate.state import AcceleratorState, GradientState
from accelerate.test_utils import require_cpu, require_huggingface_suite
from accelerate.utils import GradientAccumulationPlugin




def        lowerCAmelCase_       (							UpperCamelCase_=2    ,						UpperCamelCase_=True    ,						UpperCamelCase_=False     )   ->						str:
       UpperCamelCase_      				=     Accelerator(step_scheduler_with_optimizer=UpperCamelCase_    ,						split_batches=UpperCamelCase_     )
       UpperCamelCase_      				=     torch.nn.Linear(2    ,						4     )
       UpperCamelCase_      				=     torch.optim.AdamW(model.parameters()    ,						lr=1.0     )
       UpperCamelCase_      				=     torch.optim.lr_scheduler.OneCycleLR(UpperCamelCase_    ,						max_lr=0.01    ,						steps_per_epoch=2    ,						epochs=1     )
       UpperCamelCase_				,       UpperCamelCase_				,       UpperCamelCase_      				=     accelerator.prepare(UpperCamelCase_    ,						UpperCamelCase_    ,						UpperCamelCase_     )

       # Optimizer has stepped
       scheduler.step()
       if step_scheduler_with_optimizer or (num_processes == 1):
              assert (
                  scheduler.scheduler.last_epoch == num_processes
              ), F'''Last Epoch ({scheduler.scheduler.last_epoch}) != Num Processes ({num_processes})'''
       else:
              assert (
                  scheduler.scheduler.last_epoch != num_processes
              ), F'''Last Epoch ({scheduler.scheduler.last_epoch}) == Num Processes ({num_processes})'''




def        lowerCAmelCase_       (							UpperCamelCase_=2    ,						UpperCamelCase_=True    ,						UpperCamelCase_=False     )   ->						List[Any]:
       UpperCamelCase_      				=     Accelerator(step_scheduler_with_optimizer=UpperCamelCase_    ,						split_batches=UpperCamelCase_     )
       UpperCamelCase_      				=     torch.nn.Linear(2    ,						4     )
       UpperCamelCase_      				=     torch.optim.AdamW(model.parameters()    ,						lr=1.0     )
       UpperCamelCase_      				=     torch.optim.lr_scheduler.LambdaLR(UpperCamelCase_    ,						lr_lambda=lambda UpperCamelCase_     : 1 - n / 10     )
       UpperCamelCase_				,       UpperCamelCase_				,       UpperCamelCase_      				=     accelerator.prepare(UpperCamelCase_    ,						UpperCamelCase_    ,						UpperCamelCase_     )

       # Optimizer has stepped
       UpperCamelCase_      				=     False
       scheduler.step()
       UpperCamelCase_      				=     1 - (num_processes if (step_scheduler_with_optimizer and not split_batches) else 1) / 10
       assert (
           scheduler.get_last_lr()[0] == expected_lr
       ), F'''Wrong lr found at first step, expected {expected_lr}, got {scheduler.get_last_lr()[0]}'''

       # Optimizer has not stepped
       UpperCamelCase_      				=     True
       scheduler.step()
       if not step_scheduler_with_optimizer:
              UpperCamelCase_      				=     1 - 2 / 10
       assert (
           scheduler.get_last_lr()[0] == expected_lr
       ), F'''Wrong lr found at second step, expected {expected_lr}, got {scheduler.get_last_lr()[0]}'''




def        lowerCAmelCase_       (							UpperCamelCase_ = 2     )   ->						int:
       from transformers import get_linear_schedule_with_warmup

       UpperCamelCase_      				=     [1, 2, 4]
       for num_steps in steps:
              UpperCamelCase_      				=     GradientAccumulationPlugin(num_steps=UpperCamelCase_    ,						adjust_scheduler=num_steps > 1     )
              UpperCamelCase_      				=     Accelerator(gradient_accumulation_plugin=UpperCamelCase_     )
              UpperCamelCase_      				=     torch.nn.Linear(2    ,						4     )
              UpperCamelCase_      				=     torch.optim.AdamW(model.parameters()    ,						lr=10.0     )
              UpperCamelCase_      				=     get_linear_schedule_with_warmup(optimizer=UpperCamelCase_    ,						num_warmup_steps=0    ,						num_training_steps=20     )

              UpperCamelCase_				,       UpperCamelCase_				,       UpperCamelCase_      				=     accelerator.prepare(UpperCamelCase_    ,						UpperCamelCase_    ,						UpperCamelCase_     )

              for i in range(10 * num_steps     ):
                     with accelerator.accumulate(UpperCamelCase_     ):
                            optimizer.step()
                            scheduler.step()

                     if i == (10 * num_steps - 2):
                            assert (
                                scheduler.get_last_lr()[0] != 0
                            ), F'''Wrong lr found at second-to-last step, expected non-zero, got {scheduler.get_last_lr()[0]}. num_steps: {num_steps}'''
              assert (
                  scheduler.get_last_lr()[0] == 0
              ), F'''Wrong lr found at last step, expected 0, got {scheduler.get_last_lr()[0]}'''
              GradientState._reset_state()







@require_cpu
class 							_UpperCamelCase    (		unittest.TestCase   ):

       def lowercase       (						self:       Dict						)				->       Any:




              """simple docstring"""


              debug_launcher(partial(_SCREAMING_SNAKE_CASE    ,      num_processes=1						)    ,      num_processes=1						)
              debug_launcher(partial(_SCREAMING_SNAKE_CASE    ,      num_processes=1    ,      split_batches=_SCREAMING_SNAKE_CASE						)    ,      num_processes=1						)

       def lowercase       (						self:       Dict						)				->       Dict:




              """simple docstring"""


              debug_launcher(partial(_SCREAMING_SNAKE_CASE    ,      num_processes=1						)    ,      num_processes=1						)
              debug_launcher(partial(_SCREAMING_SNAKE_CASE    ,      num_processes=1    ,      split_batches=_SCREAMING_SNAKE_CASE						)    ,      num_processes=1						)

       def lowercase       (						self:       Dict						)				->       List[str]:




              """simple docstring"""


              debug_launcher(partial(_SCREAMING_SNAKE_CASE    ,      num_processes=1    ,      step_scheduler_with_optimizer=_SCREAMING_SNAKE_CASE						)    ,      num_processes=1						)

       def lowercase       (						self:       Any						)				->       int:




              """simple docstring"""


              debug_launcher(partial(_SCREAMING_SNAKE_CASE    ,      num_processes=1    ,      step_scheduler_with_optimizer=_SCREAMING_SNAKE_CASE						)    ,      num_processes=1						)

       def lowercase       (						self:       Optional[int]						)				->       List[str]:




              """simple docstring"""


              AcceleratorState._reset_state(_SCREAMING_SNAKE_CASE						)
              debug_launcher(_SCREAMING_SNAKE_CASE						)
              debug_launcher(partial(_SCREAMING_SNAKE_CASE    ,      num_processes=1    ,      split_batches=_SCREAMING_SNAKE_CASE						)    ,      num_processes=1						)

       def lowercase       (						self:       Tuple						)				->       Optional[int]:




              """simple docstring"""


              AcceleratorState._reset_state(_SCREAMING_SNAKE_CASE						)
              debug_launcher(_SCREAMING_SNAKE_CASE						)
              debug_launcher(partial(_SCREAMING_SNAKE_CASE    ,      num_processes=1    ,      split_batches=_SCREAMING_SNAKE_CASE						)    ,      num_processes=1						)

       def lowercase       (						self:       int						)				->       Tuple:




              """simple docstring"""


              AcceleratorState._reset_state(_SCREAMING_SNAKE_CASE						)
              debug_launcher(partial(_SCREAMING_SNAKE_CASE    ,      step_scheduler_with_optimizer=_SCREAMING_SNAKE_CASE						)						)

       def lowercase       (						self:       Dict						)				->       Dict:




              """simple docstring"""


              AcceleratorState._reset_state(_SCREAMING_SNAKE_CASE						)
              debug_launcher(partial(_SCREAMING_SNAKE_CASE    ,      step_scheduler_with_optimizer=_SCREAMING_SNAKE_CASE						)						)



       @require_huggingface_suite
       def lowercase       (						self:       Dict						)				->       Optional[Any]:




              """simple docstring"""


              AcceleratorState._reset_state(_SCREAMING_SNAKE_CASE						)
              debug_launcher(partial(_SCREAMING_SNAKE_CASE    ,      num_processes=1						)						)
              debug_launcher(_SCREAMING_SNAKE_CASE						)

