

'''simple docstring'''






import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

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 (
          RemBertConfig,
          RemBertForCausalLM,
          RemBertForMaskedLM,
          RemBertForMultipleChoice,
          RemBertForQuestionAnswering,
          RemBertForSequenceClassification,
          RemBertForTokenClassification,
          RemBertModel,
      )
      from transformers.models.rembert.modeling_rembert import REMBERT_PRETRAINED_MODEL_ARCHIVE_LIST
class 	SCREAMING_SNAKE_CASE__			:




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






          """simple docstring"""






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




    def   __lowerCamelCase      (					self			):






          """simple docstring"""






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

          SCREAMING_SNAKE_CASE_      :			List[str]     =       None
          if self.use_input_mask:
                SCREAMING_SNAKE_CASE_      :			Dict     =       random_attention_mask([self.batch_size, self.seq_length]			)

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

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

          SCREAMING_SNAKE_CASE_      :			Union[str, Any]     =       RemBertConfig(
              vocab_size=self.vocab_size				,     hidden_size=self.hidden_size				,     input_embedding_size=self.input_embedding_size				,     output_embedding_size=self.output_embedding_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=__A				,     initializer_range=self.initializer_range				,     )

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




    def   __lowerCamelCase      (					self			):






          """simple docstring"""






          (
              (
              SCREAMING_SNAKE_CASE_
          ),						(
              SCREAMING_SNAKE_CASE_
          ),						(
              SCREAMING_SNAKE_CASE_
          ),						(
              SCREAMING_SNAKE_CASE_
          ),						(
              SCREAMING_SNAKE_CASE_
          ),						(
              SCREAMING_SNAKE_CASE_
          ),						(
              SCREAMING_SNAKE_CASE_
          ),						
          )      :			List[Any]     =       self.prepare_config_and_inputs()

          SCREAMING_SNAKE_CASE_      :			Dict     =       True
          SCREAMING_SNAKE_CASE_      :			Tuple     =       floats_tensor([self.batch_size, self.seq_length, self.hidden_size]			)
          SCREAMING_SNAKE_CASE_      :			Optional[int]     =       ids_tensor([self.batch_size, self.seq_length]				,     vocab_size=2			)

          return (
              config,
              input_ids,
              token_type_ids,
              input_mask,
              sequence_labels,
              token_labels,
              choice_labels,
              encoder_hidden_states,
              encoder_attention_mask,
          )




    def   __lowerCamelCase      (					self				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__			):






          """simple docstring"""






          SCREAMING_SNAKE_CASE_      :			Tuple     =       RemBertModel(config=__A			)
          model.to(__A			)
          model.eval()
          SCREAMING_SNAKE_CASE_      :			Tuple     =       model(__A				,     attention_mask=__A				,     token_type_ids=__A			)
          SCREAMING_SNAKE_CASE_      :			Optional[int]     =       model(__A				,     token_type_ids=__A			)
          SCREAMING_SNAKE_CASE_      :			List[str]     =       model(__A			)
          self.parent.assertEqual(result.last_hidden_state.shape				,     (self.batch_size, self.seq_length, self.hidden_size)			)




    def   __lowerCamelCase      (					self				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__				,     ):






          """simple docstring"""






          SCREAMING_SNAKE_CASE_      :			List[str]     =       True
          SCREAMING_SNAKE_CASE_      :			List[str]     =       RemBertModel(__A			)
          model.to(__A			)
          model.eval()
          SCREAMING_SNAKE_CASE_      :			Optional[Any]     =       model(
              __A				,     attention_mask=__A				,     token_type_ids=__A				,     encoder_hidden_states=__A				,     encoder_attention_mask=__A				,     )
          SCREAMING_SNAKE_CASE_      :			int     =       model(
              __A				,     attention_mask=__A				,     token_type_ids=__A				,     encoder_hidden_states=__A				,     )
          SCREAMING_SNAKE_CASE_      :			int     =       model(__A				,     attention_mask=__A				,     token_type_ids=__A			)
          self.parent.assertEqual(result.last_hidden_state.shape				,     (self.batch_size, self.seq_length, self.hidden_size)			)




    def   __lowerCamelCase      (					self				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__				,     ):






          """simple docstring"""






          SCREAMING_SNAKE_CASE_      :			int     =       RemBertForCausalLM(config=__A			)
          model.to(__A			)
          model.eval()
          SCREAMING_SNAKE_CASE_      :			Optional[int]     =       model(__A				,     attention_mask=__A				,     token_type_ids=__A				,     labels=__A			)
          self.parent.assertEqual(result.logits.shape				,     (self.batch_size, self.seq_length, self.vocab_size)			)




    def   __lowerCamelCase      (					self				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__			):






          """simple docstring"""






          SCREAMING_SNAKE_CASE_      :			List[Any]     =       RemBertForMaskedLM(config=__A			)
          model.to(__A			)
          model.eval()
          SCREAMING_SNAKE_CASE_      :			int     =       model(__A				,     attention_mask=__A				,     token_type_ids=__A				,     labels=__A			)
          self.parent.assertEqual(result.logits.shape				,     (self.batch_size, self.seq_length, self.vocab_size)			)




    def   __lowerCamelCase      (					self				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__				,     ):






          """simple docstring"""






          SCREAMING_SNAKE_CASE_      :			int     =       True
          SCREAMING_SNAKE_CASE_      :			Tuple     =       True
          SCREAMING_SNAKE_CASE_      :			int     =       RemBertForCausalLM(config=__A			)
          model.to(__A			)
          model.eval()

          # first forward pass
          SCREAMING_SNAKE_CASE_      :			Tuple     =       model(
              __A				,     attention_mask=__A				,     encoder_hidden_states=__A				,     encoder_attention_mask=__A				,     use_cache=__A				,     )
          SCREAMING_SNAKE_CASE_      :			Optional[int]     =       outputs.past_key_values

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

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

          SCREAMING_SNAKE_CASE_      :			List[str]     =       model(
              __A				,     attention_mask=__A				,     encoder_hidden_states=__A				,     encoder_attention_mask=__A				,     output_hidden_states=__A				,     )["hidden_states"][0]
          SCREAMING_SNAKE_CASE_      :			Optional[Any]     =       model(
              __A				,     attention_mask=__A				,     encoder_hidden_states=__A				,     encoder_attention_mask=__A				,     past_key_values=__A				,     output_hidden_states=__A				,     )["hidden_states"][0]

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




    def   __lowerCamelCase      (					self				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__			):






          """simple docstring"""






          SCREAMING_SNAKE_CASE_      :			str     =       RemBertForQuestionAnswering(config=__A			)
          model.to(__A			)
          model.eval()
          SCREAMING_SNAKE_CASE_      :			List[str]     =       model(
              __A				,     attention_mask=__A				,     token_type_ids=__A				,     start_positions=__A				,     end_positions=__A				,     )
          self.parent.assertEqual(result.start_logits.shape				,     (self.batch_size, self.seq_length)			)
          self.parent.assertEqual(result.end_logits.shape				,     (self.batch_size, self.seq_length)			)




    def   __lowerCamelCase      (					self				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__			):






          """simple docstring"""






          SCREAMING_SNAKE_CASE_      :			int     =       self.num_labels
          SCREAMING_SNAKE_CASE_      :			int     =       RemBertForSequenceClassification(__A			)
          model.to(__A			)
          model.eval()
          SCREAMING_SNAKE_CASE_      :			int     =       model(__A				,     attention_mask=__A				,     token_type_ids=__A				,     labels=__A			)
          self.parent.assertEqual(result.logits.shape				,     (self.batch_size, self.num_labels)			)




    def   __lowerCamelCase      (					self				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__			):






          """simple docstring"""






          SCREAMING_SNAKE_CASE_      :			int     =       self.num_labels
          SCREAMING_SNAKE_CASE_      :			Dict     =       RemBertForTokenClassification(config=__A			)
          model.to(__A			)
          model.eval()
          SCREAMING_SNAKE_CASE_      :			Any     =       model(__A				,     attention_mask=__A				,     token_type_ids=__A				,     labels=__A			)
          self.parent.assertEqual(result.logits.shape				,     (self.batch_size, self.seq_length, self.num_labels)			)




    def   __lowerCamelCase      (					self				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__				,     lowercase__			):






          """simple docstring"""






          SCREAMING_SNAKE_CASE_      :			List[str]     =       self.num_choices
          SCREAMING_SNAKE_CASE_      :			Tuple     =       RemBertForMultipleChoice(config=__A			)
          model.to(__A			)
          model.eval()
          SCREAMING_SNAKE_CASE_      :			str     =       input_ids.unsqueeze(1			).expand(-1				,     self.num_choices				,     -1			).contiguous()
          SCREAMING_SNAKE_CASE_      :			Optional[Any]     =       token_type_ids.unsqueeze(1			).expand(-1				,     self.num_choices				,     -1			).contiguous()
          SCREAMING_SNAKE_CASE_      :			Tuple     =       input_mask.unsqueeze(1			).expand(-1				,     self.num_choices				,     -1			).contiguous()
          SCREAMING_SNAKE_CASE_      :			Dict     =       model(
              __A				,     attention_mask=__A				,     token_type_ids=__A				,     labels=__A				,     )
          self.parent.assertEqual(result.logits.shape				,     (self.batch_size, self.num_choices)			)







    def   __lowerCamelCase      (					self			):






          """simple docstring"""






          SCREAMING_SNAKE_CASE_      :			Tuple     =       self.prepare_config_and_inputs()
          (
              (
              SCREAMING_SNAKE_CASE_
          ),						(
              SCREAMING_SNAKE_CASE_
          ),						(
              SCREAMING_SNAKE_CASE_
          ),						(
              SCREAMING_SNAKE_CASE_
          ),						(
              SCREAMING_SNAKE_CASE_
          ),						(
              SCREAMING_SNAKE_CASE_
          ),						(
              SCREAMING_SNAKE_CASE_
          ),						
          )      :			Optional[int]     =       config_and_inputs
          SCREAMING_SNAKE_CASE_      :			Optional[Any]     =       {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
          return config, inputs_dict






@require_torch
class 	SCREAMING_SNAKE_CASE__			(				_UpperCAmelCase,_UpperCAmelCase,unittest.TestCase      ):
    _A			    =							(
        (
            RemBertModel,
            RemBertForMaskedLM,
            RemBertForCausalLM,
            RemBertForMultipleChoice,
            RemBertForQuestionAnswering,
            RemBertForSequenceClassification,
            RemBertForTokenClassification,
        )
        if is_torch_available()
        else ()
    )
    _A			    =							(RemBertForCausalLM,) if is_torch_available() else ()
    _A			    =							(
        {
            "feature-extraction": RemBertModel,
            "fill-mask": RemBertForMaskedLM,
            "question-answering": RemBertForQuestionAnswering,
            "text-classification": RemBertForSequenceClassification,
            "text-generation": RemBertForCausalLM,
            "token-classification": RemBertForTokenClassification,
            "zero-shot": RemBertForSequenceClassification,
        }
        if is_torch_available()
        else {}
    )




    def   __lowerCamelCase      (					self			):






          """simple docstring"""






          SCREAMING_SNAKE_CASE_      :			List[Any]     =       RemBertModelTester(self			)
          SCREAMING_SNAKE_CASE_      :			Optional[int]     =       ConfigTester(self				,     config_class=__A				,     hidden_size=37			)




    def   __lowerCamelCase      (					self			):






          """simple docstring"""






          self.config_tester.run_common_tests()




    def   __lowerCamelCase      (					self			):






          """simple docstring"""






          SCREAMING_SNAKE_CASE_      :			Union[str, Any]     =       self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*__A			)




    def   __lowerCamelCase      (					self			):






          """simple docstring"""






          SCREAMING_SNAKE_CASE_      :			List[str]     =       self.model_tester.prepare_config_and_inputs()
          for type in ["absolute", "relative_key", "relative_key_query"]:
                SCREAMING_SNAKE_CASE_      :			Union[str, Any]     =       type
                self.model_tester.create_and_check_model(*__A			)




    def   __lowerCamelCase      (					self			):






          """simple docstring"""






          SCREAMING_SNAKE_CASE_      :			Any     =       self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_masked_lm(*__A			)




    def   __lowerCamelCase      (					self			):






          """simple docstring"""






          SCREAMING_SNAKE_CASE_      :			Any     =       self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_multiple_choice(*__A			)




    def   __lowerCamelCase      (					self			):






          """simple docstring"""






          SCREAMING_SNAKE_CASE_      :			Dict     =       self.model_tester.prepare_config_and_inputs_for_decoder()
          self.model_tester.create_and_check_decoder_model_past_large_inputs(*__A			)




    def   __lowerCamelCase      (					self			):






          """simple docstring"""






          SCREAMING_SNAKE_CASE_      :			Optional[int]     =       self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_question_answering(*__A			)




    def   __lowerCamelCase      (					self			):






          """simple docstring"""






          SCREAMING_SNAKE_CASE_      :			Dict     =       self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_sequence_classification(*__A			)




    def   __lowerCamelCase      (					self			):






          """simple docstring"""






          SCREAMING_SNAKE_CASE_      :			str     =       self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_token_classification(*__A			)




    def   __lowerCamelCase      (					self			):






          """simple docstring"""






          SCREAMING_SNAKE_CASE_      :			Dict     =       self.model_tester.prepare_config_and_inputs_for_decoder()
          self.model_tester.create_and_check_model_as_decoder(*__A			)




    def   __lowerCamelCase      (					self			):






          """simple docstring"""






          (
              (
              SCREAMING_SNAKE_CASE_
          ),						(
              SCREAMING_SNAKE_CASE_
          ),						(
              SCREAMING_SNAKE_CASE_
          ),						(
              SCREAMING_SNAKE_CASE_
          ),						(
              SCREAMING_SNAKE_CASE_
          ),						(
              SCREAMING_SNAKE_CASE_
          ),						(
              SCREAMING_SNAKE_CASE_
          ),						(
              SCREAMING_SNAKE_CASE_
          ),						(
              SCREAMING_SNAKE_CASE_
          ),						
          )      :			Tuple     =       self.model_tester.prepare_config_and_inputs_for_decoder()

          SCREAMING_SNAKE_CASE_      :			Union[str, Any]     =       None

          self.model_tester.create_and_check_model_as_decoder(
              __A				,     __A				,     __A				,     __A				,     __A				,     __A				,     __A				,     __A				,     __A				,     )







    @slow
    def   __lowerCamelCase      (					self			):






          """simple docstring"""






          for model_name in REMBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                SCREAMING_SNAKE_CASE_      :			int     =       RemBertModel.from_pretrained(__A			)
                self.assertIsNotNone(__A			)






@require_torch
class 	SCREAMING_SNAKE_CASE__			(				unittest.TestCase      ):




    @slow
    def   __lowerCamelCase      (					self			):






          """simple docstring"""






          SCREAMING_SNAKE_CASE_      :			List[Any]     =       RemBertModel.from_pretrained("google/rembert"			)
          SCREAMING_SNAKE_CASE_      :			str     =       torch.tensor([[312, 5_6498, 313, 2125, 313]]			)
          SCREAMING_SNAKE_CASE_      :			Optional[int]     =       torch.tensor([[0, 0, 0, 1, 1]]			)
          with torch.no_grad():
                SCREAMING_SNAKE_CASE_      :			str     =       model(__A				,     token_type_ids=__A				,     output_hidden_states=__A			)

          SCREAMING_SNAKE_CASE_      :			Dict     =       1152

          SCREAMING_SNAKE_CASE_      :			List[Any]     =       torch.Size((1, 5, hidden_size)			)
          self.assertEqual(output["last_hidden_state"].shape				,     __A			)

          SCREAMING_SNAKE_CASE_      :			Any     =       torch.tensor(
              [
                  [
                      [0.0754, -0.2022, 0.1904],
                      [-0.3354, -0.3692, -0.4791],
                      [-0.2314, -0.6729, -0.0749],
                      [-0.0396, -0.3105, -0.4234],
                      [-0.1571, -0.0525, 0.5353],
                  ]
              ]			)

          # Running on the original tf implementation gives slightly different results here.
          # Not clear why this variations is present
          # TODO: Find reason for discrepancy
          # expected_original_implementation = [[
          #     [0.07630594074726105, -0.20146065950393677, 0.19107051193714142],
          #     [-0.3405614495277405, -0.36971670389175415, -0.4808273911476135],
          #     [-0.22587086260318756, -0.6656315922737122, -0.07844287157058716],
          #     [-0.04145475849509239, -0.3077218234539032, -0.42316967248916626],
          #     [-0.15887849032878876, -0.054529931396245956, 0.5356100797653198]
          # ]]

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









class   SCREAMING_SNAKE_CASE :





							"""simple docstring"""

							def __init__(     self:      Any      )			-> None:
												# numbermap keys are the number and its values are lists of indexes sorted
												# in ascending order
												_A									=       {}
												# indexmap keys are an index and it's values are the number at that index
												_A									=       {}

							def 					__A       (     self:      Any						,   __A:      list | str | range						,   __A:      int      )			-> list[int]:
												if isinstance(__A						,   (range, str)      ):
																	_A									=       list(__A      )
												elif not isinstance(__A						,   __A      ):
																	raise TypeError(
																	    '''binary_search_delete() only accepts either a list, range or str'''      )

												_A									=       0
												_A									=       len(__A      ) - 1

												while low <= high:
																	_A									=       (low + high) // 2
																	if array[mid] == item:
																						array.pop(__A      )
																						return array
																	elif array[mid] < item:
																						_A									=       mid + 1
																	else:
																						_A									=       mid - 1
												raise ValueError(
												    '''Either the item is not in the array or the array was unsorted'''      )

							def 					__A       (     self:      int						,   __A:      list | str | range						,   __A:      int      )			-> list[int]:
												if isinstance(__A						,   (range, str)      ):
																	_A									=       list(__A      )
												elif not isinstance(__A						,   __A      ):
																	raise TypeError(
																	    '''binary_search_insert() only accepts either a list, range or str'''      )

												_A									=       0
												_A									=       len(__A      ) - 1

												while low <= high:
																	_A									=       (low + high) // 2
																	if array[mid] == index:
																						# If the item already exists in the array,
																						# insert it after the existing item
																						array.insert(mid + 1						,   __A      )
																						return array
																	elif array[mid] < index:
																						_A									=       mid + 1
																	else:
																						_A									=       mid - 1

        # If the item doesn't exist in the array, insert it at the appropriate position
												array.insert(__A						,   __A      )
												return array

							def 					__A       (     self:      Union[str, Any]						,   __A:      int						,   __A:      int      )			-> None:
												# Remove previous index
												if index in self.indexmap:
																	_A									=       self.indexmap[index]
																	if len(self.numbermap[n]      ) == 1:
																						del self.numbermap[n]
																	else:
																						_A									=       self.binary_search_delete(self.numbermap[n]						,   __A      )

        # Set new index
												_A									=       number

												# Number not seen before or empty so insert number value
												if number not in self.numbermap:
																	_A									=       [index]

												# Here we need to perform a binary search insertion in order to insert
												# The item in the correct place
												else:
																	_A									=       self.binary_search_insert(
																	    self.numbermap[number]						,   __A      )



							def 					__A       (     self:      List[Any]						,   __A:      int      )			-> int:
												return self.numbermap.get(__A						,   [-1]      )[0]


if __name__ == "__main__":
				import doctest

				doctest.testmod()



