





import copy

from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ..auto import CONFIG_MAPPING


_UpperCAmelCase				:       int	   =				logging.get_logger(__name__)

_UpperCAmelCase				:       str	   =				{
    """SenseTime/deformable-detr""": """https://huggingface.co/sensetime/deformable-detr/resolve/main/config.json""",
    # See all Deformable DETR models at https://huggingface.co/models?filter=deformable-detr
}

class    lowerCAmelCase							(					__UpperCamelCase			):
 UpperCAmelCase__													=		"""deformable_detr"""
 UpperCAmelCase__													=		{
     """hidden_size""": """d_model""",
     """num_attention_heads""": """encoder_attention_heads""",
 }
 def __init__(      self  :      str			,     UpperCAmelCase  :      Any=True			,     UpperCAmelCase  :      str=None			,     UpperCAmelCase  :      Dict=3			,     UpperCAmelCase  :      Union[str, Any]=300			,     UpperCAmelCase  :      Dict=1024			,     UpperCAmelCase  :      Optional[int]=6			,     UpperCAmelCase  :      Optional[int]=1024			,     UpperCAmelCase  :      Optional[int]=8			,     UpperCAmelCase  :      List[str]=6			,     UpperCAmelCase  :      int=1024			,     UpperCAmelCase  :      str=8			,     UpperCAmelCase  :      Optional[int]=0.0			,     UpperCAmelCase  :      str=True			,     UpperCAmelCase  :      List[Any]="relu"			,     UpperCAmelCase  :      str=256			,     UpperCAmelCase  :      Dict=0.1			,     UpperCAmelCase  :      List[Any]=0.0			,     UpperCAmelCase  :      Optional[Any]=0.0			,     UpperCAmelCase  :      List[str]=0.0_2			,     UpperCAmelCase  :      int=1.0			,     UpperCAmelCase  :      int=True			,     UpperCAmelCase  :      List[str]=False			,     UpperCAmelCase  :      str="sine"			,     UpperCAmelCase  :      int="resnet50"			,     UpperCAmelCase  :      Any=True			,     UpperCAmelCase  :      int=False			,     UpperCAmelCase  :      Union[str, Any]=4			,     UpperCAmelCase  :      Union[str, Any]=4			,     UpperCAmelCase  :      Tuple=4			,     UpperCAmelCase  :      Any=False			,     UpperCAmelCase  :      Optional[int]=300			,     UpperCAmelCase  :      List[str]=False			,     UpperCAmelCase  :      int=1			,     UpperCAmelCase  :      Optional[Any]=5			,     UpperCAmelCase  :      Optional[int]=2			,     UpperCAmelCase  :      Union[str, Any]=1			,     UpperCAmelCase  :      Optional[Any]=1			,     UpperCAmelCase  :      List[str]=5			,     UpperCAmelCase  :      Dict=2			,     UpperCAmelCase  :      Any=0.1			,     UpperCAmelCase  :      List[Any]=0.2_5			,     UpperCAmelCase  :      Dict=False			,     **UpperCAmelCase  :      Optional[Any]			,     )			->	int:
     if backbone_config is not None and use_timm_backbone:
         raise ValueError('You can\'t specify both `backbone_config` and `use_timm_backbone`.'	)

     if not use_timm_backbone:
         if backbone_config is None:
             logger.info('`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone.'	)
             lowerCamelCase__    :      Optional[Any]					 =		CONFIG_MAPPING['resnet'](out_features=['stage4']	)
         elif isinstance(UpperCAmelCase			,     UpperCAmelCase	):
             lowerCamelCase__    :      Optional[int]					 =		backbone_config.get('model_type'	)
             lowerCamelCase__    :      int					 =		CONFIG_MAPPING[backbone_model_type]
             lowerCamelCase__    :      Union[str, Any]					 =		config_class.from_dict(UpperCAmelCase	)
     lowerCamelCase__    :      Optional[Any]					 =		use_timm_backbone
     lowerCamelCase__    :      List[Any]					 =		backbone_config
     lowerCamelCase__    :      Union[str, Any]					 =		num_channels
     lowerCamelCase__    :      str					 =		num_queries
     lowerCamelCase__    :      Any					 =		max_position_embeddings
     lowerCamelCase__    :      Optional[Any]					 =		d_model
     lowerCamelCase__    :      List[str]					 =		encoder_ffn_dim
     lowerCamelCase__    :      Optional[Any]					 =		encoder_layers
     lowerCamelCase__    :      Tuple					 =		encoder_attention_heads
     lowerCamelCase__    :      Optional[int]					 =		decoder_ffn_dim
     lowerCamelCase__    :      List[Any]					 =		decoder_layers
     lowerCamelCase__    :      Optional[Any]					 =		decoder_attention_heads
     lowerCamelCase__    :      str					 =		dropout
     lowerCamelCase__    :      Any					 =		attention_dropout
     lowerCamelCase__    :      Any					 =		activation_dropout
     lowerCamelCase__    :      Tuple					 =		activation_function
     lowerCamelCase__    :      Union[str, Any]					 =		init_std
     lowerCamelCase__    :      str					 =		init_xavier_std
     lowerCamelCase__    :      Optional[int]					 =		encoder_layerdrop
     lowerCamelCase__    :      Union[str, Any]					 =		auxiliary_loss
     lowerCamelCase__    :      List[Any]					 =		position_embedding_type
     lowerCamelCase__    :      List[Any]					 =		backbone
     lowerCamelCase__    :      int					 =		use_pretrained_backbone
     lowerCamelCase__    :      List[str]					 =		dilation
     # deformable attributes
     lowerCamelCase__    :      Any					 =		num_feature_levels
     lowerCamelCase__    :      Optional[int]					 =		encoder_n_points
     lowerCamelCase__    :      Union[str, Any]					 =		decoder_n_points
     lowerCamelCase__    :      Union[str, Any]					 =		two_stage
     lowerCamelCase__    :      str					 =		two_stage_num_proposals
     lowerCamelCase__    :      Union[str, Any]					 =		with_box_refine
     if two_stage is True and with_box_refine is False:
         raise ValueError('If two_stage is True, with_box_refine must be True.'	)
     # Hungarian matcher
     lowerCamelCase__    :      int					 =		class_cost
     lowerCamelCase__    :      str					 =		bbox_cost
     lowerCamelCase__    :      Optional[int]					 =		giou_cost
     # Loss coefficients
     lowerCamelCase__    :      int					 =		mask_loss_coefficient
     lowerCamelCase__    :      int					 =		dice_loss_coefficient
     lowerCamelCase__    :      Tuple					 =		bbox_loss_coefficient
     lowerCamelCase__    :      Union[str, Any]					 =		giou_loss_coefficient
     lowerCamelCase__    :      Any					 =		eos_coefficient
     lowerCamelCase__    :      Any					 =		focal_alpha
     lowerCamelCase__    :      Tuple					 =		disable_custom_kernels
     super().__init__(is_encoder_decoder=UpperCAmelCase			,     **UpperCAmelCase	)
 @property
 def      A_	(      self  :      str	)			->	int:
     return self.encoder_attention_heads
 @property
 def      A_	(      self  :      str	)			->	int:
     return self.d_model







 def      A_	(      self  :      Optional[Any]	)			->	List[Any]:
     lowerCamelCase__    :      int					 =		copy.deepcopy(self.__dict__	)
     if self.backbone_config is not None:
         lowerCamelCase__    :      Union[str, Any]					 =		self.backbone_config.to_dict()
     lowerCamelCase__    :      Dict					 =		self.__class__.model_type
     return output












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				:       str	   =				pytest.mark.integration

@require_faiss
class    lowerCAmelCase							(					__UpperCamelCase			):
 def      A_	(      self  :      List[Any]	)			->	Union[str, Any]:
     lowerCamelCase__    :      List[Any]					 =		Dataset.from_dict({'filename': ['my_name-train' + '_' + str(UpperCAmelCase	) for x in np.arange(30	).tolist()]}	)
     return dset
 def      A_	(      self  :      Optional[Any]	)			->	Optional[int]:
     import faiss

     lowerCamelCase__    :      Dataset					 =		self._create_dummy_dataset()
     lowerCamelCase__    :      List[Any]					 =		dset.map(
         lambda UpperCAmelCase			,     UpperCAmelCase	: {"vecs": i * np.ones(5			,     dtype=np.floataa	)}			,     with_indices=UpperCAmelCase			,     keep_in_memory=UpperCAmelCase	)
     lowerCamelCase__    :      Tuple					 =		dset.add_faiss_index('vecs'			,     batch_size=100			,     metric_type=faiss.METRIC_INNER_PRODUCT	)
     lowerCamelCase__ ,							lowerCamelCase__    :      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      A_	(      self  :      Union[str, Any]	)			->	int:
     import faiss

     lowerCamelCase__    :      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=100			,     metric_type=faiss.METRIC_INNER_PRODUCT			,     )
     lowerCamelCase__ ,							lowerCamelCase__    :      Optional[Any]					 =		dset.get_nearest_examples('vecs'			,     np.ones(5			,     dtype=np.floataa	)	)
     self.assertEqual(examples['filename'][0]			,     'my_name-train_29'	)
 def      A_	(      self  :      List[str]	)			->	Tuple:
     import faiss

     lowerCamelCase__    :      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=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	)

     lowerCamelCase__ ,							lowerCamelCase__    :      str					 =		dset.get_nearest_examples('vecs2'			,     np.ones(5			,     dtype=np.floataa	)	)
     self.assertEqual(examples['filename'][0]			,     'my_name-train_29'	)
 def      A_	(      self  :      Any	)			->	Optional[Any]:
     lowerCamelCase__    :      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(UpperCAmelCase			,     partial(dset.get_nearest_examples			,     'vecs2'			,     np.ones(5			,     dtype=np.floataa	)	)	)







 def      A_	(      self  :      Dict	)			->	Dict:
     from elasticsearch import Elasticsearch

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

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


@require_faiss
class    lowerCAmelCase							(					__UpperCamelCase			):
 def      A_	(      self  :      Any	)			->	Dict:
     import faiss

     lowerCamelCase__    :      Tuple					 =		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
     lowerCamelCase__    :      int					 =		np.zeros(5			,     dtype=np.floataa	)
     lowerCamelCase__    :      Any					 =		1
     lowerCamelCase__ ,							lowerCamelCase__    :      Union[str, Any]					 =		index.search(UpperCAmelCase	)
     self.assertRaises(UpperCAmelCase			,     index.search			,     query.reshape(-1			,     1	)	)
     self.assertGreater(scores[0]			,     0	)
     self.assertEqual(indices[0]			,     1	)

     # batched queries
     lowerCamelCase__    :      str					 =		np.eye(5			,     dtype=np.floataa	)[::-1]
     lowerCamelCase__ ,							lowerCamelCase__    :      Dict					 =		index.search_batch(UpperCAmelCase	)
     self.assertRaises(UpperCAmelCase			,     index.search_batch			,     queries[0]	)
     lowerCamelCase__    :      str					 =		[scores[0] for scores in total_scores]
     lowerCamelCase__    :      List[str]					 =		[indices[0] for indices in total_indices]
     self.assertGreater(np.min(UpperCAmelCase	)			,     0	)
     self.assertListEqual([4, 3, 2, 1, 0]			,     UpperCAmelCase	)
 def      A_	(      self  :      List[Any]	)			->	List[Any]:
     import faiss

     lowerCamelCase__    :      Any					 =		FaissIndex(string_factory='Flat'	)
     index.add_vectors(np.eye(5			,     dtype=np.floataa	)	)
     self.assertIsInstance(index.faiss_index			,     faiss.IndexFlat	)
     lowerCamelCase__    :      Tuple					 =		FaissIndex(string_factory='LSH'	)
     index.add_vectors(np.eye(5			,     dtype=np.floataa	)	)
     self.assertIsInstance(index.faiss_index			,     faiss.IndexLSH	)
     with self.assertRaises(UpperCAmelCase	):
         lowerCamelCase__    :      List[str]					 =		FaissIndex(string_factory='Flat'			,     custom_index=faiss.IndexFlat(5	)	)
 def      A_	(      self  :      List[str]	)			->	Optional[int]:
     import faiss

     lowerCamelCase__    :      Optional[Any]					 =		faiss.IndexFlat(5	)
     lowerCamelCase__    :      int					 =		FaissIndex(custom_index=UpperCAmelCase	)
     index.add_vectors(np.eye(5			,     dtype=np.floataa	)	)
     self.assertIsInstance(index.faiss_index			,     faiss.IndexFlat	)







 def      A_	(      self  :      Any	)			->	Optional[int]:
     import faiss

     lowerCamelCase__    :      Any					 =		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	)
         lowerCamelCase__    :      List[Any]					 =		FaissIndex.load(tmp_file.name	)
     os.unlink(tmp_file.name	)

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


@require_faiss
def        SCREAMING_SNAKE_CASE      (       _UpperCAmelCase )			->     Any:
    import faiss

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

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

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


@require_elasticsearch
class    lowerCAmelCase							(					__UpperCamelCase			):
 def      A_	(      self  :      Dict	)			->	List[Any]:
     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:
         lowerCamelCase__    :      Any					 =		Elasticsearch()
         lowerCamelCase__    :      Tuple					 =		{'acknowledged': True}
         lowerCamelCase__    :      Tuple					 =		ElasticSearchIndex(es_client=UpperCAmelCase	)
         mocked_bulk.return_value([(True, None)] * 3	)
         index.add_documents(['foo', 'bar', 'foobar']	)

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

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

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

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





