

'''simple docstring'''
import tempfile
import unittest

import numpy as np

from diffusers import (
    DDIMScheduler,
    DPMSolverMultistepScheduler,
    EulerAncestralDiscreteScheduler,
    EulerDiscreteScheduler,
    LMSDiscreteScheduler,
    OnnxStableDiffusionPipeline,
    PNDMScheduler,
)
from diffusers.utils.testing_utils import is_onnx_available, nightly, require_onnxruntime, require_torch_gpu

from ..test_pipelines_onnx_common import OnnxPipelineTesterMixin


if is_onnx_available():
     import onnxruntime as ort







class 							A__    (       UpperCamelCase__     ,   unittest.TestCase  ):
  A__		 =				"""hf-internal-testing/tiny-random-OnnxStableDiffusionPipeline"""





  def 						A  (		self      :	List[str]							,					_a      :	str=0 )	->    Dict:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =np.random.RandomState(__a )
       _SCREAMING_SNAKE_CASE							    ={
           'prompt': 'A painting of a squirrel eating a burger',
           'generator': generator,
           'num_inference_steps': 2,
           'guidance_scale': 7.5,
           'output_type': 'numpy',
       }
       return inputs





  def 						A  (		self      :	Dict )	->    Any:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint							,					provider='CPUExecutionProvider' )
       pipe.set_progress_bar_config(disable=__a )

       _SCREAMING_SNAKE_CASE							    =self.get_dummy_inputs()
       _SCREAMING_SNAKE_CASE							    =pipe(**__a ).images
       _SCREAMING_SNAKE_CASE							    =image[0, -3:, -3:, -1]

       assert image.shape == (1, 128, 128, 3)
       _SCREAMING_SNAKE_CASE							    =np.array([0.6_50_72, 0.5_84_92, 0.4_82_19, 0.5_55_21, 0.5_31_80, 0.5_59_39, 0.5_06_97, 0.3_98_00, 0.4_64_55] )

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





  def 						A  (		self      :	Any )	->    Optional[Any]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint							,					provider='CPUExecutionProvider' )
       _SCREAMING_SNAKE_CASE							    =PNDMScheduler.from_config(pipe.scheduler.config							,					skip_prk_steps=__a )
       pipe.set_progress_bar_config(disable=__a )

       _SCREAMING_SNAKE_CASE							    =self.get_dummy_inputs()
       _SCREAMING_SNAKE_CASE							    =pipe(**__a ).images
       _SCREAMING_SNAKE_CASE							    =image[0, -3:, -3:, -1]

       assert image.shape == (1, 128, 128, 3)
       _SCREAMING_SNAKE_CASE							    =np.array([0.6_58_63, 0.5_94_25, 0.4_93_26, 0.5_63_13, 0.5_38_75, 0.5_66_27, 0.5_10_65, 0.3_97_77, 0.4_63_30] )

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





  def 						A  (		self      :	Tuple )	->    List[str]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint							,					provider='CPUExecutionProvider' )
       _SCREAMING_SNAKE_CASE							    =LMSDiscreteScheduler.from_config(pipe.scheduler.config )
       pipe.set_progress_bar_config(disable=__a )

       _SCREAMING_SNAKE_CASE							    =self.get_dummy_inputs()
       _SCREAMING_SNAKE_CASE							    =pipe(**__a ).images
       _SCREAMING_SNAKE_CASE							    =image[0, -3:, -3:, -1]

       assert image.shape == (1, 128, 128, 3)
       _SCREAMING_SNAKE_CASE							    =np.array([0.5_37_55, 0.6_07_86, 0.4_74_02, 0.4_94_88, 0.5_18_69, 0.4_98_19, 0.4_79_85, 0.3_89_57, 0.4_42_79] )

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





  def 						A  (		self      :	Union[str, Any] )	->    Tuple:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint							,					provider='CPUExecutionProvider' )
       _SCREAMING_SNAKE_CASE							    =EulerDiscreteScheduler.from_config(pipe.scheduler.config )
       pipe.set_progress_bar_config(disable=__a )

       _SCREAMING_SNAKE_CASE							    =self.get_dummy_inputs()
       _SCREAMING_SNAKE_CASE							    =pipe(**__a ).images
       _SCREAMING_SNAKE_CASE							    =image[0, -3:, -3:, -1]

       assert image.shape == (1, 128, 128, 3)
       _SCREAMING_SNAKE_CASE							    =np.array([0.5_37_55, 0.6_07_86, 0.4_74_02, 0.4_94_88, 0.5_18_69, 0.4_98_19, 0.4_79_85, 0.3_89_57, 0.4_42_79] )

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





  def 						A  (		self      :	Tuple )	->    Optional[int]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint							,					provider='CPUExecutionProvider' )
       _SCREAMING_SNAKE_CASE							    =EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config )
       pipe.set_progress_bar_config(disable=__a )

       _SCREAMING_SNAKE_CASE							    =self.get_dummy_inputs()
       _SCREAMING_SNAKE_CASE							    =pipe(**__a ).images
       _SCREAMING_SNAKE_CASE							    =image[0, -3:, -3:, -1]

       assert image.shape == (1, 128, 128, 3)
       _SCREAMING_SNAKE_CASE							    =np.array([0.5_38_17, 0.6_08_12, 0.4_73_84, 0.4_95_30, 0.5_18_94, 0.4_98_14, 0.4_79_84, 0.3_89_58, 0.4_42_71] )

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





  def 						A  (		self      :	Dict )	->    Optional[int]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint							,					provider='CPUExecutionProvider' )
       _SCREAMING_SNAKE_CASE							    =DPMSolverMultistepScheduler.from_config(pipe.scheduler.config )
       pipe.set_progress_bar_config(disable=__a )

       _SCREAMING_SNAKE_CASE							    =self.get_dummy_inputs()
       _SCREAMING_SNAKE_CASE							    =pipe(**__a ).images
       _SCREAMING_SNAKE_CASE							    =image[0, -3:, -3:, -1]

       assert image.shape == (1, 128, 128, 3)
       _SCREAMING_SNAKE_CASE							    =np.array([0.5_38_95, 0.6_08_08, 0.4_79_33, 0.4_96_08, 0.5_18_86, 0.4_99_50, 0.4_80_53, 0.3_89_57, 0.4_42_00] )

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





  def 						A  (		self      :	Dict )	->    str:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint							,					provider='CPUExecutionProvider' )
       pipe.set_progress_bar_config(disable=__a )

       _SCREAMING_SNAKE_CASE							    =self.get_dummy_inputs()
       _SCREAMING_SNAKE_CASE							    =3 * [inputs['prompt']]

       # forward
       _SCREAMING_SNAKE_CASE							    =pipe(**__a )
       _SCREAMING_SNAKE_CASE							    =output.images[0, -3:, -3:, -1]

       _SCREAMING_SNAKE_CASE							    =self.get_dummy_inputs()
       _SCREAMING_SNAKE_CASE							    =3 * [inputs.pop('prompt' )]

       _SCREAMING_SNAKE_CASE							    =pipe.tokenizer(
           __a							,					padding='max_length'							,					max_length=pipe.tokenizer.model_max_length							,					truncation=__a							,					return_tensors='np'							,					)
       _SCREAMING_SNAKE_CASE							    =text_inputs['input_ids']

       _SCREAMING_SNAKE_CASE							    =pipe.text_encoder(input_ids=text_inputs.astype(np.intaa ) )[0]

       _SCREAMING_SNAKE_CASE							    =prompt_embeds

       # forward
       _SCREAMING_SNAKE_CASE							    =pipe(**__a )
       _SCREAMING_SNAKE_CASE							    =output.images[0, -3:, -3:, -1]

       assert np.abs(image_slice_a.flatten() - image_slice_a.flatten() ).max() < 1e-4





  def 						A  (		self      :	Any )	->    Optional[int]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint							,					provider='CPUExecutionProvider' )
       pipe.set_progress_bar_config(disable=__a )

       _SCREAMING_SNAKE_CASE							    =self.get_dummy_inputs()
       _SCREAMING_SNAKE_CASE							    =3 * ['this is a negative prompt']
       _SCREAMING_SNAKE_CASE							    =negative_prompt
       _SCREAMING_SNAKE_CASE							    =3 * [inputs['prompt']]

       # forward
       _SCREAMING_SNAKE_CASE							    =pipe(**__a )
       _SCREAMING_SNAKE_CASE							    =output.images[0, -3:, -3:, -1]

       _SCREAMING_SNAKE_CASE							    =self.get_dummy_inputs()
       _SCREAMING_SNAKE_CASE							    =3 * [inputs.pop('prompt' )]

       _SCREAMING_SNAKE_CASE							    =[]
       for p in [prompt, negative_prompt]:
            _SCREAMING_SNAKE_CASE							    =pipe.tokenizer(
                __a							,					padding='max_length'							,					max_length=pipe.tokenizer.model_max_length							,					truncation=__a							,					return_tensors='np'							,					)
            _SCREAMING_SNAKE_CASE							    =text_inputs['input_ids']

            embeds.append(pipe.text_encoder(input_ids=text_inputs.astype(np.intaa ) )[0] )

       _SCREAMING_SNAKE_CASE    ,						_SCREAMING_SNAKE_CASE							    =embeds

       # forward
       _SCREAMING_SNAKE_CASE							    =pipe(**__a )
       _SCREAMING_SNAKE_CASE							    =output.images[0, -3:, -3:, -1]

       assert np.abs(image_slice_a.flatten() - image_slice_a.flatten() ).max() < 1e-4







@nightly
@require_onnxruntime
@require_torch_gpu
class 							A__    (       unittest.TestCase  ):





  @property
  def 						A  (		self      :	List[str] )	->    Optional[int]:





       '''simple docstring'''






       return (
           "CUDAExecutionProvider",
           {
               "gpu_mem_limit": "15000000000",  # 15GB
               "arena_extend_strategy": "kSameAsRequested",
           },
       )





  @property
  def 						A  (		self      :	int )	->    Any:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =ort.SessionOptions()
       _SCREAMING_SNAKE_CASE							    =False
       return options





  def 						A  (		self      :	str )	->    Optional[int]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =OnnxStableDiffusionPipeline.from_pretrained(
           'CompVis/stable-diffusion-v1-4'							,					revision='onnx'							,					safety_checker=__a							,					feature_extractor=__a							,					provider=self.gpu_provider							,					sess_options=self.gpu_options							,					)
       sd_pipe.set_progress_bar_config(disable=__a )

       _SCREAMING_SNAKE_CASE							    ='A painting of a squirrel eating a burger'
       np.random.seed(0 )
       _SCREAMING_SNAKE_CASE							    =sd_pipe([prompt]							,					guidance_scale=6.0							,					num_inference_steps=10							,					output_type='np' )
       _SCREAMING_SNAKE_CASE							    =output.images

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

       assert image.shape == (1, 512, 512, 3)
       _SCREAMING_SNAKE_CASE							    =np.array([0.04_52, 0.03_90, 0.00_87, 0.03_50, 0.06_17, 0.03_64, 0.05_44, 0.05_23, 0.07_20] )

       assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-3





  def 						A  (		self      :	Tuple )	->    Tuple:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =DDIMScheduler.from_pretrained(
           'runwayml/stable-diffusion-v1-5'							,					subfolder='scheduler'							,					revision='onnx' )
       _SCREAMING_SNAKE_CASE							    =OnnxStableDiffusionPipeline.from_pretrained(
           'runwayml/stable-diffusion-v1-5'							,					revision='onnx'							,					scheduler=__a							,					safety_checker=__a							,					feature_extractor=__a							,					provider=self.gpu_provider							,					sess_options=self.gpu_options							,					)
       sd_pipe.set_progress_bar_config(disable=__a )

       _SCREAMING_SNAKE_CASE							    ='open neural network exchange'
       _SCREAMING_SNAKE_CASE							    =np.random.RandomState(0 )
       _SCREAMING_SNAKE_CASE							    =sd_pipe([prompt]							,					guidance_scale=7.5							,					num_inference_steps=10							,					generator=__a							,					output_type='np' )
       _SCREAMING_SNAKE_CASE							    =output.images
       _SCREAMING_SNAKE_CASE							    =image[0, -3:, -3:, -1]

       assert image.shape == (1, 512, 512, 3)
       _SCREAMING_SNAKE_CASE							    =np.array([0.28_67, 0.19_74, 0.14_81, 0.72_94, 0.72_51, 0.66_67, 0.41_94, 0.56_42, 0.64_86] )

       assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-3





  def 						A  (		self      :	List[Any] )	->    Optional[int]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =LMSDiscreteScheduler.from_pretrained(
           'runwayml/stable-diffusion-v1-5'							,					subfolder='scheduler'							,					revision='onnx' )
       _SCREAMING_SNAKE_CASE							    =OnnxStableDiffusionPipeline.from_pretrained(
           'runwayml/stable-diffusion-v1-5'							,					revision='onnx'							,					scheduler=__a							,					safety_checker=__a							,					feature_extractor=__a							,					provider=self.gpu_provider							,					sess_options=self.gpu_options							,					)
       sd_pipe.set_progress_bar_config(disable=__a )

       _SCREAMING_SNAKE_CASE							    ='open neural network exchange'
       _SCREAMING_SNAKE_CASE							    =np.random.RandomState(0 )
       _SCREAMING_SNAKE_CASE							    =sd_pipe([prompt]							,					guidance_scale=7.5							,					num_inference_steps=10							,					generator=__a							,					output_type='np' )
       _SCREAMING_SNAKE_CASE							    =output.images
       _SCREAMING_SNAKE_CASE							    =image[0, -3:, -3:, -1]

       assert image.shape == (1, 512, 512, 3)
       _SCREAMING_SNAKE_CASE							    =np.array([0.23_06, 0.19_59, 0.15_93, 0.65_49, 0.63_94, 0.54_08, 0.50_65, 0.60_10, 0.61_61] )

       assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-3





  def 						A  (		self      :	Optional[int] )	->    Optional[int]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =0

       def test_callback_fn(_a      :	int							,					_a      :	int							,					_a      :	np.ndarray ) -> None:
            _SCREAMING_SNAKE_CASE							    =True
            nonlocal number_of_steps
            number_of_steps += 1
            if step == 0:
                 assert latents.shape == (1, 4, 64, 64)
                 _SCREAMING_SNAKE_CASE							    =latents[0, -3:, -3:, -1]
                 _SCREAMING_SNAKE_CASE							    =np.array(
                     [-0.67_72, -0.38_35, -1.24_56, 0.19_05, -1.09_74, 0.69_67, -1.93_53, 0.01_78, 1.01_67] )

                 assert np.abs(latents_slice.flatten() - expected_slice ).max() < 1e-3
            elif step == 5:
                 assert latents.shape == (1, 4, 64, 64)
                 _SCREAMING_SNAKE_CASE							    =latents[0, -3:, -3:, -1]
                 _SCREAMING_SNAKE_CASE							    =np.array(
                     [-0.33_51, 0.22_41, -0.18_37, -0.23_25, -0.65_77, 0.33_93, -0.02_41, 0.58_99, 1.38_75] )

                 assert np.abs(latents_slice.flatten() - expected_slice ).max() < 1e-3

       _SCREAMING_SNAKE_CASE							    =False

       _SCREAMING_SNAKE_CASE							    =OnnxStableDiffusionPipeline.from_pretrained(
           'runwayml/stable-diffusion-v1-5'							,					revision='onnx'							,					safety_checker=__a							,					feature_extractor=__a							,					provider=self.gpu_provider							,					sess_options=self.gpu_options							,					)
       pipe.set_progress_bar_config(disable=__a )

       _SCREAMING_SNAKE_CASE							    ='Andromeda galaxy in a bottle'

       _SCREAMING_SNAKE_CASE							    =np.random.RandomState(0 )
       pipe(
           prompt=__a							,					num_inference_steps=5							,					guidance_scale=7.5							,					generator=__a							,					callback=__a							,					callback_steps=1							,					)
       assert test_callback_fn.has_been_called
       assert number_of_steps == 6





  def 						A  (		self      :	List[Any] )	->    Optional[Any]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =OnnxStableDiffusionPipeline.from_pretrained(
           'runwayml/stable-diffusion-v1-5'							,					revision='onnx'							,					safety_checker=__a							,					feature_extractor=__a							,					provider=self.gpu_provider							,					sess_options=self.gpu_options							,					)
       assert isinstance(__a							,					__a )
       assert pipe.safety_checker is None

       _SCREAMING_SNAKE_CASE							    =pipe('example prompt'							,					num_inference_steps=2 ).images[0]
       assert image is not None

       # check that there's no error when saving a pipeline with one of the models being None
       with tempfile.TemporaryDirectory() as tmpdirname:
            pipe.save_pretrained(__a )
            _SCREAMING_SNAKE_CASE							    =OnnxStableDiffusionPipeline.from_pretrained(__a )

       # sanity check that the pipeline still works
       assert pipe.safety_checker is None
       _SCREAMING_SNAKE_CASE							    =pipe('example prompt'							,					num_inference_steps=2 ).images[0]
       assert image is not None

'''simple docstring'''





import json
import logging
import os
import sys
from pathlib import Path

import finetune_rag

from transformers.file_utils import is_apex_available
from transformers.testing_utils import (
    TestCasePlus,
    execute_subprocess_async,
    require_ray,
    require_torch_gpu,
    require_torch_multi_gpu,
)


logging.basicConfig(level=logging.DEBUG)
SCREAMING_SNAKE_CASE_:     Optional[int]  =logging.getLogger()

SCREAMING_SNAKE_CASE_:     Union[str, Any]  =logging.StreamHandler(sys.stdout)
logger.addHandler(stream_handler)




class        __A      ( UpperCamelCase__	):



    def 					_lowercase						(self    :    Optional[Any]   ,			__a    :    str ):
         os.makedirs(__a   ,			exist_ok=__a )
         UpperCAmelCase_           =   {"source": "What is love ?", "target": "life"}
         UpperCAmelCase_           =   {"train": 12, "val": 2, "test": 2}
         for split in ["train", "test", "val"]:
              for field in ["source", "target"]:
                   UpperCAmelCase_           =   "\n".join([contents[field]] * n_lines[split] )
                   with open(os.path.join(__a   ,			f"""{split}.{field}""" )   ,			"w" ) as f:
                        f.write(__a )



    def 					_lowercase						(self    :    Optional[int]   ,			__a    :    int   ,			__a    :    str = "pytorch" ):
         UpperCAmelCase_           =   self.get_auto_remove_tmp_dir()
         UpperCAmelCase_           =   os.path.join(__a   ,			"output" )
         UpperCAmelCase_           =   os.path.join(__a   ,			"data" )
         self._create_dummy_data(data_dir=__a )

         UpperCAmelCase_           =   f"""
                --data_dir {data_dir} \
                --output_dir {output_dir} \
                --model_name_or_path facebook/rag-sequence-base \
                --model_type rag_sequence \
                --do_train \
                --do_predict \
                --n_val -1 \
                --val_check_interval 1.0 \
                --train_batch_size 2 \
                --eval_batch_size 1 \
                --max_source_length 25 \
                --max_target_length 25 \
                --val_max_target_length 25 \
                --test_max_target_length 25 \
                --label_smoothing 0.1 \
                --dropout 0.1 \
                --attention_dropout 0.1 \
                --weight_decay 0.001 \
                --adam_epsilon 1e-08 \
                --max_grad_norm 0.1 \
                --lr_scheduler polynomial \
                --learning_rate 3e-04 \
                --num_train_epochs 1 \
                --warmup_steps 4 \
                --gradient_accumulation_steps 1 \
                --distributed-port 8787 \
                --use_dummy_dataset 1 \
                --distributed_retriever {distributed_retriever} \
            """.split()

         if gpus > 0:
              testargs.append(f"""--gpus={gpus}""" )
              if is_apex_available():
                   testargs.append("--fp16" )
         else:
              testargs.append("--gpus=0" )
              testargs.append("--distributed_backend=ddp_cpu" )
              testargs.append("--num_processes=2" )

         UpperCAmelCase_           =   [sys.executable, str(Path(finetune_rag.__file__ ).resolve() )] + testargs
         execute_subprocess_async(__a   ,			env=self.get_env() )

         UpperCAmelCase_           =   os.path.join(__a   ,			"metrics.json" )
         with open(__a ) as f:
              UpperCAmelCase_           =   json.load(__a )
         return result



    @require_torch_gpu
    def 					_lowercase						(self    :    Optional[int] ):
         UpperCAmelCase_           =   self._run_finetune(gpus=1 )
         self.assertGreaterEqual(result["test"][0]["test_avg_em"]   ,			0.2 )



    @require_torch_multi_gpu
    def 					_lowercase						(self    :    Dict ):
         UpperCAmelCase_           =   self._run_finetune(gpus=2 )
         self.assertGreaterEqual(result["test"][0]["test_avg_em"]   ,			0.2 )



    @require_torch_gpu
    @require_ray
    def 					_lowercase						(self    :    Optional[int] ):
         UpperCAmelCase_           =   self._run_finetune(gpus=1   ,			distributed_retriever="ray" )
         self.assertGreaterEqual(result["test"][0]["test_avg_em"]   ,			0.2 )






    @require_torch_multi_gpu
    @require_ray
    def 					_lowercase						(self    :    Any ):
         UpperCAmelCase_           =   self._run_finetune(gpus=1   ,			distributed_retriever="ray" )
         self.assertGreaterEqual(result["test"][0]["test_avg_em"]   ,			0.2 )




