


'''simple docstring'''
from typing import Optional, Union

import torch
from torch import nn
from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss

from ...activations import ACTaFN
from ...modeling_outputs import BaseModelOutputWithPoolingAndNoAttention, ImageClassifierOutputWithNoAttention
from ...modeling_utils import PreTrainedModel
from ...utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, logging
from .configuration_mobilenet_va import MobileNetVaConfig


__snake_case											=logging.get_logger(__name__)


# General docstring
__snake_case											='''MobileNetV1Config'''

# Base docstring
__snake_case											='''google/mobilenet_v1_1.0_224'''
__snake_case											=[1, 1024, 7, 7]

# Image classification docstring
__snake_case											='''google/mobilenet_v1_1.0_224'''
__snake_case											='''tabby, tabby cat'''


__snake_case											=[
    '''google/mobilenet_v1_1.0_224''',
    '''google/mobilenet_v1_0.75_192''',
    # See all MobileNetV1 models at https://huggingface.co/models?filter=mobilenet_v1
]


def       a				(      __a			, __a			, __a=None       )  ->     List[str]:



					'''simple docstring'''






					UpperCamelCase__       :Optional[int]							 =			{}

					if isinstance(a__			, a__       ):
										UpperCamelCase__       :List[str]							 =			model.mobilenet_va
					else:
										UpperCamelCase__       :Tuple							 =			model

					UpperCamelCase__       :Optional[Any]							 =			'''MobilenetV1/Conv2d_0/'''
					UpperCamelCase__       :Any							 =			backbone.conv_stem.convolution.weight
					UpperCamelCase__       :List[str]							 =			backbone.conv_stem.normalization.bias
					UpperCamelCase__       :Dict							 =			backbone.conv_stem.normalization.weight
					UpperCamelCase__       :Any							 =			backbone.conv_stem.normalization.running_mean
					UpperCamelCase__       :str							 =			backbone.conv_stem.normalization.running_var

					for i in range(13       ):
										UpperCamelCase__       :Any							 =			i + 1
										UpperCamelCase__       :Any							 =			i * 2

										UpperCamelCase__       :int							 =			backbone.layer[pt_index]
										UpperCamelCase__       :Union[str, Any]							 =			f'''MobilenetV1/Conv2d_{tf_index}_depthwise/'''
										UpperCamelCase__       :Dict							 =			pointer.convolution.weight
										UpperCamelCase__       :str							 =			pointer.normalization.bias
										UpperCamelCase__       :Any							 =			pointer.normalization.weight
										UpperCamelCase__       :Optional[int]							 =			pointer.normalization.running_mean
										UpperCamelCase__       :int							 =			pointer.normalization.running_var

										UpperCamelCase__       :Dict							 =			backbone.layer[pt_index + 1]
										UpperCamelCase__       :Dict							 =			f'''MobilenetV1/Conv2d_{tf_index}_pointwise/'''
										UpperCamelCase__       :List[str]							 =			pointer.convolution.weight
										UpperCamelCase__       :Optional[int]							 =			pointer.normalization.bias
										UpperCamelCase__       :Optional[int]							 =			pointer.normalization.weight
										UpperCamelCase__       :Any							 =			pointer.normalization.running_mean
										UpperCamelCase__       :Union[str, Any]							 =			pointer.normalization.running_var

					if isinstance(a__			, a__       ):
										UpperCamelCase__       :int							 =			'''MobilenetV1/Logits/Conv2d_1c_1x1/'''
										UpperCamelCase__       :List[str]							 =			model.classifier.weight
										UpperCamelCase__       :Tuple							 =			model.classifier.bias

					return tf_to_pt_map


def       a				(      __a			, __a			, __a       )  ->     Any:



					'''simple docstring'''






					try:
										import numpy as np
										import tensorflow as tf
					except ImportError:
										logger.error(
										    '''Loading a TensorFlow models in PyTorch, requires TensorFlow to be installed. Please see '''
										    '''https://www.tensorflow.org/install/ for installation instructions.'''       )
										raise

					# Load weights from TF model
					UpperCamelCase__       :Optional[Any]							 =			tf.train.list_variables(a__       )
					UpperCamelCase__       :List[str]							 =			{}
					for name, shape in init_vars:
										logger.info(f'''Loading TF weight {name} with shape {shape}'''       )
										UpperCamelCase__       :int							 =			tf.train.load_variable(a__			, a__       )
										UpperCamelCase__       :Optional[Any]							 =			array

					# Build TF to PyTorch weights loading map
					UpperCamelCase__       :List[Any]							 =			_build_tf_to_pytorch_map(a__			, a__			, a__       )

					for name, pointer in tf_to_pt_map.items():
										logger.info(f'''Importing {name}'''       )
										if name not in tf_weights:
															logger.info(f'''{name} not in tf pre-trained weights, skipping'''       )
															continue

										UpperCamelCase__       :Optional[int]							 =			tf_weights[name]

										if "depthwise_weights" in name:
															logger.info('''Transposing depthwise'''       )
															UpperCamelCase__       :int							 =			np.transpose(a__			, (2, 3, 0, 1)       )
										elif "weights" in name:
															logger.info('''Transposing'''       )
															if len(pointer.shape       ) == 2:  # copying into linear layer
																				UpperCamelCase__       :int							 =			array.squeeze().transpose()
															else:
																				UpperCamelCase__       :str							 =			np.transpose(a__			, (3, 2, 0, 1)       )

										if pointer.shape != array.shape:
															raise ValueError(f'''Pointer shape {pointer.shape} and array shape {array.shape} mismatched'''       )

										logger.info(f'''Initialize PyTorch weight {name} {array.shape}'''       )
										UpperCamelCase__       :List[str]							 =			torch.from_numpy(a__       )

										tf_weights.pop(a__			, a__       )
										tf_weights.pop(name + '''/RMSProp'''			, a__       )
										tf_weights.pop(name + '''/RMSProp_1'''			, a__       )
										tf_weights.pop(name + '''/ExponentialMovingAverage'''			, a__       )

					logger.info(f'''Weights not copied to PyTorch model: {", ".join(tf_weights.keys()       )}'''       )
					return model


def       a				(      __a			, __a       )  ->     Dict:



					'''simple docstring'''






					UpperCamelCase__ ,    UpperCamelCase__       :Dict							 =			features.shape[-2:]
					UpperCamelCase__ ,    UpperCamelCase__       :List[str]							 =			conv_layer.stride
					UpperCamelCase__ ,    UpperCamelCase__       :Optional[Any]							 =			conv_layer.kernel_size

					if in_height % stride_height == 0:
										UpperCamelCase__       :str							 =			max(kernel_height - stride_height			, 0       )
					else:
										UpperCamelCase__       :Dict							 =			max(kernel_height - (in_height % stride_height)			, 0       )

					if in_width % stride_width == 0:
										UpperCamelCase__       :Any							 =			max(kernel_width - stride_width			, 0       )
					else:
										UpperCamelCase__       :Optional[Any]							 =			max(kernel_width - (in_width % stride_width)			, 0       )

					UpperCamelCase__       :Any							 =			pad_along_width // 2
					UpperCamelCase__       :int							 =			pad_along_width - pad_left
					UpperCamelCase__       :Dict							 =			pad_along_height // 2
					UpperCamelCase__       :List[str]							 =			pad_along_height - pad_top

					UpperCamelCase__       :Tuple							 =			(pad_left, pad_right, pad_top, pad_bottom)
					return nn.functional.pad(a__			, a__			, '''constant'''			, 0.0       )
class        lowercase     (							nn.Module ):

		"""simple docstring"""

		def __init__(		self   ,       UpperCamelCase_   ,       UpperCamelCase_   ,       UpperCamelCase_   ,       UpperCamelCase_   ,       UpperCamelCase_ = 1   ,       UpperCamelCase_ = 1   ,       UpperCamelCase_ = False   ,       UpperCamelCase_ = True   ,       UpperCamelCase_ = True   ,       ):

							'''simple docstring'''


							super().__init__()
							UpperCamelCase__       :int							 =			config

							if in_channels % groups != 0:
												raise ValueError(F'''Input channels ({in_channels}) are not divisible by {groups} groups.'''							)
							if out_channels % groups != 0:
												raise ValueError(F'''Output channels ({out_channels}) are not divisible by {groups} groups.'''							)

							UpperCamelCase__       :Dict							 =			0 if config.tf_padding else int((kernel_size - 1) / 2							)

							UpperCamelCase__       :Any							 =			nn.Convad(
							    in_channels=__lowerCAmelCase   ,       out_channels=__lowerCAmelCase   ,       kernel_size=__lowerCAmelCase   ,       stride=__lowerCAmelCase   ,       padding=__lowerCAmelCase   ,       groups=__lowerCAmelCase   ,       bias=__lowerCAmelCase   ,       padding_mode='''zeros'''   ,       )

							if use_normalization:
												UpperCamelCase__       :Optional[Any]							 =			nn.BatchNormad(
												    num_features=__lowerCAmelCase   ,       eps=config.layer_norm_eps   ,       momentum=0.9997   ,       affine=__lowerCAmelCase   ,       track_running_stats=__lowerCAmelCase   ,       )
							else:
												UpperCamelCase__       :Optional[int]							 =			None

							if use_activation:
												if isinstance(__lowerCAmelCase   ,       __lowerCAmelCase							):
																	UpperCamelCase__       :Optional[int]							 =			ACTaFN[use_activation]
												elif isinstance(config.hidden_act   ,       __lowerCAmelCase							):
																	UpperCamelCase__       :List[Any]							 =			ACTaFN[config.hidden_act]
												else:
																	UpperCamelCase__       :Optional[Any]							 =			config.hidden_act
							else:
												UpperCamelCase__       :Optional[int]							 =			None




		def    lowerCAmelCase__    (		self   ,       UpperCamelCase_							):

							'''simple docstring'''


							if self.config.tf_padding:
												UpperCamelCase__       :int							 =			apply_tf_padding(__lowerCAmelCase   ,       self.convolution							)
							UpperCamelCase__       :Tuple							 =			self.convolution(__lowerCAmelCase							)
							if self.normalization is not None:
												UpperCamelCase__       :Any							 =			self.normalization(__lowerCAmelCase							)
							if self.activation is not None:
												UpperCamelCase__       :List[str]							 =			self.activation(__lowerCAmelCase							)
							return features



class        lowercase     (							_a ):

		"""simple docstring"""

		_a   			=    MobileNetVaConfig
		_a   			=    load_tf_weights_in_mobilenet_va
		_a   			=    """mobilenet_v1"""
		_a   			=    """pixel_values"""
		_a   			=    False




		def    lowerCAmelCase__    (		self   ,       UpperCamelCase_							):

							'''simple docstring'''


							if isinstance(__lowerCAmelCase   ,       (nn.Linear, nn.Convad)							):
												module.weight.data.normal_(mean=0.0   ,       std=self.config.initializer_range							)
												if module.bias is not None:
																	module.bias.data.zero_()
							elif isinstance(__lowerCAmelCase   ,       nn.BatchNormad							):
												module.bias.data.zero_()
												module.weight.data.fill_(1.0							)



__snake_case											=R'''\n    This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. Use it\n    as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and\n    behavior.\n\n    Parameters:\n        config ([`MobileNetV1Config`]): Model configuration class with all the parameters of the model.\n            Initializing with a config file does not load the weights associated with the model, only the\n            configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.\n'''

__snake_case											=R'''\n    Args:\n        pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):\n            Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See\n            [`MobileNetV1ImageProcessor.__call__`] for details.\n        output_hidden_states (`bool`, *optional*):\n            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for\n            more detail.\n        return_dict (`bool`, *optional*):\n            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.\n'''
@add_start_docstrings(
    'The bare MobileNetV1 model outputting raw hidden-states without any specific head on top.'     ,		_a     ,		)
class        lowercase     (							_a ):

		"""simple docstring"""

		def __init__(		self   ,       UpperCamelCase_   ,       UpperCamelCase_ = True							):

							'''simple docstring'''


							super().__init__(__lowerCAmelCase							)
							UpperCamelCase__       :List[str]							 =			config

							UpperCamelCase__       :Optional[int]							 =			32
							UpperCamelCase__       :List[Any]							 =			max(int(depth * config.depth_multiplier							)   ,       config.min_depth							)

							UpperCamelCase__       :int							 =			MobileNetVaConvLayer(
							    __lowerCAmelCase   ,       in_channels=config.num_channels   ,       out_channels=__lowerCAmelCase   ,       kernel_size=3   ,       stride=2   ,       )

							UpperCamelCase__       :List[str]							 =			[1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 2, 1]

							UpperCamelCase__       :Dict							 =			nn.ModuleList()
							for i in range(13							):
												UpperCamelCase__       :Dict							 =			out_channels

												if strides[i] == 2 or i == 0:
																	depth *= 2
																	UpperCamelCase__       :Tuple							 =			max(int(depth * config.depth_multiplier							)   ,       config.min_depth							)

												self.layer.append(
												    MobileNetVaConvLayer(
												        __lowerCAmelCase   ,       in_channels=__lowerCAmelCase   ,       out_channels=__lowerCAmelCase   ,       kernel_size=3   ,       stride=strides[i]   ,       groups=__lowerCAmelCase   ,       )							)

												self.layer.append(
												    MobileNetVaConvLayer(
												        __lowerCAmelCase   ,       in_channels=__lowerCAmelCase   ,       out_channels=__lowerCAmelCase   ,       kernel_size=1   ,       )							)

							UpperCamelCase__       :Optional[int]							 =			nn.AdaptiveAvgPoolad((1, 1)							) if add_pooling_layer else None

							# Initialize weights and apply final processing
							self.post_init()




		def    lowerCAmelCase__    (		self   ,       UpperCamelCase_							):

							'''simple docstring'''


							raise NotImplementedError




		@add_start_docstrings_to_model_forward(__lowerCAmelCase							)
		@add_code_sample_docstrings(
		    checkpoint=_CHECKPOINT_FOR_DOC   ,       output_type=__lowerCAmelCase   ,       config_class=_CONFIG_FOR_DOC   ,       modality='''vision'''   ,       expected_output=_EXPECTED_OUTPUT_SHAPE   ,       )
		def    lowerCAmelCase__    (		self   ,       UpperCamelCase_ = None   ,       UpperCamelCase_ = None   ,       UpperCamelCase_ = None   ,       ):

							'''simple docstring'''


							UpperCamelCase__       :Any							 =			(
							    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
							)
							UpperCamelCase__       :List[Any]							 =			return_dict if return_dict is not None else self.config.use_return_dict

							if pixel_values is None:
												raise ValueError('''You have to specify pixel_values'''							)

							UpperCamelCase__       :Any							 =			self.conv_stem(__lowerCAmelCase							)

							UpperCamelCase__       :Any							 =			() if output_hidden_states else None

							for i, layer_module in enumerate(self.layer							):
												UpperCamelCase__       :Any							 =			layer_module(__lowerCAmelCase							)

												if output_hidden_states:
																	UpperCamelCase__       :Any							 =			all_hidden_states + (hidden_states,)

							UpperCamelCase__       :Dict							 =			hidden_states

							if self.pooler is not None:
												UpperCamelCase__       :List[str]							 =			torch.flatten(self.pooler(__lowerCAmelCase							)   ,       start_dim=1							)
							else:
												UpperCamelCase__       :Union[str, Any]							 =			None

							if not return_dict:
												return tuple(v for v in [last_hidden_state, pooled_output, all_hidden_states] if v is not None							)

							return BaseModelOutputWithPoolingAndNoAttention(
							    last_hidden_state=__lowerCAmelCase   ,       pooler_output=__lowerCAmelCase   ,       hidden_states=__lowerCAmelCase   ,       )



@add_start_docstrings(
    '\n    MobileNetV1 model with an image classification head on top (a linear layer on top of the pooled features), e.g. for\n    ImageNet.\n    '     ,		_a     ,		)
class        lowercase     (							_a ):

		"""simple docstring"""

		def __init__(		self   ,       UpperCamelCase_							):

							'''simple docstring'''


							super().__init__(__lowerCAmelCase							)

							UpperCamelCase__       :List[str]							 =			config.num_labels
							UpperCamelCase__       :Tuple							 =			MobileNetVaModel(__lowerCAmelCase							)

							UpperCamelCase__       :int							 =			self.mobilenet_va.layer[-1].convolution.out_channels

							# Classifier head
							UpperCamelCase__       :str							 =			nn.Dropout(config.classifier_dropout_prob   ,       inplace=__lowerCAmelCase							)
							UpperCamelCase__       :Optional[int]							 =			nn.Linear(__lowerCAmelCase   ,       config.num_labels							) if config.num_labels > 0 else nn.Identity()

							# Initialize weights and apply final processing
							self.post_init()




		@add_start_docstrings_to_model_forward(__lowerCAmelCase							)
		@add_code_sample_docstrings(
		    checkpoint=_IMAGE_CLASS_CHECKPOINT   ,       output_type=__lowerCAmelCase   ,       config_class=_CONFIG_FOR_DOC   ,       expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT   ,       )
		def    lowerCAmelCase__    (		self   ,       UpperCamelCase_ = None   ,       UpperCamelCase_ = None   ,       UpperCamelCase_ = None   ,       UpperCamelCase_ = None   ,       ):

							'''simple docstring'''


							UpperCamelCase__       :List[Any]							 =			return_dict if return_dict is not None else self.config.use_return_dict

							UpperCamelCase__       :Any							 =			self.mobilenet_va(__lowerCAmelCase   ,       output_hidden_states=__lowerCAmelCase   ,       return_dict=__lowerCAmelCase							)

							UpperCamelCase__       :List[str]							 =			outputs.pooler_output if return_dict else outputs[1]

							UpperCamelCase__       :int							 =			self.classifier(self.dropout(__lowerCAmelCase							)							)

							UpperCamelCase__       :int							 =			None
							if labels is not None:
												if self.config.problem_type is None:
																	if self.num_labels == 1:
																						UpperCamelCase__       :int							 =			'''regression'''
																	elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
																						UpperCamelCase__       :Optional[int]							 =			'''single_label_classification'''
																	else:
																						UpperCamelCase__       :Tuple							 =			'''multi_label_classification'''

												if self.config.problem_type == "regression":
																	UpperCamelCase__       :Optional[int]							 =			MSELoss()
																	if self.num_labels == 1:
																						UpperCamelCase__       :Optional[int]							 =			loss_fct(logits.squeeze()   ,       labels.squeeze()							)
																	else:
																						UpperCamelCase__       :int							 =			loss_fct(__lowerCAmelCase   ,       __lowerCAmelCase							)
												elif self.config.problem_type == "single_label_classification":
																	UpperCamelCase__       :List[Any]							 =			CrossEntropyLoss()
																	UpperCamelCase__       :Tuple							 =			loss_fct(logits.view(-1   ,       self.num_labels							)   ,       labels.view(-1							)							)
												elif self.config.problem_type == "multi_label_classification":
																	UpperCamelCase__       :str							 =			BCEWithLogitsLoss()
																	UpperCamelCase__       :Tuple							 =			loss_fct(__lowerCAmelCase   ,       __lowerCAmelCase							)

							if not return_dict:
												UpperCamelCase__       :Dict							 =			(logits,) + outputs[2:]
												return ((loss,) + output) if loss is not None else output

							return ImageClassifierOutputWithNoAttention(
							    loss=__lowerCAmelCase   ,       logits=__lowerCAmelCase   ,       hidden_states=outputs.hidden_states   ,       )



'''simple docstring'''
import gc
import unittest

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

from diffusers import AltDiffusionPipeline, AutoencoderKL, DDIMScheduler, PNDMScheduler, UNetaDConditionModel
from diffusers.pipelines.alt_diffusion.modeling_roberta_series import (
    RobertaSeriesConfig,
    RobertaSeriesModelWithTransformation,
)
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS
from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()
class        lowercase     (							A__     ,		A__     ,		A__     ,		unittest.TestCase ):

		"""simple docstring"""

		_a   			=    AltDiffusionPipeline
		_a   			=    TEXT_TO_IMAGE_PARAMS
		_a   			=    TEXT_TO_IMAGE_BATCH_PARAMS
		_a   			=    TEXT_TO_IMAGE_IMAGE_PARAMS
		_a   			=    TEXT_TO_IMAGE_IMAGE_PARAMS




		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							torch.manual_seed(0							)
							UpperCamelCase__       :List[str]							 =			UNetaDConditionModel(
							    block_out_channels=(32, 64)   ,       layers_per_block=2   ,       sample_size=32   ,       in_channels=4   ,       out_channels=4   ,       down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''')   ,       up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''')   ,       cross_attention_dim=32   ,       )
							UpperCamelCase__       :int							 =			DDIMScheduler(
							    beta_start=0.00085   ,       beta_end=0.012   ,       beta_schedule='''scaled_linear'''   ,       clip_sample=UpperCamelCase_   ,       set_alpha_to_one=UpperCamelCase_   ,       )
							torch.manual_seed(0							)
							UpperCamelCase__       :List[str]							 =			AutoencoderKL(
							    block_out_channels=[32, 64]   ,       in_channels=3   ,       out_channels=3   ,       down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D''']   ,       up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D''']   ,       latent_channels=4   ,       )

							# TODO: address the non-deterministic text encoder (fails for save-load tests)
							# torch.manual_seed(0)
							# text_encoder_config = RobertaSeriesConfig(
							#     hidden_size=32,
							#     project_dim=32,
							#     intermediate_size=37,
							#     layer_norm_eps=1e-05,
							#     num_attention_heads=4,
							#     num_hidden_layers=5,
							#     vocab_size=5002,
							# )
							# text_encoder = RobertaSeriesModelWithTransformation(text_encoder_config)

							torch.manual_seed(0							)
							UpperCamelCase__       :Union[str, Any]							 =			CLIPTextConfig(
							    bos_token_id=0   ,       eos_token_id=2   ,       hidden_size=32   ,       projection_dim=32   ,       intermediate_size=37   ,       layer_norm_eps=1e-05   ,       num_attention_heads=4   ,       num_hidden_layers=5   ,       pad_token_id=1   ,       vocab_size=5002   ,       )
							UpperCamelCase__       :Optional[Any]							 =			CLIPTextModel(UpperCamelCase_							)

							UpperCamelCase__       :int							 =			XLMRobertaTokenizer.from_pretrained('''hf-internal-testing/tiny-xlm-roberta'''							)
							UpperCamelCase__       :Dict							 =			77

							UpperCamelCase__       :Dict							 =			{
							    '''unet''': unet,
							    '''scheduler''': scheduler,
							    '''vae''': vae,
							    '''text_encoder''': text_encoder,
							    '''tokenizer''': tokenizer,
							    '''safety_checker''': None,
							    '''feature_extractor''': None,
							}
							return components




		def    lowerCAmelCase__    (		self   ,       UpperCamelCase_   ,       UpperCamelCase_=0							):

							'''simple docstring'''


							if str(UpperCamelCase_							).startswith('''mps'''							):
												UpperCamelCase__       :Dict							 =			torch.manual_seed(UpperCamelCase_							)
							else:
												UpperCamelCase__       :List[str]							 =			torch.Generator(device=UpperCamelCase_							).manual_seed(UpperCamelCase_							)
							UpperCamelCase__       :Optional[int]							 =			{
							    '''prompt''': '''A painting of a squirrel eating a burger''',
							    '''generator''': generator,
							    '''num_inference_steps''': 2,
							    '''guidance_scale''': 6.0,
							    '''output_type''': '''numpy''',
							}
							return inputs




		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							super().test_attention_slicing_forward_pass(expected_max_diff=3e-3							)




		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							super().test_inference_batch_single_identical(expected_max_diff=3e-3							)




		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							UpperCamelCase__       :str							 =			'''cpu'''  # ensure determinism for the device-dependent torch.Generator

							UpperCamelCase__       :Tuple							 =			self.get_dummy_components()
							torch.manual_seed(0							)
							UpperCamelCase__       :Dict							 =			RobertaSeriesConfig(
							    hidden_size=32   ,       project_dim=32   ,       intermediate_size=37   ,       layer_norm_eps=1e-05   ,       num_attention_heads=4   ,       num_hidden_layers=5   ,       vocab_size=5002   ,       )
							# TODO: remove after fixing the non-deterministic text encoder
							UpperCamelCase__       :Union[str, Any]							 =			RobertaSeriesModelWithTransformation(UpperCamelCase_							)
							UpperCamelCase__       :Union[str, Any]							 =			text_encoder

							UpperCamelCase__       :Any							 =			AltDiffusionPipeline(**UpperCamelCase_							)
							UpperCamelCase__       :List[Any]							 =			alt_pipe.to(UpperCamelCase_							)
							alt_pipe.set_progress_bar_config(disable=UpperCamelCase_							)

							UpperCamelCase__       :int							 =			self.get_dummy_inputs(UpperCamelCase_							)
							UpperCamelCase__       :Dict							 =			'''A photo of an astronaut'''
							UpperCamelCase__       :Union[str, Any]							 =			alt_pipe(**UpperCamelCase_							)
							UpperCamelCase__       :Any							 =			output.images
							UpperCamelCase__       :List[Any]							 =			image[0, -3:, -3:, -1]

							assert image.shape == (1, 64, 64, 3)
							UpperCamelCase__       :List[Any]							 =			np.array(
							    [0.5748162, 0.60447145, 0.48821217, 0.50100636, 0.5431185, 0.45763683, 0.49657696, 0.48132733, 0.47573093]							)

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




		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							UpperCamelCase__       :str							 =			'''cpu'''  # ensure determinism for the device-dependent torch.Generator

							UpperCamelCase__       :int							 =			self.get_dummy_components()
							UpperCamelCase__       :Union[str, Any]							 =			PNDMScheduler(skip_prk_steps=UpperCamelCase_							)
							torch.manual_seed(0							)
							UpperCamelCase__       :Dict							 =			RobertaSeriesConfig(
							    hidden_size=32   ,       project_dim=32   ,       intermediate_size=37   ,       layer_norm_eps=1e-05   ,       num_attention_heads=4   ,       num_hidden_layers=5   ,       vocab_size=5002   ,       )
							# TODO: remove after fixing the non-deterministic text encoder
							UpperCamelCase__       :str							 =			RobertaSeriesModelWithTransformation(UpperCamelCase_							)
							UpperCamelCase__       :Union[str, Any]							 =			text_encoder
							UpperCamelCase__       :Union[str, Any]							 =			AltDiffusionPipeline(**UpperCamelCase_							)
							UpperCamelCase__       :Optional[int]							 =			alt_pipe.to(UpperCamelCase_							)
							alt_pipe.set_progress_bar_config(disable=UpperCamelCase_							)

							UpperCamelCase__       :List[str]							 =			self.get_dummy_inputs(UpperCamelCase_							)
							UpperCamelCase__       :Optional[Any]							 =			alt_pipe(**UpperCamelCase_							)
							UpperCamelCase__       :str							 =			output.images
							UpperCamelCase__       :int							 =			image[0, -3:, -3:, -1]

							assert image.shape == (1, 64, 64, 3)
							UpperCamelCase__       :List[Any]							 =			np.array(
							    [0.51605093, 0.5707241, 0.47365507, 0.50578886, 0.5633877, 0.4642503, 0.5182081, 0.48763484, 0.49084237]							)

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



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

		"""simple docstring"""

		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							super().tearDown()
							gc.collect()
							torch.cuda.empty_cache()




		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							UpperCamelCase__       :Optional[Any]							 =			AltDiffusionPipeline.from_pretrained('''BAAI/AltDiffusion'''   ,       safety_checker=UpperCamelCase_							)
							UpperCamelCase__       :str							 =			alt_pipe.to(UpperCamelCase_							)
							alt_pipe.set_progress_bar_config(disable=UpperCamelCase_							)

							UpperCamelCase__       :int							 =			'''A painting of a squirrel eating a burger'''
							UpperCamelCase__       :int							 =			torch.manual_seed(0							)
							UpperCamelCase__       :Optional[Any]							 =			alt_pipe([prompt]   ,       generator=UpperCamelCase_   ,       guidance_scale=6.0   ,       num_inference_steps=20   ,       output_type='''np'''							)

							UpperCamelCase__       :Union[str, Any]							 =			output.images

							UpperCamelCase__       :Optional[Any]							 =			image[0, -3:, -3:, -1]

							assert image.shape == (1, 512, 512, 3)
							UpperCamelCase__       :Union[str, Any]							 =			np.array([0.1010, 0.0800, 0.0794, 0.0885, 0.0843, 0.0762, 0.0769, 0.0729, 0.0586]							)

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




		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							UpperCamelCase__       :Any							 =			DDIMScheduler.from_pretrained('''BAAI/AltDiffusion'''   ,       subfolder='''scheduler'''							)

							UpperCamelCase__       :List[str]							 =			AltDiffusionPipeline.from_pretrained('''BAAI/AltDiffusion'''   ,       scheduler=UpperCamelCase_   ,       safety_checker=UpperCamelCase_							)
							UpperCamelCase__       :Dict							 =			alt_pipe.to(UpperCamelCase_							)
							alt_pipe.set_progress_bar_config(disable=UpperCamelCase_							)

							UpperCamelCase__       :List[Any]							 =			'''A painting of a squirrel eating a burger'''
							UpperCamelCase__       :str							 =			torch.manual_seed(0							)

							UpperCamelCase__       :Optional[int]							 =			alt_pipe([prompt]   ,       generator=UpperCamelCase_   ,       num_inference_steps=2   ,       output_type='''numpy'''							)
							UpperCamelCase__       :Any							 =			output.images

							UpperCamelCase__       :int							 =			image[0, -3:, -3:, -1]

							assert image.shape == (1, 512, 512, 3)
							UpperCamelCase__       :int							 =			np.array([0.4019, 0.4052, 0.3810, 0.4119, 0.3916, 0.3982, 0.4651, 0.4195, 0.5323]							)

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