



"""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 			_a :
			"""simple docstring"""



			def __init__(		self     :    Union[str, Any]				,   __UpperCamelCase     :    Any				,   __UpperCamelCase     :    Tuple=1_3				,   __UpperCamelCase     :    str=7				,   __UpperCamelCase     :    List[str]=True				,   __UpperCamelCase     :    Optional[int]=True				,   __UpperCamelCase     :    Any=False				,   __UpperCamelCase     :    int=True				,   __UpperCamelCase     :    Any=9_9				,   __UpperCamelCase     :    Tuple=3_2				,   __UpperCamelCase     :    Optional[int]=5				,   __UpperCamelCase     :    int=4				,   __UpperCamelCase     :    str=3_7				,   __UpperCamelCase     :    str="gelu"				,   __UpperCamelCase     :    Optional[Any]=0.1				,   __UpperCamelCase     :    int=0.1				,   __UpperCamelCase     :    Tuple=5_1_2				,   __UpperCamelCase     :    Dict=1_6				,   __UpperCamelCase     :    Union[str, Any]=2				,   __UpperCamelCase     :    int=0.0_2				,   __UpperCamelCase     :    Any=3				,   __UpperCamelCase     :    str=4				,   __UpperCamelCase     :    Any=None				,   )->Any:
							_UpperCAmelCase					=    parent
							_UpperCAmelCase					=    batch_size
							_UpperCAmelCase					=    seq_length
							_UpperCAmelCase					=    is_training
							_UpperCAmelCase					=    use_input_mask
							_UpperCAmelCase					=    use_token_type_ids
							_UpperCAmelCase					=    use_labels
							_UpperCAmelCase					=    vocab_size
							_UpperCAmelCase					=    hidden_size
							_UpperCAmelCase					=    num_hidden_layers
							_UpperCAmelCase					=    num_attention_heads
							_UpperCAmelCase					=    intermediate_size
							_UpperCAmelCase					=    hidden_act
							_UpperCAmelCase					=    hidden_dropout_prob
							_UpperCAmelCase					=    attention_probs_dropout_prob
							_UpperCAmelCase					=    max_position_embeddings
							_UpperCAmelCase					=    type_vocab_size
							_UpperCAmelCase					=    type_sequence_label_size
							_UpperCAmelCase					=    initializer_range
							_UpperCAmelCase					=    num_labels
							_UpperCAmelCase					=    num_choices
							_UpperCAmelCase					=    scope



			def  lowercase__						(		self     :    Optional[Any]   )->Dict:
							_UpperCAmelCase					=    ids_tensor([self.batch_size, self.seq_length]				,   self.vocab_size   )

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

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

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

							_UpperCAmelCase					=    self.get_config()

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



			def  lowercase__						(		self     :    List[str]   )->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  lowercase__						(		self     :    List[str]				,   __UpperCamelCase     :    List[str]				,   __UpperCamelCase     :    Tuple				,   __UpperCamelCase     :    List[Any]				,   __UpperCamelCase     :    Optional[int]				,   __UpperCamelCase     :    Tuple				,   __UpperCamelCase     :    Optional[Any]				,   __UpperCamelCase     :    Union[str, Any]   )->List[str]:
							_UpperCAmelCase					=    BioGptModel(config=__UpperCamelCase   )
							model.to(__UpperCamelCase   )
							model.eval()
							_UpperCAmelCase					=    model(__UpperCamelCase				,   attention_mask=__UpperCamelCase   )
							_UpperCAmelCase					=    model(__UpperCamelCase   )
							self.parent.assertEqual(result.last_hidden_state.shape				,   (self.batch_size, self.seq_length, self.hidden_size)   )



			def  lowercase__						(		self     :    int				,   __UpperCamelCase     :    Optional[int]				,   __UpperCamelCase     :    List[Any]				,   __UpperCamelCase     :    Dict				,   __UpperCamelCase     :    Optional[Any]				,   __UpperCamelCase     :    Tuple				,   __UpperCamelCase     :    Optional[int]				,   __UpperCamelCase     :    Optional[int]				,   __UpperCamelCase     :    Optional[int]				,   __UpperCamelCase     :    Any				,   )->int:
							_UpperCAmelCase					=    BioGptForCausalLM(config=__UpperCamelCase   )
							model.to(__UpperCamelCase   )
							model.eval()
							_UpperCAmelCase					=    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  lowercase__						(		self     :    Any				,   __UpperCamelCase     :    List[str]				,   __UpperCamelCase     :    int				,   __UpperCamelCase     :    str				,   __UpperCamelCase     :    Optional[int]				,   __UpperCamelCase     :    Optional[int]				,   *__UpperCamelCase     :    Union[str, Any]   )->List[Any]:
							_UpperCAmelCase					=    BioGptModel(config=__UpperCamelCase   )
							model.to(__UpperCamelCase   )
							model.eval()

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

							# first forward pass
							_UpperCAmelCase		,						_UpperCAmelCase					=    model(__UpperCamelCase				,   attention_mask=__UpperCamelCase   ).to_tuple()

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

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

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

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

							# select random slice
							_UpperCAmelCase					=    ids_tensor((1,)				,   output_from_past.shape[-1]   ).item()
							_UpperCAmelCase					=    output_from_no_past[:, -1, random_slice_idx].detach()
							_UpperCAmelCase					=    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  lowercase__						(		self     :    int				,   __UpperCamelCase     :    List[Any]				,   __UpperCamelCase     :    Optional[Any]				,   __UpperCamelCase     :    List[Any]				,   __UpperCamelCase     :    Union[str, Any]				,   __UpperCamelCase     :    int				,   *__UpperCamelCase     :    Optional[Any]   )->Tuple:
							_UpperCAmelCase					=    BioGptModel(config=__UpperCamelCase   ).to(__UpperCamelCase   ).eval()

							_UpperCAmelCase					=    torch.ones(input_ids.shape				,   dtype=torch.long				,   device=__UpperCamelCase   )

							# first forward pass
							_UpperCAmelCase					=    model(__UpperCamelCase				,   attention_mask=__UpperCamelCase				,   use_cache=__UpperCamelCase   )

							_UpperCAmelCase		,						_UpperCAmelCase					=    outputs.to_tuple()

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

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

							_UpperCAmelCase					=    model(__UpperCamelCase				,   attention_mask=__UpperCamelCase   )['''last_hidden_state''']
							_UpperCAmelCase					=    model(__UpperCamelCase				,   attention_mask=__UpperCamelCase				,   past_key_values=__UpperCamelCase   )[
							    '''last_hidden_state'''
							]

							# select random slice
							_UpperCAmelCase					=    ids_tensor((1,)				,   output_from_past.shape[-1]   ).item()
							_UpperCAmelCase					=    output_from_no_past[:, -3:, random_slice_idx].detach()
							_UpperCAmelCase					=    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  lowercase__						(		self     :    Any				,   __UpperCamelCase     :    int				,   __UpperCamelCase     :    Tuple				,   __UpperCamelCase     :    List[Any]				,   __UpperCamelCase     :    str				,   __UpperCamelCase     :    Dict				,   *__UpperCamelCase     :    Any				,   __UpperCamelCase     :    Dict=False   )->Any:
							_UpperCAmelCase					=    BioGptForCausalLM(__UpperCamelCase   )
							model.to(__UpperCamelCase   )
							if gradient_checkpointing:
											model.gradient_checkpointing_enable()

							_UpperCAmelCase					=    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  lowercase__						(		self     :    int				,   __UpperCamelCase     :    List[Any]				,   *__UpperCamelCase     :    Any   )->Dict:
							_UpperCAmelCase					=    BioGptModel(__UpperCamelCase   )
							_UpperCAmelCase					=    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_0_1   )
															self.parent.assertLessEqual(abs(torch.mean(model.state_dict()[key]   ) - 0.0   )				,   0.0_1   )



			def  lowercase__						(		self     :    str				,   __UpperCamelCase     :    Tuple				,   __UpperCamelCase     :    int				,   __UpperCamelCase     :    List[str]				,   __UpperCamelCase     :    Optional[Any]				,   __UpperCamelCase     :    Union[str, Any]				,   *__UpperCamelCase     :    List[Any]   )->List[Any]:
							_UpperCAmelCase					=    self.num_labels
							_UpperCAmelCase					=    BioGptForTokenClassification(__UpperCamelCase   )
							model.to(__UpperCamelCase   )
							model.eval()
							_UpperCAmelCase					=    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  lowercase__						(		self     :    str   )->Any:
							_UpperCAmelCase					=    self.prepare_config_and_inputs()
							(
							    (
							    _UpperCAmelCase						
							)		,						(
							    _UpperCAmelCase						
							)		,						(
							    _UpperCAmelCase						
							)		,						(
							    _UpperCAmelCase						
							)		,						(
							    _UpperCAmelCase						
							)		,						(
							    _UpperCAmelCase						
							)		,						(
							    _UpperCAmelCase						
							)		,												
							)					=    config_and_inputs
							_UpperCAmelCase					=    {'''input_ids''': input_ids, '''attention_mask''': input_mask}
							return config, inputs_dict



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



			def  lowercase__						(		self     :    Any   )->int:
							_UpperCAmelCase					=    BioGptModelTester(self   )
							_UpperCAmelCase					=    ConfigTester(self				,   config_class=__UpperCamelCase				,   hidden_size=3_7   )



			def  lowercase__						(		self     :    Any   )->Union[str, Any]:
							self.config_tester.run_common_tests()



			def  lowercase__						(		self     :    Dict   )->int:
							_UpperCAmelCase					=    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*__UpperCamelCase   )



			def  lowercase__						(		self     :    Dict   )->Union[str, Any]:
							_UpperCAmelCase					=    self.model_tester.prepare_config_and_inputs()
							for type in ["absolute", "relative_key", "relative_key_query"]:
											_UpperCAmelCase					=    type
											self.model_tester.create_and_check_model(*__UpperCamelCase   )



			def  lowercase__						(		self     :    List[str]   )->Tuple:
							_UpperCAmelCase					=    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_biogpt_model_attention_mask_past(*__UpperCamelCase   )



			def  lowercase__						(		self     :    List[str]   )->Optional[Any]:
							_UpperCAmelCase					=    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_forward_and_backwards(*__UpperCamelCase				,   gradient_checkpointing=__UpperCamelCase   )



			def  lowercase__						(		self     :    Optional[Any]   )->List[Any]:
							_UpperCAmelCase					=    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_biogpt_model_past_large_inputs(*__UpperCamelCase   )



			def  lowercase__						(		self     :    Any   )->Optional[Any]:
							_UpperCAmelCase					=    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_biogpt_weight_initialization(*__UpperCamelCase   )



			def  lowercase__						(		self     :    List[str]   )->List[Any]:
							_UpperCAmelCase					=    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_biogpt_for_token_classification(*__UpperCamelCase   )



			@slow
			def  lowercase__						(		self     :    Optional[Any]   )->Tuple:
							_UpperCAmelCase					=    BioGptForCausalLM.from_pretrained('''microsoft/biogpt'''   )
							model.to(__UpperCamelCase   )
							_UpperCAmelCase					=    BioGptTokenizer.from_pretrained('''microsoft/biogpt'''   )

							_UpperCAmelCase					=    '''left'''

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

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

							_UpperCAmelCase					=    tokenizer(__UpperCamelCase				,   return_tensors='''pt'''				,   padding=__UpperCamelCase   )
							_UpperCAmelCase					=    inputs['''input_ids'''].to(__UpperCamelCase   )

							_UpperCAmelCase					=    model.generate(
							    input_ids=__UpperCamelCase				,   attention_mask=inputs['''attention_mask'''].to(__UpperCamelCase   )				,   )

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

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

							_UpperCAmelCase					=    tokenizer.batch_decode(__UpperCamelCase				,   skip_special_tokens=__UpperCamelCase   )
							_UpperCAmelCase					=    tokenizer.decode(output_non_padded[0]				,   skip_special_tokens=__UpperCamelCase   )
							_UpperCAmelCase					=    tokenizer.decode(output_padded[0]				,   skip_special_tokens=__UpperCamelCase   )

							_UpperCAmelCase					=    [
							    '''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  lowercase__						(		self     :    Optional[int]   )->Optional[int]:
							for model_name in BIOGPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											_UpperCAmelCase					=    BioGptModel.from_pretrained(__UpperCamelCase   )
											self.assertIsNotNone(__UpperCamelCase   )



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



			def  lowercase__						(		self     :    Tuple   )->Optional[int]:
							_UpperCAmelCase		,						_UpperCAmelCase					=    self.model_tester.prepare_config_and_inputs_for_common()
							_UpperCAmelCase					=    3
							_UpperCAmelCase					=    '''multi_label_classification'''
							_UpperCAmelCase					=    input_dict['''input_ids''']
							_UpperCAmelCase					=    input_ids.ne(1   ).to(__UpperCamelCase   )
							_UpperCAmelCase					=    ids_tensor(
							    [self.model_tester.batch_size, config.num_labels]				,   self.model_tester.type_sequence_label_size   ).to(torch.float   )
							_UpperCAmelCase					=    BioGptForSequenceClassification(__UpperCamelCase   )
							model.to(__UpperCamelCase   )
							model.eval()
							_UpperCAmelCase					=    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 			_a (					unittest.TestCase):
			"""simple docstring"""



			@slow
			def  lowercase__						(		self     :    Any   )->Dict:
							_UpperCAmelCase					=    BioGptForCausalLM.from_pretrained('''microsoft/biogpt'''   )
							_UpperCAmelCase					=    torch.tensor([[2, 4_8_0_5, 9, 6_5_6, 2_1]]   )
							_UpperCAmelCase					=    model(__UpperCamelCase   )[0]

							_UpperCAmelCase					=    4_2_3_8_4

							_UpperCAmelCase					=    torch.Size((1, 5, vocab_size)   )
							self.assertEqual(output.shape				,   __UpperCamelCase   )

							_UpperCAmelCase					=    torch.tensor(
							    [[[-9.5_2_3_6, -9.8_9_1_8, 1_0.4_5_5_7], [-1_1.0_4_6_9, -9.6_4_2_3, 8.1_0_2_2], [-8.8_6_6_4, -7.8_8_2_6, 5.5_3_2_5]]]   )

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



			@slow
			def  lowercase__						(		self     :    List[str]   )->Dict:
							_UpperCAmelCase					=    BioGptTokenizer.from_pretrained('''microsoft/biogpt'''   )
							_UpperCAmelCase					=    BioGptForCausalLM.from_pretrained('''microsoft/biogpt'''   )
							model.to(__UpperCamelCase   )

							torch.manual_seed(0   )
							_UpperCAmelCase					=    tokenizer('''COVID-19 is'''				,   return_tensors='''pt'''   ).to(__UpperCamelCase   )
							_UpperCAmelCase					=    model.generate(
							    **__UpperCamelCase				,   min_length=1_0_0				,   max_length=1_0_2_4				,   num_beams=5				,   early_stopping=__UpperCamelCase				,   )
							_UpperCAmelCase					=    tokenizer.decode(output_ids[0]				,   skip_special_tokens=__UpperCamelCase   )

							_UpperCAmelCase					=    (
							    '''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 operator







def  lowercase	(      _SCREAMING_SNAKE_CASE   :						list					,     _SCREAMING_SNAKE_CASE   :						bool = False					,     _SCREAMING_SNAKE_CASE   :						list | None = None				):






				'''simple docstring'''



				_UpperCAmelCase					=    operator.lt if reverse else operator.gt
				_UpperCAmelCase					=    solution or []

				if not arr:
								return solution

				_UpperCAmelCase					=    [arr.pop(0				)]
				for i, item in enumerate(_SCREAMING_SNAKE_CASE				):
								if _operator(_SCREAMING_SNAKE_CASE					,     sublist[-1]				):
												sublist.append(_SCREAMING_SNAKE_CASE				)
												arr.pop(_SCREAMING_SNAKE_CASE				)

    #  merging sublist into solution list
				if not solution:
								solution.extend(_SCREAMING_SNAKE_CASE				)
				else:
								while sublist:
												_UpperCAmelCase					=    sublist.pop(0				)
												for i, xx in enumerate(_SCREAMING_SNAKE_CASE				):
																if not _operator(_SCREAMING_SNAKE_CASE					,     _SCREAMING_SNAKE_CASE				):
																				solution.insert(_SCREAMING_SNAKE_CASE					,     _SCREAMING_SNAKE_CASE				)
																				break
												else:
																solution.append(_SCREAMING_SNAKE_CASE				)

				strand_sort(_SCREAMING_SNAKE_CASE					,     _SCREAMING_SNAKE_CASE					,     _SCREAMING_SNAKE_CASE				)
				return solution


if __name__ == "__main__":
					assert strand_sort([4, 3, 5, 1, 2]) == [1, 2, 3, 4, 5]
					assert strand_sort([4, 3, 5, 1, 2], reverse=True) == [5, 4, 3, 2, 1]



