




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


lowerCamelCase__					:  Tuple      							=				logging.get_logger(__name__)


# General docstring
lowerCamelCase__					:  List[Any]      							=				'MobileNetV1Config'

# Base docstring
lowerCamelCase__					:  Dict      							=				'google/mobilenet_v1_1.0_224'
lowerCamelCase__					:  Tuple      							=				[1, 1_024, 7, 7]

# Image classification docstring
lowerCamelCase__					:  Any      							=				'google/mobilenet_v1_1.0_224'
lowerCamelCase__					:  Tuple      							=				'tabby, tabby cat'


lowerCamelCase__					:  Union[str, Any]      							=				[
    '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    UpperCAmelCase_				( __UpperCAmelCase		:      int     ,     __UpperCAmelCase		:      List[Any]     ,     __UpperCAmelCase		:      Union[str, Any]=None				)   ->					Dict:
      SCREAMING_SNAKE_CASE_      		=     {}

      if isinstance(__UpperCAmelCase     ,     __UpperCAmelCase				):
            SCREAMING_SNAKE_CASE_      		=     model.mobilenet_va
      else:
            SCREAMING_SNAKE_CASE_      		=     model

      SCREAMING_SNAKE_CASE_      		=     'MobilenetV1/Conv2d_0/'
      SCREAMING_SNAKE_CASE_      		=     backbone.conv_stem.convolution.weight
      SCREAMING_SNAKE_CASE_      		=     backbone.conv_stem.normalization.bias
      SCREAMING_SNAKE_CASE_      		=     backbone.conv_stem.normalization.weight
      SCREAMING_SNAKE_CASE_      		=     backbone.conv_stem.normalization.running_mean
      SCREAMING_SNAKE_CASE_      		=     backbone.conv_stem.normalization.running_var

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

            SCREAMING_SNAKE_CASE_      		=     backbone.layer[pt_index]
            SCREAMING_SNAKE_CASE_      		=     f"MobilenetV1/Conv2d_{tf_index}_depthwise/"
            SCREAMING_SNAKE_CASE_      		=     pointer.convolution.weight
            SCREAMING_SNAKE_CASE_      		=     pointer.normalization.bias
            SCREAMING_SNAKE_CASE_      		=     pointer.normalization.weight
            SCREAMING_SNAKE_CASE_      		=     pointer.normalization.running_mean
            SCREAMING_SNAKE_CASE_      		=     pointer.normalization.running_var

            SCREAMING_SNAKE_CASE_      		=     backbone.layer[pt_index + 1]
            SCREAMING_SNAKE_CASE_      		=     f"MobilenetV1/Conv2d_{tf_index}_pointwise/"
            SCREAMING_SNAKE_CASE_      		=     pointer.convolution.weight
            SCREAMING_SNAKE_CASE_      		=     pointer.normalization.bias
            SCREAMING_SNAKE_CASE_      		=     pointer.normalization.weight
            SCREAMING_SNAKE_CASE_      		=     pointer.normalization.running_mean
            SCREAMING_SNAKE_CASE_      		=     pointer.normalization.running_var

      if isinstance(__UpperCAmelCase     ,     __UpperCAmelCase				):
            SCREAMING_SNAKE_CASE_      		=     'MobilenetV1/Logits/Conv2d_1c_1x1/'
            SCREAMING_SNAKE_CASE_      		=     model.classifier.weight
            SCREAMING_SNAKE_CASE_      		=     model.classifier.bias

      return tf_to_pt_map

def    UpperCAmelCase_				( __UpperCAmelCase		:      List[Any]     ,     __UpperCAmelCase		:      Optional[Any]     ,     __UpperCAmelCase		:      List[Any]				)   ->					Optional[Any]:
      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
      SCREAMING_SNAKE_CASE_      		=     tf.train.list_variables(__UpperCAmelCase				)
      SCREAMING_SNAKE_CASE_      		=     {}
      for name, shape in init_vars:
            logger.info(f"Loading TF weight {name} with shape {shape}"				)
            SCREAMING_SNAKE_CASE_      		=     tf.train.load_variable(__UpperCAmelCase     ,     __UpperCAmelCase				)
            SCREAMING_SNAKE_CASE_      		=     array

      # Build TF to PyTorch weights loading map
      SCREAMING_SNAKE_CASE_      		=     _build_tf_to_pytorch_map(__UpperCAmelCase     ,     __UpperCAmelCase     ,     __UpperCAmelCase				)

      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

            SCREAMING_SNAKE_CASE_      		=     tf_weights[name]

            if "depthwise_weights" in name:
                  logger.info('Transposing depthwise'				)
                  SCREAMING_SNAKE_CASE_      		=     np.transpose(__UpperCAmelCase     ,     (2, 3, 0, 1)				)
            elif "weights" in name:
                  logger.info('Transposing'				)
                  if len(pointer.shape				) == 2:  # copying into linear layer
                        SCREAMING_SNAKE_CASE_      		=     array.squeeze().transpose()
                  else:
                        SCREAMING_SNAKE_CASE_      		=     np.transpose(__UpperCAmelCase     ,     (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}"				)
            SCREAMING_SNAKE_CASE_      		=     torch.from_numpy(__UpperCAmelCase				)

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

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

def    UpperCAmelCase_				( __UpperCAmelCase		:      torch.Tensor     ,     __UpperCAmelCase		:      nn.Convad				)   ->					torch.Tensor:
      SCREAMING_SNAKE_CASE_		,					SCREAMING_SNAKE_CASE_      		=     features.shape[-2:]
      SCREAMING_SNAKE_CASE_		,					SCREAMING_SNAKE_CASE_      		=     conv_layer.stride
      SCREAMING_SNAKE_CASE_		,					SCREAMING_SNAKE_CASE_      		=     conv_layer.kernel_size

      if in_height % stride_height == 0:
            SCREAMING_SNAKE_CASE_      		=     max(kernel_height - stride_height     ,     0				)
      else:
            SCREAMING_SNAKE_CASE_      		=     max(kernel_height - (in_height % stride_height)     ,     0				)

      if in_width % stride_width == 0:
            SCREAMING_SNAKE_CASE_      		=     max(kernel_width - stride_width     ,     0				)
      else:
            SCREAMING_SNAKE_CASE_      		=     max(kernel_width - (in_width % stride_width)     ,     0				)

      SCREAMING_SNAKE_CASE_      		=     pad_along_width // 2
      SCREAMING_SNAKE_CASE_      		=     pad_along_width - pad_left
      SCREAMING_SNAKE_CASE_      		=     pad_along_height // 2
      SCREAMING_SNAKE_CASE_      		=     pad_along_height - pad_top

      SCREAMING_SNAKE_CASE_      		=     (pad_left, pad_right, pad_top, pad_bottom)
      return nn.functional.pad(__UpperCAmelCase     ,     __UpperCAmelCase     ,     'constant'     ,     0.0				)





class 			lowerCamelCase_				( nn.Module   ):


 '''simple docstring'''







 def __init__(				self    :						Tuple							,							_lowerCAmelCase    :						MobileNetVaConfig							,							_lowerCAmelCase    :						int							,							_lowerCAmelCase    :						int							,							_lowerCAmelCase    :						int							,							_lowerCAmelCase    :						Optional[int] = 1							,							_lowerCAmelCase    :						Optional[int] = 1							,							_lowerCAmelCase    :						bool = False							,							_lowerCAmelCase    :						Optional[bool] = True							,							_lowerCAmelCase    :						Optional[bool or str] = True							,							):
       super().__init__()
       SCREAMING_SNAKE_CASE_      		=     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."		)

       SCREAMING_SNAKE_CASE_      		=     0 if config.tf_padding else int((kernel_size - 1) / 2		)

       SCREAMING_SNAKE_CASE_      		=     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:
             SCREAMING_SNAKE_CASE_      		=     nn.BatchNormad(
                 num_features=_lowerCAmelCase							,							eps=config.layer_norm_eps							,							momentum=0.9997							,							affine=_lowerCAmelCase							,							track_running_stats=_lowerCAmelCase							,							)
       else:
             SCREAMING_SNAKE_CASE_      		=     None

       if use_activation:
             if isinstance(_lowerCAmelCase							,							_lowerCAmelCase		):
                   SCREAMING_SNAKE_CASE_      		=     ACTaFN[use_activation]
             elif isinstance(config.hidden_act							,							_lowerCAmelCase		):
                   SCREAMING_SNAKE_CASE_      		=     ACTaFN[config.hidden_act]
             else:
                   SCREAMING_SNAKE_CASE_      		=     config.hidden_act
       else:
             SCREAMING_SNAKE_CASE_      		=     None




 def  lowerCAmelCase_    (				self    :						Any							,							_lowerCAmelCase    :						torch.Tensor		):
       if self.config.tf_padding:
             SCREAMING_SNAKE_CASE_      		=     apply_tf_padding(_lowerCAmelCase							,							self.convolution		)
       SCREAMING_SNAKE_CASE_      		=     self.convolution(_lowerCAmelCase		)
       if self.normalization is not None:
             SCREAMING_SNAKE_CASE_      		=     self.normalization(_lowerCAmelCase		)
       if self.activation is not None:
             SCREAMING_SNAKE_CASE_      		=     self.activation(_lowerCAmelCase		)
       return features





class 			lowerCamelCase_				( _SCREAMING_SNAKE_CASE   ):


 '''simple docstring'''







 lowercase_							       =       MobileNetVaConfig
 lowercase_							       =       load_tf_weights_in_mobilenet_va
 lowercase_							       =       "mobilenet_v1"
 lowercase_							       =       "pixel_values"
 lowercase_							       =       False



 def  lowerCAmelCase_    (				self    :						Union[str, Any]							,							_lowerCAmelCase    :						Union[nn.Linear, nn.Convad]		):
       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		)


lowerCamelCase__					:  Optional[int]      							=				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'

lowerCamelCase__					:  str      							=				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."			,  _SCREAMING_SNAKE_CASE			,  )
class 			lowerCamelCase_				( _SCREAMING_SNAKE_CASE   ):


 '''simple docstring'''







 def __init__(				self    :						Any							,							_lowerCAmelCase    :						MobileNetVaConfig							,							_lowerCAmelCase    :						bool = True		):
       super().__init__(_lowerCAmelCase		)
       SCREAMING_SNAKE_CASE_      		=     config

       SCREAMING_SNAKE_CASE_      		=     32
       SCREAMING_SNAKE_CASE_      		=     max(int(depth * config.depth_multiplier		)							,							config.min_depth		)

       SCREAMING_SNAKE_CASE_      		=     MobileNetVaConvLayer(
           _lowerCAmelCase							,							in_channels=config.num_channels							,							out_channels=_lowerCAmelCase							,							kernel_size=3							,							stride=2							,							)

       SCREAMING_SNAKE_CASE_      		=     [1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 2, 1]

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

             if strides[i] == 2 or i == 0:
                   depth *= 2
                   SCREAMING_SNAKE_CASE_      		=     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							,							)		)

       SCREAMING_SNAKE_CASE_      		=     nn.AdaptiveAvgPoolad((1, 1)		) if add_pooling_layer else None

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



 def  lowerCAmelCase_    (				self    :						List[str]							,							_lowerCAmelCase    :						Any		):
       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    :						Optional[Any]							,							_lowerCAmelCase    :						Optional[torch.Tensor] = None							,							_lowerCAmelCase    :						Optional[bool] = None							,							_lowerCAmelCase    :						Optional[bool] = None							,							):
       SCREAMING_SNAKE_CASE_      		=     (
           output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
       )
       SCREAMING_SNAKE_CASE_      		=     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'		)

       SCREAMING_SNAKE_CASE_      		=     self.conv_stem(_lowerCAmelCase		)

       SCREAMING_SNAKE_CASE_      		=     () if output_hidden_states else None

       for i, layer_module in enumerate(self.layer		):
             SCREAMING_SNAKE_CASE_      		=     layer_module(_lowerCAmelCase		)

             if output_hidden_states:
                   SCREAMING_SNAKE_CASE_      		=     all_hidden_states + (hidden_states,)

       SCREAMING_SNAKE_CASE_      		=     hidden_states

       if self.pooler is not None:
             SCREAMING_SNAKE_CASE_      		=     torch.flatten(self.pooler(_lowerCAmelCase		)							,							start_dim=1		)
       else:
             SCREAMING_SNAKE_CASE_      		=     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    "			,  _SCREAMING_SNAKE_CASE			,  )
class 			lowerCamelCase_				( _SCREAMING_SNAKE_CASE   ):


 '''simple docstring'''







 def __init__(				self    :						int							,							_lowerCAmelCase    :						MobileNetVaConfig		):
       super().__init__(_lowerCAmelCase		)

       SCREAMING_SNAKE_CASE_      		=     config.num_labels
       SCREAMING_SNAKE_CASE_      		=     MobileNetVaModel(_lowerCAmelCase		)

       SCREAMING_SNAKE_CASE_      		=     self.mobilenet_va.layer[-1].convolution.out_channels

       # Classifier head
       SCREAMING_SNAKE_CASE_      		=     nn.Dropout(config.classifier_dropout_prob							,							inplace=_lowerCAmelCase		)
       SCREAMING_SNAKE_CASE_      		=     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    :						Optional[Any]							,							_lowerCAmelCase    :						Optional[torch.Tensor] = None							,							_lowerCAmelCase    :						Optional[bool] = None							,							_lowerCAmelCase    :						Optional[torch.Tensor] = None							,							_lowerCAmelCase    :						Optional[bool] = None							,							):
       SCREAMING_SNAKE_CASE_      		=     return_dict if return_dict is not None else self.config.use_return_dict

       SCREAMING_SNAKE_CASE_      		=     self.mobilenet_va(_lowerCAmelCase							,							output_hidden_states=_lowerCAmelCase							,							return_dict=_lowerCAmelCase		)

       SCREAMING_SNAKE_CASE_      		=     outputs.pooler_output if return_dict else outputs[1]

       SCREAMING_SNAKE_CASE_      		=     self.classifier(self.dropout(_lowerCAmelCase		)		)

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

             if self.config.problem_type == "regression":
                   SCREAMING_SNAKE_CASE_      		=     MSELoss()
                   if self.num_labels == 1:
                         SCREAMING_SNAKE_CASE_      		=     loss_fct(logits.squeeze()							,							labels.squeeze()		)
                   else:
                         SCREAMING_SNAKE_CASE_      		=     loss_fct(_lowerCAmelCase							,							_lowerCAmelCase		)
             elif self.config.problem_type == "single_label_classification":
                   SCREAMING_SNAKE_CASE_      		=     CrossEntropyLoss()
                   SCREAMING_SNAKE_CASE_      		=     loss_fct(logits.view(-1							,							self.num_labels		)							,							labels.view(-1		)		)
             elif self.config.problem_type == "multi_label_classification":
                   SCREAMING_SNAKE_CASE_      		=     BCEWithLogitsLoss()
                   SCREAMING_SNAKE_CASE_      		=     loss_fct(_lowerCAmelCase							,							_lowerCAmelCase		)

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

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





import gc
import random
import unittest

import numpy as np
import torch
from transformers import XLMRobertaTokenizer

from diffusers import (
    AltDiffusionImgaImgPipeline,
    AutoencoderKL,
    PNDMScheduler,
    UNetaDConditionModel,
)
from diffusers.image_processor import VaeImageProcessor
from diffusers.pipelines.alt_diffusion.modeling_roberta_series import (
    RobertaSeriesConfig,
    RobertaSeriesModelWithTransformation,
)
from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu


enable_full_determinism()





class 			lowerCamelCase_				( unittest.TestCase   ):


 '''simple docstring'''







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



 @property
 def  lowerCAmelCase_    (				self    :						Union[str, Any]		):
       SCREAMING_SNAKE_CASE_      		=     1
       SCREAMING_SNAKE_CASE_      		=     3
       SCREAMING_SNAKE_CASE_      		=     (32, 32)

       SCREAMING_SNAKE_CASE_      		=     floats_tensor((batch_size, num_channels) + sizes							,							rng=random.Random(0		)		).to(_lowerCAmelCase		)
       return image



 @property
 def  lowerCAmelCase_    (				self    :						Union[str, Any]		):
       torch.manual_seed(0		)
       SCREAMING_SNAKE_CASE_      		=     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							,							)
       return model



 @property
 def  lowerCAmelCase_    (				self    :						Tuple		):
       torch.manual_seed(0		)
       SCREAMING_SNAKE_CASE_      		=     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							,							)
       return model



 @property
 def  lowerCAmelCase_    (				self    :						Optional[int]		):
       torch.manual_seed(0		)
       SCREAMING_SNAKE_CASE_      		=     RobertaSeriesConfig(
           hidden_size=32							,							project_dim=32							,							intermediate_size=37							,							layer_norm_eps=1E-05							,							num_attention_heads=4							,							num_hidden_layers=5							,							pad_token_id=1							,							vocab_size=5_006							,							)
       return RobertaSeriesModelWithTransformation(_lowerCAmelCase		)



 @property
 def  lowerCAmelCase_    (				self    :						List[Any]		):
       def extract(*_lowerCAmelCase    :						Optional[int]							,							**_lowerCAmelCase    :						str		):





             class 			lowerCamelCase_				:


              '''simple docstring'''







              def __init__(				self    :						str		):
                    SCREAMING_SNAKE_CASE_      		=     torch.ones([0]		)




              def  lowerCAmelCase_    (				self    :						Union[str, Any]							,							_lowerCAmelCase    :						int		):
                    self.pixel_values.to(_lowerCAmelCase		)
                    return self


             return Out()

       return extract



 def  lowerCAmelCase_    (				self    :						Optional[Any]		):
       SCREAMING_SNAKE_CASE_      		=     'cpu'  # ensure determinism for the device-dependent torch.Generator
       SCREAMING_SNAKE_CASE_      		=     self.dummy_cond_unet
       SCREAMING_SNAKE_CASE_      		=     PNDMScheduler(skip_prk_steps=_lowerCAmelCase		)
       SCREAMING_SNAKE_CASE_      		=     self.dummy_vae
       SCREAMING_SNAKE_CASE_      		=     self.dummy_text_encoder
       SCREAMING_SNAKE_CASE_      		=     XLMRobertaTokenizer.from_pretrained('hf-internal-testing/tiny-xlm-roberta'		)
       SCREAMING_SNAKE_CASE_      		=     77

       SCREAMING_SNAKE_CASE_      		=     self.dummy_image.to(_lowerCAmelCase		)
       SCREAMING_SNAKE_CASE_      		=     init_image / 2 + 0.5

       # make sure here that pndm scheduler skips prk
       SCREAMING_SNAKE_CASE_      		=     AltDiffusionImgaImgPipeline(
           unet=_lowerCAmelCase							,							scheduler=_lowerCAmelCase							,							vae=_lowerCAmelCase							,							text_encoder=_lowerCAmelCase							,							tokenizer=_lowerCAmelCase							,							safety_checker=_lowerCAmelCase							,							feature_extractor=self.dummy_extractor							,							)
       SCREAMING_SNAKE_CASE_      		=     VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor							,							do_normalize=_lowerCAmelCase		)
       SCREAMING_SNAKE_CASE_      		=     alt_pipe.to(_lowerCAmelCase		)
       alt_pipe.set_progress_bar_config(disable=_lowerCAmelCase		)

       SCREAMING_SNAKE_CASE_      		=     'A painting of a squirrel eating a burger'
       SCREAMING_SNAKE_CASE_      		=     torch.Generator(device=_lowerCAmelCase		).manual_seed(0		)
       SCREAMING_SNAKE_CASE_      		=     alt_pipe(
           [prompt]							,							generator=_lowerCAmelCase							,							guidance_scale=6.0							,							num_inference_steps=2							,							output_type='np'							,							image=_lowerCAmelCase							,							)

       SCREAMING_SNAKE_CASE_      		=     output.images

       SCREAMING_SNAKE_CASE_      		=     torch.Generator(device=_lowerCAmelCase		).manual_seed(0		)
       SCREAMING_SNAKE_CASE_      		=     alt_pipe(
           [prompt]							,							generator=_lowerCAmelCase							,							guidance_scale=6.0							,							num_inference_steps=2							,							output_type='np'							,							image=_lowerCAmelCase							,							return_dict=_lowerCAmelCase							,							)[0]

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

       assert image.shape == (1, 32, 32, 3)
       SCREAMING_SNAKE_CASE_      		=     np.array([0.4427, 0.3731, 0.4249, 0.4941, 0.4546, 0.4148, 0.4193, 0.4666, 0.4499]		)

       assert np.abs(image_slice.flatten() - expected_slice		).max() < 5E-3
       assert np.abs(image_from_tuple_slice.flatten() - expected_slice		).max() < 5E-3



 @unittest.skipIf(torch_device != 'cuda'							,							'This test requires a GPU'		)
 def  lowerCAmelCase_    (				self    :						Tuple		):
       SCREAMING_SNAKE_CASE_      		=     self.dummy_cond_unet
       SCREAMING_SNAKE_CASE_      		=     PNDMScheduler(skip_prk_steps=_lowerCAmelCase		)
       SCREAMING_SNAKE_CASE_      		=     self.dummy_vae
       SCREAMING_SNAKE_CASE_      		=     self.dummy_text_encoder
       SCREAMING_SNAKE_CASE_      		=     XLMRobertaTokenizer.from_pretrained('hf-internal-testing/tiny-xlm-roberta'		)
       SCREAMING_SNAKE_CASE_      		=     77

       SCREAMING_SNAKE_CASE_      		=     self.dummy_image.to(_lowerCAmelCase		)

       # put models in fp16
       SCREAMING_SNAKE_CASE_      		=     unet.half()
       SCREAMING_SNAKE_CASE_      		=     vae.half()
       SCREAMING_SNAKE_CASE_      		=     bert.half()

       # make sure here that pndm scheduler skips prk
       SCREAMING_SNAKE_CASE_      		=     AltDiffusionImgaImgPipeline(
           unet=_lowerCAmelCase							,							scheduler=_lowerCAmelCase							,							vae=_lowerCAmelCase							,							text_encoder=_lowerCAmelCase							,							tokenizer=_lowerCAmelCase							,							safety_checker=_lowerCAmelCase							,							feature_extractor=self.dummy_extractor							,							)
       SCREAMING_SNAKE_CASE_      		=     VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor							,							do_normalize=_lowerCAmelCase		)
       SCREAMING_SNAKE_CASE_      		=     alt_pipe.to(_lowerCAmelCase		)
       alt_pipe.set_progress_bar_config(disable=_lowerCAmelCase		)

       SCREAMING_SNAKE_CASE_      		=     'A painting of a squirrel eating a burger'
       SCREAMING_SNAKE_CASE_      		=     torch.manual_seed(0		)
       SCREAMING_SNAKE_CASE_      		=     alt_pipe(
           [prompt]							,							generator=_lowerCAmelCase							,							num_inference_steps=2							,							output_type='np'							,							image=_lowerCAmelCase							,							).images

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




 @unittest.skipIf(torch_device != 'cuda'							,							'This test requires a GPU'		)
 def  lowerCAmelCase_    (				self    :						str		):
       SCREAMING_SNAKE_CASE_      		=     load_image(
           'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'
           '/img2img/sketch-mountains-input.jpg'		)
       # resize to resolution that is divisible by 8 but not 16 or 32
       SCREAMING_SNAKE_CASE_      		=     init_image.resize((760, 504)		)

       SCREAMING_SNAKE_CASE_      		=     'BAAI/AltDiffusion'
       SCREAMING_SNAKE_CASE_      		=     AltDiffusionImgaImgPipeline.from_pretrained(
           _lowerCAmelCase							,							safety_checker=_lowerCAmelCase							,							)
       pipe.to(_lowerCAmelCase		)
       pipe.set_progress_bar_config(disable=_lowerCAmelCase		)
       pipe.enable_attention_slicing()

       SCREAMING_SNAKE_CASE_      		=     'A fantasy landscape, trending on artstation'

       SCREAMING_SNAKE_CASE_      		=     torch.manual_seed(0		)
       SCREAMING_SNAKE_CASE_      		=     pipe(
           prompt=_lowerCAmelCase							,							image=_lowerCAmelCase							,							strength=0.75							,							guidance_scale=7.5							,							generator=_lowerCAmelCase							,							output_type='np'							,							)
       SCREAMING_SNAKE_CASE_      		=     output.images[0]

       SCREAMING_SNAKE_CASE_      		=     image[255:258, 383:386, -1]

       assert image.shape == (504, 760, 3)
       SCREAMING_SNAKE_CASE_      		=     np.array([0.9358, 0.9397, 0.9599, 0.9901, 1.0000, 1.0000, 0.9882, 1.0000, 1.0000]		)

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





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


 '''simple docstring'''







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




 def  lowerCAmelCase_    (				self    :						int		):
       SCREAMING_SNAKE_CASE_      		=     load_image(
           'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'
           '/img2img/sketch-mountains-input.jpg'		)
       SCREAMING_SNAKE_CASE_      		=     init_image.resize((768, 512)		)
       SCREAMING_SNAKE_CASE_      		=     load_numpy(
           'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/img2img/fantasy_landscape_alt.npy'		)

       SCREAMING_SNAKE_CASE_      		=     'BAAI/AltDiffusion'
       SCREAMING_SNAKE_CASE_      		=     AltDiffusionImgaImgPipeline.from_pretrained(
           _lowerCAmelCase							,							safety_checker=_lowerCAmelCase							,							)
       pipe.to(_lowerCAmelCase		)
       pipe.set_progress_bar_config(disable=_lowerCAmelCase		)
       pipe.enable_attention_slicing()

       SCREAMING_SNAKE_CASE_      		=     'A fantasy landscape, trending on artstation'

       SCREAMING_SNAKE_CASE_      		=     torch.manual_seed(0		)
       SCREAMING_SNAKE_CASE_      		=     pipe(
           prompt=_lowerCAmelCase							,							image=_lowerCAmelCase							,							strength=0.75							,							guidance_scale=7.5							,							generator=_lowerCAmelCase							,							output_type='np'							,							)
       SCREAMING_SNAKE_CASE_      		=     output.images[0]

       assert image.shape == (512, 768, 3)
       # img2img is flaky across GPUs even in fp32, so using MAE here
       assert np.abs(expected_image - image		).max() < 1E-2