
"""simple docstring"""



import os
from tempfile import TemporaryDirectory
from unittest import TestCase

import pytest
from absl.testing import parameterized

from datasets import config
from datasets.arrow_reader import HF_GCP_BASE_URL
from datasets.builder import DatasetBuilder
from datasets.dataset_dict import IterableDatasetDict
from datasets.iterable_dataset import IterableDataset
from datasets.load import dataset_module_factory, import_main_class
from datasets.utils.file_utils import cached_path


_snake_case          =						[
    {'''dataset''': '''wikipedia''', '''config_name''': '''20220301.de'''},
    {'''dataset''': '''wikipedia''', '''config_name''': '''20220301.en'''},
    {'''dataset''': '''wikipedia''', '''config_name''': '''20220301.fr'''},
    {'''dataset''': '''wikipedia''', '''config_name''': '''20220301.frr'''},
    {'''dataset''': '''wikipedia''', '''config_name''': '''20220301.it'''},
    {'''dataset''': '''wikipedia''', '''config_name''': '''20220301.simple'''},
    {'''dataset''': '''snli''', '''config_name''': '''plain_text'''},
    {'''dataset''': '''eli5''', '''config_name''': '''LFQA_reddit'''},
    {'''dataset''': '''wiki40b''', '''config_name''': '''en'''},
    {'''dataset''': '''wiki_dpr''', '''config_name''': '''psgs_w100.nq.compressed'''},
    {'''dataset''': '''wiki_dpr''', '''config_name''': '''psgs_w100.nq.no_index'''},
    {'''dataset''': '''wiki_dpr''', '''config_name''': '''psgs_w100.multiset.no_index'''},
    {'''dataset''': '''natural_questions''', '''config_name''': '''default'''},
]






def 	__snake_case  (    SCREAMING_SNAKE_CASE:					List[str]=True						):






							"""simple docstring"""



							if with_config:
														return [
														    {
														        "testcase_name": d["dataset"] + "/" + d["config_name"],
														        "dataset": d["dataset"],
														        "config_name": d["config_name"],
														    }
														    for d in DATASETS_ON_HF_GCP
														]
							else:
														return [
														    {"testcase_name": dataset, "dataset": dataset} for dataset in {d["dataset"] for d in DATASETS_ON_HF_GCP}
														]
@parameterized.named_parameters(list_datasets_on_hf_gcp_parameters(with_config=UpperCAmelCase							)							)
class        _SCREAMING_SNAKE_CASE  (  UpperCAmelCase							):






				'''simple docstring'''



				SCREAMING_SNAKE_CASE_:							List[str]							=					None
				SCREAMING_SNAKE_CASE_:							Optional[int]							=					None


				def 				__lowerCamelCase	(					self	:			Union[str, Any]				,     UpperCAmelCase_	:			Dict				,     UpperCAmelCase_	:			List[Any]  )		->  str:







											"""simple docstring"""




											with TemporaryDirectory() as tmp_dir:
																		_lowerCAmelCase  	=			dataset_module_factory(UpperCAmelCase_				,     cache_dir=UpperCAmelCase_  )

																		_lowerCAmelCase  	=			import_main_class(dataset_module.module_path				,     dataset=UpperCAmelCase_  )

																		_lowerCAmelCase  	=			builder_cls(
																		    cache_dir=UpperCAmelCase_				,     config_name=UpperCAmelCase_				,     hash=dataset_module.hash				,     )

																		_lowerCAmelCase  	=			'/'.join(
																		    [
																		        HF_GCP_BASE_URL,
																		        builder_instance._relative_data_dir(with_hash=UpperCAmelCase_  ).replace(os.sep				,     '/'  ),
																		        config.DATASET_INFO_FILENAME,
																		    ]  )
																		_lowerCAmelCase  	=			cached_path(UpperCAmelCase_				,     cache_dir=UpperCAmelCase_  )
																		self.assertTrue(os.path.exists(UpperCAmelCase_  )  )






@pytest.mark.integration
def 	__snake_case  (    SCREAMING_SNAKE_CASE:					Union[str, Any]						):






							"""simple docstring"""



							_lowerCAmelCase  	=			tmp_path_factory.mktemp('test_hf_gcp'						) / 'test_wikipedia_simple'
							_lowerCAmelCase  	=			dataset_module_factory('wikipedia'     ,  cache_dir=SCREAMING_SNAKE_CASE						)
							_lowerCAmelCase  	=			import_main_class(dataset_module.module_path						)
							_lowerCAmelCase  	=			builder_cls(
							    cache_dir=SCREAMING_SNAKE_CASE     ,  config_name='20220301.frr'     ,  hash=dataset_module.hash     ,  )
							# use the HF cloud storage, not the original download_and_prepare that uses apache-beam
							_lowerCAmelCase  	=			None
							builder_instance.download_and_prepare()
							_lowerCAmelCase  	=			builder_instance.as_dataset()
							assert ds






@pytest.mark.integration
def 	__snake_case  (    SCREAMING_SNAKE_CASE:					Optional[Any]						):






							"""simple docstring"""



							_lowerCAmelCase  	=			dataset_module_factory('wikipedia'     ,  cache_dir=SCREAMING_SNAKE_CASE						)
							_lowerCAmelCase  	=			import_main_class(dataset_module.module_path     ,  dataset=SCREAMING_SNAKE_CASE						)
							_lowerCAmelCase  	=			builder_cls(
							    cache_dir=SCREAMING_SNAKE_CASE     ,  config_name='20220301.frr'     ,  hash=dataset_module.hash     ,  )
							_lowerCAmelCase  	=			builder_instance.as_streaming_dataset()
							assert ds
							assert isinstance(SCREAMING_SNAKE_CASE     ,  SCREAMING_SNAKE_CASE						)
							assert "train" in ds
							assert isinstance(ds['train']     ,  SCREAMING_SNAKE_CASE						)
							assert next(iter(ds['train']						)						)








"""simple docstring"""



import argparse

import intel_extension_for_pytorch as ipex
import torch

from diffusers import DPMSolverMultistepScheduler, StableDiffusionPipeline


_snake_case          =						argparse.ArgumentParser('''Stable Diffusion script with intel optimization''', add_help=False)
parser.add_argument('''--dpm''', action='''store_true''', help='''Enable DPMSolver or not''')
parser.add_argument('''--steps''', default=None, type=int, help='''Num inference steps''')
_snake_case          =						parser.parse_args()


_snake_case          =						'''cpu'''
_snake_case          =						'''a lovely <dicoo> in red dress and hat, in the snowly and brightly night, with many brighly buildings'''

_snake_case          =						'''path-to-your-trained-model'''
_snake_case          =						StableDiffusionPipeline.from_pretrained(model_id)
if args.dpm:
	_snake_case          =						DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
_snake_case          =						pipe.to(device)

# to channels last
_snake_case          =						pipe.unet.to(memory_format=torch.channels_last)
_snake_case          =						pipe.vae.to(memory_format=torch.channels_last)
_snake_case          =						pipe.text_encoder.to(memory_format=torch.channels_last)
if pipe.requires_safety_checker:
	_snake_case          =						pipe.safety_checker.to(memory_format=torch.channels_last)

# optimize with ipex
_snake_case          =						torch.randn(2, 4, 6_4, 6_4)
_snake_case          =						torch.rand(1) * 9_9_9
_snake_case          =						torch.randn(2, 7_7, 7_6_8)
_snake_case          =						(sample, timestep, encoder_hidden_status)
try:
	_snake_case          =						ipex.optimize(pipe.unet.eval(), dtype=torch.bfloataa, inplace=True, sample_input=input_example)
except Exception:
	_snake_case          =						ipex.optimize(pipe.unet.eval(), dtype=torch.bfloataa, inplace=True)
_snake_case          =						ipex.optimize(pipe.vae.eval(), dtype=torch.bfloataa, inplace=True)
_snake_case          =						ipex.optimize(pipe.text_encoder.eval(), dtype=torch.bfloataa, inplace=True)
if pipe.requires_safety_checker:
	_snake_case          =						ipex.optimize(pipe.safety_checker.eval(), dtype=torch.bfloataa, inplace=True)

# compute
_snake_case          =						6_6_6
_snake_case          =						torch.Generator(device).manual_seed(seed)
_snake_case          =						{'''generator''': generator}
if args.steps is not None:
	_snake_case          =						args.steps

with torch.cpu.amp.autocast(enabled=True, dtype=torch.bfloataa):
	_snake_case          =						pipe(prompt, **generate_kwargs).images[0]

# save image
image.save('''generated.png''')






