


import collections.abc
from typing import Optional, Tuple, Union

import torch
import torch.utils.checkpoint
from torch import nn
from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss

from ...activations import ACTaFN
from ...modeling_outputs import BaseModelOutputWithNoAttention, ImageClassifierOutputWithNoAttention
from ...modeling_utils import PreTrainedModel
from ...utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, logging
from .configuration_poolformer import PoolFormerConfig


A       :		Optional[Any]    =   logging.get_logger(__name__)

# General docstring
A       :		List[str]    =   'PoolFormerConfig'

# Base docstring
A       :		Union[str, Any]    =   'sail/poolformer_s12'
A       :		List[str]    =   [1, 5_1_2, 7, 7]

# Image classification docstring
A       :		Any    =   'sail/poolformer_s12'
A       :		Any    =   'tabby, tabby cat'

A       :		Dict    =   [
    'sail/poolformer_s12',
    # See all PoolFormer models at https://huggingface.co/models?filter=poolformer
]






def  UpperCamelCase					(	__magic_name__     :							Dict    ,    __magic_name__     :							float = 0.0    ,    __magic_name__     :							bool = False     )   ->  Optional[Any]:
		"""simple docstring"""




		if drop_prob == 0.0 or not training:
				return input
		lowercase__     			=		1 - drop_prob
		lowercase__     			=		(input.shape[0],) + (1,) * (input.ndim - 1)  # work with diff dim tensors, not just 2D ConvNets
		lowercase__     			=		keep_prob + torch.rand(__magic_name__    ,    dtype=input.dtype    ,    device=input.device     )
		random_tensor.floor_()  # binarize
		lowercase__     			=		input.div(__magic_name__     ) * random_tensor
		return output



class 							A    (       nn.Module							):
					'''simple docstring'''






					def __init__(self    :						int      ,					_UpperCAmelCase    :						Optional[float] = None		)						->   None:




							"""simple docstring"""


							super().__init__()
							lowercase__     			=		drop_prob

					def   lowerCamelCase__     (self    :						Optional[Any]      ,					_UpperCAmelCase    :						torch.Tensor		)						->   torch.Tensor:




							"""simple docstring"""


							return drop_path(__UpperCAmelCase      ,					self.drop_prob      ,					self.training		)


					def   lowerCamelCase__     (self    :						int		)						->   str:




							"""simple docstring"""


							return "p={}".format(self.drop_prob		)






class 							A    (       nn.Module							):
					'''simple docstring'''






					def __init__(self    :						List[Any]      ,					_UpperCAmelCase    :						Optional[int]      ,					_UpperCAmelCase    :						Optional[int]      ,					_UpperCAmelCase    :						str      ,					_UpperCAmelCase    :						str      ,					_UpperCAmelCase    :						int      ,					_UpperCAmelCase    :						Any=None		)						->   int:




							"""simple docstring"""


							super().__init__()
							lowercase__     			=		patch_size if isinstance(__UpperCAmelCase      ,					collections.abc.Iterable		) else (patch_size, patch_size)
							lowercase__     			=		stride if isinstance(__UpperCAmelCase      ,					collections.abc.Iterable		) else (stride, stride)
							lowercase__     			=		padding if isinstance(__UpperCAmelCase      ,					collections.abc.Iterable		) else (padding, padding)

							lowercase__     			=		nn.Convad(__UpperCAmelCase      ,					__UpperCAmelCase      ,					kernel_size=__UpperCAmelCase      ,					stride=__UpperCAmelCase      ,					padding=__UpperCAmelCase		)
							lowercase__     			=		norm_layer(__UpperCAmelCase		) if norm_layer else nn.Identity()


					def   lowerCamelCase__     (self    :						int      ,					_UpperCAmelCase    :						Optional[int]		)						->   List[str]:




							"""simple docstring"""


							lowercase__     			=		self.projection(__UpperCAmelCase		)
							lowercase__     			=		self.norm(__UpperCAmelCase		)
							return embeddings






class 							A    (       nn.GroupNorm							):
					'''simple docstring'''






					def __init__(self    :						List[Any]      ,					_UpperCAmelCase    :						Union[str, Any]      ,					**_UpperCAmelCase    :						Union[str, Any]		)						->   Dict:




							"""simple docstring"""


							super().__init__(1      ,					__UpperCAmelCase      ,					**__UpperCAmelCase		)






class 							A    (       nn.Module							):
					'''simple docstring'''






					def __init__(self    :						int      ,					_UpperCAmelCase    :						Optional[Any]		)						->   List[Any]:




							"""simple docstring"""


							super().__init__()
							lowercase__     			=		nn.AvgPoolad(__UpperCAmelCase      ,					stride=1      ,					padding=pool_size // 2      ,					count_include_pad=__UpperCAmelCase		)


					def   lowerCamelCase__     (self    :						Optional[int]      ,					_UpperCAmelCase    :						List[str]		)						->   Union[str, Any]:




							"""simple docstring"""


							return self.pool(__UpperCAmelCase		) - hidden_states






class 							A    (       nn.Module							):
					'''simple docstring'''






					def __init__(self    :						Dict      ,					_UpperCAmelCase    :						Dict      ,					_UpperCAmelCase    :						List[str]      ,					_UpperCAmelCase    :						int      ,					_UpperCAmelCase    :						Dict		)						->   List[Any]:




							"""simple docstring"""


							super().__init__()
							lowercase__     			=		nn.Convad(__UpperCAmelCase      ,					__UpperCAmelCase      ,					1		)
							lowercase__     			=		nn.Convad(__UpperCAmelCase      ,					__UpperCAmelCase      ,					1		)
							lowercase__     			=		PoolFormerDropPath(__UpperCAmelCase		)
							if isinstance(config.hidden_act      ,					__UpperCAmelCase		):
									lowercase__     			=		ACTaFN[config.hidden_act]
							else:
									lowercase__     			=		config.hidden_act


					def   lowerCamelCase__     (self    :						int      ,					_UpperCAmelCase    :						Optional[int]		)						->   List[Any]:




							"""simple docstring"""


							lowercase__     			=		self.conva(__UpperCAmelCase		)
							lowercase__     			=		self.act_fn(__UpperCAmelCase		)
							lowercase__     			=		self.drop(__UpperCAmelCase		)
							lowercase__     			=		self.conva(__UpperCAmelCase		)
							lowercase__     			=		self.drop(__UpperCAmelCase		)

							return hidden_states






class 							A    (       nn.Module							):
					'''simple docstring'''






					def __init__(self    :						int      ,					_UpperCAmelCase    :						str      ,					_UpperCAmelCase    :						Dict      ,					_UpperCAmelCase    :						List[str]      ,					_UpperCAmelCase    :						str      ,					_UpperCAmelCase    :						Tuple      ,					_UpperCAmelCase    :						Tuple		)						->   int:




							"""simple docstring"""


							super().__init__()
							lowercase__     			=		PoolFormerPooling(__UpperCAmelCase		)
							lowercase__     			=		PoolFormerOutput(__UpperCAmelCase      ,					__UpperCAmelCase      ,					__UpperCAmelCase      ,					__UpperCAmelCase		)
							lowercase__     			=		PoolFormerGroupNorm(__UpperCAmelCase		)
							lowercase__     			=		PoolFormerGroupNorm(__UpperCAmelCase		)

							# Useful for training neural nets
							lowercase__     			=		PoolFormerDropPath(__UpperCAmelCase		) if drop_path > 0.0 else nn.Identity()
							lowercase__     			=		config.use_layer_scale
							if config.use_layer_scale:
									lowercase__     			=		nn.Parameter(
									    config.layer_scale_init_value * torch.ones((__UpperCAmelCase)		)      ,					requires_grad=__UpperCAmelCase		)
									lowercase__     			=		nn.Parameter(
									    config.layer_scale_init_value * torch.ones((__UpperCAmelCase)		)      ,					requires_grad=__UpperCAmelCase		)


					def   lowerCamelCase__     (self    :						Any      ,					_UpperCAmelCase    :						Dict		)						->   Any:




							"""simple docstring"""


							if self.use_layer_scale:
									lowercase__     			=		self.pooling(self.before_norm(__UpperCAmelCase		)		)
									lowercase__     			=		self.layer_scale_a.unsqueeze(-1		).unsqueeze(-1		) * pooling_output
									# First residual connection
									lowercase__     			=		hidden_states + self.drop_path(__UpperCAmelCase		)
									lowercase__     			=		()

									lowercase__     			=		self.output(self.after_norm(__UpperCAmelCase		)		)
									lowercase__     			=		self.layer_scale_a.unsqueeze(-1		).unsqueeze(-1		) * layer_output
									# Second residual connection
									lowercase__     			=		hidden_states + self.drop_path(__UpperCAmelCase		)

									lowercase__     			=		(output,) + outputs
									return outputs

							else:
									lowercase__     			=		self.drop_path(self.pooling(self.before_norm(__UpperCAmelCase		)		)		)
									# First residual connection
									lowercase__     			=		pooling_output + hidden_states
									lowercase__     			=		()

									# Second residual connection inside the PoolFormerOutput block
									lowercase__     			=		self.drop_path(self.output(self.after_norm(__UpperCAmelCase		)		)		)
									lowercase__     			=		hidden_states + layer_output

									lowercase__     			=		(output,) + outputs
									return outputs






class 							A    (       nn.Module							):
					'''simple docstring'''






					def __init__(self    :						List[Any]      ,					_UpperCAmelCase    :						Optional[int]		)						->   Dict:




							"""simple docstring"""


							super().__init__()
							lowercase__     			=		config
							# stochastic depth decay rule
							lowercase__     			=		[x.item() for x in torch.linspace(0      ,					config.drop_path_rate      ,					sum(config.depths		)		)]

							# patch embeddings
							lowercase__     			=		[]
							for i in range(config.num_encoder_blocks		):
									embeddings.append(
									    PoolFormerEmbeddings(
									        patch_size=config.patch_sizes[i]      ,					stride=config.strides[i]      ,					padding=config.padding[i]      ,					num_channels=config.num_channels if i == 0 else config.hidden_sizes[i - 1]      ,					hidden_size=config.hidden_sizes[i]      ,					)		)
							lowercase__     			=		nn.ModuleList(__UpperCAmelCase		)

							# Transformer blocks
							lowercase__     			=		[]
							lowercase__     			=		0
							for i in range(config.num_encoder_blocks		):
									# each block consists of layers
									lowercase__     			=		[]
									if i != 0:
											cur += config.depths[i - 1]
									for j in range(config.depths[i]		):
											layers.append(
											    PoolFormerLayer(
											        __UpperCAmelCase      ,					num_channels=config.hidden_sizes[i]      ,					pool_size=config.pool_size      ,					hidden_size=config.hidden_sizes[i]      ,					intermediate_size=int(config.hidden_sizes[i] * config.mlp_ratio		)      ,					drop_path=dpr[cur + j]      ,					)		)
									blocks.append(nn.ModuleList(__UpperCAmelCase		)		)

							lowercase__     			=		nn.ModuleList(__UpperCAmelCase		)


					def   lowerCamelCase__     (self    :						List[Any]      ,					_UpperCAmelCase    :						str      ,					_UpperCAmelCase    :						Optional[Any]=False      ,					_UpperCAmelCase    :						Tuple=True		)						->   Union[str, Any]:




							"""simple docstring"""


							lowercase__     			=		() if output_hidden_states else None

							lowercase__     			=		pixel_values
							for idx, layers in enumerate(zip(self.patch_embeddings      ,					self.block		)		):
									lowercase__			,	lowercase__     			=		layers
									# Get patch embeddings from hidden_states
									lowercase__     			=		embedding_layer(__UpperCAmelCase		)
									# Send the embeddings through the blocks
									for _, blk in enumerate(__UpperCAmelCase		):
											lowercase__     			=		blk(__UpperCAmelCase		)
											lowercase__     			=		layer_outputs[0]

									if output_hidden_states:
											lowercase__     			=		all_hidden_states + (hidden_states,)

							if not return_dict:
									return tuple(v for v in [hidden_states, all_hidden_states] if v is not None		)

							return BaseModelOutputWithNoAttention(last_hidden_state=__UpperCAmelCase      ,					hidden_states=__UpperCAmelCase		)






class 							A    (       UpperCAmelCase__							):
					'''simple docstring'''






					A__			= PoolFormerConfig
					A__			= '''poolformer'''
					A__			= '''pixel_values'''
					A__			= True

					def   lowerCamelCase__     (self    :						List[str]      ,					_UpperCAmelCase    :						List[str]		)						->   Optional[Any]:




							"""simple docstring"""


							if isinstance(__UpperCAmelCase      ,					(nn.Linear, nn.Convad)		):
									module.weight.data.normal_(mean=0.0      ,					std=self.config.initializer_range		)
									if module.bias is not None:
											module.bias.data.zero_()
							elif isinstance(__UpperCAmelCase      ,					nn.LayerNorm		):
									module.bias.data.zero_()
									module.weight.data.fill_(1.0		)


					def   lowerCamelCase__     (self    :						Tuple      ,					_UpperCAmelCase    :						Tuple      ,					_UpperCAmelCase    :						Union[str, Any]=False		)						->   int:




							"""simple docstring"""


							if isinstance(__UpperCAmelCase      ,					__UpperCAmelCase		):
									lowercase__     			=		value






A       :		int    =   r'\n    This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) sub-class. Use\n    it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and\n    behavior.\n\n    Parameters:\n        config ([`PoolFormerConfig`]): Model configuration class with all the parameters of the model.\n            Initializing with a config file does not load the weights associated with the model, only the\n            configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.\n'

A       :		int    =   r'\n    Args:\n        pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):\n            Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See\n            [`PoolFormerImageProcessor.__call__`] for details.\n'



@add_start_docstrings(
    '''The bare PoolFormer Model transformer outputting raw hidden-states without any specific head on top.''' ,  UpperCAmelCase__ ,  )
class 							A    (       UpperCAmelCase__							):
					'''simple docstring'''






					def __init__(self    :						str      ,					_UpperCAmelCase    :						int		)						->   Optional[Any]:




							"""simple docstring"""


							super().__init__(__UpperCAmelCase		)
							lowercase__     			=		config

							lowercase__     			=		PoolFormerEncoder(__UpperCAmelCase		)

							# Initialize weights and apply final processing
							self.post_init()

					def   lowerCamelCase__     (self    :						Optional[int]		)						->   Union[str, Any]:




							"""simple docstring"""


							return self.embeddings.patch_embeddings


					@add_start_docstrings_to_model_forward(__UpperCAmelCase		)
					@add_code_sample_docstrings(
					    checkpoint=_CHECKPOINT_FOR_DOC      ,					output_type=__UpperCAmelCase      ,					config_class=_CONFIG_FOR_DOC      ,					modality="""vision"""      ,					expected_output=_EXPECTED_OUTPUT_SHAPE      ,					)
					def   lowerCamelCase__     (self    :						str      ,					_UpperCAmelCase    :						Optional[torch.FloatTensor] = None      ,					_UpperCAmelCase    :						Optional[bool] = None      ,					_UpperCAmelCase    :						Optional[bool] = None      ,					)						->   Union[Tuple, BaseModelOutputWithNoAttention]:




							"""simple docstring"""


							lowercase__     			=		(
							    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
							)
							lowercase__     			=		return_dict if return_dict is not None else self.config.use_return_dict

							if pixel_values is None:
									raise ValueError("""You have to specify pixel_values"""		)

							lowercase__     			=		self.encoder(
							    __UpperCAmelCase      ,					output_hidden_states=__UpperCAmelCase      ,					return_dict=__UpperCAmelCase      ,					)
							lowercase__     			=		encoder_outputs[0]

							if not return_dict:
									return (sequence_output, None) + encoder_outputs[1:]

							return BaseModelOutputWithNoAttention(
							    last_hidden_state=__UpperCAmelCase      ,					hidden_states=encoder_outputs.hidden_states      ,					)






class 							A    (       nn.Module							):
					'''simple docstring'''






					def __init__(self    :						Optional[int]      ,					_UpperCAmelCase    :						Dict		)						->   List[str]:




							"""simple docstring"""


							super().__init__()
							lowercase__     			=		nn.Linear(config.hidden_size      ,					config.hidden_size		)


					def   lowerCamelCase__     (self    :						int      ,					_UpperCAmelCase    :						Optional[Any]		)						->   Union[str, Any]:




							"""simple docstring"""


							lowercase__     			=		self.dense(__UpperCAmelCase		)
							return output






@add_start_docstrings(
    '''
    PoolFormer Model transformer with an image classification head on top
    ''' ,  UpperCAmelCase__ ,  )
class 							A    (       UpperCAmelCase__							):
					'''simple docstring'''






					def __init__(self    :						Union[str, Any]      ,					_UpperCAmelCase    :						List[str]		)						->   Any:




							"""simple docstring"""


							super().__init__(__UpperCAmelCase		)
							lowercase__     			=		config.num_labels
							lowercase__     			=		PoolFormerModel(__UpperCAmelCase		)

							# Final norm
							lowercase__     			=		PoolFormerGroupNorm(config.hidden_sizes[-1]		)
							# Classifier head
							lowercase__     			=		(
							    nn.Linear(config.hidden_sizes[-1]      ,					config.num_labels		) if config.num_labels > 0 else nn.Identity()
							)

							# Initialize weights and apply final processing
							self.post_init()


					@add_start_docstrings_to_model_forward(__UpperCAmelCase		)
					@add_code_sample_docstrings(
					    checkpoint=_IMAGE_CLASS_CHECKPOINT      ,					output_type=__UpperCAmelCase      ,					config_class=_CONFIG_FOR_DOC      ,					expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT      ,					)
					def   lowerCamelCase__     (self    :						Tuple      ,					_UpperCAmelCase    :						Optional[torch.FloatTensor] = None      ,					_UpperCAmelCase    :						Optional[torch.LongTensor] = None      ,					_UpperCAmelCase    :						Optional[bool] = None      ,					_UpperCAmelCase    :						Optional[bool] = None      ,					)						->   Union[Tuple, ImageClassifierOutputWithNoAttention]:




							"""simple docstring"""


							lowercase__     			=		return_dict if return_dict is not None else self.config.use_return_dict

							lowercase__     			=		self.poolformer(
							    __UpperCAmelCase      ,					output_hidden_states=__UpperCAmelCase      ,					return_dict=__UpperCAmelCase      ,					)

							lowercase__     			=		outputs[0]

							lowercase__     			=		self.classifier(self.norm(__UpperCAmelCase		).mean([-2, -1]		)		)

							lowercase__     			=		None
							if labels is not None:
									if self.config.problem_type is None:
											if self.num_labels == 1:
													lowercase__     			=		"""regression"""
											elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
													lowercase__     			=		"""single_label_classification"""
											else:
													lowercase__     			=		"""multi_label_classification"""

									if self.config.problem_type == "regression":
											lowercase__     			=		MSELoss()
											if self.num_labels == 1:
													lowercase__     			=		loss_fct(logits.squeeze()      ,					labels.squeeze()		)
											else:
													lowercase__     			=		loss_fct(__UpperCAmelCase      ,					__UpperCAmelCase		)
									elif self.config.problem_type == "single_label_classification":
											lowercase__     			=		CrossEntropyLoss()
											lowercase__     			=		loss_fct(logits.view(-1      ,					self.num_labels		)      ,					labels.view(-1		)		)
									elif self.config.problem_type == "multi_label_classification":
											lowercase__     			=		BCEWithLogitsLoss()
											lowercase__     			=		loss_fct(__UpperCAmelCase      ,					__UpperCAmelCase		)

							if not return_dict:
									lowercase__     			=		(logits,) + outputs[2:]
									return ((loss,) + output) if loss is not None else output

							return ImageClassifierOutputWithNoAttention(loss=__UpperCAmelCase      ,					logits=__UpperCAmelCase      ,					hidden_states=outputs.hidden_states		)









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


UpperCAmelCase__				   =		pytest.mark.integration






@require_faiss
class 		lowercase_					(	lowercase   ):






      '''simple docstring'''




      def       __lowerCAmelCase					(      self      :						Tuple  )    ->Any:




       """simple docstring"""


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







      def       __lowerCAmelCase					(      self      :						Tuple  )    ->Any:




       """simple docstring"""


       import faiss

       a		  =      self._create_dummy_dataset()
       a		  =      dset.map(
           lambda __UpperCAmelCase ,					__UpperCAmelCase  : {"vecs": i * np.ones(5 ,					dtype=np.floataa  )} ,					with_indices=__UpperCAmelCase ,					keep_in_memory=__UpperCAmelCase  )
       a		  =      dset.add_faiss_index('''vecs''' ,					batch_size=100 ,					metric_type=faiss.METRIC_INNER_PRODUCT  )
       a  ,     a		  =      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       __lowerCAmelCase					(      self      :						Union[str, Any]  )    ->Optional[Any]:




       """simple docstring"""


       import faiss

       a		  =      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=100 ,					metric_type=faiss.METRIC_INNER_PRODUCT ,					)
       a  ,     a		  =      dset.get_nearest_examples('''vecs''' ,					np.ones(5 ,					dtype=np.floataa  )  )
       self.assertEqual(examples['''filename'''][0] ,					'''my_name-train_29'''  )







      def       __lowerCAmelCase					(      self      :						Any  )    ->Union[str, Any]:




       """simple docstring"""


       import faiss

       a		  =      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=__UpperCAmelCase  ) as tmp_file:
        dset.save_faiss_index('''vecs''' ,					tmp_file.name  )
        dset.load_faiss_index('''vecs2''' ,					tmp_file.name  )
       os.unlink(tmp_file.name  )

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







      def       __lowerCAmelCase					(      self      :						Optional[Any]  )    ->Any:




       """simple docstring"""


       a		  =      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(__UpperCAmelCase ,					partial(dset.get_nearest_examples ,					'''vecs2''' ,					np.ones(5 ,					dtype=np.floataa  )  )  )







      def       __lowerCAmelCase					(      self      :						List[Any]  )    ->List[str]:




       """simple docstring"""


       from elasticsearch import Elasticsearch

       a		  =      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:
        a		  =      {'''acknowledged''': True}
        mocked_bulk.return_value([(True, None)] * 30  )
        a		  =      {'''hits''': {'''hits''': [{'''_score''': 1, '''_id''': 29}]}}
        a		  =      Elasticsearch()

        dset.add_elasticsearch_index('''filename''' ,					es_client=__UpperCAmelCase  )
        a  ,     a		  =      dset.get_nearest_examples('''filename''' ,					'''my_name-train_29'''  )
        self.assertEqual(examples['''filename'''][0] ,					'''my_name-train_29'''  )






@require_faiss
class 		lowercase_					(	lowercase   ):






      '''simple docstring'''




      def       __lowerCAmelCase					(      self      :						Any  )    ->Any:




       """simple docstring"""


       import faiss

       a		  =      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
       a		  =      np.zeros(5 ,					dtype=np.floataa  )
       a		  =      1
       a  ,     a		  =      index.search(__UpperCAmelCase  )
       self.assertRaises(__UpperCAmelCase ,					index.search ,					query.reshape(-1 ,					1  )  )
       self.assertGreater(scores[0] ,					0  )
       self.assertEqual(indices[0] ,					1  )

       # batched queries
       a		  =      np.eye(5 ,					dtype=np.floataa  )[::-1]
       a  ,     a		  =      index.search_batch(__UpperCAmelCase  )
       self.assertRaises(__UpperCAmelCase ,					index.search_batch ,					queries[0]  )
       a		  =      [scores[0] for scores in total_scores]
       a		  =      [indices[0] for indices in total_indices]
       self.assertGreater(np.min(__UpperCAmelCase  ) ,					0  )
       self.assertListEqual([4, 3, 2, 1, 0] ,					__UpperCAmelCase  )







      def       __lowerCAmelCase					(      self      :						Any  )    ->List[Any]:




       """simple docstring"""


       import faiss

       a		  =      FaissIndex(string_factory='''Flat'''  )
       index.add_vectors(np.eye(5 ,					dtype=np.floataa  )  )
       self.assertIsInstance(index.faiss_index ,					faiss.IndexFlat  )
       a		  =      FaissIndex(string_factory='''LSH'''  )
       index.add_vectors(np.eye(5 ,					dtype=np.floataa  )  )
       self.assertIsInstance(index.faiss_index ,					faiss.IndexLSH  )
       with self.assertRaises(__UpperCAmelCase  ):
        a		  =      FaissIndex(string_factory='''Flat''' ,					custom_index=faiss.IndexFlat(5  )  )







      def       __lowerCAmelCase					(      self      :						int  )    ->Optional[Any]:




       """simple docstring"""


       import faiss

       a		  =      faiss.IndexFlat(5  )
       a		  =      FaissIndex(custom_index=__UpperCAmelCase  )
       index.add_vectors(np.eye(5 ,					dtype=np.floataa  )  )
       self.assertIsInstance(index.faiss_index ,					faiss.IndexFlat  )







      def       __lowerCAmelCase					(      self      :						int  )    ->Dict:




       """simple docstring"""


       import faiss

       a		  =      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=__UpperCAmelCase  ) as tmp_file:
        index.save(tmp_file.name  )
        a		  =      FaissIndex.load(tmp_file.name  )
       os.unlink(tmp_file.name  )

       a		  =      np.zeros(5 ,					dtype=np.floataa  )
       a		  =      1
       a  ,     a		  =      index.search(__UpperCAmelCase  )
       self.assertGreater(scores[0] ,					0  )
       self.assertEqual(indices[0] ,					1  )
@require_faiss
def 						_a    (      a :Dict )							->			Any:
 import faiss

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

 a		  =      '''index.faiss'''
 a		  =      F"""mock://{index_name}"""
 index.save(a	,							storage_options=mockfs.storage_options )
 a		  =      FaissIndex.load(a	,							storage_options=mockfs.storage_options )

 a		  =      np.zeros(5	,							dtype=np.floataa )
 a		  =      1
 a  ,     a		  =      index.search(a )
 assert scores[0] > 0
 assert indices[0] == 1






@require_elasticsearch
class 		lowercase_					(	lowercase   ):






      '''simple docstring'''




      def       __lowerCAmelCase					(      self      :						int  )    ->List[Any]:




       """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:
        a		  =      Elasticsearch()
        a		  =      {'''acknowledged''': True}
        a		  =      ElasticSearchIndex(es_client=__UpperCAmelCase  )
        mocked_bulk.return_value([(True, None)] * 3  )
        index.add_documents(['''foo''', '''bar''', '''foobar''']  )

        # single query
        a		  =      '''foo'''
        a		  =      {'''hits''': {'''hits''': [{'''_score''': 1, '''_id''': 0}]}}
        a  ,     a		  =      index.search(__UpperCAmelCase  )
        self.assertEqual(scores[0] ,					1  )
        self.assertEqual(indices[0] ,					0  )

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

        # batched queries
        a		  =      ['''foo''', '''bar''', '''foobar''']
        a		  =      {'''hits''': {'''hits''': [{'''_score''': 1, '''_id''': 1}]}}
        a  ,     a		  =      index.search_batch(__UpperCAmelCase  )
        a		  =      [scores[0] for scores in total_scores]
        a		  =      [indices[0] for indices in total_indices]
        self.assertGreater(np.min(__UpperCAmelCase  ) ,					0  )
        self.assertListEqual([1, 1, 1] ,					__UpperCAmelCase  )

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


