



"""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__		:    List[str]				   =     collections.namedtuple("""_Datasets""", ["""train""", """validation""", """test"""])

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

def     UpperCamelCase_   (   lowerCAmelCase__				:    Any						)						->       str:

  """simple docstring"""




  lowerCAmelCase_       :    int								=			numpy.dtype(numpy.uintaa						).newbyteorder('>'						)
  return numpy.frombuffer(bytestream.read(4						)   ,       dtype=lowerCAmelCase__						)[0]

@deprecated(lowerCAmelCase__   ,       'Please use tf.data to implement this functionality.'						)
def     UpperCamelCase_   (   lowerCAmelCase__				:    str						)						->       List[str]:

  """simple docstring"""




  print('Extracting'   ,       f.name						)
  with gzip.GzipFile(fileobj=lowerCAmelCase__						) as bytestream:
    lowerCAmelCase_       :    Any								=			_readaa(lowerCAmelCase__						)
    if magic != 2051:
      raise ValueError(
          'Invalid magic number %d in MNIST image file: %s' % (magic, f.name)						)
    lowerCAmelCase_       :    str								=			_readaa(lowerCAmelCase__						)
    lowerCAmelCase_       :    Union[str, Any]								=			_readaa(lowerCAmelCase__						)
    lowerCAmelCase_       :    str								=			_readaa(lowerCAmelCase__						)
    lowerCAmelCase_       :    List[str]								=			bytestream.read(rows * cols * num_images						)
    lowerCAmelCase_       :    Tuple								=			numpy.frombuffer(lowerCAmelCase__   ,       dtype=numpy.uinta						)
    lowerCAmelCase_       :    List[str]								=			data.reshape(lowerCAmelCase__   ,       lowerCAmelCase__   ,       lowerCAmelCase__   ,       1						)
    return data

@deprecated(lowerCAmelCase__   ,       'Please use tf.one_hot on tensors.'						)
def     UpperCamelCase_   (   lowerCAmelCase__				:    int   ,       lowerCAmelCase__				:    Tuple						)						->       Optional[Any]:

  """simple docstring"""




  lowerCAmelCase_       :    List[Any]								=			labels_dense.shape[0]
  lowerCAmelCase_       :    Optional[int]								=			numpy.arange(lowerCAmelCase__						) * num_classes
  lowerCAmelCase_       :    Tuple								=			numpy.zeros((num_labels, num_classes)						)
  lowerCAmelCase_       :    List[Any]								=			1
  return labels_one_hot

@deprecated(lowerCAmelCase__   ,       'Please use tf.data to implement this functionality.'						)
def     UpperCamelCase_   (   lowerCAmelCase__				:    Optional[Any]   ,       lowerCAmelCase__				:    Optional[int]=False   ,       lowerCAmelCase__				:    Tuple=10						)						->       int:

  """simple docstring"""




  print('Extracting'   ,       f.name						)
  with gzip.GzipFile(fileobj=lowerCAmelCase__						) as bytestream:
    lowerCAmelCase_       :    Optional[Any]								=			_readaa(lowerCAmelCase__						)
    if magic != 2049:
      raise ValueError(
          'Invalid magic number %d in MNIST label file: %s' % (magic, f.name)						)
    lowerCAmelCase_       :    int								=			_readaa(lowerCAmelCase__						)
    lowerCAmelCase_       :    Optional[int]								=			bytestream.read(lowerCAmelCase__						)
    lowerCAmelCase_       :    Optional[Any]								=			numpy.frombuffer(lowerCAmelCase__   ,       dtype=numpy.uinta						)
    if one_hot:
      return _dense_to_one_hot(lowerCAmelCase__   ,       lowerCAmelCase__						)
    return labels







class 				UpperCamelCase__       :




      """simple docstring"""




      @deprecated(
          _a   ,      'Please use alternatives such as official/mnist/_DataSet.py'
          ' from tensorflow/models.'   ,      )
      def __init__(				self    :				Optional[int]   ,      SCREAMING_SNAKE_CASE_    :				List[Any]   ,      SCREAMING_SNAKE_CASE_    :				Tuple   ,      SCREAMING_SNAKE_CASE_    :				str=False   ,      SCREAMING_SNAKE_CASE_    :				Union[str, Any]=False   ,      SCREAMING_SNAKE_CASE_    :				Optional[int]=dtypes.floataa   ,      SCREAMING_SNAKE_CASE_    :				Optional[Any]=True   ,      SCREAMING_SNAKE_CASE_    :				str=None   ,      ):
        lowerCAmelCase_       :    int								=			random_seed.get_seed(_a	)
        # 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_       :    Dict								=			dtypes.as_dtype(_a	).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_       :    List[Any]								=			1_0_0_0_0
          lowerCAmelCase_       :    Any								=			one_hot
        else:
          assert (
              images.shape[0] == labels.shape[0]
          ), F"images.shape: {images.shape} labels.shape: {labels.shape}"
          lowerCAmelCase_       :    Dict								=			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_       :    List[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_       :    Tuple								=			images.astype(numpy.floataa	)
            lowerCAmelCase_       :    List[Any]								=			numpy.multiply(_a   ,      1.0 / 2_5_5.0	)
        lowerCAmelCase_       :    str								=			images
        lowerCAmelCase_       :    Dict								=			labels
        lowerCAmelCase_       :    List[str]								=			0
        lowerCAmelCase_       :    Optional[Any]								=			0



      @property
      def 					SCREAMING_SNAKE_CASE__			(				self    :				List[str]	):
        return self._images



      @property
      def 					SCREAMING_SNAKE_CASE__			(				self    :				List[Any]	):
        return self._labels



      @property
      def 					SCREAMING_SNAKE_CASE__			(				self    :				str	):
        return self._num_examples



      @property
      def 					SCREAMING_SNAKE_CASE__			(				self    :				Dict	):
        return self._epochs_completed





      def 					SCREAMING_SNAKE_CASE__			(				self    :				Tuple   ,      SCREAMING_SNAKE_CASE_    :				List[str]   ,      SCREAMING_SNAKE_CASE_    :				List[str]=False   ,      SCREAMING_SNAKE_CASE_    :				Optional[int]=True	):
        if fake_data:
          lowerCAmelCase_       :    str								=			[1] * 7_8_4
          lowerCAmelCase_       :    Optional[int]								=			[1] + [0] * 9 if self.one_hot else 0
          return (
              [fake_image for _ in range(_a	)],
              [fake_label for _ in range(_a	)],
          )
        lowerCAmelCase_       :    List[Any]								=			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(_a	)
          lowerCAmelCase_       :    int								=			self.images[perma]
          lowerCAmelCase_       :    Dict								=			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_       :    Tuple								=			self._num_examples - start
          lowerCAmelCase_       :    str								=			self._images[start : self._num_examples]
          lowerCAmelCase_       :    Optional[Any]								=			self._labels[start : self._num_examples]
          # Shuffle the data
          if shuffle:
            lowerCAmelCase_       :    List[Any]								=			numpy.arange(self._num_examples	)
            numpy.random.shuffle(_a	)
            lowerCAmelCase_       :    Any								=			self.images[perm]
            lowerCAmelCase_       :    Optional[int]								=			self.labels[perm]
          # Start next epoch
          lowerCAmelCase_       :    Tuple								=			0
          lowerCAmelCase_       :    List[Any]								=			batch_size - rest_num_examples
          lowerCAmelCase_       :    Any								=			self._index_in_epoch
          lowerCAmelCase_       :    List[str]								=			self._images[start:end]
          lowerCAmelCase_       :    Tuple								=			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_       :    Dict								=			self._index_in_epoch
          return self._images[start:end], self._labels[start:end]




@deprecated(lowerCAmelCase__   ,       'Please write your own downloading logic.'						)
def     UpperCamelCase_   (   lowerCAmelCase__				:    Tuple   ,       lowerCAmelCase__				:    Union[str, Any]   ,       lowerCAmelCase__				:    Optional[Any]						)						->       Tuple:

  """simple docstring"""




  if not gfile.Exists(lowerCAmelCase__						):
    gfile.MakeDirs(lowerCAmelCase__						)
  lowerCAmelCase_       :    int								=			os.path.join(lowerCAmelCase__   ,       lowerCAmelCase__						)
  if not gfile.Exists(lowerCAmelCase__						):
    urllib.request.urlretrieve(lowerCAmelCase__   ,       lowerCAmelCase__						)  # noqa: S310
    with gfile.GFile(lowerCAmelCase__						) as f:
      lowerCAmelCase_       :    Optional[int]								=			f.size()
    print('Successfully downloaded'   ,       lowerCAmelCase__   ,       lowerCAmelCase__   ,       'bytes.'						)
  return filepath

@deprecated(
    lowerCAmelCase__   ,       'Please use alternatives such as:' ' tensorflow_datasets.load(\'mnist\')'						)
def     UpperCamelCase_   (   lowerCAmelCase__				:    Dict   ,       lowerCAmelCase__				:    Union[str, Any]=False   ,       lowerCAmelCase__				:    Union[str, Any]=False   ,       lowerCAmelCase__				:    Optional[int]=dtypes.floataa   ,       lowerCAmelCase__				:    Union[str, Any]=True   ,       lowerCAmelCase__				:    Optional[int]=5000   ,       lowerCAmelCase__				:    List[Any]=None   ,       lowerCAmelCase__				:    Union[str, Any]=DEFAULT_SOURCE_URL   ,       )						->       Any:

  """simple docstring"""




  if fake_data:

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

    lowerCAmelCase_       :    Any								=			fake()
    lowerCAmelCase_       :    Optional[Any]								=			fake()
    lowerCAmelCase_       :    str								=			fake()
    return _Datasets(train=lowerCAmelCase__   ,       validation=lowerCAmelCase__   ,       test=lowerCAmelCase__						)

  if not source_url:  # empty string check
    lowerCAmelCase_       :    int								=			DEFAULT_SOURCE_URL

  lowerCAmelCase_       :    str								=			"train-images-idx3-ubyte.gz"
  lowerCAmelCase_       :    int								=			"train-labels-idx1-ubyte.gz"
  lowerCAmelCase_       :    List[str]								=			"t10k-images-idx3-ubyte.gz"
  lowerCAmelCase_       :    Tuple								=			"t10k-labels-idx1-ubyte.gz"

  lowerCAmelCase_       :    Union[str, Any]								=			_maybe_download(
      lowerCAmelCase__   ,       lowerCAmelCase__   ,       source_url + train_images_file						)
  with gfile.Open(lowerCAmelCase__   ,       'rb'						) as f:
    lowerCAmelCase_       :    int								=			_extract_images(lowerCAmelCase__						)

  lowerCAmelCase_       :    str								=			_maybe_download(
      lowerCAmelCase__   ,       lowerCAmelCase__   ,       source_url + train_labels_file						)
  with gfile.Open(lowerCAmelCase__   ,       'rb'						) as f:
    lowerCAmelCase_       :    int								=			_extract_labels(lowerCAmelCase__   ,       one_hot=lowerCAmelCase__						)

  lowerCAmelCase_       :    Any								=			_maybe_download(
      lowerCAmelCase__   ,       lowerCAmelCase__   ,       source_url + test_images_file						)
  with gfile.Open(lowerCAmelCase__   ,       'rb'						) as f:
    lowerCAmelCase_       :    int								=			_extract_images(lowerCAmelCase__						)

  lowerCAmelCase_       :    Optional[int]								=			_maybe_download(
      lowerCAmelCase__   ,       lowerCAmelCase__   ,       source_url + test_labels_file						)
  with gfile.Open(lowerCAmelCase__   ,       'rb'						) as f:
    lowerCAmelCase_       :    Optional[Any]								=			_extract_labels(lowerCAmelCase__   ,       one_hot=lowerCAmelCase__						)

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

  lowerCAmelCase_       :    Any								=			train_images[:validation_size]
  lowerCAmelCase_       :    Optional[int]								=			train_labels[:validation_size]
  lowerCAmelCase_       :    Dict								=			train_images[validation_size:]
  lowerCAmelCase_       :    Optional[int]								=			train_labels[validation_size:]

  lowerCAmelCase_       :    List[str]								=			{"dtype": dtype, "reshape": reshape, "seed": seed}

  lowerCAmelCase_       :    Tuple								=			_DataSet(lowerCAmelCase__   ,       lowerCAmelCase__   ,       **lowerCAmelCase__						)
  lowerCAmelCase_       :    Any								=			_DataSet(lowerCAmelCase__   ,       lowerCAmelCase__   ,       **lowerCAmelCase__						)
  lowerCAmelCase_       :    Any								=			_DataSet(lowerCAmelCase__   ,       lowerCAmelCase__   ,       **lowerCAmelCase__						)

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










"""simple docstring"""







from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_torch_available,
)


lowercase__		:    Dict				   =     {
    """configuration_falcon""": ["""FALCON_PRETRAINED_CONFIG_ARCHIVE_MAP""", """FalconConfig"""],
}

try:
     if not is_torch_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     lowercase__		:    str				   =     [
         """FALCON_PRETRAINED_MODEL_ARCHIVE_LIST""",
         """FalconForCausalLM""",
         """FalconModel""",
         """FalconPreTrainedModel""",
         """FalconForSequenceClassification""",
         """FalconForTokenClassification""",
         """FalconForQuestionAnswering""",
     ]


if TYPE_CHECKING:
     from .configuration_falcon import FALCON_PRETRAINED_CONFIG_ARCHIVE_MAP, FalconConfig

     try:
          if not is_torch_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_falcon import (
              FALCON_PRETRAINED_MODEL_ARCHIVE_LIST,
              FalconForCausalLM,
              FalconForQuestionAnswering,
              FalconForSequenceClassification,
              FalconForTokenClassification,
              FalconModel,
              FalconPreTrainedModel,
          )


else:
     import sys

     lowercase__		:    Dict				   =     _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)





