
'''simple docstring'''





import inspect
import unittest

from transformers import ResNetConfig, is_flax_available
from transformers.testing_utils import require_flax, slow
from transformers.utils import cached_property, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor


if is_flax_available():
     import jax
     import jax.numpy as jnp

     from transformers.models.resnet.modeling_flax_resnet import FlaxResNetForImageClassification, FlaxResNetModel

if is_vision_available():
     from PIL import Image

     from transformers import AutoImageProcessor



class 			lowerCAmelCase__ (							unittest.TestCase							):



  '''simple docstring'''






  def __init__(self	:			List[Any]						,			lowerCAmelCase_	:			Union[str, Any]						,			lowerCAmelCase_	:			int=3						,			lowerCAmelCase_	:			List[Any]=32						,			lowerCAmelCase_	:			str=3						,			lowerCAmelCase_	:			Dict=10						,			lowerCAmelCase_	:			Dict=[10, 20, 30, 40]						,			lowerCAmelCase_	:			Any=[1, 1, 2, 1]						,			lowerCAmelCase_	:			List[Any]=True						,			lowerCAmelCase_	:			str=True						,			lowerCAmelCase_	:			List[Any]="relu"						,			lowerCAmelCase_	:			List[str]=3						,			lowerCAmelCase_	:			List[Any]=None						,			) ->							Dict:
        snake_case_    							=				parent
        snake_case_    							=				batch_size
        snake_case_    							=				image_size
        snake_case_    							=				num_channels
        snake_case_    							=				embeddings_size
        snake_case_    							=				hidden_sizes
        snake_case_    							=				depths
        snake_case_    							=				is_training
        snake_case_    							=				use_labels
        snake_case_    							=				hidden_act
        snake_case_    							=				num_labels
        snake_case_    							=				scope
        snake_case_    							=				len(lowerCAmelCase_     )

  def 		_lowerCamelCase						(self	:			Optional[Any]     ) ->							int:
        snake_case_    							=				floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]     )

        snake_case_    							=				self.get_config()

        return config, pixel_values

  def 		_lowerCamelCase						(self	:			List[str]     ) ->							Tuple:
        return ResNetConfig(
            num_channels=self.num_channels						,			embeddings_size=self.embeddings_size						,			hidden_sizes=self.hidden_sizes						,			depths=self.depths						,			hidden_act=self.hidden_act						,			num_labels=self.num_labels						,			image_size=self.image_size						,			)

  def 		_lowerCamelCase						(self	:			int						,			lowerCAmelCase_	:			List[str]						,			lowerCAmelCase_	:			Dict     ) ->							Optional[int]:
        snake_case_    							=				FlaxResNetModel(config=lowerCAmelCase_     )
        snake_case_    							=				model(lowerCAmelCase_     )

        # Output shape (b, c, h, w)
        self.parent.assertEqual(
            result.last_hidden_state.shape						,			(self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32)						,			)

  def 		_lowerCamelCase						(self	:			str						,			lowerCAmelCase_	:			List[str]						,			lowerCAmelCase_	:			Any     ) ->							List[Any]:
        snake_case_    							=				self.num_labels
        snake_case_    							=				FlaxResNetForImageClassification(config=lowerCAmelCase_     )
        snake_case_    							=				model(lowerCAmelCase_     )
        self.parent.assertEqual(result.logits.shape						,			(self.batch_size, self.num_labels)     )


  def 		_lowerCamelCase						(self	:			int     ) ->							List[str]:
        snake_case_    							=				self.prepare_config_and_inputs()
        snake_case_							,						snake_case_    							=				config_and_inputs
        snake_case_    							=				{"pixel_values": pixel_values}
        return config, inputs_dict



@require_flax
class 			lowerCAmelCase__ (							UpperCamelCase       ,				unittest.TestCase							):



  '''simple docstring'''






  _UpperCamelCase							=			(FlaxResNetModel, FlaxResNetForImageClassification) if is_flax_available() else ()

  _UpperCamelCase							=			False
  _UpperCamelCase							=			False
  _UpperCamelCase							=			False

  def 		_lowerCamelCase						(self	:			Optional[Any]     ) ->							None:
        snake_case_    							=				FlaxResNetModelTester(self     )
        snake_case_    							=				ConfigTester(self						,			config_class=lowerCAmelCase_						,			has_text_modality=lowerCAmelCase_     )

  def 		_lowerCamelCase						(self	:			Optional[Any]     ) ->							int:
        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 		_lowerCamelCase						(self	:			Dict     ) ->							Optional[int]:
        return

  def 		_lowerCamelCase						(self	:			List[Any]     ) ->							Dict:
        snake_case_    							=				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*lowerCAmelCase_     )

  def 		_lowerCamelCase						(self	:			List[Any]     ) ->							str:
        snake_case_    							=				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_classification(*lowerCAmelCase_     )

  @unittest.skip(reason="ResNet does not use inputs_embeds"     )
  def 		_lowerCamelCase						(self	:			int     ) ->							Optional[int]:
        pass

  @unittest.skip(reason="ResNet does not support input and output embeddings"     )
  def 		_lowerCamelCase						(self	:			str     ) ->							Any:
        pass

  def 		_lowerCamelCase						(self	:			Union[str, Any]     ) ->							Union[str, Any]:
        snake_case_							,						snake_case_    							=				self.model_tester.prepare_config_and_inputs_for_common()

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

              snake_case_    							=				["pixel_values"]
              self.assertListEqual(arg_names[:1]						,			lowerCAmelCase_     )

  def 		_lowerCamelCase						(self	:			Dict     ) ->							List[Any]:
        def check_hidden_states_output(lowerCAmelCase_	:			List[str]						,			lowerCAmelCase_	:			Optional[Any]						,			lowerCAmelCase_	:			Optional[int]     ):
              snake_case_    							=				model_class(lowerCAmelCase_     )

              snake_case_    							=				model(**self._prepare_for_class(lowerCAmelCase_						,			lowerCAmelCase_     )     )

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

              snake_case_    							=				self.model_tester.num_stages
              self.assertEqual(len(lowerCAmelCase_     )						,			expected_num_stages + 1     )

  @unittest.skip(reason="ResNet does not use feedforward chunking"     )
  def 		_lowerCamelCase						(self	:			Tuple     ) ->							Tuple:
        pass


  def 		_lowerCamelCase						(self	:			Any     ) ->							Optional[int]:
        snake_case_							,						snake_case_    							=				self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
              with self.subTest(model_class.__name__     ):
                    snake_case_    							=				self._prepare_for_class(lowerCAmelCase_						,			lowerCAmelCase_     )
                    snake_case_    							=				model_class(lowerCAmelCase_     )

                    @jax.jit
                    def model_jitted(lowerCAmelCase_	:			int						,			**lowerCAmelCase_	:			Any     ):
                          return model(pixel_values=lowerCAmelCase_						,			**lowerCAmelCase_     )

                    with self.subTest("JIT Enabled"     ):
                          snake_case_    							=				model_jitted(**lowerCAmelCase_     ).to_tuple()

                    with self.subTest("JIT Disabled"     ):
                          with jax.disable_jit():
                                snake_case_    							=				model_jitted(**lowerCAmelCase_     ).to_tuple()

                    self.assertEqual(len(lowerCAmelCase_     )						,			len(lowerCAmelCase_     )     )
                    for jitted_output, output in zip(lowerCAmelCase_						,			lowerCAmelCase_     ):
                          self.assertEqual(jitted_output.shape						,			output.shape     )

def 	snake_case_							(				):
      snake_case_    							=				Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"      )
      return image




@require_flax
class 			lowerCAmelCase__ (							unittest.TestCase							):



  '''simple docstring'''






  @cached_property
  def 		_lowerCamelCase						(self	:			int     ) ->							str:
        return AutoImageProcessor.from_pretrained("microsoft/resnet-50"     ) if is_vision_available() else None


  @slow
  def 		_lowerCamelCase						(self	:			List[Any]     ) ->							Union[str, Any]:
        snake_case_    							=				FlaxResNetForImageClassification.from_pretrained("microsoft/resnet-50"     )

        snake_case_    							=				self.default_image_processor
        snake_case_    							=				prepare_img()
        snake_case_    							=				image_processor(images=lowerCAmelCase_						,			return_tensors="np"     )

        snake_case_    							=				model(**lowerCAmelCase_     )

        # verify the logits
        snake_case_    							=				(1, 10_00)
        self.assertEqual(outputs.logits.shape						,			lowerCAmelCase_     )

        snake_case_    							=				jnp.array([-11.1_069, -9.7_877, -8.3_777]     )

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







'''simple docstring'''






from manim import *

class 	SCREAMING_SNAKE_CASE_		(							lowerCamelCase_					):



 def  SCREAMING_SNAKE_CASE(	self			):






        '''simple docstring'''




        lowercase	:		str    =   Rectangle(height=0.5 ,		width=0.5			)
        lowercase	:		Optional[Any]    =   Rectangle(height=0.4_6 ,		width=0.4_6			).set_stroke(width=0			)
        lowercase	:		str    =   Rectangle(height=0.2_5 ,		width=0.2_5			)

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

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

        lowercase	:		str    =   [mem.copy() for i in range(6			)]
        lowercase	:		int    =   VGroup(*A			).arrange(A ,		buff=0			)

        lowercase	:		int    =   Text('''Model''' ,		font_size=24			)
        lowercase	:		Tuple    =   Group(A ,		A			).arrange(A ,		buff=0.5 ,		aligned_edge=A			)
        model.move_to([3, -1.0, 0]			)
        self.add(A			)

        lowercase	:		str    =   []
        lowercase	:		int    =   []

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

               lowercase	:		List[Any]    =   Rectangle(height=0.4_6 / 4 ,		width=0.4_6 / 3			).set_stroke(width=0.0			).set_fill(A ,		opacity=0.7			)

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

        self.add(*A ,		*A			)

        lowercase	:		Optional[int]    =   [meta_mem.copy() for i in range(6			)]
        lowercase	:		List[str]    =   [meta_mem.copy() for i in range(6			)]
        lowercase	:		int    =   VGroup(*A			).arrange(A ,		buff=0			)
        lowercase	:		int    =   VGroup(*A			).arrange(A ,		buff=0			)
        lowercase	:		Any    =   VGroup(A ,		A			).arrange(A ,		buff=0			)
        lowercase	:		Union[str, Any]    =   Text('''Disk''' ,		font_size=24			)
        lowercase	:		Dict    =   Group(A ,		A			).arrange(A ,		buff=0.5 ,		aligned_edge=A			)
        disk.move_to([-4.0, -1.2_5, 0]			)
        self.add(A ,		A			)

        lowercase	:		List[str]    =   []

        for i in range(6			):
               lowercase	:		Tuple    =   fill.copy().set_fill(A ,		opacity=0.8			)
               target.move_to(disk_left_col_base[i]			).scale(0.5			)
               cpu_disk_arr.append(A			)

        self.add(*A			)

        lowercase	:		Any    =   Square(side_length=2.2			)
        key.move_to([-5, 2, 0]			)

        lowercase	:		Tuple    =   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(A ,		A			)

        lowercase	:		List[Any]    =   MarkupText(
            f"""<span fgcolor='{BLUE}'>●</span> Checkpoint""" ,		font_size=18 ,		)

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

        lowercase	:		Optional[Any]    =   MarkupText(f"""The offloaded weights are all sent to the CPU.""" ,		font_size=24			)
        step_a.move_to([2, 2, 0]			)

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

        for i in range(6			):
               lowercase	:		List[str]    =   cpu_disk_arr[i]
               lowercase	:		Optional[Any]    =   rect.copy().set_fill(A ,		opacity=0.8			).scale(2.0			)
               cpa.generate_target()
               cpa.target.move_to(model_base[i]			)

               if i == 0:
                      rect.set_fill(A ,		opacity=0.8			)
                      rect.generate_target()
                      rect.target.move_to(cpu_left_col_base[0]			).scale(2.0			)

                      self.remove(
                          *A ,		*A ,		)

               else:
                      rect.generate_target()
                      rect.target.move_to(cpu_left_col_base[i]			).scale(2.0			)
               self.play(MoveToTarget(A			) ,		MoveToTarget(A			) ,		model_base[i].animate.set_stroke(A			)			)
        self.play(FadeOut(A			)			)

        lowercase	:		Optional[Any]    =   MarkupText(
            f"""Finally, hooks are added to each weight in the model\nto transfer the weights from CPU to GPU\n\t\tand back when needed.""" ,		font_size=24 ,		)
        step_a.move_to([2, 2, 0]			)

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

        lowercase	:		Tuple    =   []
        lowercase	:		List[Any]    =   []
        for i in range(6			):
               lowercase	:		List[str]    =   Arrow(start=A ,		end=A ,		color=A ,		buff=0.5			)
               a.next_to(model_base[i].get_left() ,		A ,		buff=0.2			)
               arrows.append(A			)
               animations.append(Write(A			)			)
        self.play(*A			)
        self.wait()




