


from manim import *


class   a__	(						lowerCAmelCase_    ):

       def 						UpperCAmelCase(   self      :     str    ):
            a_    :     Tuple										=    Rectangle(height=0.5      ,							width=0.5    )
            a_    :     Any										=    Rectangle(height=0.2_5      ,							width=0.2_5    )
            a_    :     Optional[Any]										=    Rectangle(height=0.4_6      ,							width=0.4_6    ).set_stroke(width=0    )

            a_    :     Optional[int]										=    [mem.copy() for i in range(6    )]
            a_    :     Union[str, Any]										=    [mem.copy() for i in range(6    )]
            a_    :     int										=    VGroup(*lowerCamelCase_    ).arrange(lowerCamelCase_      ,							buff=0    )
            a_    :     str										=    VGroup(*lowerCamelCase_    ).arrange(lowerCamelCase_      ,							buff=0    )
            a_    :     Any										=    VGroup(lowerCamelCase_      ,							lowerCamelCase_    ).arrange(lowerCamelCase_      ,							buff=0    )
            a_    :     Dict										=    Text("""CPU"""      ,							font_size=2_4    )
            a_    :     Union[str, Any]										=    Group(lowerCamelCase_      ,							lowerCamelCase_    ).arrange(lowerCamelCase_      ,							buff=0.5      ,							aligned_edge=lowerCamelCase_    )
            cpu.move_to([-2.5, -0.5, 0]    )
            self.add(lowerCamelCase_    )

            a_    :     str										=    [mem.copy() for i in range(4    )]
            a_    :     Union[str, Any]										=    VGroup(*lowerCamelCase_    ).arrange(lowerCamelCase_      ,							buff=0    )
            a_    :     Tuple										=    Text("""GPU"""      ,							font_size=2_4    )
            a_    :     Optional[Any]										=    Group(lowerCamelCase_      ,							lowerCamelCase_    ).arrange(lowerCamelCase_      ,							buff=0.5      ,							aligned_edge=lowerCamelCase_    )
            gpu.move_to([-1, -1, 0]    )
            self.add(lowerCamelCase_    )

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

            a_    :     str										=    Text("""Model"""      ,							font_size=2_4    )
            a_    :     Union[str, Any]										=    Group(lowerCamelCase_      ,							lowerCamelCase_    ).arrange(lowerCamelCase_      ,							buff=0.5      ,							aligned_edge=lowerCamelCase_    )
            model.move_to([3, -1.0, 0]    )
            self.add(lowerCamelCase_    )

            a_    :     Tuple										=    []
            a_    :     List[Any]										=    []
            a_    :     Optional[int]										=    []

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

                 a_    :     str										=    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_    )

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

            a_    :     Tuple										=    Text("""Loaded Checkpoint"""      ,							font_size=2_4    )
            a_    :     str										=    Group(lowerCamelCase_      ,							lowerCamelCase_    ).arrange(lowerCamelCase_      ,							buff=0.5      ,							aligned_edge=lowerCamelCase_    )
            checkpoint.move_to([3, 0.5, 0]    )

            self.add(lowerCamelCase_    )

            a_    :     int										=    []
            a_    :     Optional[int]										=    []

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

                 a_    :     Tuple										=    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_    )

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

            a_    :     Union[str, Any]										=    MarkupText(
                F'''<b>Key:</b>\n\n<span fgcolor=\'{YELLOW}\'>●</span> Empty Model'''      ,							font_size=1_8      ,							)

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

            self.add(lowerCamelCase_      ,							lowerCamelCase_    )

            a_    :     Dict										=    MarkupText(
                F'''<span fgcolor=\'{BLUE}\'>●</span> Checkpoint'''      ,							font_size=1_8      ,							)

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

            a_    :     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=2_4      ,							)
            step_a.move_to([2, 2, 0]    )

            a_    :     Optional[int]										=    [meta_mem.copy() for i in range(6    )]
            a_    :     Any										=    [meta_mem.copy() for i in range(6    )]
            a_    :     int										=    VGroup(*lowerCamelCase_    ).arrange(lowerCamelCase_      ,							buff=0    )
            a_    :     Dict										=    VGroup(*lowerCamelCase_    ).arrange(lowerCamelCase_      ,							buff=0    )
            a_    :     int										=    VGroup(lowerCamelCase_      ,							lowerCamelCase_    ).arrange(lowerCamelCase_      ,							buff=0    )
            a_    :     Any										=    Text("""Disk"""      ,							font_size=2_4    )
            a_    :     Optional[Any]										=    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    )    )

            a_    :     Union[str, Any]										=    []
            for i, rect in enumerate(lowerCamelCase_    ):
                 a_    :     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_    )    )

            a_    :     Optional[Any]										=    MarkupText(F'''Then, the checkpoint is removed from memory\nthrough garbage collection.'''      ,							font_size=2_4    )
            step_a.move_to([2, 2, 0]    )

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

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

            self.wait()







import json
import sys
import tempfile
import unittest
from pathlib import Path

import transformers
from transformers import (
    CONFIG_MAPPING,
    IMAGE_PROCESSOR_MAPPING,
    AutoConfig,
    AutoImageProcessor,
    CLIPConfig,
    CLIPImageProcessor,
)
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER


sys.path.append(str(Path(__file__).parent.parent.parent.parent / '''utils'''))

from test_module.custom_configuration import CustomConfig  # noqa E402
from test_module.custom_image_processing import CustomImageProcessor  # noqa E402


class   a__	(						unittest.TestCase    ):

       def 						UpperCAmelCase(   self      :     int    ):
            a_    :     Any										=    0

       def 						UpperCAmelCase(   self      :     Dict    ):
            a_    :     List[str]										=    AutoImageProcessor.from_pretrained("""openai/clip-vit-base-patch32"""    )
            self.assertIsInstance(lowerCamelCase_      ,							lowerCamelCase_    )

       def 						UpperCAmelCase(   self      :     int    ):
            with tempfile.TemporaryDirectory() as tmpdirname:
                 a_    :     Any										=    Path(lowerCamelCase_    ) / """preprocessor_config.json"""
                 a_    :     Tuple										=    Path(lowerCamelCase_    ) / """config.json"""
                 json.dump(
                     {"""image_processor_type""": """CLIPImageProcessor""", """processor_class""": """CLIPProcessor"""}      ,							open(lowerCamelCase_      ,							"""w"""    )      ,							)
                 json.dump({"""model_type""": """clip"""}      ,							open(lowerCamelCase_      ,							"""w"""    )    )

                 a_    :     Union[str, Any]										=    AutoImageProcessor.from_pretrained(lowerCamelCase_    )
                 self.assertIsInstance(lowerCamelCase_      ,							lowerCamelCase_    )

       def 						UpperCAmelCase(   self      :     List[Any]    ):
            # Ensure we can load the image processor from the feature extractor config
            with tempfile.TemporaryDirectory() as tmpdirname:
                 a_    :     Dict										=    Path(lowerCamelCase_    ) / """preprocessor_config.json"""
                 a_    :     List[str]										=    Path(lowerCamelCase_    ) / """config.json"""
                 json.dump(
                     {"""feature_extractor_type""": """CLIPFeatureExtractor""", """processor_class""": """CLIPProcessor"""}      ,							open(lowerCamelCase_      ,							"""w"""    )      ,							)
                 json.dump({"""model_type""": """clip"""}      ,							open(lowerCamelCase_      ,							"""w"""    )    )

                 a_    :     int										=    AutoImageProcessor.from_pretrained(lowerCamelCase_    )
                 self.assertIsInstance(lowerCamelCase_      ,							lowerCamelCase_    )

       def 						UpperCAmelCase(   self      :     Optional[Any]    ):
            with tempfile.TemporaryDirectory() as tmpdirname:
                 a_    :     int										=    CLIPConfig()

                 # Create a dummy config file with image_proceesor_type
                 a_    :     Tuple										=    Path(lowerCamelCase_    ) / """preprocessor_config.json"""
                 a_    :     Tuple										=    Path(lowerCamelCase_    ) / """config.json"""
                 json.dump(
                     {"""image_processor_type""": """CLIPImageProcessor""", """processor_class""": """CLIPProcessor"""}      ,							open(lowerCamelCase_      ,							"""w"""    )      ,							)
                 json.dump({"""model_type""": """clip"""}      ,							open(lowerCamelCase_      ,							"""w"""    )    )

                 # remove image_processor_type to make sure config.json alone is enough to load image processor locally
                 a_    :     Optional[int]										=    AutoImageProcessor.from_pretrained(lowerCamelCase_    ).to_dict()

                 config_dict.pop("""image_processor_type"""    )
                 a_    :     Tuple										=    CLIPImageProcessor(**lowerCamelCase_    )

                 # save in new folder
                 model_config.save_pretrained(lowerCamelCase_    )
                 config.save_pretrained(lowerCamelCase_    )

                 a_    :     Optional[int]										=    AutoImageProcessor.from_pretrained(lowerCamelCase_    )

                 # make sure private variable is not incorrectly saved
                 a_    :     str										=    json.loads(config.to_json_string()    )
                 self.assertTrue("""_processor_class""" not in dict_as_saved    )

            self.assertIsInstance(lowerCamelCase_      ,							lowerCamelCase_    )

       def 						UpperCAmelCase(   self      :     Dict    ):
            with tempfile.TemporaryDirectory() as tmpdirname:
                 a_    :     Union[str, Any]										=    Path(lowerCamelCase_    ) / """preprocessor_config.json"""
                 json.dump(
                     {"""image_processor_type""": """CLIPImageProcessor""", """processor_class""": """CLIPProcessor"""}      ,							open(lowerCamelCase_      ,							"""w"""    )      ,							)

                 a_    :     Optional[int]										=    AutoImageProcessor.from_pretrained(lowerCamelCase_    )
                 self.assertIsInstance(lowerCamelCase_      ,							lowerCamelCase_    )

       def 						UpperCAmelCase(   self      :     int    ):
            with self.assertRaisesRegex(
                lowerCamelCase_      ,							"""clip-base is not a local folder and is not a valid model identifier"""    ):
                 a_    :     Any										=    AutoImageProcessor.from_pretrained("""clip-base"""    )

       def 						UpperCAmelCase(   self      :     Optional[Any]    ):
            with self.assertRaisesRegex(
                lowerCamelCase_      ,							R"""aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)"""    ):
                 a_    :     Dict										=    AutoImageProcessor.from_pretrained(lowerCamelCase_      ,							revision="""aaaaaa"""    )

       def 						UpperCAmelCase(   self      :     Dict    ):
            with self.assertRaisesRegex(
                lowerCamelCase_      ,							"""hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json."""      ,							):
                 a_    :     Any										=    AutoImageProcessor.from_pretrained("""hf-internal-testing/config-no-model"""    )

       def 						UpperCAmelCase(   self      :     str    ):
            # If remote code is not set, we will time out when asking whether to load the model.
            with self.assertRaises(lowerCamelCase_    ):
                 a_    :     int										=    AutoImageProcessor.from_pretrained("""hf-internal-testing/test_dynamic_image_processor"""    )
            # If remote code is disabled, we can't load this config.
            with self.assertRaises(lowerCamelCase_    ):
                 a_    :     Tuple										=    AutoImageProcessor.from_pretrained(
                     """hf-internal-testing/test_dynamic_image_processor"""      ,							trust_remote_code=lowerCamelCase_    )

            a_    :     int										=    AutoImageProcessor.from_pretrained(
                """hf-internal-testing/test_dynamic_image_processor"""      ,							trust_remote_code=lowerCamelCase_    )
            self.assertEqual(image_processor.__class__.__name__      ,							"""NewImageProcessor"""    )

            # Test image processor can be reloaded.
            with tempfile.TemporaryDirectory() as tmp_dir:
                 image_processor.save_pretrained(lowerCamelCase_    )
                 a_    :     int										=    AutoImageProcessor.from_pretrained(lowerCamelCase_      ,							trust_remote_code=lowerCamelCase_    )
            self.assertEqual(reloaded_image_processor.__class__.__name__      ,							"""NewImageProcessor"""    )

       def 						UpperCAmelCase(   self      :     List[str]    ):
            try:
                 AutoConfig.register("""custom"""      ,							lowerCamelCase_    )
                 AutoImageProcessor.register(lowerCamelCase_      ,							lowerCamelCase_    )
                 # Trying to register something existing in the Transformers library will raise an error
                 with self.assertRaises(lowerCamelCase_    ):
                      AutoImageProcessor.register(lowerCamelCase_      ,							lowerCamelCase_    )

                 with tempfile.TemporaryDirectory() as tmpdirname:
                      a_    :     Dict										=    Path(lowerCamelCase_    ) / """preprocessor_config.json"""
                      a_    :     Union[str, Any]										=    Path(lowerCamelCase_    ) / """config.json"""
                      json.dump(
                          {"""feature_extractor_type""": """CLIPFeatureExtractor""", """processor_class""": """CLIPProcessor"""}      ,							open(lowerCamelCase_      ,							"""w"""    )      ,							)
                      json.dump({"""model_type""": """clip"""}      ,							open(lowerCamelCase_      ,							"""w"""    )    )

                      a_    :     str										=    CustomImageProcessor.from_pretrained(lowerCamelCase_    )

                 # Now that the config is registered, it can be used as any other config with the auto-API
                 with tempfile.TemporaryDirectory() as tmp_dir:
                      image_processor.save_pretrained(lowerCamelCase_    )
                      a_    :     Any										=    AutoImageProcessor.from_pretrained(lowerCamelCase_    )
                      self.assertIsInstance(lowerCamelCase_      ,							lowerCamelCase_    )

            finally:
                 if "custom" in CONFIG_MAPPING._extra_content:
                      del CONFIG_MAPPING._extra_content["custom"]
                 if CustomConfig in IMAGE_PROCESSOR_MAPPING._extra_content:
                      del IMAGE_PROCESSOR_MAPPING._extra_content[CustomConfig]

       def 						UpperCAmelCase(   self      :     Union[str, Any]    ):


            class   a__	(						lowerCAmelCase_    ):
                   lowerCamelCase__:     List[Any]							    =     True

            try:
                 AutoConfig.register("""custom"""      ,							lowerCamelCase_    )
                 AutoImageProcessor.register(lowerCamelCase_      ,							lowerCamelCase_    )
                 # If remote code is not set, the default is to use local
                 a_    :     Dict										=    AutoImageProcessor.from_pretrained("""hf-internal-testing/test_dynamic_image_processor"""    )
                 self.assertEqual(image_processor.__class__.__name__      ,							"""NewImageProcessor"""    )
                 self.assertTrue(image_processor.is_local    )

                 # If remote code is disabled, we load the local one.
                 a_    :     Optional[Any]										=    AutoImageProcessor.from_pretrained(
                     """hf-internal-testing/test_dynamic_image_processor"""      ,							trust_remote_code=lowerCamelCase_    )
                 self.assertEqual(image_processor.__class__.__name__      ,							"""NewImageProcessor"""    )
                 self.assertTrue(image_processor.is_local    )

                 # If remote is enabled, we load from the Hub
                 a_    :     Dict										=    AutoImageProcessor.from_pretrained(
                     """hf-internal-testing/test_dynamic_image_processor"""      ,							trust_remote_code=lowerCamelCase_    )
                 self.assertEqual(image_processor.__class__.__name__      ,							"""NewImageProcessor"""    )
                 self.assertTrue(not hasattr(lowerCamelCase_      ,							"""is_local"""    )    )

            finally:
                 if "custom" in CONFIG_MAPPING._extra_content:
                      del CONFIG_MAPPING._extra_content["custom"]
                 if CustomConfig in IMAGE_PROCESSOR_MAPPING._extra_content:
                      del IMAGE_PROCESSOR_MAPPING._extra_content[CustomConfig]



