



"""simple docstring"""






import unittest

from transformers import BertGenerationConfig, 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, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
   import torch

   from transformers import BertGenerationDecoder, BertGenerationEncoder






class 	__snake_case	:




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






         """simple docstring"""






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




  def 						SCREAMING_SNAKE_CASE    (			self					:						Optional[int]		):






         """simple docstring"""






         _lowerCamelCase						:    Optional[int]         =			ids_tensor([self.batch_size, self.seq_length]						, self.vocab_size		)

         _lowerCamelCase						:    Optional[Any]         =			None
         if self.use_input_mask:
                _lowerCamelCase						:    Dict         =			random_attention_mask([self.batch_size, self.seq_length]		)

         if self.use_labels:
                _lowerCamelCase						:    List[Any]         =			ids_tensor([self.batch_size, self.seq_length]						, self.vocab_size		)

         _lowerCamelCase						:    str         =			self.get_config()

         return config, input_ids, input_mask, token_labels




  def 						SCREAMING_SNAKE_CASE    (			self					:						Optional[int]		):






         """simple docstring"""






         return BertGenerationConfig(
             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						, is_decoder=__lowerCAmelCase						, initializer_range=self.initializer_range						, )




  def 						SCREAMING_SNAKE_CASE    (			self					:						Any		):






         """simple docstring"""






         (
             _lowerCamelCase   
         )						:    List[Any]         =			self.prepare_config_and_inputs()

         _lowerCamelCase						:    List[Any]         =			True
         _lowerCamelCase						:    Dict         =			floats_tensor([self.batch_size, self.seq_length, self.hidden_size]		)
         _lowerCamelCase						:    List[Any]         =			ids_tensor([self.batch_size, self.seq_length]						, vocab_size=2		)

         return (
             config,
             input_ids,
             input_mask,
             token_labels,
             encoder_hidden_states,
             encoder_attention_mask,
         )




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






         """simple docstring"""






         _lowerCamelCase						:    Any         =			BertGenerationEncoder(config=__lowerCAmelCase		)
         model.to(__lowerCAmelCase		)
         model.eval()
         _lowerCamelCase						:    Optional[int]         =			model(__lowerCAmelCase						, attention_mask=__lowerCAmelCase		)
         _lowerCamelCase						:    Union[str, 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[Any]						, __lowerCAmelCase					:						int						, __lowerCAmelCase					:						Any						, __lowerCAmelCase					:						List[Any]						, __lowerCAmelCase					:						List[str]						, __lowerCAmelCase					:						int						, __lowerCAmelCase					:						List[str]						, **__lowerCAmelCase					:						Optional[int]						, ):






         """simple docstring"""






         _lowerCamelCase						:    int         =			True
         _lowerCamelCase						:    int         =			BertGenerationEncoder(config=__lowerCAmelCase		)
         model.to(__lowerCAmelCase		)
         model.eval()
         _lowerCamelCase						:    List[Any]         =			model(
             __lowerCAmelCase						, attention_mask=__lowerCAmelCase						, encoder_hidden_states=__lowerCAmelCase						, encoder_attention_mask=__lowerCAmelCase						, )
         _lowerCamelCase						:    List[str]         =			model(
             __lowerCAmelCase						, attention_mask=__lowerCAmelCase						, encoder_hidden_states=__lowerCAmelCase						, )
         self.parent.assertEqual(result.last_hidden_state.shape						, (self.batch_size, self.seq_length, self.hidden_size)		)




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






         """simple docstring"""






         _lowerCamelCase						:    List[Any]         =			True
         _lowerCamelCase						:    Dict         =			True
         _lowerCamelCase						:    Tuple         =			BertGenerationDecoder(config=__lowerCAmelCase		).to(__lowerCAmelCase		).eval()

         # first forward pass
         _lowerCamelCase						:    Optional[int]         =			model(
             __lowerCAmelCase						, attention_mask=__lowerCAmelCase						, encoder_hidden_states=__lowerCAmelCase						, encoder_attention_mask=__lowerCAmelCase						, use_cache=__lowerCAmelCase						, )
         _lowerCamelCase						:    List[str]         =			outputs.past_key_values

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

         # append to next input_ids and
         _lowerCamelCase						:    Any         =			torch.cat([input_ids, next_tokens]						, dim=-1		)
         _lowerCamelCase						:    Optional[Any]         =			torch.cat([input_mask, next_mask]						, dim=-1		)

         _lowerCamelCase						:    Optional[int]         =			model(
             __lowerCAmelCase						, attention_mask=__lowerCAmelCase						, encoder_hidden_states=__lowerCAmelCase						, encoder_attention_mask=__lowerCAmelCase						, output_hidden_states=__lowerCAmelCase						, )['''hidden_states'''][0]
         _lowerCamelCase						:    Optional[Any]         =			model(
             __lowerCAmelCase						, attention_mask=__lowerCAmelCase						, encoder_hidden_states=__lowerCAmelCase						, encoder_attention_mask=__lowerCAmelCase						, past_key_values=__lowerCAmelCase						, output_hidden_states=__lowerCAmelCase						, )['''hidden_states'''][0]

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




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






         """simple docstring"""






         _lowerCamelCase						:    Dict         =			BertGenerationDecoder(__lowerCAmelCase		)
         model.to(__lowerCAmelCase		)
         model.eval()
         _lowerCamelCase						:    List[str]         =			model(__lowerCAmelCase						, attention_mask=__lowerCAmelCase						, labels=__lowerCAmelCase		)
         self.parent.assertEqual(result.logits.shape						, (self.batch_size, self.seq_length, self.vocab_size)		)




  def 						SCREAMING_SNAKE_CASE    (			self					:						Tuple		):






         """simple docstring"""






         _lowerCamelCase						:    str         =			self.prepare_config_and_inputs()
         _lowerCamelCase						:    str         =			{'''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        =						(BertGenerationEncoder, BertGenerationDecoder) if is_torch_available() else ()
  snake_case__    :       int        =						(BertGenerationDecoder,) if is_torch_available() else ()
  snake_case__    :       List[str]        =						(
      {"feature-extraction": BertGenerationEncoder, "text-generation": BertGenerationDecoder}
      if is_torch_available()
      else {}
  )




  def 						SCREAMING_SNAKE_CASE    (			self					:						Any		):






         """simple docstring"""






         _lowerCamelCase						:    Any         =			BertGenerationEncoderTester(self		)
         _lowerCamelCase						:    List[Any]         =			ConfigTester(self						, config_class=__lowerCAmelCase						, hidden_size=3_7		)




  def 						SCREAMING_SNAKE_CASE    (			self					:						Optional[Any]		):






         """simple docstring"""






         self.config_tester.run_common_tests()




  def 						SCREAMING_SNAKE_CASE    (			self					:						Optional[int]		):






         """simple docstring"""






         _lowerCamelCase						:    Optional[int]         =			self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*__lowerCAmelCase		)




  def 						SCREAMING_SNAKE_CASE    (			self					:						List[Any]		):






         """simple docstring"""






         _lowerCamelCase						:    Union[str, Any]         =			self.model_tester.prepare_config_and_inputs()
         _lowerCamelCase						:    Any         =			'''bert'''
         self.model_tester.create_and_check_model(__lowerCAmelCase						, __lowerCAmelCase						, __lowerCAmelCase						, __lowerCAmelCase		)




  def 						SCREAMING_SNAKE_CASE    (			self					:						Optional[int]		):






         """simple docstring"""






         _lowerCamelCase						:    Dict         =			self.model_tester.prepare_config_and_inputs_for_decoder()
         self.model_tester.create_and_check_model_as_decoder(*__lowerCAmelCase		)




  def 						SCREAMING_SNAKE_CASE    (			self					:						int		):






         """simple docstring"""






         _lowerCamelCase						:    List[str]         =			self.model_tester.prepare_config_and_inputs_for_decoder()
         self.model_tester.create_and_check_decoder_model_past_large_inputs(*__lowerCAmelCase		)




  def 						SCREAMING_SNAKE_CASE    (			self					:						str		):






         """simple docstring"""






         (
             _lowerCamelCase   
         )						:    int         =			self.model_tester.prepare_config_and_inputs_for_decoder()

         _lowerCamelCase						:    int         =			None

         self.model_tester.create_and_check_model_as_decoder(
             __lowerCAmelCase						, __lowerCAmelCase						, __lowerCAmelCase						, __lowerCAmelCase						, __lowerCAmelCase						, __lowerCAmelCase						, )




  def 						SCREAMING_SNAKE_CASE    (			self					:						Any		):






         """simple docstring"""






         _lowerCamelCase						:    Any         =			self.model_tester.prepare_config_and_inputs_for_decoder()
         self.model_tester.create_and_check_for_causal_lm(*__lowerCAmelCase		)




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






         """simple docstring"""






         _lowerCamelCase						:    str         =			BertGenerationEncoder.from_pretrained('''google/bert_for_seq_generation_L-24_bbc_encoder'''		)
         self.assertIsNotNone(__lowerCAmelCase		)






@require_torch
class 	__snake_case	(     unittest.TestCase):




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






         """simple docstring"""






         _lowerCamelCase						:    Any         =			BertGenerationEncoder.from_pretrained('''google/bert_for_seq_generation_L-24_bbc_encoder'''		)
         _lowerCamelCase						:    Any         =			torch.tensor([[1_0_1, 7_5_9_2, 1_0_1_0, 2_0_2_6, 3_8_9_9, 2_0_0_3, 1_0_1_4_0, 1_0_2]]		)
         with torch.no_grad():
                _lowerCamelCase						:    Optional[Any]         =			model(__lowerCAmelCase		)[0]
         _lowerCamelCase						:    Union[str, Any]         =			torch.Size([1, 8, 1_0_2_4]		)
         self.assertEqual(output.shape						, __lowerCAmelCase		)
         _lowerCamelCase						:    int         =			torch.tensor(
             [[[0.17_75, 0.00_83, -0.03_21], [1.60_02, 0.12_87, 0.39_12], [2.14_73, 0.57_91, 0.60_66]]]		)
         self.assertTrue(torch.allclose(output[:, :3, :3]						, __lowerCAmelCase						, atol=1E-4		)		)






@require_torch
class 	__snake_case	(     unittest.TestCase):




  @slow
  def 						SCREAMING_SNAKE_CASE    (			self					:						Any		):






         """simple docstring"""






         _lowerCamelCase						:    str         =			BertGenerationDecoder.from_pretrained('''google/bert_for_seq_generation_L-24_bbc_encoder'''		)
         _lowerCamelCase						:    Any         =			torch.tensor([[1_0_1, 7_5_9_2, 1_0_1_0, 2_0_2_6, 3_8_9_9, 2_0_0_3, 1_0_1_4_0, 1_0_2]]		)
         with torch.no_grad():
                _lowerCamelCase						:    str         =			model(__lowerCAmelCase		)[0]
         _lowerCamelCase						:    Optional[int]         =			torch.Size([1, 8, 5_0_3_5_8]		)
         self.assertEqual(output.shape						, __lowerCAmelCase		)
         _lowerCamelCase						:    int         =			torch.tensor(
             [[[-0.57_88, -2.59_94, -3.70_54], [0.04_38, 4.79_97, 1.87_95], [1.58_62, 6.64_09, 4.46_38]]]		)
         self.assertTrue(torch.allclose(output[:, :3, :3]						, __lowerCAmelCase						, atol=1E-4		)		)







"""simple docstring"""






def 	snake_case_  (				A_ :		list						):


       '''simple docstring'''





       _lowerCamelCase						:    Union[str, Any]         =			len(A_						)
       for i in range(1,    A_						):
              _lowerCamelCase						:    Tuple         =			collection[i]
              _lowerCamelCase						:    Dict         =			0
              _lowerCamelCase						:    Any         =			i - 1

              while low <= high:
                     _lowerCamelCase						:    Optional[int]         =			(low + high) // 2
                     if val < collection[mid]:
                            _lowerCamelCase						:    List[str]         =			mid - 1
                     else:
                            _lowerCamelCase						:    Dict         =			mid + 1
              for j in range(A_,    A_,    -1						):
                     _lowerCamelCase						:    Optional[int]         =			collection[j - 1]
              _lowerCamelCase						:    Tuple         =			val
       return collection


if __name__ == "__main__":
   lowerCAmelCase__      =    input('''Enter numbers separated by a comma:\n''').strip()
   lowerCAmelCase__      =    [int(item) for item in user_input.split(''',''')]
   print(binary_insertion_sort(unsorted))


