
"""simple docstring"""




import math
import unittest

from transformers import BioGptConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
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 (
					    BioGptForCausalLM,
					    BioGptForSequenceClassification,
					    BioGptForTokenClassification,
					    BioGptModel,
					    BioGptTokenizer,
					)
					from transformers.models.biogpt.modeling_biogpt import BIOGPT_PRETRAINED_MODEL_ARCHIVE_LIST



class 	lowerCAmelCase_						:


				"""simple docstring"""



				def __init__(self   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__=13   ,       SCREAMING_SNAKE_CASE__=7   ,       SCREAMING_SNAKE_CASE__=True   ,       SCREAMING_SNAKE_CASE__=True   ,       SCREAMING_SNAKE_CASE__=False   ,       SCREAMING_SNAKE_CASE__=True   ,       SCREAMING_SNAKE_CASE__=99   ,       SCREAMING_SNAKE_CASE__=32   ,       SCREAMING_SNAKE_CASE__=5   ,       SCREAMING_SNAKE_CASE__=4   ,       SCREAMING_SNAKE_CASE__=37   ,       SCREAMING_SNAKE_CASE__="gelu"   ,       SCREAMING_SNAKE_CASE__=0.1   ,       SCREAMING_SNAKE_CASE__=0.1   ,       SCREAMING_SNAKE_CASE__=5_12   ,       SCREAMING_SNAKE_CASE__=16   ,       SCREAMING_SNAKE_CASE__=2   ,       SCREAMING_SNAKE_CASE__=0.02   ,       SCREAMING_SNAKE_CASE__=3   ,       SCREAMING_SNAKE_CASE__=4   ,       SCREAMING_SNAKE_CASE__=None   ,       )					->		Tuple:



											"""simple docstring"""




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



				def 	__magic_name__    (self    )					->		Dict:



											"""simple docstring"""




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

											SCREAMING_SNAKE_CASE__						:       Optional[int]           =   None
											if self.use_input_mask:
																		SCREAMING_SNAKE_CASE__						:       List[str]           =   random_attention_mask([self.batch_size, self.seq_length]    )

											SCREAMING_SNAKE_CASE__						:       List[Any]           =   None
											if self.use_token_type_ids:
																		SCREAMING_SNAKE_CASE__						:       Tuple           =   ids_tensor([self.batch_size, self.seq_length]   ,       self.type_vocab_size    )

											SCREAMING_SNAKE_CASE__						:       str           =   None
											SCREAMING_SNAKE_CASE__						:       Optional[int]           =   None
											SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   None
											if self.use_labels:
																		SCREAMING_SNAKE_CASE__						:       int           =   ids_tensor([self.batch_size]   ,       self.type_sequence_label_size    )
																		SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   ids_tensor([self.batch_size, self.seq_length]   ,       self.num_labels    )
																		SCREAMING_SNAKE_CASE__						:       str           =   ids_tensor([self.batch_size]   ,       self.num_choices    )

											SCREAMING_SNAKE_CASE__						:       List[str]           =   self.get_config()

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



				def 	__magic_name__    (self    )					->		List[Any]:



											"""simple docstring"""




											return BioGptConfig(
											    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   ,       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=_A   ,       initializer_range=self.initializer_range   ,       )



				def 	__magic_name__    (self   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__    )					->		List[Any]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       List[str]           =   BioGptModel(config=_A    )
											model.to(_A    )
											model.eval()
											SCREAMING_SNAKE_CASE__						:       Tuple           =   model(_A   ,       attention_mask=_A    )
											SCREAMING_SNAKE_CASE__						:       str           =   model(_A    )
											self.parent.assertEqual(result.last_hidden_state.shape   ,       (self.batch_size, self.seq_length, self.hidden_size)    )



				def 	__magic_name__    (self   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       )					->		Any:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       List[str]           =   BioGptForCausalLM(config=_A    )
											model.to(_A    )
											model.eval()
											SCREAMING_SNAKE_CASE__						:       str           =   model(_A   ,       attention_mask=_A   ,       token_type_ids=_A   ,       labels=_A    )
											self.parent.assertEqual(result.logits.shape   ,       (self.batch_size, self.seq_length, self.vocab_size)    )



				def 	__magic_name__    (self   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       *SCREAMING_SNAKE_CASE__    )					->		str:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       Any           =   BioGptModel(config=_A    )
											model.to(_A    )
											model.eval()

											# create attention mask
											SCREAMING_SNAKE_CASE__						:       List[str]           =   torch.ones(input_ids.shape   ,       dtype=torch.long   ,       device=_A    )
											SCREAMING_SNAKE_CASE__						:       str           =   self.seq_length // 2
											SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   0

											# first forward pass
											SCREAMING_SNAKE_CASE__						:       List[str]           =   model(_A   ,       attention_mask=_A    ).to_tuple()

											# create hypothetical next token and extent to next_input_ids
											SCREAMING_SNAKE_CASE__						:       Optional[int]           =   ids_tensor((self.batch_size, 1)   ,       config.vocab_size    )

											# change a random masked slice from input_ids
											SCREAMING_SNAKE_CASE__						:       List[Any]           =   ids_tensor((1,)   ,       _A    ).item() + 1
											SCREAMING_SNAKE_CASE__						:       str           =   ids_tensor((self.batch_size, 1)   ,       config.vocab_size    ).squeeze(-1    )
											SCREAMING_SNAKE_CASE__						:       str           =   random_other_next_tokens

											# append to next input_ids and attn_mask
											SCREAMING_SNAKE_CASE__						:       str           =   torch.cat([input_ids, next_tokens]   ,       dim=-1    )
											SCREAMING_SNAKE_CASE__						:       int           =   torch.cat(
											    [attn_mask, torch.ones((attn_mask.shape[0], 1)   ,       dtype=torch.long   ,       device=_A    )]   ,       dim=1   ,       )

											# get two different outputs
											SCREAMING_SNAKE_CASE__						:       Any           =   model(_A   ,       attention_mask=_A    )['''last_hidden_state''']
											SCREAMING_SNAKE_CASE__						:       Optional[int]           =   model(_A   ,       past_key_values=_A   ,       attention_mask=_A    )['''last_hidden_state''']

											# select random slice
											SCREAMING_SNAKE_CASE__						:       List[Any]           =   ids_tensor((1,)   ,       output_from_past.shape[-1]    ).item()
											SCREAMING_SNAKE_CASE__						:       Any           =   output_from_no_past[:, -1, random_slice_idx].detach()
											SCREAMING_SNAKE_CASE__						:       List[Any]           =   output_from_past[:, 0, random_slice_idx].detach()

											# test that outputs are equal for slice
											self.parent.assertTrue(torch.allclose(_A   ,       _A   ,       atol=1E-3    )    )



				def 	__magic_name__    (self   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       *SCREAMING_SNAKE_CASE__    )					->		Any:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   BioGptModel(config=_A    ).to(_A    ).eval()

											SCREAMING_SNAKE_CASE__						:       str           =   torch.ones(input_ids.shape   ,       dtype=torch.long   ,       device=_A    )

											# first forward pass
											SCREAMING_SNAKE_CASE__						:       List[str]           =   model(_A   ,       attention_mask=_A   ,       use_cache=_A    )

											SCREAMING_SNAKE_CASE__						:       Optional[int]           =   outputs.to_tuple()

											# create hypothetical multiple next token and extent to next_input_ids
											SCREAMING_SNAKE_CASE__						:       int           =   ids_tensor((self.batch_size, 3)   ,       config.vocab_size    )
											SCREAMING_SNAKE_CASE__						:       Dict           =   ids_tensor((self.batch_size, 3)   ,       2    )

											# append to next input_ids and
											SCREAMING_SNAKE_CASE__						:       int           =   torch.cat([input_ids, next_tokens]   ,       dim=-1    )
											SCREAMING_SNAKE_CASE__						:       List[str]           =   torch.cat([attention_mask, next_attn_mask]   ,       dim=-1    )

											SCREAMING_SNAKE_CASE__						:       Dict           =   model(_A   ,       attention_mask=_A    )['''last_hidden_state''']
											SCREAMING_SNAKE_CASE__						:       Dict           =   model(_A   ,       attention_mask=_A   ,       past_key_values=_A    )[
											    '''last_hidden_state'''
											]

											# select random slice
											SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   ids_tensor((1,)   ,       output_from_past.shape[-1]    ).item()
											SCREAMING_SNAKE_CASE__						:       List[str]           =   output_from_no_past[:, -3:, random_slice_idx].detach()
											SCREAMING_SNAKE_CASE__						:       List[str]           =   output_from_past[:, :, random_slice_idx].detach()

											self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1]    )

											# test that outputs are equal for slice
											self.parent.assertTrue(torch.allclose(_A   ,       _A   ,       atol=1E-3    )    )



				def 	__magic_name__    (self   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       *SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__=False    )					->		Dict:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       Dict           =   BioGptForCausalLM(_A    )
											model.to(_A    )
											if gradient_checkpointing:
																		model.gradient_checkpointing_enable()

											SCREAMING_SNAKE_CASE__						:       Tuple           =   model(_A   ,       labels=_A    )
											self.parent.assertEqual(result.loss.shape   ,       ()    )
											self.parent.assertEqual(result.logits.shape   ,       (self.batch_size, self.seq_length, self.vocab_size)    )
											result.loss.backward()



				def 	__magic_name__    (self   ,       SCREAMING_SNAKE_CASE__   ,       *SCREAMING_SNAKE_CASE__    )					->		List[Any]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       Any           =   BioGptModel(_A    )
											SCREAMING_SNAKE_CASE__						:       Any           =   model.config.initializer_range / math.sqrt(2 * model.config.num_hidden_layers    )
											for key in model.state_dict().keys():
																		if "c_proj" in key and "weight" in key:
																									self.parent.assertLessEqual(abs(torch.std(model.state_dict()[key]    ) - model_std    )   ,       0.001    )
																									self.parent.assertLessEqual(abs(torch.mean(model.state_dict()[key]    ) - 0.0    )   ,       0.01    )



				def 	__magic_name__    (self   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       *SCREAMING_SNAKE_CASE__    )					->		Tuple:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   self.num_labels
											SCREAMING_SNAKE_CASE__						:       str           =   BioGptForTokenClassification(_A    )
											model.to(_A    )
											model.eval()
											SCREAMING_SNAKE_CASE__						:       Optional[Any]           =   model(_A   ,       attention_mask=_A   ,       token_type_ids=_A    )
											self.parent.assertEqual(result.logits.shape   ,       (self.batch_size, self.seq_length, self.num_labels)    )



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



											"""simple docstring"""




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






@require_torch
class 	lowerCAmelCase_						(__a ,					__a ,					__a ,					unittest.TestCase  ):


				"""simple docstring"""



				__UpperCamelCase      :						Optional[int]					       =   (
				    (BioGptModel, BioGptForCausalLM, BioGptForSequenceClassification, BioGptForTokenClassification)
				    if is_torch_available()
				    else ()
				)
				__UpperCamelCase      :						str					       =   (BioGptForCausalLM,) if is_torch_available() else ()
				__UpperCamelCase      :						Optional[Any]					       =   (
				    {
				        '''feature-extraction''': BioGptModel,
				        '''text-classification''': BioGptForSequenceClassification,
				        '''text-generation''': BioGptForCausalLM,
				        '''token-classification''': BioGptForTokenClassification,
				        '''zero-shot''': BioGptForSequenceClassification,
				    }
				    if is_torch_available()
				    else {}
				)
				__UpperCamelCase      :						Optional[Any]					       =   False



				def 	__magic_name__    (self    )					->		List[str]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       List[Any]           =   BioGptModelTester(self    )
											SCREAMING_SNAKE_CASE__						:       Any           =   ConfigTester(self   ,       config_class=_A   ,       hidden_size=37    )



				def 	__magic_name__    (self    )					->		Any:



											"""simple docstring"""




											self.config_tester.run_common_tests()



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



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       str           =   self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*_A    )



				def 	__magic_name__    (self    )					->		Optional[Any]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   self.model_tester.prepare_config_and_inputs()
											for type in ["absolute", "relative_key", "relative_key_query"]:
																		SCREAMING_SNAKE_CASE__						:       Any           =   type
																		self.model_tester.create_and_check_model(*_A    )



				def 	__magic_name__    (self    )					->		Optional[Any]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       Dict           =   self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_biogpt_model_attention_mask_past(*_A    )



				def 	__magic_name__    (self    )					->		str:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       Any           =   self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_forward_and_backwards(*_A   ,       gradient_checkpointing=_A    )



				def 	__magic_name__    (self    )					->		List[Any]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_biogpt_model_past_large_inputs(*_A    )



				def 	__magic_name__    (self    )					->		Optional[Any]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       int           =   self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_biogpt_weight_initialization(*_A    )



				def 	__magic_name__    (self    )					->		Dict:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       int           =   self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_biogpt_for_token_classification(*_A    )



				@slow
				def 	__magic_name__    (self    )					->		str:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       List[Any]           =   BioGptForCausalLM.from_pretrained("""microsoft/biogpt"""    )
											model.to(_A    )
											SCREAMING_SNAKE_CASE__						:       Dict           =   BioGptTokenizer.from_pretrained("""microsoft/biogpt"""    )

											SCREAMING_SNAKE_CASE__						:       str           =   '''left'''

											# Define PAD Token = EOS Token = 50256
											SCREAMING_SNAKE_CASE__						:       Tuple           =   tokenizer.eos_token
											SCREAMING_SNAKE_CASE__						:       str           =   model.config.eos_token_id

											# use different length sentences to test batching
											SCREAMING_SNAKE_CASE__						:       List[str]           =   [
											    '''Hello, my dog is a little''',
											    '''Today, I''',
											]

											SCREAMING_SNAKE_CASE__						:       List[Any]           =   tokenizer(_A   ,       return_tensors="""pt"""   ,       padding=_A    )
											SCREAMING_SNAKE_CASE__						:       Dict           =   inputs['''input_ids'''].to(_A    )

											SCREAMING_SNAKE_CASE__						:       List[str]           =   model.generate(
											    input_ids=_A   ,       attention_mask=inputs["""attention_mask"""].to(_A    )   ,       )

											SCREAMING_SNAKE_CASE__						:       List[str]           =   tokenizer(sentences[0]   ,       return_tensors="""pt"""    ).input_ids.to(_A    )
											SCREAMING_SNAKE_CASE__						:       List[str]           =   model.generate(input_ids=_A    )

											SCREAMING_SNAKE_CASE__						:       List[Any]           =   inputs_non_padded.shape[-1] - inputs['''attention_mask'''][-1].long().sum().cpu().item()
											SCREAMING_SNAKE_CASE__						:       Optional[Any]           =   tokenizer(sentences[1]   ,       return_tensors="""pt"""    ).input_ids.to(_A    )
											SCREAMING_SNAKE_CASE__						:       Optional[Any]           =   model.generate(input_ids=_A   ,       max_length=model.config.max_length - num_paddings    )

											SCREAMING_SNAKE_CASE__						:       List[Any]           =   tokenizer.batch_decode(_A   ,       skip_special_tokens=_A    )
											SCREAMING_SNAKE_CASE__						:       List[str]           =   tokenizer.decode(output_non_padded[0]   ,       skip_special_tokens=_A    )
											SCREAMING_SNAKE_CASE__						:       Tuple           =   tokenizer.decode(output_padded[0]   ,       skip_special_tokens=_A    )

											SCREAMING_SNAKE_CASE__						:       List[str]           =   [
											    '''Hello, my dog is a little bit bigger than a little bit.''',
											    '''Today, I have a good idea of how to use the information''',
											]
											self.assertListEqual(_A   ,       _A    )
											self.assertListEqual(_A   ,       [non_padded_sentence, padded_sentence]    )



				@slow
				def 	__magic_name__    (self    )					->		str:



											"""simple docstring"""




											for model_name in BIOGPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																		SCREAMING_SNAKE_CASE__						:       str           =   BioGptModel.from_pretrained(_A    )
																		self.assertIsNotNone(_A    )



				def 	__magic_name__    (self    )					->		Optional[Any]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       Dict           =   self.model_tester.prepare_config_and_inputs_for_common()
											SCREAMING_SNAKE_CASE__						:       Dict           =   3
											SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   input_dict['''input_ids''']
											SCREAMING_SNAKE_CASE__						:       Dict           =   input_ids.ne(1    ).to(_A    )
											SCREAMING_SNAKE_CASE__						:       Optional[int]           =   ids_tensor([self.model_tester.batch_size]   ,       self.model_tester.type_sequence_label_size    )
											SCREAMING_SNAKE_CASE__						:       Optional[Any]           =   BioGptForSequenceClassification(_A    )
											model.to(_A    )
											model.eval()
											SCREAMING_SNAKE_CASE__						:       int           =   model(_A   ,       attention_mask=_A   ,       labels=_A    )
											self.assertEqual(result.logits.shape   ,       (self.model_tester.batch_size, self.model_tester.num_labels)    )



				def 	__magic_name__    (self    )					->		Any:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       int           =   self.model_tester.prepare_config_and_inputs_for_common()
											SCREAMING_SNAKE_CASE__						:       Optional[Any]           =   3
											SCREAMING_SNAKE_CASE__						:       Dict           =   '''multi_label_classification'''
											SCREAMING_SNAKE_CASE__						:       str           =   input_dict['''input_ids''']
											SCREAMING_SNAKE_CASE__						:       int           =   input_ids.ne(1    ).to(_A    )
											SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   ids_tensor(
											    [self.model_tester.batch_size, config.num_labels]   ,       self.model_tester.type_sequence_label_size    ).to(torch.float    )
											SCREAMING_SNAKE_CASE__						:       List[Any]           =   BioGptForSequenceClassification(_A    )
											model.to(_A    )
											model.eval()
											SCREAMING_SNAKE_CASE__						:       int           =   model(_A   ,       attention_mask=_A   ,       labels=_A    )
											self.assertEqual(result.logits.shape   ,       (self.model_tester.batch_size, self.model_tester.num_labels)    )






@require_torch
class 	lowerCAmelCase_						(unittest.TestCase  ):


				"""simple docstring"""



				@slow
				def 	__magic_name__    (self    )					->		Optional[Any]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       int           =   BioGptForCausalLM.from_pretrained("""microsoft/biogpt"""    )
											SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   torch.tensor([[2, 48_05, 9, 6_56, 21]]    )
											SCREAMING_SNAKE_CASE__						:       List[Any]           =   model(_A    )[0]

											SCREAMING_SNAKE_CASE__						:       Tuple           =   4_23_84

											SCREAMING_SNAKE_CASE__						:       Tuple           =   torch.Size((1, 5, vocab_size)    )
											self.assertEqual(output.shape   ,       _A    )

											SCREAMING_SNAKE_CASE__						:       int           =   torch.tensor(
											    [[[-9.5236, -9.8918, 10.45_57], [-11.04_69, -9.6423, 8.1022], [-8.8664, -7.8826, 5.5325]]]    )

											self.assertTrue(torch.allclose(output[:, :3, :3]   ,       _A   ,       atol=1E-4    )    )



				@slow
				def 	__magic_name__    (self    )					->		Optional[Any]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       int           =   BioGptTokenizer.from_pretrained("""microsoft/biogpt"""    )
											SCREAMING_SNAKE_CASE__						:       int           =   BioGptForCausalLM.from_pretrained("""microsoft/biogpt"""    )
											model.to(_A    )

											torch.manual_seed(0    )
											SCREAMING_SNAKE_CASE__						:       Dict           =   tokenizer("""COVID-19 is"""   ,       return_tensors="""pt"""    ).to(_A    )
											SCREAMING_SNAKE_CASE__						:       List[str]           =   model.generate(
											    **_A   ,       min_length=1_00   ,       max_length=10_24   ,       num_beams=5   ,       early_stopping=_A   ,       )
											SCREAMING_SNAKE_CASE__						:       str           =   tokenizer.decode(output_ids[0]   ,       skip_special_tokens=_A    )

											SCREAMING_SNAKE_CASE__						:       Optional[Any]           =   (
											    '''COVID-19 is a global pandemic caused by severe acute respiratory syndrome coronavirus 2 (SARS-CoV-2), the'''
											    ''' causative agent of coronavirus disease 2019 (COVID-19), which has spread to more than 200 countries and'''
											    ''' territories, including the United States (US), Canada, Australia, New Zealand, the United Kingdom (UK),'''
											    ''' and the United States of America (USA), as of March 11, 2020, with more than 800,000 confirmed cases and'''
											    ''' more than 800,000 deaths.'''
											)
											self.assertEqual(_A   ,       _A    )








'''simple docstring'''
from __future__ import annotations

import inspect
import unittest
from math import floor

import numpy as np

from transformers import CvtConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
  import tensorflow as tf

  from transformers import TFCvtForImageClassification, TFCvtModel
  from transformers.models.cvt.modeling_tf_cvt import TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
  from PIL import Image

  from transformers import AutoImageProcessor




class 			lowerCamelCase_   (	__a      ):






      def lowercase_							(					self				:    Union[str, Any] ):


          '''simple docstring'''


          UpperCAmelCase__			:		str													=				self.config_class(**self.inputs_dict )
          self.parent.assertTrue(hasattr(_A						,						'''embed_dim''' ) )
          self.parent.assertTrue(hasattr(_A						,						'''num_heads''' ) )




class 			lowerCamelCase_   :






      def __init__(					self				:    int						,						_A				:    Tuple						,						_A				:    Any=13						,						_A				:    Optional[int]=64						,						_A				:    Optional[Any]=3						,						_A				:    List[str]=[16, 48, 96]						,						_A				:    int=[1, 3, 6]						,						_A				:    Optional[int]=[1, 2, 10]						,						_A				:    int=[7, 3, 3]						,						_A				:    Union[str, Any]=[4, 2, 2]						,						_A				:    Dict=[2, 1, 1]						,						_A				:    Optional[Any]=[2, 2, 2]						,						_A				:    Optional[Any]=[False, False, True]						,						_A				:    List[Any]=[0.0, 0.0, 0.0]						,						_A				:    str=0.0_2						,						_A				:    Tuple=1e-12						,						_A				:    Union[str, Any]=True						,						_A				:    Optional[Any]=True						,						_A				:    Optional[int]=2						,						):


          '''simple docstring'''


          UpperCAmelCase__			:		Dict													=				parent
          UpperCAmelCase__			:		List[str]													=				batch_size
          UpperCAmelCase__			:		Optional[int]													=				image_size
          UpperCAmelCase__			:		List[str]													=				patch_sizes
          UpperCAmelCase__			:		Any													=				patch_stride
          UpperCAmelCase__			:		Tuple													=				patch_padding
          UpperCAmelCase__			:		int													=				is_training
          UpperCAmelCase__			:		Dict													=				use_labels
          UpperCAmelCase__			:		List[Any]													=				num_labels
          UpperCAmelCase__			:		Optional[Any]													=				num_channels
          UpperCAmelCase__			:		Optional[int]													=				embed_dim
          UpperCAmelCase__			:		int													=				num_heads
          UpperCAmelCase__			:		Any													=				stride_kv
          UpperCAmelCase__			:		str													=				depth
          UpperCAmelCase__			:		List[Any]													=				cls_token
          UpperCAmelCase__			:		List[Any]													=				attention_drop_rate
          UpperCAmelCase__			:		Optional[int]													=				initializer_range
          UpperCAmelCase__			:		Optional[int]													=				layer_norm_eps






      def lowercase_							(					self				:    Optional[int] ):


          '''simple docstring'''


          UpperCAmelCase__			:		int													=				floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] )

          UpperCAmelCase__			:		Any													=				None
          if self.use_labels:
              # create a random int32 tensor of given shape
              UpperCAmelCase__			:		List[Any]													=				ids_tensor([self.batch_size]						,						self.num_labels )

          UpperCAmelCase__			:		List[Any]													=				self.get_config()
          return config, pixel_values, labels






      def lowercase_							(					self				:    Any ):


          '''simple docstring'''


          return CvtConfig(
              image_size=self.image_size						,						num_labels=self.num_labels						,						num_channels=self.num_channels						,						embed_dim=self.embed_dim						,						num_heads=self.num_heads						,						patch_sizes=self.patch_sizes						,						patch_padding=self.patch_padding						,						patch_stride=self.patch_stride						,						stride_kv=self.stride_kv						,						depth=self.depth						,						cls_token=self.cls_token						,						attention_drop_rate=self.attention_drop_rate						,						initializer_range=self.initializer_range						,						)






      def lowercase_							(					self				:    Optional[int]						,						_A				:    List[Any]						,						_A				:    Tuple						,						_A				:    Dict ):


          '''simple docstring'''


          UpperCAmelCase__			:		Optional[int]													=				TFCvtModel(config=_A )
          UpperCAmelCase__			:		List[str]													=				model(_A						,						training=_A )
          UpperCAmelCase__			:		int													=				(self.image_size, self.image_size)
          UpperCAmelCase__						,						UpperCAmelCase__			:		Optional[int]													=				image_size[0], image_size[1]
          for i in range(len(self.depth ) ):
              UpperCAmelCase__			:		Union[str, Any]													=				floor(((height + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1 )
              UpperCAmelCase__			:		Optional[Any]													=				floor(((width + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1 )
          self.parent.assertEqual(result.last_hidden_state.shape						,						(self.batch_size, self.embed_dim[-1], height, width) )






      def lowercase_							(					self				:    Optional[Any]						,						_A				:    Optional[Any]						,						_A				:    List[Any]						,						_A				:    int ):


          '''simple docstring'''


          UpperCAmelCase__			:		str													=				self.num_labels
          UpperCAmelCase__			:		Union[str, Any]													=				TFCvtForImageClassification(_A )
          UpperCAmelCase__			:		Any													=				model(_A						,						labels=_A						,						training=_A )
          self.parent.assertEqual(result.logits.shape						,						(self.batch_size, self.num_labels) )






      def lowercase_							(					self				:    Optional[int] ):


          '''simple docstring'''


          UpperCAmelCase__			:		List[Any]													=				self.prepare_config_and_inputs()
          UpperCAmelCase__						,						UpperCAmelCase__						,						UpperCAmelCase__			:		Dict													=				config_and_inputs
          UpperCAmelCase__			:		Optional[Any]													=				{'''pixel_values''': pixel_values}
          return config, inputs_dict




@require_tf
class 			lowerCamelCase_   (	__a     ,       __a     ,       unittest.TestCase      ):
      lowerCAmelCase__		     =     (TFCvtModel, TFCvtForImageClassification) if is_tf_available() else ()
      lowerCAmelCase__		     =     (
          {'feature-extraction': TFCvtModel, 'image-classification': TFCvtForImageClassification}
          if is_tf_available()
          else {}
      )
      lowerCAmelCase__		     =     False
      lowerCAmelCase__		     =     False
      lowerCAmelCase__		     =     False
      lowerCAmelCase__		     =     False
      lowerCAmelCase__		     =     False






      def lowercase_							(					self				:    Any ):


          '''simple docstring'''


          UpperCAmelCase__			:		Union[str, Any]													=				TFCvtModelTester(self )
          UpperCAmelCase__			:		Tuple													=				TFCvtConfigTester(self						,						config_class=_A						,						has_text_modality=_A						,						hidden_size=37 )






      def lowercase_							(					self				:    Any ):


          '''simple docstring'''


          self.config_tester.create_and_test_config_common_properties()
          self.config_tester.create_and_test_config_to_json_string()
          self.config_tester.create_and_test_config_to_json_file()
          self.config_tester.create_and_test_config_from_and_save_pretrained()
          self.config_tester.create_and_test_config_with_num_labels()
          self.config_tester.check_config_can_be_init_without_params()
          self.config_tester.check_config_arguments_init()






      @unittest.skip(reason='''Cvt does not output attentions''' )
      def lowercase_							(					self				:    Any ):


          '''simple docstring'''


          pass






      @unittest.skip(reason='''Cvt does not use inputs_embeds''' )
      def lowercase_							(					self				:    str ):


          '''simple docstring'''


          pass






      @unittest.skip(reason='''Cvt does not support input and output embeddings''' )
      def lowercase_							(					self				:    Optional[Any] ):


          '''simple docstring'''


          pass






      @unittest.skipIf(
          not is_tf_available() or len(tf.config.list_physical_devices('''GPU''' ) ) == 0						,						reason='''TF does not support backprop for grouped convolutions on CPU.'''						,						)
      def lowercase_							(					self				:    List[str] ):


          '''simple docstring'''


          super().test_dataset_conversion()






      @unittest.skipIf(
          not is_tf_available() or len(tf.config.list_physical_devices('''GPU''' ) ) == 0						,						reason='''TF does not support backprop for grouped convolutions on CPU.'''						,						)
      @slow
      def lowercase_							(					self				:    Optional[int] ):


          '''simple docstring'''


          super().test_keras_fit()






      @unittest.skip(reason='''Get `Failed to determine best cudnn convolution algo.` error after using TF 2.12+cuda 11.8''' )
      def lowercase_							(					self				:    Union[str, Any] ):


          '''simple docstring'''


          UpperCAmelCase__			:		List[Any]													=				tf.keras.mixed_precision.Policy('''mixed_float16''' )
          tf.keras.mixed_precision.set_global_policy(_A )
          super().test_keras_fit()
          tf.keras.mixed_precision.set_global_policy('''float32''' )






      def lowercase_							(					self				:    Optional[Any] ):


          '''simple docstring'''


          UpperCAmelCase__						,						UpperCAmelCase__			:		str													=				self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
              UpperCAmelCase__			:		str													=				model_class(_A )
              UpperCAmelCase__			:		Optional[int]													=				inspect.signature(model.call )
              # signature.parameters is an OrderedDict => so arg_names order is deterministic
              UpperCAmelCase__			:		List[Any]													=				[*signature.parameters.keys()]

              UpperCAmelCase__			:		Any													=				['''pixel_values''']
              self.assertListEqual(arg_names[:1]						,						_A )






      def lowercase_							(					self				:    Any ):


          '''simple docstring'''


          def check_hidden_states_output(_A				:    Dict						,						_A				:    Optional[Any]						,						_A				:    Dict ):
              UpperCAmelCase__			:		str													=				model_class(_A )

              UpperCAmelCase__			:		List[str]													=				model(**self._prepare_for_class(_A						,						_A ) )
              UpperCAmelCase__			:		Tuple													=				outputs.hidden_states

              UpperCAmelCase__			:		int													=				len(self.model_tester.depth )
              self.assertEqual(len(_A )						,						_A )

              # verify the first hidden states (first block)
              self.assertListEqual(
                  list(hidden_states[0].shape[-3:] )						,						[
                      self.model_tester.embed_dim[0],
                      self.model_tester.image_size // 4,
                      self.model_tester.image_size // 4,
                  ]						,						)

          UpperCAmelCase__						,						UpperCAmelCase__			:		int													=				self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
              UpperCAmelCase__			:		Tuple													=				True
              check_hidden_states_output(_A						,						_A						,						_A )

              # check that output_hidden_states also work using config
              del inputs_dict["output_hidden_states"]
              UpperCAmelCase__			:		List[str]													=				True

              check_hidden_states_output(_A						,						_A						,						_A )






      def lowercase_							(					self				:    Tuple ):


          '''simple docstring'''


          UpperCAmelCase__			:		Union[str, Any]													=				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*_A )






      def lowercase_							(					self				:    str ):


          '''simple docstring'''


          UpperCAmelCase__			:		Optional[Any]													=				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_image_classification(*_A )






      @slow
      def lowercase_							(					self				:    Optional[Any] ):


          '''simple docstring'''


          for model_name in TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              UpperCAmelCase__			:		Optional[int]													=				TFCvtModel.from_pretrained(_A )
              self.assertIsNotNone(_A )





def 					a__       (					)    ->	Any:
    UpperCAmelCase__			:		str													=				Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''						)
    return image







@require_tf
@require_vision
class 			lowerCamelCase_   (	unittest.TestCase      ):






      @cached_property
      def lowercase_							(					self				:    Union[str, Any] ):


          '''simple docstring'''


          return AutoImageProcessor.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0] )






      @slow
      def lowercase_							(					self				:    Any ):


          '''simple docstring'''


          UpperCAmelCase__			:		Dict													=				TFCvtForImageClassification.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0] )

          UpperCAmelCase__			:		Union[str, Any]													=				self.default_image_processor
          UpperCAmelCase__			:		Optional[Any]													=				prepare_img()
          UpperCAmelCase__			:		Tuple													=				image_processor(images=_A						,						return_tensors='''tf''' )

          # forward pass
          UpperCAmelCase__			:		Optional[Any]													=				model(**_A )

          # verify the logits
          UpperCAmelCase__			:		Union[str, Any]													=				tf.TensorShape((1, 1_000) )
          self.assertEqual(outputs.logits.shape						,						_A )

          UpperCAmelCase__			:		Union[str, Any]													=				tf.constant([0.9_2_8_5, 0.9_0_1_5, -0.3_1_5_0] )
          self.assertTrue(np.allclose(outputs.logits[0, :3].numpy()						,						_A						,						atol=1e-4 ) )
