


"""simple docstring"""
import tempfile

import numpy as np
import torch
from transformers import AutoTokenizer, TaEncoderModel

from diffusers import DDPMScheduler, UNetaDConditionModel
from diffusers.models.attention_processor import AttnAddedKVProcessor
from diffusers.pipelines.deepfloyd_if import IFWatermarker
from diffusers.utils.testing_utils import torch_device

from ..test_pipelines_common import to_np





class        __A       :





							'''simple docstring'''

							def 						lowerCAmelCase    (			self							: Optional[int]    )   ->Union[str, Any]:



														"""simple docstring"""



														torch.manual_seed(0    )
														snake_case_          =						TaEncoderModel.from_pretrained("""hf-internal-testing/tiny-random-t5"""    )

														torch.manual_seed(0    )
														snake_case_          =						AutoTokenizer.from_pretrained("""hf-internal-testing/tiny-random-t5"""    )

														torch.manual_seed(0    )
														snake_case_          =						UNetaDConditionModel(
														    sample_size=32						,    layers_per_block=1						,    block_out_channels=[32, 64]						,    down_block_types=[
														        """ResnetDownsampleBlock2D""",
														        """SimpleCrossAttnDownBlock2D""",
														    ]						,    mid_block_type="""UNetMidBlock2DSimpleCrossAttn"""						,    up_block_types=["""SimpleCrossAttnUpBlock2D""", """ResnetUpsampleBlock2D"""]						,    in_channels=3						,    out_channels=6						,    cross_attention_dim=32						,    encoder_hid_dim=32						,    attention_head_dim=8						,    addition_embed_type="""text"""						,    addition_embed_type_num_heads=2						,    cross_attention_norm="""group_norm"""						,    resnet_time_scale_shift="""scale_shift"""						,    act_fn="""gelu"""						,    )
														unet.set_attn_processor(AttnAddedKVProcessor()    )  # For reproducibility tests

														torch.manual_seed(0    )
														snake_case_          =						DDPMScheduler(
														    num_train_timesteps=1_000						,    beta_schedule="""squaredcos_cap_v2"""						,    beta_start=0.0_001						,    beta_end=0.02						,    thresholding=UpperCAmelCase_						,    dynamic_thresholding_ratio=0.95						,    sample_max_value=1.0						,    prediction_type="""epsilon"""						,    variance_type="""learned_range"""						,    )

														torch.manual_seed(0    )
														snake_case_          =						IFWatermarker()

														return {
														    "text_encoder": text_encoder,
														    "tokenizer": tokenizer,
														    "unet": unet,
														    "scheduler": scheduler,
														    "watermarker": watermarker,
														    "safety_checker": None,
														    "feature_extractor": None,
														}



							def 						lowerCAmelCase    (			self							: Dict    )   ->List[str]:



														"""simple docstring"""



														torch.manual_seed(0    )
														snake_case_          =						TaEncoderModel.from_pretrained("""hf-internal-testing/tiny-random-t5"""    )

														torch.manual_seed(0    )
														snake_case_          =						AutoTokenizer.from_pretrained("""hf-internal-testing/tiny-random-t5"""    )

														torch.manual_seed(0    )
														snake_case_          =						UNetaDConditionModel(
														    sample_size=32						,    layers_per_block=[1, 2]						,    block_out_channels=[32, 64]						,    down_block_types=[
														        """ResnetDownsampleBlock2D""",
														        """SimpleCrossAttnDownBlock2D""",
														    ]						,    mid_block_type="""UNetMidBlock2DSimpleCrossAttn"""						,    up_block_types=["""SimpleCrossAttnUpBlock2D""", """ResnetUpsampleBlock2D"""]						,    in_channels=6						,    out_channels=6						,    cross_attention_dim=32						,    encoder_hid_dim=32						,    attention_head_dim=8						,    addition_embed_type="""text"""						,    addition_embed_type_num_heads=2						,    cross_attention_norm="""group_norm"""						,    resnet_time_scale_shift="""scale_shift"""						,    act_fn="""gelu"""						,    class_embed_type="""timestep"""						,    mid_block_scale_factor=1.414						,    time_embedding_act_fn="""gelu"""						,    time_embedding_dim=32						,    )
														unet.set_attn_processor(AttnAddedKVProcessor()    )  # For reproducibility tests

														torch.manual_seed(0    )
														snake_case_          =						DDPMScheduler(
														    num_train_timesteps=1_000						,    beta_schedule="""squaredcos_cap_v2"""						,    beta_start=0.0_001						,    beta_end=0.02						,    thresholding=UpperCAmelCase_						,    dynamic_thresholding_ratio=0.95						,    sample_max_value=1.0						,    prediction_type="""epsilon"""						,    variance_type="""learned_range"""						,    )

														torch.manual_seed(0    )
														snake_case_          =						DDPMScheduler(
														    num_train_timesteps=1_000						,    beta_schedule="""squaredcos_cap_v2"""						,    beta_start=0.0_001						,    beta_end=0.02						,    )

														torch.manual_seed(0    )
														snake_case_          =						IFWatermarker()

														return {
														    "text_encoder": text_encoder,
														    "tokenizer": tokenizer,
														    "unet": unet,
														    "scheduler": scheduler,
														    "image_noising_scheduler": image_noising_scheduler,
														    "watermarker": watermarker,
														    "safety_checker": None,
														    "feature_extractor": None,
														}



							def 						lowerCAmelCase    (			self							: str    )   ->List[Any]:



														"""simple docstring"""



														snake_case_          =						self.get_dummy_components()
														snake_case_          =						self.pipeline_class(**UpperCAmelCase_    )
														pipe.to(UpperCAmelCase_    )
														pipe.set_progress_bar_config(disable=UpperCAmelCase_    )

														snake_case_          =						self.get_dummy_inputs(UpperCAmelCase_    )

														snake_case_          =						inputs['prompt']
														snake_case_          =						inputs['generator']
														snake_case_          =						inputs['num_inference_steps']
														snake_case_          =						inputs['output_type']

														if "image" in inputs:
																					snake_case_          =						inputs['image']
														else:
																					snake_case_          =						None

														if "mask_image" in inputs:
																					snake_case_          =						inputs['mask_image']
														else:
																					snake_case_          =						None

														if "original_image" in inputs:
																					snake_case_          =						inputs['original_image']
														else:
																					snake_case_          =						None

														snake_case_          =						pipe.encode_prompt(UpperCAmelCase_    )

														# inputs with prompt converted to embeddings
														snake_case_          =						{
														    'prompt_embeds': prompt_embeds,
														    'negative_prompt_embeds': negative_prompt_embeds,
														    'generator': generator,
														    'num_inference_steps': num_inference_steps,
														    'output_type': output_type,
														}

														if image is not None:
																					snake_case_          =						image

														if mask_image is not None:
																					snake_case_          =						mask_image

														if original_image is not None:
																					snake_case_          =						original_image

														# set all optional components to None
														for optional_component in pipe._optional_components:
																					setattr(UpperCAmelCase_						,    UpperCAmelCase_						,    UpperCAmelCase_    )

														snake_case_          =						pipe(**UpperCAmelCase_    )[0]

														with tempfile.TemporaryDirectory() as tmpdir:
																					pipe.save_pretrained(UpperCAmelCase_    )
																					snake_case_          =						self.pipeline_class.from_pretrained(UpperCAmelCase_    )
																					pipe_loaded.to(UpperCAmelCase_    )
																					pipe_loaded.set_progress_bar_config(disable=UpperCAmelCase_    )

														pipe_loaded.unet.set_attn_processor(AttnAddedKVProcessor()    )  # For reproducibility tests

														for optional_component in pipe._optional_components:
																					self.assertTrue(
																					    getattr(UpperCAmelCase_						,    UpperCAmelCase_    ) is None						,    F"""`{optional_component}` did not stay set to None after loading."""						,    )

														snake_case_          =						self.get_dummy_inputs(UpperCAmelCase_    )

														snake_case_          =						inputs['generator']
														snake_case_          =						inputs['num_inference_steps']
														snake_case_          =						inputs['output_type']

														# inputs with prompt converted to embeddings
														snake_case_          =						{
														    'prompt_embeds': prompt_embeds,
														    'negative_prompt_embeds': negative_prompt_embeds,
														    'generator': generator,
														    'num_inference_steps': num_inference_steps,
														    'output_type': output_type,
														}

														if image is not None:
																					snake_case_          =						image

														if mask_image is not None:
																					snake_case_          =						mask_image

														if original_image is not None:
																					snake_case_          =						original_image

														snake_case_          =						pipe_loaded(**UpperCAmelCase_    )[0]

														snake_case_          =						np.abs(to_np(UpperCAmelCase_    ) - to_np(UpperCAmelCase_    )    ).max()
														self.assertLess(UpperCAmelCase_						,    1E-4    )



							def 						lowerCAmelCase    (			self							: List[Any]    )   ->Union[str, Any]:



														"""simple docstring"""



														snake_case_          =						self.get_dummy_components()
														snake_case_          =						self.pipeline_class(**UpperCAmelCase_    )
														pipe.to(UpperCAmelCase_    )
														pipe.set_progress_bar_config(disable=UpperCAmelCase_    )

														snake_case_          =						self.get_dummy_inputs(UpperCAmelCase_    )
														snake_case_          =						pipe(**UpperCAmelCase_    )[0]

														with tempfile.TemporaryDirectory() as tmpdir:
																					pipe.save_pretrained(UpperCAmelCase_    )
																					snake_case_          =						self.pipeline_class.from_pretrained(UpperCAmelCase_    )
																					pipe_loaded.to(UpperCAmelCase_    )
																					pipe_loaded.set_progress_bar_config(disable=UpperCAmelCase_    )

														pipe_loaded.unet.set_attn_processor(AttnAddedKVProcessor()    )  # For reproducibility tests

														snake_case_          =						self.get_dummy_inputs(UpperCAmelCase_    )
														snake_case_          =						pipe_loaded(**UpperCAmelCase_    )[0]

														snake_case_          =						np.abs(to_np(UpperCAmelCase_    ) - to_np(UpperCAmelCase_    )    ).max()
														self.assertLess(UpperCAmelCase_						,    1E-4    )







"""simple docstring"""
import ast
import os
import re
import shutil
import tempfile
import unittest
from unittest import mock

import torch

from accelerate.test_utils.examples import compare_against_test
from accelerate.test_utils.testing import TempDirTestCase, require_trackers, run_command, slow
from accelerate.utils import write_basic_config


# DataLoaders built from `test_samples/MRPC` for quick testing
# Should mock `{script_name}.get_dataloaders` via:
# @mock.patch("{script_name}.get_dataloaders", mocked_dataloaders)

__SCREAMING_SNAKE_CASE							:      Tuple						=      [
    'cross_validation.py',
    'gradient_accumulation.py',
    'local_sgd.py',
    'multi_process_metrics.py',
    'memory.py',
    'automatic_gradient_accumulation.py',
    'fsdp_with_peak_mem_tracking.py',
    'deepspeed_with_config_support.py',
    'megatron_lm_gpt_pretraining.py',
]





class        __A       (unittest.TestCase):





							'''simple docstring'''

							def 						lowerCAmelCase    (			self							: str						,    UpperCAmelCase_							: str						,    UpperCAmelCase_							: bool						,    UpperCAmelCase_							: str = None						,    UpperCAmelCase_							: list = None    )   ->List[Any]:



														"""simple docstring"""



														snake_case_          =						None
														snake_case_          =						os.path.abspath(os.path.join("""examples"""						,    """by_feature"""    )    )
														snake_case_          =						os.path.abspath("""examples"""    )
														for item in os.listdir(UpperCAmelCase_    ):
																					if item not in EXCLUDE_EXAMPLES:
																												snake_case_          =						os.path.join(UpperCAmelCase_						,    UpperCAmelCase_    )
																												if os.path.isfile(UpperCAmelCase_    ) and ".py" in item_path:
																																			with self.subTest(
																																			    tested_script=UpperCAmelCase_						,    feature_script=UpperCAmelCase_						,    tested_section="""main()""" if parser_only else """training_function()"""						,    ):
																																										snake_case_          =						compare_against_test(
																																										    os.path.join(UpperCAmelCase_						,    UpperCAmelCase_    )						,    UpperCAmelCase_						,    UpperCAmelCase_						,    UpperCAmelCase_    )
																																										snake_case_          =						"""\n""".join(UpperCAmelCase_    )
																																										if special_strings is not None:
																																																	for string in special_strings:
																																																								snake_case_          =						diff.replace(UpperCAmelCase_						,    """"""    )
																																										self.assertEqual(UpperCAmelCase_						,    """"""    )



							def 						lowerCAmelCase    (			self							: Tuple    )   ->List[Any]:



														"""simple docstring"""



														self.one_complete_example("""complete_nlp_example.py"""						,    UpperCAmelCase_    )
														self.one_complete_example("""complete_nlp_example.py"""						,    UpperCAmelCase_    )



							def 						lowerCAmelCase    (			self							: Tuple    )   ->Dict:



														"""simple docstring"""



														snake_case_          =						os.path.abspath(os.path.join("""examples"""						,    """cv_example.py"""    )    )
														snake_case_          =						[
														    """ """ * 16 + """{\n\n""",
														    """ """ * 20 + """\"accuracy\": eval_metric[\"accuracy\"],\n\n""",
														    """ """ * 20 + """\"f1\": eval_metric[\"f1\"],\n\n""",
														    """ """ * 20 + """\"train_loss\": total_loss.item() / len(train_dataloader),\n\n""",
														    """ """ * 20 + """\"epoch\": epoch,\n\n""",
														    """ """ * 16 + """},\n\n""",
														    """ """ * 16 + """step=epoch,\n""",
														    """ """ * 12,
														    """ """ * 8 + """for step, batch in enumerate(active_dataloader):\n""",
														]
														self.one_complete_example("""complete_cv_example.py"""						,    UpperCAmelCase_						,    UpperCAmelCase_						,    UpperCAmelCase_    )
														self.one_complete_example("""complete_cv_example.py"""						,    UpperCAmelCase_						,    UpperCAmelCase_						,    UpperCAmelCase_    )





@mock.patch.dict(os.environ  ,						{"""TESTING_MOCKED_DATALOADERS""": """1"""})
class        __A       (snake_case__):





							'''simple docstring'''

							__lowercase:						str							 =      False



							@classmethod
							def 						lowerCAmelCase    (			cls							: Any    )   ->List[str]:



														"""simple docstring"""



														super().setUpClass()
														snake_case_          =						tempfile.mkdtemp()
														snake_case_          =						os.path.join(cls._tmpdir						,    """default_config.yml"""    )

														write_basic_config(save_location=cls.configPath    )
														snake_case_          =						["""accelerate""", """launch""", """--config_file""", cls.configPath]



							@classmethod
							def 						lowerCAmelCase    (			cls							: List[str]    )   ->int:



														"""simple docstring"""



														super().tearDownClass()
														shutil.rmtree(cls._tmpdir    )



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



														"""simple docstring"""



														snake_case_          =						F"""
        examples/by_feature/checkpointing.py
        --checkpointing_steps epoch
        --output_dir {self.tmpdir}
        """.split()
														run_command(self._launch_args + testargs    )
														self.assertTrue(os.path.exists(os.path.join(self.tmpdir						,    """epoch_0"""    )    )    )



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



														"""simple docstring"""



														snake_case_          =						F"""
        examples/by_feature/checkpointing.py
        --checkpointing_steps 1
        --output_dir {self.tmpdir}
        """.split()
														snake_case_          =						run_command(self._launch_args + testargs    )
														self.assertTrue(os.path.exists(os.path.join(self.tmpdir						,    """step_2"""    )    )    )



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



														"""simple docstring"""



														snake_case_          =						F"""
        examples/by_feature/checkpointing.py
        --resume_from_checkpoint {os.path.join(self.tmpdir						,    "epoch_0"    )}
        """.split()
														snake_case_          =						run_command(self._launch_args + testargs						,    return_stdout=UpperCAmelCase_    )
														self.assertNotIn("""epoch 0:"""						,    UpperCAmelCase_    )
														self.assertIn("""epoch 1:"""						,    UpperCAmelCase_    )



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



														"""simple docstring"""



														snake_case_          =						F"""
        examples/by_feature/checkpointing.py
        --resume_from_checkpoint {os.path.join(self.tmpdir						,    "step_2"    )}
        """.split()
														snake_case_          =						run_command(self._launch_args + testargs						,    return_stdout=UpperCAmelCase_    )
														if torch.cuda.is_available():
																					snake_case_          =						torch.cuda.device_count()
														else:
																					snake_case_          =						1
														if num_processes > 1:
																					self.assertNotIn("""epoch 0:"""						,    UpperCAmelCase_    )
																					self.assertIn("""epoch 1:"""						,    UpperCAmelCase_    )
														else:
																					self.assertIn("""epoch 0:"""						,    UpperCAmelCase_    )
																					self.assertIn("""epoch 1:"""						,    UpperCAmelCase_    )



							@slow
							def 						lowerCAmelCase    (			self							: Dict    )   ->Dict:



														"""simple docstring"""



														snake_case_          =						"""
        examples/by_feature/cross_validation.py
        --num_folds 2
        """.split()
														with mock.patch.dict(os.environ						,    {"""TESTING_MOCKED_DATALOADERS""": """0"""}    ):
																					snake_case_          =						run_command(self._launch_args + testargs						,    return_stdout=UpperCAmelCase_    )
																					snake_case_          =						re.findall("""({.+})"""						,    UpperCAmelCase_    )
																					snake_case_          =						[r for r in results if """accuracy""" in r][-1]
																					snake_case_          =						ast.literal_eval(UpperCAmelCase_    )
																					self.assertGreaterEqual(results["""accuracy"""]						,    0.75    )



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



														"""simple docstring"""



														snake_case_          =						["""examples/by_feature/multi_process_metrics.py"""]
														run_command(self._launch_args + testargs    )



							@require_trackers
							@mock.patch.dict(os.environ						,    {"""WANDB_MODE""": """offline"""}    )
							def 						lowerCAmelCase    (			self							: Dict    )   ->Optional[Any]:



														"""simple docstring"""



														with tempfile.TemporaryDirectory() as tmpdir:
																					snake_case_          =						F"""
            examples/by_feature/tracking.py
            --with_tracking
            --project_dir {tmpdir}
            """.split()
																					run_command(self._launch_args + testargs    )
																					self.assertTrue(os.path.exists(os.path.join(UpperCAmelCase_						,    """tracking"""    )    )    )



							def 						lowerCAmelCase    (			self							: List[str]    )   ->Union[str, Any]:



														"""simple docstring"""



														snake_case_          =						["""examples/by_feature/gradient_accumulation.py"""]
														run_command(self._launch_args + testargs    )



							def 						lowerCAmelCase    (			self							: str    )   ->Dict:



														"""simple docstring"""



														snake_case_          =						["""examples/by_feature/local_sgd.py"""]
														run_command(self._launch_args + testargs    )



