
import gc
import unittest

import numpy as np
import torch
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer

from diffusers import TransformeraDModel, VQDiffusionPipeline, VQDiffusionScheduler, VQModel
from diffusers.pipelines.vq_diffusion.pipeline_vq_diffusion import LearnedClassifierFreeSamplingEmbeddings
from diffusers.utils import load_numpy, slow, torch_device
from diffusers.utils.testing_utils import require_torch_gpu


UpperCAmelCase	      = False







class        A_   (			unittest.TestCase   ):




	'''simple docstring'''


	def 	SCREAMING_SNAKE_CASE__						(       self       ):
		# clean up the VRAM after each test
		super().tearDown()
		gc.collect()
		torch.cuda.empty_cache()





	@property
	def 	SCREAMING_SNAKE_CASE__						(       self       ):
		return 12





	@property
	def 	SCREAMING_SNAKE_CASE__						(       self       ):
		return 12





	@property
	def 	SCREAMING_SNAKE_CASE__						(       self       ):
		return 32





	@property
	def 	SCREAMING_SNAKE_CASE__						(       self       ):
		torch.manual_seed(0       )
		lowercase       =    VQModel(
		    block_out_channels=[32, 64]		,    in_channels=3		,    out_channels=3		,    down_block_types=['DownEncoderBlock2D', 'DownEncoderBlock2D']		,    up_block_types=['UpDecoderBlock2D', 'UpDecoderBlock2D']		,    latent_channels=3		,    num_vq_embeddings=self.num_embed		,    vq_embed_dim=3		,    )
		return model





	@property
	def 	SCREAMING_SNAKE_CASE__						(       self       ):
		lowercase       =    CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'       )
		return tokenizer





	@property
	def 	SCREAMING_SNAKE_CASE__						(       self       ):
		torch.manual_seed(0       )
		lowercase       =    CLIPTextConfig(
		    bos_token_id=0		,    eos_token_id=2		,    hidden_size=self.text_embedder_hidden_size		,    intermediate_size=37		,    layer_norm_eps=1E-05		,    num_attention_heads=4		,    num_hidden_layers=5		,    pad_token_id=1		,    vocab_size=1000		,    )
		return CLIPTextModel(snake_case       )





	@property
	def 	SCREAMING_SNAKE_CASE__						(       self       ):
		torch.manual_seed(0       )

		lowercase       =    12
		lowercase       =    12

		lowercase       =    {
		    'attention_bias': True,
		    'cross_attention_dim': 32,
		    'attention_head_dim': height * width,
		    'num_attention_heads': 1,
		    'num_vector_embeds': self.num_embed,
		    'num_embeds_ada_norm': self.num_embeds_ada_norm,
		    'norm_num_groups': 32,
		    'sample_size': width,
		    'activation_fn': 'geglu-approximate',
		}

		lowercase       =    TransformeraDModel(**snake_case       )
		return model





	def 	SCREAMING_SNAKE_CASE__						(       self       ):
		lowercase       =    'cpu'

		lowercase       =    self.dummy_vqvae
		lowercase       =    self.dummy_text_encoder
		lowercase       =    self.dummy_tokenizer
		lowercase       =    self.dummy_transformer
		lowercase       =    VQDiffusionScheduler(self.num_embed       )
		lowercase       =    LearnedClassifierFreeSamplingEmbeddings(learnable=snake_case       )

		lowercase       =    VQDiffusionPipeline(
		    vqvae=snake_case		,    text_encoder=snake_case		,    tokenizer=snake_case		,    transformer=snake_case		,    scheduler=snake_case		,    learned_classifier_free_sampling_embeddings=snake_case		,    )
		lowercase       =    pipe.to(snake_case       )
		pipe.set_progress_bar_config(disable=snake_case       )

		lowercase       =    'teddy bear playing in the pool'

		lowercase       =    torch.Generator(device=snake_case       ).manual_seed(0       )
		lowercase       =    pipe([prompt]		,    generator=snake_case		,    num_inference_steps=2		,    output_type='np'       )
		lowercase       =    output.images

		lowercase       =    torch.Generator(device=snake_case       ).manual_seed(0       )
		lowercase       =    pipe(
		    [prompt]		,    generator=snake_case		,    output_type='np'		,    return_dict=snake_case		,    num_inference_steps=2       )[0]

		lowercase       =    image[0, -3:, -3:, -1]
		lowercase       =    image_from_tuple[0, -3:, -3:, -1]

		assert image.shape == (1, 24, 24, 3)

		lowercase       =    np.array([0.6_551, 0.6_168, 0.5_008, 0.5_676, 0.5_659, 0.4_295, 0.6_073, 0.5_599, 0.4_992]       )

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





	def 	SCREAMING_SNAKE_CASE__						(       self       ):
		lowercase       =    'cpu'

		lowercase       =    self.dummy_vqvae
		lowercase       =    self.dummy_text_encoder
		lowercase       =    self.dummy_tokenizer
		lowercase       =    self.dummy_transformer
		lowercase       =    VQDiffusionScheduler(self.num_embed       )
		lowercase       =    LearnedClassifierFreeSamplingEmbeddings(
		    learnable=snake_case		,    hidden_size=self.text_embedder_hidden_size		,    length=tokenizer.model_max_length       )

		lowercase       =    VQDiffusionPipeline(
		    vqvae=snake_case		,    text_encoder=snake_case		,    tokenizer=snake_case		,    transformer=snake_case		,    scheduler=snake_case		,    learned_classifier_free_sampling_embeddings=snake_case		,    )
		lowercase       =    pipe.to(snake_case       )
		pipe.set_progress_bar_config(disable=snake_case       )

		lowercase       =    'teddy bear playing in the pool'

		lowercase       =    torch.Generator(device=snake_case       ).manual_seed(0       )
		lowercase       =    pipe([prompt]		,    generator=snake_case		,    num_inference_steps=2		,    output_type='np'       )
		lowercase       =    output.images

		lowercase       =    torch.Generator(device=snake_case       ).manual_seed(0       )
		lowercase       =    pipe(
		    [prompt]		,    generator=snake_case		,    output_type='np'		,    return_dict=snake_case		,    num_inference_steps=2       )[0]

		lowercase       =    image[0, -3:, -3:, -1]
		lowercase       =    image_from_tuple[0, -3:, -3:, -1]

		assert image.shape == (1, 24, 24, 3)

		lowercase       =    np.array([0.6_693, 0.6_075, 0.4_959, 0.5_701, 0.5_583, 0.4_333, 0.6_171, 0.5_684, 0.4_988]       )

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







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




	'''simple docstring'''


	def 	SCREAMING_SNAKE_CASE__						(       self       ):
		# clean up the VRAM after each test
		super().tearDown()
		gc.collect()
		torch.cuda.empty_cache()





	def 	SCREAMING_SNAKE_CASE__						(       self       ):
		lowercase       =    load_numpy(
		    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'
		    '/vq_diffusion/teddy_bear_pool_classifier_free_sampling.npy'       )

		lowercase       =    VQDiffusionPipeline.from_pretrained('microsoft/vq-diffusion-ithq'       )
		lowercase       =    pipeline.to(snake_case       )
		pipeline.set_progress_bar_config(disable=snake_case       )

		# requires GPU generator for gumbel softmax
		# don't use GPU generator in tests though
		lowercase       =    torch.Generator(device=snake_case       ).manual_seed(0       )
		lowercase       =    pipeline(
		    'teddy bear playing in the pool'		,    num_images_per_prompt=1		,    generator=snake_case		,    output_type='np'		,    )

		lowercase       =    output.images[0]

		assert image.shape == (256, 256, 3)
		assert np.abs(expected_image - image       ).max() < 2.0



# Copyright 2021 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from pathlib import Path

import torch

from ...utils import is_npu_available, is_xpu_available
from .config_args import ClusterConfig, default_json_config_file
from .config_utils import SubcommandHelpFormatter


UpperCAmelCase	      = '''Create a default config file for Accelerate with only a few flags set.'''






def 							UpperCAmelCase_		(							__SCREAMING_SNAKE_CASE="no" ,							__SCREAMING_SNAKE_CASE = default_json_config_file ,							__SCREAMING_SNAKE_CASE = False							):
	lowercase       =    Path(__SCREAMING_SNAKE_CASE							)
	path.parent.mkdir(parents=__SCREAMING_SNAKE_CASE ,							exist_ok=__SCREAMING_SNAKE_CASE							)
	if path.exists():
		print(
		    F'''Configuration already exists at {save_location}, will not override. Run `accelerate config` manually or pass a different `save_location`.'''							)
		return False
	lowercase       =    mixed_precision.lower()
	if mixed_precision not in ["no", "fp16", "bf16", "fp8"]:
		raise ValueError(
		    F'''`mixed_precision` should be one of \'no\', \'fp16\', \'bf16\', or \'fp8\'. Received {mixed_precision}'''							)
	lowercase       =    {
	    'compute_environment': 'LOCAL_MACHINE',
	    'mixed_precision': mixed_precision,
	}
	if torch.cuda.is_available():
		lowercase       =    torch.cuda.device_count()
		lowercase       =    num_gpus
		lowercase       =    False
		if num_gpus > 1:
			lowercase       =    'MULTI_GPU'
		else:
			lowercase       =    'NO'
	elif is_xpu_available() and use_xpu:
		lowercase       =    torch.xpu.device_count()
		lowercase       =    num_xpus
		lowercase       =    False
		if num_xpus > 1:
			lowercase       =    'MULTI_XPU'
		else:
			lowercase       =    'NO'
	elif is_npu_available():
		lowercase       =    torch.npu.device_count()
		lowercase       =    num_npus
		lowercase       =    False
		if num_npus > 1:
			lowercase       =    'MULTI_NPU'
		else:
			lowercase       =    'NO'
	else:
		lowercase       =    0
		lowercase       =    True
		lowercase       =    1
		lowercase       =    'NO'
	lowercase       =    ClusterConfig(**__SCREAMING_SNAKE_CASE							)
	config.to_json_file(__SCREAMING_SNAKE_CASE							)
	return path






def 							UpperCAmelCase_		(							__SCREAMING_SNAKE_CASE ,							__SCREAMING_SNAKE_CASE							):
	lowercase       =    parser.add_parser('default' ,							parents=__SCREAMING_SNAKE_CASE ,							help=__SCREAMING_SNAKE_CASE ,							formatter_class=__SCREAMING_SNAKE_CASE							)
	parser.add_argument(
	    '--config_file' ,							default=__SCREAMING_SNAKE_CASE ,							help=(
	        'The path to use to store the config file. Will default to a file named default_config.yaml in the cache '
	        'location, which is the content of the environment `HF_HOME` suffixed with \'accelerate\', or if you don\'t have '
	        'such an environment variable, your cache directory (\'~/.cache\' or the content of `XDG_CACHE_HOME`) suffixed '
	        'with \'huggingface\'.'
	    ) ,							dest='save_location' ,							)

	parser.add_argument(
	    '--mixed_precision' ,							choices=['no', 'fp16', 'bf16'] ,							type=__SCREAMING_SNAKE_CASE ,							help='Whether or not to use mixed precision training. '
	    'Choose between FP16 and BF16 (bfloat16) training. '
	    'BF16 training is only supported on Nvidia Ampere GPUs and PyTorch 1.10 or later.' ,							default='no' ,							)
	parser.set_defaults(func=__SCREAMING_SNAKE_CASE							)
	return parser






def 							UpperCAmelCase_		(							__SCREAMING_SNAKE_CASE							):
	lowercase       =    write_basic_config(args.mixed_precision ,							args.save_location							)
	if config_file:
		print(F'''accelerate configuration saved at {config_file}'''							)

