





"""simple docstring"""





import unittest

import numpy as np

from transformers import RobertaConfig, is_flax_available
from transformers.testing_utils import require_flax, slow

from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask


if is_flax_available():
  from transformers.models.roberta.modeling_flax_roberta import (
      FlaxRobertaForCausalLM,
      FlaxRobertaForMaskedLM,
      FlaxRobertaForMultipleChoice,
      FlaxRobertaForQuestionAnswering,
      FlaxRobertaForSequenceClassification,
      FlaxRobertaForTokenClassification,
      FlaxRobertaModel,
  )




class 							snake_case							(			unittest.TestCase ):

      '''simple docstring'''



      def __init__(							self       :      Optional[Any]						,				__lowercase       :      List[Any]						,				__lowercase       :      Any=13						,				__lowercase       :      Tuple=7						,				__lowercase       :      Optional[Any]=True						,				__lowercase       :      Optional[Any]=True						,				__lowercase       :      Any=True						,				__lowercase       :      List[str]=True						,				__lowercase       :      List[Any]=99						,				__lowercase       :      Union[str, Any]=32						,				__lowercase       :      Optional[Any]=5						,				__lowercase       :      Dict=4						,				__lowercase       :      Optional[Any]=37						,				__lowercase       :      Dict="gelu"						,				__lowercase       :      Optional[int]=0.1						,				__lowercase       :      str=0.1						,				__lowercase       :      List[str]=512						,				__lowercase       :      Any=16						,				__lowercase       :      Any=2						,				__lowercase       :      List[Any]=0.0_2						,				__lowercase       :      Tuple=4						,				):


        '''simple docstring'''




        __UpperCAmelCase    :						Optional[int]       =						parent
        __UpperCAmelCase    :						Optional[int]       =						batch_size
        __UpperCAmelCase    :						Optional[int]       =						seq_length
        __UpperCAmelCase    :						Optional[Any]       =						is_training
        __UpperCAmelCase    :						Any       =						use_attention_mask
        __UpperCAmelCase    :						Dict       =						use_token_type_ids
        __UpperCAmelCase    :						Optional[int]       =						use_labels
        __UpperCAmelCase    :						Union[str, Any]       =						vocab_size
        __UpperCAmelCase    :						Tuple       =						hidden_size
        __UpperCAmelCase    :						Optional[int]       =						num_hidden_layers
        __UpperCAmelCase    :						Optional[int]       =						num_attention_heads
        __UpperCAmelCase    :						Optional[int]       =						intermediate_size
        __UpperCAmelCase    :						str       =						hidden_act
        __UpperCAmelCase    :						int       =						hidden_dropout_prob
        __UpperCAmelCase    :						Any       =						attention_probs_dropout_prob
        __UpperCAmelCase    :						Optional[Any]       =						max_position_embeddings
        __UpperCAmelCase    :						Tuple       =						type_vocab_size
        __UpperCAmelCase    :						List[Any]       =						type_sequence_label_size
        __UpperCAmelCase    :						Tuple       =						initializer_range
        __UpperCAmelCase    :						Union[str, Any]       =						num_choices

      def       A_					(							self       :      List[str] ):


        '''simple docstring'''




        __UpperCAmelCase    :						List[str]       =						ids_tensor([self.batch_size, self.seq_length]						,				self.vocab_size )

        __UpperCAmelCase    :						Optional[int]       =						None
        if self.use_attention_mask:
          __UpperCAmelCase    :						Union[str, Any]       =						random_attention_mask([self.batch_size, self.seq_length] )

        __UpperCAmelCase    :						Dict       =						None
        if self.use_token_type_ids:
          __UpperCAmelCase    :						List[Any]       =						ids_tensor([self.batch_size, self.seq_length]						,				self.type_vocab_size )

        __UpperCAmelCase    :						List[str]       =						RobertaConfig(
            vocab_size=self.vocab_size						,				hidden_size=self.hidden_size						,				num_hidden_layers=self.num_hidden_layers						,				num_attention_heads=self.num_attention_heads						,				intermediate_size=self.intermediate_size						,				hidden_act=self.hidden_act						,				hidden_dropout_prob=self.hidden_dropout_prob						,				attention_probs_dropout_prob=self.attention_probs_dropout_prob						,				max_position_embeddings=self.max_position_embeddings						,				type_vocab_size=self.type_vocab_size						,				is_decoder=__lowercase						,				initializer_range=self.initializer_range						,				)

        return config, input_ids, token_type_ids, attention_mask

      def       A_					(							self       :      str ):


        '''simple docstring'''




        __UpperCAmelCase    :						Any       =						self.prepare_config_and_inputs()
        __UpperCAmelCase       ,     __UpperCAmelCase       ,     __UpperCAmelCase       ,     __UpperCAmelCase    :						Tuple       =						config_and_inputs
        __UpperCAmelCase    :						Any       =						{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': attention_mask}
        return config, inputs_dict







      def       A_					(							self       :      Optional[Any] ):


        '''simple docstring'''




        __UpperCAmelCase    :						List[Any]       =						self.prepare_config_and_inputs()
        __UpperCAmelCase       ,     __UpperCAmelCase       ,     __UpperCAmelCase       ,     __UpperCAmelCase    :						Tuple       =						config_and_inputs

        __UpperCAmelCase    :						Any       =						True
        __UpperCAmelCase    :						Tuple       =						floats_tensor([self.batch_size, self.seq_length, self.hidden_size] )
        __UpperCAmelCase    :						List[Any]       =						ids_tensor([self.batch_size, self.seq_length]						,				vocab_size=2 )

        return (
            config,
            input_ids,
            token_type_ids,
            encoder_hidden_states,
            encoder_attention_mask,
        )




@require_flax
class 							snake_case							(			__UpperCAmelCase      ,  unittest.TestCase ):

      '''simple docstring'''



      _A     :       str         =       True

      _A     :       Any         =       (
          (
              FlaxRobertaModel,
              FlaxRobertaForCausalLM,
              FlaxRobertaForMaskedLM,
              FlaxRobertaForSequenceClassification,
              FlaxRobertaForTokenClassification,
              FlaxRobertaForMultipleChoice,
              FlaxRobertaForQuestionAnswering,
          )
          if is_flax_available()
          else ()
      )

      def       A_					(							self       :      Optional[int] ):


        '''simple docstring'''




        __UpperCAmelCase    :						List[Any]       =						FlaxRobertaModelTester(self )







      @slow
      def       A_					(							self       :      Dict ):


        '''simple docstring'''




        for model_class_name in self.all_model_classes:
          __UpperCAmelCase    :						Any       =						model_class_name.from_pretrained('''roberta-base'''						,				from_pt=__lowercase )
          __UpperCAmelCase    :						Union[str, Any]       =						model(np.ones((1, 1) ) )
          self.assertIsNotNone(__lowercase )






"""simple docstring"""





import gc
import unittest

from diffusers import FlaxDPMSolverMultistepScheduler, FlaxStableDiffusionPipeline
from diffusers.utils import is_flax_available, slow
from diffusers.utils.testing_utils import require_flax


if is_flax_available():
  import jax
  import jax.numpy as jnp
  from flax.jax_utils import replicate
  from flax.training.common_utils import shard




@slow
@require_flax
class 							snake_case							(			unittest.TestCase ):

      '''simple docstring'''



      def       A_					(							self       :      str ):


        '''simple docstring'''




        super().tearDown()
        gc.collect()

      def       A_					(							self       :      List[Any] ):


        '''simple docstring'''




        __UpperCAmelCase       ,     __UpperCAmelCase    :						List[Any]       =						FlaxStableDiffusionPipeline.from_pretrained(
            '''stabilityai/stable-diffusion-2'''						,				revision='''bf16'''						,				dtype=jnp.bfloataa						,				)

        __UpperCAmelCase    :						Dict       =						'''A painting of a squirrel eating a burger'''
        __UpperCAmelCase    :						Any       =						jax.device_count()
        __UpperCAmelCase    :						Optional[int]       =						num_samples * [prompt]
        __UpperCAmelCase    :						Tuple       =						sd_pipe.prepare_inputs(__lowercase )

        __UpperCAmelCase    :						List[Any]       =						replicate(__lowercase )
        __UpperCAmelCase    :						Optional[Any]       =						shard(__lowercase )

        __UpperCAmelCase    :						str       =						jax.random.PRNGKey(0 )
        __UpperCAmelCase    :						Dict       =						jax.random.split(__lowercase						,				jax.device_count() )

        __UpperCAmelCase    :						Tuple       =						sd_pipe(__lowercase						,				__lowercase						,				__lowercase						,				num_inference_steps=25						,				jit=__lowercase )[0]
        assert images.shape == (jax.device_count(), 1, 768, 768, 3)

        __UpperCAmelCase    :						Any       =						images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:] )
        __UpperCAmelCase    :						Union[str, Any]       =						images[0, 253:256, 253:256, -1]

        __UpperCAmelCase    :						str       =						jnp.asarray(jax.device_get(image_slice.flatten() ) )
        __UpperCAmelCase    :						List[Any]       =						jnp.array([0.4_2_3_8, 0.4_4_1_4, 0.4_3_9_5, 0.4_4_5_3, 0.4_6_2_9, 0.4_5_9_0, 0.4_5_3_1, 0.4_5_5_0_8, 0.4_5_1_2] )
        print(f'''output_slice: {output_slice}''' )
        assert jnp.abs(output_slice - expected_slice ).max() < 1e-2







      def       A_					(							self       :      Union[str, Any] ):


        '''simple docstring'''




        __UpperCAmelCase    :						Optional[int]       =						'''stabilityai/stable-diffusion-2'''
        __UpperCAmelCase       ,     __UpperCAmelCase    :						Any       =						FlaxDPMSolverMultistepScheduler.from_pretrained(__lowercase						,				subfolder='''scheduler''' )
        __UpperCAmelCase       ,     __UpperCAmelCase    :						Optional[int]       =						FlaxStableDiffusionPipeline.from_pretrained(
            __lowercase						,				scheduler=__lowercase						,				revision='''bf16'''						,				dtype=jnp.bfloataa						,				)
        __UpperCAmelCase    :						Dict       =						scheduler_params

        __UpperCAmelCase    :						int       =						'''A painting of a squirrel eating a burger'''
        __UpperCAmelCase    :						List[Any]       =						jax.device_count()
        __UpperCAmelCase    :						Optional[int]       =						num_samples * [prompt]
        __UpperCAmelCase    :						Tuple       =						sd_pipe.prepare_inputs(__lowercase )

        __UpperCAmelCase    :						Optional[int]       =						replicate(__lowercase )
        __UpperCAmelCase    :						List[Any]       =						shard(__lowercase )

        __UpperCAmelCase    :						Union[str, Any]       =						jax.random.PRNGKey(0 )
        __UpperCAmelCase    :						List[Any]       =						jax.random.split(__lowercase						,				jax.device_count() )

        __UpperCAmelCase    :						List[Any]       =						sd_pipe(__lowercase						,				__lowercase						,				__lowercase						,				num_inference_steps=25						,				jit=__lowercase )[0]
        assert images.shape == (jax.device_count(), 1, 768, 768, 3)

        __UpperCAmelCase    :						Tuple       =						images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:] )
        __UpperCAmelCase    :						Any       =						images[0, 253:256, 253:256, -1]

        __UpperCAmelCase    :						List[str]       =						jnp.asarray(jax.device_get(image_slice.flatten() ) )
        __UpperCAmelCase    :						Any       =						jnp.array([0.4_3_3_6, 0.4_2_9_6_9, 0.4_4_5_3, 0.4_1_9_9, 0.4_2_9_7, 0.4_5_3_1, 0.4_4_3_4, 0.4_4_3_4, 0.4_2_9_7] )
        print(f'''output_slice: {output_slice}''' )
        assert jnp.abs(output_slice - expected_slice ).max() < 1e-2