




"""simple docstring"""

import os
import random
from pathlib import Path
from typing import List

import numpy as np
import torch
from torch.cuda.amp import GradScaler

from .utils import (
    MODEL_NAME,
    OPTIMIZER_NAME,
    RNG_STATE_NAME,
    SCALER_NAME,
    SCHEDULER_NAME,
    get_pretty_name,
    is_tpu_available,
    is_xpu_available,
    save,
)


if is_tpu_available(check_device=False):
		import torch_xla.core.xla_model as xm

from .logging import get_logger
from .state import PartialState


_lowerCamelCase					:     Optional[Any]				       =    get_logger(__name__)

def 						lowercase_ (						_UpperCAmelCase					, _UpperCAmelCase					, _UpperCAmelCase					, _UpperCAmelCase					, _UpperCAmelCase					, _UpperCAmelCase = None					, ):




	"""simple docstring"""






	for i, state in enumerate(_UpperCAmelCase		):
		A_					:			Optional[int]						=						f"""{MODEL_NAME}.bin""" if i == 0 else f"""{MODEL_NAME}_{i}.bin"""
		A_					:			Union[str, Any]						=						os.path.join(_UpperCAmelCase					, _UpperCAmelCase		)
		save(_UpperCAmelCase					, _UpperCAmelCase		)
		logger.info(f"""Model weights saved in {output_model_file}"""		)
	# Optimizer states
	for i, opt in enumerate(_UpperCAmelCase		):
		A_					:			Any						=						opt.state_dict()
		A_					:			int						=						f"""{OPTIMIZER_NAME}.bin""" if i == 0 else f"""{OPTIMIZER_NAME}_{i}.bin"""
		A_					:			Optional[int]						=						os.path.join(_UpperCAmelCase					, _UpperCAmelCase		)
		save(_UpperCAmelCase					, _UpperCAmelCase		)
		logger.info(f"""Optimizer state saved in {output_optimizer_file}"""		)
	# Scheduler states
	for i, scheduler in enumerate(_UpperCAmelCase		):
		A_					:			Dict						=						scheduler.state_dict()
		A_					:			Any						=						f"""{SCHEDULER_NAME}.bin""" if i == 0 else f"""{SCHEDULER_NAME}_{i}.bin"""
		A_					:			Union[str, Any]						=						os.path.join(_UpperCAmelCase					, _UpperCAmelCase		)
		save(_UpperCAmelCase					, _UpperCAmelCase		)
		logger.info(f"""Scheduler state saved in {output_scheduler_file}"""		)
	# GradScaler state
	if scaler is not None:
		A_					:			Dict						=						scaler.state_dict()
		A_					:			str						=						os.path.join(_UpperCAmelCase					, _UpperCAmelCase		)
		torch.save(_UpperCAmelCase					, _UpperCAmelCase		)
		logger.info(f"""Gradient scaler state saved in {output_scaler_file}"""		)
	# Random number generator states
	A_					:			int						=						{}
	A_					:			int						=						f"""{RNG_STATE_NAME}_{process_index}.pkl"""
	A_					:			int						=						random.getstate()
	A_					:			Dict						=						np.random.get_state()
	A_					:			Optional[Any]						=						torch.get_rng_state()
	if is_xpu_available():
		A_					:			Union[str, Any]						=						torch.xpu.get_rng_state_all()
	else:
		A_					:			str						=						torch.cuda.get_rng_state_all()
	if is_tpu_available():
		A_					:			Optional[Any]						=						xm.get_rng_state()
	A_					:			Any						=						os.path.join(_UpperCAmelCase					, _UpperCAmelCase		)
	torch.save(_UpperCAmelCase					, _UpperCAmelCase		)
	logger.info(f"""Random states saved in {output_states_file}"""		)
	return output_dir

def 						lowercase_ (						_UpperCAmelCase					, _UpperCAmelCase					, _UpperCAmelCase					, _UpperCAmelCase					, _UpperCAmelCase					, _UpperCAmelCase=None					, _UpperCAmelCase=None					, **_UpperCAmelCase					, ):




	"""simple docstring"""






	if map_location not in [None, "cpu", "on_device"]:
		raise TypeError(
		    '''Unsupported optimizer map location passed, please choose one of `None`, `\'cpu\'`, or `\'on_device\'`'''		)
	if map_location is None:
		A_					:			Optional[int]						=						'''cpu'''
	elif map_location == "on_device":
		A_					:			int						=						PartialState().device
	# Model states
	for i, model in enumerate(_UpperCAmelCase		):
		A_					:			Union[str, Any]						=						f"""{MODEL_NAME}.bin""" if i == 0 else f"""{MODEL_NAME}_{i}.bin"""
		A_					:			List[Any]						=						os.path.join(_UpperCAmelCase					, _UpperCAmelCase		)
		models[i].load_state_dict(torch.load(_UpperCAmelCase					, map_location=_UpperCAmelCase		)					, **_UpperCAmelCase		)
	logger.info('''All model weights loaded successfully'''		)

	# Optimizer states
	for i, opt in enumerate(_UpperCAmelCase		):
		A_					:			str						=						f"""{OPTIMIZER_NAME}.bin""" if i == 0 else f"""{OPTIMIZER_NAME}_{i}.bin"""
		A_					:			Any						=						os.path.join(_UpperCAmelCase					, _UpperCAmelCase		)
		A_					:			int						=						torch.load(_UpperCAmelCase					, map_location=_UpperCAmelCase		)
		optimizers[i].load_state_dict(_UpperCAmelCase		)
	logger.info('''All optimizer states loaded successfully'''		)

	# Scheduler states
	for i, scheduler in enumerate(_UpperCAmelCase		):
		A_					:			Union[str, Any]						=						f"""{SCHEDULER_NAME}.bin""" if i == 0 else f"""{SCHEDULER_NAME}_{i}.bin"""
		A_					:			Tuple						=						os.path.join(_UpperCAmelCase					, _UpperCAmelCase		)
		scheduler.load_state_dict(torch.load(_UpperCAmelCase		)		)
	logger.info('''All scheduler states loaded successfully'''		)

	# GradScaler state
	if scaler is not None:
		A_					:			Optional[int]						=						os.path.join(_UpperCAmelCase					, _UpperCAmelCase		)
		scaler.load_state_dict(torch.load(_UpperCAmelCase		)		)
		logger.info('''GradScaler state loaded successfully'''		)

	# Random states
	try:
		A_					:			List[str]						=						torch.load(os.path.join(_UpperCAmelCase					, f"""{RNG_STATE_NAME}_{process_index}.pkl"""		)		)
		random.setstate(states['''random_state''']		)
		np.random.set_state(states['''numpy_random_seed''']		)
		torch.set_rng_state(states['''torch_manual_seed''']		)
		if is_xpu_available():
			torch.xpu.set_rng_state_all(states['''torch_xpu_manual_seed''']		)
		else:
			torch.cuda.set_rng_state_all(states['''torch_cuda_manual_seed''']		)
		if is_tpu_available():
			xm.set_rng_state(states['''xm_seed''']		)
		logger.info('''All random states loaded successfully'''		)
	except Exception:
		logger.info('''Could not load random states'''		)

def 						lowercase_ (						_UpperCAmelCase					, _UpperCAmelCase					, _UpperCAmelCase = 0		):




	"""simple docstring"""






	A_					:			Optional[Any]						=						Path(_UpperCAmelCase		) / f"""custom_checkpoint_{index}.pkl"""
	logger.info(f"""Saving the state of {get_pretty_name(_UpperCAmelCase		)} to {save_location}"""		)
	torch.save(obj.state_dict()					, _UpperCAmelCase		)

def 						lowercase_ (						_UpperCAmelCase					, _UpperCAmelCase					, _UpperCAmelCase = 0		):




	"""simple docstring"""






	A_					:			List[str]						=						f"""{path}/custom_checkpoint_{index}.pkl"""
	logger.info(f"""Loading the state of {get_pretty_name(_UpperCAmelCase		)} from {load_location}"""		)
	obj.load_state_dict(torch.load(_UpperCAmelCase					, map_location='''cpu'''		)		)











"""simple docstring"""

import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers.testing_utils import require_vision
from transformers.utils import is_vision_available


if is_vision_available():
		from PIL import Image

		from transformers import AutoProcessor, BertTokenizer, CLIPImageProcessor, GitProcessor, PreTrainedTokenizerFast

@require_vision
class     lowercase (      unittest.TestCase):







				def    a_					(							self				:   Any    ):



					"""simple docstring"""

					A_					:			List[str]						=						tempfile.mkdtemp()

					A_					:			List[str]						=						CLIPImageProcessor()
					A_					:			Any						=						BertTokenizer.from_pretrained(
					    '''hf-internal-testing/tiny-random-BertModel''' ,       model_input_names=['''input_ids''', '''attention_mask''']    )

					A_					:			str						=						GitProcessor(_lowerCamelCase ,       _lowerCamelCase    )

					processor.save_pretrained(self.tmpdirname    )







				def    a_					(							self				:   Tuple ,       **_lowerCamelCase				:   List[str]    ):



					"""simple docstring"""

					return AutoProcessor.from_pretrained(self.tmpdirname ,       **_lowerCamelCase    ).tokenizer







				def    a_					(							self				:   List[Any] ,       **_lowerCamelCase				:   int    ):



					"""simple docstring"""

					return AutoProcessor.from_pretrained(self.tmpdirname ,       **_lowerCamelCase    ).image_processor







				def    a_					(							self				:   Optional[Any]    ):



					"""simple docstring"""

					shutil.rmtree(self.tmpdirname    )







				def    a_					(							self				:   Optional[Any]    ):



					"""simple docstring"""

					A_					:			List[str]						=						[np.random.randint(2_55 ,       size=(3, 30, 4_00) ,       dtype=np.uinta    )]

					A_					:			Any						=						[Image.fromarray(np.moveaxis(_lowerCamelCase ,       0 ,       -1    )    ) for x in image_inputs]

					return image_inputs







				def    a_					(							self				:   str    ):



					"""simple docstring"""

					A_					:			Union[str, Any]						=						GitProcessor(tokenizer=self.get_tokenizer() ,       image_processor=self.get_image_processor()    )
					processor.save_pretrained(self.tmpdirname    )

					A_					:			Union[str, Any]						=						self.get_tokenizer(bos_token='''(BOS)''' ,       eos_token='''(EOS)'''    )
					A_					:			List[Any]						=						self.get_image_processor(do_normalize=_lowerCamelCase ,       padding_value=1.0    )

					A_					:			List[str]						=						GitProcessor.from_pretrained(
					    self.tmpdirname ,       bos_token='''(BOS)''' ,       eos_token='''(EOS)''' ,       do_normalize=_lowerCamelCase ,       padding_value=1.0    )

					self.assertEqual(processor.tokenizer.get_vocab() ,       tokenizer_add_kwargs.get_vocab()    )
					self.assertIsInstance(processor.tokenizer ,       _lowerCamelCase    )

					self.assertEqual(processor.image_processor.to_json_string() ,       image_processor_add_kwargs.to_json_string()    )
					self.assertIsInstance(processor.image_processor ,       _lowerCamelCase    )







				def    a_					(							self				:   Optional[Any]    ):



					"""simple docstring"""

					A_					:			Tuple						=						self.get_image_processor()
					A_					:			Optional[Any]						=						self.get_tokenizer()

					A_					:			int						=						GitProcessor(tokenizer=_lowerCamelCase ,       image_processor=_lowerCamelCase    )

					A_					:			Tuple						=						self.prepare_image_inputs()

					A_					:			Any						=						image_processor(_lowerCamelCase ,       return_tensors='''np'''    )
					A_					:			Optional[Any]						=						processor(images=_lowerCamelCase ,       return_tensors='''np'''    )

					for key in input_feat_extract.keys():
						self.assertAlmostEqual(input_feat_extract[key].sum() ,       input_processor[key].sum() ,       delta=1E-2    )







				def    a_					(							self				:   Optional[int]    ):



					"""simple docstring"""

					A_					:			Union[str, Any]						=						self.get_image_processor()
					A_					:			Union[str, Any]						=						self.get_tokenizer()

					A_					:			int						=						GitProcessor(tokenizer=_lowerCamelCase ,       image_processor=_lowerCamelCase    )

					A_					:			List[str]						=						'''lower newer'''

					A_					:			Tuple						=						processor(text=_lowerCamelCase    )

					A_					:			List[str]						=						tokenizer(_lowerCamelCase ,       return_token_type_ids=_lowerCamelCase    )

					for key in encoded_tok.keys():
						self.assertListEqual(encoded_tok[key] ,       encoded_processor[key]    )







				def    a_					(							self				:   Tuple    ):



					"""simple docstring"""

					A_					:			Optional[int]						=						self.get_image_processor()
					A_					:			Optional[Any]						=						self.get_tokenizer()

					A_					:			Optional[Any]						=						GitProcessor(tokenizer=_lowerCamelCase ,       image_processor=_lowerCamelCase    )

					A_					:			Tuple						=						'''lower newer'''
					A_					:			str						=						self.prepare_image_inputs()

					A_					:			Optional[int]						=						processor(text=_lowerCamelCase ,       images=_lowerCamelCase    )

					self.assertListEqual(list(inputs.keys()    ) ,       ['''input_ids''', '''attention_mask''', '''pixel_values''']    )

					# test if it raises when no input is passed
					with pytest.raises(_lowerCamelCase    ):
						processor()







				def    a_					(							self				:   Optional[int]    ):



					"""simple docstring"""

					A_					:			Optional[int]						=						self.get_image_processor()
					A_					:			List[Any]						=						self.get_tokenizer()

					A_					:			Optional[Any]						=						GitProcessor(tokenizer=_lowerCamelCase ,       image_processor=_lowerCamelCase    )

					A_					:			Dict						=						[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

					A_					:			str						=						processor.batch_decode(_lowerCamelCase    )
					A_					:			int						=						tokenizer.batch_decode(_lowerCamelCase    )

					self.assertListEqual(_lowerCamelCase ,       _lowerCamelCase    )







				def    a_					(							self				:   Any    ):



					"""simple docstring"""

					A_					:			Dict						=						self.get_image_processor()
					A_					:			List[str]						=						self.get_tokenizer()

					A_					:			Any						=						GitProcessor(tokenizer=_lowerCamelCase ,       image_processor=_lowerCamelCase    )

					A_					:			int						=						'''lower newer'''
					A_					:			Union[str, Any]						=						self.prepare_image_inputs()

					A_					:			List[str]						=						processor(text=_lowerCamelCase ,       images=_lowerCamelCase    )

					# For now the processor supports only ['input_ids', 'attention_mask', 'pixel_values']
					self.assertListEqual(list(inputs.keys()    ) ,       ['''input_ids''', '''attention_mask''', '''pixel_values''']    )





