


'''simple docstring'''




from argparse import ArgumentParser, Namespace

from ..utils import logging
from . import BaseTransformersCLICommand





def 	a_					( lowerCamelCase	:     Namespace				):
   return ConvertCommand(
       args.model_type	, args.tf_checkpoint	, args.pytorch_dump_output	, args.config	, args.finetuning_task_name				)


__snake_case    						="""
transformers can only be used from the commandline to convert TensorFlow models in PyTorch, In that case, it requires
TensorFlow to be installed. Please see https://www.tensorflow.org/install/ for installation instructions.
"""




class 		UpperCAmelCase_      (  __lowercase		):





     @staticmethod
     def 							__UpperCAmelCase      ( UpperCAmelCase__       :      ArgumentParser    )				->  Optional[int]:
        lowerCAmelCase     =			parser.add_parser(
            'convert'    ,      help='CLI tool to run convert model from original author checkpoints to Transformers PyTorch checkpoints.'    ,      )
        train_parser.add_argument('--model_type'    ,      type=UpperCAmelCase__    ,      required=UpperCAmelCase__    ,      help='Model\'s type.'    )
        train_parser.add_argument(
            '--tf_checkpoint'    ,      type=UpperCAmelCase__    ,      required=UpperCAmelCase__    ,      help='TensorFlow checkpoint path or folder.'    )
        train_parser.add_argument(
            '--pytorch_dump_output'    ,      type=UpperCAmelCase__    ,      required=UpperCAmelCase__    ,      help='Path to the PyTorch saved model output.'    )
        train_parser.add_argument('--config'    ,      type=UpperCAmelCase__    ,      default=''    ,      help='Configuration file path or folder.'    )
        train_parser.add_argument(
            '--finetuning_task_name'    ,      type=UpperCAmelCase__    ,      default=UpperCAmelCase__    ,      help='Optional fine-tuning task name if the TF model was a finetuned model.'    ,      )
        train_parser.set_defaults(func=UpperCAmelCase__    )





     def __init__( self       :      Optional[int]    ,      UpperCAmelCase__       :      str    ,      UpperCAmelCase__       :      str    ,      UpperCAmelCase__       :      str    ,      UpperCAmelCase__       :      str    ,      UpperCAmelCase__       :      str    ,      *UpperCAmelCase__       :      List[str]    ,      )				->  Optional[Any]:
        lowerCAmelCase     =			logging.get_logger('transformers-cli/converting'    )

        self._logger.info(F'''Loading model {model_type}'''    )
        lowerCAmelCase     =			model_type
        lowerCAmelCase     =			tf_checkpoint
        lowerCAmelCase     =			pytorch_dump_output
        lowerCAmelCase     =			config
        lowerCAmelCase     =			finetuning_task_name





     def 							__UpperCAmelCase      ( self       :      List[str]    )				->  Dict:
        if self._model_type == "albert":
           try:
              from ..models.albert.convert_albert_original_tf_checkpoint_to_pytorch import (
                  convert_tf_checkpoint_to_pytorch,
              )
           except ImportError:
              raise ImportError(UpperCAmelCase__    )

           convert_tf_checkpoint_to_pytorch(self._tf_checkpoint    ,      self._config    ,      self._pytorch_dump_output    )
        elif self._model_type == "bert":
           try:
              from ..models.bert.convert_bert_original_tf_checkpoint_to_pytorch import (
                  convert_tf_checkpoint_to_pytorch,
              )
           except ImportError:
              raise ImportError(UpperCAmelCase__    )

           convert_tf_checkpoint_to_pytorch(self._tf_checkpoint    ,      self._config    ,      self._pytorch_dump_output    )
        elif self._model_type == "funnel":
           try:
              from ..models.funnel.convert_funnel_original_tf_checkpoint_to_pytorch import (
                  convert_tf_checkpoint_to_pytorch,
              )
           except ImportError:
              raise ImportError(UpperCAmelCase__    )

           convert_tf_checkpoint_to_pytorch(self._tf_checkpoint    ,      self._config    ,      self._pytorch_dump_output    )
        elif self._model_type == "t5":
           try:
              from ..models.ta.convert_ta_original_tf_checkpoint_to_pytorch import convert_tf_checkpoint_to_pytorch
           except ImportError:
              raise ImportError(UpperCAmelCase__    )

           convert_tf_checkpoint_to_pytorch(self._tf_checkpoint    ,      self._config    ,      self._pytorch_dump_output    )
        elif self._model_type == "gpt":
           from ..models.openai.convert_openai_original_tf_checkpoint_to_pytorch import (
               convert_openai_checkpoint_to_pytorch,
           )

           convert_openai_checkpoint_to_pytorch(self._tf_checkpoint    ,      self._config    ,      self._pytorch_dump_output    )
        elif self._model_type == "transfo_xl":
           try:
              from ..models.transfo_xl.convert_transfo_xl_original_tf_checkpoint_to_pytorch import (
                  convert_transfo_xl_checkpoint_to_pytorch,
              )
           except ImportError:
              raise ImportError(UpperCAmelCase__    )

           if "ckpt" in self._tf_checkpoint.lower():
              lowerCAmelCase     =			self._tf_checkpoint
              lowerCAmelCase     =			''
           else:
              lowerCAmelCase     =			self._tf_checkpoint
              lowerCAmelCase     =			''
           convert_transfo_xl_checkpoint_to_pytorch(
               UpperCAmelCase__    ,      self._config    ,      self._pytorch_dump_output    ,      UpperCAmelCase__    )
        elif self._model_type == "gpt2":
           try:
              from ..models.gpta.convert_gpta_original_tf_checkpoint_to_pytorch import (
                  convert_gpta_checkpoint_to_pytorch,
              )
           except ImportError:
              raise ImportError(UpperCAmelCase__    )

           convert_gpta_checkpoint_to_pytorch(self._tf_checkpoint    ,      self._config    ,      self._pytorch_dump_output    )
        elif self._model_type == "xlnet":
           try:
              from ..models.xlnet.convert_xlnet_original_tf_checkpoint_to_pytorch import (
                  convert_xlnet_checkpoint_to_pytorch,
              )
           except ImportError:
              raise ImportError(UpperCAmelCase__    )

           convert_xlnet_checkpoint_to_pytorch(
               self._tf_checkpoint    ,      self._config    ,      self._pytorch_dump_output    ,      self._finetuning_task_name    )
        elif self._model_type == "xlm":
           from ..models.xlm.convert_xlm_original_pytorch_checkpoint_to_pytorch import (
               convert_xlm_checkpoint_to_pytorch,
           )

           convert_xlm_checkpoint_to_pytorch(self._tf_checkpoint    ,      self._pytorch_dump_output    )
        elif self._model_type == "lxmert":
           from ..models.lxmert.convert_lxmert_original_tf_checkpoint_to_pytorch import (
               convert_lxmert_checkpoint_to_pytorch,
           )

           convert_lxmert_checkpoint_to_pytorch(self._tf_checkpoint    ,      self._pytorch_dump_output    )
        elif self._model_type == "rembert":
           from ..models.rembert.convert_rembert_tf_checkpoint_to_pytorch import (
               convert_rembert_tf_checkpoint_to_pytorch,
           )

           convert_rembert_tf_checkpoint_to_pytorch(self._tf_checkpoint    ,      self._config    ,      self._pytorch_dump_output    )
        else:
           raise ValueError(
               '--model_type should be selected in the list [bert, gpt, gpt2, t5, transfo_xl, xlnet, xlm, lxmert]'    )









'''simple docstring'''




import gc
import unittest

import numpy as np
import torch

from diffusers import StableDiffusionKDiffusionPipeline
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu


enable_full_determinism()

@slow
@require_torch_gpu
class 		UpperCAmelCase_      (  unittest.TestCase		):





     def 							__UpperCAmelCase      ( self       :      Optional[int]    )				->  int:
        # clean up the VRAM after each test
        super().tearDown()
        gc.collect()
        torch.cuda.empty_cache()





     def 							__UpperCAmelCase      ( self       :      Tuple    )				->  Any:
        lowerCAmelCase     =			StableDiffusionKDiffusionPipeline.from_pretrained('CompVis/stable-diffusion-v1-4'    )
        lowerCAmelCase     =			sd_pipe.to(UpperCAmelCase__    )
        sd_pipe.set_progress_bar_config(disable=UpperCAmelCase__    )

        sd_pipe.set_scheduler('sample_euler'    )

        lowerCAmelCase     =			'A painting of a squirrel eating a burger'
        lowerCAmelCase     =			torch.manual_seed(0    )
        lowerCAmelCase     =			sd_pipe([prompt]    ,      generator=UpperCAmelCase__    ,      guidance_scale=9.0    ,      num_inference_steps=2_0    ,      output_type='np'    )

        lowerCAmelCase     =			output.images

        lowerCAmelCase     =			image[0, -3:, -3:, -1]

        assert image.shape == (1, 5_1_2, 5_1_2, 3)
        lowerCAmelCase     =			np.array([0.0_447, 0.0_492, 0.0_468, 0.0_408, 0.0_383, 0.0_408, 0.0_354, 0.0_380, 0.0_339]    )

        assert np.abs(image_slice.flatten() - expected_slice    ).max() < 1E-2





     def 							__UpperCAmelCase      ( self       :      List[str]    )				->  Dict:
        lowerCAmelCase     =			StableDiffusionKDiffusionPipeline.from_pretrained('stabilityai/stable-diffusion-2-1-base'    )
        lowerCAmelCase     =			sd_pipe.to(UpperCAmelCase__    )
        sd_pipe.set_progress_bar_config(disable=UpperCAmelCase__    )

        sd_pipe.set_scheduler('sample_euler'    )

        lowerCAmelCase     =			'A painting of a squirrel eating a burger'
        lowerCAmelCase     =			torch.manual_seed(0    )
        lowerCAmelCase     =			sd_pipe([prompt]    ,      generator=UpperCAmelCase__    ,      guidance_scale=9.0    ,      num_inference_steps=2_0    ,      output_type='np'    )

        lowerCAmelCase     =			output.images

        lowerCAmelCase     =			image[0, -3:, -3:, -1]

        assert image.shape == (1, 5_1_2, 5_1_2, 3)
        lowerCAmelCase     =			np.array([0.1_237, 0.1_320, 0.1_438, 0.1_359, 0.1_390, 0.1_132, 0.1_277, 0.1_175, 0.1_112]    )

        assert np.abs(image_slice.flatten() - expected_slice    ).max() < 5E-1





     def 							__UpperCAmelCase      ( self       :      Optional[Any]    )				->  List[str]:
        lowerCAmelCase     =			StableDiffusionKDiffusionPipeline.from_pretrained('stabilityai/stable-diffusion-2-1-base'    )
        lowerCAmelCase     =			sd_pipe.to(UpperCAmelCase__    )
        sd_pipe.set_progress_bar_config(disable=UpperCAmelCase__    )

        sd_pipe.set_scheduler('sample_dpmpp_2m'    )

        lowerCAmelCase     =			'A painting of a squirrel eating a burger'
        lowerCAmelCase     =			torch.manual_seed(0    )
        lowerCAmelCase     =			sd_pipe(
            [prompt]    ,      generator=UpperCAmelCase__    ,      guidance_scale=7.5    ,      num_inference_steps=1_5    ,      output_type='np'    ,      use_karras_sigmas=UpperCAmelCase__    ,      )

        lowerCAmelCase     =			output.images

        lowerCAmelCase     =			image[0, -3:, -3:, -1]

        assert image.shape == (1, 5_1_2, 5_1_2, 3)
        lowerCAmelCase     =			np.array(
            [0.11_381_689, 0.12_112_921, 0.1_389_457, 0.12_549_606, 0.1_244_964, 0.10_831_517, 0.11_562_866, 0.10_867_816, 0.10_499_048]    )

        assert np.abs(image_slice.flatten() - expected_slice    ).max() < 1E-2





