




'''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    __lowercase						:







   def __init__(	self      ,							UpperCamelCase      ,							UpperCamelCase=13      ,							UpperCamelCase=7      ,							UpperCamelCase=True      ,							UpperCamelCase=True      ,							UpperCamelCase=False      ,							UpperCamelCase=True      ,							UpperCamelCase=99      ,							UpperCamelCase=32      ,							UpperCamelCase=5      ,							UpperCamelCase=4      ,							UpperCamelCase=37      ,							UpperCamelCase="gelu"      ,							UpperCamelCase=0.1      ,							UpperCamelCase=0.1      ,							UpperCamelCase=512      ,							UpperCamelCase=16      ,							UpperCamelCase=2      ,							UpperCamelCase=0.02      ,							UpperCamelCase=3      ,							UpperCamelCase=4      ,							UpperCamelCase=None      ,							)				->    Optional[Any]:
     __a			=					parent
     __a			=					batch_size
     __a			=					seq_length
     __a			=					is_training
     __a			=					use_input_mask
     __a			=					use_token_type_ids
     __a			=					use_labels
     __a			=					vocab_size
     __a			=					hidden_size
     __a			=					num_hidden_layers
     __a			=					num_attention_heads
     __a			=					intermediate_size
     __a			=					hidden_act
     __a			=					hidden_dropout_prob
     __a			=					attention_probs_dropout_prob
     __a			=					max_position_embeddings
     __a			=					type_vocab_size
     __a			=					type_sequence_label_size
     __a			=					initializer_range
     __a			=					num_labels
     __a			=					num_choices
     __a			=					scope







   def 		UpperCamelCase__			(	self      )				->    Union[str, Any]:
     __a			=					ids_tensor([self.batch_size, self.seq_length]      ,							self.vocab_size      )

     __a			=					None
     if self.use_input_mask:
       __a			=					random_attention_mask([self.batch_size, self.seq_length]      )

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

     __a			=					None
     __a			=					None
     __a			=					None
     if self.use_labels:
       __a			=					ids_tensor([self.batch_size]      ,							self.type_sequence_label_size      )
       __a			=					ids_tensor([self.batch_size, self.seq_length]      ,							self.num_labels      )
       __a			=					ids_tensor([self.batch_size]      ,							self.num_choices      )

     __a			=					self.get_config()

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







   def 		UpperCamelCase__			(	self      )				->    Tuple:
     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=UpperCamelCase      ,							initializer_range=self.initializer_range      ,							)







   def 		UpperCamelCase__			(	self      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      )				->    List[Any]:
     __a			=					BioGptModel(config=UpperCamelCase      )
     model.to(UpperCamelCase      )
     model.eval()
     __a			=					model(UpperCamelCase      ,							attention_mask=UpperCamelCase      )
     __a			=					model(UpperCamelCase      )
     self.parent.assertEqual(result.last_hidden_state.shape      ,							(self.batch_size, self.seq_length, self.hidden_size)      )







   def 		UpperCamelCase__			(	self      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							)				->    List[str]:
     __a			=					BioGptForCausalLM(config=UpperCamelCase      )
     model.to(UpperCamelCase      )
     model.eval()
     __a			=					model(UpperCamelCase      ,							attention_mask=UpperCamelCase      ,							token_type_ids=UpperCamelCase      ,							labels=UpperCamelCase      )
     self.parent.assertEqual(result.logits.shape      ,							(self.batch_size, self.seq_length, self.vocab_size)      )







   def 		UpperCamelCase__			(	self      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							*UpperCamelCase      )				->    Tuple:
     __a			=					BioGptModel(config=UpperCamelCase      )
     model.to(UpperCamelCase      )
     model.eval()

     # create attention mask
     __a			=					torch.ones(input_ids.shape      ,							dtype=torch.long      ,							device=UpperCamelCase      )
     __a			=					self.seq_length // 2
     __a			=					0

     # first forward pass
     __a	,       __a			=					model(UpperCamelCase      ,							attention_mask=UpperCamelCase      ).to_tuple()

     # create hypothetical next token and extent to next_input_ids
     __a			=					ids_tensor((self.batch_size, 1)      ,							config.vocab_size      )

     # change a random masked slice from input_ids
     __a			=					ids_tensor((1,)      ,							UpperCamelCase      ).item() + 1
     __a			=					ids_tensor((self.batch_size, 1)      ,							config.vocab_size      ).squeeze(-1      )
     __a			=					random_other_next_tokens

     # append to next input_ids and attn_mask
     __a			=					torch.cat([input_ids, next_tokens]      ,							dim=-1      )
     __a			=					torch.cat(
         [attn_mask, torch.ones((attn_mask.shape[0], 1)      ,							dtype=torch.long      ,							device=UpperCamelCase      )]      ,							dim=1      ,							)

     # get two different outputs
     __a			=					model(UpperCamelCase      ,							attention_mask=UpperCamelCase      )['last_hidden_state']
     __a			=					model(UpperCamelCase      ,							past_key_values=UpperCamelCase      ,							attention_mask=UpperCamelCase      )['last_hidden_state']

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

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







   def 		UpperCamelCase__			(	self      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							*UpperCamelCase      )				->    int:
     __a			=					BioGptModel(config=UpperCamelCase      ).to(UpperCamelCase      ).eval()

     __a			=					torch.ones(input_ids.shape      ,							dtype=torch.long      ,							device=UpperCamelCase      )

     # first forward pass
     __a			=					model(UpperCamelCase      ,							attention_mask=UpperCamelCase      ,							use_cache=UpperCamelCase      )

     __a	,       __a			=					outputs.to_tuple()

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

     # append to next input_ids and
     __a			=					torch.cat([input_ids, next_tokens]      ,							dim=-1      )
     __a			=					torch.cat([attention_mask, next_attn_mask]      ,							dim=-1      )

     __a			=					model(UpperCamelCase      ,							attention_mask=UpperCamelCase      )['last_hidden_state']
     __a			=					model(UpperCamelCase      ,							attention_mask=UpperCamelCase      ,							past_key_values=UpperCamelCase      )[
         'last_hidden_state'
     ]

     # select random slice
     __a			=					ids_tensor((1,)      ,							output_from_past.shape[-1]      ).item()
     __a			=					output_from_no_past[:, -3:, random_slice_idx].detach()
     __a			=					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(UpperCamelCase      ,							UpperCamelCase      ,							atol=1e-3      )      )







   def 		UpperCamelCase__			(	self      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							*UpperCamelCase      ,							UpperCamelCase=False      )				->    str:
     __a			=					BioGptForCausalLM(UpperCamelCase      )
     model.to(UpperCamelCase      )
     if gradient_checkpointing:
       model.gradient_checkpointing_enable()

     __a			=					model(UpperCamelCase      ,							labels=UpperCamelCase      )
     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 		UpperCamelCase__			(	self      ,							UpperCamelCase      ,							*UpperCamelCase      )				->    int:
     __a			=					BioGptModel(UpperCamelCase      )
     __a			=					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 		UpperCamelCase__			(	self      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      ,							*UpperCamelCase      )				->    Union[str, Any]:
     __a			=					self.num_labels
     __a			=					BioGptForTokenClassification(UpperCamelCase      )
     model.to(UpperCamelCase      )
     model.eval()
     __a			=					model(UpperCamelCase      ,							attention_mask=UpperCamelCase      ,							token_type_ids=UpperCamelCase      )
     self.parent.assertEqual(result.logits.shape      ,							(self.batch_size, self.seq_length, self.num_labels)      )







   def 		UpperCamelCase__			(	self      )				->    Dict:
     __a			=					self.prepare_config_and_inputs()
     (
         (
         __a
     )	,       (
         __a
     )	,       (
         __a
     )	,       (
         __a
     )	,       (
         __a
     )	,       (
         __a
     )	,       (
         __a
     )	,       
     )			=					config_and_inputs
     __a			=					{'input_ids': input_ids, 'attention_mask': input_mask}
     return config, inputs_dict





@require_torch
class    __lowercase						(  __magic_name__       ,							__magic_name__       ,							__magic_name__       ,							unittest.TestCase ):
   _a							=      (
       (BioGptModel, BioGptForCausalLM, BioGptForSequenceClassification, BioGptForTokenClassification)
       if is_torch_available()
       else ()
   )
   _a							=      (BioGptForCausalLM,) if is_torch_available() else ()
   _a							=      (
       {
           """feature-extraction""": BioGptModel,
           """text-classification""": BioGptForSequenceClassification,
           """text-generation""": BioGptForCausalLM,
           """token-classification""": BioGptForTokenClassification,
           """zero-shot""": BioGptForSequenceClassification,
       }
       if is_torch_available()
       else {}
   )
   _a							=      False







   def 		UpperCamelCase__			(	self      )				->    Union[str, Any]:
     __a			=					BioGptModelTester(self      )
     __a			=					ConfigTester(self      ,							config_class=UpperCamelCase      ,							hidden_size=37      )







   def 		UpperCamelCase__			(	self      )				->    str:
     self.config_tester.run_common_tests()







   def 		UpperCamelCase__			(	self      )				->    str:
     __a			=					self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*UpperCamelCase      )







   def 		UpperCamelCase__			(	self      )				->    List[str]:
     __a			=					self.model_tester.prepare_config_and_inputs()
     for type in ["absolute", "relative_key", "relative_key_query"]:
       __a			=					type
       self.model_tester.create_and_check_model(*UpperCamelCase      )







   def 		UpperCamelCase__			(	self      )				->    Tuple:
     __a			=					self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_biogpt_model_attention_mask_past(*UpperCamelCase      )







   def 		UpperCamelCase__			(	self      )				->    Optional[Any]:
     __a			=					self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_forward_and_backwards(*UpperCamelCase      ,							gradient_checkpointing=UpperCamelCase      )







   def 		UpperCamelCase__			(	self      )				->    Optional[int]:
     __a			=					self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_biogpt_model_past_large_inputs(*UpperCamelCase      )







   def 		UpperCamelCase__			(	self      )				->    Dict:
     __a			=					self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_biogpt_weight_initialization(*UpperCamelCase      )







   def 		UpperCamelCase__			(	self      )				->    Dict:
     __a			=					self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_biogpt_for_token_classification(*UpperCamelCase      )







   @slow
   def 		UpperCamelCase__			(	self      )				->    Tuple:
     __a			=					BioGptForCausalLM.from_pretrained('microsoft/biogpt'      )
     model.to(UpperCamelCase      )
     __a			=					BioGptTokenizer.from_pretrained('microsoft/biogpt'      )

     __a			=					'left'

     # Define PAD Token = EOS Token = 50256
     __a			=					tokenizer.eos_token
     __a			=					model.config.eos_token_id

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

     __a			=					tokenizer(UpperCamelCase      ,							return_tensors='pt'      ,							padding=UpperCamelCase      )
     __a			=					inputs['input_ids'].to(UpperCamelCase      )

     __a			=					model.generate(
         input_ids=UpperCamelCase      ,							attention_mask=inputs['attention_mask'].to(UpperCamelCase      )      ,							)

     __a			=					tokenizer(sentences[0]      ,							return_tensors='pt'      ).input_ids.to(UpperCamelCase      )
     __a			=					model.generate(input_ids=UpperCamelCase      )

     __a			=					inputs_non_padded.shape[-1] - inputs['attention_mask'][-1].long().sum().cpu().item()
     __a			=					tokenizer(sentences[1]      ,							return_tensors='pt'      ).input_ids.to(UpperCamelCase      )
     __a			=					model.generate(input_ids=UpperCamelCase      ,							max_length=model.config.max_length - num_paddings      )

     __a			=					tokenizer.batch_decode(UpperCamelCase      ,							skip_special_tokens=UpperCamelCase      )
     __a			=					tokenizer.decode(output_non_padded[0]      ,							skip_special_tokens=UpperCamelCase      )
     __a			=					tokenizer.decode(output_padded[0]      ,							skip_special_tokens=UpperCamelCase      )

     __a			=					[
         '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(UpperCamelCase      ,							UpperCamelCase      )
     self.assertListEqual(UpperCamelCase      ,							[non_padded_sentence, padded_sentence]      )







   @slow
   def 		UpperCamelCase__			(	self      )				->    Tuple:
     for model_name in BIOGPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       __a			=					BioGptModel.from_pretrained(UpperCamelCase      )
       self.assertIsNotNone(UpperCamelCase      )







   def 		UpperCamelCase__			(	self      )				->    Any:
     __a	,       __a			=					self.model_tester.prepare_config_and_inputs_for_common()
     __a			=					3
     __a			=					input_dict['input_ids']
     __a			=					input_ids.ne(1      ).to(UpperCamelCase      )
     __a			=					ids_tensor([self.model_tester.batch_size]      ,							self.model_tester.type_sequence_label_size      )
     __a			=					BioGptForSequenceClassification(UpperCamelCase      )
     model.to(UpperCamelCase      )
     model.eval()
     __a			=					model(UpperCamelCase      ,							attention_mask=UpperCamelCase      ,							labels=UpperCamelCase      )
     self.assertEqual(result.logits.shape      ,							(self.model_tester.batch_size, self.model_tester.num_labels)      )







   def 		UpperCamelCase__			(	self      )				->    Optional[Any]:
     __a	,       __a			=					self.model_tester.prepare_config_and_inputs_for_common()
     __a			=					3
     __a			=					'multi_label_classification'
     __a			=					input_dict['input_ids']
     __a			=					input_ids.ne(1      ).to(UpperCamelCase      )
     __a			=					ids_tensor(
         [self.model_tester.batch_size, config.num_labels]      ,							self.model_tester.type_sequence_label_size      ).to(torch.float      )
     __a			=					BioGptForSequenceClassification(UpperCamelCase      )
     model.to(UpperCamelCase      )
     model.eval()
     __a			=					model(UpperCamelCase      ,							attention_mask=UpperCamelCase      ,							labels=UpperCamelCase      )
     self.assertEqual(result.logits.shape      ,							(self.model_tester.batch_size, self.model_tester.num_labels)      )





@require_torch
class    __lowercase						(  unittest.TestCase ):







   @slow
   def 		UpperCamelCase__			(	self      )				->    Tuple:
     __a			=					BioGptForCausalLM.from_pretrained('microsoft/biogpt'      )
     __a			=					torch.tensor([[2, 4805, 9, 656, 21]]      )
     __a			=					model(UpperCamelCase      )[0]

     __a			=					4_2384

     __a			=					torch.Size((1, 5, vocab_size)      )
     self.assertEqual(output.shape      ,							UpperCamelCase      )

     __a			=					torch.tensor(
         [[[-9.5_236, -9.8_918, 10.4_557], [-11.0_469, -9.6_423, 8.1_022], [-8.8_664, -7.8_826, 5.5_325]]]      )

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







   @slow
   def 		UpperCamelCase__			(	self      )				->    Optional[int]:
     __a			=					BioGptTokenizer.from_pretrained('microsoft/biogpt'      )
     __a			=					BioGptForCausalLM.from_pretrained('microsoft/biogpt'      )
     model.to(UpperCamelCase      )

     torch.manual_seed(0      )
     __a			=					tokenizer('COVID-19 is'      ,							return_tensors='pt'      ).to(UpperCamelCase      )
     __a			=					model.generate(
         **UpperCamelCase      ,							min_length=100      ,							max_length=1024      ,							num_beams=5      ,							early_stopping=UpperCamelCase      ,							)
     __a			=					tokenizer.decode(output_ids[0]      ,							skip_special_tokens=UpperCamelCase      )

     __a			=					(
         '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(UpperCamelCase      ,							UpperCamelCase      )









'''simple docstring'''


import numpy as np

import datasets


UpperCAmelCase_ 		=      "\nCompute the Mahalanobis Distance\n\nMahalonobis distance is the distance between a point and a distribution.\nAnd not between two distinct points. It is effectively a multivariate equivalent of the Euclidean distance.\nIt was introduced by Prof. P. C. Mahalanobis in 1936\nand has been used in various statistical applications ever since\n[source: https://www.machinelearningplus.com/statistics/mahalanobis-distance/]\n"

UpperCAmelCase_ 		=      "\\n@article{de2000mahalanobis,\n  title={The mahalanobis distance},\n  author={De Maesschalck, Roy and Jouan-Rimbaud, Delphine and Massart, D{\'e}sir{\'e} L},\n  journal={Chemometrics and intelligent laboratory systems},\n  volume={50},\n  number={1},\n  pages={1--18},\n  year={2000},\n  publisher={Elsevier}\n}\n"

UpperCAmelCase_ 		=      "\nArgs:\n    X: List of datapoints to be compared with the `reference_distribution`.\n    reference_distribution: List of datapoints from the reference distribution we want to compare to.\nReturns:\n    mahalanobis: The Mahalonobis distance for each datapoint in `X`.\nExamples:\n\n    >>> mahalanobis_metric = datasets.load_metric(\"mahalanobis\")\n    >>> results = mahalanobis_metric.compute(reference_distribution=[[0, 1], [1, 0]], X=[[0, 1]])\n    >>> print(results)\n    {'mahalanobis': array([0.5])}\n"





@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION       ,							_KWARGS_DESCRIPTION )
class    __lowercase						(  datasets.Metric ):







   def 		UpperCamelCase__			(	self      )				->    Optional[Any]:
     return datasets.MetricInfo(
         description=_DESCRIPTION      ,							citation=_CITATION      ,							inputs_description=_KWARGS_DESCRIPTION      ,							features=datasets.Features(
             {
                 'X': datasets.Sequence(datasets.Value('float'      ,							id='sequence'      )      ,							id='X'      ),
             }      )      ,							)







   def 		UpperCamelCase__			(	self      ,							UpperCamelCase      ,							UpperCamelCase      )				->    Optional[int]:
     # convert to numpy arrays
     __a			=					np.array(UpperCamelCase      )
     __a			=					np.array(UpperCamelCase      )

     # Assert that arrays are 2D
     if len(X.shape      ) != 2:
       raise ValueError('Expected `X` to be a 2D vector'      )
     if len(reference_distribution.shape      ) != 2:
       raise ValueError('Expected `reference_distribution` to be a 2D vector'      )
     if reference_distribution.shape[0] < 2:
       raise ValueError(
           'Expected `reference_distribution` to be a 2D vector with more than one element in the first dimension'      )

     # Get mahalanobis distance for each prediction
     __a			=					X - np.mean(UpperCamelCase      )
     __a			=					np.cov(reference_distribution.T      )
     try:
       __a			=					np.linalg.inv(UpperCamelCase      )
     except np.linalg.LinAlgError:
       __a			=					np.linalg.pinv(UpperCamelCase      )
     __a			=					np.dot(UpperCamelCase      ,							UpperCamelCase      )
     __a			=					np.dot(UpperCamelCase      ,							X_minus_mu.T      ).diagonal()

     return {"mahalanobis": mahal_dist}



