



import PIL.Image
import PIL.ImageOps
from packaging import version
from PIL import Image


if version.parse(version.parse(PIL.__version__).base_version) >= version.parse('''9.1.0'''):
				lowercase_	       =			{
				    '''linear''': PIL.Image.Resampling.BILINEAR,
				    '''bilinear''': PIL.Image.Resampling.BILINEAR,
				    '''bicubic''': PIL.Image.Resampling.BICUBIC,
				    '''lanczos''': PIL.Image.Resampling.LANCZOS,
				    '''nearest''': PIL.Image.Resampling.NEAREST,
				}
else:
				lowercase_	       =			{
				    '''linear''': PIL.Image.LINEAR,
				    '''bilinear''': PIL.Image.BILINEAR,
				    '''bicubic''': PIL.Image.BICUBIC,
				    '''lanczos''': PIL.Image.LANCZOS,
				    '''nearest''': PIL.Image.NEAREST,
				}






def 					__lowerCAmelCase		(  __lowerCamelCase	:    List[str]	)  ->						int:
					__lowerCAmelCase	   =(images / 2 + 0.5).clamp(0				,   1	)
					__lowerCAmelCase	   =images.cpu().permute(0				,   2				,   3				,   1	).float().numpy()
					__lowerCAmelCase	   =numpy_to_pil(__lowerCamelCase	)
					return images






def 					__lowerCAmelCase		(  __lowerCamelCase	:    str	)  ->						str:
					if images.ndim == 3:
										__lowerCAmelCase	   =images[None, ...]
					__lowerCAmelCase	   =(images * 255).round().astype("""uint8"""	)
					if images.shape[-1] == 1:
										# special case for grayscale (single channel) images
										__lowerCAmelCase	   =[Image.fromarray(image.squeeze()				,   mode="""L"""	) for image in images]
					else:
										__lowerCAmelCase	   =[Image.fromarray(__lowerCamelCase	) for image in images]

					return pil_images









from ...configuration_utils import PretrainedConfig
from ...utils import logging


lowercase_	       =			logging.get_logger(__name__)

lowercase_	       =			{
    '''google/realm-cc-news-pretrained-embedder''': (
        '''https://huggingface.co/google/realm-cc-news-pretrained-embedder/resolve/main/config.json'''
    ),
    '''google/realm-cc-news-pretrained-encoder''': (
        '''https://huggingface.co/google/realm-cc-news-pretrained-encoder/resolve/main/config.json'''
    ),
    '''google/realm-cc-news-pretrained-scorer''': (
        '''https://huggingface.co/google/realm-cc-news-pretrained-scorer/resolve/main/config.json'''
    ),
    '''google/realm-cc-news-pretrained-openqa''': (
        '''https://huggingface.co/google/realm-cc-news-pretrained-openqa/aresolve/main/config.json'''
    ),
    '''google/realm-orqa-nq-openqa''': '''https://huggingface.co/google/realm-orqa-nq-openqa/resolve/main/config.json''',
    '''google/realm-orqa-nq-reader''': '''https://huggingface.co/google/realm-orqa-nq-reader/resolve/main/config.json''',
    '''google/realm-orqa-wq-openqa''': '''https://huggingface.co/google/realm-orqa-wq-openqa/resolve/main/config.json''',
    '''google/realm-orqa-wq-reader''': '''https://huggingface.co/google/realm-orqa-wq-reader/resolve/main/config.json''',
    # See all REALM models at https://huggingface.co/models?filter=realm
}






class 						__a (     SCREAMING_SNAKE_CASE				):
	SCREAMING_SNAKE_CASE       =       "realm"



	def __init__(						self							:    Tuple				,	snake_case_							:    Tuple=3_05_22				,	snake_case_							:    List[Any]=7_68				,	snake_case_							:    Any=1_28				,	snake_case_							:    Union[str, Any]=12				,	snake_case_							:    List[str]=12				,	snake_case_							:    Any=8				,	snake_case_							:    Any=30_72				,	snake_case_							:    int="gelu_new"				,	snake_case_							:    Optional[int]=0.1				,	snake_case_							:    Tuple=0.1				,	snake_case_							:    List[Any]=5_12				,	snake_case_							:    List[Any]=2				,	snake_case_							:    Any=0.0_2				,	snake_case_							:    List[str]=1e-12				,	snake_case_							:    Optional[Any]=2_56				,	snake_case_							:    Dict=10				,	snake_case_							:    str=1e-3				,	snake_case_							:    List[str]=5				,	snake_case_							:    Optional[Any]=3_20				,	snake_case_							:    Optional[Any]=13_35_37_18				,	snake_case_							:    Tuple=50_00				,	snake_case_							:    Union[str, Any]=1				,	snake_case_							:    Union[str, Any]=0				,	snake_case_							:    List[str]=2				,	**snake_case_							:    List[str]				,	)->							str:
						super().__init__(pad_token_id=snake_case_				,	bos_token_id=snake_case_				,	eos_token_id=snake_case_				,	**snake_case_)

						# Common config
						__lowerCAmelCase	   =vocab_size
						__lowerCAmelCase	   =max_position_embeddings
						__lowerCAmelCase	   =hidden_size
						__lowerCAmelCase	   =retriever_proj_size
						__lowerCAmelCase	   =num_hidden_layers
						__lowerCAmelCase	   =num_attention_heads
						__lowerCAmelCase	   =num_candidates
						__lowerCAmelCase	   =intermediate_size
						__lowerCAmelCase	   =hidden_act
						__lowerCAmelCase	   =hidden_dropout_prob
						__lowerCAmelCase	   =attention_probs_dropout_prob
						__lowerCAmelCase	   =initializer_range
						__lowerCAmelCase	   =type_vocab_size
						__lowerCAmelCase	   =layer_norm_eps

						# Reader config
						__lowerCAmelCase	   =span_hidden_size
						__lowerCAmelCase	   =max_span_width
						__lowerCAmelCase	   =reader_layer_norm_eps
						__lowerCAmelCase	   =reader_beam_size
						__lowerCAmelCase	   =reader_seq_len

						# Retrieval config
						__lowerCAmelCase	   =num_block_records
						__lowerCAmelCase	   =searcher_beam_size




