




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       UpperCAmelCase__  (     lowerCamelCase   ):
  lowercase    :int      	=						[2, 2, 6, 2] if "tiny" in model_name else [2, 2, 18, 2]
  lowercase    :Optional[Any]      	=						True if "large" in model_name or "huge" in model_name else False
  lowercase    :Tuple      	=						True if "large" in model_name or "huge" in model_name else False
  lowercase    :Optional[int]      	=						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:
      lowercase    :List[str]      	=						[3, 3, 3, 3]
      lowercase    :str      	=						[5, 5, 5, 5]
    elif "fl4" in model_name:
      lowercase    :Optional[Any]      	=						[4, 4, 4, 4]
      lowercase    :List[Any]      	=						[3, 3, 3, 3]

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

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

  # set label information
  lowercase    :Dict      	=						"huggingface/label-files"
  if "large" in model_name or "huge" in model_name:
    lowercase    :Tuple      	=						"imagenet-22k-id2label.json"
  else:
    lowercase    :int      	=						"imagenet-1k-id2label.json"

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

  lowercase    :str      	=						FocalNetConfig(
      embed_dim=lowerCamelCase,			depths=lowerCamelCase,			focal_levels=lowerCamelCase,			focal_windows=lowerCamelCase,			use_conv_embed=lowerCamelCase,			idalabel=lowerCamelCase,			labelaid=lowerCamelCase,			use_post_layernorm=lowerCamelCase,			use_layerscale=lowerCamelCase,			)

  return config



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

  if name == "norm.weight":
    lowercase    :Any      	=						"layernorm.weight"
  if name == "norm.bias":
    lowercase    :Tuple      	=						"layernorm.bias"

  if "head" in name:
    lowercase    :Any      	=						name.replace("head",			"classifier"   )
  else:
    lowercase    :str      	=						"focalnet." + name

  return name



def       UpperCAmelCase__  (     lowerCamelCase,			lowerCamelCase,			lowerCamelCase=False   ):
  # fmt: off
  lowercase    :Optional[int]      	=						{
      "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

  lowercase    :Union[str, Any]      	=						model_name_to_url[model_name]
  print("Checkpoint URL: ",			lowerCamelCase   )
  lowercase    :Optional[int]      	=						torch.hub.load_state_dict_from_url(lowerCamelCase,			map_location="cpu"   )["model"]

  # rename keys
  for key in state_dict.copy().keys():
    lowercase    :Optional[int]      	=						state_dict.pop(lowerCamelCase   )
    lowercase    :Union[str, Any]      	=						val

  lowercase    :str      	=						get_focalnet_config(lowerCamelCase   )
  lowercase    :List[Any]      	=						FocalNetForImageClassification(lowerCamelCase   )
  model.eval()

  # load state dict
  model.load_state_dict(lowerCamelCase   )

  # verify conversion
  lowercase    :Any      	=						"http://images.cocodataset.org/val2017/000000039769.jpg"

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

  lowercase    :int      	=						transforms.Compose(
      [
          transforms.Resize(256   ),
          transforms.CenterCrop(224   ),
          transforms.ToTensor(),
          transforms.Normalize(mean=[0.485, 0.456, 0.406],			std=[0.229, 0.224, 0.225]   ),
      ]   )

  lowercase    :Dict      	=						image_transforms(lowerCamelCase   ).unsqueeze(0   )

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

  lowercase    :Dict      	=						model(**lowerCamelCase   )

  lowercase    :Optional[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":
    lowercase    :Optional[int]      	=						torch.tensor([0.2_166, -0.4_368, 0.2_191]   )
  elif model_name == "focalnet-tiny-lrf":
    lowercase    :Optional[Any]      	=						torch.tensor([1.1_669, 0.0_125, -0.1_695]   )
  elif model_name == "focalnet-small":
    lowercase    :List[str]      	=						torch.tensor([0.4_917, -0.0_430, 0.1_341]   )
  elif model_name == "focalnet-small-lrf":
    lowercase    :int      	=						torch.tensor([-0.2_588, -0.5_342, -0.2_331]   )
  elif model_name == "focalnet-base":
    lowercase    :int      	=						torch.tensor([-0.1_655, -0.4_090, -0.1_730]   )
  elif model_name == "focalnet-base-lrf":
    lowercase    :List[Any]      	=						torch.tensor([0.5_306, -0.0_483, -0.3_928]   )
  assert torch.allclose(outputs.logits[0, :3],			lowerCamelCase,			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(lowerCamelCase   )
    processor.save_pretrained(lowerCamelCase   )

  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[Any]			=     parser.parse_args()
     convert_focalnet_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)









import json
from typing import List, Optional, Tuple

from tokenizers import normalizers

from ...tokenization_utils_base import BatchEncoding
from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import PaddingStrategy, logging
from .tokenization_realm import RealmTokenizer


_UpperCAmelCase	:						Optional[int]			=     logging.get_logger(__name__)

_UpperCAmelCase	:						Union[str, Any]			=     {"vocab_file": "vocab.txt", "tokenizer_file": "tokenizer.json"}

_UpperCAmelCase	:						Optional[Any]			=     {
    "vocab_file": {
        "google/realm-cc-news-pretrained-embedder": (
            "https://huggingface.co/google/realm-cc-news-pretrained-embedder/resolve/main/vocab.txt"
        ),
        "google/realm-cc-news-pretrained-encoder": (
            "https://huggingface.co/google/realm-cc-news-pretrained-encoder/resolve/main/vocab.txt"
        ),
        "google/realm-cc-news-pretrained-scorer": (
            "https://huggingface.co/google/realm-cc-news-pretrained-scorer/resolve/main/vocab.txt"
        ),
        "google/realm-cc-news-pretrained-openqa": (
            "https://huggingface.co/google/realm-cc-news-pretrained-openqa/aresolve/main/vocab.txt"
        ),
        "google/realm-orqa-nq-openqa": "https://huggingface.co/google/realm-orqa-nq-openqa/resolve/main/vocab.txt",
        "google/realm-orqa-nq-reader": "https://huggingface.co/google/realm-orqa-nq-reader/resolve/main/vocab.txt",
        "google/realm-orqa-wq-openqa": "https://huggingface.co/google/realm-orqa-wq-openqa/resolve/main/vocab.txt",
        "google/realm-orqa-wq-reader": "https://huggingface.co/google/realm-orqa-wq-reader/resolve/main/vocab.txt",
    },
    "tokenizer_file": {
        "google/realm-cc-news-pretrained-embedder": (
            "https://huggingface.co/google/realm-cc-news-pretrained-embedder/resolve/main/tokenizer.jsont"
        ),
        "google/realm-cc-news-pretrained-encoder": (
            "https://huggingface.co/google/realm-cc-news-pretrained-encoder/resolve/main/tokenizer.json"
        ),
        "google/realm-cc-news-pretrained-scorer": (
            "https://huggingface.co/google/realm-cc-news-pretrained-scorer/resolve/main/tokenizer.json"
        ),
        "google/realm-cc-news-pretrained-openqa": (
            "https://huggingface.co/google/realm-cc-news-pretrained-openqa/aresolve/main/tokenizer.json"
        ),
        "google/realm-orqa-nq-openqa": (
            "https://huggingface.co/google/realm-orqa-nq-openqa/resolve/main/tokenizer.json"
        ),
        "google/realm-orqa-nq-reader": (
            "https://huggingface.co/google/realm-orqa-nq-reader/resolve/main/tokenizer.json"
        ),
        "google/realm-orqa-wq-openqa": (
            "https://huggingface.co/google/realm-orqa-wq-openqa/resolve/main/tokenizer.json"
        ),
        "google/realm-orqa-wq-reader": (
            "https://huggingface.co/google/realm-orqa-wq-reader/resolve/main/tokenizer.json"
        ),
    },
}

_UpperCAmelCase	:						Tuple			=     {
    "google/realm-cc-news-pretrained-embedder": 512,
    "google/realm-cc-news-pretrained-encoder": 512,
    "google/realm-cc-news-pretrained-scorer": 512,
    "google/realm-cc-news-pretrained-openqa": 512,
    "google/realm-orqa-nq-openqa": 512,
    "google/realm-orqa-nq-reader": 512,
    "google/realm-orqa-wq-openqa": 512,
    "google/realm-orqa-wq-reader": 512,
}

_UpperCAmelCase	:						Dict			=     {
    "google/realm-cc-news-pretrained-embedder": {"do_lower_case": True},
    "google/realm-cc-news-pretrained-encoder": {"do_lower_case": True},
    "google/realm-cc-news-pretrained-scorer": {"do_lower_case": True},
    "google/realm-cc-news-pretrained-openqa": {"do_lower_case": True},
    "google/realm-orqa-nq-openqa": {"do_lower_case": True},
    "google/realm-orqa-nq-reader": {"do_lower_case": True},
    "google/realm-orqa-wq-openqa": {"do_lower_case": True},
    "google/realm-orqa-wq-reader": {"do_lower_case": True},
}




class    __lowerCAmelCase       (							lowerCAmelCase):
       _a 					=						VOCAB_FILES_NAMES
       _a 					=						PRETRAINED_VOCAB_FILES_MAP
       _a 					=						PRETRAINED_INIT_CONFIGURATION
       _a 					=						PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
       _a 					=						RealmTokenizer






       def __init__(   self:					Tuple       ,					_lowerCAmelCase:					int=None       ,					_lowerCAmelCase:					List[Any]=None       ,					_lowerCAmelCase:					Optional[int]=True       ,					_lowerCAmelCase:					Optional[Any]="[UNK]"       ,					_lowerCAmelCase:					Dict="[SEP]"       ,					_lowerCAmelCase:					List[str]="[PAD]"       ,					_lowerCAmelCase:					Optional[int]="[CLS]"       ,					_lowerCAmelCase:					List[Any]="[MASK]"       ,					_lowerCAmelCase:					Tuple=True       ,					_lowerCAmelCase:					Tuple=None       ,					**_lowerCAmelCase:					Union[str, Any]       ,					):
         super().__init__(
             _lowerCAmelCase       ,					tokenizer_file=_lowerCAmelCase       ,					do_lower_case=_lowerCAmelCase       ,					unk_token=_lowerCAmelCase       ,					sep_token=_lowerCAmelCase       ,					pad_token=_lowerCAmelCase       ,					cls_token=_lowerCAmelCase       ,					mask_token=_lowerCAmelCase       ,					tokenize_chinese_chars=_lowerCAmelCase       ,					strip_accents=_lowerCAmelCase       ,					**_lowerCAmelCase       ,					)

         lowercase    :List[str]      	=						json.loads(self.backend_tokenizer.normalizer.__getstate__()				)
         if (
             normalizer_state.get("lowercase"       ,					_lowerCAmelCase				) != do_lower_case
             or normalizer_state.get("strip_accents"       ,					_lowerCAmelCase				) != strip_accents
             or normalizer_state.get("handle_chinese_chars"       ,					_lowerCAmelCase				) != tokenize_chinese_chars
         ):
           lowercase    :List[str]      	=						getattr(_lowerCAmelCase       ,					normalizer_state.pop("type"				)				)
           lowercase    :List[str]      	=						do_lower_case
           lowercase    :Any      	=						strip_accents
           lowercase    :Tuple      	=						tokenize_chinese_chars
           lowercase    :Any      	=						normalizer_class(**_lowerCAmelCase				)

         lowercase    :Any      	=						do_lower_case






       def 					SCREAMING_SNAKE_CASE    (   self:					Any       ,					_lowerCAmelCase:					Optional[Any]       ,					**_lowerCAmelCase:					Optional[int]				):
         lowercase    :List[str]      	=						PaddingStrategy.MAX_LENGTH

         lowercase    :Optional[Any]      	=						text
         lowercase    :str      	=						kwargs.pop("text_pair"       ,					_lowerCAmelCase				)
         lowercase    :Any      	=						kwargs.pop("return_tensors"       ,					_lowerCAmelCase				)

         lowercase    :List[str]      	=						{
             "input_ids": [],
             "attention_mask": [],
             "token_type_ids": [],
         }

         for idx, candidate_text in enumerate(_lowerCAmelCase				):
           if batch_text_pair is not None:
             lowercase    :Any      	=						batch_text_pair[idx]
           else:
             lowercase    :Optional[Any]      	=						None

           lowercase    :Optional[Any]      	=						super().__call__(_lowerCAmelCase       ,					_lowerCAmelCase       ,					return_tensors=_lowerCAmelCase       ,					**_lowerCAmelCase				)

           lowercase    :Tuple      	=						encoded_candidates.get("input_ids"				)
           lowercase    :Optional[Any]      	=						encoded_candidates.get("attention_mask"				)
           lowercase    :int      	=						encoded_candidates.get("token_type_ids"				)

           if encoded_input_ids is not None:
             output_data["input_ids"].append(_lowerCAmelCase				)
           if encoded_attention_mask is not None:
             output_data["attention_mask"].append(_lowerCAmelCase				)
           if encoded_token_type_ids is not None:
             output_data["token_type_ids"].append(_lowerCAmelCase				)

         lowercase    :str      	=						{key: item for key, item in output_data.items() if len(_lowerCAmelCase				) != 0}

         return BatchEncoding(_lowerCAmelCase       ,					tensor_type=_lowerCAmelCase				)






       def 					SCREAMING_SNAKE_CASE    (   self:					Any       ,					_lowerCAmelCase:					Any       ,					_lowerCAmelCase:					Tuple=None				):
         lowercase    :Dict      	=						[self.cls_token_id] + token_ids_a + [self.sep_token_id]

         if token_ids_a:
           output += token_ids_a + [self.sep_token_id]

         return output






       def 					SCREAMING_SNAKE_CASE    (   self:					Tuple       ,					_lowerCAmelCase:					List[int]       ,					_lowerCAmelCase:					Optional[List[int]] = None				):
         lowercase    :List[str]      	=						[self.sep_token_id]
         lowercase    :List[Any]      	=						[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				) * [0] + len(token_ids_a + sep				) * [1]






       def 					SCREAMING_SNAKE_CASE    (   self:					Optional[Any]       ,					_lowerCAmelCase:					str       ,					_lowerCAmelCase:					Optional[str] = None				):
         lowercase    :Tuple      	=						self._tokenizer.model.save(_lowerCAmelCase       ,					name=_lowerCAmelCase				)
         return tuple(_lowerCAmelCase				)



