






"""simple docstring"""



import math
import os
import unittest

from transformers import MegatronBertConfig, is_torch_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
						import torch

						from transformers import (
						    MODEL_FOR_PRETRAINING_MAPPING,
						    MegatronBertForCausalLM,
						    MegatronBertForMaskedLM,
						    MegatronBertForMultipleChoice,
						    MegatronBertForNextSentencePrediction,
						    MegatronBertForPreTraining,
						    MegatronBertForQuestionAnswering,
						    MegatronBertForSequenceClassification,
						    MegatronBertForTokenClassification,
						    MegatronBertModel,
						)






class 							__SCREAMING_SNAKE_CASE						:


							def __init__(			self      :						Optional[int]			,    snake_case      :						Tuple			,    snake_case      :						Any=13			,    snake_case      :						Dict=7			,    snake_case      :						Tuple=True			,    snake_case      :						Dict=True			,    snake_case      :						Optional[int]=True			,    snake_case      :						Tuple=True			,    snake_case      :						int=99			,    snake_case      :						Tuple=64			,    snake_case      :						int=32			,    snake_case      :						Union[str, Any]=5			,    snake_case      :						Dict=4			,    snake_case      :						Optional[Any]=37			,    snake_case      :						Dict="gelu"			,    snake_case      :						Optional[Any]=0.1			,    snake_case      :						Optional[Any]=0.1			,    snake_case      :						str=512			,    snake_case      :						Any=16			,    snake_case      :						int=2			,    snake_case      :						Union[str, Any]=0.02			,    snake_case      :						Tuple=3			,    snake_case      :						Any=4			,    snake_case      :						Optional[int]=None			,    ):
													'''simple docstring'''





													A__						:  int		  =  parent
													A__						:  int		  =  batch_size
													A__						:  Union[str, Any]		  =  seq_length
													A__						:  Any		  =  is_training
													A__						:  List[str]		  =  use_input_mask
													A__						:  Optional[int]		  =  use_token_type_ids
													A__						:  Dict		  =  use_labels
													A__						:  List[Any]		  =  vocab_size
													A__						:  List[Any]		  =  hidden_size
													A__						:  List[Any]		  =  embedding_size
													A__						:  str		  =  num_hidden_layers
													A__						:  List[str]		  =  num_attention_heads
													A__						:  Optional[Any]		  =  intermediate_size
													A__						:  Optional[int]		  =  hidden_act
													A__						:  int		  =  hidden_dropout_prob
													A__						:  Union[str, Any]		  =  attention_probs_dropout_prob
													A__						:  Tuple		  =  max_position_embeddings
													A__						:  List[str]		  =  type_vocab_size
													A__						:  int		  =  type_sequence_label_size
													A__						:  List[Any]		  =  initializer_range
													A__						:  Any		  =  num_labels
													A__						:  Optional[int]		  =  num_choices
													A__						:  Optional[Any]		  =  scope


							def 						_UpperCamelCase       (			self      :						Optional[int]							):
													'''simple docstring'''





													A__						:  Any		  =  ids_tensor([self.batch_size, self.seq_length]			,    self.vocab_size							)

													A__						:  int		  =  None
													if self.use_input_mask:
																			A__						:  Union[str, Any]		  =  random_attention_mask([self.batch_size, self.seq_length]							)

													A__						:  Dict		  =  None
													if self.use_token_type_ids:
																			A__						:  Any		  =  ids_tensor([self.batch_size, self.seq_length]			,    self.type_vocab_size							)

													A__						:  List[str]		  =  None
													A__						:  List[Any]		  =  None
													A__						:  Union[str, Any]		  =  None
													if self.use_labels:
																			A__						:  Any		  =  ids_tensor([self.batch_size]			,    self.type_sequence_label_size							)
																			A__						:  Tuple		  =  ids_tensor([self.batch_size, self.seq_length]			,    self.num_labels							)
																			A__						:  List[Any]		  =  ids_tensor([self.batch_size]			,    self.num_choices							)

													A__						:  Dict		  =  self.get_config()

													return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels


							def 						_UpperCamelCase       (			self      :						str							):
													'''simple docstring'''





													return MegatronBertConfig(
													    vocab_size=self.vocab_size			,    hidden_size=self.hidden_size			,    num_hidden_layers=self.num_hidden_layers			,    num_attention_heads=self.num_attention_heads			,    intermediate_size=self.intermediate_size			,    embedding_size=self.embedding_size			,    hidden_act=self.hidden_act			,    hidden_dropout_prob=self.hidden_dropout_prob			,    attention_probs_dropout_prob=self.attention_probs_dropout_prob			,    max_position_embeddings=self.max_position_embeddings			,    type_vocab_size=self.type_vocab_size			,    is_decoder=__lowerCamelCase			,    initializer_range=self.initializer_range			,    )


							def 						_UpperCamelCase       (			self      :						Tuple			,    snake_case      :						int			,    snake_case      :						List[Any]			,    snake_case      :						List[str]			,    snake_case      :						Optional[Any]			,    snake_case      :						List[Any]			,    snake_case      :						str			,    snake_case      :						Optional[Any]							):
													'''simple docstring'''





													A__						:  Optional[Any]		  =  MegatronBertModel(config=__lowerCamelCase							)
													model.to(__lowerCamelCase							)
													model.eval()
													A__						:  str		  =  model(__lowerCamelCase			,    attention_mask=__lowerCamelCase			,    token_type_ids=__lowerCamelCase							)
													A__						:  List[Any]		  =  model(__lowerCamelCase			,    token_type_ids=__lowerCamelCase							)
													A__						:  str		  =  model(__lowerCamelCase							)

													self.parent.assertEqual(result.last_hidden_state.shape			,    (self.batch_size, self.seq_length, self.hidden_size)							)
													self.parent.assertEqual(result.pooler_output.shape			,    (self.batch_size, self.hidden_size)							)


							def 						_UpperCamelCase       (			self      :						str			,    snake_case      :						Union[str, Any]			,    snake_case      :						str			,    snake_case      :						List[str]			,    snake_case      :						Optional[int]			,    snake_case      :						str			,    snake_case      :						Optional[int]			,    snake_case      :						Optional[int]							):
													'''simple docstring'''





													A__						:  Dict		  =  MegatronBertForMaskedLM(config=__lowerCamelCase							)
													model.to(__lowerCamelCase							)
													model.eval()
													A__						:  Dict		  =  model(__lowerCamelCase			,    attention_mask=__lowerCamelCase			,    token_type_ids=__lowerCamelCase			,    labels=__lowerCamelCase							)
													self.parent.assertEqual(result.logits.shape			,    (self.batch_size, self.seq_length, self.vocab_size)							)


							def 						_UpperCamelCase       (			self      :						List[str]			,    snake_case      :						Dict			,    snake_case      :						Optional[Any]			,    snake_case      :						Optional[Any]			,    snake_case      :						Dict			,    snake_case      :						int			,    snake_case      :						List[Any]			,    snake_case      :						int							):
													'''simple docstring'''





													A__						:  Dict		  =  MegatronBertForCausalLM(config=__lowerCamelCase							)
													model.to(__lowerCamelCase							)
													model.eval()
													A__						:  List[Any]		  =  model(__lowerCamelCase			,    attention_mask=__lowerCamelCase			,    token_type_ids=__lowerCamelCase			,    labels=__lowerCamelCase							)
													self.parent.assertEqual(result.logits.shape			,    (self.batch_size, self.seq_length, self.vocab_size)							)


							def 						_UpperCamelCase       (			self      :						List[str]			,    snake_case      :						Optional[Any]			,    snake_case      :						List[str]			,    snake_case      :						Any			,    snake_case      :						str			,    snake_case      :						Union[str, Any]			,    snake_case      :						int			,    snake_case      :						Tuple							):
													'''simple docstring'''





													A__						:  Optional[int]		  =  MegatronBertForNextSentencePrediction(config=__lowerCamelCase							)
													model.to(__lowerCamelCase							)
													model.eval()
													A__						:  List[str]		  =  model(
													    __lowerCamelCase			,    attention_mask=__lowerCamelCase			,    token_type_ids=__lowerCamelCase			,    labels=__lowerCamelCase			,    )
													self.parent.assertEqual(result.logits.shape			,    (self.batch_size, 2)							)


							def 						_UpperCamelCase       (			self      :						List[Any]			,    snake_case      :						Optional[int]			,    snake_case      :						Any			,    snake_case      :						Optional[int]			,    snake_case      :						Union[str, Any]			,    snake_case      :						Optional[int]			,    snake_case      :						Union[str, Any]			,    snake_case      :						Optional[int]							):
													'''simple docstring'''





													A__						:  List[str]		  =  MegatronBertForPreTraining(config=__lowerCamelCase							)
													model.to(__lowerCamelCase							)
													model.eval()
													A__						:  List[Any]		  =  model(
													    __lowerCamelCase			,    attention_mask=__lowerCamelCase			,    token_type_ids=__lowerCamelCase			,    labels=__lowerCamelCase			,    next_sentence_label=__lowerCamelCase			,    )
													self.parent.assertEqual(result.prediction_logits.shape			,    (self.batch_size, self.seq_length, self.vocab_size)							)
													self.parent.assertEqual(result.seq_relationship_logits.shape			,    (self.batch_size, 2)							)


							def 						_UpperCamelCase       (			self      :						Optional[Any]			,    snake_case      :						Union[str, Any]			,    snake_case      :						int			,    snake_case      :						List[Any]			,    snake_case      :						Tuple			,    snake_case      :						List[Any]			,    snake_case      :						Dict			,    snake_case      :						Tuple							):
													'''simple docstring'''





													A__						:  Dict		  =  MegatronBertForQuestionAnswering(config=__lowerCamelCase							)
													model.to(__lowerCamelCase							)
													model.eval()
													A__						:  str		  =  model(
													    __lowerCamelCase			,    attention_mask=__lowerCamelCase			,    token_type_ids=__lowerCamelCase			,    start_positions=__lowerCamelCase			,    end_positions=__lowerCamelCase			,    )
													self.parent.assertEqual(result.start_logits.shape			,    (self.batch_size, self.seq_length)							)
													self.parent.assertEqual(result.end_logits.shape			,    (self.batch_size, self.seq_length)							)


							def 						_UpperCamelCase       (			self      :						List[str]			,    snake_case      :						str			,    snake_case      :						List[Any]			,    snake_case      :						Optional[int]			,    snake_case      :						str			,    snake_case      :						int			,    snake_case      :						Union[str, Any]			,    snake_case      :						Optional[Any]							):
													'''simple docstring'''





													A__						:  List[str]		  =  self.num_labels
													A__						:  Tuple		  =  MegatronBertForSequenceClassification(__lowerCamelCase							)
													model.to(__lowerCamelCase							)
													model.eval()
													A__						:  Optional[Any]		  =  model(__lowerCamelCase			,    attention_mask=__lowerCamelCase			,    token_type_ids=__lowerCamelCase			,    labels=__lowerCamelCase							)
													self.parent.assertEqual(result.logits.shape			,    (self.batch_size, self.num_labels)							)


							def 						_UpperCamelCase       (			self      :						Optional[int]			,    snake_case      :						str			,    snake_case      :						Optional[int]			,    snake_case      :						Optional[Any]			,    snake_case      :						List[Any]			,    snake_case      :						List[str]			,    snake_case      :						Optional[Any]			,    snake_case      :						Union[str, Any]							):
													'''simple docstring'''





													A__						:  Optional[int]		  =  self.num_labels
													A__						:  str		  =  MegatronBertForTokenClassification(config=__lowerCamelCase							)
													model.to(__lowerCamelCase							)
													model.eval()
													A__						:  Dict		  =  model(__lowerCamelCase			,    attention_mask=__lowerCamelCase			,    token_type_ids=__lowerCamelCase			,    labels=__lowerCamelCase							)
													self.parent.assertEqual(result.logits.shape			,    (self.batch_size, self.seq_length, self.num_labels)							)


							def 						_UpperCamelCase       (			self      :						Optional[Any]			,    snake_case      :						Union[str, Any]			,    snake_case      :						Union[str, Any]			,    snake_case      :						List[str]			,    snake_case      :						int			,    snake_case      :						int			,    snake_case      :						List[str]			,    snake_case      :						List[Any]							):
													'''simple docstring'''





													A__						:  Dict		  =  self.num_choices
													A__						:  Union[str, Any]		  =  MegatronBertForMultipleChoice(config=__lowerCamelCase							)
													model.to(__lowerCamelCase							)
													model.eval()
													A__						:  Optional[int]		  =  input_ids.unsqueeze(1							).expand(-1			,    self.num_choices			,    -1							).contiguous()
													A__						:  Any		  =  token_type_ids.unsqueeze(1							).expand(-1			,    self.num_choices			,    -1							).contiguous()
													A__						:  Optional[Any]		  =  input_mask.unsqueeze(1							).expand(-1			,    self.num_choices			,    -1							).contiguous()
													A__						:  Any		  =  model(
													    __lowerCamelCase			,    attention_mask=__lowerCamelCase			,    token_type_ids=__lowerCamelCase			,    labels=__lowerCamelCase			,    )
													self.parent.assertEqual(result.logits.shape			,    (self.batch_size, self.num_choices)							)


							def 						_UpperCamelCase       (			self      :						Any							):
													'''simple docstring'''





													A__						:  Optional[Any]		  =  self.prepare_config_and_inputs()
													(
													    A__	
													)						:  Optional[Any]		  =  config_and_inputs
													A__						:  Tuple		  =  {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
													return config, inputs_dict






@require_torch
class 							__SCREAMING_SNAKE_CASE						(				lowerCamelCase__       ,		lowerCamelCase__       ,		unittest.TestCase		):
							snake_case_     =							(
							    (
							        MegatronBertModel,
							        MegatronBertForMaskedLM,
							        MegatronBertForCausalLM,
							        MegatronBertForMultipleChoice,
							        MegatronBertForNextSentencePrediction,
							        MegatronBertForPreTraining,
							        MegatronBertForQuestionAnswering,
							        MegatronBertForSequenceClassification,
							        MegatronBertForTokenClassification,
							    )
							    if is_torch_available()
							    else ()
							)
							snake_case_     =							(
							    {
							        'feature-extraction': MegatronBertModel,
							        'fill-mask': MegatronBertForMaskedLM,
							        'question-answering': MegatronBertForQuestionAnswering,
							        'text-classification': MegatronBertForSequenceClassification,
							        'text-generation': MegatronBertForCausalLM,
							        'token-classification': MegatronBertForTokenClassification,
							        'zero-shot': MegatronBertForSequenceClassification,
							    }
							    if is_torch_available()
							    else {}
							)
							snake_case_     =							True
							# test_resize_embeddings = False
							snake_case_     =							False


							def 						_UpperCamelCase       (			self      :						List[str]			,    snake_case      :						str			,    snake_case      :						List[str]			,    snake_case      :						Optional[int]=False							):
													'''simple docstring'''





													A__						:  List[str]		  =  super()._prepare_for_class(__lowerCamelCase			,    __lowerCamelCase			,    return_labels=__lowerCamelCase							)

													if return_labels:
																			if model_class in get_values(__lowerCamelCase							):
																									A__						:  int		  =  torch.zeros(
																									    (self.model_tester.batch_size, self.model_tester.seq_length)			,    dtype=torch.long			,    device=__lowerCamelCase							)
																									A__						:  str		  =  torch.zeros(
																									    self.model_tester.batch_size			,    dtype=torch.long			,    device=__lowerCamelCase							)
													return inputs_dict


							def 						_UpperCamelCase       (			self      :						Optional[Any]							):
													'''simple docstring'''





													A__						:  Optional[Any]		  =  MegatronBertModelTester(self							)
													A__						:  Union[str, Any]		  =  ConfigTester(self			,    config_class=__lowerCamelCase			,    hidden_size=37							)


							def 						_UpperCamelCase       (			self      :						List[str]							):
													'''simple docstring'''





													self.config_tester.run_common_tests()


							def 						_UpperCamelCase       (			self      :						Optional[Any]							):
													'''simple docstring'''





													A__						:  Optional[Any]		  =  self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_megatron_bert_model(*__lowerCamelCase							)


							def 						_UpperCamelCase       (			self      :						Union[str, Any]							):
													'''simple docstring'''





													A__						:  List[Any]		  =  self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_megatron_bert_for_masked_lm(*__lowerCamelCase							)


							def 						_UpperCamelCase       (			self      :						Dict							):
													'''simple docstring'''





													A__						:  List[str]		  =  self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_megatron_bert_for_multiple_choice(*__lowerCamelCase							)


							def 						_UpperCamelCase       (			self      :						str							):
													'''simple docstring'''





													A__						:  Tuple		  =  self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_megatron_bert_for_next_sequence_prediction(*__lowerCamelCase							)


							def 						_UpperCamelCase       (			self      :						Any							):
													'''simple docstring'''





													A__						:  Dict		  =  self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_megatron_bert_for_pretraining(*__lowerCamelCase							)


							def 						_UpperCamelCase       (			self      :						Optional[Any]							):
													'''simple docstring'''





													A__						:  Any		  =  self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_megatron_bert_for_question_answering(*__lowerCamelCase							)


							def 						_UpperCamelCase       (			self      :						Tuple							):
													'''simple docstring'''





													A__						:  Dict		  =  self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_megatron_bert_for_sequence_classification(*__lowerCamelCase							)


							def 						_UpperCamelCase       (			self      :						List[str]							):
													'''simple docstring'''





													A__						:  Any		  =  self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_megatron_bert_for_token_classification(*__lowerCamelCase							)




def      _lowerCAmelCase		(							UpperCAmelCase__       :					Dict      )		->Dict:
						return torch.tensor(
						    lowerCamelCase_,						dtype=torch.long,						device=lowerCamelCase_,						)


A_    =							1e-4






@require_torch
@require_sentencepiece
@require_tokenizers
class 							__SCREAMING_SNAKE_CASE						(				unittest.TestCase		):


							@slow
							@unittest.skip("""Model is not available."""							)
							def 						_UpperCamelCase       (			self      :						List[str]							):
													'''simple docstring'''





													A__						:  Any		  =  '''nvidia/megatron-bert-uncased-345m'''
													if "MYDIR" in os.environ:
																			A__						:  Union[str, Any]		  =  os.path.join(os.environ["""MYDIR"""]			,    __lowerCamelCase							)
													A__						:  Union[str, Any]		  =  MegatronBertModel.from_pretrained(__lowerCamelCase							)
													model.to(__lowerCamelCase							)
													model.half()
													A__						:  Union[str, Any]		  =  _long_tensor([[101, 7110, 1005, 1056, 2023, 1_1333, 1_7413, 1029, 102]]							)
													with torch.no_grad():
																			A__						:  str		  =  model(__lowerCamelCase							)[0]
													A__						:  Optional[Any]		  =  torch.Size((1, 9, 1024)							)
													self.assertEqual(output.shape			,    __lowerCamelCase							)

													A__						:  Any		  =  [-0.6040, -0.2517, -0.1025, 0.3420, -0.6758, -0.0017, -0.1089, -0.1990, 0.5728]
													for ii in range(3							):
																			for jj in range(3							):
																									A__						:  Optional[Any]		  =  output[0, ii, jj]
																									A__						:  Tuple		  =  expected[3 * ii + jj]
																									A__						:  List[str]		  =  '''ii={} jj={} a={} b={}'''.format(__lowerCamelCase			,    __lowerCamelCase			,    __lowerCamelCase			,    __lowerCamelCase							)
																									self.assertTrue(math.isclose(__lowerCamelCase			,    __lowerCamelCase			,    rel_tol=__lowerCamelCase			,    abs_tol=__lowerCamelCase							)			,    msg=__lowerCamelCase							)














"""simple docstring"""



import unittest

from transformers import (
    MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
    TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
    TextClassificationPipeline,
    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.
A_    =							{'''LayoutLMv2Config''', '''LayoutLMv3Config'''}






@is_pipeline_test
class 							__SCREAMING_SNAKE_CASE						(				unittest.TestCase		):
							snake_case_     =							MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING
							snake_case_     =							TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING

							if model_mapping is not None:
														snake_case_     =							{config: model for config, model in model_mapping.items() if config.__name__ not in _TO_SKIP}
							if tf_model_mapping is not None:
														snake_case_     =							{
														    config: model for config, model in tf_model_mapping.items() if config.__name__ not in _TO_SKIP
														}


							@require_torch
							def 						_UpperCamelCase       (			self      :						Tuple							):
													'''simple docstring'''





													A__						:  Tuple		  =  pipeline(
													    task="""text-classification"""			,    model="""hf-internal-testing/tiny-random-distilbert"""			,    framework="""pt"""							)

													A__						:  Dict		  =  text_classifier("""This is great !"""							)
													self.assertEqual(nested_simplify(snake_case							)			,    [{"""label""": """LABEL_0""", """score""": 0.504}]							)

													A__						:  Optional[int]		  =  text_classifier("""This is great !"""			,    top_k=2							)
													self.assertEqual(
													    nested_simplify(snake_case							)			,    [{"""label""": """LABEL_0""", """score""": 0.504}, {"""label""": """LABEL_1""", """score""": 0.496}]							)

													A__						:  Any		  =  text_classifier(["""This is great !""", """This is bad"""]			,    top_k=2							)
													self.assertEqual(
													    nested_simplify(snake_case							)			,    [
													        [{"""label""": """LABEL_0""", """score""": 0.504}, {"""label""": """LABEL_1""", """score""": 0.496}],
													        [{"""label""": """LABEL_0""", """score""": 0.504}, {"""label""": """LABEL_1""", """score""": 0.496}],
													    ]			,    )

													A__						:  Optional[Any]		  =  text_classifier("""This is great !"""			,    top_k=1							)
													self.assertEqual(nested_simplify(snake_case							)			,    [{"""label""": """LABEL_0""", """score""": 0.504}]							)

													# Legacy behavior
													A__						:  str		  =  text_classifier("""This is great !"""			,    return_all_scores=snake_case							)
													self.assertEqual(nested_simplify(snake_case							)			,    [{"""label""": """LABEL_0""", """score""": 0.504}]							)

													A__						:  int		  =  text_classifier("""This is great !"""			,    return_all_scores=snake_case							)
													self.assertEqual(
													    nested_simplify(snake_case							)			,    [[{"""label""": """LABEL_0""", """score""": 0.504}, {"""label""": """LABEL_1""", """score""": 0.496}]]							)

													A__						:  str		  =  text_classifier(["""This is great !""", """Something else"""]			,    return_all_scores=snake_case							)
													self.assertEqual(
													    nested_simplify(snake_case							)			,    [
													        [{"""label""": """LABEL_0""", """score""": 0.504}, {"""label""": """LABEL_1""", """score""": 0.496}],
													        [{"""label""": """LABEL_0""", """score""": 0.504}, {"""label""": """LABEL_1""", """score""": 0.496}],
													    ]			,    )

													A__						:  Optional[int]		  =  text_classifier(["""This is great !""", """Something else"""]			,    return_all_scores=snake_case							)
													self.assertEqual(
													    nested_simplify(snake_case							)			,    [
													        {"""label""": """LABEL_0""", """score""": 0.504},
													        {"""label""": """LABEL_0""", """score""": 0.504},
													    ]			,    )


							@require_torch
							def 						_UpperCamelCase       (			self      :						int							):
													'''simple docstring'''





													import torch

													A__						:  Tuple		  =  pipeline(
													    task="""text-classification"""			,    model="""hf-internal-testing/tiny-random-distilbert"""			,    framework="""pt"""			,    device=torch.device("""cpu"""							)			,    )

													A__						:  List[str]		  =  text_classifier("""This is great !"""							)
													self.assertEqual(nested_simplify(snake_case							)			,    [{"""label""": """LABEL_0""", """score""": 0.504}]							)


							@require_tf
							def 						_UpperCamelCase       (			self      :						List[str]							):
													'''simple docstring'''





													A__						:  str		  =  pipeline(
													    task="""text-classification"""			,    model="""hf-internal-testing/tiny-random-distilbert"""			,    framework="""tf"""							)

													A__						:  Optional[Any]		  =  text_classifier("""This is great !"""							)
													self.assertEqual(nested_simplify(snake_case							)			,    [{"""label""": """LABEL_0""", """score""": 0.504}]							)


							@slow
							@require_torch
							def 						_UpperCamelCase       (			self      :						List[Any]							):
													'''simple docstring'''





													A__						:  Optional[int]		  =  pipeline("""text-classification"""							)

													A__						:  Optional[int]		  =  text_classifier("""This is great !"""							)
													self.assertEqual(nested_simplify(snake_case							)			,    [{"""label""": """POSITIVE""", """score""": 1.0}]							)
													A__						:  Optional[Any]		  =  text_classifier("""This is bad !"""							)
													self.assertEqual(nested_simplify(snake_case							)			,    [{"""label""": """NEGATIVE""", """score""": 1.0}]							)
													A__						:  Dict		  =  text_classifier("""Birds are a type of animal"""							)
													self.assertEqual(nested_simplify(snake_case							)			,    [{"""label""": """POSITIVE""", """score""": 0.988}]							)


							@slow
							@require_tf
							def 						_UpperCamelCase       (			self      :						Any							):
													'''simple docstring'''





													A__						:  Union[str, Any]		  =  pipeline("""text-classification"""			,    framework="""tf"""							)

													A__						:  Dict		  =  text_classifier("""This is great !"""							)
													self.assertEqual(nested_simplify(snake_case							)			,    [{"""label""": """POSITIVE""", """score""": 1.0}]							)
													A__						:  Optional[int]		  =  text_classifier("""This is bad !"""							)
													self.assertEqual(nested_simplify(snake_case							)			,    [{"""label""": """NEGATIVE""", """score""": 1.0}]							)
													A__						:  Tuple		  =  text_classifier("""Birds are a type of animal"""							)
													self.assertEqual(nested_simplify(snake_case							)			,    [{"""label""": """POSITIVE""", """score""": 0.988}]							)


							def 						_UpperCamelCase       (			self      :						Optional[Any]			,    snake_case      :						Any			,    snake_case      :						List[Any]			,    snake_case      :						Any							):
													'''simple docstring'''





													A__						:  Union[str, Any]		  =  TextClassificationPipeline(model=snake_case			,    tokenizer=snake_case							)
													return text_classifier, ["HuggingFace is in", "This is another test"]


							def 						_UpperCamelCase       (			self      :						int			,    snake_case      :						Union[str, Any]			,    snake_case      :						Dict							):
													'''simple docstring'''





													A__						:  List[str]		  =  text_classifier.model
													# Small inputs because BartTokenizer tiny has maximum position embeddings = 22
													A__						:  int		  =  """HuggingFace is in"""
													A__						:  List[Any]		  =  text_classifier(snake_case							)

													self.assertEqual(nested_simplify(snake_case							)			,    [{"""label""": ANY(snake_case							), """score""": ANY(snake_case							)}]							)
													self.assertTrue(outputs[0]["""label"""] in model.config.idalabel.values()							)

													A__						:  Any		  =  ["""HuggingFace is in """, """Paris is in France"""]
													A__						:  Any		  =  text_classifier(snake_case							)
													self.assertEqual(
													    nested_simplify(snake_case							)			,    [{"""label""": ANY(snake_case							), """score""": ANY(snake_case							)}, {"""label""": ANY(snake_case							), """score""": ANY(snake_case							)}]			,    )
													self.assertTrue(outputs[0]["""label"""] in model.config.idalabel.values()							)
													self.assertTrue(outputs[1]["""label"""] in model.config.idalabel.values()							)

													# Forcing to get all results with `top_k=None`
													# This is NOT the legacy format
													A__						:  List[Any]		  =  text_classifier(snake_case			,    top_k=snake_case							)
													A__						:  Tuple		  =  len(model.config.idalabel.values()							)
													self.assertEqual(
													    nested_simplify(snake_case							)			,    [[{"""label""": ANY(snake_case							), """score""": ANY(snake_case							)}] * N, [{"""label""": ANY(snake_case							), """score""": ANY(snake_case							)}] * N]			,    )

													A__						:  Optional[Any]		  =  {"""text""": """HuggingFace is in """, """text_pair""": """Paris is in France"""}
													A__						:  Union[str, Any]		  =  text_classifier(snake_case							)
													self.assertEqual(
													    nested_simplify(snake_case							)			,    {"""label""": ANY(snake_case							), """score""": ANY(snake_case							)}			,    )
													self.assertTrue(outputs["""label"""] in model.config.idalabel.values()							)

													# This might be used a text pair, but tokenizer + pipe interaction
													# makes it hard to understand that it's not using the pair properly
													# https://github.com/huggingface/transformers/issues/17305
													# We disabled this usage instead as it was outputting wrong outputs.
													A__						:  Tuple		  =  [["""HuggingFace is in """, """Paris is in France"""]]
													with self.assertRaises(snake_case							):
																			text_classifier(snake_case							)

													# This used to be valid for doing text pairs
													# We're keeping it working because of backward compatibility
													A__						:  List[Any]		  =  text_classifier([[["""HuggingFace is in """, """Paris is in France"""]]]							)
													self.assertEqual(
													    nested_simplify(snake_case							)			,    [{"""label""": ANY(snake_case							), """score""": ANY(snake_case							)}]			,    )
													self.assertTrue(outputs[0]["""label"""] in model.config.idalabel.values()							)






