





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   ,				A_     ):





        '''simple docstring'''

        if isinstance(A_   ,				A_     ):
            # 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
            SCREAMING_SNAKE_CASE__											=			deepcopy(A_     )
        elif os.path.exists(A_     ):
            with io.open(A_   ,				'''r'''   ,				encoding='''utf-8'''     ) as f:
                SCREAMING_SNAKE_CASE__											=			json.load(A_     )
        else:
            try:
                SCREAMING_SNAKE_CASE__											=			baseaa.urlsafe_baadecode(A_     ).decode('''utf-8'''     )
                SCREAMING_SNAKE_CASE__											=			json.loads(A_     )
            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}'''     )

        SCREAMING_SNAKE_CASE__											=			config

        self.set_stage_and_offload()



    def  lowercase_					(       self     ):





        '''simple docstring'''

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

        # offload
        SCREAMING_SNAKE_CASE__											=			False
        if self.is_zeroa() or self.is_zeroa():
            SCREAMING_SNAKE_CASE__											=			set(['''cpu''', '''nvme''']     )
            SCREAMING_SNAKE_CASE__											=			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:
                SCREAMING_SNAKE_CASE__											=			True



    def  lowercase_					(       self   ,				A_     ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__											=			self.config

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

        return config, ds_key



    def  lowercase_					(       self   ,				A_   ,				A_=None     ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__				,		SCREAMING_SNAKE_CASE__											=			self.find_config_node(A_     )
        if config is None:
            return default
        return config.get(A_   ,				A_     )



    def  lowercase_					(       self   ,				A_   ,				A_=False     ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__											=			self.config

        # find the config node of interest if it exists
        SCREAMING_SNAKE_CASE__											=			ds_key_long.split('''.'''     )
        for node in nodes:
            SCREAMING_SNAKE_CASE__											=			config
            SCREAMING_SNAKE_CASE__											=			config.get(A_     )
            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(A_     )



    def  lowercase_					(       self   ,				A_     ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__											=			self.get_value(A_     )
        return False if value is None else bool(A_     )



    def  lowercase_					(       self   ,				A_     ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__											=			self.get_value(A_     )
        return False if value is None else not bool(A_     )



    def  lowercase_					(       self     ):





        '''simple docstring'''

        return self._stage == 2



    def  lowercase_					(       self     ):





        '''simple docstring'''

        return self._stage == 3




    def  lowercase_					(       self     ):





        '''simple docstring'''

        return self._offload





class 					__snake_case	:


    '''simple docstring'''







    def __init__(       self   ,				A_     ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__											=			engine




    def  lowercase_					(       self   ,				A_   ,				**A_     ):





        '''simple docstring'''

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

        # 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	(  __SCREAMING_SNAKE_CASE    ):


    '''simple docstring'''







    def __init__(       self   ,				A_     ):





        '''simple docstring'''

        super().__init__(A_   ,				device_placement=A_   ,				scaler=A_     )
        SCREAMING_SNAKE_CASE__											=			hasattr(self.optimizer   ,				'''overflow'''     )



    def  lowercase_					(       self   ,				A_=None     ):





        '''simple docstring'''

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



    def  lowercase_					(       self     ):





        '''simple docstring'''

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




    @property
    def  lowercase_					(       self     ):





        '''simple docstring'''

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





class 					__snake_case	(  __SCREAMING_SNAKE_CASE    ):


    '''simple docstring'''







    def __init__(       self   ,				A_   ,				A_     ):





        '''simple docstring'''

        super().__init__(A_   ,				A_     )




    def  lowercase_					(       self     ):





        '''simple docstring'''

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





class 					__snake_case	:


    '''simple docstring'''







    def __init__(       self   ,				A_   ,				A_=0.001   ,				A_=0   ,				**A_     ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__											=			params
        SCREAMING_SNAKE_CASE__											=			lr
        SCREAMING_SNAKE_CASE__											=			weight_decay
        SCREAMING_SNAKE_CASE__											=			kwargs





class 					__snake_case	:


    '''simple docstring'''







    def __init__(       self   ,				A_   ,				A_=None   ,				A_=0   ,				**A_     ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__											=			optimizer
        SCREAMING_SNAKE_CASE__											=			total_num_steps
        SCREAMING_SNAKE_CASE__											=			warmup_num_steps
        SCREAMING_SNAKE_CASE__											=			kwargs






'''simple docstring'''



from collections import UserDict
from typing import List, Union

from ..utils import (
    add_end_docstrings,
    is_tf_available,
    is_torch_available,
    is_vision_available,
    logging,
    requires_backends,
)
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_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING

if is_tf_available():
     from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING
     from ..tf_utils import stable_softmax

UpperCAmelCase_							:	str	     =		logging.get_logger(__name__)





@add_end_docstrings(__lowerCAmelCase)
class      lowerCAmelCase (			__lowerCAmelCase):


       def __init__( self     ,	**__SCREAMING_SNAKE_CASE	)   ->	Optional[int]:



            '''simple docstring'''




            super().__init__(**__SCREAMING_SNAKE_CASE	)

            requires_backends(self     ,	'''vision'''	)
            self.check_model_type(
                TF_MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING
                if self.framework == '''tf'''
                else MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING	)


       def __call__( self     ,	__SCREAMING_SNAKE_CASE     ,	**__SCREAMING_SNAKE_CASE	)   ->	List[Any]:



            '''simple docstring'''




            return super().__call__(__SCREAMING_SNAKE_CASE     ,	**__SCREAMING_SNAKE_CASE	)


       def    lowerCAmelCase   ( self     ,	**__SCREAMING_SNAKE_CASE	)   ->	Tuple:



            '''simple docstring'''




            __snake_case            =	{}
            if "candidate_labels" in kwargs:
                 __snake_case            =	kwargs['''candidate_labels''']
            if "hypothesis_template" in kwargs:
                 __snake_case            =	kwargs['''hypothesis_template''']

            return preprocess_params, {}, {}


       def    lowerCAmelCase   ( self     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE=None     ,	__SCREAMING_SNAKE_CASE="This is a photo of {}."	)   ->	Optional[Any]:



            '''simple docstring'''




            __snake_case            =	load_image(__SCREAMING_SNAKE_CASE	)
            __snake_case            =	self.image_processor(images=[image]     ,	return_tensors=self.framework	)
            __snake_case            =	candidate_labels
            __snake_case            =	[hypothesis_template.format(__SCREAMING_SNAKE_CASE	) for x in candidate_labels]
            __snake_case            =	self.tokenizer(__SCREAMING_SNAKE_CASE     ,	return_tensors=self.framework     ,	padding=__SCREAMING_SNAKE_CASE	)
            __snake_case            =	[text_inputs]
            return inputs


       def    lowerCAmelCase   ( self     ,	__SCREAMING_SNAKE_CASE	)   ->	Dict:



            '''simple docstring'''




            __snake_case            =	model_inputs.pop('''candidate_labels'''	)
            __snake_case            =	model_inputs.pop('''text_inputs'''	)
            if isinstance(text_inputs[0]     ,	__SCREAMING_SNAKE_CASE	):
                 __snake_case            =	text_inputs[0]
            else:
                 # Batching case.
                 __snake_case            =	text_inputs[0][0]

            __snake_case            =	self.model(**__SCREAMING_SNAKE_CASE     ,	**__SCREAMING_SNAKE_CASE	)

            __snake_case            =	{
                '''candidate_labels''': candidate_labels,
                '''logits''': outputs.logits_per_image,
            }
            return model_outputs




       def    lowerCAmelCase   ( self     ,	__SCREAMING_SNAKE_CASE	)   ->	str:



            '''simple docstring'''




            __snake_case            =	model_outputs.pop('''candidate_labels'''	)
            __snake_case            =	model_outputs['''logits'''][0]
            if self.framework == "pt":
                 __snake_case            =	logits.softmax(dim=-1	).squeeze(-1	)
                 __snake_case            =	probs.tolist()
                 if not isinstance(__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE	):
                      __snake_case            =	[scores]
            elif self.framework == "tf":
                 __snake_case            =	stable_softmax(__SCREAMING_SNAKE_CASE     ,	axis=-1	)
                 __snake_case            =	probs.numpy().tolist()
            else:
                 raise ValueError(F'''Unsupported framework: {self.framework}'''	)

            __snake_case            =	[
                {'''score''': score, '''label''': candidate_label}
                for score, candidate_label in sorted(zip(__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE	)     ,	key=lambda __SCREAMING_SNAKE_CASE	: -x[0]	)
            ]
            return result






