
'''simple docstring'''







import os
import tempfile
from functools import partial
from unittest import TestCase
from unittest.mock import patch

import numpy as np
import pytest

from datasets.arrow_dataset import Dataset
from datasets.search import ElasticSearchIndex, FaissIndex, MissingIndex

from .utils import require_elasticsearch, require_faiss


_lowercase			:  Optional[Any]   							=	pytest.mark.integration






@require_faiss
class 			lowerCAmelCase__       (  lowerCamelCase_					):






					def      _snake_case   (       self		):


											"""simple docstring"""





											lowercase_     :					Any													=	Dataset.from_dict({'''filename''': ['''my_name-train''' + '''_''' + str(__SCREAMING_SNAKE_CASE		) for x in np.arange(30		).tolist()]}		)
											return dset






					def      _snake_case   (       self		):


											"""simple docstring"""





											import faiss

											lowercase_     :					Dataset													=	self._create_dummy_dataset()
											lowercase_     :					Any													=	dset.map(
											    lambda __SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE		: {"vecs": i * np.ones(5    ,							dtype=np.floataa		)}    ,							with_indices=__SCREAMING_SNAKE_CASE    ,							keep_in_memory=__SCREAMING_SNAKE_CASE		)
											lowercase_     :					List[Any]													=	dset.add_faiss_index('''vecs'''    ,							batch_size=1_00    ,							metric_type=faiss.METRIC_INNER_PRODUCT		)
											lowercase_						,			lowercase_     :					str													=	dset.get_nearest_examples('''vecs'''    ,							np.ones(5    ,							dtype=np.floataa		)		)
											self.assertEqual(examples['''filename'''][0]    ,							'''my_name-train_29'''		)
											dset.drop_index('''vecs'''		)






					def      _snake_case   (       self		):


											"""simple docstring"""





											import faiss

											lowercase_     :					Dataset													=	self._create_dummy_dataset()
											dset.add_faiss_index_from_external_arrays(
											    external_arrays=np.ones((30, 5)		) * np.arange(30		).reshape(-1    ,							1		)    ,							index_name='''vecs'''    ,							batch_size=1_00    ,							metric_type=faiss.METRIC_INNER_PRODUCT    ,							)
											lowercase_						,			lowercase_     :					int													=	dset.get_nearest_examples('''vecs'''    ,							np.ones(5    ,							dtype=np.floataa		)		)
											self.assertEqual(examples['''filename'''][0]    ,							'''my_name-train_29'''		)






					def      _snake_case   (       self		):


											"""simple docstring"""





											import faiss

											lowercase_     :					Dataset													=	self._create_dummy_dataset()
											dset.add_faiss_index_from_external_arrays(
											    external_arrays=np.ones((30, 5)		) * np.arange(30		).reshape(-1    ,							1		)    ,							index_name='''vecs'''    ,							metric_type=faiss.METRIC_INNER_PRODUCT    ,							)

											# Setting delete=False and unlinking manually is not pretty... but it is required on Windows to
											# ensure somewhat stable behaviour. If we don't, we get PermissionErrors. This is an age-old issue.
											# see https://bugs.python.org/issue14243 and
											# https://stackoverflow.com/questions/23212435/permission-denied-to-write-to-my-temporary-file/23212515
											with tempfile.NamedTemporaryFile(delete=__SCREAMING_SNAKE_CASE		) as tmp_file:
																	dset.save_faiss_index('''vecs'''    ,							tmp_file.name		)
																	dset.load_faiss_index('''vecs2'''    ,							tmp_file.name		)
											os.unlink(tmp_file.name		)

											lowercase_						,			lowercase_     :					Optional[int]													=	dset.get_nearest_examples('''vecs2'''    ,							np.ones(5    ,							dtype=np.floataa		)		)
											self.assertEqual(examples['''filename'''][0]    ,							'''my_name-train_29'''		)






					def      _snake_case   (       self		):


											"""simple docstring"""





											lowercase_     :					Dataset													=	self._create_dummy_dataset()
											dset.add_faiss_index_from_external_arrays(
											    external_arrays=np.ones((30, 5)		) * np.arange(30		).reshape(-1    ,							1		)    ,							index_name='''vecs'''		)
											dset.drop_index('''vecs'''		)
											self.assertRaises(__SCREAMING_SNAKE_CASE    ,							partial(dset.get_nearest_examples    ,							'''vecs2'''    ,							np.ones(5    ,							dtype=np.floataa		)		)		)






					def      _snake_case   (       self		):


											"""simple docstring"""





											from elasticsearch import Elasticsearch

											lowercase_     :					Dataset													=	self._create_dummy_dataset()
											with patch('''elasticsearch.Elasticsearch.search'''		) as mocked_search, patch(
											    '''elasticsearch.client.IndicesClient.create'''		) as mocked_index_create, patch('''elasticsearch.helpers.streaming_bulk'''		) as mocked_bulk:
																	lowercase_     :					List[Any]													=	{'''acknowledged''': True}
																	mocked_bulk.return_value([(True, None)] * 30		)
																	lowercase_     :					Tuple													=	{'''hits''': {'''hits''': [{'''_score''': 1, '''_id''': 29}]}}
																	lowercase_     :					Any													=	Elasticsearch()

																	dset.add_elasticsearch_index('''filename'''    ,							es_client=__SCREAMING_SNAKE_CASE		)
																	lowercase_						,			lowercase_     :					Dict													=	dset.get_nearest_examples('''filename'''    ,							'''my_name-train_29'''		)
																	self.assertEqual(examples['''filename'''][0]    ,							'''my_name-train_29'''		)







@require_faiss
class 			lowerCAmelCase__       (  lowerCamelCase_					):






					def      _snake_case   (       self		):


											"""simple docstring"""





											import faiss

											lowercase_     :					Dict													=	FaissIndex(metric_type=faiss.METRIC_INNER_PRODUCT		)

											# add vectors
											index.add_vectors(np.eye(5    ,							dtype=np.floataa		)		)
											self.assertIsNotNone(index.faiss_index		)
											self.assertEqual(index.faiss_index.ntotal    ,							5		)
											index.add_vectors(np.zeros((5, 5)    ,							dtype=np.floataa		)		)
											self.assertEqual(index.faiss_index.ntotal    ,							10		)

											# single query
											lowercase_     :					Optional[Any]													=	np.zeros(5    ,							dtype=np.floataa		)
											lowercase_     :					Optional[Any]													=	1
											lowercase_						,			lowercase_     :					Optional[Any]													=	index.search(__SCREAMING_SNAKE_CASE		)
											self.assertRaises(__SCREAMING_SNAKE_CASE    ,							index.search    ,							query.reshape(-1    ,							1		)		)
											self.assertGreater(scores[0]    ,							0		)
											self.assertEqual(indices[0]    ,							1		)

											# batched queries
											lowercase_     :					Any													=	np.eye(5    ,							dtype=np.floataa		)[::-1]
											lowercase_						,			lowercase_     :					Union[str, Any]													=	index.search_batch(__SCREAMING_SNAKE_CASE		)
											self.assertRaises(__SCREAMING_SNAKE_CASE    ,							index.search_batch    ,							queries[0]		)
											lowercase_     :					Tuple													=	[scores[0] for scores in total_scores]
											lowercase_     :					int													=	[indices[0] for indices in total_indices]
											self.assertGreater(np.min(__SCREAMING_SNAKE_CASE		)    ,							0		)
											self.assertListEqual([4, 3, 2, 1, 0]    ,							__SCREAMING_SNAKE_CASE		)






					def      _snake_case   (       self		):


											"""simple docstring"""





											import faiss

											lowercase_     :					List[str]													=	FaissIndex(string_factory='''Flat'''		)
											index.add_vectors(np.eye(5    ,							dtype=np.floataa		)		)
											self.assertIsInstance(index.faiss_index    ,							faiss.IndexFlat		)
											lowercase_     :					Any													=	FaissIndex(string_factory='''LSH'''		)
											index.add_vectors(np.eye(5    ,							dtype=np.floataa		)		)
											self.assertIsInstance(index.faiss_index    ,							faiss.IndexLSH		)
											with self.assertRaises(__SCREAMING_SNAKE_CASE		):
																	lowercase_     :					Optional[int]													=	FaissIndex(string_factory='''Flat'''    ,							custom_index=faiss.IndexFlat(5		)		)






					def      _snake_case   (       self		):


											"""simple docstring"""





											import faiss

											lowercase_     :					List[str]													=	faiss.IndexFlat(5		)
											lowercase_     :					Tuple													=	FaissIndex(custom_index=__SCREAMING_SNAKE_CASE		)
											index.add_vectors(np.eye(5    ,							dtype=np.floataa		)		)
											self.assertIsInstance(index.faiss_index    ,							faiss.IndexFlat		)






					def      _snake_case   (       self		):


											"""simple docstring"""





											import faiss

											lowercase_     :					str													=	FaissIndex(metric_type=faiss.METRIC_INNER_PRODUCT		)
											index.add_vectors(np.eye(5    ,							dtype=np.floataa		)		)

											# Setting delete=False and unlinking manually is not pretty... but it is required on Windows to
											# ensure somewhat stable behaviour. If we don't, we get PermissionErrors. This is an age-old issue.
											# see https://bugs.python.org/issue14243 and
											# https://stackoverflow.com/questions/23212435/permission-denied-to-write-to-my-temporary-file/23212515
											with tempfile.NamedTemporaryFile(delete=__SCREAMING_SNAKE_CASE		) as tmp_file:
																	index.save(tmp_file.name		)
																	lowercase_     :					str													=	FaissIndex.load(tmp_file.name		)
											os.unlink(tmp_file.name		)

											lowercase_     :					List[str]													=	np.zeros(5    ,							dtype=np.floataa		)
											lowercase_     :					Tuple													=	1
											lowercase_						,			lowercase_     :					List[Any]													=	index.search(__SCREAMING_SNAKE_CASE		)
											self.assertGreater(scores[0]    ,							0		)
											self.assertEqual(indices[0]    ,							1		)







@require_faiss
def  snake_case_		(    __SCREAMING_SNAKE_CASE :			Optional[Any]							):



						"""simple docstring"""





						import faiss

						lowercase_     :					Optional[Any]													=	FaissIndex(metric_type=faiss.METRIC_INNER_PRODUCT							)
						index.add_vectors(np.eye(5				,				dtype=np.floataa							)							)

						lowercase_     :					Optional[int]													=	'''index.faiss'''
						lowercase_     :					Optional[int]													=	F'''mock://{index_name}'''
						index.save(__SCREAMING_SNAKE_CASE				,				storage_options=mockfs.storage_options							)
						lowercase_     :					Optional[Any]													=	FaissIndex.load(__SCREAMING_SNAKE_CASE				,				storage_options=mockfs.storage_options							)

						lowercase_     :					List[str]													=	np.zeros(5				,				dtype=np.floataa							)
						lowercase_     :					Optional[int]													=	1
						lowercase_						,			lowercase_     :					Dict													=	index.search(__SCREAMING_SNAKE_CASE							)
						assert scores[0] > 0
						assert indices[0] == 1






@require_elasticsearch
class 			lowerCAmelCase__       (  lowerCamelCase_					):






					def      _snake_case   (       self		):


											"""simple docstring"""





											from elasticsearch import Elasticsearch

											with patch('''elasticsearch.Elasticsearch.search'''		) as mocked_search, patch(
											    '''elasticsearch.client.IndicesClient.create'''		) as mocked_index_create, patch('''elasticsearch.helpers.streaming_bulk'''		) as mocked_bulk:
																	lowercase_     :					Dict													=	Elasticsearch()
																	lowercase_     :					List[Any]													=	{'''acknowledged''': True}
																	lowercase_     :					Any													=	ElasticSearchIndex(es_client=__SCREAMING_SNAKE_CASE		)
																	mocked_bulk.return_value([(True, None)] * 3		)
																	index.add_documents(['''foo''', '''bar''', '''foobar''']		)

																	# single query
																	lowercase_     :					Optional[int]													=	'''foo'''
																	lowercase_     :					Optional[int]													=	{'''hits''': {'''hits''': [{'''_score''': 1, '''_id''': 0}]}}
																	lowercase_						,			lowercase_     :					str													=	index.search(__SCREAMING_SNAKE_CASE		)
																	self.assertEqual(scores[0]    ,							1		)
																	self.assertEqual(indices[0]    ,							0		)

																	# single query with timeout
																	lowercase_     :					Tuple													=	'''foo'''
																	lowercase_     :					int													=	{'''hits''': {'''hits''': [{'''_score''': 1, '''_id''': 0}]}}
																	lowercase_						,			lowercase_     :					str													=	index.search(__SCREAMING_SNAKE_CASE    ,							request_timeout=30		)
																	self.assertEqual(scores[0]    ,							1		)
																	self.assertEqual(indices[0]    ,							0		)

																	# batched queries
																	lowercase_     :					str													=	['''foo''', '''bar''', '''foobar''']
																	lowercase_     :					List[Any]													=	{'''hits''': {'''hits''': [{'''_score''': 1, '''_id''': 1}]}}
																	lowercase_						,			lowercase_     :					List[str]													=	index.search_batch(__SCREAMING_SNAKE_CASE		)
																	lowercase_     :					Dict													=	[scores[0] for scores in total_scores]
																	lowercase_     :					int													=	[indices[0] for indices in total_indices]
																	self.assertGreater(np.min(__SCREAMING_SNAKE_CASE		)    ,							0		)
																	self.assertListEqual([1, 1, 1]    ,							__SCREAMING_SNAKE_CASE		)

																	# batched queries with timeout
																	lowercase_     :					List[str]													=	['''foo''', '''bar''', '''foobar''']
																	lowercase_     :					List[str]													=	{'''hits''': {'''hits''': [{'''_score''': 1, '''_id''': 1}]}}
																	lowercase_						,			lowercase_     :					List[Any]													=	index.search_batch(__SCREAMING_SNAKE_CASE    ,							request_timeout=30		)
																	lowercase_     :					Any													=	[scores[0] for scores in total_scores]
																	lowercase_     :					List[str]													=	[indices[0] for indices in total_indices]
																	self.assertGreater(np.min(__SCREAMING_SNAKE_CASE		)    ,							0		)
																	self.assertListEqual([1, 1, 1]    ,							__SCREAMING_SNAKE_CASE		)





'''simple docstring'''







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


_lowercase			:  Any   							=	logging.get_logger(__name__)

_lowercase			:  Dict   							=	{
    "EleutherAI/gpt-neox-20b": "https://huggingface.co/EleutherAI/gpt-neox-20b/resolve/main/config.json",
    # See all GPTNeoX models at https://huggingface.co/models?filter=gpt_neox
}






class 			lowerCAmelCase__       (  lowerCamelCase_					):
					lowerCAmelCase_					       =					'''gpt_neox'''






					def __init__(       self    ,							__SCREAMING_SNAKE_CASE=5_04_32    ,							__SCREAMING_SNAKE_CASE=61_44    ,							__SCREAMING_SNAKE_CASE=44    ,							__SCREAMING_SNAKE_CASE=64    ,							__SCREAMING_SNAKE_CASE=2_45_76    ,							__SCREAMING_SNAKE_CASE="gelu"    ,							__SCREAMING_SNAKE_CASE=0.25    ,							__SCREAMING_SNAKE_CASE=1_00_00    ,							__SCREAMING_SNAKE_CASE=0.0    ,							__SCREAMING_SNAKE_CASE=0.0    ,							__SCREAMING_SNAKE_CASE=0.1    ,							__SCREAMING_SNAKE_CASE=20_48    ,							__SCREAMING_SNAKE_CASE=0.02    ,							__SCREAMING_SNAKE_CASE=1E-5    ,							__SCREAMING_SNAKE_CASE=True    ,							__SCREAMING_SNAKE_CASE=0    ,							__SCREAMING_SNAKE_CASE=2    ,							__SCREAMING_SNAKE_CASE=False    ,							__SCREAMING_SNAKE_CASE=True    ,							__SCREAMING_SNAKE_CASE=None    ,							**__SCREAMING_SNAKE_CASE    ,							):


											"""simple docstring"""





											super().__init__(bos_token_id=__SCREAMING_SNAKE_CASE    ,							eos_token_id=__SCREAMING_SNAKE_CASE    ,							**__SCREAMING_SNAKE_CASE		)
											lowercase_     :					Optional[Any]													=	vocab_size
											lowercase_     :					Optional[Any]													=	max_position_embeddings
											lowercase_     :					Optional[int]													=	hidden_size
											lowercase_     :					Tuple													=	num_hidden_layers
											lowercase_     :					Any													=	num_attention_heads
											lowercase_     :					str													=	intermediate_size
											lowercase_     :					Any													=	hidden_act
											lowercase_     :					Tuple													=	rotary_pct
											lowercase_     :					Optional[Any]													=	rotary_emb_base
											lowercase_     :					Any													=	attention_dropout
											lowercase_     :					str													=	hidden_dropout
											lowercase_     :					Dict													=	classifier_dropout
											lowercase_     :					Tuple													=	initializer_range
											lowercase_     :					List[str]													=	layer_norm_eps
											lowercase_     :					Union[str, Any]													=	use_cache
											lowercase_     :					int													=	tie_word_embeddings
											lowercase_     :					Tuple													=	use_parallel_residual
											lowercase_     :					Optional[Any]													=	rope_scaling
											self._rope_scaling_validation()

											if self.hidden_size % self.num_attention_heads != 0:
																	raise ValueError(
																	    '''The hidden size is not divisble by the number of attention heads! Make sure to update them!'''		)






					def      _snake_case   (       self		):


											"""simple docstring"""





											if self.rope_scaling is None:
																	return

											if not isinstance(self.rope_scaling    ,							__SCREAMING_SNAKE_CASE		) or len(self.rope_scaling		) != 2:
																	raise ValueError(
																	    '''`rope_scaling` must be a dictionary with with two fields, `name` and `factor`, '''
																	    F'''got {self.rope_scaling}'''		)
											lowercase_     :					List[Any]													=	self.rope_scaling.get('''type'''    ,							__SCREAMING_SNAKE_CASE		)
											lowercase_     :					int													=	self.rope_scaling.get('''factor'''    ,							__SCREAMING_SNAKE_CASE		)
											if rope_scaling_type is None or rope_scaling_type not in ["linear", "dynamic"]:
																	raise ValueError(
																	    F'''`rope_scaling`\'s name field must be one of [\'linear\', \'dynamic\'], got {rope_scaling_type}'''		)
											if rope_scaling_factor is None or not isinstance(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE		) or rope_scaling_factor <= 1.0:
																	raise ValueError(F'''`rope_scaling`\'s factor field must be an float > 1, got {rope_scaling_factor}'''		)



