





import baseaa
import io
import json
import os
from copy import deepcopy

from ..optimizer import AcceleratedOptimizer
from ..scheduler import AcceleratedScheduler

class 		snake_case_   :







	'''simple docstring'''




	def __init__(						self							:     Any					,      __lowerCamelCase							:     int   )       -> Dict:

			'''simple docstring'''




			if isinstance(__lowerCamelCase					,      __lowerCamelCase   ):
					# Don't modify user's data should they want to reuse it (e.g. in tests), because once we
					# modified it, it will not be accepted here again, since `auto` values would have been overridden
					__lowercase											=     deepcopy(__lowerCamelCase   )
			elif os.path.exists(__lowerCamelCase   ):
					with io.open(__lowerCamelCase					,      'r'					,      encoding='utf-8'   ) as f:
							__lowercase											=     json.load(__lowerCamelCase   )
			else:
					try:
							__lowercase											=     baseaa.urlsafe_baadecode(__lowerCamelCase   ).decode('utf-8'   )
							__lowercase											=     json.loads(__lowerCamelCase   )
					except (UnicodeDecodeError, AttributeError, ValueError):
							raise ValueError(
							    F"Expected a string path to an existing deepspeed config, or a dictionary, or a base64 encoded string. Received: {config_file_or_dict}"   )

			__lowercase											=     config

			self.set_stage_and_offload()




	def    UpperCAmelCase			(						self							:     List[str]   )       -> List[Any]:

			'''simple docstring'''




			__lowercase											=     self.get_value('zero_optimization.stage'					,      -1   )

			# offload
			__lowercase											=     False
			if self.is_zeroa() or self.is_zeroa():
					__lowercase											=     set(['cpu', 'nvme']   )
					__lowercase											=     set(
					    [
					        self.get_value('zero_optimization.offload_optimizer.device'   ),
					        self.get_value('zero_optimization.offload_param.device'   ),
					    ]   )
					if len(offload_devices & offload_devices_valid   ) > 0:
							__lowercase											=     True




	def    UpperCAmelCase			(						self							:     Tuple					,      __lowerCamelCase							:     List[str]   )       -> Union[str, Any]:

			'''simple docstring'''




			__lowercase											=     self.config

			# find the config node of interest if it exists
			__lowercase											=     ds_key_long.split('.'   )
			__lowercase											=     nodes.pop()
			for node in nodes:
					__lowercase											=     config.get(__lowerCamelCase   )
					if config is None:
							return None, ds_key

			return config, ds_key




	def    UpperCAmelCase			(						self							:     Dict					,      __lowerCamelCase							:     List[Any]					,      __lowerCamelCase							:     Dict=None   )       -> Tuple:

			'''simple docstring'''




			__lowercase						,      __lowercase											=     self.find_config_node(__lowerCamelCase   )
			if config is None:
					return default
			return config.get(__lowerCamelCase					,      __lowerCamelCase   )




	def    UpperCAmelCase			(						self							:     Tuple					,      __lowerCamelCase							:     Any					,      __lowerCamelCase							:     Union[str, Any]=False   )       -> Union[str, Any]:

			'''simple docstring'''




			__lowercase											=     self.config

			# find the config node of interest if it exists
			__lowercase											=     ds_key_long.split('.'   )
			for node in nodes:
					__lowercase											=     config
					__lowercase											=     config.get(__lowerCamelCase   )
					if config is None:
							if must_exist:
									raise ValueError(F"Can't find {ds_key_long} entry in the config: {self.config}"   )
							else:
									return

        # if found remove it
			if parent_config is not None:
					parent_config.pop(__lowerCamelCase   )




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

			'''simple docstring'''




			__lowercase											=     self.get_value(__lowerCamelCase   )
			return False if value is None else bool(__lowerCamelCase   )




	def    UpperCAmelCase			(						self							:     Optional[int]					,      __lowerCamelCase							:     Optional[int]   )       -> Union[str, Any]:

			'''simple docstring'''




			__lowercase											=     self.get_value(__lowerCamelCase   )
			return False if value is None else not bool(__lowerCamelCase   )




	def    UpperCAmelCase			(						self							:     Optional[Any]   )       -> List[Any]:

			'''simple docstring'''




			return self._stage == 2




	def    UpperCAmelCase			(						self							:     Optional[Any]   )       -> str:

			'''simple docstring'''




			return self._stage == 3






	def    UpperCAmelCase			(						self							:     str   )       -> Any:

			'''simple docstring'''




			return self._offload





class 		snake_case_   :







	'''simple docstring'''




	def __init__(						self							:     Union[str, Any]					,      __lowerCamelCase							:     Tuple   )       -> int:

			'''simple docstring'''




			__lowercase											=     engine






	def    UpperCAmelCase			(						self							:     Union[str, Any]					,      __lowerCamelCase							:     List[str]					,      **__lowerCamelCase							:     Optional[int]   )       -> List[Any]:

			'''simple docstring'''




			self.engine.backward(__lowerCamelCase					,      **__lowerCamelCase   )

			# Deepspeed's `engine.step` performs the following operations:
			# - gradient accumulation check
			# - gradient clipping
			# - optimizer step
			# - zero grad
			# - checking overflow
			# - lr_scheduler step (only if engine.lr_scheduler is not None)
			self.engine.step()
			# and this plugin overrides the above calls with no-ops when Accelerate runs under
			# Deepspeed, but allows normal functionality for non-Deepspeed cases thus enabling a simple
			# training loop that works transparently under many training regimes.





class 		snake_case_   (							UpperCAmelCase_						):







	'''simple docstring'''




	def __init__(						self							:     List[Any]					,      __lowerCamelCase							:     int   )       -> Dict:

			'''simple docstring'''




			super().__init__(__lowerCamelCase					,      device_placement=__lowerCamelCase					,      scaler=__lowerCamelCase   )
			__lowercase											=     hasattr(self.optimizer					,      'overflow'   )




	def    UpperCAmelCase			(						self							:     List[Any]					,      __lowerCamelCase							:     int=None   )       -> Any:

			'''simple docstring'''




			pass  # `accelerator.backward(loss)` is doing that automatically. Therefore, its implementation is not needed




	def    UpperCAmelCase			(						self							:     Optional[int]   )       -> int:

			'''simple docstring'''




			pass  # `accelerator.backward(loss)` is doing that automatically. Therefore, its implementation is not needed






	@property
	def    UpperCAmelCase			(						self							:     Any   )       -> Dict:

			'''simple docstring'''




			if self.__has_overflow__:
					return self.optimizer.overflow
			return False





class 		snake_case_   (							UpperCAmelCase_						):







	'''simple docstring'''




	def __init__(						self							:     List[str]					,      __lowerCamelCase							:     List[str]					,      __lowerCamelCase							:     int   )       -> str:

			'''simple docstring'''




			super().__init__(__lowerCamelCase					,      __lowerCamelCase   )






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

			'''simple docstring'''




			pass  # `accelerator.backward(loss)` is doing that automatically. Therefore, its implementation is not needed





class 		snake_case_   :







	'''simple docstring'''




	def __init__(						self							:     int					,      __lowerCamelCase							:     Any					,      __lowerCamelCase							:     Tuple=0.001					,      __lowerCamelCase							:     Union[str, Any]=0					,      **__lowerCamelCase							:     str   )       -> Tuple:

			'''simple docstring'''




			__lowercase											=     params
			__lowercase											=     lr
			__lowercase											=     weight_decay
			__lowercase											=     kwargs





class 		snake_case_   :







	'''simple docstring'''




	def __init__(						self							:     int					,      __lowerCamelCase							:     Optional[Any]					,      __lowerCamelCase							:     Tuple=None					,      __lowerCamelCase							:     Tuple=0					,      **__lowerCamelCase							:     List[str]   )       -> Any:

			'''simple docstring'''




			__lowercase											=     optimizer
			__lowercase											=     total_num_steps
			__lowercase											=     warmup_num_steps
			__lowercase											=     kwargs











from typing import Union

from ..utils import add_end_docstrings, is_torch_available, is_vision_available, logging
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_vision_available():
		from PIL import Image

		from ..image_utils import load_image

if is_torch_available():
		from ..models.auto.modeling_auto import MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING

SCREAMING_SNAKE_CASE_   :      Union[str, Any]			=   logging.get_logger(__name__)

@add_end_docstrings(UpperCAmelCase_						)
class 		snake_case_   (							UpperCAmelCase_						):







	'''simple docstring'''




	def __init__(						self							:     Optional[int]					,      *__lowerCamelCase							:     Optional[int]					,      **__lowerCamelCase							:     List[Any]   )       -> Optional[Any]:

			'''simple docstring'''




			super().__init__(*__lowerCamelCase					,      **__lowerCamelCase   )
			self.check_model_type(__lowerCamelCase   )




	def    UpperCAmelCase			(						self							:     Optional[int]					,      __lowerCamelCase							:     int=None					,      __lowerCamelCase							:     List[Any]=None					,      __lowerCamelCase							:     Any=None					,      **__lowerCamelCase							:     Optional[int]   )       -> Any:

			'''simple docstring'''




			__lowercase						,      __lowercase											=     {}, {}
			if padding is not None:
					__lowercase											=     padding
			if truncation is not None:
					__lowercase											=     truncation
			if top_k is not None:
					__lowercase											=     top_k
			return preprocess_params, {}, postprocess_params




	def __call__(						self							:     str					,      __lowerCamelCase							:     Union["Image.Image", str]					,      __lowerCamelCase							:     str = None					,      **__lowerCamelCase							:     Dict   )       -> Tuple:

			'''simple docstring'''




			if isinstance(__lowerCamelCase					,      (Image.Image, str)   ) and isinstance(__lowerCamelCase					,      __lowerCamelCase   ):
					__lowercase											=     {'image': image, 'question': question}
			else:
					__lowercase											=     image
			__lowercase											=     super().__call__(__lowerCamelCase					,      **__lowerCamelCase   )
			return results




	def    UpperCAmelCase			(						self							:     List[Any]					,      __lowerCamelCase							:     List[str]					,      __lowerCamelCase							:     Optional[Any]=False					,      __lowerCamelCase							:     Optional[Any]=False   )       -> Optional[int]:

			'''simple docstring'''




			__lowercase											=     load_image(inputs['image']   )
			__lowercase											=     self.tokenizer(
			    inputs['question']					,      return_tensors=self.framework					,      padding=__lowerCamelCase					,      truncation=__lowerCamelCase   )
			__lowercase											=     self.image_processor(images=__lowerCamelCase					,      return_tensors=self.framework   )
			model_inputs.update(__lowerCamelCase   )
			return model_inputs




	def    UpperCAmelCase			(						self							:     str					,      __lowerCamelCase							:     Tuple   )       -> Any:

			'''simple docstring'''




			__lowercase											=     self.model(**__lowerCamelCase   )
			return model_outputs






	def    UpperCAmelCase			(						self							:     int					,      __lowerCamelCase							:     Any					,      __lowerCamelCase							:     int=5   )       -> Tuple:

			'''simple docstring'''




			if top_k > self.model.config.num_labels:
					__lowercase											=     self.model.config.num_labels

			if self.framework == "pt":
					__lowercase											=     model_outputs.logits.sigmoid()[0]
					__lowercase						,      __lowercase											=     probs.topk(__lowerCamelCase   )
			else:
					raise ValueError(F"Unsupported framework: {self.framework}"   )

			__lowercase											=     scores.tolist()
			__lowercase											=     ids.tolist()
			return [{"score": score, "answer": self.model.config.idalabel[_id]} for score, _id in zip(__lowerCamelCase					,      __lowerCamelCase   )]




