


'''simple docstring'''






import unittest

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

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 EsmForMaskedLM, EsmForSequenceClassification, EsmForTokenClassification, EsmModel
		from transformers.models.esm.modeling_esm import (
		    ESM_PRETRAINED_MODEL_ARCHIVE_LIST,
		    EsmEmbeddings,
		    create_position_ids_from_input_ids,
		)




class 						a__							:





							'''simple docstring'''






							def __init__(						self   ,       lowerCamelCase_   ,       lowerCamelCase_=13   ,       lowerCamelCase_=7   ,       lowerCamelCase_=False   ,       lowerCamelCase_=True   ,       lowerCamelCase_=False   ,       lowerCamelCase_=True   ,       lowerCamelCase_=33   ,       lowerCamelCase_=32   ,       lowerCamelCase_=5   ,       lowerCamelCase_=4   ,       lowerCamelCase_=37   ,       lowerCamelCase_="gelu"   ,       lowerCamelCase_=0.1   ,       lowerCamelCase_=0.1   ,       lowerCamelCase_=5_12   ,       lowerCamelCase_=16   ,       lowerCamelCase_=2   ,       lowerCamelCase_=0.02   ,       lowerCamelCase_=3   ,       lowerCamelCase_=4   ,       lowerCamelCase_=None   ,       )			->				List[str]:
														lowerCAmelCase__			  =      parent
														lowerCAmelCase__			  =      batch_size
														lowerCAmelCase__			  =      seq_length
														lowerCAmelCase__			  =      is_training
														lowerCAmelCase__			  =      use_input_mask
														lowerCAmelCase__			  =      use_token_type_ids
														lowerCAmelCase__			  =      use_labels
														lowerCAmelCase__			  =      vocab_size
														lowerCAmelCase__			  =      hidden_size
														lowerCAmelCase__			  =      num_hidden_layers
														lowerCAmelCase__			  =      num_attention_heads
														lowerCAmelCase__			  =      intermediate_size
														lowerCAmelCase__			  =      hidden_act
														lowerCAmelCase__			  =      hidden_dropout_prob
														lowerCAmelCase__			  =      attention_probs_dropout_prob
														lowerCAmelCase__			  =      max_position_embeddings
														lowerCAmelCase__			  =      type_vocab_size
														lowerCAmelCase__			  =      type_sequence_label_size
														lowerCAmelCase__			  =      initializer_range
														lowerCAmelCase__			  =      num_labels
														lowerCAmelCase__			  =      num_choices
														lowerCAmelCase__			  =      scope







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Tuple:
														lowerCAmelCase__			  =      ids_tensor([self.batch_size, self.seq_length]   ,       self.vocab_size  )

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

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

														lowerCAmelCase__			  =      self.get_config()

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







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Dict:
														return EsmConfig(
														    vocab_size=self.vocab_size   ,       hidden_size=self.hidden_size   ,       pad_token_id=1   ,       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   ,       initializer_range=self.initializer_range   ,       )







							def     __SCREAMING_SNAKE_CASE				(						self   ,       lowerCamelCase_   ,       lowerCamelCase_   ,       lowerCamelCase_   ,       lowerCamelCase_   ,       lowerCamelCase_   ,       lowerCamelCase_  )			->				Optional[int]:
														lowerCAmelCase__			  =      EsmModel(config=lowerCamelCase_  )
														model.to(lowerCamelCase_  )
														model.eval()
														lowerCAmelCase__			  =      model(lowerCamelCase_   ,       attention_mask=lowerCamelCase_  )
														lowerCAmelCase__			  =      model(lowerCamelCase_  )
														lowerCAmelCase__			  =      model(lowerCamelCase_  )

														self.parent.assertEqual(result.last_hidden_state.shape   ,       (self.batch_size, self.seq_length, self.hidden_size)  )
														self.parent.assertEqual(result.pooler_output.shape   ,       (self.batch_size, self.hidden_size)  )







							def     __SCREAMING_SNAKE_CASE				(						self   ,       lowerCamelCase_   ,       lowerCamelCase_   ,       lowerCamelCase_   ,       lowerCamelCase_   ,       lowerCamelCase_   ,       lowerCamelCase_  )			->				Any:
														lowerCAmelCase__			  =      EsmForMaskedLM(config=lowerCamelCase_  )
														model.to(lowerCamelCase_  )
														model.eval()
														lowerCAmelCase__			  =      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   ,       lowerCamelCase_   ,       lowerCamelCase_   ,       lowerCamelCase_   ,       lowerCamelCase_   ,       lowerCamelCase_   ,       lowerCamelCase_  )			->				Union[str, Any]:
														lowerCAmelCase__			  =      self.num_labels
														lowerCAmelCase__			  =      EsmForTokenClassification(config=lowerCamelCase_  )
														model.to(lowerCamelCase_  )
														model.eval()
														lowerCAmelCase__			  =      model(lowerCamelCase_   ,       attention_mask=lowerCamelCase_   ,       labels=lowerCamelCase_  )
														self.parent.assertEqual(result.logits.shape   ,       (self.batch_size, self.seq_length, self.num_labels)  )







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Dict:
														lowerCAmelCase__			  =      self.prepare_config_and_inputs()
														(
														    (
														    lowerCAmelCase__
														)  ,   (
														    lowerCAmelCase__
														)  ,   (
														    lowerCAmelCase__
														)  ,   (
														    lowerCAmelCase__
														)  ,   (
														    lowerCAmelCase__
														)  ,   (
														    lowerCAmelCase__
														)  ,   
														)			  =      config_and_inputs
														lowerCAmelCase__			  =      {'''input_ids''': input_ids, '''attention_mask''': input_mask}
														return config, inputs_dict




@require_torch
class 						a__							(				a__							,							a__							,							unittest.TestCase    ):





							'''simple docstring'''






							lowercase__     :  Optional[Any]      		= False

							lowercase__     :  Optional[Any]      		= (
							    (
							        EsmForMaskedLM,
							        EsmModel,
							        EsmForSequenceClassification,
							        EsmForTokenClassification,
							    )
							    if is_torch_available()
							    else ()
							)
							lowercase__     :  int      		= ()
							lowercase__     :  Optional[int]      		= (
							    {
							        "feature-extraction": EsmModel,
							        "fill-mask": EsmForMaskedLM,
							        "text-classification": EsmForSequenceClassification,
							        "token-classification": EsmForTokenClassification,
							        "zero-shot": EsmForSequenceClassification,
							    }
							    if is_torch_available()
							    else {}
							)
							lowercase__     :  Tuple      		= True







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Any:
														lowerCAmelCase__			  =      EsmModelTester(self  )
														lowerCAmelCase__			  =      ConfigTester(self   ,       config_class=lowerCamelCase_   ,       hidden_size=37  )







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Dict:
														self.config_tester.run_common_tests()







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Optional[Any]:
														lowerCAmelCase__			  =      self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_model(*lowerCamelCase_  )







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Dict:
														lowerCAmelCase__			  =      self.model_tester.prepare_config_and_inputs()
														for type in ["absolute", "relative_key", "relative_key_query"]:
																					lowerCAmelCase__			  =      type
																					self.model_tester.create_and_check_model(*lowerCamelCase_  )







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Optional[int]:
														lowerCAmelCase__			  =      self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_masked_lm(*lowerCamelCase_  )







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Tuple:
														lowerCAmelCase__			  =      self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_token_classification(*lowerCamelCase_  )







							@slow
							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Dict:
														for model_name in ESM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																					lowerCAmelCase__			  =      EsmModel.from_pretrained(lowerCamelCase_  )
																					self.assertIsNotNone(lowerCamelCase_  )







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Optional[Any]:
														lowerCAmelCase__			  =      self.model_tester.prepare_config_and_inputs()[0]
														lowerCAmelCase__			  =      EsmEmbeddings(config=lowerCamelCase_  )

														lowerCAmelCase__			  =      torch.as_tensor([[12, 31, 13, model.padding_idx]]  )
														lowerCAmelCase__			  =      torch.as_tensor(
														    [
														        [
														            0 + model.padding_idx + 1,
														            1 + model.padding_idx + 1,
														            2 + model.padding_idx + 1,
														            model.padding_idx,
														        ]
														    ]  )
														lowerCAmelCase__			  =      create_position_ids_from_input_ids(lowerCamelCase_   ,       model.padding_idx  )
														self.assertEqual(position_ids.shape   ,       expected_positions.shape  )
														self.assertTrue(torch.all(torch.eq(lowerCamelCase_   ,       lowerCamelCase_  )  )  )







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				List[str]:
														lowerCAmelCase__			  =      self.model_tester.prepare_config_and_inputs()[0]
														lowerCAmelCase__			  =      EsmEmbeddings(config=lowerCamelCase_  )

														lowerCAmelCase__			  =      torch.empty(2   ,       4   ,       30  )
														lowerCAmelCase__			  =      [
														    0 + embeddings.padding_idx + 1,
														    1 + embeddings.padding_idx + 1,
														    2 + embeddings.padding_idx + 1,
														    3 + embeddings.padding_idx + 1,
														]
														lowerCAmelCase__			  =      torch.as_tensor([expected_single_positions, expected_single_positions]  )
														lowerCAmelCase__			  =      embeddings.create_position_ids_from_inputs_embeds(lowerCamelCase_  )
														self.assertEqual(position_ids.shape   ,       expected_positions.shape  )
														self.assertTrue(torch.all(torch.eq(lowerCamelCase_   ,       lowerCamelCase_  )  )  )







							@unittest.skip('''Esm does not support embedding resizing'''  )
							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Optional[Any]:
														pass







							@unittest.skip('''Esm does not support embedding resizing'''  )
							def     __SCREAMING_SNAKE_CASE				(						self  )			->				List[Any]:
														pass







							@unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.'''  )
							def     __SCREAMING_SNAKE_CASE				(						self  )			->				List[Any]:
														pass




@require_torch
class 						a__							(				a__    ):





							'''simple docstring'''






							@slow
							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Any:
														with torch.no_grad():
																					lowerCAmelCase__			  =      EsmForMaskedLM.from_pretrained('''facebook/esm2_t6_8M_UR50D'''  )
																					model.eval()
																					lowerCAmelCase__			  =      torch.tensor([[0, 1, 2, 3, 4, 5]]  )
																					lowerCAmelCase__			  =      model(lowerCamelCase_  )[0]

																					lowerCAmelCase__			  =      33

																					lowerCAmelCase__			  =      torch.Size((1, 6, vocab_size)  )
																					self.assertEqual(output.shape   ,       lowerCamelCase_  )

																					lowerCAmelCase__			  =      torch.tensor(
																					    [[[8.9_215, -10.5_898, -6.4_671], [-6.3_967, -13.9_114, -1.1_212], [-7.7_812, -13.9_516, -3.7_406]]]  )
																					self.assertTrue(torch.allclose(output[:, :3, :3]   ,       lowerCamelCase_   ,       atol=1e-4  )  )







							@slow
							def     __SCREAMING_SNAKE_CASE				(						self  )			->				List[str]:
														with torch.no_grad():
																					lowerCAmelCase__			  =      EsmModel.from_pretrained('''facebook/esm2_t6_8M_UR50D'''  )
																					model.eval()

																					lowerCAmelCase__			  =      torch.tensor([[0, 6, 4, 13, 5, 4, 16, 12, 11, 7, 2]]  )
																					lowerCAmelCase__			  =      model(lowerCamelCase_  )[0]
																					# compare the actual values for a slice.
																					lowerCAmelCase__			  =      torch.tensor(
																					    [[[0.1_444, 0.5_413, 0.3_248], [0.3_034, 0.0_053, 0.3_108], [0.3_228, -0.2_499, 0.3_415]]]  )
																					self.assertTrue(torch.allclose(output[:, :3, :3]   ,       lowerCamelCase_   ,       atol=1e-4  )  )

"""simple docstring"""




import math
import sys




def 			_lowerCAmelCase		(		_UpperCamelCase    ):






  """simple docstring"""



  _lowercase:      Any   			=  ''''''
  try:
    with open(_UpperCamelCase			,					'''rb'''    ) as binary_file:
      _lowercase:      Dict   			=  binary_file.read()
    for dat in data:
      _lowercase:      List[str]   			=  f'''{dat:08b}'''
      result += curr_byte
    return result
  except OSError:
    print('''File not accessible'''    )
    sys.exit()




def 			_lowerCAmelCase		(		_UpperCamelCase    ):






  """simple docstring"""



  _lowercase:      Any   			=  {'''0''': '''0''', '''1''': '''1'''}
  _lowercase			,					_lowercase:      Optional[int]   			=  '''''', ''''''
  _lowercase:      Dict   			=  len(_UpperCamelCase    )

  for i in range(len(_UpperCamelCase    )    ):
    curr_string += data_bits[i]
    if curr_string not in lexicon:
      continue

    _lowercase:      Any   			=  lexicon[curr_string]
    result += last_match_id
    _lowercase:      Optional[int]   			=  last_match_id + '''0'''

    if math.loga(_UpperCamelCase    ).is_integer():
      _lowercase:      str   			=  {}
      for curr_key in list(_UpperCamelCase    ):
        _lowercase:      List[Any]   			=  lexicon.pop(_UpperCamelCase    )
      _lowercase:      Tuple   			=  new_lex

    _lowercase:      List[Any]   			=  last_match_id + '''1'''
    index += 1
    _lowercase:      List[Any]   			=  ''''''
  return result




def 			_lowerCAmelCase		(		_UpperCamelCase			,					_UpperCamelCase    ):






  """simple docstring"""



  _lowercase:      Optional[Any]   			=  8
  try:
    with open(_UpperCamelCase			,					'''wb'''    ) as opened_file:
      _lowercase:      List[Any]   			=  [
          to_write[i : i + byte_length]
          for i in range(0			,					len(_UpperCamelCase    )			,					_UpperCamelCase    )
      ]

      if len(result_byte_array[-1]    ) % byte_length == 0:
        result_byte_array.append('''10000000'''    )
      else:
        result_byte_array[-1] += "1" + "0" * (
            byte_length - len(result_byte_array[-1]    ) - 1
        )

      for elem in result_byte_array[:-1]:
        opened_file.write(int(_UpperCamelCase			,					2    ).to_bytes(1			,					byteorder='''big'''    )    )
  except OSError:
    print('''File not accessible'''    )
    sys.exit()




def 			_lowerCAmelCase		(		_UpperCamelCase    ):






  """simple docstring"""



  _lowercase:      Any   			=  0
  for letter in data_bits:
    if letter == "1":
      break
    counter += 1

  _lowercase:      List[str]   			=  data_bits[counter:]
  _lowercase:      Union[str, Any]   			=  data_bits[counter + 1 :]
  return data_bits




def 			_lowerCAmelCase		(		_UpperCamelCase			,					_UpperCamelCase    ):






  """simple docstring"""



  _lowercase:      Tuple   			=  read_file_binary(_UpperCamelCase    )
  _lowercase:      Optional[Any]   			=  remove_prefix(_UpperCamelCase    )
  _lowercase:      Dict   			=  decompress_data(_UpperCamelCase    )
  write_file_binary(_UpperCamelCase			,					_UpperCamelCase    )


if __name__ == "__main__":
  compress(sys.argv[1], sys.argv[2])
