



import os
import sys
import warnings
from dataclasses import dataclass, field
from io import BytesIO
from typing import TYPE_CHECKING, Any, ClassVar, Dict, List, Optional, Union

import numpy as np
import pyarrow as pa

from .. import config
from ..download.streaming_download_manager import xopen
from ..table import array_cast
from ..utils.file_utils import is_local_path
from ..utils.py_utils import first_non_null_value, no_op_if_value_is_null, string_to_dict


if TYPE_CHECKING:
							import PIL.Image

							from .features import FeatureType


lowercase_										=  None
lowercase_										=  """<""" if sys.byteorder == """little""" else """>"""
# Origin: https://github.com/python-pillow/Pillow/blob/698951e19e19972aeed56df686868f1329981c12/src/PIL/Image.py#L3126 minus "|i1" which values are not preserved correctly when saving and loading an image
lowercase_										=  [
    np.dtype("""|b1"""),
    np.dtype("""|u1"""),
    np.dtype("""<u2"""),
    np.dtype(""">u2"""),
    np.dtype("""<i2"""),
    np.dtype(""">i2"""),
    np.dtype("""<u4"""),
    np.dtype(""">u4"""),
    np.dtype("""<i4"""),
    np.dtype(""">i4"""),
    np.dtype("""<f4"""),
    np.dtype(""">f4"""),
    np.dtype("""<f8"""),
    np.dtype(""">f8"""),
]
@dataclass
class 					_snake_case      :
				UpperCamelCase__					:       bool  		=True
				UpperCamelCase__					:       Optional[str]  		=None
				# Automatically constructed
				UpperCamelCase__					:       ClassVar[str]  		="PIL.Image.Image"
				UpperCamelCase__					:       ClassVar[Any]  		=pa.struct({"""bytes""": pa.binary(), """path""": pa.string()})
				UpperCamelCase__					:       str  		=field(default="""Image"""     ,						init=__lowercase     ,						repr=__lowercase)




				def __call__(    self       :    int    ):
							return self.pa_type




				def 				A__   (    self       :    List[Any],       __lowercase       :    str    ):




							if config.PIL_AVAILABLE:
										import PIL.Image
							else:
										raise ImportError("To support encoding images, please install \'Pillow\'."    )

							if isinstance(_lowerCAmelCase,       _lowerCAmelCase    ):
										lowercase__       	=					np.array(_lowerCAmelCase    )

							if isinstance(_lowerCAmelCase,       _lowerCAmelCase    ):
										return {"path": value, "bytes": None}
							elif isinstance(_lowerCAmelCase,       _lowerCAmelCase    ):
										return {"path": None, "bytes": value}
							elif isinstance(_lowerCAmelCase,       np.ndarray    ):
										# convert the image array to PNG/TIFF bytes
										return encode_np_array(_lowerCAmelCase    )
							elif isinstance(_lowerCAmelCase,       PIL.Image.Image    ):
										# convert the PIL image to bytes (default format is PNG/TIFF)
										return encode_pil_image(_lowerCAmelCase    )
							elif value.get("path"    ) is not None and os.path.isfile(value["path"]    ):
										# we set "bytes": None to not duplicate the data if they're already available locally
										return {"bytes": None, "path": value.get("path"    )}
							elif value.get("bytes"    ) is not None or value.get("path"    ) is not None:
										# store the image bytes, and path is used to infer the image format using the file extension
										return {"bytes": value.get("bytes"    ), "path": value.get("path"    )}
							else:
										raise ValueError(
										    F'''An image sample should have one of \'path\' or \'bytes\' but they are missing or None in {value}.'''    )




				def 				A__   (    self       :    Optional[Any],       __lowercase       :    Dict,       __lowercase       :    Tuple=None    ):




							if not self.decode:
										raise RuntimeError("Decoding is disabled for this feature. Please use Image(decode=True) instead."    )

							if config.PIL_AVAILABLE:
										import PIL.Image
							else:
										raise ImportError("To support decoding images, please install \'Pillow\'."    )

							if token_per_repo_id is None:
										lowercase__       	=					{}

							lowercase__       	=					value['''path'''], value['''bytes''']
							if bytes_ is None:
										if path is None:
													raise ValueError(F'''An image should have one of \'path\' or \'bytes\' but both are None in {value}.'''    )
										else:
													if is_local_path(_lowerCAmelCase    ):
																lowercase__       	=					PIL.Image.open(_lowerCAmelCase    )
													else:
																lowercase__       	=					path.split("::"    )[-1]
																try:
																			lowercase__       	=					string_to_dict(_lowerCAmelCase,       config.HUB_DATASETS_URL    )['''repo_id''']
																			lowercase__       	=					token_per_repo_id.get(_lowerCAmelCase    )
																except ValueError:
																			lowercase__       	=					None
																with xopen(_lowerCAmelCase,       "rb",       use_auth_token=_lowerCAmelCase    ) as f:
																			lowercase__       	=					BytesIO(f.read()    )
																lowercase__       	=					PIL.Image.open(bytes_    )
							else:
										lowercase__       	=					PIL.Image.open(BytesIO(bytes_    )    )
							image.load()  # to avoid "Too many open files" errors
							return image




				def 				A__   (    self       :    str    ):
							from .features import Value

							return (
							    self
							    if self.decode
							    else {
							        "bytes": Value("binary"    ),
							        "path": Value("string"    ),
							    }
							)




				def 				A__   (    self       :    Optional[int],       __lowercase       :    List[Any]    ):




							if pa.types.is_string(storage.type    ):
										lowercase__       	=					pa.array([None] * len(_lowerCAmelCase    ),       type=pa.binary()    )
										lowercase__       	=					pa.StructArray.from_arrays([bytes_array, storage],       ["bytes", "path"],       mask=storage.is_null()    )
							elif pa.types.is_binary(storage.type    ):
										lowercase__       	=					pa.array([None] * len(_lowerCAmelCase    ),       type=pa.string()    )
										lowercase__       	=					pa.StructArray.from_arrays([storage, path_array],       ["bytes", "path"],       mask=storage.is_null()    )
							elif pa.types.is_struct(storage.type    ):
										if storage.type.get_field_index("bytes"    ) >= 0:
													lowercase__       	=					storage.field("bytes"    )
										else:
													lowercase__       	=					pa.array([None] * len(_lowerCAmelCase    ),       type=pa.binary()    )
										if storage.type.get_field_index("path"    ) >= 0:
													lowercase__       	=					storage.field("path"    )
										else:
													lowercase__       	=					pa.array([None] * len(_lowerCAmelCase    ),       type=pa.string()    )
										lowercase__       	=					pa.StructArray.from_arrays([bytes_array, path_array],       ["bytes", "path"],       mask=storage.is_null()    )
							elif pa.types.is_list(storage.type    ):
										lowercase__       	=					pa.array(
										    [encode_np_array(np.array(_lowerCAmelCase    )    )["bytes"] if arr is not None else None for arr in storage.to_pylist()],       type=pa.binary(),       )
										lowercase__       	=					pa.array([None] * len(_lowerCAmelCase    ),       type=pa.string()    )
										lowercase__       	=					pa.StructArray.from_arrays(
										    [bytes_array, path_array],       ["bytes", "path"],       mask=bytes_array.is_null()    )
							return array_cast(_lowerCAmelCase,       self.pa_type    )




				def 				A__   (    self       :    int,       __lowercase       :    Union[str, Any]    ):




							@no_op_if_value_is_null
							def path_to_bytes(__lowercase       :    str    ):
										with xopen(_lowerCAmelCase,       "rb"    ) as f:
													lowercase__       	=					f.read()
										return bytes_

							lowercase__       	=					pa.array(
							    [
							        (path_to_bytes(x["path"]    ) if x["bytes"] is None else x["bytes"]) if x is not None else None
							        for x in storage.to_pylist()
							    ],       type=pa.binary(),       )
							lowercase__       	=					pa.array(
							    [os.path.basename(_lowerCAmelCase    ) if path is not None else None for path in storage.field("path"    ).to_pylist()],       type=pa.string(),       )
							lowercase__       	=					pa.StructArray.from_arrays([bytes_array, path_array],       ["bytes", "path"],       mask=bytes_array.is_null()    )
							return array_cast(_lowerCAmelCase,       self.pa_type    )



def __lowerCAmelCase			(  ):





			if config.PIL_AVAILABLE:
						import PIL.Image
			else:
						raise ImportError("To support encoding images, please install \'Pillow\'."	)

			global _IMAGE_COMPRESSION_FORMATS
			if _IMAGE_COMPRESSION_FORMATS is None:
						PIL.Image.init()
						lowercase__       	=					list(set(PIL.Image.OPEN.keys()	) & set(PIL.Image.SAVE.keys()	)	)
			return _IMAGE_COMPRESSION_FORMATS



def __lowerCAmelCase			(  SCREAMING_SNAKE_CASE_	):
			lowercase__       	=					BytesIO()
			if image.format in list_image_compression_formats():
						lowercase__       	=					image.format
			else:
						lowercase__       	=					'''PNG''' if image.mode in ['''1''', '''L''', '''LA''', '''RGB''', '''RGBA'''] else '''TIFF'''
			image.save(UpperCamelCase__    ,	format=UpperCamelCase__	)
			return buffer.getvalue()



def __lowerCAmelCase			(  SCREAMING_SNAKE_CASE_	):





			if hasattr(UpperCamelCase__    ,	"filename"	) and image.filename != "":
						return {"path": image.filename, "bytes": None}
			else:
						return {"path": None, "bytes": image_to_bytes(UpperCamelCase__	)}



def __lowerCAmelCase			(  SCREAMING_SNAKE_CASE_	):





			if config.PIL_AVAILABLE:
						import PIL.Image
			else:
						raise ImportError("To support encoding images, please install \'Pillow\'."	)

			lowercase__       	=					array.dtype
			lowercase__       	=					dtype.byteorder if dtype.byteorder != '''=''' else _NATIVE_BYTEORDER
			lowercase__       	=					dtype.kind
			lowercase__       	=					dtype.itemsize

			lowercase__       	=					None

			# Multi-channel array case (only np.dtype("|u1") is allowed)
			if array.shape[2:]:
						lowercase__       	=					np.dtype("|u1"	)
						if dtype_kind not in ["u", "i"]:
									raise TypeError(
									    f'''Unsupported array dtype {dtype} for image encoding. Only {dest_dtype} is supported for multi-channel arrays.'''	)
						if dtype is not dest_dtype:
									warnings.warn(f'''Downcasting array dtype {dtype} to {dest_dtype} to be compatible with \'Pillow\''''	)
    # Exact match
			elif dtype in _VALID_IMAGE_ARRAY_DTPYES:
						lowercase__       	=					dtype
			else:  # Downcast the type within the kind (np.can_cast(from_type, to_type, casting="same_kind") doesn't behave as expected, so do it manually)
						while dtype_itemsize >= 1:
									lowercase__       	=					dtype_byteorder + dtype_kind + str(UpperCamelCase__	)
									lowercase__       	=					np.dtype(UpperCamelCase__	)
									if dest_dtype in _VALID_IMAGE_ARRAY_DTPYES:
												warnings.warn(f'''Downcasting array dtype {dtype} to {dest_dtype} to be compatible with \'Pillow\''''	)
												break
									else:
												dtype_itemsize //= 2

			if dest_dtype is None:
						raise TypeError(
						    f'''Cannot convert dtype {dtype} to a valid image dtype. Valid image dtypes: {_VALID_IMAGE_ARRAY_DTPYES}'''	)
			lowercase__       	=					PIL.Image.fromarray(array.astype(UpperCamelCase__	)	)
			return {"path": None, "bytes": image_to_bytes(UpperCamelCase__	)}




def __lowerCAmelCase			(  SCREAMING_SNAKE_CASE_	):





			if config.PIL_AVAILABLE:
						import PIL.Image
			else:
						raise ImportError("To support encoding images, please install \'Pillow\'."	)

			if objs:
						lowercase__       	=					first_non_null_value(UpperCamelCase__	)
						if isinstance(UpperCamelCase__    ,	UpperCamelCase__	):
									return [{"path": obj, "bytes": None} if obj is not None else None for obj in objs]
						if isinstance(UpperCamelCase__    ,	np.ndarray	):
									lowercase__       	=					no_op_if_value_is_null(UpperCamelCase__	)
									return [obj_to_image_dict_func(UpperCamelCase__	) for obj in objs]
						elif isinstance(UpperCamelCase__    ,	PIL.Image.Image	):
									lowercase__       	=					no_op_if_value_is_null(UpperCamelCase__	)
									return [obj_to_image_dict_func(UpperCamelCase__	) for obj in objs]
						else:
									return objs
			else:
						return objs











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			:






				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=512			, _lowerCAmelCase=16			, _lowerCAmelCase=2			, _lowerCAmelCase=0.02			, _lowerCAmelCase=3			, _lowerCAmelCase=4			, _lowerCAmelCase=None			, ):

										"""simple docstring"""




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






				def 			snake_case_    (  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				List[str]           =   ids_tensor([self.batch_size, self.seq_length]			, self.vocab_size					)

										__SCREAMING_SNAKE_CASE:				int           =   None
										if self.use_input_mask:
																__SCREAMING_SNAKE_CASE:				Tuple           =   random_attention_mask([self.batch_size, self.seq_length]					)

										__SCREAMING_SNAKE_CASE:				Optional[Any]           =   None
										__SCREAMING_SNAKE_CASE:				Optional[int]           =   None
										__SCREAMING_SNAKE_CASE:				Optional[Any]           =   None
										if self.use_labels:
																__SCREAMING_SNAKE_CASE:				List[Any]           =   ids_tensor([self.batch_size]			, self.type_sequence_label_size					)
																__SCREAMING_SNAKE_CASE:				List[str]           =   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:				Optional[int]           =   self.get_config()

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






				def 			snake_case_    (  self					):

										"""simple docstring"""




										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 			snake_case_    (  self			, _lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				List[Any]           =   EsmModel(config=_lowerCAmelCase					)
										model.to(_lowerCAmelCase					)
										model.eval()
										__SCREAMING_SNAKE_CASE:				Union[str, Any]           =   model(_lowerCAmelCase			, attention_mask=_lowerCAmelCase					)
										__SCREAMING_SNAKE_CASE:				Tuple           =   model(_lowerCAmelCase					)
										__SCREAMING_SNAKE_CASE:				int           =   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 			snake_case_    (  self			, _lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				List[Any]           =   EsmForMaskedLM(config=_lowerCAmelCase					)
										model.to(_lowerCAmelCase					)
										model.eval()
										__SCREAMING_SNAKE_CASE:				Optional[Any]           =   model(_lowerCAmelCase			, attention_mask=_lowerCAmelCase			, labels=_lowerCAmelCase					)
										self.parent.assertEqual(result.logits.shape			, (self.batch_size, self.seq_length, self.vocab_size)					)






				def 			snake_case_    (  self			, _lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				Any           =   self.num_labels
										__SCREAMING_SNAKE_CASE:				List[Any]           =   EsmForTokenClassification(config=_lowerCAmelCase					)
										model.to(_lowerCAmelCase					)
										model.eval()
										__SCREAMING_SNAKE_CASE:				int           =   model(_lowerCAmelCase			, attention_mask=_lowerCAmelCase			, labels=_lowerCAmelCase					)
										self.parent.assertEqual(result.logits.shape			, (self.batch_size, self.seq_length, self.num_labels)					)






				def 			snake_case_    (  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				str           =   self.prepare_config_and_inputs()
										(
										    (
										    __SCREAMING_SNAKE_CASE
										)       ,(
										    __SCREAMING_SNAKE_CASE
										)       ,(
										    __SCREAMING_SNAKE_CASE
										)       ,(
										    __SCREAMING_SNAKE_CASE
										)       ,(
										    __SCREAMING_SNAKE_CASE
										)       ,(
										    __SCREAMING_SNAKE_CASE
										)       ,
										):				int           =   config_and_inputs
										__SCREAMING_SNAKE_CASE:				Tuple           =   {'''input_ids''': input_ids, '''attention_mask''': input_mask}
										return config, inputs_dict





@require_torch
class 				a			(      __lowercase   ,__lowercase   ,unittest.TestCase			):
				SCREAMING_SNAKE_CASE__     :   Tuple		      =     False

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






				def 			snake_case_    (  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				List[str]           =   EsmModelTester(self					)
										__SCREAMING_SNAKE_CASE:				List[str]           =   ConfigTester(self			, config_class=_lowerCAmelCase			, hidden_size=37					)






				def 			snake_case_    (  self					):

										"""simple docstring"""




										self.config_tester.run_common_tests()






				def 			snake_case_    (  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				Dict           =   self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*_lowerCAmelCase					)






				def 			snake_case_    (  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				Optional[int]           =   self.model_tester.prepare_config_and_inputs()
										for type in ["absolute", "relative_key", "relative_key_query"]:
																__SCREAMING_SNAKE_CASE:				Any           =   type
																self.model_tester.create_and_check_model(*_lowerCAmelCase					)






				def 			snake_case_    (  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				Optional[int]           =   self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_masked_lm(*_lowerCAmelCase					)






				def 			snake_case_    (  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				Optional[int]           =   self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_token_classification(*_lowerCAmelCase					)






				@slow
				def 			snake_case_    (  self					):

										"""simple docstring"""




										for model_name in ESM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																__SCREAMING_SNAKE_CASE:				Optional[int]           =   EsmModel.from_pretrained(_lowerCAmelCase					)
																self.assertIsNotNone(_lowerCAmelCase					)






				def 			snake_case_    (  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				Union[str, Any]           =   self.model_tester.prepare_config_and_inputs()[0]
										__SCREAMING_SNAKE_CASE:				int           =   EsmEmbeddings(config=_lowerCAmelCase					)

										__SCREAMING_SNAKE_CASE:				Union[str, Any]           =   torch.as_tensor([[12, 31, 13, model.padding_idx]]					)
										__SCREAMING_SNAKE_CASE:				Optional[Any]           =   torch.as_tensor(
										    [
										        [
										            0 + model.padding_idx + 1,
										            1 + model.padding_idx + 1,
										            2 + model.padding_idx + 1,
										            model.padding_idx,
										        ]
										    ]					)
										__SCREAMING_SNAKE_CASE:				Any           =   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 			snake_case_    (  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				Tuple           =   self.model_tester.prepare_config_and_inputs()[0]
										__SCREAMING_SNAKE_CASE:				Dict           =   EsmEmbeddings(config=_lowerCAmelCase					)

										__SCREAMING_SNAKE_CASE:				Optional[int]           =   torch.empty(2			, 4			, 30					)
										__SCREAMING_SNAKE_CASE:				List[str]           =   [
										    0 + embeddings.padding_idx + 1,
										    1 + embeddings.padding_idx + 1,
										    2 + embeddings.padding_idx + 1,
										    3 + embeddings.padding_idx + 1,
										]
										__SCREAMING_SNAKE_CASE:				str           =   torch.as_tensor([expected_single_positions, expected_single_positions]					)
										__SCREAMING_SNAKE_CASE:				Optional[Any]           =   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 			snake_case_    (  self					):

										"""simple docstring"""




										pass






				@unittest.skip('''Esm does not support embedding resizing'''					)
				def 			snake_case_    (  self					):

										"""simple docstring"""




										pass






				@unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.'''					)
				def 			snake_case_    (  self					):

										"""simple docstring"""




										pass





@require_torch
class 				a			(      __lowercase			):






				@slow
				def 			snake_case_    (  self					):

										"""simple docstring"""




										with torch.no_grad():
																__SCREAMING_SNAKE_CASE:				Optional[int]           =   EsmForMaskedLM.from_pretrained('''facebook/esm2_t6_8M_UR50D'''					)
																model.eval()
																__SCREAMING_SNAKE_CASE:				Tuple           =   torch.tensor([[0, 1, 2, 3, 4, 5]]					)
																__SCREAMING_SNAKE_CASE:				str           =   model(_lowerCAmelCase					)[0]

																__SCREAMING_SNAKE_CASE:				Union[str, Any]           =   33

																__SCREAMING_SNAKE_CASE:				Dict           =   torch.Size((1, 6, vocab_size)					)
																self.assertEqual(output.shape			, _lowerCAmelCase					)

																__SCREAMING_SNAKE_CASE:				Optional[int]           =   torch.tensor(
																    [[[8.9215, -10.5898, -6.4671], [-6.3967, -13.9114, -1.1212], [-7.7812, -13.9516, -3.7406]]]					)
																self.assertTrue(torch.allclose(output[:, :3, :3]			, _lowerCAmelCase			, atol=1e-4					)					)






				@slow
				def 			snake_case_    (  self					):

										"""simple docstring"""




										with torch.no_grad():
																__SCREAMING_SNAKE_CASE:				Dict           =   EsmModel.from_pretrained('''facebook/esm2_t6_8M_UR50D'''					)
																model.eval()

																__SCREAMING_SNAKE_CASE:				Dict           =   torch.tensor([[0, 6, 4, 13, 5, 4, 16, 12, 11, 7, 2]]					)
																__SCREAMING_SNAKE_CASE:				Union[str, Any]           =   model(_lowerCAmelCase					)[0]
																# compare the actual values for a slice.
																__SCREAMING_SNAKE_CASE:				Optional[int]           =   torch.tensor(
																    [[[0.1444, 0.5413, 0.3248], [0.3034, 0.0053, 0.3108], [0.3228, -0.2499, 0.3415]]]					)
																self.assertTrue(torch.allclose(output[:, :3, :3]			, _lowerCAmelCase			, atol=1e-4					)					)



