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     a_			:






 '''simple docstring'''






 def __init__(      self :  Any      ,   lowercase__ :  Optional[int]      ,   lowercase__ :  int=13      ,   lowercase__ :  Optional[Any]=32      ,   lowercase__ :  Any=3      ,   lowercase__ :  Union[str, Any]=4      ,   lowercase__ :  str=[10, 20, 30, 40]      ,   lowercase__ :  Any=[2, 2, 3, 2]      ,   lowercase__ :  Optional[Any]=True      ,   lowercase__ :  Any=True      ,   lowercase__ :  Tuple=37      ,   lowercase__ :  List[str]="gelu"      ,   lowercase__ :  Any=10      ,   lowercase__ :  Dict=0.02      ,   lowercase__ :  Optional[Any]=["stage2", "stage3", "stage4"]      ,   lowercase__ :  List[Any]=[2, 3, 4]      ,   lowercase__ :  Union[str, Any]=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 		__snake_case						(      self :  List[str]):







        '''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 		__snake_case						(      self :  Optional[int]):







        '''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=lowercase__      ,   initializer_range=self.initializer_range      ,   out_features=self.out_features      ,   out_indices=self.out_indices      ,   num_labels=self.num_labels      ,   )




 def 		__snake_case						(      self :  Optional[int]      ,   lowercase__ :  Tuple      ,   lowercase__ :  List[str]      ,   lowercase__ :  Dict):







        '''simple docstring'''



        lowerCAmelCase__					     = ConvNextModel(config=lowercase__)
        model.to(lowercase__)
        model.eval()
        lowerCAmelCase__					     = model(lowercase__)
        # 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 		__snake_case						(      self :  Optional[int]      ,   lowercase__ :  Tuple      ,   lowercase__ :  str      ,   lowercase__ :  Optional[int]):







        '''simple docstring'''



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




 def 		__snake_case						(      self :  Optional[Any]      ,   lowercase__ :  int      ,   lowercase__ :  Tuple      ,   lowercase__ :  List[Any]):







        '''simple docstring'''



        lowerCAmelCase__					     = ConvNextBackbone(config=lowercase__)
        model.to(lowercase__)
        model.eval()
        lowerCAmelCase__					     = model(lowercase__)

        # 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=lowercase__)
        model.to(lowercase__)
        model.eval()
        lowerCAmelCase__					     = model(lowercase__)

        # 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 		__snake_case						(      self :  Union[str, Any]):







        '''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     a_			(						SCREAMING_SNAKE_CASE	,					SCREAMING_SNAKE_CASE	,					unittest.TestCase						):






 '''simple docstring'''






 UpperCAmelCase_     			= (
     (
         ConvNextModel,
         ConvNextForImageClassification,
         ConvNextBackbone,
     )
     if is_torch_available()
     else ()
 )
 UpperCAmelCase_     			= (
     {'feature-extraction': ConvNextModel, 'image-classification': ConvNextForImageClassification}
     if is_torch_available()
     else {}
 )

 UpperCAmelCase_     			= True
 UpperCAmelCase_     			= False
 UpperCAmelCase_     			= False
 UpperCAmelCase_     			= False
 UpperCAmelCase_     			= False




 def 		__snake_case						(      self :  Optional[int]):







        '''simple docstring'''



        lowerCAmelCase__					     = ConvNextModelTester(self)
        lowerCAmelCase__					     = ConfigTester(self      ,   config_class=lowercase__      ,   has_text_modality=lowercase__      ,   hidden_size=37)




 def 		__snake_case						(      self :  Dict):







        '''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 		__snake_case						(      self :  str):







        '''simple docstring'''



        return




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







        '''simple docstring'''



        pass




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







        '''simple docstring'''



        pass




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







        '''simple docstring'''



        pass




 def 		__snake_case						(      self :  List[Any]):







        '''simple docstring'''



        lowerCAmelCase__     , lowerCAmelCase__					     = self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
               lowerCAmelCase__					     = model_class(lowercase__)
               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]      ,   lowercase__)




 def 		__snake_case						(      self :  List[Any]):







        '''simple docstring'''



        lowerCAmelCase__					     = self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*lowercase__)




 def 		__snake_case						(      self :  List[str]):







        '''simple docstring'''



        lowerCAmelCase__					     = self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_backbone(*lowercase__)




 def 		__snake_case						(      self :  Optional[int]):







        '''simple docstring'''



        def check_hidden_states_output(lowercase__ :  Tuple      ,   lowercase__ :  List[str]      ,   lowercase__ :  List[str]):
               lowerCAmelCase__					     = model_class(lowercase__)
               model.to(lowercase__)
               model.eval()

               with torch.no_grad():
                      lowerCAmelCase__					     = model(**self._prepare_for_class(lowercase__      ,   lowercase__))

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

               lowerCAmelCase__					     = self.model_tester.num_stages
               self.assertEqual(len(lowercase__)      ,   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(lowercase__      ,   lowercase__      ,   lowercase__)

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

               check_hidden_states_output(lowercase__      ,   lowercase__      ,   lowercase__)




 def 		__snake_case						(      self :  Union[str, Any]):







        '''simple docstring'''



        lowerCAmelCase__					     = self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_classification(*lowercase__)




 @slow
 def 		__snake_case						(      self :  Any):







        '''simple docstring'''



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






def 				__lowerCamelCase		(						):
       lowerCAmelCase__					     = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'     )
       return image
@require_torch
@require_vision
class     a_			(						unittest.TestCase						):






 '''simple docstring'''






 @cached_property
 def 		__snake_case						(      self :  str):







        '''simple docstring'''



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




 @slow
 def 		__snake_case						(      self :  int):







        '''simple docstring'''



        lowerCAmelCase__					     = ConvNextForImageClassification.from_pretrained('facebook/convnext-tiny-224').to(lowercase__)

        lowerCAmelCase__					     = self.default_image_processor
        lowerCAmelCase__					     = prepare_img()
        lowerCAmelCase__					     = image_processor(images=lowercase__      ,   return_tensors='pt').to(lowercase__)

        # forward pass
        with torch.no_grad():
               lowerCAmelCase__					     = model(**lowercase__)

        # verify the logits
        lowerCAmelCase__					     = torch.Size((1, 1_000))
        self.assertEqual(outputs.logits.shape      ,   lowercase__)

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

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






@require_torch
class     a_			(						unittest.TestCase	,					SCREAMING_SNAKE_CASE						):






 '''simple docstring'''






 UpperCAmelCase_     			= (ConvNextBackbone,) if is_torch_available() else ()
 UpperCAmelCase_     			= ConvNextConfig

 UpperCAmelCase_     			= False




 def 		__snake_case						(      self :  str):







        '''simple docstring'''



        lowerCAmelCase__					     = ConvNextModelTester(self)


import logging
import os

import quant_trainer
import torch
from torch.utils.data import DataLoader

from transformers import Trainer, is_torch_tpu_available
from transformers.trainer_utils import PredictionOutput


lowerCAmelCase__     						=			logging.getLogger(__name__)

if is_torch_tpu_available(check_device=False):
    import torch_xla.core.xla_model as xm
    import torch_xla.debug.metrics as met
class     a_			(						SCREAMING_SNAKE_CASE						):






 '''simple docstring'''






 def __init__(      self :  int      ,   *lowercase__ :  Dict      ,   lowercase__ :  List[Any]=None      ,   lowercase__ :  Optional[Any]=None      ,   lowercase__ :  Dict=None      ,   **lowercase__ :  Optional[int]):







        '''simple docstring'''



        super().__init__(*lowercase__      ,   **lowercase__)
        lowerCAmelCase__					     = eval_examples
        lowerCAmelCase__					     = post_process_function
        lowerCAmelCase__					     = quant_trainer_args
        lowerCAmelCase__					     = 128  # default number of calibration samples




 def 		__snake_case						(      self :  Tuple      ,   lowercase__ :  Any=None):







        '''simple docstring'''



        if calib_dataset is None and self.calib_dataset is None:
               raise ValueError('Trainer: calibration requires an calib_dataset.')
        lowerCAmelCase__					     = calib_dataset if calib_dataset is not None else self.calib_dataset

        lowerCAmelCase__					     = self._remove_unused_columns(lowercase__      ,   description='Calibration')

        return DataLoader(
            lowercase__      ,   batch_size=self.args.eval_batch_size      ,   collate_fn=self.data_collator      ,   drop_last=self.args.dataloader_drop_last      ,   num_workers=self.args.dataloader_num_workers      ,   pin_memory=self.args.dataloader_pin_memory      ,   shuffle=lowercase__      ,   )




 def 		__snake_case						(      self :  List[Any]      ,   lowercase__ :  Union[str, Any]=None):







        '''simple docstring'''



        lowerCAmelCase__					     = self.train_dataset if calib_dataset is None else calib_dataset
        lowerCAmelCase__					     = self.get_calib_dataloader(lowercase__)

        lowerCAmelCase__					     = self.model
        quant_trainer.configure_model(lowercase__      ,   self.quant_trainer_args      ,   calib=lowercase__)
        model.eval()
        quant_trainer.enable_calibration(lowercase__)

        logger.info('***** Running calibration *****')
        logger.info(F"""  Num examples = {self.calib_num}""")
        logger.info(F"""  Batch size = {calib_dataloader.batch_size}""")

        for step, inputs in enumerate(lowercase__):
               # Prediction step
               lowerCAmelCase__     , lowerCAmelCase__     , lowerCAmelCase__					     = self.prediction_step(lowercase__      ,   lowercase__      ,   prediction_loss_only=lowercase__)
               if (step + 1) * calib_dataloader.batch_size >= self.calib_num:
                      break

        quant_trainer.finish_calibration(lowercase__      ,   self.quant_trainer_args)
        lowerCAmelCase__					     = model




 def 		__snake_case						(      self :  Optional[Any]      ,   lowercase__ :  List[Any]=None      ,   lowercase__ :  Optional[Any]=None      ,   lowercase__ :  List[Any]=None      ,   lowercase__ :  str = "eval"):







        '''simple docstring'''



        lowerCAmelCase__					     = self.eval_dataset if eval_dataset is None else eval_dataset
        lowerCAmelCase__					     = self.get_eval_dataloader(lowercase__)
        lowerCAmelCase__					     = self.eval_examples if eval_examples is None else eval_examples

        # Temporarily disable metric computation, we will do it in the loop here.
        lowerCAmelCase__					     = self.compute_metrics
        lowerCAmelCase__					     = None
        lowerCAmelCase__					     = self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
        try:
               lowerCAmelCase__					     = eval_loop(
                   lowercase__      ,   description='Evaluation'      ,   prediction_loss_only=True if compute_metrics is None else None      ,   ignore_keys=lowercase__      ,   )
        finally:
               lowerCAmelCase__					     = compute_metrics

        if self.post_process_function is not None and self.compute_metrics is not None:
               lowerCAmelCase__					     = self.post_process_function(lowercase__      ,   lowercase__      ,   output.predictions)
               lowerCAmelCase__					     = self.compute_metrics(lowercase__)

               # Prefix all keys with metric_key_prefix + '_'
               for key in list(metrics.keys()):
                      if not key.startswith(F"""{metric_key_prefix}_"""):
                             lowerCAmelCase__					     = metrics.pop(lowercase__)

               self.log(lowercase__)
        else:
               lowerCAmelCase__					     = {}

        if self.args.tpu_metrics_debug or self.args.debug:
               # tpu-comment: Logging debug metrics for PyTorch/XLA (compile, execute times, ops, etc.)
               xm.master_print(met.metrics_report())

        lowerCAmelCase__					     = self.callback_handler.on_evaluate(self.args      ,   self.state      ,   self.control      ,   lowercase__)
        return metrics




 def 		__snake_case						(      self :  Optional[int]      ,   lowercase__ :  str      ,   lowercase__ :  Any      ,   lowercase__ :  List[str]=None      ,   lowercase__ :  str = "test"):







        '''simple docstring'''



        lowerCAmelCase__					     = self.get_test_dataloader(lowercase__)

        # Temporarily disable metric computation, we will do it in the loop here.
        lowerCAmelCase__					     = self.compute_metrics
        lowerCAmelCase__					     = None
        lowerCAmelCase__					     = self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
        try:
               lowerCAmelCase__					     = eval_loop(
                   lowercase__      ,   description='Prediction'      ,   prediction_loss_only=True if compute_metrics is None else None      ,   ignore_keys=lowercase__      ,   )
        finally:
               lowerCAmelCase__					     = compute_metrics

        if self.post_process_function is None or self.compute_metrics is None:
               return output

        lowerCAmelCase__					     = self.post_process_function(lowercase__      ,   lowercase__      ,   output.predictions      ,   'predict')
        lowerCAmelCase__					     = self.compute_metrics(lowercase__)

        # Prefix all keys with metric_key_prefix + '_'
        for key in list(metrics.keys()):
               if not key.startswith(F"""{metric_key_prefix}_"""):
                      lowerCAmelCase__					     = metrics.pop(lowercase__)

        return PredictionOutput(predictions=predictions.predictions      ,   label_ids=predictions.label_ids      ,   metrics=lowercase__)




 def 		__snake_case						(      self :  List[str]      ,   lowercase__ :  List[str]="./"):







        '''simple docstring'''



        lowerCAmelCase__					     = self.eval_dataset
        lowerCAmelCase__					     = self.get_eval_dataloader(lowercase__)

        lowerCAmelCase__					     = next(iter(lowercase__))

        # saving device - to make it consistent
        lowerCAmelCase__					     = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

        # convert to tuple
        lowerCAmelCase__					     = tuple(v.to(lowercase__) for k, v in batch.items())

        logger.info('Converting model to be onnx compatible')
        from pytorch_quantization.nn import TensorQuantizer

        lowerCAmelCase__					     = True

        lowerCAmelCase__					     = self.model.to(lowercase__)

        model.eval()
        model.float()

        lowerCAmelCase__					     = model.module if hasattr(lowercase__      ,   'module') else model
        quant_trainer.configure_model(lowercase__      ,   self.quant_trainer_args)

        lowerCAmelCase__					     = os.path.join(lowercase__      ,   'model.onnx')
        logger.info(F"""exporting model to {output_model_file}""")

        lowerCAmelCase__					     = {0: 'batch_size', 1: 'seq_len'}

        torch.onnx.export(
            lowercase__      ,   lowercase__      ,   lowercase__      ,   export_params=lowercase__      ,   opset_version=13      ,   do_constant_folding=lowercase__      ,   input_names=['input_ids', 'attention_mask', 'token_type_ids']      ,   output_names=['output_start_logits', 'output_end_logits']      ,   dynamic_axes={
                'input_ids': axes,
                'attention_mask': axes,
                'token_type_ids': axes,
                'output_start_logits': axes,
                'output_end_logits': axes,
            }      ,   verbose=lowercase__      ,   )
        logger.info('onnx export finished')
