





import tempfile
import unittest

import numpy as np
import torch

from diffusers import DiffusionPipeline
from diffusers.models.attention_processor import Attention, AttnAddedKVProcessor



class 				_lowerCAmelCase			(	unittest.TestCase							):


  '''simple docstring'''







  def UpperCamelCase_       (					self					:    Optional[Any]					, UpperCamelCase					:    bool = False							):


    '''simple docstring'''





    _snake_case      : List[Any]														=		10

    if only_cross_attention:
      _snake_case      : Optional[Any]														=		12
    else:
      # when only cross attention is not set, the cross attention dim must be the same as the query dim
      _snake_case      : Union[str, Any]														=		query_dim

    return {
        "query_dim": query_dim,
        "cross_attention_dim": cross_attention_dim,
        "heads": 2,
        "dim_head": 4,
        "added_kv_proj_dim": 6,
        "norm_num_groups": 1,
        "only_cross_attention": only_cross_attention,
        "processor": AttnAddedKVProcessor(),
    }







  def UpperCamelCase_       (					self					:    Optional[int]					, UpperCamelCase					:    Dict					, UpperCamelCase					:    List[str]							):


    '''simple docstring'''





    _snake_case      : Optional[Any]														=		2

    _snake_case      : Union[str, Any]														=		torch.rand(UpperCamelCase					, UpperCamelCase					, 3					, 2							)
    _snake_case      : Union[str, Any]														=		torch.rand(UpperCamelCase					, 4					, UpperCamelCase							)
    _snake_case      : List[Any]														=		None

    return {
        "hidden_states": hidden_states,
        "encoder_hidden_states": encoder_hidden_states,
        "attention_mask": attention_mask,
    }







  def UpperCamelCase_       (					self					:    int							):


    '''simple docstring'''





    torch.manual_seed(0							)

    _snake_case      : Optional[int]														=		self.get_constructor_arguments(only_cross_attention=UpperCamelCase							)
    _snake_case      : List[str]														=		Attention(**UpperCamelCase							)

    self.assertTrue(attn.to_k is not None							)
    self.assertTrue(attn.to_v is not None							)

    _snake_case      : List[str]														=		self.get_forward_arguments(
        query_dim=constructor_args['query_dim']					, added_kv_proj_dim=constructor_args['added_kv_proj_dim']							)

    _snake_case      : Any														=		attn(**UpperCamelCase							)

    # only self attention

    torch.manual_seed(0							)

    _snake_case      : Any														=		self.get_constructor_arguments(only_cross_attention=UpperCamelCase							)
    _snake_case      : int														=		Attention(**UpperCamelCase							)

    self.assertTrue(attn.to_k is None							)
    self.assertTrue(attn.to_v is None							)

    _snake_case      : str														=		self.get_forward_arguments(
        query_dim=constructor_args['query_dim']					, added_kv_proj_dim=constructor_args['added_kv_proj_dim']							)

    _snake_case      : int														=		attn(**UpperCamelCase							)

    self.assertTrue((only_cross_attn_out != self_and_cross_attn_out).all()							)




class 				_lowerCAmelCase			(	unittest.TestCase							):


  '''simple docstring'''







  def UpperCamelCase_       (					self					:    str							):


    '''simple docstring'''





    _snake_case      : Dict														=		DiffusionPipeline.from_pretrained('hf-internal-testing/tiny-stable-diffusion-pipe'					, safety_checker=UpperCamelCase							)

    _snake_case      : Union[str, Any]														=		pipe(
        'foo'					, num_inference_steps=2					, generator=torch.Generator('cpu'							).manual_seed(0							)					, output_type='np'					, ).images

    # the initial conversion succeeds
    _snake_case      : Dict														=		DiffusionPipeline.from_pretrained(
        'hf-internal-testing/tiny-stable-diffusion-pipe'					, device_map='sequential'					, safety_checker=UpperCamelCase							)

    _snake_case      : Tuple														=		pipe(
        'foo'					, num_inference_steps=2					, generator=torch.Generator('cpu'							).manual_seed(0							)					, output_type='np'					, ).images

    with tempfile.TemporaryDirectory() as tmpdir:
      # save the converted model
      pipe.save_pretrained(UpperCamelCase							)

      # can also load the converted weights
      _snake_case      : Optional[Any]														=		DiffusionPipeline.from_pretrained(UpperCamelCase					, device_map='sequential'					, safety_checker=UpperCamelCase							)

    _snake_case      : List[str]														=		pipe(
        'foo'					, num_inference_steps=2					, generator=torch.Generator('cpu'							).manual_seed(0							)					, output_type='np'					, ).images

    self.assertTrue(np.allclose(UpperCamelCase					, UpperCamelCase					, atol=1e-5							)							)
    self.assertTrue(np.allclose(UpperCamelCase					, UpperCamelCase					, atol=1e-5							)							)











import argparse

import torch

from transformers import ImageGPTConfig, ImageGPTForCausalLM, load_tf_weights_in_imagegpt
from transformers.utils import CONFIG_NAME, WEIGHTS_NAME, logging


logging.set_verbosity_info()


def 			lowerCamelCase_		(					lowerCAmelCase:		Optional[Any]			,							lowerCAmelCase:		Tuple			,							lowerCAmelCase:		List[Any] )->						Dict:
  # Construct configuration depending on size
  _snake_case      : Tuple														=		{'small': (5_12, 8, 24), 'medium': (10_24, 8, 36), 'large': (15_36, 16, 48)}
  _snake_case							,    _snake_case							,    _snake_case      : Optional[Any]														=		MODELS[model_size]  # set model hyperparameters
  _snake_case      : Union[str, Any]														=		ImageGPTConfig(n_embd=lowerCAmelCase			,							n_layer=lowerCAmelCase			,							n_head=lowerCAmelCase )
  _snake_case      : int														=		ImageGPTForCausalLM(lowerCAmelCase )

  # Load weights from numpy
  load_tf_weights_in_imagegpt(lowerCAmelCase			,							lowerCAmelCase			,							lowerCAmelCase )

  # Save pytorch-model
  _snake_case      : List[Any]														=		pytorch_dump_folder_path + '/' + WEIGHTS_NAME
  _snake_case      : Optional[Any]														=		pytorch_dump_folder_path + '/' + CONFIG_NAME
  print(F"""Save PyTorch model to {pytorch_weights_dump_path}""" )
  torch.save(model.state_dict()			,							lowerCAmelCase )
  print(F"""Save configuration file to {pytorch_config_dump_path}""" )
  with open(lowerCAmelCase			,							'w'			,							encoding='utf-8' ) as f:
    f.write(config.to_json_string() )


if __name__ == "__main__":
      lowerCAmelCase_        =			argparse.ArgumentParser()
      # Required parameters
      parser.add_argument(
          """--imagegpt_checkpoint_path""",
          default=None,
          type=str,
          required=True,
          help="""Path to the TensorFlow checkpoint path.""",
      )
      parser.add_argument(
          """--model_size""",
          default=None,
          type=str,
          required=True,
          help="""Size of the model (can be either 'small', 'medium' or 'large').""",
      )
      parser.add_argument(
          """--pytorch_dump_folder_path""", default=None, type=str, required=True, help="""Path to the output PyTorch model."""
      )
      lowerCAmelCase_        =			parser.parse_args()
      convert_imagegpt_checkpoint_to_pytorch(
          args.imagegpt_checkpoint_path, args.model_size, args.pytorch_dump_folder_path
      )




