




"""simple docstring"""






import collections
import gzip
import os
import urllib

import numpy
from tensorflow.python.framework import dtypes, random_seed
from tensorflow.python.platform import gfile
from tensorflow.python.util.deprecation import deprecated

lowercase__  		=		collections.namedtuple("""_Datasets""", ["""train""", """validation""", """test"""])

# CVDF mirror of http://yann.lecun.com/exdb/mnist/
lowercase__  		=		"""https://storage.googleapis.com/cvdf-datasets/mnist/"""







def   _snake_case    (       lowercase__	):
				_lowerCamelCase  :    int					=					numpy.dtype(numpy.uintaa	).newbyteorder('>'	)
				return numpy.frombuffer(bytestream.read(4	)							,					dtype=lowercase__	)[0]







@deprecated(lowercase__							,					'Please use tf.data to implement this functionality.'	)
def   _snake_case    (       lowercase__	):
				print('Extracting'							,					f.name	)
				with gzip.GzipFile(fileobj=lowercase__	) as bytestream:
								_lowerCamelCase  :    Union[str, Any]					=					_readaa(lowercase__	)
								if magic != 2051:
												raise ValueError(
												    'Invalid magic number %d in MNIST image file: %s' % (magic, f.name)	)
								_lowerCamelCase  :    List[str]					=					_readaa(lowercase__	)
								_lowerCamelCase  :    Optional[Any]					=					_readaa(lowercase__	)
								_lowerCamelCase  :    Optional[Any]					=					_readaa(lowercase__	)
								_lowerCamelCase  :    Tuple					=					bytestream.read(rows * cols * num_images	)
								_lowerCamelCase  :    Optional[int]					=					numpy.frombuffer(lowercase__							,					dtype=numpy.uinta	)
								_lowerCamelCase  :    List[Any]					=					data.reshape(lowercase__							,					lowercase__							,					lowercase__							,					1	)
								return data







@deprecated(lowercase__							,					'Please use tf.one_hot on tensors.'	)
def   _snake_case    (       lowercase__							,					lowercase__	):
				_lowerCamelCase  :    str					=					labels_dense.shape[0]
				_lowerCamelCase  :    List[str]					=					numpy.arange(lowercase__	) * num_classes
				_lowerCamelCase  :    List[str]					=					numpy.zeros((num_labels, num_classes)	)
				_lowerCamelCase  :    List[Any]					=					1
				return labels_one_hot







@deprecated(lowercase__							,					'Please use tf.data to implement this functionality.'	)
def   _snake_case    (       lowercase__							,					lowercase__=False							,					lowercase__=10	):
				print('Extracting'							,					f.name	)
				with gzip.GzipFile(fileobj=lowercase__	) as bytestream:
								_lowerCamelCase  :    Dict					=					_readaa(lowercase__	)
								if magic != 2049:
												raise ValueError(
												    'Invalid magic number %d in MNIST label file: %s' % (magic, f.name)	)
								_lowerCamelCase  :    List[str]					=					_readaa(lowercase__	)
								_lowerCamelCase  :    List[Any]					=					bytestream.read(lowercase__	)
								_lowerCamelCase  :    Dict					=					numpy.frombuffer(lowercase__							,					dtype=numpy.uinta	)
								if one_hot:
												return _dense_to_one_hot(lowercase__							,					lowercase__	)
								return labels






class lowerCAmelCase__			:






			'''simple docstring'''



			@deprecated(
			    lowercase						,   'Please use alternatives such as official/mnist/_DataSet.py'
			    ' from tensorflow/models.'						,   )
			def __init__(						self						,   lowercase						,   lowercase						,   lowercase=False						,   lowercase=False						,   lowercase=dtypes.floataa						,   lowercase=True						,   lowercase=None						,   ):
							_lowerCamelCase,				_lowerCamelCase  :    Tuple					=					random_seed.get_seed(lowercase						)
							# If op level seed is not set, use whatever graph level seed is returned
							numpy.random.seed(seeda if seed is None else seeda						)
							_lowerCamelCase  :    Tuple					=					dtypes.as_dtype(lowercase						).base_dtype
							if dtype not in (dtypes.uinta, dtypes.floataa):
											raise TypeError('Invalid image dtype %r, expected uint8 or float32' % dtype						)
							if fake_data:
											_lowerCamelCase  :    str					=					10000
											_lowerCamelCase  :    List[str]					=					one_hot
							else:
											assert (
											    images.shape[0] == labels.shape[0]
											), F'''images.shape: {images.shape} labels.shape: {labels.shape}'''
											_lowerCamelCase  :    Optional[Any]					=					images.shape[0]

											# Convert shape from [num examples, rows, columns, depth]
											# to [num examples, rows*columns] (assuming depth == 1)
											if reshape:
															assert images.shape[3] == 1
															_lowerCamelCase  :    str					=					images.reshape(
															    images.shape[0]						,   images.shape[1] * images.shape[2]						)
											if dtype == dtypes.floataa:
															# Convert from [0, 255] -> [0.0, 1.0].
															_lowerCamelCase  :    List[Any]					=					images.astype(numpy.floataa						)
															_lowerCamelCase  :    Any					=					numpy.multiply(lowercase						,   1.0 / 2_55.0						)
							_lowerCamelCase  :    Optional[Any]					=					images
							_lowerCamelCase  :    Tuple					=					labels
							_lowerCamelCase  :    Optional[Any]					=					0
							_lowerCamelCase  :    str					=					0
			@property
			def       A_					(						self						):
							return self._images
			@property
			def       A_					(						self						):
							return self._labels
			@property
			def       A_					(						self						):
							return self._num_examples
			@property
			def       A_					(						self						):
							return self._epochs_completed






			def       A_					(						self						,   lowercase						,   lowercase=False						,   lowercase=True						):
							if fake_data:
											_lowerCamelCase  :    Dict					=					[1] * 784
											_lowerCamelCase  :    Tuple					=					[1] + [0] * 9 if self.one_hot else 0
											return (
											    [fake_image for _ in range(lowercase						)],
											    [fake_label for _ in range(lowercase						)],
											)
							_lowerCamelCase  :    Optional[int]					=					self._index_in_epoch
							# Shuffle for the first epoch
							if self._epochs_completed == 0 and start == 0 and shuffle:
											_lowerCamelCase  :    Dict					=					numpy.arange(self._num_examples						)
											numpy.random.shuffle(lowercase						)
											_lowerCamelCase  :    int					=					self.images[perma]
											_lowerCamelCase  :    Any					=					self.labels[perma]
							# Go to the next epoch
							if start + batch_size > self._num_examples:
											# Finished epoch
											self._epochs_completed += 1
											# Get the rest examples in this epoch
											_lowerCamelCase  :    List[Any]					=					self._num_examples - start
											_lowerCamelCase  :    Optional[int]					=					self._images[start : self._num_examples]
											_lowerCamelCase  :    Dict					=					self._labels[start : self._num_examples]
											# Shuffle the data
											if shuffle:
															_lowerCamelCase  :    str					=					numpy.arange(self._num_examples						)
															numpy.random.shuffle(lowercase						)
															_lowerCamelCase  :    List[Any]					=					self.images[perm]
															_lowerCamelCase  :    Any					=					self.labels[perm]
											# Start next epoch
											_lowerCamelCase  :    List[str]					=					0
											_lowerCamelCase  :    Tuple					=					batch_size - rest_num_examples
											_lowerCamelCase  :    Union[str, Any]					=					self._index_in_epoch
											_lowerCamelCase  :    Dict					=					self._images[start:end]
											_lowerCamelCase  :    Optional[Any]					=					self._labels[start:end]
											return (
											    numpy.concatenate((images_rest_part, images_new_part)						,   axis=0						),
											    numpy.concatenate((labels_rest_part, labels_new_part)						,   axis=0						),
											)
							else:
											self._index_in_epoch += batch_size
											_lowerCamelCase  :    Optional[Any]					=					self._index_in_epoch
											return self._images[start:end], self._labels[start:end]







@deprecated(lowercase__							,					'Please write your own downloading logic.'	)
def   _snake_case    (       lowercase__							,					lowercase__							,					lowercase__	):
				if not gfile.Exists(lowercase__	):
								gfile.MakeDirs(lowercase__	)
				_lowerCamelCase  :    Tuple					=					os.path.join(lowercase__							,					lowercase__	)
				if not gfile.Exists(lowercase__	):
								urllib.request.urlretrieve(lowercase__							,					lowercase__	)  # noqa: S310
								with gfile.GFile(lowercase__	) as f:
												_lowerCamelCase  :    Optional[int]					=					f.size()
								print('Successfully downloaded'							,					lowercase__							,					lowercase__							,					'bytes.'	)
				return filepath







@deprecated(
    lowercase__							,					'Please use alternatives such as:' ' tensorflow_datasets.load(\'mnist\')'	)
def   _snake_case    (       lowercase__							,					lowercase__=False							,					lowercase__=False							,					lowercase__=dtypes.floataa							,					lowercase__=True							,					lowercase__=5000							,					lowercase__=None							,					lowercase__=DEFAULT_SOURCE_URL							,					):
				if fake_data:

								def fake():
												return _DataSet(
												    []							,					[]							,					fake_data=lowercase__							,					one_hot=lowercase__							,					dtype=lowercase__							,					seed=lowercase__	)

								_lowerCamelCase  :    str					=					fake()
								_lowerCamelCase  :    str					=					fake()
								_lowerCamelCase  :    List[str]					=					fake()
								return _Datasets(train=lowercase__							,					validation=lowercase__							,					test=lowercase__	)

				if not source_url:  # empty string check
								_lowerCamelCase  :    Union[str, Any]					=					DEFAULT_SOURCE_URL

				_lowerCamelCase  :    Tuple					=					'train-images-idx3-ubyte.gz'
				_lowerCamelCase  :    List[Any]					=					'train-labels-idx1-ubyte.gz'
				_lowerCamelCase  :    Dict					=					't10k-images-idx3-ubyte.gz'
				_lowerCamelCase  :    Dict					=					't10k-labels-idx1-ubyte.gz'

				_lowerCamelCase  :    Optional[Any]					=					_maybe_download(
				    lowercase__							,					lowercase__							,					source_url + train_images_file	)
				with gfile.Open(lowercase__							,					'rb'	) as f:
								_lowerCamelCase  :    Optional[Any]					=					_extract_images(lowercase__	)

				_lowerCamelCase  :    List[str]					=					_maybe_download(
				    lowercase__							,					lowercase__							,					source_url + train_labels_file	)
				with gfile.Open(lowercase__							,					'rb'	) as f:
								_lowerCamelCase  :    Optional[Any]					=					_extract_labels(lowercase__							,					one_hot=lowercase__	)

				_lowerCamelCase  :    List[Any]					=					_maybe_download(
				    lowercase__							,					lowercase__							,					source_url + test_images_file	)
				with gfile.Open(lowercase__							,					'rb'	) as f:
								_lowerCamelCase  :    str					=					_extract_images(lowercase__	)

				_lowerCamelCase  :    int					=					_maybe_download(
				    lowercase__							,					lowercase__							,					source_url + test_labels_file	)
				with gfile.Open(lowercase__							,					'rb'	) as f:
								_lowerCamelCase  :    List[str]					=					_extract_labels(lowercase__							,					one_hot=lowercase__	)

				if not 0 <= validation_size <= len(lowercase__	):
								_lowerCamelCase  :    List[Any]					=					(
								    'Validation size should be between 0 and '
								    f'''{len(lowercase__	)}. Received: {validation_size}.'''
								)
								raise ValueError(lowercase__	)

				_lowerCamelCase  :    Optional[int]					=					train_images[:validation_size]
				_lowerCamelCase  :    Optional[Any]					=					train_labels[:validation_size]
				_lowerCamelCase  :    int					=					train_images[validation_size:]
				_lowerCamelCase  :    Optional[Any]					=					train_labels[validation_size:]

				_lowerCamelCase  :    int					=					{'dtype': dtype, 'reshape': reshape, 'seed': seed}

				_lowerCamelCase  :    List[Any]					=					_DataSet(lowercase__							,					lowercase__							,					**lowercase__	)
				_lowerCamelCase  :    Optional[Any]					=					_DataSet(lowercase__							,					lowercase__							,					**lowercase__	)
				_lowerCamelCase  :    Optional[int]					=					_DataSet(lowercase__							,					lowercase__							,					**lowercase__	)

				return _Datasets(train=lowercase__							,					validation=lowercase__							,					test=lowercase__	)





"""simple docstring"""






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 lowerCAmelCase__			(  lowercase		):






			'''simple docstring'''



			lowerCamelCase__   =    """realm"""
			def __init__(						self						,   lowercase=30522						,   lowercase=768						,   lowercase=128						,   lowercase=12						,   lowercase=12						,   lowercase=8						,   lowercase=3072						,   lowercase="gelu_new"						,   lowercase=0.1						,   lowercase=0.1						,   lowercase=512						,   lowercase=2						,   lowercase=0.02						,   lowercase=1E-12						,   lowercase=256						,   lowercase=10						,   lowercase=1E-3						,   lowercase=5						,   lowercase=320						,   lowercase=13353718						,   lowercase=5000						,   lowercase=1						,   lowercase=0						,   lowercase=2						,   **lowercase						,   ):
							super().__init__(pad_token_id=lowercase						,   bos_token_id=lowercase						,   eos_token_id=lowercase						,   **lowercase						)

							# Common config
							_lowerCamelCase  :    str					=					vocab_size
							_lowerCamelCase  :    Dict					=					max_position_embeddings
							_lowerCamelCase  :    int					=					hidden_size
							_lowerCamelCase  :    Optional[Any]					=					retriever_proj_size
							_lowerCamelCase  :    Dict					=					num_hidden_layers
							_lowerCamelCase  :    Any					=					num_attention_heads
							_lowerCamelCase  :    int					=					num_candidates
							_lowerCamelCase  :    List[Any]					=					intermediate_size
							_lowerCamelCase  :    int					=					hidden_act
							_lowerCamelCase  :    Union[str, Any]					=					hidden_dropout_prob
							_lowerCamelCase  :    Dict					=					attention_probs_dropout_prob
							_lowerCamelCase  :    Union[str, Any]					=					initializer_range
							_lowerCamelCase  :    List[Any]					=					type_vocab_size
							_lowerCamelCase  :    int					=					layer_norm_eps

							# Reader config
							_lowerCamelCase  :    Tuple					=					span_hidden_size
							_lowerCamelCase  :    int					=					max_span_width
							_lowerCamelCase  :    Tuple					=					reader_layer_norm_eps
							_lowerCamelCase  :    Union[str, Any]					=					reader_beam_size
							_lowerCamelCase  :    Union[str, Any]					=					reader_seq_len

							# Retrieval config
							_lowerCamelCase  :    Optional[Any]					=					num_block_records
							_lowerCamelCase  :    str					=					searcher_beam_size