



import os
import shutil
import tempfile
from unittest import TestCase
from unittest.mock import patch

import numpy as np
from datasets import Dataset

from transformers.models.realm.configuration_realm import RealmConfig
from transformers.models.realm.retrieval_realm import _REALM_BLOCK_RECORDS_FILENAME, RealmRetriever
from transformers.models.realm.tokenization_realm import VOCAB_FILES_NAMES, RealmTokenizer



class 						UpperCAmelCase_					( a):







							def      snake_case__   (     self):

														'''simple docstring'''

														_lowerCAmelCase  :							Dict				     =					tempfile.mkdtemp()
														_lowerCAmelCase  :							Dict				     =					5

														# Realm tok
														_lowerCAmelCase  :							List[str]				     =					[
														    "[UNK]",
														    "[CLS]",
														    "[SEP]",
														    "[PAD]",
														    "[MASK]",
														    "test",
														    "question",
														    "this",
														    "is",
														    "the",
														    "first",
														    "second",
														    "third",
														    "fourth",
														    "fifth",
														    "record",
														    "want",
														    "##want",
														    "##ed",
														    "wa",
														    "un",
														    "runn",
														    "##ing",
														    ",",
														    "low",
														    "lowest",
														]
														_lowerCAmelCase  :							int				     =					os.path.join(self.tmpdirname,    "realm_tokenizer")
														os.makedirs(__a,    exist_ok=__a)
														_lowerCAmelCase  :							Dict				     =					os.path.join(__a,    VOCAB_FILES_NAMES["vocab_file"])
														with open(self.vocab_file,    "w",    encoding="utf-8") as vocab_writer:
																					vocab_writer.write("".join([x + "\n" for x in vocab_tokens]))

														_lowerCAmelCase  :							List[str]				     =					os.path.join(self.tmpdirname,    "realm_block_records")
														os.makedirs(__a,    exist_ok=__a)







							def      snake_case__   (     self):

														'''simple docstring'''

														return RealmTokenizer.from_pretrained(os.path.join(self.tmpdirname,    "realm_tokenizer"))







							def      snake_case__   (     self):

														'''simple docstring'''

														shutil.rmtree(self.tmpdirname)







							def      snake_case__   (     self):

														'''simple docstring'''

														_lowerCAmelCase  :							Tuple				     =					RealmConfig(num_block_records=self.num_block_records)
														return config







							def      snake_case__   (     self):

														'''simple docstring'''

														_lowerCAmelCase  :							Optional[int]				     =					Dataset.from_dict(
														    {
														        "id": ["0", "1"],
														        "question": ["foo", "bar"],
														        "answers": [["Foo", "Bar"], ["Bar"]],
														    })
														return dataset







							def      snake_case__   (     self):

														'''simple docstring'''

														_lowerCAmelCase  :							Optional[Any]				     =					np.array(
														    [
														        B"This is the first record",
														        B"This is the second record",
														        B"This is the third record",
														        B"This is the fourth record",
														        B"This is the fifth record",
														        B"This is a longer longer longer record",
														    ],    dtype=__a,    )
														return block_records







							def      snake_case__   (     self):

														'''simple docstring'''

														_lowerCAmelCase  :							Union[str, Any]				     =					RealmRetriever(
														    block_records=self.get_dummy_block_records(),    tokenizer=self.get_tokenizer(),    )
														return retriever







							def      snake_case__   (     self):

														'''simple docstring'''

														_lowerCAmelCase  :							Optional[int]				     =					self.get_config()
														_lowerCAmelCase  :							List[Any]				     =					self.get_dummy_retriever()
														_lowerCAmelCase  :							Union[str, Any]				     =					retriever.tokenizer

														_lowerCAmelCase  :							Tuple				     =					np.array([0, 3],    dtype="long")
														_lowerCAmelCase  :							str				     =					tokenizer(["Test question"]).input_ids
														_lowerCAmelCase  :							Tuple				     =					tokenizer(
														    ["the fourth"],    add_special_tokens=__a,    return_token_type_ids=__a,    return_attention_mask=__a,    ).input_ids
														_lowerCAmelCase  :							List[str]				     =					config.reader_seq_len

														_lowerCAmelCase  :							List[Any]				     =					retriever(
														    __a,    __a,    answer_ids=__a,    max_length=__a,    return_tensors="np")

														self.assertEqual(len(__a),    2)
														self.assertEqual(len(__a),    2)
														self.assertEqual(len(__a),    2)
														self.assertEqual(concat_inputs.input_ids.shape,    (2, 10))
														self.assertEqual(concat_inputs.attention_mask.shape,    (2, 10))
														self.assertEqual(concat_inputs.token_type_ids.shape,    (2, 10))
														self.assertEqual(concat_inputs.special_tokens_mask.shape,    (2, 10))
														self.assertEqual(
														    tokenizer.convert_ids_to_tokens(concat_inputs.input_ids[0]),    ["[CLS]", "test", "question", "[SEP]", "this", "is", "the", "first", "record", "[SEP]"],    )
														self.assertEqual(
														    tokenizer.convert_ids_to_tokens(concat_inputs.input_ids[1]),    ["[CLS]", "test", "question", "[SEP]", "this", "is", "the", "fourth", "record", "[SEP]"],    )







							def      snake_case__   (     self):

														'''simple docstring'''

														_lowerCAmelCase  :							str				     =					self.get_config()
														_lowerCAmelCase  :							Tuple				     =					self.get_dummy_retriever()
														_lowerCAmelCase  :							str				     =					retriever.tokenizer

														_lowerCAmelCase  :							List[Any]				     =					np.array([0, 3, 5],    dtype="long")
														_lowerCAmelCase  :							Optional[Any]				     =					tokenizer(["Test question"]).input_ids
														_lowerCAmelCase  :							List[str]				     =					tokenizer(
														    ["the fourth", "longer longer"],    add_special_tokens=__a,    return_token_type_ids=__a,    return_attention_mask=__a,    ).input_ids
														_lowerCAmelCase  :							int				     =					config.reader_seq_len

														_lowerCAmelCase  :							Any				     =					retriever(
														    __a,    __a,    answer_ids=__a,    max_length=__a,    return_tensors="np")

														self.assertEqual([False, True, True],    __a)
														self.assertEqual([[-1, -1, -1], [6, -1, -1], [6, 7, 8]],    __a)
														self.assertEqual([[-1, -1, -1], [7, -1, -1], [7, 8, 9]],    __a)







							def      snake_case__   (     self):

														'''simple docstring'''

														_lowerCAmelCase  :							List[str]				     =					self.get_dummy_retriever()
														retriever.save_pretrained(os.path.join(self.tmpdirname,    "realm_block_records"))

														# Test local path
														_lowerCAmelCase  :							str				     =					retriever.from_pretrained(os.path.join(self.tmpdirname,    "realm_block_records"))
														self.assertEqual(retriever.block_records[0],    B"This is the first record")

														# Test mocked remote path
														with patch("transformers.models.realm.retrieval_realm.hf_hub_download") as mock_hf_hub_download:
																					_lowerCAmelCase  :							Dict				     =					os.path.join(
																					    os.path.join(self.tmpdirname,    "realm_block_records"),    _REALM_BLOCK_RECORDS_FILENAME)
																					_lowerCAmelCase  :							Any				     =					RealmRetriever.from_pretrained("google/realm-cc-news-pretrained-openqa")

														self.assertEqual(retriever.block_records[0],    B"This is the first record")





def        A       (      _lowerCamelCase						):





							'''simple docstring'''




							_lowerCAmelCase  :							int				     =					len(_lowerCamelCase						)
							for i in range(1       ,				_lowerCamelCase						):
														_lowerCAmelCase  :							List[Any]				     =					collection[i]
														_lowerCAmelCase  :							str				     =					0
														_lowerCAmelCase  :							Union[str, Any]				     =					i - 1

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


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