

'''simple docstring'''




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


_a					:			List[Any]							=    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 							lowercase_						(		a			):

  '''simple docstring'''





  def __init__(  self ,			*a_ ,			a_=None ,			a_=None ,			a_=None ,			**a_  )				->					str:


    """simple docstring"""
    super().__init__(*a_ ,			**a_  )
    UpperCAmelCase    				=						eval_examples
    UpperCAmelCase    				=						post_process_function
    UpperCAmelCase    				=						quant_trainer_args
    UpperCAmelCase    				=						1_2_8  # default number of calibration samples
  def     snake_case_      (  self ,			a_=None  )				->					Union[str, Any]:


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

    UpperCAmelCase    				=						self._remove_unused_columns(a_ ,			description='Calibration'  )

    return DataLoader(
        a_ ,			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=a_ ,			)
  def     snake_case_      (  self ,			a_=None  )				->					Union[str, Any]:


    """simple docstring"""
    UpperCAmelCase    				=						self.train_dataset if calib_dataset is None else calib_dataset
    UpperCAmelCase    				=						self.get_calib_dataloader(a_  )

    UpperCAmelCase    				=						self.model
    quant_trainer.configure_model(a_ ,			self.quant_trainer_args ,			calib=a_  )
    model.eval()
    quant_trainer.enable_calibration(a_  )

    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(a_  ):
      # Prediction step
      UpperCAmelCase				,   UpperCAmelCase				,   UpperCAmelCase    				=						self.prediction_step(a_ ,			a_ ,			prediction_loss_only=a_  )
      if (step + 1) * calib_dataloader.batch_size >= self.calib_num:
        break

    quant_trainer.finish_calibration(a_ ,			self.quant_trainer_args  )
    UpperCAmelCase    				=						model
  def     snake_case_      (  self ,			a_=None ,			a_=None ,			a_=None ,			a_ = "eval"  )				->					Optional[Any]:


    """simple docstring"""
    UpperCAmelCase    				=						self.eval_dataset if eval_dataset is None else eval_dataset
    UpperCAmelCase    				=						self.get_eval_dataloader(a_  )
    UpperCAmelCase    				=						self.eval_examples if eval_examples is None else eval_examples

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

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

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

      self.log(a_  )
    else:
      UpperCAmelCase    				=						{}

    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()  )

    UpperCAmelCase    				=						self.callback_handler.on_evaluate(self.args ,			self.state ,			self.control ,			a_  )
    return metrics
  def     snake_case_      (  self ,			a_ ,			a_ ,			a_=None ,			a_ = "test"  )				->					Optional[Any]:


    """simple docstring"""
    UpperCAmelCase    				=						self.get_test_dataloader(a_  )

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

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

    UpperCAmelCase    				=						self.post_process_function(a_ ,			a_ ,			output.predictions ,			'predict'  )
    UpperCAmelCase    				=						self.compute_metrics(a_  )

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

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





  def     snake_case_      (  self ,			a_="./"  )				->					Any:


    """simple docstring"""
    UpperCAmelCase    				=						self.eval_dataset
    UpperCAmelCase    				=						self.get_eval_dataloader(a_  )

    UpperCAmelCase    				=						next(iter(a_  )  )

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

    # convert to tuple
    UpperCAmelCase    				=						tuple(v.to(a_  ) for k, v in batch.items()  )

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

    UpperCAmelCase    				=						True

    UpperCAmelCase    				=						self.model.to(a_  )

    model.eval()
    model.float()

    UpperCAmelCase    				=						model.module if hasattr(a_ ,			'module'  ) else model
    quant_trainer.configure_model(a_ ,			self.quant_trainer_args  )

    UpperCAmelCase    				=						os.path.join(a_ ,			'model.onnx'  )
    logger.info(F'''exporting model to {output_model_file}'''  )

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

    torch.onnx.export(
        a_ ,			a_ ,			a_ ,			export_params=a_ ,			opset_version=1_3 ,			do_constant_folding=a_ ,			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=a_ ,			)
    logger.info('onnx export finished'  )





'''simple docstring'''




import argparse
import json
from dataclasses import dataclass, field
from functools import partial
from pathlib import Path
from typing import List

import timm
import torch
import torch.nn as nn
from huggingface_hub import hf_hub_download
from torch import Tensor

from transformers import AutoImageProcessor, ResNetConfig, ResNetForImageClassification
from transformers.utils import logging


logging.set_verbosity_info()
_a					:			List[Any]							=    logging.get_logger()



@dataclass
class 							lowercase_						:

  '''simple docstring'''





  __lowerCAmelCase       :			nn.Module
  __lowerCAmelCase       :			List[nn.Module]     	= field(default_factory=a			)
  __lowerCAmelCase       :			list     	= field(default_factory=a			)
  def     snake_case_      (  self ,			a_ ,			a_ ,			a_  )				->					Tuple:


    """simple docstring"""
    UpperCAmelCase    				=						len(list(m.modules()  )  ) == 1 or isinstance(a_ ,			nn.Convad  ) or isinstance(a_ ,			nn.BatchNormad  )
    if has_not_submodules:
      self.traced.append(a_  )
  def __call__(  self ,			a_  )				->					Union[str, Any]:


    """simple docstring"""
    for m in self.module.modules():
      self.handles.append(m.register_forward_hook(self._forward_hook  )  )
    self.module(a_  )
    [x.remove() for x in self.handles]
    return self





  @property
  def     snake_case_      (  self  )				->					int:


    """simple docstring"""
    # check the len of the state_dict keys to see if we have learnable params
    return list(filter(lambda a_  : len(list(x.state_dict().keys()  )  ) > 0 ,			self.traced  )  )




@dataclass
class 							lowercase_						:

  '''simple docstring'''





  __lowerCAmelCase       :			nn.Module
  __lowerCAmelCase       :			nn.Module
  __lowerCAmelCase       :			int     	= 0
  __lowerCAmelCase       :			List     	= field(default_factory=a			)
  __lowerCAmelCase       :			List     	= field(default_factory=a			)
  def __call__(  self ,			a_  )				->					Dict:


    """simple docstring"""
    UpperCAmelCase    				=						Tracker(self.dest  )(a_  ).parametrized
    UpperCAmelCase    				=						Tracker(self.src  )(a_  ).parametrized

    UpperCAmelCase    				=						list(filter(lambda a_  : type(a_  ) not in self.src_skip ,			a_  )  )
    UpperCAmelCase    				=						list(filter(lambda a_  : type(a_  ) not in self.dest_skip ,			a_  )  )

    if len(a_  ) != len(a_  ):
      raise Exception(
          F'''Numbers of operations are different. Source module has {len(a_  )} operations while'''
          F''' destination module has {len(a_  )}.'''  )

    for dest_m, src_m in zip(a_ ,			a_  ):
      dest_m.load_state_dict(src_m.state_dict()  )
      if self.verbose == 1:
        print(F'''Transfered from={src_m} to={dest_m}'''  )




def    lowerCamelCase__		(    SCREAMING_SNAKE_CASE   : str							, SCREAMING_SNAKE_CASE   : ResNetConfig							, SCREAMING_SNAKE_CASE   : Path							, SCREAMING_SNAKE_CASE   : bool = True    ):
  print(f'''Converting {name}...'''    )
  with torch.no_grad():
    UpperCAmelCase    				=						timm.create_model(SCREAMING_SNAKE_CASE							, pretrained=SCREAMING_SNAKE_CASE    ).eval()
    UpperCAmelCase    				=						ResNetForImageClassification(SCREAMING_SNAKE_CASE    ).eval()
    UpperCAmelCase    				=						ModuleTransfer(src=SCREAMING_SNAKE_CASE							, dest=SCREAMING_SNAKE_CASE    )
    UpperCAmelCase    				=						torch.randn((1, 3, 224, 224)    )
    module_transfer(SCREAMING_SNAKE_CASE    )

  assert torch.allclose(from_model(SCREAMING_SNAKE_CASE    )							, our_model(SCREAMING_SNAKE_CASE    ).logits    ), "The model logits don't match the original one."

  UpperCAmelCase    				=						f'''resnet{'-'.join(name.split('resnet'    )    )}'''
  print(SCREAMING_SNAKE_CASE    )

  if push_to_hub:
    our_model.push_to_hub(
        repo_path_or_name=save_directory / checkpoint_name							, commit_message='Add model'							, use_temp_dir=SCREAMING_SNAKE_CASE							, )

    # we can use the convnext one
    UpperCAmelCase    				=						AutoImageProcessor.from_pretrained('facebook/convnext-base-224-22k-1k'    )
    image_processor.push_to_hub(
        repo_path_or_name=save_directory / checkpoint_name							, commit_message='Add image processor'							, use_temp_dir=SCREAMING_SNAKE_CASE							, )

    print(f'''Pushed {checkpoint_name}'''    )


def    lowerCamelCase__		(    SCREAMING_SNAKE_CASE   : Path							, SCREAMING_SNAKE_CASE   : str = None							, SCREAMING_SNAKE_CASE   : bool = True    ):
  UpperCAmelCase    				=						'imagenet-1k-id2label.json'
  UpperCAmelCase    				=						1000
  UpperCAmelCase    				=						(1, num_labels)

  UpperCAmelCase    				=						'huggingface/label-files'
  UpperCAmelCase    				=						num_labels
  UpperCAmelCase    				=						json.load(open(hf_hub_download(SCREAMING_SNAKE_CASE							, SCREAMING_SNAKE_CASE							, repo_type='dataset'    )							, 'r'    )    )
  UpperCAmelCase    				=						{int(SCREAMING_SNAKE_CASE    ): v for k, v in idalabel.items()}

  UpperCAmelCase    				=						idalabel
  UpperCAmelCase    				=						{v: k for k, v in idalabel.items()}

  UpperCAmelCase    				=						partial(SCREAMING_SNAKE_CASE							, num_labels=SCREAMING_SNAKE_CASE							, idalabel=SCREAMING_SNAKE_CASE							, labelaid=SCREAMING_SNAKE_CASE    )

  UpperCAmelCase    				=						{
      'resnet18': ImageNetPreTrainedConfig(
          depths=[2, 2, 2, 2]							, hidden_sizes=[64, 128, 256, 512]							, layer_type='basic'    ),
      'resnet26': ImageNetPreTrainedConfig(
          depths=[2, 2, 2, 2]							, hidden_sizes=[256, 512, 1024, 2048]							, layer_type='bottleneck'    ),
      'resnet34': ImageNetPreTrainedConfig(
          depths=[3, 4, 6, 3]							, hidden_sizes=[64, 128, 256, 512]							, layer_type='basic'    ),
      'resnet50': ImageNetPreTrainedConfig(
          depths=[3, 4, 6, 3]							, hidden_sizes=[256, 512, 1024, 2048]							, layer_type='bottleneck'    ),
      'resnet101': ImageNetPreTrainedConfig(
          depths=[3, 4, 23, 3]							, hidden_sizes=[256, 512, 1024, 2048]							, layer_type='bottleneck'    ),
      'resnet152': ImageNetPreTrainedConfig(
          depths=[3, 8, 36, 3]							, hidden_sizes=[256, 512, 1024, 2048]							, layer_type='bottleneck'    ),
  }

  if model_name:
    convert_weight_and_push(SCREAMING_SNAKE_CASE							, names_to_config[model_name]							, SCREAMING_SNAKE_CASE							, SCREAMING_SNAKE_CASE    )
  else:
    for model_name, config in names_to_config.items():
      convert_weight_and_push(SCREAMING_SNAKE_CASE							, SCREAMING_SNAKE_CASE							, SCREAMING_SNAKE_CASE							, SCREAMING_SNAKE_CASE    )
  return config, expected_shape


if __name__ == "__main__":
  _a					:			List[str]							=    argparse.ArgumentParser()
  # Required parameters
  parser.add_argument(
      '--model_name',
      default=None,
      type=str,
      help=(
          'The name of the model you wish to convert, it must be one of the supported resnet* architecture,'
          ' currently: resnet18,26,34,50,101,152. If `None`, all of them will the converted.'
      ),
  )
  parser.add_argument(
      '--pytorch_dump_folder_path',
      default=None,
      type=Path,
      required=True,
      help='Path to the output PyTorch model directory.',
  )
  parser.add_argument(
      '--push_to_hub',
      default=True,
      type=bool,
      required=False,
      help='If True, push model and image processor to the hub.',
  )

  _a					:			Tuple							=    parser.parse_args()
  _a					:			Path							=    args.pytorch_dump_folder_path
  pytorch_dump_folder_path.mkdir(exist_ok=True, parents=True)
  convert_weights_and_push(pytorch_dump_folder_path, args.model_name, args.push_to_hub)


