
import numpy as np
import torch
from torch.utils.data import Dataset, IterableDataset

from ..utils.generic import ModelOutput
class  lowercase__(				UpperCAmelCase							):



	"""simple docstring"""




	def __init__(       self  :			Union[str, Any]       ,							SCREAMING_SNAKE_CASE_  :			Tuple       ,							SCREAMING_SNAKE_CASE_  :			int       ,							SCREAMING_SNAKE_CASE_  :			str       )       ->     Optional[int]:
					lowercase_       =					dataset
					lowercase_       =					process
					lowercase_       =					params
	def __len__(       self  :			Optional[Any]       )       ->     Tuple:
					return len(self.dataset       )




	def __getitem__(       self  :			str       ,							SCREAMING_SNAKE_CASE_  :			str       )       ->     Dict:
					lowercase_       =					self.dataset[i]
					lowercase_       =					self.process(SCREAMING_SNAKE_CASE_       ,							**self.params       )
					return processed

class  lowercase__(				UpperCAmelCase							):



	"""simple docstring"""




	def __init__(       self  :			List[str]       ,							SCREAMING_SNAKE_CASE_  :			Any       ,							SCREAMING_SNAKE_CASE_  :			Union[str, Any]       ,							SCREAMING_SNAKE_CASE_  :			int       ,							SCREAMING_SNAKE_CASE_  :			List[str]=None       )       ->     int:
					lowercase_       =					loader
					lowercase_       =					infer
					lowercase_       =					params
					if loader_batch_size == 1:
									# Let's spare some time by deactivating altogether
									lowercase_       =					None
					lowercase_       =					loader_batch_size

					# Internal bookkeeping
					lowercase_       =					None
					lowercase_       =					None
	def __len__(       self  :			Optional[int]       )       ->     int:
					return len(self.loader       )
	def __iter__(       self  :			List[str]       )       ->     int:
					lowercase_       =					iter(self.loader       )
					return self
	def 						_lowercase  (       self  :			Optional[Any]       )       ->     Dict:
					if isinstance(self._loader_batch_data       ,							torch.Tensor       ):
									# Batch data is simple tensor, just fetch the slice
									lowercase_       =					self._loader_batch_data[self._loader_batch_index]
					else:
									# Batch data is assumed to be BaseModelOutput (or dict)
									lowercase_       =					{}
									for k, element in self._loader_batch_data.items():
													if isinstance(SCREAMING_SNAKE_CASE_       ,							SCREAMING_SNAKE_CASE_       ):
																	# Convert ModelOutput to tuple first
																	lowercase_       =					element.to_tuple()
																	if isinstance(element[0]       ,							torch.Tensor       ):
																					lowercase_       =					tuple(el[self._loader_batch_index].unsqueeze(0       ) for el in element       )
																	elif isinstance(element[0]       ,							np.ndarray       ):
																					lowercase_       =					tuple(np.expand_dims(el[self._loader_batch_index]       ,							0       ) for el in element       )
																	continue
													if k in {"hidden_states", "past_key_values", "attentions"} and isinstance(SCREAMING_SNAKE_CASE_       ,							SCREAMING_SNAKE_CASE_       ):
																	# Those are stored as lists of tensors so need specific unbatching.
																	if isinstance(element[0]       ,							torch.Tensor       ):
																					lowercase_       =					tuple(el[self._loader_batch_index].unsqueeze(0       ) for el in element       )
																	elif isinstance(element[0]       ,							np.ndarray       ):
																					lowercase_       =					tuple(np.expand_dims(el[self._loader_batch_index]       ,							0       ) for el in element       )
																	continue
													if element is None:
																	# This can happen for optional data that get passed around
																	lowercase_       =					None
													elif isinstance(element[self._loader_batch_index]       ,							torch.Tensor       ):
																	# Take correct batch data, but make it looked like batch_size=1
																	# For compatibility with other methods within transformers

																	lowercase_       =					element[self._loader_batch_index].unsqueeze(0       )
													elif isinstance(element[self._loader_batch_index]       ,							np.ndarray       ):
																	# Take correct batch data, but make it looked like batch_size=1
																	# For compatibility with other methods within transformers
																	lowercase_       =					np.expand_dims(element[self._loader_batch_index]       ,							0       )
													else:
																	# This is typically a list, so no need to `unsqueeze`.
																	lowercase_       =					element[self._loader_batch_index]
            # Recreate the element by reusing the original class to make it look
            # batch_size=1
									lowercase_       =					self._loader_batch_data.__class__(SCREAMING_SNAKE_CASE_       )
					self._loader_batch_index += 1
					return result




	def 						_lowercase  (       self  :			int       )       ->     Dict:
					if self._loader_batch_index is not None and self._loader_batch_index < self.loader_batch_size:
									# We are currently unrolling a batch so we just need to return
									# the current item within a batch
									return self.loader_batch_item()

					# We're out of items within a batch
					lowercase_       =					next(self.iterator       )
					lowercase_       =					self.infer(SCREAMING_SNAKE_CASE_       ,							**self.params       )
					# We now have a batch of "inferred things".
					if self.loader_batch_size is not None:
									# Try to infer the size of the batch
									if isinstance(SCREAMING_SNAKE_CASE_       ,							torch.Tensor       ):
													lowercase_       =					processed
									else:
													lowercase_       =					list(processed.keys()       )[0]
													lowercase_       =					processed[key]
									if isinstance(SCREAMING_SNAKE_CASE_       ,							SCREAMING_SNAKE_CASE_       ):
													lowercase_       =					len(SCREAMING_SNAKE_CASE_       )
									else:
													lowercase_       =					first_tensor.shape[0]
									if 0 < observed_batch_size < self.loader_batch_size:
													# could be last batch so we can't unroll as many
													# elements.
													lowercase_       =					observed_batch_size
									# Setting internal index to unwrap the batch
									lowercase_       =					processed
									lowercase_       =					0
									return self.loader_batch_item()
					else:
									# We're not unrolling batches
									return processed

class  lowercase__(				UpperCAmelCase							):



	"""simple docstring"""




	def __init__(       self  :			Optional[int]       ,							SCREAMING_SNAKE_CASE_  :			List[str]       ,							SCREAMING_SNAKE_CASE_  :			Optional[int]       ,							SCREAMING_SNAKE_CASE_  :			Optional[Any]       ,							SCREAMING_SNAKE_CASE_  :			Any=None       )       ->     Union[str, Any]:
					super().__init__(SCREAMING_SNAKE_CASE_       ,							SCREAMING_SNAKE_CASE_       ,							SCREAMING_SNAKE_CASE_       )
	def __iter__(       self  :			Tuple       )       ->     Optional[Any]:
					lowercase_       =					iter(self.loader       )
					lowercase_       =					None
					return self




	def 						_lowercase  (       self  :			int       )       ->     str:
					if self.subiterator is None:
									lowercase_       =					self.infer(next(self.iterator       )       ,							**self.params       )
					try:
									# Try to return next item
									lowercase_       =					next(self.subiterator       )
					except StopIteration:
									# When a preprocess iterator ends, we can start lookig at the next item
									# ChunkIterator will keep feeding until ALL elements of iterator
									# all have created their subiterator and have been iterating against.
									#
									# Another way to look at it, is we're basically flattening lists of lists
									# into a single list, but with generators
									lowercase_       =					self.infer(next(self.iterator       )       ,							**self.params       )
									lowercase_       =					next(self.subiterator       )
					return processed

class  lowercase__(				UpperCAmelCase							):



	"""simple docstring"""




	def __iter__(       self  :			Tuple       )       ->     Any:
					lowercase_       =					iter(self.loader       )
					return self




	def 						_lowercase  (       self  :			List[Any]       )       ->     str:
					# Extremely similar to PipelineIterator in its unpacking mechanism
					# BUT, we have an extra required item which is the presence of `is_last`
					# That is because everything is flattened by `PipelineChunkIterator` we
					# need to keep track of how to regroup here in the original `process`
					# boundaries so that `process` and `postprocess` see the same data.

					# This iterator accumulates items (possibly while unbatching) until it
					# its a `is_last` and then just passes it on to the caller.
					lowercase_       =					False
					lowercase_       =					[]
					if self._loader_batch_index is not None and self._loader_batch_index < self.loader_batch_size:
									while self._loader_batch_index < self.loader_batch_size:
													lowercase_       =					self.loader_batch_item()
													lowercase_       =					item.pop('''is_last'''       )
													accumulator.append(SCREAMING_SNAKE_CASE_       )
													if is_last:
																	return accumulator

					while not is_last:
									lowercase_       =					self.infer(next(self.iterator       )       ,							**self.params       )
									if self.loader_batch_size is not None:
													if isinstance(SCREAMING_SNAKE_CASE_       ,							torch.Tensor       ):
																	lowercase_       =					processed
													else:
																	lowercase_       =					list(processed.keys()       )[0]
																	lowercase_       =					processed[key]
													if isinstance(SCREAMING_SNAKE_CASE_       ,							SCREAMING_SNAKE_CASE_       ):
																	lowercase_       =					len(SCREAMING_SNAKE_CASE_       )
													else:
																	lowercase_       =					first_tensor.shape[0]
													if 0 < observed_batch_size < self.loader_batch_size:
																	# could be last batch so we can't unroll as many
																	# elements.
																	lowercase_       =					observed_batch_size
													lowercase_       =					processed
													lowercase_       =					0
													while self._loader_batch_index < self.loader_batch_size:
																	lowercase_       =					self.loader_batch_item()
																	lowercase_       =					item.pop('''is_last'''       )
																	accumulator.append(SCREAMING_SNAKE_CASE_       )
																	if is_last:
																					return accumulator
									else:
													lowercase_       =					processed
													lowercase_       =					item.pop('''is_last'''       )
													accumulator.append(SCREAMING_SNAKE_CASE_       )
					return accumulator

class  lowercase__(				UpperCAmelCase							):



	"""simple docstring"""




	def __init__(       self  :			Optional[Any]       ,							SCREAMING_SNAKE_CASE_  :			Dataset       ,							SCREAMING_SNAKE_CASE_  :			str       )       ->     int:
					lowercase_       =					dataset
					lowercase_       =					key
	def __len__(       self  :			Optional[Any]       )       ->     Optional[int]:
					return len(self.dataset       )




	def __getitem__(       self  :			Union[str, Any]       ,							SCREAMING_SNAKE_CASE_  :			int       )       ->     Tuple:
					return self.dataset[i][self.key]

class  lowercase__(				UpperCAmelCase							):



	"""simple docstring"""




	def __init__(       self  :			Tuple       ,							SCREAMING_SNAKE_CASE_  :			Dataset       ,							SCREAMING_SNAKE_CASE_  :			str       ,							SCREAMING_SNAKE_CASE_  :			str       )       ->     Optional[int]:
					lowercase_       =					dataset
					lowercase_       =					keya
					lowercase_       =					keya
	def __len__(       self  :			Tuple       )       ->     List[str]:
					return len(self.dataset       )




	def __getitem__(       self  :			Dict       ,							SCREAMING_SNAKE_CASE_  :			Union[str, Any]       )       ->     Union[str, Any]:
					return {"text": self.dataset[i][self.keya], "text_pair": self.dataset[i][self.keya]}










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


snake_case_ : Any    		=       logging.get_logger(__name__)

snake_case_ : Optional[Any]    		=       {
    "tiiuae/falcon-40b": "https://huggingface.co/tiiuae/falcon-40b/resolve/main/config.json",
    "tiiuae/falcon-7b": "https://huggingface.co/tiiuae/falcon-7b/resolve/main/config.json",
}







class __snake_case			(			a		):
   UpperCAmelCase__  :    Optional[Any]          =  '''falcon'''
   UpperCAmelCase__  :    List[Any]          =  ['''past_key_values''']







   def __init__(      self		:						Union[str, Any]					,						_snake_case		:						List[str]=65024					,						_snake_case		:						int=4544					,						_snake_case		:						int=32					,						_snake_case		:						Any=71					,						_snake_case		:						int=1e-5					,						_snake_case		:						Dict=0.0_2					,						_snake_case		:						int=True					,						_snake_case		:						List[Any]=0.0					,						_snake_case		:						Tuple=0.0					,						_snake_case		:						int=None					,						_snake_case		:						Tuple=False					,						_snake_case		:						Any=False					,						_snake_case		:						str=True					,						_snake_case		:						Any=True					,						_snake_case		:						List[str]=False					,						_snake_case		:						Tuple=11					,						_snake_case		:						Dict=11					,						**_snake_case		:						Optional[int]					,						):

        """simple docstring"""


        UpperCAmelCase_          =							vocab_size
        # Backward compatibility with n_embed kwarg
        UpperCAmelCase_          =							kwargs.pop('''n_embed'''					,						_snake_case)
        UpperCAmelCase_          =							hidden_size if n_embed is None else n_embed
        UpperCAmelCase_          =							num_hidden_layers
        UpperCAmelCase_          =							num_attention_heads
        UpperCAmelCase_          =							layer_norm_epsilon
        UpperCAmelCase_          =							initializer_range
        UpperCAmelCase_          =							use_cache
        UpperCAmelCase_          =							hidden_dropout
        UpperCAmelCase_          =							attention_dropout

        UpperCAmelCase_          =							bos_token_id
        UpperCAmelCase_          =							eos_token_id
        UpperCAmelCase_          =							num_attention_heads if num_kv_heads is None else num_kv_heads
        UpperCAmelCase_          =							alibi
        UpperCAmelCase_          =							new_decoder_architecture
        UpperCAmelCase_          =							multi_query  # Ignored when new_decoder_architecture is True
        UpperCAmelCase_          =							parallel_attn
        UpperCAmelCase_          =							bias

        super().__init__(bos_token_id=_snake_case					,						eos_token_id=_snake_case					,						**_snake_case)







   @property
   def lowerCamelCase						(      self		:						List[Any]):

        """simple docstring"""


        return self.hidden_size // self.num_attention_heads







   @property
   def lowerCamelCase						(      self		:						Union[str, Any]):

        """simple docstring"""


        return not self.alibi






