



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




  def __init__(			self					:						Optional[Any]						, __lowerCAmelCase					:						Union[str, Any]						, __lowerCAmelCase					:						Union[str, Any]=1_3						, __lowerCAmelCase					:						Any=7						, __lowerCAmelCase					:						Tuple=True						, __lowerCAmelCase					:						int=True						, __lowerCAmelCase					:						str=False						, __lowerCAmelCase					:						Tuple=True						, __lowerCAmelCase					:						str=9_9						, __lowerCAmelCase					:						str=3_2						, __lowerCAmelCase					:						str=5						, __lowerCAmelCase					:						List[str]=4						, __lowerCAmelCase					:						List[Any]=3_7						, __lowerCAmelCase					:						Tuple="gelu"						, __lowerCAmelCase					:						str=0.1						, __lowerCAmelCase					:						Optional[Any]=0.1						, __lowerCAmelCase					:						List[Any]=5_1_2						, __lowerCAmelCase					:						List[Any]=1_6						, __lowerCAmelCase					:						List[str]=2						, __lowerCAmelCase					:						str=0.02						, __lowerCAmelCase					:						List[Any]=3						, __lowerCAmelCase					:						List[Any]=4						, __lowerCAmelCase					:						Dict=None						, ):






         """simple docstring"""






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




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






         """simple docstring"""






         _lowerCamelCase						:    Union[str, Any]         =			ids_tensor([self.batch_size, self.seq_length]						, self.vocab_size		)

         _lowerCamelCase						:    str         =			None
         if self.use_input_mask:
                _lowerCamelCase						:    Tuple         =			random_attention_mask([self.batch_size, self.seq_length]		)

         _lowerCamelCase						:    Optional[int]         =			None
         if self.use_token_type_ids:
                _lowerCamelCase						:    Any         =			ids_tensor([self.batch_size, self.seq_length]						, self.type_vocab_size		)

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

         _lowerCamelCase						:    Optional[Any]         =			self.get_config()

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




  def 						SCREAMING_SNAKE_CASE    (			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=__lowerCAmelCase						, initializer_range=self.initializer_range						, )




  def 						SCREAMING_SNAKE_CASE    (			self					:						Union[str, Any]						, __lowerCAmelCase					:						Dict						, __lowerCAmelCase					:						Dict						, __lowerCAmelCase					:						Any						, __lowerCAmelCase					:						str						, __lowerCAmelCase					:						str						, __lowerCAmelCase					:						Tuple						, __lowerCAmelCase					:						List[str]		):






         """simple docstring"""






         _lowerCamelCase						:    Optional[Any]         =			BioGptModel(config=__lowerCAmelCase		)
         model.to(__lowerCAmelCase		)
         model.eval()
         _lowerCamelCase						:    Dict         =			model(__lowerCAmelCase						, attention_mask=__lowerCAmelCase		)
         _lowerCamelCase						:    Optional[Any]         =			model(__lowerCAmelCase		)
         self.parent.assertEqual(result.last_hidden_state.shape						, (self.batch_size, self.seq_length, self.hidden_size)		)




  def 						SCREAMING_SNAKE_CASE    (			self					:						Optional[int]						, __lowerCAmelCase					:						Any						, __lowerCAmelCase					:						Any						, __lowerCAmelCase					:						str						, __lowerCAmelCase					:						Union[str, Any]						, __lowerCAmelCase					:						Optional[int]						, __lowerCAmelCase					:						Optional[int]						, __lowerCAmelCase					:						str						, __lowerCAmelCase					:						List[Any]						, __lowerCAmelCase					:						Optional[Any]						, ):






         """simple docstring"""






         _lowerCamelCase						:    str         =			BioGptForCausalLM(config=__lowerCAmelCase		)
         model.to(__lowerCAmelCase		)
         model.eval()
         _lowerCamelCase						:    str         =			model(__lowerCAmelCase						, attention_mask=__lowerCAmelCase						, token_type_ids=__lowerCAmelCase						, labels=__lowerCAmelCase		)
         self.parent.assertEqual(result.logits.shape						, (self.batch_size, self.seq_length, self.vocab_size)		)




  def 						SCREAMING_SNAKE_CASE    (			self					:						Any						, __lowerCAmelCase					:						Union[str, Any]						, __lowerCAmelCase					:						Dict						, __lowerCAmelCase					:						List[str]						, __lowerCAmelCase					:						Optional[int]						, __lowerCAmelCase					:						List[Any]						, *__lowerCAmelCase					:						str		):






         """simple docstring"""






         _lowerCamelCase						:    str         =			BioGptModel(config=__lowerCAmelCase		)
         model.to(__lowerCAmelCase		)
         model.eval()

         # create attention mask
         _lowerCamelCase						:    List[str]         =			torch.ones(input_ids.shape						, dtype=torch.long						, device=__lowerCAmelCase		)
         _lowerCamelCase						:    List[str]         =			self.seq_length // 2
         _lowerCamelCase						:    Optional[int]         =			0

         # first forward pass
         _lowerCamelCase      ,   _lowerCamelCase						:    int         =			model(__lowerCAmelCase						, attention_mask=__lowerCAmelCase		).to_tuple()

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

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

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

         # get two different outputs
         _lowerCamelCase						:    List[str]         =			model(__lowerCAmelCase						, attention_mask=__lowerCAmelCase		)['''last_hidden_state''']
         _lowerCamelCase						:    List[str]         =			model(__lowerCAmelCase						, past_key_values=__lowerCAmelCase						, attention_mask=__lowerCAmelCase		)['''last_hidden_state''']

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

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




  def 						SCREAMING_SNAKE_CASE    (			self					:						List[Any]						, __lowerCAmelCase					:						List[Any]						, __lowerCAmelCase					:						List[Any]						, __lowerCAmelCase					:						List[Any]						, __lowerCAmelCase					:						Any						, __lowerCAmelCase					:						List[str]						, *__lowerCAmelCase					:						Any		):






         """simple docstring"""






         _lowerCamelCase						:    str         =			BioGptModel(config=__lowerCAmelCase		).to(__lowerCAmelCase		).eval()

         _lowerCamelCase						:    Union[str, Any]         =			torch.ones(input_ids.shape						, dtype=torch.long						, device=__lowerCAmelCase		)

         # first forward pass
         _lowerCamelCase						:    Optional[int]         =			model(__lowerCAmelCase						, attention_mask=__lowerCAmelCase						, use_cache=__lowerCAmelCase		)

         _lowerCamelCase      ,   _lowerCamelCase						:    Dict         =			outputs.to_tuple()

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

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

         _lowerCamelCase						:    str         =			model(__lowerCAmelCase						, attention_mask=__lowerCAmelCase		)['''last_hidden_state''']
         _lowerCamelCase						:    Tuple         =			model(__lowerCAmelCase						, attention_mask=__lowerCAmelCase						, past_key_values=__lowerCAmelCase		)[
             '''last_hidden_state'''
         ]

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




  def 						SCREAMING_SNAKE_CASE    (			self					:						int						, __lowerCAmelCase					:						Any						, __lowerCAmelCase					:						Tuple						, __lowerCAmelCase					:						Tuple						, __lowerCAmelCase					:						Tuple						, __lowerCAmelCase					:						int						, *__lowerCAmelCase					:						List[str]						, __lowerCAmelCase					:						Dict=False		):






         """simple docstring"""






         _lowerCamelCase						:    Dict         =			BioGptForCausalLM(__lowerCAmelCase		)
         model.to(__lowerCAmelCase		)
         if gradient_checkpointing:
                model.gradient_checkpointing_enable()

         _lowerCamelCase						:    Dict         =			model(__lowerCAmelCase						, labels=__lowerCAmelCase		)
         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 						SCREAMING_SNAKE_CASE    (			self					:						List[Any]						, __lowerCAmelCase					:						Tuple						, *__lowerCAmelCase					:						Union[str, Any]		):






         """simple docstring"""






         _lowerCamelCase						:    Any         =			BioGptModel(__lowerCAmelCase		)
         _lowerCamelCase						:    int         =			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 						SCREAMING_SNAKE_CASE    (			self					:						Optional[Any]						, __lowerCAmelCase					:						int						, __lowerCAmelCase					:						Optional[Any]						, __lowerCAmelCase					:						Dict						, __lowerCAmelCase					:						Optional[Any]						, __lowerCAmelCase					:						Union[str, Any]						, *__lowerCAmelCase					:						Optional[int]		):






         """simple docstring"""






         _lowerCamelCase						:    Dict         =			self.num_labels
         _lowerCamelCase						:    str         =			BioGptForTokenClassification(__lowerCAmelCase		)
         model.to(__lowerCAmelCase		)
         model.eval()
         _lowerCamelCase						:    Any         =			model(__lowerCAmelCase						, attention_mask=__lowerCAmelCase						, token_type_ids=__lowerCAmelCase		)
         self.parent.assertEqual(result.logits.shape						, (self.batch_size, self.seq_length, self.num_labels)		)




  def 						SCREAMING_SNAKE_CASE    (			self					:						str		):






         """simple docstring"""






         _lowerCamelCase						:    Optional[int]         =			self.prepare_config_and_inputs()
         (
             (
             _lowerCamelCase
         )      ,   (
             _lowerCamelCase
         )      ,   (
             _lowerCamelCase
         )      ,   (
             _lowerCamelCase
         )      ,   (
             _lowerCamelCase
         )      ,   (
             _lowerCamelCase
         )      ,   (
             _lowerCamelCase
         )      ,   
         )						:    Optional[int]         =			config_and_inputs
         _lowerCamelCase						:    List[Any]         =			{'''input_ids''': input_ids, '''attention_mask''': input_mask}
         return config, inputs_dict






@require_torch
class 	__snake_case	(     _lowercase  ,					_lowercase  ,					_lowercase  ,					unittest.TestCase):
  snake_case__    :       Any        =						(
      (BioGptModel, BioGptForCausalLM, BioGptForSequenceClassification, BioGptForTokenClassification)
      if is_torch_available()
      else ()
  )
  snake_case__    :       int        =						(BioGptForCausalLM,) if is_torch_available() else ()
  snake_case__    :       Optional[Any]        =						(
      {
          "feature-extraction": BioGptModel,
          "text-classification": BioGptForSequenceClassification,
          "text-generation": BioGptForCausalLM,
          "token-classification": BioGptForTokenClassification,
          "zero-shot": BioGptForSequenceClassification,
      }
      if is_torch_available()
      else {}
  )
  snake_case__    :       Any        =						False




  def 						SCREAMING_SNAKE_CASE    (			self					:						str		):






         """simple docstring"""






         _lowerCamelCase						:    int         =			BioGptModelTester(self		)
         _lowerCamelCase						:    Any         =			ConfigTester(self						, config_class=__lowerCAmelCase						, hidden_size=3_7		)




  def 						SCREAMING_SNAKE_CASE    (			self					:						Tuple		):






         """simple docstring"""






         self.config_tester.run_common_tests()




  def 						SCREAMING_SNAKE_CASE    (			self					:						List[Any]		):






         """simple docstring"""






         _lowerCamelCase						:    str         =			self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*__lowerCAmelCase		)




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






         """simple docstring"""






         _lowerCamelCase						:    Optional[int]         =			self.model_tester.prepare_config_and_inputs()
         for type in ["absolute", "relative_key", "relative_key_query"]:
                _lowerCamelCase						:    Optional[int]         =			type
                self.model_tester.create_and_check_model(*__lowerCAmelCase		)




  def 						SCREAMING_SNAKE_CASE    (			self					:						int		):






         """simple docstring"""






         _lowerCamelCase						:    List[Any]         =			self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_biogpt_model_attention_mask_past(*__lowerCAmelCase		)




  def 						SCREAMING_SNAKE_CASE    (			self					:						List[Any]		):






         """simple docstring"""






         _lowerCamelCase						:    Optional[Any]         =			self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_forward_and_backwards(*__lowerCAmelCase						, gradient_checkpointing=__lowerCAmelCase		)




  def 						SCREAMING_SNAKE_CASE    (			self					:						str		):






         """simple docstring"""






         _lowerCamelCase						:    str         =			self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_biogpt_model_past_large_inputs(*__lowerCAmelCase		)




  def 						SCREAMING_SNAKE_CASE    (			self					:						Dict		):






         """simple docstring"""






         _lowerCamelCase						:    str         =			self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_biogpt_weight_initialization(*__lowerCAmelCase		)




  def 						SCREAMING_SNAKE_CASE    (			self					:						Any		):






         """simple docstring"""






         _lowerCamelCase						:    Union[str, Any]         =			self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_biogpt_for_token_classification(*__lowerCAmelCase		)




  @slow
  def 						SCREAMING_SNAKE_CASE    (			self					:						str		):






         """simple docstring"""






         _lowerCamelCase						:    str         =			BioGptForCausalLM.from_pretrained('''microsoft/biogpt'''		)
         model.to(__lowerCAmelCase		)
         _lowerCamelCase						:    List[Any]         =			BioGptTokenizer.from_pretrained('''microsoft/biogpt'''		)

         _lowerCamelCase						:    Optional[Any]         =			'''left'''

         # Define PAD Token = EOS Token = 50256
         _lowerCamelCase						:    Tuple         =			tokenizer.eos_token
         _lowerCamelCase						:    List[Any]         =			model.config.eos_token_id

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

         _lowerCamelCase						:    Union[str, Any]         =			tokenizer(__lowerCAmelCase						, return_tensors='''pt'''						, padding=__lowerCAmelCase		)
         _lowerCamelCase						:    Any         =			inputs['''input_ids'''].to(__lowerCAmelCase		)

         _lowerCamelCase						:    List[Any]         =			model.generate(
             input_ids=__lowerCAmelCase						, attention_mask=inputs['''attention_mask'''].to(__lowerCAmelCase		)						, )

         _lowerCamelCase						:    Dict         =			tokenizer(sentences[0]						, return_tensors='''pt'''		).input_ids.to(__lowerCAmelCase		)
         _lowerCamelCase						:    str         =			model.generate(input_ids=__lowerCAmelCase		)

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

         _lowerCamelCase						:    List[Any]         =			tokenizer.batch_decode(__lowerCAmelCase						, skip_special_tokens=__lowerCAmelCase		)
         _lowerCamelCase						:    int         =			tokenizer.decode(output_non_padded[0]						, skip_special_tokens=__lowerCAmelCase		)
         _lowerCamelCase						:    Tuple         =			tokenizer.decode(output_padded[0]						, skip_special_tokens=__lowerCAmelCase		)

         _lowerCamelCase						:    List[Any]         =			[
             '''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(__lowerCAmelCase						, __lowerCAmelCase		)
         self.assertListEqual(__lowerCAmelCase						, [non_padded_sentence, padded_sentence]		)




  @slow
  def 						SCREAMING_SNAKE_CASE    (			self					:						Tuple		):






         """simple docstring"""






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




  def 						SCREAMING_SNAKE_CASE    (			self					:						Any		):






         """simple docstring"""






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




  def 						SCREAMING_SNAKE_CASE    (			self					:						Tuple		):






         """simple docstring"""






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






@require_torch
class 	__snake_case	(     unittest.TestCase):




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






         """simple docstring"""






         _lowerCamelCase						:    Optional[Any]         =			BioGptForCausalLM.from_pretrained('''microsoft/biogpt'''		)
         _lowerCamelCase						:    List[Any]         =			torch.tensor([[2, 4_8_0_5, 9, 6_5_6, 2_1]]		)
         _lowerCamelCase						:    Tuple         =			model(__lowerCAmelCase		)[0]

         _lowerCamelCase						:    Dict         =			4_2_3_8_4

         _lowerCamelCase						:    str         =			torch.Size((1, 5, vocab_size)		)
         self.assertEqual(output.shape						, __lowerCAmelCase		)

         _lowerCamelCase						:    Any         =			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]						, __lowerCAmelCase						, atol=1E-4		)		)




  @slow
  def 						SCREAMING_SNAKE_CASE    (			self					:						Optional[int]		):






         """simple docstring"""






         _lowerCamelCase						:    Optional[int]         =			BioGptTokenizer.from_pretrained('''microsoft/biogpt'''		)
         _lowerCamelCase						:    Optional[int]         =			BioGptForCausalLM.from_pretrained('''microsoft/biogpt'''		)
         model.to(__lowerCAmelCase		)

         torch.manual_seed(0		)
         _lowerCamelCase						:    int         =			tokenizer('''COVID-19 is'''						, return_tensors='''pt'''		).to(__lowerCAmelCase		)
         _lowerCamelCase						:    Tuple         =			model.generate(
             **__lowerCAmelCase						, min_length=1_0_0						, max_length=1_0_2_4						, num_beams=5						, early_stopping=__lowerCAmelCase						, )
         _lowerCamelCase						:    List[Any]         =			tokenizer.decode(output_ids[0]						, skip_special_tokens=__lowerCAmelCase		)

         _lowerCamelCase						:    Dict         =			(
             '''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(__lowerCAmelCase						, __lowerCAmelCase		)







"""simple docstring"""






import random
import unittest

import numpy as np
import torch

from diffusers import (
    DPMSolverMultistepScheduler,
    EulerAncestralDiscreteScheduler,
    EulerDiscreteScheduler,
    LMSDiscreteScheduler,
    OnnxStableDiffusionUpscalePipeline,
    PNDMScheduler,
)
from diffusers.utils import floats_tensor
from diffusers.utils.testing_utils import (
    is_onnx_available,
    load_image,
    nightly,
    require_onnxruntime,
    require_torch_gpu,
)

from ..test_pipelines_onnx_common import OnnxPipelineTesterMixin


if is_onnx_available():
   import onnxruntime as ort






class 	__snake_case	(     _lowercase  ,					unittest.TestCase):
  # TODO: is there an appropriate internal test set?
  snake_case__    :       List[str]        =						"ssube/stable-diffusion-x4-upscaler-onnx"




  def 						SCREAMING_SNAKE_CASE    (			self					:						Optional[Any]						, __lowerCAmelCase					:						int=0		):






         """simple docstring"""






         _lowerCamelCase						:    Tuple         =			floats_tensor((1, 3, 1_2_8, 1_2_8)						, rng=random.Random(__lowerCAmelCase		)		)
         _lowerCamelCase						:    Union[str, Any]         =			torch.manual_seed(__lowerCAmelCase		)
         _lowerCamelCase						:    Tuple         =			{
             '''prompt''': '''A painting of a squirrel eating a burger''',
             '''image''': image,
             '''generator''': generator,
             '''num_inference_steps''': 3,
             '''guidance_scale''': 7.5,
             '''output_type''': '''numpy''',
         }
         return inputs




  def 						SCREAMING_SNAKE_CASE    (			self					:						Optional[Any]		):






         """simple docstring"""






         _lowerCamelCase						:    Union[str, Any]         =			OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint						, provider='''CPUExecutionProvider'''		)
         pipe.set_progress_bar_config(disable=__lowerCAmelCase		)

         _lowerCamelCase						:    Union[str, Any]         =			self.get_dummy_inputs()
         _lowerCamelCase						:    Any         =			pipe(**__lowerCAmelCase		).images
         _lowerCamelCase						:    Dict         =			image[0, -3:, -3:, -1].flatten()

         # started as 128, should now be 512
         assert image.shape == (1, 5_1_2, 5_1_2, 3)
         _lowerCamelCase						:    str         =			np.array(
             [0.6_97_47_82, 0.68_90_20_93, 0.70_13_58_85, 0.7_58_36_18, 0.7_80_45_45, 0.7_85_49_12, 0.78_66_74_26, 0.78_74_38_63, 0.78_07_02_23]		)
         assert np.abs(image_slice - expected_slice		).max() < 1E-1




  def 						SCREAMING_SNAKE_CASE    (			self					:						List[Any]		):






         """simple docstring"""






         _lowerCamelCase						:    Optional[Any]         =			OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint						, provider='''CPUExecutionProvider'''		)
         _lowerCamelCase						:    List[str]         =			PNDMScheduler.from_config(pipe.scheduler.config						, skip_prk_steps=__lowerCAmelCase		)
         pipe.set_progress_bar_config(disable=__lowerCAmelCase		)

         _lowerCamelCase						:    int         =			self.get_dummy_inputs()
         _lowerCamelCase						:    Optional[Any]         =			pipe(**__lowerCAmelCase		).images
         _lowerCamelCase						:    str         =			image[0, -3:, -3:, -1]

         assert image.shape == (1, 5_1_2, 5_1_2, 3)
         _lowerCamelCase						:    Optional[int]         =			np.array(
             [0.6_89_88_92, 0.59_24_05_56, 0.52_49_95_27, 0.58_86_62_15, 0.52_25_82_35, 0.52_57_27_15, 0.62_41_44_73, 0.6_17_43_87, 0.6_21_49_64]		)
         assert np.abs(image_slice.flatten() - expected_slice		).max() < 1E-1




  def 						SCREAMING_SNAKE_CASE    (			self					:						Optional[int]		):






         """simple docstring"""






         _lowerCamelCase						:    Optional[int]         =			OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint						, provider='''CPUExecutionProvider'''		)
         _lowerCamelCase						:    Optional[int]         =			DPMSolverMultistepScheduler.from_config(pipe.scheduler.config		)
         pipe.set_progress_bar_config(disable=__lowerCAmelCase		)

         _lowerCamelCase						:    Tuple         =			self.get_dummy_inputs()
         _lowerCamelCase						:    str         =			pipe(**__lowerCAmelCase		).images
         _lowerCamelCase						:    Tuple         =			image[0, -3:, -3:, -1]

         assert image.shape == (1, 5_1_2, 5_1_2, 3)
         _lowerCamelCase						:    str         =			np.array(
             [0.7_65_92_78, 0.76_43_76_64, 0.75_57_91_07, 0.7_69_11_16, 0.77_66_69_86, 0.7_72_76_72, 0.7_75_86_64, 0.7_81_22_26, 0.76_94_25_15]		)

         assert np.abs(image_slice.flatten() - expected_slice		).max() < 1E-1




  def 						SCREAMING_SNAKE_CASE    (			self					:						Any		):






         """simple docstring"""






         _lowerCamelCase						:    Any         =			OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint						, provider='''CPUExecutionProvider'''		)
         _lowerCamelCase						:    Union[str, Any]         =			EulerDiscreteScheduler.from_config(pipe.scheduler.config		)
         pipe.set_progress_bar_config(disable=__lowerCAmelCase		)

         _lowerCamelCase						:    Optional[Any]         =			self.get_dummy_inputs()
         _lowerCamelCase						:    Tuple         =			pipe(**__lowerCAmelCase		).images
         _lowerCamelCase						:    Union[str, Any]         =			image[0, -3:, -3:, -1]

         assert image.shape == (1, 5_1_2, 5_1_2, 3)
         _lowerCamelCase						:    Union[str, Any]         =			np.array(
             [0.6_97_47_82, 0.68_90_20_93, 0.70_13_58_85, 0.7_58_36_18, 0.7_80_45_45, 0.7_85_49_12, 0.78_66_74_26, 0.78_74_38_63, 0.78_07_02_23]		)
         assert np.abs(image_slice.flatten() - expected_slice		).max() < 1E-1




  def 						SCREAMING_SNAKE_CASE    (			self					:						Any		):






         """simple docstring"""






         _lowerCamelCase						:    Dict         =			OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint						, provider='''CPUExecutionProvider'''		)
         _lowerCamelCase						:    int         =			EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config		)
         pipe.set_progress_bar_config(disable=__lowerCAmelCase		)

         _lowerCamelCase						:    Union[str, Any]         =			self.get_dummy_inputs()
         _lowerCamelCase						:    List[Any]         =			pipe(**__lowerCAmelCase		).images
         _lowerCamelCase						:    Optional[Any]         =			image[0, -3:, -3:, -1]

         assert image.shape == (1, 5_1_2, 5_1_2, 3)
         _lowerCamelCase						:    Optional[int]         =			np.array(
             [0.77_42_44_96, 0.77_36_01, 0.7_64_52_88, 0.7_76_95_98, 0.7_77_27_39, 0.7_73_86_88, 0.78_18_72_33, 0.77_87_95_84, 0.76_70_43]		)

         assert np.abs(image_slice.flatten() - expected_slice		).max() < 1E-1






@nightly
@require_onnxruntime
@require_torch_gpu
class 	__snake_case	(     unittest.TestCase):




  @property
  def 						SCREAMING_SNAKE_CASE    (			self					:						str		):






         """simple docstring"""






         return (
             "CUDAExecutionProvider",
             {
                 "gpu_mem_limit": "15000000000",  # 15GB
                 "arena_extend_strategy": "kSameAsRequested",
             },
         )




  @property
  def 						SCREAMING_SNAKE_CASE    (			self					:						Any		):






         """simple docstring"""






         _lowerCamelCase						:    Optional[int]         =			ort.SessionOptions()
         _lowerCamelCase						:    List[str]         =			False
         return options




  def 						SCREAMING_SNAKE_CASE    (			self					:						Optional[int]		):






         """simple docstring"""






         _lowerCamelCase						:    Optional[int]         =			load_image(
             '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'''
             '''/img2img/sketch-mountains-input.jpg'''		)
         _lowerCamelCase						:    Any         =			init_image.resize((1_2_8, 1_2_8)		)
         # using the PNDM scheduler by default
         _lowerCamelCase						:    List[str]         =			OnnxStableDiffusionUpscalePipeline.from_pretrained(
             '''ssube/stable-diffusion-x4-upscaler-onnx'''						, provider=self.gpu_provider						, sess_options=self.gpu_options						, )
         pipe.set_progress_bar_config(disable=__lowerCAmelCase		)

         _lowerCamelCase						:    int         =			'''A fantasy landscape, trending on artstation'''

         _lowerCamelCase						:    List[Any]         =			torch.manual_seed(0		)
         _lowerCamelCase						:    List[str]         =			pipe(
             prompt=__lowerCAmelCase						, image=__lowerCAmelCase						, guidance_scale=7.5						, num_inference_steps=1_0						, generator=__lowerCAmelCase						, output_type='''np'''						, )
         _lowerCamelCase						:    List[Any]         =			output.images
         _lowerCamelCase						:    List[Any]         =			images[0, 2_5_5:2_5_8, 3_8_3:3_8_6, -1]

         assert images.shape == (1, 5_1_2, 5_1_2, 3)
         _lowerCamelCase						:    str         =			np.array([0.48_83, 0.49_47, 0.49_80, 0.49_75, 0.49_82, 0.49_80, 0.50_00, 0.50_06, 0.49_72]		)
         # TODO: lower the tolerance after finding the cause of onnxruntime reproducibility issues

         assert np.abs(image_slice.flatten() - expected_slice		).max() < 2E-2




  def 						SCREAMING_SNAKE_CASE    (			self					:						str		):






         """simple docstring"""






         _lowerCamelCase						:    Union[str, Any]         =			load_image(
             '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'''
             '''/img2img/sketch-mountains-input.jpg'''		)
         _lowerCamelCase						:    int         =			init_image.resize((1_2_8, 1_2_8)		)
         _lowerCamelCase						:    str         =			LMSDiscreteScheduler.from_pretrained(
             '''ssube/stable-diffusion-x4-upscaler-onnx'''						, subfolder='''scheduler'''		)
         _lowerCamelCase						:    Dict         =			OnnxStableDiffusionUpscalePipeline.from_pretrained(
             '''ssube/stable-diffusion-x4-upscaler-onnx'''						, scheduler=__lowerCAmelCase						, provider=self.gpu_provider						, sess_options=self.gpu_options						, )
         pipe.set_progress_bar_config(disable=__lowerCAmelCase		)

         _lowerCamelCase						:    Optional[int]         =			'''A fantasy landscape, trending on artstation'''

         _lowerCamelCase						:    int         =			torch.manual_seed(0		)
         _lowerCamelCase						:    List[str]         =			pipe(
             prompt=__lowerCAmelCase						, image=__lowerCAmelCase						, guidance_scale=7.5						, num_inference_steps=2_0						, generator=__lowerCAmelCase						, output_type='''np'''						, )
         _lowerCamelCase						:    Union[str, Any]         =			output.images
         _lowerCamelCase						:    Optional[Any]         =			images[0, 2_5_5:2_5_8, 3_8_3:3_8_6, -1]

         assert images.shape == (1, 5_1_2, 5_1_2, 3)
         _lowerCamelCase						:    str         =			np.array(
             [0.50_17_37_53, 0.50_22_33_56, 0.50_20_39, 0.50_23_30_36, 0.5_02_37_25, 0.5_02_26_01, 0.5_01_87_58, 0.50_23_40_85, 0.50_24_15_66]		)
         # TODO: lower the tolerance after finding the cause of onnxruntime reproducibility issues

         assert np.abs(image_slice.flatten() - expected_slice		).max() < 2E-2


