
'''simple docstring'''






import argparse
import json

import requests
import torch
from huggingface_hub import hf_hub_download
from PIL import Image
from torchvision import transforms

from transformers import BitImageProcessor, FocalNetConfig, FocalNetForImageClassification
from transformers.image_utils import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD, PILImageResampling







def 		A_       (	_lowerCAmelCase   :    int							):



      """simple docstring"""




      _lowerCamelCase				:   Any								=   [2, 2, 6, 2] if '''tiny''' in model_name else [2, 2, 18, 2]
      _lowerCamelCase				:   int								=   True if '''large''' in model_name or '''huge''' in model_name else False
      _lowerCamelCase				:   Dict								=   True if '''large''' in model_name or '''huge''' in model_name else False
      _lowerCamelCase				:   List[str]								=   True if '''large''' in model_name or '''huge''' in model_name else False

      if "large" in model_name or "xlarge" in model_name or "huge" in model_name:
            if "fl3" in model_name:
                  _lowerCamelCase				:   int								=   [3, 3, 3, 3]
                  _lowerCamelCase				:   str								=   [5, 5, 5, 5]
            elif "fl4" in model_name:
                  _lowerCamelCase				:   List[str]								=   [4, 4, 4, 4]
                  _lowerCamelCase				:   Union[str, Any]								=   [3, 3, 3, 3]

      if "tiny" in model_name or "small" in model_name or "base" in model_name:
            _lowerCamelCase				:   Any								=   [3, 3, 3, 3]
            if "lrf" in model_name:
                  _lowerCamelCase				:   Optional[Any]								=   [3, 3, 3, 3]
            else:
                  _lowerCamelCase				:   Any								=   [2, 2, 2, 2]

      if "tiny" in model_name:
            _lowerCamelCase				:   Dict								=   96
      elif "small" in model_name:
            _lowerCamelCase				:   Any								=   96
      elif "base" in model_name:
            _lowerCamelCase				:   Union[str, Any]								=   128
      elif "large" in model_name:
            _lowerCamelCase				:   List[Any]								=   192
      elif "xlarge" in model_name:
            _lowerCamelCase				:   List[Any]								=   256
      elif "huge" in model_name:
            _lowerCamelCase				:   Optional[Any]								=   352

      # set label information
      _lowerCamelCase				:   Dict								=   '''huggingface/label-files'''
      if "large" in model_name or "huge" in model_name:
            _lowerCamelCase				:   Optional[Any]								=   '''imagenet-22k-id2label.json'''
      else:
            _lowerCamelCase				:   Union[str, Any]								=   '''imagenet-1k-id2label.json'''

      _lowerCamelCase				:   Tuple								=   json.load(open(hf_hub_download(a_				,	a_				,	repo_type="dataset"							)				,	"r"							)							)
      _lowerCamelCase				:   Dict								=   {int(a_							): v for k, v in idalabel.items()}
      _lowerCamelCase				:   Optional[Any]								=   {v: k for k, v in idalabel.items()}

      _lowerCamelCase				:   Any								=   FocalNetConfig(
          embed_dim=a_				,	depths=a_				,	focal_levels=a_				,	focal_windows=a_				,	use_conv_embed=a_				,	idalabel=a_				,	labelaid=a_				,	use_post_layernorm=a_				,	use_layerscale=a_				,	)

      return config







def 		A_       (	_lowerCAmelCase   :    List[Any]							):



      """simple docstring"""




      if "patch_embed.proj" in name:
            _lowerCamelCase				:   Dict								=   name.replace("patch_embed.proj"				,	"embeddings.patch_embeddings.projection"							)
      if "patch_embed.norm" in name:
            _lowerCamelCase				:   Dict								=   name.replace("patch_embed.norm"				,	"embeddings.norm"							)
      if "layers" in name:
            _lowerCamelCase				:   Any								=   '''encoder.''' + name
      if "encoder.layers" in name:
            _lowerCamelCase				:   Any								=   name.replace("encoder.layers"				,	"encoder.stages"							)
      if "downsample.proj" in name:
            _lowerCamelCase				:   Optional[int]								=   name.replace("downsample.proj"				,	"downsample.projection"							)
      if "blocks" in name:
            _lowerCamelCase				:   int								=   name.replace("blocks"				,	"layers"							)
      if "modulation.f.weight" in name or "modulation.f.bias" in name:
            _lowerCamelCase				:   int								=   name.replace("modulation.f"				,	"modulation.projection_in"							)
      if "modulation.h.weight" in name or "modulation.h.bias" in name:
            _lowerCamelCase				:   Union[str, Any]								=   name.replace("modulation.h"				,	"modulation.projection_context"							)
      if "modulation.proj.weight" in name or "modulation.proj.bias" in name:
            _lowerCamelCase				:   Tuple								=   name.replace("modulation.proj"				,	"modulation.projection_out"							)

      if name == "norm.weight":
            _lowerCamelCase				:   Tuple								=   '''layernorm.weight'''
      if name == "norm.bias":
            _lowerCamelCase				:   List[Any]								=   '''layernorm.bias'''

      if "head" in name:
            _lowerCamelCase				:   List[Any]								=   name.replace("head"				,	"classifier"							)
      else:
            _lowerCamelCase				:   Tuple								=   '''focalnet.''' + name

      return name







def 		A_       (	_lowerCAmelCase   :    Any				,	_lowerCAmelCase   :    Optional[int]				,	_lowerCAmelCase   :    Union[str, Any]=False							):



      """simple docstring"""




      _lowerCamelCase				:   Optional[Any]								=   {
          '''focalnet-tiny''': '''https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_tiny_srf.pth''',
          '''focalnet-tiny-lrf''': '''https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_tiny_lrf.pth''',
          '''focalnet-small''': '''https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_small_srf.pth''',
          '''focalnet-small-lrf''': '''https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_small_lrf.pth''',
          '''focalnet-base''': '''https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_base_srf.pth''',
          '''focalnet-base-lrf''': '''https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_base_lrf.pth''',
          '''focalnet-large-lrf-fl3''': '''https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_large_lrf_384.pth''',
          '''focalnet-large-lrf-fl4''': '''https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_large_lrf_384_fl4.pth''',
          '''focalnet-xlarge-lrf-fl3''': '''https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_xlarge_lrf_384.pth''',
          '''focalnet-xlarge-lrf-fl4''': '''https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_xlarge_lrf_384_fl4.pth''',
      }
      # fmt: on

      _lowerCamelCase				:   Optional[int]								=   model_name_to_url[model_name]
      print("Checkpoint URL: "				,	a_							)
      _lowerCamelCase				:   List[Any]								=   torch.hub.load_state_dict_from_url(a_				,	map_location="cpu"							)['''model''']

      # rename keys
      for key in state_dict.copy().keys():
            _lowerCamelCase				:   Union[str, Any]								=   state_dict.pop(a_							)
            _lowerCamelCase				:   Union[str, Any]								=   val

      _lowerCamelCase				:   Optional[int]								=   get_focalnet_config(a_							)
      _lowerCamelCase				:   Union[str, Any]								=   FocalNetForImageClassification(a_							)
      model.eval()

      # load state dict
      model.load_state_dict(a_							)

      # verify conversion
      _lowerCamelCase				:   Dict								=   '''http://images.cocodataset.org/val2017/000000039769.jpg'''

      _lowerCamelCase				:   Tuple								=   BitImageProcessor(
          do_resize=a_				,	size={"shortest_edge": 256}				,	resample=PILImageResampling.BILINEAR				,	do_center_crop=a_				,	crop_size=224				,	do_normalize=a_				,	image_mean=a_				,	image_std=a_				,	)
      _lowerCamelCase				:   Dict								=   Image.open(requests.get(a_				,	stream=a_							).raw							)
      _lowerCamelCase				:   Union[str, Any]								=   processor(images=a_				,	return_tensors="pt"							)

      _lowerCamelCase				:   Dict								=   transforms.Compose(
          [
              transforms.Resize(256							),
              transforms.CenterCrop(224							),
              transforms.ToTensor(),
              transforms.Normalize(mean=[0.4_8_5, 0.4_5_6, 0.4_0_6]				,	std=[0.2_2_9, 0.2_2_4, 0.2_2_5]							),
          ]							)

      _lowerCamelCase				:   List[str]								=   image_transforms(a_							).unsqueeze(0							)

      # verify pixel_values
      assert torch.allclose(inputs.pixel_values				,	a_				,	atol=1E-4							)

      _lowerCamelCase				:   Optional[Any]								=   model(**a_							)

      _lowerCamelCase				:   int								=   outputs.logits.argmax(-1							).item()
      print("Predicted class:"				,	model.config.idalabel[predicted_class_idx]							)

      print("First values of logits:"				,	outputs.logits[0, :3]							)

      if model_name == "focalnet-tiny":
            _lowerCamelCase				:   Optional[Any]								=   torch.tensor([0.2_1_6_6, -0.4_3_6_8, 0.2_1_9_1]							)
      elif model_name == "focalnet-tiny-lrf":
            _lowerCamelCase				:   Dict								=   torch.tensor([1.1_6_6_9, 0.0_1_2_5, -0.1_6_9_5]							)
      elif model_name == "focalnet-small":
            _lowerCamelCase				:   Any								=   torch.tensor([0.4_9_1_7, -0.0_4_3_0, 0.1_3_4_1]							)
      elif model_name == "focalnet-small-lrf":
            _lowerCamelCase				:   List[str]								=   torch.tensor([-0.2_5_8_8, -0.5_3_4_2, -0.2_3_3_1]							)
      elif model_name == "focalnet-base":
            _lowerCamelCase				:   Any								=   torch.tensor([-0.1_6_5_5, -0.4_0_9_0, -0.1_7_3_0]							)
      elif model_name == "focalnet-base-lrf":
            _lowerCamelCase				:   Dict								=   torch.tensor([0.5_3_0_6, -0.0_4_8_3, -0.3_9_2_8]							)
      assert torch.allclose(outputs.logits[0, :3]				,	a_				,	atol=1E-4							)
      print("Looks ok!"							)

      if pytorch_dump_folder_path is not None:
            print(F'Saving model and processor of {model_name} to {pytorch_dump_folder_path}'							)
            model.save_pretrained(a_							)
            processor.save_pretrained(a_							)

      if push_to_hub:
            print(F'Pushing model and processor of {model_name} to the hub...'							)
            model.push_to_hub(F'{model_name}'							)
            processor.push_to_hub(F'{model_name}'							)


if __name__ == "__main__":
 UpperCAmelCase_       :   str   =       argparse.ArgumentParser()
 # Required parameters
 parser.add_argument(
     '--model_name',
     default='focalnet-tiny',
     type=str,
     help='Name of the FocalNet model you\'d like to convert.',
 )
 parser.add_argument(
     '--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model directory.'
 )
 parser.add_argument(
     '--push_to_hub',
     action='store_true',
     help='Whether to push the model and processor to the hub.',
 )

 UpperCAmelCase_       :   List[str]   =       parser.parse_args()
 convert_focalnet_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)

'''simple docstring'''






import json
from typing import TYPE_CHECKING, List, Optional, Tuple

from tokenizers import pre_tokenizers, processors

from ...tokenization_utils_base import AddedToken, BatchEncoding
from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_blenderbot import BlenderbotTokenizer


if TYPE_CHECKING:
 from transformers.pipelines.conversational import Conversation

UpperCAmelCase_       :   Any   =       logging.get_logger(__name__)


UpperCAmelCase_       :   Any   =       {
    'vocab_file': 'vocab.json',
    'merges_file': 'merges.txt',
    'tokenizer_config_file': 'tokenizer_config.json',
}

UpperCAmelCase_       :   List[str]   =       {
    'vocab_file': {'facebook/blenderbot-3B': 'https://huggingface.co/facebook/blenderbot-3B/resolve/main/vocab.json'},
    'merges_file': {'facebook/blenderbot-3B': 'https://huggingface.co/facebook/blenderbot-3B/resolve/main/merges.txt'},
    'tokenizer_config_file': {
        'facebook/blenderbot-3B': 'https://huggingface.co/facebook/blenderbot-3B/resolve/main/tokenizer_config.json'
    },
}

UpperCAmelCase_       :   List[Any]   =       {'facebook/blenderbot-3B': 128}

class  UpperCAmelCase__						(       A ):
    lowerCAmelCase_				    =		VOCAB_FILES_NAMES
    lowerCAmelCase_				    =		PRETRAINED_VOCAB_FILES_MAP
    lowerCAmelCase_				    =		PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
    lowerCAmelCase_				    =		['input_ids', 'attention_mask']
    lowerCAmelCase_				    =		BlenderbotTokenizer



    def __init__(	self      :       Dict,__A      :       Optional[Any]=None,__A      :       List[str]=None,__A      :       Optional[Any]=None,__A      :       List[Any]="replace",__A      :       List[Any]="<s>",__A      :       str="</s>",__A      :       List[str]="</s>",__A      :       List[Any]="<s>",__A      :       Union[str, Any]="<unk>",__A      :       Optional[Any]="<pad>",__A      :       Dict="<mask>",__A      :       Any=False,__A      :       Tuple=True,**__A      :       Dict,):
          super().__init__(
              __A,__A,tokenizer_file=__A,errors=__A,bos_token=__A,eos_token=__A,sep_token=__A,cls_token=__A,unk_token=__A,pad_token=__A,mask_token=__A,add_prefix_space=__A,trim_offsets=__A,**__A,)

          _lowerCamelCase				:   Tuple								=   json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__()     )
          if pre_tok_state.get("add_prefix_space",__A     ) != add_prefix_space:
                _lowerCamelCase				:   Optional[Any]								=   getattr(__A,pre_tok_state.pop("type"     )     )
                _lowerCamelCase				:   List[Any]								=   add_prefix_space
                _lowerCamelCase				:   Optional[Any]								=   pre_tok_class(**__A     )

          _lowerCamelCase				:   Any								=   add_prefix_space

          _lowerCamelCase				:   Any								=   "post_processor"
          _lowerCamelCase				:   Optional[Any]								=   getattr(self.backend_tokenizer,__A,__A     )
          if tokenizer_component_instance:
                _lowerCamelCase				:   Tuple								=   json.loads(tokenizer_component_instance.__getstate__()     )

                # The lists 'sep' and 'cls' must be cased in tuples for the object `post_processor_class`
                if "sep" in state:
                      _lowerCamelCase				:   List[Any]								=   tuple(state["sep"]     )
                if "cls" in state:
                      _lowerCamelCase				:   Optional[int]								=   tuple(state["cls"]     )

                _lowerCamelCase				:   List[str]								=   False

                if state.get("add_prefix_space",__A     ) != add_prefix_space:
                      _lowerCamelCase				:   List[str]								=   add_prefix_space
                      _lowerCamelCase				:   int								=   True

                if state.get("trim_offsets",__A     ) != trim_offsets:
                      _lowerCamelCase				:   List[str]								=   trim_offsets
                      _lowerCamelCase				:   Dict								=   True

                if changes_to_apply:
                      _lowerCamelCase				:   Tuple								=   getattr(__A,state.pop("type"     )     )
                      _lowerCamelCase				:   Union[str, Any]								=   component_class(**__A     )
                      setattr(self.backend_tokenizer,__A,__A     )



    @property
    # Copied from transformers.models.roberta.tokenization_roberta_fast.RobertaTokenizerFast.mask_token with Roberta->Blenderbot, RoBERTa->Blenderbot
    def   lowerCamelCase_  (	self      :       Optional[int]     ):
          if self._mask_token is None:
                if self.verbose:
                      logger.error("Using mask_token, but it is not set yet."     )
                return None
          return str(self._mask_token     )



    @mask_token.setter
    def   lowerCamelCase_  (	self      :       Optional[Any],__A      :       List[Any]     ):
          _lowerCamelCase				:   Any								=   AddedToken(__A,lstrip=__A,rstrip=__A     ) if isinstance(__A,__A     ) else value
          _lowerCamelCase				:   List[str]								=   value



    def   lowerCamelCase_  (	self      :       Optional[int],*__A      :       Optional[Any],**__A      :       List[str]     ):
          _lowerCamelCase				:   int								=   kwargs.get("is_split_into_words",__A     )
          assert self.add_prefix_space or not is_split_into_words, (
              f'You need to instantiate {self.__class__.__name__} with add_prefix_space=True '
              "to use it with pretokenized inputs."
          )

          return super()._batch_encode_plus(*__A,**__A     )



    def   lowerCamelCase_  (	self      :       str,*__A      :       Union[str, Any],**__A      :       List[Any]     ):
          _lowerCamelCase				:   Optional[int]								=   kwargs.get("is_split_into_words",__A     )

          assert self.add_prefix_space or not is_split_into_words, (
              f'You need to instantiate {self.__class__.__name__} with add_prefix_space=True '
              "to use it with pretokenized inputs."
          )

          return super()._encode_plus(*__A,**__A     )



    def   lowerCamelCase_  (	self      :       str,__A      :       str,__A      :       Optional[str] = None     ):
          _lowerCamelCase				:   Optional[int]								=   self._tokenizer.model.save(__A,name=__A     )
          return tuple(__A     )



    def   lowerCamelCase_  (	self      :       Optional[int],__A      :       List[int],__A      :       Optional[List[int]] = None     ):
          _lowerCamelCase				:   int								=   [self.sep_token_id]
          _lowerCamelCase				:   str								=   [self.cls_token_id]

          if token_ids_a is None:
                return len(cls + token_ids_a + sep     ) * [0]
          return len(cls + token_ids_a + sep + sep + token_ids_a + sep     ) * [0]



    def   lowerCamelCase_  (	self      :       Dict,__A      :       List[int],__A      :       Optional[List[int]] = None     ):
          return token_ids_a + [self.eos_token_id]



    def   lowerCamelCase_  (	self      :       List[str],__A      :       "Conversation"     ):
          _lowerCamelCase				:   Dict								=   []
          for is_user, text in conversation.iter_texts():
                if is_user:
                      # We need to space prefix as it's being done within blenderbot
                      inputs.append(" " + text     )
                else:
                      # Generated responses should contain them already.
                      inputs.append(__A     )

          _lowerCamelCase				:   List[Any]								=   "  ".join(__A     )
          _lowerCamelCase				:   List[str]								=   self.encode(__A     )
          if len(__A     ) > self.model_max_length:
                _lowerCamelCase				:   Tuple								=   input_ids[-self.model_max_length :]
                logger.warning(f'Trimmed input from conversation as it was longer than {self.model_max_length} tokens.'     )
          return input_ids