



"""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.models.esm.modeling_esmfold import EsmForProteinFolding






class      UpperCAmelCase_  :



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



      def 						snake_case_ (					self					:     int						):
           _UpperCAmelCase      :     Optional[int]        =   ids_tensor([self.batch_size, self.seq_length]					,     self.vocab_size						)

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

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

           _UpperCAmelCase      :     Optional[int]        =   self.get_config()

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



      def 						snake_case_ (					self					:     int						):
           _UpperCAmelCase      :     Any        =   EsmConfig(
               vocab_size=3_3					,     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					,     is_folding_model=A					,     esmfold_config={"trunk": {"num_blocks": 2}, "fp16_esm": False}					,     )
           return config



      def 						snake_case_ (					self					:     List[Any]					,     A					:     Tuple					,     A					:     str					,     A					:     str					,     A					:     int					,     A					:     Dict					,     A					:     int						):
           _UpperCAmelCase      :     str        =   EsmForProteinFolding(config=A						).float()
           model.to(A						)
           model.eval()
           _UpperCAmelCase      :     int        =   model(A					,     attention_mask=A						)
           _UpperCAmelCase      :     Tuple        =   model(A						)
           _UpperCAmelCase      :     Tuple        =   model(A						)

           self.parent.assertEqual(result.positions.shape					,     (8, self.batch_size, self.seq_length, 1_4, 3)						)
           self.parent.assertEqual(result.angles.shape					,     (8, self.batch_size, self.seq_length, 7, 2)						)



      def 						snake_case_ (					self					:     Dict						):
           _UpperCAmelCase      :     Dict        =   self.prepare_config_and_inputs()
           (
               (
               _UpperCAmelCase
           )    ,	(
               _UpperCAmelCase
           )    ,	(
               _UpperCAmelCase
           )    ,	(
               _UpperCAmelCase
           )    ,	(
               _UpperCAmelCase
           )    ,	(
               _UpperCAmelCase
           )    ,	
           )      :     Optional[Any]        =   config_and_inputs
           _UpperCAmelCase      :     Optional[int]        =   {"input_ids": input_ids, "attention_mask": input_mask}
           return config, inputs_dict







@require_torch
class      UpperCAmelCase_  (    _UpperCamelCase	,      _UpperCamelCase	,      unittest.TestCase						):
      __SCREAMING_SNAKE_CASE						:       Dict								=			False

      __SCREAMING_SNAKE_CASE						:       Dict								=			(EsmForProteinFolding,) if is_torch_available() else ()
      __SCREAMING_SNAKE_CASE						:       int								=			()
      __SCREAMING_SNAKE_CASE						:       Union[str, Any]								=			{} if is_torch_available() else {}
      __SCREAMING_SNAKE_CASE						:       str								=			False



      def 						snake_case_ (					self					:     List[Any]						):
           _UpperCAmelCase      :     Optional[int]        =   EsmFoldModelTester(self						)
           _UpperCAmelCase      :     str        =   ConfigTester(self					,     config_class=A					,     hidden_size=3_7						)



      def 						snake_case_ (					self					:     Any						):
           self.config_tester.run_common_tests()



      def 						snake_case_ (					self					:     List[Any]						):
           _UpperCAmelCase      :     Tuple        =   self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*A						)



      @unittest.skip("Does not support attention outputs"						)
      def 						snake_case_ (					self					:     Dict						):
           pass



      @unittest.skip
      def 						snake_case_ (					self					:     str						):
           pass



      @unittest.skip("Esm does not support embedding resizing"						)
      def 						snake_case_ (					self					:     Union[str, Any]						):
           pass



      @unittest.skip("Esm does not support embedding resizing"						)
      def 						snake_case_ (					self					:     Optional[int]						):
           pass



      @unittest.skip("ESMFold does not support passing input embeds!"						)
      def 						snake_case_ (					self					:     Any						):
           pass



      @unittest.skip("ESMFold does not support head pruning."						)
      def 						snake_case_ (					self					:     List[Any]						):
           pass



      @unittest.skip("ESMFold does not support head pruning."						)
      def 						snake_case_ (					self					:     Union[str, Any]						):
           pass



      @unittest.skip("ESMFold does not support head pruning."						)
      def 						snake_case_ (					self					:     List[str]						):
           pass



      @unittest.skip("ESMFold does not support head pruning."						)
      def 						snake_case_ (					self					:     Optional[Any]						):
           pass



      @unittest.skip("ESMFold does not support head pruning."						)
      def 						snake_case_ (					self					:     Tuple						):
           pass



      @unittest.skip("ESMFold does not output hidden states in the normal way."						)
      def 						snake_case_ (					self					:     str						):
           pass



      @unittest.skip("ESMfold does not output hidden states in the normal way."						)
      def 						snake_case_ (					self					:     Union[str, Any]						):
           pass



      @unittest.skip("ESMFold only has one output format."						)
      def 						snake_case_ (					self					:     Optional[Any]						):
           pass



      @unittest.skip("This test doesn't work for ESMFold and doesn't test core functionality"						)
      def 						snake_case_ (					self					:     Union[str, Any]						):
           pass



      @unittest.skip("ESMFold does not support input chunking."						)
      def 						snake_case_ (					self					:     Optional[int]						):
           pass



      @unittest.skip("ESMFold doesn't respect you and it certainly doesn't respect your initialization arguments."						)
      def 						snake_case_ (					self					:     Optional[Any]						):
           pass



      @unittest.skip("ESMFold doesn't support torchscript compilation."						)
      def 						snake_case_ (					self					:     List[Any]						):
           pass



      @unittest.skip("ESMFold doesn't support torchscript compilation."						)
      def 						snake_case_ (					self					:     Union[str, Any]						):
           pass



      @unittest.skip("ESMFold doesn't support torchscript compilation."						)
      def 						snake_case_ (					self					:     Dict						):
           pass



      @unittest.skip("ESMFold doesn't support data parallel."						)
      def 						snake_case_ (					self					:     Any						):
           pass



      @unittest.skip("Will be fixed soon by reducing the size of the model used for common tests."						)
      def 						snake_case_ (					self					:     List[str]						):
           pass







@require_torch
class      UpperCAmelCase_  (    _UpperCamelCase						):



      @slow
      def 						snake_case_ (					self					:     Optional[int]						):
           _UpperCAmelCase      :     str        =   EsmForProteinFolding.from_pretrained("facebook/esmfold_v1"						).float()
           model.eval()
           _UpperCAmelCase      :     Tuple        =   torch.tensor([[0, 6, 4, 1_3, 5, 4, 1_6, 1_2, 1_1, 7, 2]]						)
           _UpperCAmelCase      :     int        =   model(A						)["positions"]
           _UpperCAmelCase      :     Dict        =   torch.tensor([2.5_828, 0.7_993, -10.9_334]					,     dtype=torch.floataa						)
           self.assertTrue(torch.allclose(position_outputs[0, 0, 0, 0]					,     A					,     atol=1e-4						)						)





"""simple docstring"""




import json
import os
import unittest

from transformers.models.gptsan_japanese.tokenization_gptsan_japanese import (
    VOCAB_FILES_NAMES,
    GPTSanJapaneseTokenizer,
)
from transformers.testing_utils import require_tokenizers, slow

from ...test_tokenization_common import TokenizerTesterMixin






@require_tokenizers
class      UpperCAmelCase_  (    _UpperCamelCase	,      unittest.TestCase						):
      __SCREAMING_SNAKE_CASE						:       Dict								=			GPTSanJapaneseTokenizer
      __SCREAMING_SNAKE_CASE						:       Optional[int]								=			False
      __SCREAMING_SNAKE_CASE						:       List[str]								=			{'do_clean_text': False, 'add_prefix_space': False}



      def 						snake_case_ (					self					:     Any						):
           super().setUp()

           # fmt: off
           _UpperCAmelCase      :     Any        =   ["こん", "こんに", "にちは", "ばんは", "世界,㔺界", "、", "。", "<BR>", "<SP>", "<TAB>", "<URL>", "<EMAIL>", "<TEL>", "<DATE>", "<PRICE>", "<BLOCK>", "<KIGOU>", "<U2000U2BFF>", "<|emoji1|>", "<unk>", "<|bagoftoken|>", "<|endoftext|>"]
           # fmt: on
           _UpperCAmelCase      :     Optional[int]        =   {"emoji": {"\ud83d\ude00": "<|emoji1|>"}, "emoji_inv": {"<|emoji1|>": "\ud83d\ude00"}}  # 😀
           _UpperCAmelCase      :     List[Any]        =   {"unk_token": "<unk>"}

           _UpperCAmelCase      :     List[Any]        =   os.path.join(self.tmpdirname					,     VOCAB_FILES_NAMES["vocab_file"]						)
           _UpperCAmelCase      :     Any        =   os.path.join(self.tmpdirname					,     VOCAB_FILES_NAMES["emoji_file"]						)
           with open(self.vocab_file					,     "w"					,     encoding="utf-8"						) as vocab_writer:
                vocab_writer.write("".join([x + "\n" for x in vocab_tokens]						)						)
           with open(self.emoji_file					,     "w"						) as emoji_writer:
                emoji_writer.write(json.dumps(A						)						)



      def 						snake_case_ (					self					:     int					,     **A					:     List[str]						):
           kwargs.update(self.special_tokens_map						)
           return GPTSanJapaneseTokenizer.from_pretrained(self.tmpdirname					,     **A						)



      def 						snake_case_ (					self					:     int					,     A					:     Any						):
           _UpperCAmelCase      :     Optional[Any]        =   "こんにちは、世界。 \nこんばんは、㔺界。😀"
           _UpperCAmelCase      :     List[Any]        =   "こんにちは、世界。 \nこんばんは、世界。😀"
           return input_text, output_text



      def 						snake_case_ (					self					:     Optional[Any]					,     A					:     str						):
           _UpperCAmelCase    ,	_UpperCAmelCase      :     str        =   self.get_input_output_texts(A						)
           _UpperCAmelCase      :     List[Any]        =   tokenizer.encode(A					,     add_special_tokens=A						)
           _UpperCAmelCase      :     Union[str, Any]        =   tokenizer.decode(A					,     clean_up_tokenization_spaces=A						)
           return text, ids



      def 						snake_case_ (					self					:     Any						):
           pass  # TODO add if relevant



      def 						snake_case_ (					self					:     Union[str, Any]						):
           pass  # TODO add if relevant



      def 						snake_case_ (					self					:     int						):
           pass  # TODO add if relevant



      def 						snake_case_ (					self					:     List[str]						):
           _UpperCAmelCase      :     List[Any]        =   self.get_tokenizer()

           # Testing tokenization
           _UpperCAmelCase      :     Optional[int]        =   "こんにちは、世界。　こんばんは、㔺界。"
           _UpperCAmelCase      :     Dict        =   ["こん", "にちは", "、", "世界", "。", "<SP>", "こん", "ばんは", "、", "㔺界", "。"]
           _UpperCAmelCase      :     List[Any]        =   tokenizer.tokenize(A						)
           self.assertListEqual(A					,     A						)

           # Testing conversion to ids without special tokens
           _UpperCAmelCase      :     Optional[int]        =   [0, 2, 5, 4, 6, 8, 0, 3, 5, 4, 6]
           _UpperCAmelCase      :     List[str]        =   tokenizer.convert_tokens_to_ids(A						)
           self.assertListEqual(A					,     A						)

           # Testing conversion to ids with special tokens
           _UpperCAmelCase      :     str        =   tokens + [tokenizer.unk_token]
           _UpperCAmelCase      :     Any        =   [0, 2, 5, 4, 6, 8, 0, 3, 5, 4, 6, 1_9]
           _UpperCAmelCase      :     str        =   tokenizer.convert_tokens_to_ids(A						)
           self.assertListEqual(A					,     A						)



      def 						snake_case_ (					self					:     Any						):
           _UpperCAmelCase      :     Union[str, Any]        =   self.get_tokenizer()

           # Testing tokenization
           _UpperCAmelCase      :     Dict        =   "こんにちは、<|bagoftoken|>世界。こんばんは、<|bagoftoken|>㔺界。"
           _UpperCAmelCase      :     Tuple        =   "こんにちは、、、、世界。こんばんは、、、、世界。"
           _UpperCAmelCase      :     int        =   tokenizer.encode(A						)
           _UpperCAmelCase      :     Optional[Any]        =   tokenizer.decode(A						)
           self.assertEqual(A					,     A						)



      @slow
      def 						snake_case_ (					self					:     Dict						):
           _UpperCAmelCase      :     List[Any]        =   self.tokenizer_class.from_pretrained("Tanrei/GPTSAN-japanese"						)

           # Testing tokenization
           _UpperCAmelCase      :     List[Any]        =   "こんにちは、世界。"
           _UpperCAmelCase      :     List[str]        =   "こんばんは、㔺界。😀"
           _UpperCAmelCase      :     Any        =   "こんにちは、世界。こんばんは、世界。😀"
           _UpperCAmelCase      :     Union[str, Any]        =   tokenizer.encode(prefix_text + input_text						)
           _UpperCAmelCase      :     Tuple        =   tokenizer.encode(""					,     prefix_text=prefix_text + input_text						)
           _UpperCAmelCase      :     Optional[int]        =   tokenizer.encode(A					,     prefix_text=A						)
           _UpperCAmelCase      :     Tuple        =   tokenizer.decode(A						)
           _UpperCAmelCase      :     Optional[Any]        =   tokenizer.decode(A						)
           _UpperCAmelCase      :     Tuple        =   tokenizer.decode(A						)
           self.assertEqual(A					,     A						)
           self.assertEqual(A					,     A						)
           self.assertEqual(A					,     A						)



      @slow
      def 						snake_case_ (					self					:     Optional[Any]						):
           _UpperCAmelCase      :     Dict        =   self.tokenizer_class.from_pretrained("Tanrei/GPTSAN-japanese"						)

           # Testing tokenization
           _UpperCAmelCase      :     Any        =   "こんにちは、世界。"
           _UpperCAmelCase      :     List[Any]        =   "こんばんは、㔺界。😀"

           _UpperCAmelCase      :     Optional[Any]        =   len(tokenizer.encode(A						)						) - 2
           _UpperCAmelCase      :     List[Any]        =   len(tokenizer.encode(A						)						) - 2

           _UpperCAmelCase      :     List[str]        =   [1] + [0] * (len_prefix + len_text + 1)
           _UpperCAmelCase      :     str        =   [1] * (len_prefix + len_text + 1) + [0]
           _UpperCAmelCase      :     int        =   [1] + [1] * (len_prefix) + [0] * (len_text + 1)

           _UpperCAmelCase      :     Union[str, Any]        =   tokenizer(prefix_text + input_text						).token_type_ids
           _UpperCAmelCase      :     Any        =   tokenizer(""					,     prefix_text=prefix_text + input_text						).token_type_ids
           _UpperCAmelCase      :     List[Any]        =   tokenizer(A					,     prefix_text=A						).token_type_ids
           self.assertListEqual(A					,     A						)
           self.assertListEqual(A					,     A						)
           self.assertListEqual(A					,     A						)



      @slow
      def 						snake_case_ (					self					:     List[str]						):
           _UpperCAmelCase      :     str        =   self.tokenizer_class.from_pretrained("Tanrei/GPTSAN-japanese"						)

           _UpperCAmelCase      :     Dict        =   tokenizer.encode("あンいワ"						)
           _UpperCAmelCase      :     str        =   tokenizer.encode(""					,     prefix_text="あンいワ"						)
           _UpperCAmelCase      :     Dict        =   tokenizer.encode("いワ"					,     prefix_text="あン"						)

           self.assertEqual(tokenizer.decode(A						)					,     tokenizer.decode(A						)						)
           self.assertEqual(tokenizer.decode(A						)					,     tokenizer.decode(A						)						)
           self.assertNotEqual(A					,     A						)
           self.assertNotEqual(A					,     A						)
           self.assertEqual(x_token_a[1]					,     x_token_a[-1]						)  # SEG token
           self.assertEqual(x_token_a[1]					,     x_token_a[3]						)  # SEG token



      @slow
      def 						snake_case_ (					self					:     List[str]						):
           _UpperCAmelCase      :     Optional[Any]        =   self.tokenizer_class.from_pretrained("Tanrei/GPTSAN-japanese"						)

           _UpperCAmelCase      :     Tuple        =   [["武田信玄", "は、"], ["織田信長", "の配下の、"]]
           _UpperCAmelCase      :     Tuple        =   tokenizer(A					,     padding=A						)
           _UpperCAmelCase      :     str        =   tokenizer.batch_encode_plus(A					,     padding=A						)

           # fmt: off
           _UpperCAmelCase      :     str        =   [[3_5_9_9_3, 8_6_4_0, 2_5_9_4_8, 3_5_9_9_8, 3_0_6_4_7, 3_5_6_7_5, 3_5_9_9_9, 3_5_9_9_9], [3_5_9_9_3, 1_0_3_8_2, 9_8_6_8, 3_5_9_9_8, 3_0_6_4_6, 9_4_5_9, 3_0_6_4_6, 3_5_6_7_5]]
           _UpperCAmelCase      :     str        =   [[1, 1, 1, 0, 0, 0, 0, 0], [1, 1, 1, 0, 0, 0, 0, 0]]
           _UpperCAmelCase      :     int        =   [[1, 1, 1, 1, 1, 1, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1]]
           # fmt: on
           self.assertListEqual(x_token.input_ids					,     A						)
           self.assertListEqual(x_token.token_type_ids					,     A						)
           self.assertListEqual(x_token.attention_mask					,     A						)
           self.assertListEqual(x_token_a.input_ids					,     A						)
           self.assertListEqual(x_token_a.token_type_ids					,     A						)
           self.assertListEqual(x_token_a.attention_mask					,     A						)



      def 						snake_case_ (					self					:     List[Any]						):
           # Intentionally convert some words to accommodate character fluctuations unique to Japanese
           pass



      def 						snake_case_ (					self					:     int						):
           # tokenizer has no padding token
           pass
