


'''simple docstring'''
# Copyright 2023 The HuggingFace Inc. 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.
import copy
import importlib.metadata
import json
import os
from dataclasses import dataclass
from typing import Any, Dict, Union

from packaging import version

from ..utils import is_torch_available, logging


if is_torch_available():
		import torch


__snake_case							      =     logging.get_logger(__name__)
@dataclass
class    lowercase	:

		"""simple docstring"""

		def __init__(		self    ,   UpperCamelCase_=False    ,   UpperCamelCase_=False    ,   UpperCamelCase_=6.0    ,   UpperCamelCase_=None    ,   UpperCamelCase_=False    ,   UpperCamelCase_=False    ,   UpperCamelCase_=None    ,   UpperCamelCase_="fp4"    ,   UpperCamelCase_=False    ,   **UpperCamelCase_    ,   ):

							'''simple docstring'''


							UpperCamelCase__   :List[str]												= load_in_abit
							UpperCamelCase__   :Any												= load_in_abit
							UpperCamelCase__   :Optional[int]												= llm_inta_threshold
							UpperCamelCase__   :Optional[Any]												= llm_inta_skip_modules
							UpperCamelCase__   :List[Any]												= llm_inta_enable_fpaa_cpu_offload
							UpperCamelCase__   :Optional[Any]												= llm_inta_has_fpaa_weight
							UpperCamelCase__   :Dict												= bnb_abit_quant_type
							UpperCamelCase__   :int												= bnb_abit_use_double_quant

							if bnb_abit_compute_dtype is None:
												UpperCamelCase__   :Union[str, Any]												= torch.floataa
							elif isinstance(UpperCamelCase_    ,   UpperCamelCase_							):
												UpperCamelCase__   :Union[str, Any]												= getattr(UpperCamelCase_    ,   UpperCamelCase_							)
							elif isinstance(UpperCamelCase_    ,   torch.dtype							):
												UpperCamelCase__   :Any												= bnb_abit_compute_dtype
							else:
												raise ValueError('''bnb_4bit_compute_dtype must be a string or a torch.dtype'''							)

							self.post_init()




		def     lowerCAmelCase__				(		self							):

							'''simple docstring'''


							if not isinstance(self.llm_inta_threshold    ,   UpperCamelCase_							):
												raise ValueError('''llm_int8_threshold must be a float'''							)

							if self.llm_inta_skip_modules is not None and not isinstance(self.llm_inta_skip_modules    ,   UpperCamelCase_							):
												raise ValueError('''llm_int8_skip_modules must be a list of strings'''							)
							if not isinstance(self.llm_inta_enable_fpaa_cpu_offload    ,   UpperCamelCase_							):
												raise ValueError('''llm_int8_enable_fp32_cpu_offload must be a boolean'''							)

							if not isinstance(self.llm_inta_has_fpaa_weight    ,   UpperCamelCase_							):
												raise ValueError('''llm_int8_has_fp16_weight must be a boolean'''							)

							if self.bnb_abit_compute_dtype is not None and not isinstance(self.bnb_abit_compute_dtype    ,   torch.dtype							):
												raise ValueError('''bnb_4bit_compute_dtype must be torch.dtype'''							)

							if not isinstance(self.bnb_abit_quant_type    ,   UpperCamelCase_							):
												raise ValueError('''bnb_4bit_quant_type must be a string'''							)

							if not isinstance(self.bnb_abit_use_double_quant    ,   UpperCamelCase_							):
												raise ValueError('''bnb_4bit_use_double_quant must be a boolean'''							)

							if self.load_in_abit and not version.parse(importlib.metadata.version('''bitsandbytes'''							)							) >= version.parse(
							    '''0.39.0'''							):
												raise ValueError(
												    '''4 bit quantization requires bitsandbytes>=0.39.0 - please upgrade your bitsandbytes version'''							)




		def     lowerCAmelCase__				(		self							):

							'''simple docstring'''


							return self.load_in_abit or self.load_in_abit




		def     lowerCAmelCase__				(		self							):

							'''simple docstring'''


							if self.load_in_abit:
												return "llm_int8"
							elif self.load_in_abit and self.bnb_abit_quant_type == "fp4":
												return "fp4"
							elif self.load_in_abit and self.bnb_abit_quant_type == "nf4":
												return "nf4"
							else:
												return None




		@classmethod
		def     lowerCAmelCase__				(		cls    ,   UpperCamelCase_    ,   UpperCamelCase_    ,   **UpperCamelCase_							):

							'''simple docstring'''


							UpperCamelCase__   :Optional[Any]												= cls(**UpperCamelCase_							)

							UpperCamelCase__   :int												= []
							for key, value in kwargs.items():
												if hasattr(UpperCamelCase_    ,   UpperCamelCase_							):
																	setattr(UpperCamelCase_    ,   UpperCamelCase_    ,   UpperCamelCase_							)
																	to_remove.append(UpperCamelCase_							)
							for key in to_remove:
												kwargs.pop(UpperCamelCase_    ,   UpperCamelCase_							)

							if return_unused_kwargs:
												return config, kwargs
							else:
												return config




		def     lowerCAmelCase__				(		self    ,   UpperCamelCase_							):

							'''simple docstring'''


							with open(UpperCamelCase_    ,   '''w'''    ,   encoding='''utf-8'''							) as writer:
												UpperCamelCase__   :Dict												= self.to_dict()
												UpperCamelCase__   :Any												= json.dumps(UpperCamelCase_    ,   indent=2    ,   sort_keys=UpperCamelCase_							) + '''\n'''

												writer.write(UpperCamelCase_							)




		def     lowerCAmelCase__				(		self							):

							'''simple docstring'''


							UpperCamelCase__   :Optional[Any]												= copy.deepcopy(self.__dict__							)
							UpperCamelCase__   :Dict												= str(output['''bnb_4bit_compute_dtype''']							).split('''.'''							)[1]

							return output




		def __repr__(		self							):

							'''simple docstring'''


							return F'''{self.__class__.__name__} {self.to_json_string()}'''




		def     lowerCAmelCase__				(		self    ,   UpperCamelCase_ = True							):

							'''simple docstring'''


							if use_diff is True:
												UpperCamelCase__   :Any												= self.to_diff_dict()
							else:
												UpperCamelCase__   :Optional[int]												= self.to_dict()
							return json.dumps(UpperCamelCase_    ,   indent=2    ,   sort_keys=UpperCamelCase_							) + "\n"




		def     lowerCAmelCase__				(		self							):

							'''simple docstring'''


							UpperCamelCase__   :Any												= self.to_dict()

							# get the default config dict
							UpperCamelCase__   :Optional[Any]												= BitsAndBytesConfig().to_dict()

							UpperCamelCase__   :int												= {}

							# only serialize values that differ from the default config
							for key, value in config_dict.items():
												if value != default_config_dict[key]:
																	UpperCamelCase__   :Union[str, Any]												= value

							return serializable_config_dict



import gc
import inspect
import unittest

import torch
from parameterized import parameterized

from diffusers import PriorTransformer
from diffusers.utils import floats_tensor, slow, torch_all_close, torch_device
from diffusers.utils.testing_utils import enable_full_determinism

from .test_modeling_common import ModelTesterMixin


enable_full_determinism()





class       A    (   A_					,				unittest.TestCase ):
							UpperCamelCase_   :							Any       =PriorTransformer
							UpperCamelCase_   :							List[str]       ='''hidden_states'''


							@property
							def 			_A       (self					):
									__lowercase=			4
									__lowercase=			8
									__lowercase=			7

									__lowercase=			floats_tensor((batch_size, embedding_dim)					).to(lowerCAmelCase					)

									__lowercase=			floats_tensor((batch_size, embedding_dim)					).to(lowerCAmelCase					)
									__lowercase=			floats_tensor((batch_size, num_embeddings, embedding_dim)					).to(lowerCAmelCase					)

									return {
									    "hidden_states": hidden_states,
									    "timestep": 2,
									    "proj_embedding": proj_embedding,
									    "encoder_hidden_states": encoder_hidden_states,
									}


							def 			_A       (self      ,				lowerCAmelCase=0					):
									torch.manual_seed(lowerCAmelCase					)
									__lowercase=			4
									__lowercase=			8
									__lowercase=			7

									__lowercase=			torch.randn((batch_size, embedding_dim)					).to(lowerCAmelCase					)

									__lowercase=			torch.randn((batch_size, embedding_dim)					).to(lowerCAmelCase					)
									__lowercase=			torch.randn((batch_size, num_embeddings, embedding_dim)					).to(lowerCAmelCase					)

									return {
									    "hidden_states": hidden_states,
									    "timestep": 2,
									    "proj_embedding": proj_embedding,
									    "encoder_hidden_states": encoder_hidden_states,
									}


							@property
							def 			_A       (self					):
									return (4, 8)


							@property
							def 			_A       (self					):
									return (4, 8)


							def 			_A       (self					):
									__lowercase=			{
									    'num_attention_heads': 2,
									    'attention_head_dim': 4,
									    'num_layers': 2,
									    'embedding_dim': 8,
									    'num_embeddings': 7,
									    'additional_embeddings': 4,
									}
									__lowercase=			self.dummy_input
									return init_dict, inputs_dict


							def 			_A       (self					):
									__lowercase,							__lowercase=			PriorTransformer.from_pretrained(
									    'hf-internal-testing/prior-dummy'      ,				output_loading_info=lowerCAmelCase					)
									self.assertIsNotNone(lowerCAmelCase					)
									self.assertEqual(len(loading_info['missing_keys']					)      ,				0					)

									model.to(lowerCAmelCase					)
									__lowercase=			model(**self.dummy_input					)[0]

									assert hidden_states is not None, "Make sure output is not None"


							def 			_A       (self					):
									__lowercase,							__lowercase=			self.prepare_init_args_and_inputs_for_common()

									__lowercase=			self.model_class(**lowerCAmelCase					)
									__lowercase=			inspect.signature(model.forward					)
									# signature.parameters is an OrderedDict => so arg_names order is deterministic
									__lowercase=			[*signature.parameters.keys()]

									__lowercase=			['hidden_states', 'timestep']
									self.assertListEqual(arg_names[:2]      ,				lowerCAmelCase					)







							def 			_A       (self					):
									__lowercase=			PriorTransformer.from_pretrained('hf-internal-testing/prior-dummy'					)
									__lowercase=			model.to(lowerCAmelCase					)

									if hasattr(lowerCAmelCase      ,				'set_default_attn_processor'					):
											model.set_default_attn_processor()

									__lowercase=			self.get_dummy_seed_input()

									with torch.no_grad():
											__lowercase=			model(**lowerCAmelCase					)[0]

									__lowercase=			output[0, :5].flatten().cpu()
									print(lowerCAmelCase					)

									# Since the VAE Gaussian prior's generator is seeded on the appropriate device,
									# the expected output slices are not the same for CPU and GPU.
									__lowercase=			torch.tensor([-1.34_36, -0.28_70, 0.75_38, 0.43_68, -0.02_39]					)
									self.assertTrue(torch_all_close(lowerCAmelCase      ,				lowerCAmelCase      ,				rtol=1E-2					)					)





@slow
class       A    (   unittest.TestCase ):


							def 			_A       (self      ,				lowerCAmelCase=1      ,				lowerCAmelCase=7_6_8      ,				lowerCAmelCase=7_7      ,				lowerCAmelCase=0					):
									torch.manual_seed(lowerCAmelCase					)
									__lowercase=			batch_size
									__lowercase=			embedding_dim
									__lowercase=			num_embeddings

									__lowercase=			torch.randn((batch_size, embedding_dim)					).to(lowerCAmelCase					)

									__lowercase=			torch.randn((batch_size, embedding_dim)					).to(lowerCAmelCase					)
									__lowercase=			torch.randn((batch_size, num_embeddings, embedding_dim)					).to(lowerCAmelCase					)

									return {
									    "hidden_states": hidden_states,
									    "timestep": 2,
									    "proj_embedding": proj_embedding,
									    "encoder_hidden_states": encoder_hidden_states,
									}


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







							@parameterized.expand(
							    [
							        # fmt: off
							        [1_3, [-0.58_61,  0.12_83, -0.09_31,  0.08_82,  0.44_76,  0.13_29, -0.04_98,  0.06_40]],
							        [3_7, [-0.49_13,  0.01_10, -0.04_83,  0.05_41,  0.49_54, -0.01_70,  0.03_54,  0.16_51]],
							        # fmt: on
							    ]					)
							def 			_A       (self      ,				lowerCAmelCase      ,				lowerCAmelCase					):
									__lowercase=			PriorTransformer.from_pretrained('kandinsky-community/kandinsky-2-1-prior'      ,				subfolder='prior'					)
									model.to(lowerCAmelCase					)
									__lowercase=			self.get_dummy_seed_input(seed=lowerCAmelCase					)

									with torch.no_grad():
											__lowercase=			model(**lowerCAmelCase					)[0]

									assert list(sample.shape					) == [1, 7_6_8]

									__lowercase=			sample[0, :8].flatten().cpu()
									print(lowerCAmelCase					)
									__lowercase=			torch.tensor(lowerCAmelCase					)

									assert torch_all_close(lowerCAmelCase      ,				lowerCAmelCase      ,				atol=1E-3					)






