




import random
import unittest

import numpy as np

import transformers
from transformers import is_flax_available, is_torch_available
from transformers.testing_utils import is_pt_flax_cross_test, require_flax


if is_flax_available():
				import os

				import jax.numpy as jnp
				from jax import jit

				from transformers import AutoTokenizer, FlaxAutoModelForCausalLM
				from transformers.modeling_flax_pytorch_utils import load_flax_weights_in_pytorch_model

				UpperCamelCase__						=			'''0.12'''  # assumed parallelism: 8


if is_torch_available():
				import torch





def 							UpperCAmelCase		(		snake_case			:					Optional[int]      ,       snake_case			:					Tuple      ,       snake_case			:					Union[str, Any]=None    ):
					if rng is None:
										_lowerCAmelCase:int									=  random.Random()

					_lowerCAmelCase:List[Any]									=  1
					for dim in shape:
										total_dims *= dim

					_lowerCAmelCase:Optional[Any]									=  []
					for _ in range(lowercase__    ):
										values.append(rng.randint(0      ,       vocab_size - 1    )    )

					_lowerCAmelCase:List[str]									=  np.array(lowercase__      ,       dtype=jnp.intaa    ).reshape(lowercase__    )

					return output





def 							UpperCAmelCase		(		snake_case			:					Optional[int]      ,       snake_case			:					List[str]=None    ):
					_lowerCAmelCase:Tuple									=  ids_tensor(lowercase__      ,       vocab_size=2      ,       rng=lowercase__    )
					# make sure that at least one token is attended to for each batch
					_lowerCAmelCase:Union[str, Any]									=  1
					return attn_mask




@require_flax
class   a__			:
						snake_case__             = None
						snake_case__             = ()





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




											"""simple docstring"""







											_lowerCAmelCase							,   _lowerCAmelCase:Dict									=  self.model_tester.prepare_config_and_inputs_for_common()

											# cut to half length & take max batch_size 3
											_lowerCAmelCase:Optional[int]									=  2
											_lowerCAmelCase:int									=  inputs['''input_ids'''].shape[-1] // 2
											_lowerCAmelCase:Optional[int]									=  inputs['''input_ids'''][:max_batch_size, :sequence_length]

											_lowerCAmelCase:int									=  jnp.ones_like(__UpperCamelCase)
											_lowerCAmelCase:Union[str, Any]									=  attention_mask[:max_batch_size, :sequence_length]

											# generate max 5 tokens
											_lowerCAmelCase:Optional[Any]									=  input_ids.shape[-1] + 5
											if config.eos_token_id is not None and config.pad_token_id is None:
																# hack to allow generate for models such as GPT2 as is done in `generate()`
																_lowerCAmelCase:List[Any]									=  config.eos_token_id
											return config, input_ids, attention_mask, max_length





						@is_pt_flax_cross_test
						def       __UpperCamelCase     (							self      :	int)						->       List[str]:




											"""simple docstring"""







											_lowerCAmelCase							,   _lowerCAmelCase							,   _lowerCAmelCase							,   _lowerCAmelCase:Optional[int]									=  self._get_input_ids_and_config()
											_lowerCAmelCase:str									=  False
											_lowerCAmelCase:Optional[int]									=  max_length
											_lowerCAmelCase:Optional[int]									=  0

											for model_class in self.all_generative_model_classes:
																_lowerCAmelCase:Any									=  model_class(__UpperCamelCase)

																_lowerCAmelCase:List[str]									=  model_class.__name__[4:]  # Skip the "Flax" at the beginning
																_lowerCAmelCase:Optional[int]									=  getattr(__UpperCamelCase     ,__UpperCamelCase)
																_lowerCAmelCase:str									=  pt_model_class(__UpperCamelCase).eval()
																_lowerCAmelCase:Tuple									=  load_flax_weights_in_pytorch_model(__UpperCamelCase     ,flax_model.params)

																_lowerCAmelCase:List[str]									=  flax_model.generate(__UpperCamelCase).sequences
																_lowerCAmelCase:Optional[Any]									=  pt_model.generate(torch.tensor(__UpperCamelCase     ,dtype=torch.long))

																if flax_generation_outputs.shape[-1] > pt_generation_outputs.shape[-1]:
																					_lowerCAmelCase:Optional[int]									=  flax_generation_outputs[:, : pt_generation_outputs.shape[-1]]

																self.assertListEqual(pt_generation_outputs.numpy().tolist()     ,flax_generation_outputs.tolist())





						def       __UpperCamelCase     (							self      :	List[Any])						->       Tuple:




											"""simple docstring"""







											_lowerCAmelCase							,   _lowerCAmelCase							,   _lowerCAmelCase							,   _lowerCAmelCase:int									=  self._get_input_ids_and_config()
											_lowerCAmelCase:Tuple									=  False
											_lowerCAmelCase:List[str]									=  max_length

											for model_class in self.all_generative_model_classes:
																_lowerCAmelCase:List[Any]									=  model_class(__UpperCamelCase)

																_lowerCAmelCase:Optional[int]									=  model.generate(__UpperCamelCase).sequences
																self.assertEqual(generation_outputs.shape[-1]     ,__UpperCamelCase)

																_lowerCAmelCase:Dict									=  jit(model.generate)
																_lowerCAmelCase:Union[str, Any]									=  jit_generate(__UpperCamelCase).sequences

																self.assertListEqual(generation_outputs.tolist()     ,jit_generation_outputs.tolist())





						def       __UpperCamelCase     (							self      :	Dict)						->       List[Any]:




											"""simple docstring"""







											_lowerCAmelCase							,   _lowerCAmelCase							,   _lowerCAmelCase							,   _lowerCAmelCase:int									=  self._get_input_ids_and_config()
											_lowerCAmelCase:List[str]									=  True
											_lowerCAmelCase:Optional[int]									=  max_length

											for model_class in self.all_generative_model_classes:
																_lowerCAmelCase:Union[str, Any]									=  model_class(__UpperCamelCase)

																_lowerCAmelCase:str									=  model.generate(__UpperCamelCase).sequences
																self.assertEqual(generation_outputs.shape[-1]     ,__UpperCamelCase)

																_lowerCAmelCase:List[Any]									=  jit(model.generate)
																_lowerCAmelCase:Any									=  jit_generate(__UpperCamelCase).sequences

																self.assertListEqual(generation_outputs.tolist()     ,jit_generation_outputs.tolist())





						def       __UpperCamelCase     (							self      :	str)						->       str:




											"""simple docstring"""







											_lowerCAmelCase							,   _lowerCAmelCase							,   _lowerCAmelCase							,   _lowerCAmelCase:List[Any]									=  self._get_input_ids_and_config()
											_lowerCAmelCase:List[Any]									=  False
											_lowerCAmelCase:Dict									=  max_length
											_lowerCAmelCase:Tuple									=  2

											for model_class in self.all_generative_model_classes:
																_lowerCAmelCase:Union[str, Any]									=  model_class(__UpperCamelCase)

																_lowerCAmelCase:Optional[Any]									=  model.generate(__UpperCamelCase).sequences
																self.assertEqual(generation_outputs.shape[-1]     ,__UpperCamelCase)

																_lowerCAmelCase:Any									=  jit(model.generate)
																_lowerCAmelCase:Tuple									=  jit_generate(__UpperCamelCase).sequences

																self.assertListEqual(generation_outputs.tolist()     ,jit_generation_outputs.tolist())





						def       __UpperCamelCase     (							self      :	int)						->       str:




											"""simple docstring"""







											_lowerCAmelCase							,   _lowerCAmelCase							,   _lowerCAmelCase							,   _lowerCAmelCase:List[Any]									=  self._get_input_ids_and_config()
											_lowerCAmelCase:Dict									=  False
											_lowerCAmelCase:Any									=  max_length
											_lowerCAmelCase:List[Any]									=  2
											_lowerCAmelCase:Dict									=  2

											for model_class in self.all_generative_model_classes:
																_lowerCAmelCase:List[Any]									=  model_class(__UpperCamelCase)

																_lowerCAmelCase:Optional[int]									=  model.generate(__UpperCamelCase).sequences
																self.assertEqual(generation_outputs.shape[0]     ,input_ids.shape[0] * config.num_return_sequences)





						def       __UpperCamelCase     (							self      :	Any)						->       Dict:




											"""simple docstring"""







											_lowerCAmelCase							,   _lowerCAmelCase							,   _lowerCAmelCase							,   _lowerCAmelCase:Tuple									=  self._get_input_ids_and_config()
											_lowerCAmelCase:Any									=  True
											_lowerCAmelCase:Optional[Any]									=  max_length
											_lowerCAmelCase:List[Any]									=  0.8
											_lowerCAmelCase:int									=  10
											_lowerCAmelCase:List[str]									=  0.3
											_lowerCAmelCase:List[str]									=  1
											_lowerCAmelCase:str									=  8
											_lowerCAmelCase:List[Any]									=  9

											for model_class in self.all_generative_model_classes:
																_lowerCAmelCase:Dict									=  model_class(__UpperCamelCase)

																_lowerCAmelCase:Dict									=  model.generate(__UpperCamelCase).sequences
																self.assertEqual(generation_outputs.shape[-1]     ,__UpperCamelCase)

																_lowerCAmelCase:str									=  jit(model.generate)
																_lowerCAmelCase:str									=  jit_generate(__UpperCamelCase).sequences

																self.assertListEqual(generation_outputs.tolist()     ,jit_generation_outputs.tolist())





						def       __UpperCamelCase     (							self      :	Tuple)						->       Dict:




											"""simple docstring"""







											_lowerCAmelCase							,   _lowerCAmelCase							,   _lowerCAmelCase							,   _lowerCAmelCase:Tuple									=  self._get_input_ids_and_config()
											_lowerCAmelCase:Optional[int]									=  max_length
											_lowerCAmelCase:Tuple									=  1
											_lowerCAmelCase:Tuple									=  8
											_lowerCAmelCase:Tuple									=  9

											for model_class in self.all_generative_model_classes:
																_lowerCAmelCase:List[str]									=  model_class(__UpperCamelCase)

																_lowerCAmelCase:Dict									=  model.generate(__UpperCamelCase).sequences
																self.assertEqual(generation_outputs.shape[-1]     ,__UpperCamelCase)

																_lowerCAmelCase:str									=  jit(model.generate)
																_lowerCAmelCase:List[Any]									=  jit_generate(__UpperCamelCase).sequences

																self.assertListEqual(generation_outputs.tolist()     ,jit_generation_outputs.tolist())





						def       __UpperCamelCase     (							self      :	Optional[int])						->       List[str]:




											"""simple docstring"""







											_lowerCAmelCase							,   _lowerCAmelCase							,   _lowerCAmelCase							,   _lowerCAmelCase:Any									=  self._get_input_ids_and_config()
											_lowerCAmelCase:List[Any]									=  max_length
											_lowerCAmelCase:str									=  2
											_lowerCAmelCase:int									=  1
											_lowerCAmelCase:int									=  8
											_lowerCAmelCase:Union[str, Any]									=  9

											for model_class in self.all_generative_model_classes:
																_lowerCAmelCase:Dict									=  model_class(__UpperCamelCase)

																_lowerCAmelCase:Optional[Any]									=  model.generate(__UpperCamelCase).sequences
																self.assertEqual(generation_outputs.shape[-1]     ,__UpperCamelCase)

																_lowerCAmelCase:Optional[Any]									=  jit(model.generate)
																_lowerCAmelCase:Optional[Any]									=  jit_generate(__UpperCamelCase).sequences

																self.assertListEqual(generation_outputs.tolist()     ,jit_generation_outputs.tolist())





						def       __UpperCamelCase     (							self      :	str)						->       int:




											"""simple docstring"""







											_lowerCAmelCase							,   _lowerCAmelCase							,   _lowerCAmelCase							,   _lowerCAmelCase:Tuple									=  self._get_input_ids_and_config()

											# pad attention mask on the left
											_lowerCAmelCase:Any									=  attention_mask.at[(0, 0)].set(0)

											_lowerCAmelCase:Tuple									=  False
											_lowerCAmelCase:Optional[int]									=  max_length

											for model_class in self.all_generative_model_classes:
																_lowerCAmelCase:str									=  model_class(__UpperCamelCase)

																_lowerCAmelCase:List[str]									=  model.generate(__UpperCamelCase     ,attention_mask=__UpperCamelCase).sequences
																self.assertEqual(generation_outputs.shape[-1]     ,__UpperCamelCase)

																_lowerCAmelCase:str									=  jit(model.generate)
																_lowerCAmelCase:Tuple									=  jit_generate(__UpperCamelCase     ,attention_mask=__UpperCamelCase).sequences

																self.assertListEqual(generation_outputs.tolist()     ,jit_generation_outputs.tolist())





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




											"""simple docstring"""







											_lowerCAmelCase							,   _lowerCAmelCase							,   _lowerCAmelCase							,   _lowerCAmelCase:Dict									=  self._get_input_ids_and_config()

											# pad attention mask on the left
											_lowerCAmelCase:Any									=  attention_mask.at[(0, 0)].set(0)

											_lowerCAmelCase:Any									=  True
											_lowerCAmelCase:Tuple									=  max_length

											for model_class in self.all_generative_model_classes:
																_lowerCAmelCase:List[Any]									=  model_class(__UpperCamelCase)

																_lowerCAmelCase:Union[str, Any]									=  model.generate(__UpperCamelCase     ,attention_mask=__UpperCamelCase).sequences
																self.assertEqual(generation_outputs.shape[-1]     ,__UpperCamelCase)

																_lowerCAmelCase:Any									=  jit(model.generate)
																_lowerCAmelCase:Dict									=  jit_generate(__UpperCamelCase     ,attention_mask=__UpperCamelCase).sequences

																self.assertListEqual(generation_outputs.tolist()     ,jit_generation_outputs.tolist())







						def       __UpperCamelCase     (							self      :	List[Any])						->       int:




											"""simple docstring"""







											_lowerCAmelCase							,   _lowerCAmelCase							,   _lowerCAmelCase							,   _lowerCAmelCase:List[str]									=  self._get_input_ids_and_config()

											# pad attention mask on the left
											_lowerCAmelCase:Union[str, Any]									=  attention_mask.at[(0, 0)].set(0)

											_lowerCAmelCase:Any									=  2
											_lowerCAmelCase:Any									=  max_length

											for model_class in self.all_generative_model_classes:
																_lowerCAmelCase:int									=  model_class(__UpperCamelCase)

																_lowerCAmelCase:Any									=  model.generate(__UpperCamelCase     ,attention_mask=__UpperCamelCase).sequences
																self.assertEqual(generation_outputs.shape[-1]     ,__UpperCamelCase)

																_lowerCAmelCase:Optional[int]									=  jit(model.generate)
																_lowerCAmelCase:List[Any]									=  jit_generate(__UpperCamelCase     ,attention_mask=__UpperCamelCase).sequences

																self.assertListEqual(generation_outputs.tolist()     ,jit_generation_outputs.tolist())




@require_flax
class   a__			(       unittest.TestCase  ):





						def       __UpperCamelCase     (							self      :	List[str])						->       Optional[int]:




											"""simple docstring"""







											_lowerCAmelCase:Tuple									=  AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-bert''')
											_lowerCAmelCase:int									=  FlaxAutoModelForCausalLM.from_pretrained('''hf-internal-testing/tiny-bert-flax-only''')

											_lowerCAmelCase:Optional[Any]									=  '''Hello world'''
											_lowerCAmelCase:List[str]									=  tokenizer(__UpperCamelCase     ,return_tensors='''np''').input_ids

											# typos are quickly detected (the correct argument is `do_sample`)
											with self.assertRaisesRegex(__UpperCamelCase     ,'''do_samples'''):
																model.generate(__UpperCamelCase     ,do_samples=__UpperCamelCase)

											# arbitrary arguments that will not be used anywhere are also not accepted
											with self.assertRaisesRegex(__UpperCamelCase     ,'''foo'''):
																_lowerCAmelCase:Any									=  {'''foo''': '''bar'''}
																model.generate(__UpperCamelCase     ,**__UpperCamelCase)












"""simple docstring"""






import warnings
from typing import Dict

import numpy as np

from ..utils import ExplicitEnum, add_end_docstrings, is_tf_available, is_torch_available
from .base import PIPELINE_INIT_ARGS, GenericTensor, Pipeline


if is_tf_available():
				from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING

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





def 							UpperCAmelCase		(		snake_case			:					Any    ):
					return 1.0 / (1.0 + np.exp(-_outputs    ))





def 							UpperCAmelCase		(		snake_case			:					Optional[Any]    ):
					_lowerCAmelCase:Optional[Any]									=  np.max(_outputs      ,       axis=-1      ,       keepdims=snake_case    )
					_lowerCAmelCase:List[Any]									=  np.exp(_outputs - maxes    )
					return shifted_exp / shifted_exp.sum(axis=-1      ,       keepdims=snake_case    )




class   a__			(       UpperCamelCase_  ):
						snake_case__             = '''sigmoid'''
						snake_case__             = '''softmax'''
						snake_case__             = '''none'''




@add_end_docstrings(
    UpperCamelCase_					,	r'''
        return_all_scores (`bool`, *optional*, defaults to `False`):
            Whether to return all prediction scores or just the one of the predicted class.
        function_to_apply (`str`, *optional*, defaults to `"default"`):
            The function to apply to the model outputs in order to retrieve the scores. Accepts four different values:

            - `"default"`: if the model has a single label, will apply the sigmoid function on the output. If the model
              has several labels, will apply the softmax function on the output.
            - `"sigmoid"`: Applies the sigmoid function on the output.
            - `"softmax"`: Applies the softmax function on the output.
            - `"none"`: Does not apply any function on the output.
    '''					,	)
class   a__			(       UpperCamelCase_  ):
						snake_case__             = False
						snake_case__             = ClassificationFunction.NONE





						def __init__(							self      :	str     ,**a__      :	List[str])						->       Optional[int]:




											"""simple docstring"""







											super().__init__(**a__)

											self.check_model_type(
											    TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING
											    if self.framework == '''tf'''
											    else MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING)





						def       __UpperCamelCase     (							self      :	int     ,a__      :	Union[str, Any]=None     ,a__      :	Optional[Any]=None     ,a__      :	Dict=""     ,**a__      :	Dict)						->       str:




											"""simple docstring"""







											_lowerCAmelCase:List[Any]									=  tokenizer_kwargs

											_lowerCAmelCase:Union[str, Any]									=  {}
											if hasattr(self.model.config     ,'''return_all_scores''') and return_all_scores is None:
																_lowerCAmelCase:str									=  self.model.config.return_all_scores

											if isinstance(a__     ,a__) or top_k is None:
																_lowerCAmelCase:Union[str, Any]									=  top_k
																_lowerCAmelCase:Any									=  False
											elif return_all_scores is not None:
																warnings.warn(
																    '''`return_all_scores` is now deprecated,  if want a similar functionality use `top_k=None` instead of'''
																    ''' `return_all_scores=True` or `top_k=1` instead of `return_all_scores=False`.'''     ,a__     ,)
																if return_all_scores:
																					_lowerCAmelCase:Tuple									=  None
																else:
																					_lowerCAmelCase:Union[str, Any]									=  1

											if isinstance(a__     ,a__):
																_lowerCAmelCase:Union[str, Any]									=  ClassificationFunction[function_to_apply.upper()]

											if function_to_apply is not None:
																_lowerCAmelCase:Union[str, Any]									=  function_to_apply
											return preprocess_params, {}, postprocess_params





						def __call__(							self      :	List[str]     ,*a__      :	Union[str, Any]     ,**a__      :	List[Any])						->       List[Any]:




											"""simple docstring"""







											_lowerCAmelCase:Dict									=  super().__call__(*a__     ,**a__)
											# TODO try and retrieve it in a nicer way from _sanitize_parameters.
											_lowerCAmelCase:str									=  '''top_k''' not in kwargs
											if isinstance(args[0]     ,a__) and _legacy:
																# This pipeline is odd, and return a list when single item is run
																return [result]
											else:
																return result





						def       __UpperCamelCase     (							self      :	Union[str, Any]     ,a__      :	Optional[Any]     ,**a__      :	Optional[int])						->       Dict[str, GenericTensor]:




											"""simple docstring"""







											_lowerCAmelCase:Dict									=  self.framework
											if isinstance(a__     ,a__):
																return self.tokenizer(**a__     ,return_tensors=a__     ,**a__)
											elif isinstance(a__     ,a__) and len(a__) == 1 and isinstance(inputs[0]     ,a__) and len(inputs[0]) == 2:
																# It used to be valid to use a list of list of list for text pairs, keeping this path for BC
																return self.tokenizer(
																    text=inputs[0][0]     ,text_pair=inputs[0][1]     ,return_tensors=a__     ,**a__)
											elif isinstance(a__     ,a__):
																# This is likely an invalid usage of the pipeline attempting to pass text pairs.
																raise ValueError(
																    '''The pipeline received invalid inputs, if you are trying to send text pairs, you can try to send a'''
																    ''' dictionary `{"text": "My text", "text_pair": "My pair"}` in order to send a text pair.''')
											return self.tokenizer(a__     ,return_tensors=a__     ,**a__)





						def       __UpperCamelCase     (							self      :	List[Any]     ,a__      :	List[str])						->       Optional[Any]:




											"""simple docstring"""







											return self.model(**a__)







						def       __UpperCamelCase     (							self      :	Optional[int]     ,a__      :	Optional[Any]     ,a__      :	Any=None     ,a__      :	Optional[Any]=1     ,a__      :	Any=True)						->       Union[str, Any]:




											"""simple docstring"""







											if function_to_apply is None:
																if self.model.config.problem_type == "multi_label_classification" or self.model.config.num_labels == 1:
																					_lowerCAmelCase:Union[str, Any]									=  ClassificationFunction.SIGMOID
																elif self.model.config.problem_type == "single_label_classification" or self.model.config.num_labels > 1:
																					_lowerCAmelCase:Any									=  ClassificationFunction.SOFTMAX
																elif hasattr(self.model.config     ,'''function_to_apply''') and function_to_apply is None:
																					_lowerCAmelCase:List[Any]									=  self.model.config.function_to_apply
																else:
																					_lowerCAmelCase:List[Any]									=  ClassificationFunction.NONE

											_lowerCAmelCase:List[Any]									=  model_outputs['''logits'''][0]
											_lowerCAmelCase:Optional[Any]									=  outputs.numpy()

											if function_to_apply == ClassificationFunction.SIGMOID:
																_lowerCAmelCase:Dict									=  sigmoid(a__)
											elif function_to_apply == ClassificationFunction.SOFTMAX:
																_lowerCAmelCase:Dict									=  softmax(a__)
											elif function_to_apply == ClassificationFunction.NONE:
																_lowerCAmelCase:Union[str, Any]									=  outputs
											else:
																raise ValueError(F'Unrecognized `function_to_apply` argument: {function_to_apply}')

											if top_k == 1 and _legacy:
																return {"label": self.model.config.idalabel[scores.argmax().item()], "score": scores.max().item()}

											_lowerCAmelCase:Union[str, Any]									=  [
											    {'''label''': self.model.config.idalabel[i], '''score''': score.item()} for i, score in enumerate(a__)
											]
											if not _legacy:
																dict_scores.sort(key=lambda a__: x["score"]     ,reverse=a__)
																if top_k is not None:
																					_lowerCAmelCase:Dict									=  dict_scores[:top_k]
											return dict_scores






