'''simple docstring'''

import inspect
import unittest

from transformers import MobileNetVaConfig
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 MobileNetVaForImageClassification, MobileNetVaForSemanticSegmentation, MobileNetVaModel
  from transformers.models.mobilenet_va.modeling_mobilenet_va import MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
  from PIL import Image

  from transformers import MobileNetVaImageProcessor



class       UpperCAmelCase  (						a__ ):





       '''simple docstring'''



       def _lowerCAmelCase(						self			)  ->  Union[str, Any]:
        lowercase__    :   int						= self.config_class(**self.inputs_dict			)
        self.parent.assertTrue(hasattr(__lowerCAmelCase     ,       '''tf_padding'''			)			)
        self.parent.assertTrue(hasattr(__lowerCAmelCase     ,       '''depth_multiplier'''			)			)



class       UpperCAmelCase  :





       '''simple docstring'''



       def __init__(						self     ,       __lowerCAmelCase     ,       __lowerCAmelCase=13     ,       __lowerCAmelCase=3     ,       __lowerCAmelCase=32     ,       __lowerCAmelCase=0.2_5     ,       __lowerCAmelCase=8     ,       __lowerCAmelCase=8     ,       __lowerCAmelCase=6     ,       __lowerCAmelCase=32     ,       __lowerCAmelCase=True     ,       __lowerCAmelCase=True     ,       __lowerCAmelCase=True     ,       __lowerCAmelCase="relu6"     ,       __lowerCAmelCase=1280     ,       __lowerCAmelCase=0.1     ,       __lowerCAmelCase=0.0_2     ,       __lowerCAmelCase=True     ,       __lowerCAmelCase=True     ,       __lowerCAmelCase=10     ,       __lowerCAmelCase=None     ,       )  ->  List[Any]:
        lowercase__    :   str						= parent
        lowercase__    :   Union[str, Any]						= batch_size
        lowercase__    :   Tuple						= num_channels
        lowercase__    :   Union[str, Any]						= image_size
        lowercase__    :   int						= depth_multiplier
        lowercase__    :   Dict						= depth_divisible_by
        lowercase__    :   Optional[Any]						= min_depth
        lowercase__    :   int						= expand_ratio
        lowercase__    :   Union[str, Any]						= tf_padding
        lowercase__    :   int						= output_stride
        lowercase__    :   List[Any]						= first_layer_is_expansion
        lowercase__    :   List[Any]						= finegrained_output
        lowercase__    :   List[Any]						= hidden_act
        lowercase__    :   int						= last_hidden_size if finegrained_output else int(last_hidden_size * depth_multiplier			)
        lowercase__    :   int						= classifier_dropout_prob
        lowercase__    :   Optional[int]						= use_labels
        lowercase__    :   Optional[Any]						= is_training
        lowercase__    :   Tuple						= num_labels
        lowercase__    :   Union[str, Any]						= initializer_range
        lowercase__    :   Any						= scope

       def _lowerCAmelCase(						self			)  ->  List[str]:
        lowercase__    :   Optional[int]						= floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]			)

        lowercase__    :   List[Any]						= None
        lowercase__    :   str						= None
        if self.use_labels:
         lowercase__    :   int						= ids_tensor([self.batch_size]     ,       self.num_labels			)
         lowercase__    :   Optional[int]						= ids_tensor([self.batch_size, self.image_size, self.image_size]     ,       self.num_labels			)

        lowercase__    :   List[Any]						= self.get_config()

        return config, pixel_values, labels, pixel_labels

       def _lowerCAmelCase(						self			)  ->  str:
        return MobileNetVaConfig(
            num_channels=self.num_channels     ,       image_size=self.image_size     ,       depth_multiplier=self.depth_multiplier     ,       depth_divisible_by=self.depth_divisible_by     ,       min_depth=self.min_depth     ,       expand_ratio=self.expand_ratio     ,       output_stride=self.output_stride     ,       first_layer_is_expansion=self.first_layer_is_expansion     ,       finegrained_output=self.finegrained_output     ,       hidden_act=self.hidden_act     ,       tf_padding=self.tf_padding     ,       classifier_dropout_prob=self.classifier_dropout_prob     ,       initializer_range=self.initializer_range     ,       )

       def _lowerCAmelCase(						self     ,       __lowerCAmelCase     ,       __lowerCAmelCase     ,       __lowerCAmelCase     ,       __lowerCAmelCase			)  ->  Any:
        lowercase__    :   List[str]						= MobileNetVaModel(config=__lowerCAmelCase			)
        model.to(__lowerCAmelCase			)
        model.eval()
        lowercase__    :   int						= model(__lowerCAmelCase			)
        self.parent.assertEqual(
            result.last_hidden_state.shape     ,       (
                self.batch_size,
                self.last_hidden_size,
                self.image_size // self.output_stride,
                self.image_size // self.output_stride,
            )     ,       )
        self.parent.assertEqual(
            result.pooler_output.shape     ,       (self.batch_size, self.last_hidden_size)     ,       )

       def _lowerCAmelCase(						self     ,       __lowerCAmelCase     ,       __lowerCAmelCase     ,       __lowerCAmelCase     ,       __lowerCAmelCase			)  ->  int:
        lowercase__    :   int						= self.num_labels
        lowercase__    :   Optional[Any]						= MobileNetVaForImageClassification(__lowerCAmelCase			)
        model.to(__lowerCAmelCase			)
        model.eval()
        lowercase__    :   Union[str, Any]						= model(__lowerCAmelCase     ,       labels=__lowerCAmelCase			)
        self.parent.assertEqual(result.logits.shape     ,       (self.batch_size, self.num_labels)			)

       def _lowerCAmelCase(						self     ,       __lowerCAmelCase     ,       __lowerCAmelCase     ,       __lowerCAmelCase     ,       __lowerCAmelCase			)  ->  List[Any]:
        lowercase__    :   Union[str, Any]						= self.num_labels
        lowercase__    :   List[str]						= MobileNetVaForSemanticSegmentation(__lowerCAmelCase			)
        model.to(__lowerCAmelCase			)
        model.eval()
        lowercase__    :   int						= model(__lowerCAmelCase			)
        self.parent.assertEqual(
            result.logits.shape     ,       (
                self.batch_size,
                self.num_labels,
                self.image_size // self.output_stride,
                self.image_size // self.output_stride,
            )     ,       )
        lowercase__    :   Optional[Any]						= model(__lowerCAmelCase     ,       labels=__lowerCAmelCase			)
        self.parent.assertEqual(
            result.logits.shape     ,       (
                self.batch_size,
                self.num_labels,
                self.image_size // self.output_stride,
                self.image_size // self.output_stride,
            )     ,       )


       def _lowerCAmelCase(						self			)  ->  Optional[Any]:
        lowercase__    :   Any						= self.prepare_config_and_inputs()
        lowercase__			,							lowercase__			,							lowercase__			,							lowercase__    :   Dict						= config_and_inputs
        lowercase__    :   Union[str, Any]						= {'''pixel_values''': pixel_values}
        return config, inputs_dict



@require_torch
class       UpperCAmelCase  (						a__   ,						a__   ,						unittest.TestCase ):





       '''simple docstring'''



       SCREAMING_SNAKE_CASE 		=   (
           (MobileNetVaModel, MobileNetVaForImageClassification, MobileNetVaForSemanticSegmentation)
           if is_torch_available()
           else ()
       )
       SCREAMING_SNAKE_CASE 		=   (
           {
               "feature-extraction": MobileNetVaModel,
               "image-classification": MobileNetVaForImageClassification,
               "image-segmentation": MobileNetVaForSemanticSegmentation,
           }
           if is_torch_available()
           else {}
       )

       SCREAMING_SNAKE_CASE 		=   False
       SCREAMING_SNAKE_CASE 		=   False
       SCREAMING_SNAKE_CASE 		=   False
       SCREAMING_SNAKE_CASE 		=   False

       def _lowerCAmelCase(						self			)  ->  Optional[int]:
        lowercase__    :   Union[str, Any]						= MobileNetVaModelTester(self			)
        lowercase__    :   Dict						= MobileNetVaConfigTester(self     ,       config_class=__lowerCAmelCase     ,       has_text_modality=__lowerCAmelCase			)

       def _lowerCAmelCase(						self			)  ->  Optional[Any]:
        self.config_tester.run_common_tests()

       @unittest.skip(reason='''MobileNetV2 does not use inputs_embeds'''			)
       def _lowerCAmelCase(						self			)  ->  Optional[Any]:
        pass

       @unittest.skip(reason='''MobileNetV2 does not support input and output embeddings'''			)
       def _lowerCAmelCase(						self			)  ->  int:
        pass

       @unittest.skip(reason='''MobileNetV2 does not output attentions'''			)
       def _lowerCAmelCase(						self			)  ->  int:
        pass

       def _lowerCAmelCase(						self			)  ->  Any:
        lowercase__			,							lowercase__    :   Tuple						= self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
         lowercase__    :   List[Any]						= model_class(__lowerCAmelCase			)
         lowercase__    :   int						= inspect.signature(model.forward			)
         # signature.parameters is an OrderedDict => so arg_names order is deterministic
         lowercase__    :   Union[str, Any]						= [*signature.parameters.keys()]

         lowercase__    :   List[Any]						= ['''pixel_values''']
         self.assertListEqual(arg_names[:1]     ,       __lowerCAmelCase			)

       def _lowerCAmelCase(						self			)  ->  Optional[int]:
        lowercase__    :   List[str]						= self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*__lowerCAmelCase			)

       def _lowerCAmelCase(						self			)  ->  str:
        def check_hidden_states_output(__lowerCAmelCase     ,       __lowerCAmelCase     ,       __lowerCAmelCase			):
         lowercase__    :   Optional[Any]						= model_class(__lowerCAmelCase			)
         model.to(__lowerCAmelCase			)
         model.eval()

         with torch.no_grad():
          lowercase__    :   Optional[Any]						= model(**self._prepare_for_class(__lowerCAmelCase     ,       __lowerCAmelCase			)			)

         lowercase__    :   Dict						= outputs.hidden_states

         lowercase__    :   List[str]						= 16
         self.assertEqual(len(__lowerCAmelCase			)     ,       __lowerCAmelCase			)

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

        for model_class in self.all_model_classes:
         lowercase__    :   Any						= True
         check_hidden_states_output(__lowerCAmelCase     ,       __lowerCAmelCase     ,       __lowerCAmelCase			)

         # check that output_hidden_states also work using config
         del inputs_dict["output_hidden_states"]
         lowercase__    :   List[str]						= True

         check_hidden_states_output(__lowerCAmelCase     ,       __lowerCAmelCase     ,       __lowerCAmelCase			)

       def _lowerCAmelCase(						self			)  ->  Union[str, Any]:
        lowercase__    :   Optional[Any]						= self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_classification(*__lowerCAmelCase			)

       def _lowerCAmelCase(						self			)  ->  Optional[Any]:
        lowercase__    :   str						= self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_semantic_segmentation(*__lowerCAmelCase			)


       @slow
       def _lowerCAmelCase(						self			)  ->  str:
        for model_name in MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         lowercase__    :   List[Any]						= MobileNetVaModel.from_pretrained(__lowerCAmelCase			)
         self.assertIsNotNone(__lowerCAmelCase			)


def       __UpperCamelCase	(       ):
 lowercase__    :   Optional[Any]						= Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''					)
 return image





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





       '''simple docstring'''



       @cached_property
       def _lowerCAmelCase(						self			)  ->  Optional[Any]:
        return (
            MobileNetVaImageProcessor.from_pretrained('''google/mobilenet_v2_1.0_224'''			) if is_vision_available() else None
        )

       @slow
       def _lowerCAmelCase(						self			)  ->  Any:
        lowercase__    :   List[Any]						= MobileNetVaForImageClassification.from_pretrained('''google/mobilenet_v2_1.0_224'''			).to(__lowerCAmelCase			)

        lowercase__    :   Tuple						= self.default_image_processor
        lowercase__    :   Any						= prepare_img()
        lowercase__    :   Union[str, Any]						= image_processor(images=__lowerCAmelCase     ,       return_tensors='''pt'''			).to(__lowerCAmelCase			)

        # forward pass
        with torch.no_grad():
         lowercase__    :   List[Any]						= model(**__lowerCAmelCase			)

        # verify the logits
        lowercase__    :   Union[str, Any]						= torch.Size((1, 1001)			)
        self.assertEqual(outputs.logits.shape     ,       __lowerCAmelCase			)

        lowercase__    :   List[str]						= torch.tensor([0.2_4_4_5, -1.1_9_9_3, 0.1_9_0_5]			).to(__lowerCAmelCase			)

        self.assertTrue(torch.allclose(outputs.logits[0, :3]     ,       __lowerCAmelCase     ,       atol=1E-4			)			)


       @slow
       def _lowerCAmelCase(						self			)  ->  int:
        lowercase__    :   str						= MobileNetVaForSemanticSegmentation.from_pretrained('''google/deeplabv3_mobilenet_v2_1.0_513'''			)
        lowercase__    :   str						= model.to(__lowerCAmelCase			)

        lowercase__    :   Optional[Any]						= MobileNetVaImageProcessor.from_pretrained('''google/deeplabv3_mobilenet_v2_1.0_513'''			)

        lowercase__    :   List[str]						= prepare_img()
        lowercase__    :   Optional[int]						= image_processor(images=__lowerCAmelCase     ,       return_tensors='''pt'''			).to(__lowerCAmelCase			)

        # forward pass
        with torch.no_grad():
         lowercase__    :   int						= model(**__lowerCAmelCase			)
        lowercase__    :   List[str]						= outputs.logits

        # verify the logits
        lowercase__    :   List[Any]						= torch.Size((1, 21, 65, 65)			)
        self.assertEqual(logits.shape     ,       __lowerCAmelCase			)

        lowercase__    :   str						= torch.tensor(
            [
                [[1_7.5_7_9_0, 1_7.7_5_8_1, 1_8.3_3_5_5], [1_8.3_2_5_7, 1_8.4_2_3_0, 1_8.8_9_7_3], [1_8.6_1_6_9, 1_8.8_6_5_0, 1_9.2_1_8_7]],
                [[-2.1_5_9_5, -2.0_9_7_7, -2.3_7_4_1], [-2.4_2_2_6, -2.3_0_2_8, -2.6_8_3_5], [-2.7_8_1_9, -2.5_9_9_1, -2.7_7_0_6]],
                [[4.2_0_5_8, 4.8_3_1_7, 4.7_6_3_8], [4.4_1_3_6, 5.0_3_6_1, 4.9_3_8_3], [4.5_0_2_8, 4.9_6_4_4, 4.8_7_3_4]],
            ]     ,       device=__lowerCAmelCase     ,       )

        self.assertTrue(torch.allclose(logits[0, :3, :3, :3]     ,       __lowerCAmelCase     ,       atol=1E-4			)			)


'''simple docstring'''

from manim import *



class       UpperCAmelCase  (						a__ ):





       '''simple docstring'''



       def _lowerCAmelCase(						self			)  ->  List[Any]:
        lowercase__    :   int						= Rectangle(height=0.5     ,       width=0.5			)
        lowercase__    :   Optional[int]						= Rectangle(height=0.2_5     ,       width=0.2_5			)
        lowercase__    :   Tuple						= Rectangle(height=0.4_6     ,       width=0.4_6			).set_stroke(width=0			)

        lowercase__    :   str						= [mem.copy() for i in range(6			)]
        lowercase__    :   Dict						= [mem.copy() for i in range(6			)]
        lowercase__    :   Tuple						= VGroup(*__lowerCAmelCase			).arrange(__lowerCAmelCase     ,       buff=0			)
        lowercase__    :   List[str]						= VGroup(*__lowerCAmelCase			).arrange(__lowerCAmelCase     ,       buff=0			)
        lowercase__    :   str						= VGroup(__lowerCAmelCase     ,       __lowerCAmelCase			).arrange(__lowerCAmelCase     ,       buff=0			)
        lowercase__    :   str						= Text('''CPU'''     ,       font_size=24			)
        lowercase__    :   List[Any]						= Group(__lowerCAmelCase     ,       __lowerCAmelCase			).arrange(__lowerCAmelCase     ,       buff=0.5     ,       aligned_edge=__lowerCAmelCase			)
        cpu.move_to([-2.5, -0.5, 0]			)
        self.add(__lowerCAmelCase			)

        lowercase__    :   Any						= [mem.copy() for i in range(4			)]
        lowercase__    :   int						= VGroup(*__lowerCAmelCase			).arrange(__lowerCAmelCase     ,       buff=0			)
        lowercase__    :   Optional[int]						= Text('''GPU'''     ,       font_size=24			)
        lowercase__    :   Tuple						= Group(__lowerCAmelCase     ,       __lowerCAmelCase			).arrange(__lowerCAmelCase     ,       buff=0.5     ,       aligned_edge=__lowerCAmelCase			)
        gpu.move_to([-1, -1, 0]			)
        self.add(__lowerCAmelCase			)

        lowercase__    :   int						= [mem.copy() for i in range(6			)]
        lowercase__    :   Optional[int]						= VGroup(*__lowerCAmelCase			).arrange(__lowerCAmelCase     ,       buff=0			)

        lowercase__    :   Any						= Text('''Model'''     ,       font_size=24			)
        lowercase__    :   Optional[Any]						= Group(__lowerCAmelCase     ,       __lowerCAmelCase			).arrange(__lowerCAmelCase     ,       buff=0.5     ,       aligned_edge=__lowerCAmelCase			)
        model.move_to([3, -1.0, 0]			)
        self.add(__lowerCAmelCase			)

        lowercase__    :   int						= []
        lowercase__    :   int						= []
        lowercase__    :   Any						= []

        for i, rect in enumerate(__lowerCAmelCase			):
         rect.set_stroke(__lowerCAmelCase			)

         lowercase__    :   Optional[int]						= Rectangle(height=0.4_6 / 4     ,       width=0.4_6 / 3			).set_stroke(width=0.0			).set_fill(__lowerCAmelCase     ,       opacity=0.7			)

         if i == 0:
          cpu_target.next_to(cpu_left_col_base[0].get_corner(DOWN + LEFT			)     ,       buff=0.0_2     ,       direction=__lowerCAmelCase			)
          cpu_target.set_x(cpu_target.get_x() + 0.1			)
         elif i == 3:
          cpu_target.next_to(model_cpu_arr[0]     ,       direction=__lowerCAmelCase     ,       buff=0.0			)
         else:
          cpu_target.next_to(model_cpu_arr[i - 1]     ,       direction=__lowerCAmelCase     ,       buff=0.0			)
         self.add(__lowerCAmelCase			)
         model_cpu_arr.append(__lowerCAmelCase			)

        self.add(*__lowerCAmelCase     ,       *__lowerCAmelCase     ,       *__lowerCAmelCase			)

        lowercase__    :   Optional[int]						= [mem.copy() for i in range(6			)]
        lowercase__    :   List[Any]						= VGroup(*__lowerCAmelCase			).arrange(__lowerCAmelCase     ,       buff=0			)

        lowercase__    :   Optional[Any]						= Text('''Loaded Checkpoint'''     ,       font_size=24			)
        lowercase__    :   Dict						= Group(__lowerCAmelCase     ,       __lowerCAmelCase			).arrange(__lowerCAmelCase     ,       buff=0.5     ,       aligned_edge=__lowerCAmelCase			)
        checkpoint.move_to([3, 0.5, 0]			)

        self.add(__lowerCAmelCase			)

        lowercase__    :   str						= []
        lowercase__    :   List[str]						= []

        for i, rect in enumerate(__lowerCAmelCase			):
         lowercase__    :   List[str]						= fill.copy().set_fill(__lowerCAmelCase     ,       opacity=0.7			)
         target.move_to(__lowerCAmelCase			)
         ckpt_arr.append(__lowerCAmelCase			)

         lowercase__    :   Any						= target.copy()
         if i < 5:
          cpu_target.move_to(cpu_left_col_base[i + 1]			)
         else:
          cpu_target.move_to(cpu_right_col_base[i - 5]			)
         ckpt_cpu_arr.append(__lowerCAmelCase			)
        self.add(*__lowerCAmelCase     ,       *__lowerCAmelCase			)

        lowercase__    :   Optional[int]						= Square(side_length=2.2			)
        key.move_to([-5, 2, 0]			)

        lowercase__    :   Optional[Any]						= MarkupText(
            F"""<b>Key:</b>\n\n<span fgcolor='{YELLOW}'>●</span> Empty Model"""     ,       font_size=18     ,       )

        key_text.move_to([-5, 2.4, 0]			)

        self.add(__lowerCAmelCase     ,       __lowerCAmelCase			)

        lowercase__    :   str						= MarkupText(
            F"""<span fgcolor='{BLUE}'>●</span> Checkpoint"""     ,       font_size=18     ,       )

        blue_text.next_to(__lowerCAmelCase     ,       DOWN * 2.4     ,       aligned_edge=key_text.get_left()			)
        self.add(__lowerCAmelCase			)

        lowercase__    :   Union[str, Any]						= MarkupText(
            F"""Based on the passed in configuration, weights are stored in\na variety of np.memmaps on disk or to a particular device."""     ,       font_size=24     ,       )
        step_a.move_to([2, 2, 0]			)

        lowercase__    :   Tuple						= [meta_mem.copy() for i in range(6			)]
        lowercase__    :   Any						= [meta_mem.copy() for i in range(6			)]
        lowercase__    :   str						= VGroup(*__lowerCAmelCase			).arrange(__lowerCAmelCase     ,       buff=0			)
        lowercase__    :   Any						= VGroup(*__lowerCAmelCase			).arrange(__lowerCAmelCase     ,       buff=0			)
        lowercase__    :   Union[str, Any]						= VGroup(__lowerCAmelCase     ,       __lowerCAmelCase			).arrange(__lowerCAmelCase     ,       buff=0			)
        lowercase__    :   Union[str, Any]						= Text('''Disk'''     ,       font_size=24			)
        lowercase__    :   Tuple						= Group(__lowerCAmelCase     ,       __lowerCAmelCase			).arrange(__lowerCAmelCase     ,       buff=0.5     ,       aligned_edge=__lowerCAmelCase			)
        disk.move_to([-4.0, -1.2_5, 0]			)
        self.play(Write(__lowerCAmelCase     ,       run_time=3			)     ,       Write(__lowerCAmelCase     ,       run_time=1			)     ,       Create(__lowerCAmelCase     ,       run_time=1			)			)

        lowercase__    :   Tuple						= []
        for i, rect in enumerate(__lowerCAmelCase			):
         lowercase__    :   Dict						= rect.copy()
         target.generate_target()
         target.target.move_to(disk_left_col_base[i]			).scale(0.5			)
         animations.append(MoveToTarget(__lowerCAmelCase     ,       run_time=1.5			)			)
        self.play(*__lowerCAmelCase			)

        self.play(FadeOut(__lowerCAmelCase			)			)

        lowercase__    :   Dict						= MarkupText(F"""Then, the checkpoint is removed from memory\nthrough garbage collection."""     ,       font_size=24			)
        step_a.move_to([2, 2, 0]			)

        self.play(Write(__lowerCAmelCase     ,       run_time=3			)			)

        self.play(
            FadeOut(__lowerCAmelCase     ,       __lowerCAmelCase     ,       *__lowerCAmelCase     ,       *__lowerCAmelCase			)     ,       )

        self.wait()

