"""simple docstring"""



import os
import unittest
from tempfile import TemporaryDirectory

import torch
import torch.nn as nn

from accelerate.utils import (
    OffloadedWeightsLoader,
    extract_submodules_state_dict,
    load_offloaded_weight,
    offload_state_dict,
    offload_weight,
)
class 						__UpperCAmelCase    (nn.Module     ):







			'''simple docstring'''





			def __init__(			self							):







										'''simple docstring'''


										super().__init__()
										A__       :     int								=			nn.Linear(3   ,      4							)
										A__       :     Dict								=			nn.BatchNormad(4							)
										A__       :     Union[str, Any]								=			nn.Linear(4   ,      5							)





			def 		lowerCamelCase					(			self   ,      snake_case_							):







										'''simple docstring'''


										return self.lineara(self.batchnorm(self.lineara(snake_case_							)							)							)





class 						__UpperCAmelCase    (unittest.TestCase     ):







			'''simple docstring'''





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										A__       :     List[str]								=			ModelForTest()
										with TemporaryDirectory() as tmp_dir:
																	offload_state_dict(snake_case_   ,      model.state_dict()							)
																	A__       :     str								=			os.path.join(snake_case_   ,      """index.json"""							)
																	self.assertTrue(os.path.isfile(snake_case_							)							)
																	# TODO: add tests on what is inside the index

																	for key in ["linear1.weight", "linear1.bias", "linear2.weight", "linear2.bias"]:
																								A__       :     Optional[int]								=			os.path.join(snake_case_   ,      F'''{key}.dat'''							)
																								self.assertTrue(os.path.isfile(snake_case_							)							)
																								# TODO: add tests on the fact weights are properly loaded





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										A__       :     List[Any]								=			[torch.floataa, torch.floataa, torch.bfloataa]

										for dtype in dtypes:
																	A__       :     List[str]								=			torch.randn(2   ,      3   ,      dtype=snake_case_							)
																	with TemporaryDirectory() as tmp_dir:
																								A__       :     List[Any]								=			offload_weight(snake_case_   ,      """weight"""   ,      snake_case_   ,      {}							)
																								A__       :     Union[str, Any]								=			os.path.join(snake_case_   ,      """weight.dat"""							)
																								self.assertTrue(os.path.isfile(snake_case_							)							)
																								self.assertDictEqual(snake_case_   ,      {"""weight""": {"""shape""": [2, 3], """dtype""": str(snake_case_							).split("""."""							)[1]}}							)

																								A__       :     Tuple								=			load_offloaded_weight(snake_case_   ,      index["""weight"""]							)
																								self.assertTrue(torch.equal(snake_case_   ,      snake_case_							)							)





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										A__       :     Optional[Any]								=			ModelForTest()
										A__       :     str								=			model.state_dict()
										A__       :     Optional[int]								=			{k: v for k, v in state_dict.items() if """linear2""" not in k}
										A__       :     int								=			{k: v for k, v in state_dict.items() if """linear2""" in k}

										with TemporaryDirectory() as tmp_dir:
																	offload_state_dict(snake_case_   ,      snake_case_							)
																	A__       :     Union[str, Any]								=			OffloadedWeightsLoader(state_dict=snake_case_   ,      save_folder=snake_case_							)

																	# Every key is there with the right value
																	self.assertEqual(sorted(snake_case_							)   ,      sorted(state_dict.keys()							)							)
																	for key, param in state_dict.items():
																								self.assertTrue(torch.allclose(snake_case_   ,      weight_map[key]							)							)

										A__       :     List[str]								=			{k: v for k, v in state_dict.items() if """weight""" in k}
										A__       :     str								=			{k: v for k, v in state_dict.items() if """weight""" not in k}

										with TemporaryDirectory() as tmp_dir:
																	offload_state_dict(snake_case_   ,      snake_case_							)
																	A__       :     Optional[int]								=			OffloadedWeightsLoader(state_dict=snake_case_   ,      save_folder=snake_case_							)

																	# Every key is there with the right value
																	self.assertEqual(sorted(snake_case_							)   ,      sorted(state_dict.keys()							)							)
																	for key, param in state_dict.items():
																								self.assertTrue(torch.allclose(snake_case_   ,      weight_map[key]							)							)

										with TemporaryDirectory() as tmp_dir:
																	offload_state_dict(snake_case_   ,      snake_case_							)
																	# Duplicates are removed
																	A__       :     List[Any]								=			OffloadedWeightsLoader(state_dict=snake_case_   ,      save_folder=snake_case_							)

																	# Every key is there with the right value
																	self.assertEqual(sorted(snake_case_							)   ,      sorted(state_dict.keys()							)							)
																	for key, param in state_dict.items():
																								self.assertTrue(torch.allclose(snake_case_   ,      weight_map[key]							)							)





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										A__       :     List[Any]								=			{"""a.1""": 0, """a.10""": 1, """a.2""": 2}
										A__       :     Union[str, Any]								=			extract_submodules_state_dict(snake_case_   ,      ["""a.1""", """a.2"""]							)
										self.assertDictEqual(snake_case_   ,      {"""a.1""": 0, """a.2""": 2}							)

										A__       :     Optional[int]								=			{"""a.1.a""": 0, """a.10.a""": 1, """a.2.a""": 2}
										A__       :     int								=			extract_submodules_state_dict(snake_case_   ,      ["""a.1""", """a.2"""]							)
										self.assertDictEqual(snake_case_   ,      {"""a.1.a""": 0, """a.2.a""": 2}							)






"""simple docstring"""



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 						__UpperCAmelCase    (unittest.TestCase     ):







			'''simple docstring'''





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										A__       :     List[str]								=			0





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										A__       :     List[str]								=			AutoImageProcessor.from_pretrained("""openai/clip-vit-base-patch32"""							)
										self.assertIsInstance(snake_case_   ,      snake_case_							)





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										with tempfile.TemporaryDirectory() as tmpdirname:
																	A__       :     Optional[Any]								=			Path(snake_case_							) / """preprocessor_config.json"""
																	A__       :     Any								=			Path(snake_case_							) / """config.json"""
																	json.dump(
																	    {"""image_processor_type""": """CLIPImageProcessor""", """processor_class""": """CLIPProcessor"""}   ,      open(snake_case_   ,      """w"""							)   ,      )
																	json.dump({"""model_type""": """clip"""}   ,      open(snake_case_   ,      """w"""							)							)

																	A__       :     Any								=			AutoImageProcessor.from_pretrained(snake_case_							)
																	self.assertIsInstance(snake_case_   ,      snake_case_							)





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


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

																	A__       :     Optional[int]								=			AutoImageProcessor.from_pretrained(snake_case_							)
																	self.assertIsInstance(snake_case_   ,      snake_case_							)





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										with tempfile.TemporaryDirectory() as tmpdirname:
																	A__       :     Union[str, Any]								=			CLIPConfig()

																	# Create a dummy config file with image_proceesor_type
																	A__       :     Dict								=			Path(snake_case_							) / """preprocessor_config.json"""
																	A__       :     Optional[int]								=			Path(snake_case_							) / """config.json"""
																	json.dump(
																	    {"""image_processor_type""": """CLIPImageProcessor""", """processor_class""": """CLIPProcessor"""}   ,      open(snake_case_   ,      """w"""							)   ,      )
																	json.dump({"""model_type""": """clip"""}   ,      open(snake_case_   ,      """w"""							)							)

																	# remove image_processor_type to make sure config.json alone is enough to load image processor locally
																	A__       :     List[str]								=			AutoImageProcessor.from_pretrained(snake_case_							).to_dict()

																	config_dict.pop("""image_processor_type"""							)
																	A__       :     str								=			CLIPImageProcessor(**snake_case_							)

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

																	A__       :     Optional[int]								=			AutoImageProcessor.from_pretrained(snake_case_							)

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

										self.assertIsInstance(snake_case_   ,      snake_case_							)





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										with tempfile.TemporaryDirectory() as tmpdirname:
																	A__       :     List[Any]								=			Path(snake_case_							) / """preprocessor_config.json"""
																	json.dump(
																	    {"""image_processor_type""": """CLIPImageProcessor""", """processor_class""": """CLIPProcessor"""}   ,      open(snake_case_   ,      """w"""							)   ,      )

																	A__       :     List[str]								=			AutoImageProcessor.from_pretrained(snake_case_							)
																	self.assertIsInstance(snake_case_   ,      snake_case_							)





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										with self.assertRaisesRegex(
										    snake_case_   ,      """clip-base is not a local folder and is not a valid model identifier"""							):
																	A__       :     Optional[Any]								=			AutoImageProcessor.from_pretrained("""clip-base"""							)





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										with self.assertRaisesRegex(
										    snake_case_   ,      r"""aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)"""							):
																	A__       :     Any								=			AutoImageProcessor.from_pretrained(snake_case_   ,      revision="""aaaaaa"""							)





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										with self.assertRaisesRegex(
										    snake_case_   ,      """hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json."""   ,      ):
																	A__       :     Optional[Any]								=			AutoImageProcessor.from_pretrained("""hf-internal-testing/config-no-model"""							)





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										with self.assertRaises(snake_case_							):
																	A__       :     str								=			AutoImageProcessor.from_pretrained("""hf-internal-testing/test_dynamic_image_processor"""							)
										# If remote code is disabled, we can't load this config.
										with self.assertRaises(snake_case_							):
																	A__       :     int								=			AutoImageProcessor.from_pretrained(
																	    """hf-internal-testing/test_dynamic_image_processor"""   ,      trust_remote_code=snake_case_							)

										A__       :     Optional[int]								=			AutoImageProcessor.from_pretrained(
										    """hf-internal-testing/test_dynamic_image_processor"""   ,      trust_remote_code=snake_case_							)
										self.assertEqual(image_processor.__class__.__name__   ,      """NewImageProcessor"""							)

										# Test image processor can be reloaded.
										with tempfile.TemporaryDirectory() as tmp_dir:
																	image_processor.save_pretrained(snake_case_							)
																	A__       :     Optional[Any]								=			AutoImageProcessor.from_pretrained(snake_case_   ,      trust_remote_code=snake_case_							)
										self.assertEqual(reloaded_image_processor.__class__.__name__   ,      """NewImageProcessor"""							)





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										try:
																	AutoConfig.register("""custom"""   ,      snake_case_							)
																	AutoImageProcessor.register(snake_case_   ,      snake_case_							)
																	# Trying to register something existing in the Transformers library will raise an error
																	with self.assertRaises(snake_case_							):
																								AutoImageProcessor.register(snake_case_   ,      snake_case_							)

																	with tempfile.TemporaryDirectory() as tmpdirname:
																								A__       :     Tuple								=			Path(snake_case_							) / """preprocessor_config.json"""
																								A__       :     List[str]								=			Path(snake_case_							) / """config.json"""
																								json.dump(
																								    {"""feature_extractor_type""": """CLIPFeatureExtractor""", """processor_class""": """CLIPProcessor"""}   ,      open(snake_case_   ,      """w"""							)   ,      )
																								json.dump({"""model_type""": """clip"""}   ,      open(snake_case_   ,      """w"""							)							)

																								A__       :     Optional[int]								=			CustomImageProcessor.from_pretrained(snake_case_							)

																	# 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(snake_case_							)
																								A__       :     int								=			AutoImageProcessor.from_pretrained(snake_case_							)
																								self.assertIsInstance(snake_case_   ,      snake_case_							)

										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 		lowerCamelCase					(			self							):







										'''simple docstring'''


										class 						__UpperCAmelCase    (__A     ):







													'''simple docstring'''
													_UpperCamelCase  :						Union[str, Any]					     =				True

										try:
																	AutoConfig.register("""custom"""   ,      snake_case_							)
																	AutoImageProcessor.register(snake_case_   ,      snake_case_							)
																	# If remote code is not set, the default is to use local
																	A__       :     int								=			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__       :     Any								=			AutoImageProcessor.from_pretrained(
																	    """hf-internal-testing/test_dynamic_image_processor"""   ,      trust_remote_code=snake_case_							)
																	self.assertEqual(image_processor.__class__.__name__   ,      """NewImageProcessor"""							)
																	self.assertTrue(image_processor.is_local							)

																	# If remote is enabled, we load from the Hub
																	A__       :     Optional[Any]								=			AutoImageProcessor.from_pretrained(
																	    """hf-internal-testing/test_dynamic_image_processor"""   ,      trust_remote_code=snake_case_							)
																	self.assertEqual(image_processor.__class__.__name__   ,      """NewImageProcessor"""							)
																	self.assertTrue(not hasattr(snake_case_   ,      """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]





