"""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 						__UpperCAmelCase    :







			'''simple docstring'''





			def __init__(			self   ,      snake_case_   ,      snake_case_=13   ,      snake_case_=7   ,      snake_case_=True   ,      snake_case_=True   ,      snake_case_=False   ,      snake_case_=True   ,      snake_case_=99   ,      snake_case_=32   ,      snake_case_=5   ,      snake_case_=4   ,      snake_case_=37   ,      snake_case_="gelu"   ,      snake_case_=0.1   ,      snake_case_=0.1   ,      snake_case_=512   ,      snake_case_=16   ,      snake_case_=2   ,      snake_case_=0.02   ,      snake_case_=3   ,      snake_case_=4   ,      snake_case_=None   ,      ):







										'''simple docstring'''


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





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


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

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

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

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

										A__       :     Any								=			self.get_config()

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





			def 		lowerCamelCase					(			self							):







										'''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=snake_case_   ,      initializer_range=self.initializer_range   ,      )





			def 		lowerCamelCase					(			self   ,      snake_case_   ,      snake_case_   ,      snake_case_   ,      snake_case_   ,      snake_case_   ,      snake_case_   ,      snake_case_							):







										'''simple docstring'''


										A__       :     Optional[int]								=			BioGptModel(config=snake_case_							)
										model.to(snake_case_							)
										model.eval()
										A__       :     List[Any]								=			model(snake_case_   ,      attention_mask=snake_case_							)
										A__       :     int								=			model(snake_case_							)
										self.parent.assertEqual(result.last_hidden_state.shape   ,      (self.batch_size, self.seq_length, self.hidden_size)							)





			def 		lowerCamelCase					(			self   ,      snake_case_   ,      snake_case_   ,      snake_case_   ,      snake_case_   ,      snake_case_   ,      snake_case_   ,      snake_case_   ,      snake_case_   ,      snake_case_   ,      ):







										'''simple docstring'''


										A__       :     Optional[Any]								=			BioGptForCausalLM(config=snake_case_							)
										model.to(snake_case_							)
										model.eval()
										A__       :     Union[str, Any]								=			model(snake_case_   ,      attention_mask=snake_case_   ,      token_type_ids=snake_case_   ,      labels=snake_case_							)
										self.parent.assertEqual(result.logits.shape   ,      (self.batch_size, self.seq_length, self.vocab_size)							)





			def 		lowerCamelCase					(			self   ,      snake_case_   ,      snake_case_   ,      snake_case_   ,      snake_case_   ,      snake_case_   ,      *snake_case_							):







										'''simple docstring'''


										A__       :     Dict								=			BioGptModel(config=snake_case_							)
										model.to(snake_case_							)
										model.eval()

										# create attention mask
										A__       :     Any								=			torch.ones(input_ids.shape   ,      dtype=torch.long   ,      device=snake_case_							)
										A__       :     Union[str, Any]								=			self.seq_length // 2
										A__       :     Optional[int]								=			0

										# first forward pass
										A__				,							A__       :     Union[str, Any]								=			model(snake_case_   ,      attention_mask=snake_case_							).to_tuple()

										# create hypothetical next token and extent to next_input_ids
										A__       :     List[str]								=			ids_tensor((self.batch_size, 1)   ,      config.vocab_size							)

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

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

										# get two different outputs
										A__       :     Union[str, Any]								=			model(snake_case_   ,      attention_mask=snake_case_							)["""last_hidden_state"""]
										A__       :     List[Any]								=			model(snake_case_   ,      past_key_values=snake_case_   ,      attention_mask=snake_case_							)["""last_hidden_state"""]

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

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





			def 		lowerCamelCase					(			self   ,      snake_case_   ,      snake_case_   ,      snake_case_   ,      snake_case_   ,      snake_case_   ,      *snake_case_							):







										'''simple docstring'''


										A__       :     Tuple								=			BioGptModel(config=snake_case_							).to(snake_case_							).eval()

										A__       :     Any								=			torch.ones(input_ids.shape   ,      dtype=torch.long   ,      device=snake_case_							)

										# first forward pass
										A__       :     Any								=			model(snake_case_   ,      attention_mask=snake_case_   ,      use_cache=snake_case_							)

										A__				,							A__       :     Optional[Any]								=			outputs.to_tuple()

										# create hypothetical multiple next token and extent to next_input_ids
										A__       :     str								=			ids_tensor((self.batch_size, 3)   ,      config.vocab_size							)
										A__       :     Union[str, Any]								=			ids_tensor((self.batch_size, 3)   ,      2							)

										# append to next input_ids and
										A__       :     Optional[Any]								=			torch.cat([input_ids, next_tokens]   ,      dim=-1							)
										A__       :     Dict								=			torch.cat([attention_mask, next_attn_mask]   ,      dim=-1							)

										A__       :     int								=			model(snake_case_   ,      attention_mask=snake_case_							)["""last_hidden_state"""]
										A__       :     Optional[int]								=			model(snake_case_   ,      attention_mask=snake_case_   ,      past_key_values=snake_case_							)[
										    """last_hidden_state"""
										]

										# select random slice
										A__       :     Tuple								=			ids_tensor((1,)   ,      output_from_past.shape[-1]							).item()
										A__       :     List[str]								=			output_from_no_past[:, -3:, random_slice_idx].detach()
										A__       :     Union[str, Any]								=			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(snake_case_   ,      snake_case_   ,      atol=1E-3							)							)





			def 		lowerCamelCase					(			self   ,      snake_case_   ,      snake_case_   ,      snake_case_   ,      snake_case_   ,      snake_case_   ,      *snake_case_   ,      snake_case_=False							):







										'''simple docstring'''


										A__       :     int								=			BioGptForCausalLM(snake_case_							)
										model.to(snake_case_							)
										if gradient_checkpointing:
																	model.gradient_checkpointing_enable()

										A__       :     Dict								=			model(snake_case_   ,      labels=snake_case_							)
										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 		lowerCamelCase					(			self   ,      snake_case_   ,      *snake_case_							):







										'''simple docstring'''


										A__       :     Union[str, Any]								=			BioGptModel(snake_case_							)
										A__       :     List[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.0_01							)
																								self.parent.assertLessEqual(abs(torch.mean(model.state_dict()[key]							) - 0.0							)   ,      0.01							)





			def 		lowerCamelCase					(			self   ,      snake_case_   ,      snake_case_   ,      snake_case_   ,      snake_case_   ,      snake_case_   ,      *snake_case_							):







										'''simple docstring'''


										A__       :     List[str]								=			self.num_labels
										A__       :     Dict								=			BioGptForTokenClassification(snake_case_							)
										model.to(snake_case_							)
										model.eval()
										A__       :     List[Any]								=			model(snake_case_   ,      attention_mask=snake_case_   ,      token_type_ids=snake_case_							)
										self.parent.assertEqual(result.logits.shape   ,      (self.batch_size, self.seq_length, self.num_labels)							)





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										A__       :     List[str]								=			self.prepare_config_and_inputs()
										(
										    (
										    A__
										)				,							(
										    A__
										)				,							(
										    A__
										)				,							(
										    A__
										)				,							(
										    A__
										)				,							(
										    A__
										)				,							(
										    A__
										)				,							
										)       :     Union[str, Any]								=			config_and_inputs
										A__       :     Union[str, Any]								=			{"""input_ids""": input_ids, """attention_mask""": input_mask}
										return config, inputs_dict





@require_torch
class 						__UpperCAmelCase    (__A							,   __A							,   __A							,   unittest.TestCase     ):







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





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										A__       :     List[str]								=			BioGptModelTester(self							)
										A__       :     str								=			ConfigTester(self   ,      config_class=snake_case_   ,      hidden_size=37							)





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										self.config_tester.run_common_tests()





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										A__       :     Any								=			self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*snake_case_							)





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


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





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										A__       :     Union[str, Any]								=			self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_biogpt_model_attention_mask_past(*snake_case_							)





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										A__       :     Union[str, Any]								=			self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_forward_and_backwards(*snake_case_   ,      gradient_checkpointing=snake_case_							)





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										A__       :     Optional[int]								=			self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_biogpt_model_past_large_inputs(*snake_case_							)





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										A__       :     Tuple								=			self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_biogpt_weight_initialization(*snake_case_							)





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										A__       :     Optional[int]								=			self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_biogpt_for_token_classification(*snake_case_							)





			@slow
			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										A__       :     List[Any]								=			BioGptForCausalLM.from_pretrained("""microsoft/biogpt"""							)
										model.to(snake_case_							)
										A__       :     Union[str, Any]								=			BioGptTokenizer.from_pretrained("""microsoft/biogpt"""							)

										A__       :     Dict								=			"""left"""

										# Define PAD Token = EOS Token = 50256
										A__       :     Dict								=			tokenizer.eos_token
										A__       :     str								=			model.config.eos_token_id

										# use different length sentences to test batching
										A__       :     Dict								=			[
										    """Hello, my dog is a little""",
										    """Today, I""",
										]

										A__       :     Dict								=			tokenizer(snake_case_   ,      return_tensors="""pt"""   ,      padding=snake_case_							)
										A__       :     Union[str, Any]								=			inputs["""input_ids"""].to(snake_case_							)

										A__       :     Dict								=			model.generate(
										    input_ids=snake_case_   ,      attention_mask=inputs["""attention_mask"""].to(snake_case_							)   ,      )

										A__       :     Optional[int]								=			tokenizer(sentences[0]   ,      return_tensors="""pt"""							).input_ids.to(snake_case_							)
										A__       :     Any								=			model.generate(input_ids=snake_case_							)

										A__       :     Tuple								=			inputs_non_padded.shape[-1] - inputs["""attention_mask"""][-1].long().sum().cpu().item()
										A__       :     List[str]								=			tokenizer(sentences[1]   ,      return_tensors="""pt"""							).input_ids.to(snake_case_							)
										A__       :     Optional[int]								=			model.generate(input_ids=snake_case_   ,      max_length=model.config.max_length - num_paddings							)

										A__       :     Tuple								=			tokenizer.batch_decode(snake_case_   ,      skip_special_tokens=snake_case_							)
										A__       :     Union[str, Any]								=			tokenizer.decode(output_non_padded[0]   ,      skip_special_tokens=snake_case_							)
										A__       :     Dict								=			tokenizer.decode(output_padded[0]   ,      skip_special_tokens=snake_case_							)

										A__       :     Tuple								=			[
										    """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(snake_case_   ,      snake_case_							)
										self.assertListEqual(snake_case_   ,      [non_padded_sentence, padded_sentence]							)





			@slow
			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										for model_name in BIOGPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																	A__       :     Union[str, Any]								=			BioGptModel.from_pretrained(snake_case_							)
																	self.assertIsNotNone(snake_case_							)





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										A__				,							A__       :     List[Any]								=			self.model_tester.prepare_config_and_inputs_for_common()
										A__       :     Dict								=			3
										A__       :     Any								=			input_dict["""input_ids"""]
										A__       :     Union[str, Any]								=			input_ids.ne(1							).to(snake_case_							)
										A__       :     str								=			ids_tensor([self.model_tester.batch_size]   ,      self.model_tester.type_sequence_label_size							)
										A__       :     str								=			BioGptForSequenceClassification(snake_case_							)
										model.to(snake_case_							)
										model.eval()
										A__       :     Any								=			model(snake_case_   ,      attention_mask=snake_case_   ,      labels=snake_case_							)
										self.assertEqual(result.logits.shape   ,      (self.model_tester.batch_size, self.model_tester.num_labels)							)





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


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





@require_torch
class 						__UpperCAmelCase    (unittest.TestCase     ):







			'''simple docstring'''





			@slow
			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										A__       :     Tuple								=			BioGptForCausalLM.from_pretrained("""microsoft/biogpt"""							)
										A__       :     Tuple								=			torch.tensor([[2, 4_805, 9, 656, 21]]							)
										A__       :     str								=			model(snake_case_							)[0]

										A__       :     Union[str, Any]								=			42_384

										A__       :     Any								=			torch.Size((1, 5, vocab_size)							)
										self.assertEqual(output.shape   ,      snake_case_							)

										A__       :     Optional[int]								=			torch.tensor(
										    [[[-9.52_36, -9.89_18, 10.45_57], [-11.04_69, -9.64_23, 8.10_22], [-8.86_64, -7.88_26, 5.53_25]]]							)

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





			@slow
			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										A__       :     int								=			BioGptTokenizer.from_pretrained("""microsoft/biogpt"""							)
										A__       :     str								=			BioGptForCausalLM.from_pretrained("""microsoft/biogpt"""							)
										model.to(snake_case_							)

										torch.manual_seed(0							)
										A__       :     int								=			tokenizer("""COVID-19 is"""   ,      return_tensors="""pt"""							).to(snake_case_							)
										A__       :     Union[str, Any]								=			model.generate(
										    **snake_case_   ,      min_length=100   ,      max_length=1_024   ,      num_beams=5   ,      early_stopping=snake_case_   ,      )
										A__       :     Tuple								=			tokenizer.decode(output_ids[0]   ,      skip_special_tokens=snake_case_							)

										A__       :     List[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(snake_case_   ,      snake_case_							)






"""simple docstring"""



import unittest

from transformers import load_tool

from .test_tools_common import ToolTesterMixin
class 						__UpperCAmelCase    (unittest.TestCase							,   __A     ):







			'''simple docstring'''





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										A__       :     List[Any]								=			load_tool("""text-classification"""							)
										self.tool.setup()
										A__       :     Any								=			load_tool("""text-classification"""   ,      remote=snake_case_							)





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										A__       :     int								=			self.tool("""That's quite cool"""   ,      ["""positive""", """negative"""]							)
										self.assertEqual(snake_case_   ,      """positive"""							)





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										A__       :     Optional[Any]								=			self.remote_tool("""That's quite cool"""   ,      ["""positive""", """negative"""]							)
										self.assertEqual(snake_case_   ,      """positive"""							)





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										A__       :     str								=			self.tool(text="""That's quite cool"""   ,      labels=["""positive""", """negative"""]							)
										self.assertEqual(snake_case_   ,      """positive"""							)





			def 		lowerCamelCase					(			self							):







										'''simple docstring'''


										A__       :     int								=			self.remote_tool(text="""That's quite cool"""   ,      labels=["""positive""", """negative"""]							)
										self.assertEqual(snake_case_   ,      """positive"""							)





