





"""simple docstring"""
from typing import Optional, Union

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

from ...activations import ACTaFN
from ...modeling_outputs import BaseModelOutputWithPoolingAndNoAttention, 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_mobilenet_va import MobileNetVaConfig


lowerCamelCase_								=		logging.get_logger(__name__)


# General docstring
lowerCamelCase_								=		"MobileNetV1Config"

# Base docstring
lowerCamelCase_								=		"google/mobilenet_v1_1.0_224"
lowerCamelCase_								=		[1, 1_0_2_4, 7, 7]

# Image classification docstring
lowerCamelCase_								=		"google/mobilenet_v1_1.0_224"
lowerCamelCase_								=		"tabby, tabby cat"


lowerCamelCase_								=		[
    "google/mobilenet_v1_1.0_224",
    "google/mobilenet_v1_0.75_192",
    # See all MobileNetV1 models at https://huggingface.co/models?filter=mobilenet_v1
]



def      __lowerCamelCase   (							a_     :		str ,   a_     :		str ,   a_     :		List[Any]=None							)			->      Any:
      __SCREAMING_SNAKE_CASE   :Optional[Any]						=  {}

      if isinstance(a_ ,   a_							):
            __SCREAMING_SNAKE_CASE   :Optional[Any]						=  model.mobilenet_va
      else:
            __SCREAMING_SNAKE_CASE   :Dict						=  model

      __SCREAMING_SNAKE_CASE   :List[str]						=  '''MobilenetV1/Conv2d_0/'''
      __SCREAMING_SNAKE_CASE   :Dict						=  backbone.conv_stem.convolution.weight
      __SCREAMING_SNAKE_CASE   :Union[str, Any]						=  backbone.conv_stem.normalization.bias
      __SCREAMING_SNAKE_CASE   :Union[str, Any]						=  backbone.conv_stem.normalization.weight
      __SCREAMING_SNAKE_CASE   :str						=  backbone.conv_stem.normalization.running_mean
      __SCREAMING_SNAKE_CASE   :List[str]						=  backbone.conv_stem.normalization.running_var

      for i in range(13							):
            __SCREAMING_SNAKE_CASE   :Optional[int]						=  i + 1
            __SCREAMING_SNAKE_CASE   :Any						=  i * 2

            __SCREAMING_SNAKE_CASE   :List[Any]						=  backbone.layer[pt_index]
            __SCREAMING_SNAKE_CASE   :int						=  f'''MobilenetV1/Conv2d_{tf_index}_depthwise/'''
            __SCREAMING_SNAKE_CASE   :Dict						=  pointer.convolution.weight
            __SCREAMING_SNAKE_CASE   :List[str]						=  pointer.normalization.bias
            __SCREAMING_SNAKE_CASE   :Optional[Any]						=  pointer.normalization.weight
            __SCREAMING_SNAKE_CASE   :str						=  pointer.normalization.running_mean
            __SCREAMING_SNAKE_CASE   :Union[str, Any]						=  pointer.normalization.running_var

            __SCREAMING_SNAKE_CASE   :Dict						=  backbone.layer[pt_index + 1]
            __SCREAMING_SNAKE_CASE   :Tuple						=  f'''MobilenetV1/Conv2d_{tf_index}_pointwise/'''
            __SCREAMING_SNAKE_CASE   :str						=  pointer.convolution.weight
            __SCREAMING_SNAKE_CASE   :str						=  pointer.normalization.bias
            __SCREAMING_SNAKE_CASE   :List[str]						=  pointer.normalization.weight
            __SCREAMING_SNAKE_CASE   :Tuple						=  pointer.normalization.running_mean
            __SCREAMING_SNAKE_CASE   :int						=  pointer.normalization.running_var

      if isinstance(a_ ,   a_							):
            __SCREAMING_SNAKE_CASE   :str						=  '''MobilenetV1/Logits/Conv2d_1c_1x1/'''
            __SCREAMING_SNAKE_CASE   :List[str]						=  model.classifier.weight
            __SCREAMING_SNAKE_CASE   :Union[str, Any]						=  model.classifier.bias

      return tf_to_pt_map



def      __lowerCamelCase   (							a_     :		Union[str, Any] ,   a_     :		Union[str, Any] ,   a_     :		Tuple							)			->      Optional[int]:
      try:
            import numpy as np
            import tensorflow as tf
      except ImportError:
            logger.error(
                '''Loading a TensorFlow models in PyTorch, requires TensorFlow to be installed. Please see '''
                '''https://www.tensorflow.org/install/ for installation instructions.'''							)
            raise

      # Load weights from TF model
      __SCREAMING_SNAKE_CASE   :List[str]						=  tf.train.list_variables(a_							)
      __SCREAMING_SNAKE_CASE   :Any						=  {}
      for name, shape in init_vars:
            logger.info(f'''Loading TF weight {name} with shape {shape}'''							)
            __SCREAMING_SNAKE_CASE   :int						=  tf.train.load_variable(a_ ,   a_							)
            __SCREAMING_SNAKE_CASE   :Any						=  array

      # Build TF to PyTorch weights loading map
      __SCREAMING_SNAKE_CASE   :List[Any]						=  _build_tf_to_pytorch_map(a_ ,   a_ ,   a_							)

      for name, pointer in tf_to_pt_map.items():
            logger.info(f'''Importing {name}'''							)
            if name not in tf_weights:
                  logger.info(f'''{name} not in tf pre-trained weights, skipping'''							)
                  continue

            __SCREAMING_SNAKE_CASE   :Dict						=  tf_weights[name]

            if "depthwise_weights" in name:
                  logger.info('''Transposing depthwise'''							)
                  __SCREAMING_SNAKE_CASE   :List[Any]						=  np.transpose(a_ ,   (2, 3, 0, 1)							)
            elif "weights" in name:
                  logger.info('''Transposing'''							)
                  if len(pointer.shape							) == 2:  # copying into linear layer
                        __SCREAMING_SNAKE_CASE   :Tuple						=  array.squeeze().transpose()
                  else:
                        __SCREAMING_SNAKE_CASE   :str						=  np.transpose(a_ ,   (3, 2, 0, 1)							)

            if pointer.shape != array.shape:
                  raise ValueError(f'''Pointer shape {pointer.shape} and array shape {array.shape} mismatched'''							)

            logger.info(f'''Initialize PyTorch weight {name} {array.shape}'''							)
            __SCREAMING_SNAKE_CASE   :List[Any]						=  torch.from_numpy(a_							)

            tf_weights.pop(a_ ,   a_							)
            tf_weights.pop(name + '''/RMSProp''' ,   a_							)
            tf_weights.pop(name + '''/RMSProp_1''' ,   a_							)
            tf_weights.pop(name + '''/ExponentialMovingAverage''' ,   a_							)

      logger.info(f'''Weights not copied to PyTorch model: {", ".join(tf_weights.keys()							)}'''							)
      return model



def      __lowerCamelCase   (							a_     :		torch.Tensor ,   a_     :		nn.Convad							)			->      torch.Tensor:
      __SCREAMING_SNAKE_CASE				,__SCREAMING_SNAKE_CASE   :Optional[Any]						=  features.shape[-2:]
      __SCREAMING_SNAKE_CASE				,__SCREAMING_SNAKE_CASE   :Dict						=  conv_layer.stride
      __SCREAMING_SNAKE_CASE				,__SCREAMING_SNAKE_CASE   :List[Any]						=  conv_layer.kernel_size

      if in_height % stride_height == 0:
            __SCREAMING_SNAKE_CASE   :Tuple						=  max(kernel_height - stride_height ,   0							)
      else:
            __SCREAMING_SNAKE_CASE   :Dict						=  max(kernel_height - (in_height % stride_height) ,   0							)

      if in_width % stride_width == 0:
            __SCREAMING_SNAKE_CASE   :int						=  max(kernel_width - stride_width ,   0							)
      else:
            __SCREAMING_SNAKE_CASE   :List[Any]						=  max(kernel_width - (in_width % stride_width) ,   0							)

      __SCREAMING_SNAKE_CASE   :str						=  pad_along_width // 2
      __SCREAMING_SNAKE_CASE   :Union[str, Any]						=  pad_along_width - pad_left
      __SCREAMING_SNAKE_CASE   :str						=  pad_along_height // 2
      __SCREAMING_SNAKE_CASE   :Dict						=  pad_along_height - pad_top

      __SCREAMING_SNAKE_CASE   :Optional[Any]						=  (pad_left, pad_right, pad_top, pad_bottom)
      return nn.functional.pad(a_ ,   a_ ,   '''constant''' ,   0.0							)






class 					_SCREAMING_SNAKE_CASE(   nn.Module  ):




       def __init__(						self		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__ = 1		,SCREAMING_SNAKE_CASE__ = 1		,SCREAMING_SNAKE_CASE__ = False		,SCREAMING_SNAKE_CASE__ = True		,SCREAMING_SNAKE_CASE__ = True		,)			->      None:





             """simple docstring"""






             super().__init__()
             __SCREAMING_SNAKE_CASE   :str						=  config

             if in_channels % groups != 0:
                   raise ValueError(f'''Input channels ({in_channels}) are not divisible by {groups} groups.'''							)
             if out_channels % groups != 0:
                   raise ValueError(f'''Output channels ({out_channels}) are not divisible by {groups} groups.'''							)

             __SCREAMING_SNAKE_CASE   :List[Any]						=  0 if config.tf_padding else int((kernel_size - 1) / 2							)

             __SCREAMING_SNAKE_CASE   :Optional[int]						=  nn.Convad(
                 in_channels=SCREAMING_SNAKE_CASE__		,out_channels=SCREAMING_SNAKE_CASE__		,kernel_size=SCREAMING_SNAKE_CASE__		,stride=SCREAMING_SNAKE_CASE__		,padding=SCREAMING_SNAKE_CASE__		,groups=SCREAMING_SNAKE_CASE__		,bias=SCREAMING_SNAKE_CASE__		,padding_mode='''zeros'''		,)

             if use_normalization:
                   __SCREAMING_SNAKE_CASE   :int						=  nn.BatchNormad(
                       num_features=SCREAMING_SNAKE_CASE__		,eps=config.layer_norm_eps		,momentum=0.9_9_9_7		,affine=SCREAMING_SNAKE_CASE__		,track_running_stats=SCREAMING_SNAKE_CASE__		,)
             else:
                   __SCREAMING_SNAKE_CASE   :str						=  None

             if use_activation:
                   if isinstance(SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							):
                         __SCREAMING_SNAKE_CASE   :Any						=  ACTaFN[use_activation]
                   elif isinstance(config.hidden_act		,SCREAMING_SNAKE_CASE__							):
                         __SCREAMING_SNAKE_CASE   :int						=  ACTaFN[config.hidden_act]
                   else:
                         __SCREAMING_SNAKE_CASE   :List[Any]						=  config.hidden_act
             else:
                   __SCREAMING_SNAKE_CASE   :Any						=  None




       def     _UpperCamelCase     (						self		,SCREAMING_SNAKE_CASE__							)			->      torch.Tensor:





             """simple docstring"""






             if self.config.tf_padding:
                   __SCREAMING_SNAKE_CASE   :List[str]						=  apply_tf_padding(SCREAMING_SNAKE_CASE__		,self.convolution							)
             __SCREAMING_SNAKE_CASE   :List[str]						=  self.convolution(SCREAMING_SNAKE_CASE__							)
             if self.normalization is not None:
                   __SCREAMING_SNAKE_CASE   :List[str]						=  self.normalization(SCREAMING_SNAKE_CASE__							)
             if self.activation is not None:
                   __SCREAMING_SNAKE_CASE   :Optional[int]						=  self.activation(SCREAMING_SNAKE_CASE__							)
             return features






class 					_SCREAMING_SNAKE_CASE(   A  ):
       SCREAMING_SNAKE_CASE_   :      Dict							=  MobileNetVaConfig
       SCREAMING_SNAKE_CASE_   :      Optional[Any]							=  load_tf_weights_in_mobilenet_va
       SCREAMING_SNAKE_CASE_   :      Tuple							=  '''mobilenet_v1'''
       SCREAMING_SNAKE_CASE_   :      List[str]							=  '''pixel_values'''
       SCREAMING_SNAKE_CASE_   :      Optional[Any]							=  False




       def     _UpperCamelCase     (						self		,SCREAMING_SNAKE_CASE__							)			->      None:





             """simple docstring"""






             if isinstance(SCREAMING_SNAKE_CASE__		,(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(SCREAMING_SNAKE_CASE__		,nn.BatchNormad							):
                   module.bias.data.zero_()
                   module.weight.data.fill_(1.0							)




lowerCamelCase_								=		r"\n    This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. Use it\n    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 ([`MobileNetV1Config`]): 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"

lowerCamelCase_								=		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            [`MobileNetV1ImageProcessor.__call__`] for details.\n        output_hidden_states (`bool`, *optional*):\n            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for\n            more detail.\n        return_dict (`bool`, *optional*):\n            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.\n"






@add_start_docstrings(
    '''The bare MobileNetV1 model outputting raw hidden-states without any specific head on top.'''    ,	A    ,	)
class 					_SCREAMING_SNAKE_CASE(   A  ):




       def __init__(						self		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__ = True							)			->      Dict:





             """simple docstring"""






             super().__init__(SCREAMING_SNAKE_CASE__							)
             __SCREAMING_SNAKE_CASE   :List[Any]						=  config

             __SCREAMING_SNAKE_CASE   :Optional[Any]						=  32
             __SCREAMING_SNAKE_CASE   :List[str]						=  max(int(depth * config.depth_multiplier							)		,config.min_depth							)

             __SCREAMING_SNAKE_CASE   :int						=  MobileNetVaConvLayer(
                 SCREAMING_SNAKE_CASE__		,in_channels=config.num_channels		,out_channels=SCREAMING_SNAKE_CASE__		,kernel_size=3		,stride=2		,)

             __SCREAMING_SNAKE_CASE   :Any						=  [1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 2, 1]

             __SCREAMING_SNAKE_CASE   :Optional[Any]						=  nn.ModuleList()
             for i in range(13							):
                   __SCREAMING_SNAKE_CASE   :Tuple						=  out_channels

                   if strides[i] == 2 or i == 0:
                         depth *= 2
                         __SCREAMING_SNAKE_CASE   :List[str]						=  max(int(depth * config.depth_multiplier							)		,config.min_depth							)

                   self.layer.append(
                       MobileNetVaConvLayer(
                           SCREAMING_SNAKE_CASE__		,in_channels=SCREAMING_SNAKE_CASE__		,out_channels=SCREAMING_SNAKE_CASE__		,kernel_size=3		,stride=strides[i]		,groups=SCREAMING_SNAKE_CASE__		,)							)

                   self.layer.append(
                       MobileNetVaConvLayer(
                           SCREAMING_SNAKE_CASE__		,in_channels=SCREAMING_SNAKE_CASE__		,out_channels=SCREAMING_SNAKE_CASE__		,kernel_size=1		,)							)

             __SCREAMING_SNAKE_CASE   :Dict						=  nn.AdaptiveAvgPoolad((1, 1)							) if add_pooling_layer else None

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




       def     _UpperCamelCase     (						self		,SCREAMING_SNAKE_CASE__							)			->      List[str]:





             """simple docstring"""






             raise NotImplementedError




       @add_start_docstrings_to_model_forward(SCREAMING_SNAKE_CASE__							)
       @add_code_sample_docstrings(
           checkpoint=_CHECKPOINT_FOR_DOC		,output_type=SCREAMING_SNAKE_CASE__		,config_class=_CONFIG_FOR_DOC		,modality='''vision'''		,expected_output=_EXPECTED_OUTPUT_SHAPE		,)
       def     _UpperCamelCase     (						self		,SCREAMING_SNAKE_CASE__ = None		,SCREAMING_SNAKE_CASE__ = None		,SCREAMING_SNAKE_CASE__ = None		,)			->      Union[tuple, BaseModelOutputWithPoolingAndNoAttention]:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE   :Any						=  (
                 output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
             )
             __SCREAMING_SNAKE_CASE   :Any						=  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'''							)

             __SCREAMING_SNAKE_CASE   :Tuple						=  self.conv_stem(SCREAMING_SNAKE_CASE__							)

             __SCREAMING_SNAKE_CASE   :str						=  () if output_hidden_states else None

             for i, layer_module in enumerate(self.layer							):
                   __SCREAMING_SNAKE_CASE   :Tuple						=  layer_module(SCREAMING_SNAKE_CASE__							)

                   if output_hidden_states:
                         __SCREAMING_SNAKE_CASE   :List[Any]						=  all_hidden_states + (hidden_states,)

             __SCREAMING_SNAKE_CASE   :Tuple						=  hidden_states

             if self.pooler is not None:
                   __SCREAMING_SNAKE_CASE   :str						=  torch.flatten(self.pooler(SCREAMING_SNAKE_CASE__							)		,start_dim=1							)
             else:
                   __SCREAMING_SNAKE_CASE   :Dict						=  None

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

             return BaseModelOutputWithPoolingAndNoAttention(
                 last_hidden_state=SCREAMING_SNAKE_CASE__		,pooler_output=SCREAMING_SNAKE_CASE__		,hidden_states=SCREAMING_SNAKE_CASE__		,)






@add_start_docstrings(
    '''
    MobileNetV1 model with an image classification head on top (a linear layer on top of the pooled features), e.g. for
    ImageNet.
    '''    ,	A    ,	)
class 					_SCREAMING_SNAKE_CASE(   A  ):




       def __init__(						self		,SCREAMING_SNAKE_CASE__							)			->      None:





             """simple docstring"""






             super().__init__(SCREAMING_SNAKE_CASE__							)

             __SCREAMING_SNAKE_CASE   :Optional[Any]						=  config.num_labels
             __SCREAMING_SNAKE_CASE   :Any						=  MobileNetVaModel(SCREAMING_SNAKE_CASE__							)

             __SCREAMING_SNAKE_CASE   :int						=  self.mobilenet_va.layer[-1].convolution.out_channels

             # Classifier head
             __SCREAMING_SNAKE_CASE   :Any						=  nn.Dropout(config.classifier_dropout_prob		,inplace=SCREAMING_SNAKE_CASE__							)
             __SCREAMING_SNAKE_CASE   :str						=  nn.Linear(SCREAMING_SNAKE_CASE__		,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(SCREAMING_SNAKE_CASE__							)
       @add_code_sample_docstrings(
           checkpoint=_IMAGE_CLASS_CHECKPOINT		,output_type=SCREAMING_SNAKE_CASE__		,config_class=_CONFIG_FOR_DOC		,expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT		,)
       def     _UpperCamelCase     (						self		,SCREAMING_SNAKE_CASE__ = None		,SCREAMING_SNAKE_CASE__ = None		,SCREAMING_SNAKE_CASE__ = None		,SCREAMING_SNAKE_CASE__ = None		,)			->      Union[tuple, ImageClassifierOutputWithNoAttention]:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE   :List[Any]						=  return_dict if return_dict is not None else self.config.use_return_dict

             __SCREAMING_SNAKE_CASE   :Tuple						=  self.mobilenet_va(SCREAMING_SNAKE_CASE__		,output_hidden_states=SCREAMING_SNAKE_CASE__		,return_dict=SCREAMING_SNAKE_CASE__							)

             __SCREAMING_SNAKE_CASE   :Tuple						=  outputs.pooler_output if return_dict else outputs[1]

             __SCREAMING_SNAKE_CASE   :str						=  self.classifier(self.dropout(SCREAMING_SNAKE_CASE__							)							)

             __SCREAMING_SNAKE_CASE   :Any						=  None
             if labels is not None:
                   if self.config.problem_type is None:
                         if self.num_labels == 1:
                               __SCREAMING_SNAKE_CASE   :Any						=  '''regression'''
                         elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
                               __SCREAMING_SNAKE_CASE   :List[Any]						=  '''single_label_classification'''
                         else:
                               __SCREAMING_SNAKE_CASE   :Tuple						=  '''multi_label_classification'''

                   if self.config.problem_type == "regression":
                         __SCREAMING_SNAKE_CASE   :Union[str, Any]						=  MSELoss()
                         if self.num_labels == 1:
                               __SCREAMING_SNAKE_CASE   :Optional[int]						=  loss_fct(logits.squeeze()		,labels.squeeze()							)
                         else:
                               __SCREAMING_SNAKE_CASE   :str						=  loss_fct(SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)
                   elif self.config.problem_type == "single_label_classification":
                         __SCREAMING_SNAKE_CASE   :Union[str, Any]						=  CrossEntropyLoss()
                         __SCREAMING_SNAKE_CASE   :Dict						=  loss_fct(logits.view(-1		,self.num_labels							)		,labels.view(-1							)							)
                   elif self.config.problem_type == "multi_label_classification":
                         __SCREAMING_SNAKE_CASE   :Optional[int]						=  BCEWithLogitsLoss()
                         __SCREAMING_SNAKE_CASE   :List[str]						=  loss_fct(SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)

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

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






"""simple docstring"""
import argparse
import os

import torch

from transformers import FlavaImageCodebook, FlavaImageCodebookConfig



def      __lowerCamelCase   (							a_     :		str ,   a_     :		Dict ,   a_     :		Any ,   a_     :		str							)			->      str:
      __SCREAMING_SNAKE_CASE   :int						=  s.rsplit(a_ ,   a_							)
      return new.join(a_							)



def      __lowerCamelCase   (							a_     :		List[str]							)			->      Dict:
      # encoder.embeddings are double copied in original FLAVA
      return sum(param.float().sum() if '''encoder.embeddings''' not in key else 0 for key, param in state_dict.items()							)



def      __lowerCamelCase   (							a_     :		Optional[int]							)			->      Any:
      __SCREAMING_SNAKE_CASE   :Optional[int]						=  {}

      __SCREAMING_SNAKE_CASE   :Union[str, Any]						=  ['''group_1''', '''group_2''', '''group_3''', '''group_4''']
      for key, value in state_dict.items():
            for group_key in group_keys:
                  if group_key in key:
                        __SCREAMING_SNAKE_CASE   :Optional[Any]						=  key.replace(f'''{group_key}.''' ,   f'''{group_key}.group.'''							)

            if "res_path" in key:
                  __SCREAMING_SNAKE_CASE   :str						=  key.replace('''res_path.''' ,   '''res_path.path.'''							)

            if key.endswith('''.w'''							):
                  __SCREAMING_SNAKE_CASE   :List[Any]						=  rreplace(a_ ,   '''.w''' ,   '''.weight''' ,   1							)
            if key.endswith('''.b'''							):
                  __SCREAMING_SNAKE_CASE   :List[Any]						=  rreplace(a_ ,   '''.b''' ,   '''.bias''' ,   1							)

            __SCREAMING_SNAKE_CASE   :Optional[Any]						=  value.float()

      return upgrade



@torch.no_grad()
def      __lowerCamelCase   (							a_     :		List[Any] ,   a_     :		Optional[int] ,   a_     :		Optional[int]=None ,   a_     :		Dict=True							)			->      Union[str, Any]:
      from dall_e import Encoder

      __SCREAMING_SNAKE_CASE   :int						=  Encoder()
      if os.path.exists(a_							):
            __SCREAMING_SNAKE_CASE   :Dict						=  torch.load(a_							)
      else:
            __SCREAMING_SNAKE_CASE   :List[str]						=  torch.hub.load_state_dict_from_url(a_							)

      if isinstance(a_ ,   a_							):
            __SCREAMING_SNAKE_CASE   :List[str]						=  ckpt.state_dict()
      encoder.load_state_dict(a_							)

      if config_path is not None:
            __SCREAMING_SNAKE_CASE   :Any						=  FlavaImageCodebookConfig.from_pretrained(a_							)
      else:
            __SCREAMING_SNAKE_CASE   :Optional[int]						=  FlavaImageCodebookConfig()

      __SCREAMING_SNAKE_CASE   :Tuple						=  FlavaImageCodebook(a_							).eval()
      __SCREAMING_SNAKE_CASE   :List[str]						=  encoder.state_dict()

      __SCREAMING_SNAKE_CASE   :Union[str, Any]						=  upgrade_state_dict(a_							)
      hf_model.load_state_dict(a_							)
      __SCREAMING_SNAKE_CASE   :Union[str, Any]						=  hf_model.state_dict()
      __SCREAMING_SNAKE_CASE   :Union[str, Any]						=  count_parameters(a_							)
      __SCREAMING_SNAKE_CASE   :Any						=  count_parameters(a_							)

      assert torch.allclose(a_ ,   a_ ,   atol=1e-3							)

      if save_checkpoint:
            hf_model.save_pretrained(a_							)
      else:
            return hf_state_dict


if __name__ == "__main__":
  lowerCamelCase_								=		argparse.ArgumentParser()
  parser.add_argument("--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model.")
  parser.add_argument("--checkpoint_path", default=None, type=str, help="Path to flava checkpoint")
  parser.add_argument("--config_path", default=None, type=str, help="Path to hf config.json of model to convert")
  lowerCamelCase_								=		parser.parse_args()

  convert_dalle_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path)