

'''simple docstring'''



import json
import os
import pickle
import shutil
import tempfile
from unittest import TestCase
from unittest.mock import patch

import numpy as np
from datasets import Dataset

from transformers import is_faiss_available
from transformers.models.bart.configuration_bart import BartConfig
from transformers.models.bart.tokenization_bart import BartTokenizer
from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES as DPR_VOCAB_FILES_NAMES
from transformers.models.dpr.configuration_dpr import DPRConfig
from transformers.models.dpr.tokenization_dpr import DPRContextEncoderTokenizer, DPRQuestionEncoderTokenizer
from transformers.models.rag.configuration_rag import RagConfig
from transformers.models.rag.retrieval_rag import CustomHFIndex, RagRetriever
from transformers.models.roberta.tokenization_roberta import VOCAB_FILES_NAMES as BART_VOCAB_FILES_NAMES
from transformers.testing_utils import require_faiss, require_sentencepiece, require_tokenizers, require_torch


if is_faiss_available():
     import faiss



@require_faiss
class 				_snake_case					(   a_			):

  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      lowerCAmelCase 						=							tempfile.mkdtemp()
      lowerCAmelCase 						=							8

      # DPR tok
      lowerCAmelCase 						=							[
          '[UNK]',
          '[CLS]',
          '[SEP]',
          '[PAD]',
          '[MASK]',
          'want',
          '##want',
          '##ed',
          'wa',
          'un',
          'runn',
          '##ing',
          ',',
          'low',
          'lowest',
      ]
      lowerCAmelCase 						=							os.path.join(self.tmpdirname      , 'dpr_tokenizer'	)
      os.makedirs(_SCREAMING_SNAKE_CASE      , exist_ok=_SCREAMING_SNAKE_CASE	)
      lowerCAmelCase 						=							os.path.join(_SCREAMING_SNAKE_CASE      , DPR_VOCAB_FILES_NAMES['vocab_file']	)
      with open(self.vocab_file      , 'w'      , encoding='utf-8'	) as vocab_writer:
          vocab_writer.write(''.join([x + '\n' for x in vocab_tokens]	)	)

      # BART tok
      lowerCAmelCase 						=							[
          'l',
          'o',
          'w',
          'e',
          'r',
          's',
          't',
          'i',
          'd',
          'n',
          '\u0120',
          '\u0120l',
          '\u0120n',
          '\u0120lo',
          '\u0120low',
          'er',
          '\u0120lowest',
          '\u0120newer',
          '\u0120wider',
          '<unk>',
      ]
      lowerCAmelCase 						=							dict(zip(_SCREAMING_SNAKE_CASE      , range(len(_SCREAMING_SNAKE_CASE	)	)	)	)
      lowerCAmelCase 						=							['#version: 0.2', '\u0120 l', '\u0120l o', '\u0120lo w', 'e r', '']
      lowerCAmelCase 						=							{'unk_token': '<unk>'}

      lowerCAmelCase 						=							os.path.join(self.tmpdirname      , 'bart_tokenizer'	)
      os.makedirs(_SCREAMING_SNAKE_CASE      , exist_ok=_SCREAMING_SNAKE_CASE	)
      lowerCAmelCase 						=							os.path.join(_SCREAMING_SNAKE_CASE      , BART_VOCAB_FILES_NAMES['vocab_file']	)
      lowerCAmelCase 						=							os.path.join(_SCREAMING_SNAKE_CASE      , BART_VOCAB_FILES_NAMES['merges_file']	)
      with open(self.vocab_file      , 'w'      , encoding='utf-8'	) as fp:
          fp.write(json.dumps(_SCREAMING_SNAKE_CASE	) + '\n'	)
      with open(self.merges_file      , 'w'      , encoding='utf-8'	) as fp:
          fp.write('\n'.join(_SCREAMING_SNAKE_CASE	)	)

  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      return DPRQuestionEncoderTokenizer.from_pretrained(os.path.join(self.tmpdirname      , 'dpr_tokenizer'	)	)

  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      return DPRContextEncoderTokenizer.from_pretrained(os.path.join(self.tmpdirname      , 'dpr_tokenizer'	)	)

  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      return BartTokenizer.from_pretrained(os.path.join(self.tmpdirname      , 'bart_tokenizer'	)	)

  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      shutil.rmtree(self.tmpdirname	)

  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      lowerCAmelCase 						=							Dataset.from_dict(
          {
              'id': ['0', '1'],
              'text': ['foo', 'bar'],
              'title': ['Foo', 'Bar'],
              'embeddings': [np.ones(self.retrieval_vector_size	), 2 * np.ones(self.retrieval_vector_size	)],
          }	)
      dataset.add_faiss_index('embeddings'      , string_factory='Flat'      , metric_type=faiss.METRIC_INNER_PRODUCT	)
      return dataset

  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      lowerCAmelCase 						=							self.get_dummy_dataset()
      lowerCAmelCase 						=							RagConfig(
          retrieval_vector_size=self.retrieval_vector_size      , question_encoder=DPRConfig().to_dict()      , generator=BartConfig().to_dict()      , )
      with patch('transformers.models.rag.retrieval_rag.load_dataset'	) as mock_load_dataset:
          lowerCAmelCase 						=							dataset
          lowerCAmelCase 						=							RagRetriever(
              _SCREAMING_SNAKE_CASE      , question_encoder_tokenizer=self.get_dpr_tokenizer()      , generator_tokenizer=self.get_bart_tokenizer()      , )
      return retriever

  def   _SCREAMING_SNAKE_CASE				(				self      , _SCREAMING_SNAKE_CASE	):





      '''simple docstring'''

      lowerCAmelCase 						=							self.get_dummy_dataset()
      lowerCAmelCase 						=							RagConfig(
          retrieval_vector_size=self.retrieval_vector_size      , question_encoder=DPRConfig().to_dict()      , generator=BartConfig().to_dict()      , index_name='custom'      , )
      if from_disk:
          lowerCAmelCase 						=							os.path.join(self.tmpdirname      , 'dataset'	)
          lowerCAmelCase 						=							os.path.join(self.tmpdirname      , 'index.faiss'	)
          dataset.get_index('embeddings'	).save(os.path.join(self.tmpdirname      , 'index.faiss'	)	)
          dataset.drop_index('embeddings'	)
          dataset.save_to_disk(os.path.join(self.tmpdirname      , 'dataset'	)	)
          del dataset
          lowerCAmelCase 						=							RagRetriever(
              _SCREAMING_SNAKE_CASE      , question_encoder_tokenizer=self.get_dpr_tokenizer()      , generator_tokenizer=self.get_bart_tokenizer()      , )
      else:
          lowerCAmelCase 						=							RagRetriever(
              _SCREAMING_SNAKE_CASE      , question_encoder_tokenizer=self.get_dpr_tokenizer()      , generator_tokenizer=self.get_bart_tokenizer()      , index=CustomHFIndex(config.retrieval_vector_size      , _SCREAMING_SNAKE_CASE	)      , )
      return retriever

  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      lowerCAmelCase 						=							Dataset.from_dict(
          {
              'id': ['0', '1'],
              'text': ['foo', 'bar'],
              'title': ['Foo', 'Bar'],
              'embeddings': [np.ones(self.retrieval_vector_size + 1	), 2 * np.ones(self.retrieval_vector_size + 1	)],
          }	)
      dataset.add_faiss_index('embeddings'      , string_factory='Flat'      , metric_type=faiss.METRIC_INNER_PRODUCT	)

      lowerCAmelCase 						=							os.path.join(self.tmpdirname      , 'hf_bert_base.hnswSQ8_correct_phi_128.c_index'	)
      dataset.save_faiss_index('embeddings'      , index_file_name + '.index.dpr'	)
      pickle.dump(dataset['id']      , open(index_file_name + '.index_meta.dpr'      , 'wb'	)	)

      lowerCAmelCase 						=							os.path.join(self.tmpdirname      , 'psgs_w100.tsv.pkl'	)
      lowerCAmelCase 						=							{sample['id']: [sample['text'], sample['title']] for sample in dataset}
      pickle.dump(_SCREAMING_SNAKE_CASE      , open(_SCREAMING_SNAKE_CASE      , 'wb'	)	)

      lowerCAmelCase 						=							RagConfig(
          retrieval_vector_size=self.retrieval_vector_size      , question_encoder=DPRConfig().to_dict()      , generator=BartConfig().to_dict()      , index_name='legacy'      , index_path=self.tmpdirname      , )
      lowerCAmelCase 						=							RagRetriever(
          _SCREAMING_SNAKE_CASE      , question_encoder_tokenizer=self.get_dpr_tokenizer()      , generator_tokenizer=self.get_bart_tokenizer()	)
      return retriever

  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      lowerCAmelCase 						=							1
      lowerCAmelCase 						=							self.get_dummy_canonical_hf_index_retriever()
      lowerCAmelCase 						=							np.array(
          [np.ones(self.retrieval_vector_size	), -np.ones(self.retrieval_vector_size	)]      , dtype=np.floataa	)
      lowerCAmelCase						,   lowerCAmelCase						,   lowerCAmelCase 						=							retriever.retrieve(_SCREAMING_SNAKE_CASE      , n_docs=_SCREAMING_SNAKE_CASE	)
      self.assertEqual(retrieved_doc_embeds.shape      , (2, n_docs, self.retrieval_vector_size)	)
      self.assertEqual(len(_SCREAMING_SNAKE_CASE	)      , 2	)
      self.assertEqual(sorted(doc_dicts[0]	)      , ['embeddings', 'id', 'text', 'title']	)
      self.assertEqual(len(doc_dicts[0]['id']	)      , _SCREAMING_SNAKE_CASE	)
      self.assertEqual(doc_dicts[0]['id'][0]      , '1'	)  # max inner product is reached with second doc
      self.assertEqual(doc_dicts[1]['id'][0]      , '0'	)  # max inner product is reached with first doc
      self.assertListEqual(doc_ids.tolist()      , [[1], [0]]	)

  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      lowerCAmelCase 						=							self.get_dummy_canonical_hf_index_retriever()
      with tempfile.TemporaryDirectory() as tmp_dirname:
          with patch('transformers.models.rag.retrieval_rag.load_dataset'	) as mock_load_dataset:
              lowerCAmelCase 						=							self.get_dummy_dataset()
              retriever.save_pretrained(_SCREAMING_SNAKE_CASE	)
              lowerCAmelCase 						=							RagRetriever.from_pretrained(_SCREAMING_SNAKE_CASE	)
              self.assertIsInstance(_SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE	)
              lowerCAmelCase 						=							np.array(
                  [np.ones(self.retrieval_vector_size	), -np.ones(self.retrieval_vector_size	)]      , dtype=np.floataa	)
              lowerCAmelCase 						=							retriever.retrieve(_SCREAMING_SNAKE_CASE      , n_docs=1	)
              self.assertTrue(out is not None	)

  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      lowerCAmelCase 						=							1
      lowerCAmelCase 						=							self.get_dummy_custom_hf_index_retriever(from_disk=_SCREAMING_SNAKE_CASE	)
      lowerCAmelCase 						=							np.array(
          [np.ones(self.retrieval_vector_size	), -np.ones(self.retrieval_vector_size	)]      , dtype=np.floataa	)
      lowerCAmelCase						,   lowerCAmelCase						,   lowerCAmelCase 						=							retriever.retrieve(_SCREAMING_SNAKE_CASE      , n_docs=_SCREAMING_SNAKE_CASE	)
      self.assertEqual(retrieved_doc_embeds.shape      , (2, n_docs, self.retrieval_vector_size)	)
      self.assertEqual(len(_SCREAMING_SNAKE_CASE	)      , 2	)
      self.assertEqual(sorted(doc_dicts[0]	)      , ['embeddings', 'id', 'text', 'title']	)
      self.assertEqual(len(doc_dicts[0]['id']	)      , _SCREAMING_SNAKE_CASE	)
      self.assertEqual(doc_dicts[0]['id'][0]      , '1'	)  # max inner product is reached with second doc
      self.assertEqual(doc_dicts[1]['id'][0]      , '0'	)  # max inner product is reached with first doc
      self.assertListEqual(doc_ids.tolist()      , [[1], [0]]	)

  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      lowerCAmelCase 						=							self.get_dummy_custom_hf_index_retriever(from_disk=_SCREAMING_SNAKE_CASE	)
      with tempfile.TemporaryDirectory() as tmp_dirname:
          retriever.save_pretrained(_SCREAMING_SNAKE_CASE	)
          lowerCAmelCase 						=							RagRetriever.from_pretrained(_SCREAMING_SNAKE_CASE	)
          self.assertIsInstance(_SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE	)
          lowerCAmelCase 						=							np.array(
              [np.ones(self.retrieval_vector_size	), -np.ones(self.retrieval_vector_size	)]      , dtype=np.floataa	)
          lowerCAmelCase 						=							retriever.retrieve(_SCREAMING_SNAKE_CASE      , n_docs=1	)
          self.assertTrue(out is not None	)

  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      lowerCAmelCase 						=							1
      lowerCAmelCase 						=							self.get_dummy_custom_hf_index_retriever(from_disk=_SCREAMING_SNAKE_CASE	)
      lowerCAmelCase 						=							np.array(
          [np.ones(self.retrieval_vector_size	), -np.ones(self.retrieval_vector_size	)]      , dtype=np.floataa	)
      lowerCAmelCase						,   lowerCAmelCase						,   lowerCAmelCase 						=							retriever.retrieve(_SCREAMING_SNAKE_CASE      , n_docs=_SCREAMING_SNAKE_CASE	)
      self.assertEqual(retrieved_doc_embeds.shape      , (2, n_docs, self.retrieval_vector_size)	)
      self.assertEqual(len(_SCREAMING_SNAKE_CASE	)      , 2	)
      self.assertEqual(sorted(doc_dicts[0]	)      , ['embeddings', 'id', 'text', 'title']	)
      self.assertEqual(len(doc_dicts[0]['id']	)      , _SCREAMING_SNAKE_CASE	)
      self.assertEqual(doc_dicts[0]['id'][0]      , '1'	)  # max inner product is reached with second doc
      self.assertEqual(doc_dicts[1]['id'][0]      , '0'	)  # max inner product is reached with first doc
      self.assertListEqual(doc_ids.tolist()      , [[1], [0]]	)

  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      lowerCAmelCase 						=							self.get_dummy_custom_hf_index_retriever(from_disk=_SCREAMING_SNAKE_CASE	)
      with tempfile.TemporaryDirectory() as tmp_dirname:
          retriever.save_pretrained(_SCREAMING_SNAKE_CASE	)
          lowerCAmelCase 						=							RagRetriever.from_pretrained(_SCREAMING_SNAKE_CASE	)
          self.assertIsInstance(_SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE	)
          lowerCAmelCase 						=							np.array(
              [np.ones(self.retrieval_vector_size	), -np.ones(self.retrieval_vector_size	)]      , dtype=np.floataa	)
          lowerCAmelCase 						=							retriever.retrieve(_SCREAMING_SNAKE_CASE      , n_docs=1	)
          self.assertTrue(out is not None	)

  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      lowerCAmelCase 						=							1
      lowerCAmelCase 						=							self.get_dummy_legacy_index_retriever()
      lowerCAmelCase 						=							np.array(
          [np.ones(self.retrieval_vector_size	), -np.ones(self.retrieval_vector_size	)]      , dtype=np.floataa	)
      lowerCAmelCase						,   lowerCAmelCase						,   lowerCAmelCase 						=							retriever.retrieve(_SCREAMING_SNAKE_CASE      , n_docs=_SCREAMING_SNAKE_CASE	)
      self.assertEqual(retrieved_doc_embeds.shape      , (2, n_docs, self.retrieval_vector_size)	)
      self.assertEqual(len(_SCREAMING_SNAKE_CASE	)      , 2	)
      self.assertEqual(sorted(doc_dicts[0]	)      , ['text', 'title']	)
      self.assertEqual(len(doc_dicts[0]['text']	)      , _SCREAMING_SNAKE_CASE	)
      self.assertEqual(doc_dicts[0]['text'][0]      , 'bar'	)  # max inner product is reached with second doc
      self.assertEqual(doc_dicts[1]['text'][0]      , 'foo'	)  # max inner product is reached with first doc
      self.assertListEqual(doc_ids.tolist()      , [[1], [0]]	)

  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      lowerCAmelCase 						=							self.get_dummy_legacy_index_retriever()
      with tempfile.TemporaryDirectory() as tmp_dirname:
          retriever.save_pretrained(_SCREAMING_SNAKE_CASE	)
          lowerCAmelCase 						=							RagRetriever.from_pretrained(_SCREAMING_SNAKE_CASE	)
          self.assertIsInstance(_SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE	)
          lowerCAmelCase 						=							np.array(
              [np.ones(self.retrieval_vector_size	), -np.ones(self.retrieval_vector_size	)]      , dtype=np.floataa	)
          lowerCAmelCase 						=							retriever.retrieve(_SCREAMING_SNAKE_CASE      , n_docs=1	)
          self.assertTrue(out is not None	)

  @require_torch
  @require_tokenizers
  @require_sentencepiece
  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      import torch

      lowerCAmelCase 						=							1
      lowerCAmelCase 						=							self.get_dummy_canonical_hf_index_retriever()
      lowerCAmelCase 						=							[[5, 7], [10, 11]]
      lowerCAmelCase 						=							np.array(
          [np.ones(self.retrieval_vector_size	), -np.ones(self.retrieval_vector_size	)]      , dtype=np.floataa	)
      lowerCAmelCase 						=							retriever(_SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , prefix=retriever.config.generator.prefix      , n_docs=_SCREAMING_SNAKE_CASE	)
      lowerCAmelCase						,   lowerCAmelCase						,   lowerCAmelCase 						=							(
          out['context_input_ids'],
          out['context_attention_mask'],
          out['retrieved_doc_embeds'],
      )
      self.assertEqual(retrieved_doc_embeds.shape      , (2, n_docs, self.retrieval_vector_size)	)
      self.assertIsInstance(_SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE	)
      self.assertIsInstance(_SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE	)
      self.assertIsInstance(_SCREAMING_SNAKE_CASE      , np.ndarray	)

      lowerCAmelCase 						=							retriever(
          _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , prefix=retriever.config.generator.prefix      , n_docs=_SCREAMING_SNAKE_CASE      , return_tensors='pt'      , )
      lowerCAmelCase						,   lowerCAmelCase						,   lowerCAmelCase						,   lowerCAmelCase 						=							(  # noqa: F841
          out['context_input_ids'],
          out['context_attention_mask'],
          out['retrieved_doc_embeds'],
          out['doc_ids'],
      )
      self.assertEqual(retrieved_doc_embeds.shape      , (2, n_docs, self.retrieval_vector_size)	)
      self.assertIsInstance(_SCREAMING_SNAKE_CASE      , torch.Tensor	)
      self.assertIsInstance(_SCREAMING_SNAKE_CASE      , torch.Tensor	)
      self.assertIsInstance(_SCREAMING_SNAKE_CASE      , torch.Tensor	)


  @require_torch
  @require_tokenizers
  @require_sentencepiece
  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      lowerCAmelCase 						=							self.get_dpr_ctx_encoder_tokenizer()
      lowerCAmelCase 						=							1
      lowerCAmelCase 						=							self.get_dummy_custom_hf_index_retriever(from_disk=_SCREAMING_SNAKE_CASE	)
      retriever.set_ctx_encoder_tokenizer(_SCREAMING_SNAKE_CASE	)

      lowerCAmelCase 						=							[[5, 7], [10, 11]]
      lowerCAmelCase 						=							np.array(
          [np.ones(self.retrieval_vector_size	), -np.ones(self.retrieval_vector_size	)]      , dtype=np.floataa	)
      lowerCAmelCase 						=							retriever(_SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , prefix=retriever.config.generator.prefix      , n_docs=_SCREAMING_SNAKE_CASE	)

      self.assertEqual(
          len(_SCREAMING_SNAKE_CASE	)      , 6	)  # check whether the retriever output consist of 6 attributes including tokenized docs
      self.assertEqual(
          all(k in out for k in ('tokenized_doc_ids', 'tokenized_doc_attention_mask')	)      , _SCREAMING_SNAKE_CASE	)  # check for doc token related keys in dictionary.








'''simple docstring'''



import inspect
import unittest

from transformers import ConvNextConfig
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_backbone_common import BackboneTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
     import torch

     from transformers import ConvNextBackbone, ConvNextForImageClassification, ConvNextModel
     from transformers.models.convnext.modeling_convnext import CONVNEXT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
     from PIL import Image

     from transformers import AutoImageProcessor



class 				_snake_case					:

  def __init__(				self      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE=13      , _SCREAMING_SNAKE_CASE=32      , _SCREAMING_SNAKE_CASE=3      , _SCREAMING_SNAKE_CASE=4      , _SCREAMING_SNAKE_CASE=[10, 20, 30, 40]      , _SCREAMING_SNAKE_CASE=[2, 2, 3, 2]      , _SCREAMING_SNAKE_CASE=True      , _SCREAMING_SNAKE_CASE=True      , _SCREAMING_SNAKE_CASE=37      , _SCREAMING_SNAKE_CASE="gelu"      , _SCREAMING_SNAKE_CASE=10      , _SCREAMING_SNAKE_CASE=0.02      , _SCREAMING_SNAKE_CASE=["stage2", "stage3", "stage4"]      , _SCREAMING_SNAKE_CASE=[2, 3, 4]      , _SCREAMING_SNAKE_CASE=None      , ):





      '''simple docstring'''

      lowerCAmelCase 						=							parent
      lowerCAmelCase 						=							batch_size
      lowerCAmelCase 						=							image_size
      lowerCAmelCase 						=							num_channels
      lowerCAmelCase 						=							num_stages
      lowerCAmelCase 						=							hidden_sizes
      lowerCAmelCase 						=							depths
      lowerCAmelCase 						=							is_training
      lowerCAmelCase 						=							use_labels
      lowerCAmelCase 						=							intermediate_size
      lowerCAmelCase 						=							hidden_act
      lowerCAmelCase 						=							num_labels
      lowerCAmelCase 						=							initializer_range
      lowerCAmelCase 						=							out_features
      lowerCAmelCase 						=							out_indices
      lowerCAmelCase 						=							scope

  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      lowerCAmelCase 						=							floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]	)

      lowerCAmelCase 						=							None
      if self.use_labels:
          lowerCAmelCase 						=							ids_tensor([self.batch_size]      , self.num_labels	)

      lowerCAmelCase 						=							self.get_config()
      return config, pixel_values, labels

  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      return ConvNextConfig(
          num_channels=self.num_channels      , hidden_sizes=self.hidden_sizes      , depths=self.depths      , num_stages=self.num_stages      , hidden_act=self.hidden_act      , is_decoder=_SCREAMING_SNAKE_CASE      , initializer_range=self.initializer_range      , out_features=self.out_features      , out_indices=self.out_indices      , num_labels=self.num_labels      , )

  def   _SCREAMING_SNAKE_CASE				(				self      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE	):





      '''simple docstring'''

      lowerCAmelCase 						=							ConvNextModel(config=_SCREAMING_SNAKE_CASE	)
      model.to(_SCREAMING_SNAKE_CASE	)
      model.eval()
      lowerCAmelCase 						=							model(_SCREAMING_SNAKE_CASE	)
      # expected last hidden states: B, C, H // 32, W // 32
      self.parent.assertEqual(
          result.last_hidden_state.shape      , (self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32)      , )

  def   _SCREAMING_SNAKE_CASE				(				self      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE	):





      '''simple docstring'''

      lowerCAmelCase 						=							ConvNextForImageClassification(_SCREAMING_SNAKE_CASE	)
      model.to(_SCREAMING_SNAKE_CASE	)
      model.eval()
      lowerCAmelCase 						=							model(_SCREAMING_SNAKE_CASE      , labels=_SCREAMING_SNAKE_CASE	)
      self.parent.assertEqual(result.logits.shape      , (self.batch_size, self.num_labels)	)

  def   _SCREAMING_SNAKE_CASE				(				self      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE	):





      '''simple docstring'''

      lowerCAmelCase 						=							ConvNextBackbone(config=_SCREAMING_SNAKE_CASE	)
      model.to(_SCREAMING_SNAKE_CASE	)
      model.eval()
      lowerCAmelCase 						=							model(_SCREAMING_SNAKE_CASE	)

      # verify hidden states
      self.parent.assertEqual(len(result.feature_maps	)      , len(config.out_features	)	)
      self.parent.assertListEqual(list(result.feature_maps[0].shape	)      , [self.batch_size, self.hidden_sizes[1], 4, 4]	)

      # verify channels
      self.parent.assertEqual(len(model.channels	)      , len(config.out_features	)	)
      self.parent.assertListEqual(model.channels      , config.hidden_sizes[1:]	)

      # verify backbone works with out_features=None
      lowerCAmelCase 						=							None
      lowerCAmelCase 						=							ConvNextBackbone(config=_SCREAMING_SNAKE_CASE	)
      model.to(_SCREAMING_SNAKE_CASE	)
      model.eval()
      lowerCAmelCase 						=							model(_SCREAMING_SNAKE_CASE	)

      # verify feature maps
      self.parent.assertEqual(len(result.feature_maps	)      , 1	)
      self.parent.assertListEqual(list(result.feature_maps[0].shape	)      , [self.batch_size, self.hidden_sizes[-1], 1, 1]	)

      # verify channels
      self.parent.assertEqual(len(model.channels	)      , 1	)
      self.parent.assertListEqual(model.channels      , [config.hidden_sizes[-1]]	)


  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      lowerCAmelCase 						=							self.prepare_config_and_inputs()
      lowerCAmelCase						,   lowerCAmelCase						,   lowerCAmelCase 						=							config_and_inputs
      lowerCAmelCase 						=							{'pixel_values': pixel_values}
      return config, inputs_dict



@require_torch
class 				_snake_case					(   a_     , a_     , unittest.TestCase			):
  SCREAMING_SNAKE_CASE			:       List[Any]				=     (
      (
          ConvNextModel,
          ConvNextForImageClassification,
          ConvNextBackbone,
      )
      if is_torch_available()
      else ()
  )
  SCREAMING_SNAKE_CASE			:       int				=     (
      {'''feature-extraction''': ConvNextModel, '''image-classification''': ConvNextForImageClassification}
      if is_torch_available()
      else {}
  )

  SCREAMING_SNAKE_CASE			:       Tuple				=     True
  SCREAMING_SNAKE_CASE			:       Union[str, Any]				=     False
  SCREAMING_SNAKE_CASE			:       Tuple				=     False
  SCREAMING_SNAKE_CASE			:       List[str]				=     False
  SCREAMING_SNAKE_CASE			:       Optional[int]				=     False

  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      lowerCAmelCase 						=							ConvNextModelTester(self	)
      lowerCAmelCase 						=							ConfigTester(self      , config_class=_SCREAMING_SNAKE_CASE      , has_text_modality=_SCREAMING_SNAKE_CASE      , hidden_size=37	)

  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      self.create_and_test_config_common_properties()
      self.config_tester.create_and_test_config_to_json_string()
      self.config_tester.create_and_test_config_to_json_file()
      self.config_tester.create_and_test_config_from_and_save_pretrained()
      self.config_tester.create_and_test_config_with_num_labels()
      self.config_tester.check_config_can_be_init_without_params()
      self.config_tester.check_config_arguments_init()

  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      return

  @unittest.skip(reason='ConvNext does not use inputs_embeds'	)
  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      pass

  @unittest.skip(reason='ConvNext does not support input and output embeddings'	)
  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      pass

  @unittest.skip(reason='ConvNext does not use feedforward chunking'	)
  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      pass

  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      lowerCAmelCase						,   lowerCAmelCase 						=							self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes:
          lowerCAmelCase 						=							model_class(_SCREAMING_SNAKE_CASE	)
          lowerCAmelCase 						=							inspect.signature(model.forward	)
          # signature.parameters is an OrderedDict => so arg_names order is deterministic
          lowerCAmelCase 						=							[*signature.parameters.keys()]

          lowerCAmelCase 						=							['pixel_values']
          self.assertListEqual(arg_names[:1]      , _SCREAMING_SNAKE_CASE	)

  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      lowerCAmelCase 						=							self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE	)

  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      lowerCAmelCase 						=							self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_backbone(*_SCREAMING_SNAKE_CASE	)

  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      def check_hidden_states_output(_SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE	):
          lowerCAmelCase 						=							model_class(_SCREAMING_SNAKE_CASE	)
          model.to(_SCREAMING_SNAKE_CASE	)
          model.eval()

          with torch.no_grad():
              lowerCAmelCase 						=							model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE	)	)

          lowerCAmelCase 						=							outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

          lowerCAmelCase 						=							self.model_tester.num_stages
          self.assertEqual(len(_SCREAMING_SNAKE_CASE	)      , expected_num_stages + 1	)

          # ConvNext's feature maps are of shape (batch_size, num_channels, height, width)
          self.assertListEqual(
              list(hidden_states[0].shape[-2:]	)      , [self.model_tester.image_size // 4, self.model_tester.image_size // 4]      , )

      lowerCAmelCase						,   lowerCAmelCase 						=							self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes:
          lowerCAmelCase 						=							True
          check_hidden_states_output(_SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE	)

          # check that output_hidden_states also work using config
          del inputs_dict["output_hidden_states"]
          lowerCAmelCase 						=							True

          check_hidden_states_output(_SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE	)

  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      lowerCAmelCase 						=							self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_image_classification(*_SCREAMING_SNAKE_CASE	)


  @slow
  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      for model_name in CONVNEXT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
          lowerCAmelCase 						=							ConvNextModel.from_pretrained(_SCREAMING_SNAKE_CASE	)
          self.assertIsNotNone(_SCREAMING_SNAKE_CASE	)



def       snake_case   (	) ->  List[str]:


    """simple docstring"""


    lowerCAmelCase 						=							Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'			)
    return image



@require_torch
@require_vision
class 				_snake_case					(   unittest.TestCase			):

  @cached_property
  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      return AutoImageProcessor.from_pretrained('facebook/convnext-tiny-224'	) if is_vision_available() else None


  @slow
  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      lowerCAmelCase 						=							ConvNextForImageClassification.from_pretrained('facebook/convnext-tiny-224'	).to(_SCREAMING_SNAKE_CASE	)

      lowerCAmelCase 						=							self.default_image_processor
      lowerCAmelCase 						=							prepare_img()
      lowerCAmelCase 						=							image_processor(images=_SCREAMING_SNAKE_CASE      , return_tensors='pt'	).to(_SCREAMING_SNAKE_CASE	)

      # forward pass
      with torch.no_grad():
          lowerCAmelCase 						=							model(**_SCREAMING_SNAKE_CASE	)

      # verify the logits
      lowerCAmelCase 						=							torch.Size((1, 10_00)	)
      self.assertEqual(outputs.logits.shape      , _SCREAMING_SNAKE_CASE	)

      lowerCAmelCase 						=							torch.tensor([-0.0_260, -0.4_739, 0.1_911]	).to(_SCREAMING_SNAKE_CASE	)

      self.assertTrue(torch.allclose(outputs.logits[0, :3]      , _SCREAMING_SNAKE_CASE      , atol=1e-4	)	)



@require_torch
class 				_snake_case					(   unittest.TestCase     , a_			):
  SCREAMING_SNAKE_CASE			:       Dict				=     (ConvNextBackbone,) if is_torch_available() else ()
  SCREAMING_SNAKE_CASE			:       Tuple				=     ConvNextConfig

  SCREAMING_SNAKE_CASE			:       Dict				=     False

  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      lowerCAmelCase 						=							ConvNextModelTester(self	)





