
'''simple docstring'''







import unittest

from transformers import (
    MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
    TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
    Pipeline,
    ZeroShotClassificationPipeline,
    pipeline,
)
from transformers.testing_utils import is_pipeline_test, nested_simplify, require_tf, require_torch, slow

from .test_pipelines_common import ANY


# These 2 model types require different inputs than those of the usual text models.
_snake_case				:   Any     =				{"""LayoutLMv2Config""", """LayoutLMv3Config"""}


@is_pipeline_test
class 			lowerCAmelCase (				unittest.TestCase			):
		a							:					Dict       =					MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING
		a							:					Any       =					TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING

		if model_mapping is not None:
				a							:					Optional[Any]       =					{config: model for config, model in model_mapping.items() if config.__name__ not in _TO_SKIP}
		if tf_model_mapping is not None:
				a							:					str       =					{
				    config: model for config, model in tf_model_mapping.items() if config.__name__ not in _TO_SKIP
				}




		def     lowercase       (				self					,						UpperCamelCase					,						UpperCamelCase					,						UpperCamelCase      ):
								_SCREAMING_SNAKE_CASE				= ZeroShotClassificationPipeline(
								    model=UpperCamelCase					,						tokenizer=UpperCamelCase					,						candidate_labels=["polics", "health"]      )
								return classifier, ["Who are you voting for in 2020?", "My stomach hurts."]




		def     lowercase       (				self					,						UpperCamelCase					,						UpperCamelCase      ):
								_SCREAMING_SNAKE_CASE				= classifier("Who are you voting for in 2020?"					,						candidate_labels="politics"      )
								self.assertEqual(UpperCamelCase					,						{"sequence": ANY(UpperCamelCase      ), "labels": [ANY(UpperCamelCase      )], "scores": [ANY(UpperCamelCase      )]}      )

								# No kwarg
								_SCREAMING_SNAKE_CASE				= classifier("Who are you voting for in 2020?"					,						["politics"]      )
								self.assertEqual(UpperCamelCase					,						{"sequence": ANY(UpperCamelCase      ), "labels": [ANY(UpperCamelCase      )], "scores": [ANY(UpperCamelCase      )]}      )

								_SCREAMING_SNAKE_CASE				= classifier("Who are you voting for in 2020?"					,						candidate_labels=["politics"]      )
								self.assertEqual(UpperCamelCase					,						{"sequence": ANY(UpperCamelCase      ), "labels": [ANY(UpperCamelCase      )], "scores": [ANY(UpperCamelCase      )]}      )

								_SCREAMING_SNAKE_CASE				= classifier("Who are you voting for in 2020?"					,						candidate_labels="politics, public health"      )
								self.assertEqual(
								    UpperCamelCase					,						{"sequence": ANY(UpperCamelCase      ), "labels": [ANY(UpperCamelCase      ), ANY(UpperCamelCase      )], "scores": [ANY(UpperCamelCase      ), ANY(UpperCamelCase      )]}      )
								self.assertAlmostEqual(sum(nested_simplify(outputs["scores"]      )      )					,						1.0      )

								_SCREAMING_SNAKE_CASE				= classifier("Who are you voting for in 2020?"					,						candidate_labels=["politics", "public health"]      )
								self.assertEqual(
								    UpperCamelCase					,						{"sequence": ANY(UpperCamelCase      ), "labels": [ANY(UpperCamelCase      ), ANY(UpperCamelCase      )], "scores": [ANY(UpperCamelCase      ), ANY(UpperCamelCase      )]}      )
								self.assertAlmostEqual(sum(nested_simplify(outputs["scores"]      )      )					,						1.0      )

								_SCREAMING_SNAKE_CASE				= classifier(
								    "Who are you voting for in 2020?"					,						candidate_labels="politics"					,						hypothesis_template="This text is about {}"      )
								self.assertEqual(UpperCamelCase					,						{"sequence": ANY(UpperCamelCase      ), "labels": [ANY(UpperCamelCase      )], "scores": [ANY(UpperCamelCase      )]}      )

								# https://github.com/huggingface/transformers/issues/13846
								_SCREAMING_SNAKE_CASE				= classifier(["I am happy"]					,						["positive", "negative"]      )
								self.assertEqual(
								    UpperCamelCase					,						[
								        {"sequence": ANY(UpperCamelCase      ), "labels": [ANY(UpperCamelCase      ), ANY(UpperCamelCase      )], "scores": [ANY(UpperCamelCase      ), ANY(UpperCamelCase      )]}
								        for i in range(1      )
								    ]					,						)
								_SCREAMING_SNAKE_CASE				= classifier(["I am happy", "I am sad"]					,						["positive", "negative"]      )
								self.assertEqual(
								    UpperCamelCase					,						[
								        {"sequence": ANY(UpperCamelCase      ), "labels": [ANY(UpperCamelCase      ), ANY(UpperCamelCase      )], "scores": [ANY(UpperCamelCase      ), ANY(UpperCamelCase      )]}
								        for i in range(2      )
								    ]					,						)

								with self.assertRaises(UpperCamelCase      ):
														classifier(""					,						candidate_labels="politics"      )

								with self.assertRaises(UpperCamelCase      ):
														classifier(UpperCamelCase					,						candidate_labels="politics"      )

								with self.assertRaises(UpperCamelCase      ):
														classifier("Who are you voting for in 2020?"					,						candidate_labels=""      )

								with self.assertRaises(UpperCamelCase      ):
														classifier("Who are you voting for in 2020?"					,						candidate_labels=UpperCamelCase      )

								with self.assertRaises(UpperCamelCase      ):
														classifier(
														    "Who are you voting for in 2020?"					,						candidate_labels="politics"					,						hypothesis_template="Not formatting template"					,						)

								with self.assertRaises(UpperCamelCase      ):
														classifier(
														    "Who are you voting for in 2020?"					,						candidate_labels="politics"					,						hypothesis_template=UpperCamelCase					,						)

								self.run_entailment_id(UpperCamelCase      )




		def     lowercase       (				self					,						UpperCamelCase      ):
								_SCREAMING_SNAKE_CASE				= zero_shot_classifier.model.config
								_SCREAMING_SNAKE_CASE				= config.labelaid
								_SCREAMING_SNAKE_CASE				= zero_shot_classifier.entailment_id

								_SCREAMING_SNAKE_CASE				= {"LABEL_0": 0, "LABEL_1": 1, "LABEL_2": 2}
								self.assertEqual(zero_shot_classifier.entailment_id					,						-1      )

								_SCREAMING_SNAKE_CASE				= {"entailment": 0, "neutral": 1, "contradiction": 2}
								self.assertEqual(zero_shot_classifier.entailment_id					,						0      )

								_SCREAMING_SNAKE_CASE				= {"ENTAIL": 0, "NON-ENTAIL": 1}
								self.assertEqual(zero_shot_classifier.entailment_id					,						0      )

								_SCREAMING_SNAKE_CASE				= {"ENTAIL": 2, "NEUTRAL": 1, "CONTR": 0}
								self.assertEqual(zero_shot_classifier.entailment_id					,						2      )

								_SCREAMING_SNAKE_CASE				= original_labelaid
								self.assertEqual(UpperCamelCase					,						zero_shot_classifier.entailment_id      )




		@require_torch
		def     lowercase       (				self      ):
								_SCREAMING_SNAKE_CASE				= pipeline(
								    "zero-shot-classification"					,						model="sshleifer/tiny-distilbert-base-cased-distilled-squad"					,						framework="pt"					,						)
								# There was a regression in 4.10 for this
								# Adding a test so we don't make the mistake again.
								# https://github.com/huggingface/transformers/issues/13381#issuecomment-912343499
								zero_shot_classifier(
								    "Who are you voting for in 2020?" * 100					,						candidate_labels=["politics", "public health", "science"]      )




		@require_torch
		def     lowercase       (				self      ):
								_SCREAMING_SNAKE_CASE				= pipeline(
								    "zero-shot-classification"					,						model="sshleifer/tiny-distilbert-base-cased-distilled-squad"					,						framework="pt"					,						)
								_SCREAMING_SNAKE_CASE				= zero_shot_classifier(
								    "Who are you voting for in 2020?"					,						candidate_labels=["politics", "public health", "science"]      )

								self.assertEqual(
								    nested_simplify(UpperCamelCase      )					,						{
								        "sequence": "Who are you voting for in 2020?",
								        "labels": ["science", "public health", "politics"],
								        "scores": [0.3_33, 0.3_33, 0.3_33],
								    }					,						)




		@require_tf
		def     lowercase       (				self      ):
								_SCREAMING_SNAKE_CASE				= pipeline(
								    "zero-shot-classification"					,						model="sshleifer/tiny-distilbert-base-cased-distilled-squad"					,						framework="tf"					,						)
								_SCREAMING_SNAKE_CASE				= zero_shot_classifier(
								    "Who are you voting for in 2020?"					,						candidate_labels=["politics", "public health", "science"]      )

								self.assertEqual(
								    nested_simplify(UpperCamelCase      )					,						{
								        "sequence": "Who are you voting for in 2020?",
								        "labels": ["science", "public health", "politics"],
								        "scores": [0.3_33, 0.3_33, 0.3_33],
								    }					,						)




		@slow
		@require_torch
		def     lowercase       (				self      ):
								_SCREAMING_SNAKE_CASE				= pipeline("zero-shot-classification"					,						model="roberta-large-mnli"					,						framework="pt"      )
								_SCREAMING_SNAKE_CASE				= zero_shot_classifier(
								    "Who are you voting for in 2020?"					,						candidate_labels=["politics", "public health", "science"]      )

								self.assertEqual(
								    nested_simplify(UpperCamelCase      )					,						{
								        "sequence": "Who are you voting for in 2020?",
								        "labels": ["politics", "public health", "science"],
								        "scores": [0.9_76, 0.0_15, 0.0_09],
								    }					,						)
								_SCREAMING_SNAKE_CASE				= zero_shot_classifier(
								    "The dominant sequence transduction models are based on complex recurrent or convolutional neural networks"
								    " in an encoder-decoder configuration. The best performing models also connect the encoder and decoder"
								    " through an attention mechanism. We propose a new simple network architecture, the Transformer, based"
								    " solely on attention mechanisms, dispensing with recurrence and convolutions entirely. Experiments on two"
								    " machine translation tasks show these models to be superior in quality while being more parallelizable"
								    " and requiring significantly less time to train. Our model achieves 28.4 BLEU on the WMT 2014"
								    " English-to-German translation task, improving over the existing best results, including ensembles by"
								    " over 2 BLEU. On the WMT 2014 English-to-French translation task, our model establishes a new"
								    " single-model state-of-the-art BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small"
								    " fraction of the training costs of the best models from the literature. We show that the Transformer"
								    " generalizes well to other tasks by applying it successfully to English constituency parsing both with"
								    " large and limited training data."					,						candidate_labels=["machine learning", "statistics", "translation", "vision"]					,						multi_label=UpperCamelCase					,						)
								self.assertEqual(
								    nested_simplify(UpperCamelCase      )					,						{
								        "sequence": (
								            "The dominant sequence transduction models are based on complex recurrent or convolutional neural"
								            " networks in an encoder-decoder configuration. The best performing models also connect the"
								            " encoder and decoder through an attention mechanism. We propose a new simple network"
								            " architecture, the Transformer, based solely on attention mechanisms, dispensing with recurrence"
								            " and convolutions entirely. Experiments on two machine translation tasks show these models to be"
								            " superior in quality while being more parallelizable and requiring significantly less time to"
								            " train. Our model achieves 28.4 BLEU on the WMT 2014 English-to-German translation task,"
								            " improving over the existing best results, including ensembles by over 2 BLEU. On the WMT 2014"
								            " English-to-French translation task, our model establishes a new single-model state-of-the-art"
								            " BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small fraction of the training"
								            " costs of the best models from the literature. We show that the Transformer generalizes well to"
								            " other tasks by applying it successfully to English constituency parsing both with large and"
								            " limited training data."
								        ),
								        "labels": ["translation", "machine learning", "vision", "statistics"],
								        "scores": [0.8_17, 0.7_13, 0.0_18, 0.0_18],
								    }					,						)




		@slow
		@require_tf
		def     lowercase       (				self      ):
								_SCREAMING_SNAKE_CASE				= pipeline("zero-shot-classification"					,						model="roberta-large-mnli"					,						framework="tf"      )
								_SCREAMING_SNAKE_CASE				= zero_shot_classifier(
								    "Who are you voting for in 2020?"					,						candidate_labels=["politics", "public health", "science"]      )

								self.assertEqual(
								    nested_simplify(UpperCamelCase      )					,						{
								        "sequence": "Who are you voting for in 2020?",
								        "labels": ["politics", "public health", "science"],
								        "scores": [0.9_76, 0.0_15, 0.0_09],
								    }					,						)
								_SCREAMING_SNAKE_CASE				= zero_shot_classifier(
								    "The dominant sequence transduction models are based on complex recurrent or convolutional neural networks"
								    " in an encoder-decoder configuration. The best performing models also connect the encoder and decoder"
								    " through an attention mechanism. We propose a new simple network architecture, the Transformer, based"
								    " solely on attention mechanisms, dispensing with recurrence and convolutions entirely. Experiments on two"
								    " machine translation tasks show these models to be superior in quality while being more parallelizable"
								    " and requiring significantly less time to train. Our model achieves 28.4 BLEU on the WMT 2014"
								    " English-to-German translation task, improving over the existing best results, including ensembles by"
								    " over 2 BLEU. On the WMT 2014 English-to-French translation task, our model establishes a new"
								    " single-model state-of-the-art BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small"
								    " fraction of the training costs of the best models from the literature. We show that the Transformer"
								    " generalizes well to other tasks by applying it successfully to English constituency parsing both with"
								    " large and limited training data."					,						candidate_labels=["machine learning", "statistics", "translation", "vision"]					,						multi_label=UpperCamelCase					,						)
								self.assertEqual(
								    nested_simplify(UpperCamelCase      )					,						{
								        "sequence": (
								            "The dominant sequence transduction models are based on complex recurrent or convolutional neural"
								            " networks in an encoder-decoder configuration. The best performing models also connect the"
								            " encoder and decoder through an attention mechanism. We propose a new simple network"
								            " architecture, the Transformer, based solely on attention mechanisms, dispensing with recurrence"
								            " and convolutions entirely. Experiments on two machine translation tasks show these models to be"
								            " superior in quality while being more parallelizable and requiring significantly less time to"
								            " train. Our model achieves 28.4 BLEU on the WMT 2014 English-to-German translation task,"
								            " improving over the existing best results, including ensembles by over 2 BLEU. On the WMT 2014"
								            " English-to-French translation task, our model establishes a new single-model state-of-the-art"
								            " BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small fraction of the training"
								            " costs of the best models from the literature. We show that the Transformer generalizes well to"
								            " other tasks by applying it successfully to English constituency parsing both with large and"
								            " limited training data."
								        ),
								        "labels": ["translation", "machine learning", "vision", "statistics"],
								        "scores": [0.8_17, 0.7_13, 0.0_18, 0.0_18],
								    }					,						)

'''simple docstring'''







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

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


class 			lowerCAmelCase :




		def __init__(				self					,						UpperCamelCase      ):
								if isinstance(UpperCamelCase					,						UpperCamelCase      ):
														# 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(UpperCamelCase      )
								elif os.path.exists(UpperCamelCase      ):
														with io.open(UpperCamelCase					,						"r"					,						encoding="utf-8"      ) as f:
																				_SCREAMING_SNAKE_CASE				= json.load(UpperCamelCase      )
								else:
														try:
																				_SCREAMING_SNAKE_CASE				= baseaa.urlsafe_baadecode(UpperCamelCase      ).decode("utf-8"      )
																				_SCREAMING_SNAKE_CASE				= json.loads(UpperCamelCase      )
														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      ):
								# zero stage - this is done as early as possible, before model is created, to allow
								# ``is_deepspeed_zero3_enabled`` query and getting to the early deepspeed config object
								# during ``zero.Init()`` which needs to know the dtype, and some other hparams.
								_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					,						UpperCamelCase      ):
								_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(UpperCamelCase      )
														if config is None:
																				return None, ds_key

								return config, ds_key




		def     lowercase       (				self					,						UpperCamelCase					,						UpperCamelCase=None      ):
								_SCREAMING_SNAKE_CASE,							_SCREAMING_SNAKE_CASE				= self.find_config_node(UpperCamelCase      )
								if config is None:
														return default
								return config.get(UpperCamelCase					,						UpperCamelCase      )




		def     lowercase       (				self					,						UpperCamelCase					,						UpperCamelCase=False      ):
								_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(UpperCamelCase      )
														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(UpperCamelCase      )




		def     lowercase       (				self					,						UpperCamelCase      ):
								_SCREAMING_SNAKE_CASE				= self.get_value(UpperCamelCase      )
								return False if value is None else bool(UpperCamelCase      )




		def     lowercase       (				self					,						UpperCamelCase      ):
								_SCREAMING_SNAKE_CASE				= self.get_value(UpperCamelCase      )
								return False if value is None else not bool(UpperCamelCase      )




		def     lowercase       (				self      ):
								return self._stage == 2




		def     lowercase       (				self      ):
								return self._stage == 3




		def     lowercase       (				self      ):
								return self._offload


class 			lowerCAmelCase :




		def __init__(				self					,						UpperCamelCase      ):
								_SCREAMING_SNAKE_CASE				= engine




		def     lowercase       (				self					,						UpperCamelCase					,						**UpperCamelCase      ):
								# runs backpropagation and handles mixed precision
								self.engine.backward(UpperCamelCase					,						**UpperCamelCase      )

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




		def __init__(				self					,						UpperCamelCase      ):
								super().__init__(UpperCamelCase					,						device_placement=UpperCamelCase					,						scaler=UpperCamelCase      )
								_SCREAMING_SNAKE_CASE				= hasattr(self.optimizer					,						"overflow"      )




		def     lowercase       (				self					,						UpperCamelCase=None      ):
								pass  # `accelerator.backward(loss)` is doing that automatically. Therefore, its implementation is not needed




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




		@property
		def     lowercase       (				self      ):
								if self.__has_overflow__:
														return self.optimizer.overflow
								return False


class 			lowerCAmelCase (				__UpperCAmelCase			):




		def __init__(				self					,						UpperCamelCase					,						UpperCamelCase      ):
								super().__init__(UpperCamelCase					,						UpperCamelCase      )




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


class 			lowerCAmelCase :




		def __init__(				self					,						UpperCamelCase					,						UpperCamelCase=0.0_01					,						UpperCamelCase=0					,						**UpperCamelCase      ):
								_SCREAMING_SNAKE_CASE				= params
								_SCREAMING_SNAKE_CASE				= lr
								_SCREAMING_SNAKE_CASE				= weight_decay
								_SCREAMING_SNAKE_CASE				= kwargs


class 			lowerCAmelCase :




		def __init__(				self					,						UpperCamelCase					,						UpperCamelCase=None					,						UpperCamelCase=0					,						**UpperCamelCase      ):
								_SCREAMING_SNAKE_CASE				= optimizer
								_SCREAMING_SNAKE_CASE				= total_num_steps
								_SCREAMING_SNAKE_CASE				= warmup_num_steps
								_SCREAMING_SNAKE_CASE				= kwargs