





import argparse

import requests
import torch
from PIL import Image

from transformers import ViTMAEConfig, ViTMAEForPreTraining, ViTMAEImageProcessor


def        SCREAMING_SNAKE_CASE      (       _UpperCAmelCase )			->     str:
    if "cls_token" in name:
        lowerCamelCase__    :      List[str]					 =		name.replace('cls_token'						,	'vit.embeddings.cls_token' )
    if "mask_token" in name:
        lowerCamelCase__    :      Optional[int]					 =		name.replace('mask_token'						,	'decoder.mask_token' )
    if "decoder_pos_embed" in name:
        lowerCamelCase__    :      Optional[Any]					 =		name.replace('decoder_pos_embed'						,	'decoder.decoder_pos_embed' )
    if "pos_embed" in name and "decoder" not in name:
        lowerCamelCase__    :      Dict					 =		name.replace('pos_embed'						,	'vit.embeddings.position_embeddings' )
    if "patch_embed.proj" in name:
        lowerCamelCase__    :      Union[str, Any]					 =		name.replace('patch_embed.proj'						,	'vit.embeddings.patch_embeddings.projection' )
    if "patch_embed.norm" in name:
        lowerCamelCase__    :      int					 =		name.replace('patch_embed.norm'						,	'vit.embeddings.norm' )
    if "decoder_blocks" in name:
        lowerCamelCase__    :      Optional[int]					 =		name.replace('decoder_blocks'						,	'decoder.decoder_layers' )
    if "blocks" in name:
        lowerCamelCase__    :      str					 =		name.replace('blocks'						,	'vit.encoder.layer' )
    if "attn.proj" in name:
        lowerCamelCase__    :      Dict					 =		name.replace('attn.proj'						,	'attention.output.dense' )
    if "attn" in name:
        lowerCamelCase__    :      int					 =		name.replace('attn'						,	'attention.self' )
    if "norm1" in name:
        lowerCamelCase__    :      Any					 =		name.replace('norm1'						,	'layernorm_before' )
    if "norm2" in name:
        lowerCamelCase__    :      int					 =		name.replace('norm2'						,	'layernorm_after' )
    if "mlp.fc1" in name:
        lowerCamelCase__    :      str					 =		name.replace('mlp.fc1'						,	'intermediate.dense' )
    if "mlp.fc2" in name:
        lowerCamelCase__    :      int					 =		name.replace('mlp.fc2'						,	'output.dense' )
    if "decoder_embed" in name:
        lowerCamelCase__    :      Dict					 =		name.replace('decoder_embed'						,	'decoder.decoder_embed' )
    if "decoder_norm" in name:
        lowerCamelCase__    :      Any					 =		name.replace('decoder_norm'						,	'decoder.decoder_norm' )
    if "decoder_pred" in name:
        lowerCamelCase__    :      List[Any]					 =		name.replace('decoder_pred'						,	'decoder.decoder_pred' )
    if "norm.weight" in name and "decoder" not in name:
        lowerCamelCase__    :      str					 =		name.replace('norm.weight'						,	'vit.layernorm.weight' )
    if "norm.bias" in name and "decoder" not in name:
        lowerCamelCase__    :      List[Any]					 =		name.replace('norm.bias'						,	'vit.layernorm.bias' )

    return name


def        SCREAMING_SNAKE_CASE      (       _UpperCAmelCase						,	_UpperCAmelCase )			->     Union[str, Any]:
    for key in orig_state_dict.copy().keys():
        lowerCamelCase__    :      Tuple					 =		orig_state_dict.pop(_UpperCAmelCase )

        if "qkv" in key:
            lowerCamelCase__    :      List[str]					 =		key.split('.' )
            lowerCamelCase__    :      List[str]					 =		int(key_split[1] )
            if "decoder_blocks" in key:
                lowerCamelCase__    :      Any					 =		config.decoder_hidden_size
                lowerCamelCase__    :      List[Any]					 =		'decoder.decoder_layers.'
                if "weight" in key:
                    lowerCamelCase__    :      int					 =		val[:dim, :]
                    lowerCamelCase__    :      Union[str, Any]					 =		val[dim : dim * 2, :]
                    lowerCamelCase__    :      Union[str, Any]					 =		val[-dim:, :]
                elif "bias" in key:
                    lowerCamelCase__    :      List[str]					 =		val[:dim]
                    lowerCamelCase__    :      Optional[int]					 =		val[dim : dim * 2]
                    lowerCamelCase__    :      str					 =		val[-dim:]
            else:
                lowerCamelCase__    :      Any					 =		config.hidden_size
                lowerCamelCase__    :      Dict					 =		'vit.encoder.layer.'
                if "weight" in key:
                    lowerCamelCase__    :      Optional[Any]					 =		val[:dim, :]
                    lowerCamelCase__    :      List[str]					 =		val[dim : dim * 2, :]
                    lowerCamelCase__    :      List[str]					 =		val[-dim:, :]
                elif "bias" in key:
                    lowerCamelCase__    :      Dict					 =		val[:dim]
                    lowerCamelCase__    :      Union[str, Any]					 =		val[dim : dim * 2]
                    lowerCamelCase__    :      Optional[Any]					 =		val[-dim:]

        else:
            lowerCamelCase__    :      Dict					 =		val

    return orig_state_dict


def        SCREAMING_SNAKE_CASE      (       _UpperCAmelCase						,	_UpperCAmelCase )			->     int:
    lowerCamelCase__    :      Union[str, Any]					 =		ViTMAEConfig()
    if "large" in checkpoint_url:
        lowerCamelCase__    :      List[Any]					 =		1024
        lowerCamelCase__    :      str					 =		4096
        lowerCamelCase__    :      Optional[Any]					 =		24
        lowerCamelCase__    :      List[str]					 =		16
    elif "huge" in checkpoint_url:
        lowerCamelCase__    :      Any					 =		14
        lowerCamelCase__    :      str					 =		1280
        lowerCamelCase__    :      Dict					 =		5120
        lowerCamelCase__    :      Tuple					 =		32
        lowerCamelCase__    :      List[Any]					 =		16

    lowerCamelCase__    :      List[Any]					 =		ViTMAEForPreTraining(_UpperCAmelCase )

    lowerCamelCase__    :      Union[str, Any]					 =		torch.hub.load_state_dict_from_url(_UpperCAmelCase						,	map_location='cpu' )['model']

    lowerCamelCase__    :      List[str]					 =		ViTMAEImageProcessor(size=config.image_size )

    lowerCamelCase__    :      int					 =		convert_state_dict(_UpperCAmelCase						,	_UpperCAmelCase )

    model.load_state_dict(_UpperCAmelCase )
    model.eval()

    lowerCamelCase__    :      Dict					 =		'https://user-images.githubusercontent.com/11435359/147738734-196fd92f-9260-48d5-ba7e-bf103d29364d.jpg'

    lowerCamelCase__    :      Union[str, Any]					 =		Image.open(requests.get(_UpperCAmelCase						,	stream=_UpperCAmelCase ).raw )
    lowerCamelCase__    :      int					 =		ViTMAEImageProcessor(size=config.image_size )
    lowerCamelCase__    :      Tuple					 =		image_processor(images=_UpperCAmelCase						,	return_tensors='pt' )

    # forward pass
    torch.manual_seed(2 )
    lowerCamelCase__    :      str					 =		model(**_UpperCAmelCase )
    lowerCamelCase__    :      Any					 =		outputs.logits

    if "large" in checkpoint_url:
        lowerCamelCase__    :      List[str]					 =		torch.tensor(
            [[-0.7_309, -0.7_128, -1.0_169], [-1.0_161, -0.9_058, -1.1_878], [-1.0_478, -0.9_411, -1.1_911]] )
    elif "huge" in checkpoint_url:
        lowerCamelCase__    :      Optional[Any]					 =		torch.tensor(
            [[-1.1_599, -0.9_199, -1.2_221], [-1.1_952, -0.9_269, -1.2_307], [-1.2_143, -0.9_337, -1.2_262]] )
    else:
        lowerCamelCase__    :      Tuple					 =		torch.tensor(
            [[-0.9_192, -0.8_481, -1.1_259], [-1.1_349, -1.0_034, -1.2_599], [-1.1_757, -1.0_429, -1.2_726]] )

    # verify logits
    assert torch.allclose(logits[0, :3, :3]						,	_UpperCAmelCase						,	atol=1e-4 )

    print(F"""Saving model to {pytorch_dump_folder_path}""" )
    model.save_pretrained(_UpperCAmelCase )

    print(F"""Saving image processor to {pytorch_dump_folder_path}""" )
    image_processor.save_pretrained(_UpperCAmelCase )


if __name__ == "__main__":
    _UpperCAmelCase				:       Optional[Any]	   =				argparse.ArgumentParser()
    # Required parameters
    parser.add_argument(
        """--checkpoint_url""",
        default="""https://dl.fbaipublicfiles.com/mae/visualize/mae_visualize_vit_base.pth""",
        type=str,
        help="""URL of the checkpoint you'd like to convert.""",
    )
    parser.add_argument(
        """--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model directory."""
    )

    _UpperCAmelCase				:       Union[str, Any]	   =				parser.parse_args()
    convert_vit_mae_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path)













from math import log

from scipy.constants import Boltzmann, physical_constants

a		:       Any			     = 300  # TEMPERATURE (unit = K)






def 				lowerCamelCase__      (    __lowerCamelCase			:     float  ,		__lowerCamelCase			:     float  ,		__lowerCamelCase			:     float  ,		):

  if donor_conc <= 0:
    raise ValueError("""Donor concentration should be positive"""		)
  elif acceptor_conc <= 0:
    raise ValueError("""Acceptor concentration should be positive"""		)
  elif intrinsic_conc <= 0:
    raise ValueError("""Intrinsic concentration should be positive"""		)
  elif donor_conc <= intrinsic_conc:
    raise ValueError(
        """Donor concentration should be greater than intrinsic concentration"""		)
  elif acceptor_conc <= intrinsic_conc:
    raise ValueError(
        """Acceptor concentration should be greater than intrinsic concentration"""		)
  else:
    return (
        Boltzmann
        * T
        * log((donor_conc * acceptor_conc) / intrinsic_conc**2		)
        / physical_constants["electron volt"][0]
    )


if __name__ == "__main__":
       import doctest

       doctest.testmod()


