'''simple docstring'''




import gc
import unittest

from diffusers import FlaxControlNetModel, FlaxStableDiffusionControlNetPipeline
from diffusers.utils import is_flax_available, load_image, 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    a__				(       unittest.TestCase			):






     def        SCREAMING_SNAKE_CASE__       (			self		:		Tuple   ):





         """simple docstring"""



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






     def        SCREAMING_SNAKE_CASE__       (			self		:		Optional[Any]   ):





         """simple docstring"""



         __lowerCamelCase     ,  __lowerCamelCase          =  FlaxControlNetModel.from_pretrained(
             '''lllyasviel/sd-controlnet-canny'''  ,    from_pt=a  ,    dtype=jnp.bfloataa   )
         __lowerCamelCase     ,  __lowerCamelCase          =  FlaxStableDiffusionControlNetPipeline.from_pretrained(
             '''runwayml/stable-diffusion-v1-5'''  ,    controlnet=a  ,    from_pt=a  ,    dtype=jnp.bfloataa   )
         __lowerCamelCase          =  controlnet_params

         __lowerCamelCase          =  '''bird'''
         __lowerCamelCase          =  jax.device_count()
         __lowerCamelCase          =  pipe.prepare_text_inputs([prompts] * num_samples   )

         __lowerCamelCase          =  load_image(
             '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd_controlnet/bird_canny.png'''   )
         __lowerCamelCase          =  pipe.prepare_image_inputs([canny_image] * num_samples   )

         __lowerCamelCase          =  jax.random.PRNGKey(0   )
         __lowerCamelCase          =  jax.random.split(a  ,    jax.device_count()   )

         __lowerCamelCase          =  replicate(a   )
         __lowerCamelCase          =  shard(a   )
         __lowerCamelCase          =  shard(a   )

         __lowerCamelCase          =  pipe(
             prompt_ids=a  ,    image=a  ,    params=a  ,    prng_seed=a  ,    num_inference_steps=50  ,    jit=a  ,    ).images
         assert images.shape == (jax.device_count(), 1, 7_68, 5_12, 3)

         __lowerCamelCase          =  images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:]   )
         __lowerCamelCase          =  images[0, 2_53:2_56, 2_53:2_56, -1]

         __lowerCamelCase          =  jnp.asarray(jax.device_get(image_slice.flatten()   )   )
         __lowerCamelCase          =  jnp.array(
             [0.16_79_69, 0.11_66_99, 0.08_15_43, 0.15_42_97, 0.13_28_12, 0.10_88_87, 0.16_99_22, 0.16_99_22, 0.20_50_78]   )
         print(f"""output_slice: {output_slice}"""   )
         assert jnp.abs(output_slice - expected_slice   ).max() < 1e-2






     def        SCREAMING_SNAKE_CASE__       (			self		:		Any   ):





         """simple docstring"""



         __lowerCamelCase     ,  __lowerCamelCase          =  FlaxControlNetModel.from_pretrained(
             '''lllyasviel/sd-controlnet-openpose'''  ,    from_pt=a  ,    dtype=jnp.bfloataa   )
         __lowerCamelCase     ,  __lowerCamelCase          =  FlaxStableDiffusionControlNetPipeline.from_pretrained(
             '''runwayml/stable-diffusion-v1-5'''  ,    controlnet=a  ,    from_pt=a  ,    dtype=jnp.bfloataa   )
         __lowerCamelCase          =  controlnet_params

         __lowerCamelCase          =  '''Chef in the kitchen'''
         __lowerCamelCase          =  jax.device_count()
         __lowerCamelCase          =  pipe.prepare_text_inputs([prompts] * num_samples   )

         __lowerCamelCase          =  load_image(
             '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd_controlnet/pose.png'''   )
         __lowerCamelCase          =  pipe.prepare_image_inputs([pose_image] * num_samples   )

         __lowerCamelCase          =  jax.random.PRNGKey(0   )
         __lowerCamelCase          =  jax.random.split(a  ,    jax.device_count()   )

         __lowerCamelCase          =  replicate(a   )
         __lowerCamelCase          =  shard(a   )
         __lowerCamelCase          =  shard(a   )

         __lowerCamelCase          =  pipe(
             prompt_ids=a  ,    image=a  ,    params=a  ,    prng_seed=a  ,    num_inference_steps=50  ,    jit=a  ,    ).images
         assert images.shape == (jax.device_count(), 1, 7_68, 5_12, 3)

         __lowerCamelCase          =  images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:]   )
         __lowerCamelCase          =  images[0, 2_53:2_56, 2_53:2_56, -1]

         __lowerCamelCase          =  jnp.asarray(jax.device_get(image_slice.flatten()   )   )
         __lowerCamelCase          =  jnp.array(
             [[0.27_14_84, 0.26_17_19, 0.27_53_91, 0.27_73_44, 0.27_92_97, 0.29_10_16, 0.29_49_22, 0.30_27_34, 0.30_27_34]]   )
         print(f"""output_slice: {output_slice}"""   )
         assert jnp.abs(output_slice - expected_slice   ).max() < 1e-2

'''simple docstring'''




import time
import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_torch, torch_device

from ..test_modeling_common import ids_tensor


if is_torch_available():
  import torch

  from transformers.generation import (
      MaxLengthCriteria,
      MaxNewTokensCriteria,
      MaxTimeCriteria,
      StoppingCriteriaList,
      validate_stopping_criteria,
  )




@require_torch
class    a__				(       unittest.TestCase			):






     def        SCREAMING_SNAKE_CASE__       (			self		:		Optional[int]  ,    a		:		int   ):





         """simple docstring"""



         __lowerCamelCase          =  3
         __lowerCamelCase          =  2_50

         __lowerCamelCase          =  ids_tensor((batch_size, length)  ,    a   )
         __lowerCamelCase          =  torch.ones((batch_size, length)  ,    device=a  ,    dtype=torch.float   ) / length
         return input_ids, scores






     def        SCREAMING_SNAKE_CASE__       (			self		:		List[Any]   ):





         """simple docstring"""



         __lowerCamelCase     ,  __lowerCamelCase          =  self._get_tensors(5   )

         __lowerCamelCase          =  StoppingCriteriaList(
             [
                 MaxLengthCriteria(max_length=10   ),
                 MaxTimeCriteria(max_time=0.1   ),
             ]   )

         self.assertFalse(criteria(a  ,    a   )   )

         __lowerCamelCase     ,  __lowerCamelCase          =  self._get_tensors(9   )
         self.assertFalse(criteria(a  ,    a   )   )

         __lowerCamelCase     ,  __lowerCamelCase          =  self._get_tensors(10   )
         self.assertTrue(criteria(a  ,    a   )   )






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





         """simple docstring"""



         __lowerCamelCase          =  MaxLengthCriteria(max_length=10   )

         __lowerCamelCase     ,  __lowerCamelCase          =  self._get_tensors(5   )
         self.assertFalse(criteria(a  ,    a   )   )

         __lowerCamelCase     ,  __lowerCamelCase          =  self._get_tensors(9   )
         self.assertFalse(criteria(a  ,    a   )   )

         __lowerCamelCase     ,  __lowerCamelCase          =  self._get_tensors(10   )
         self.assertTrue(criteria(a  ,    a   )   )






     def        SCREAMING_SNAKE_CASE__       (			self		:		List[Any]   ):





         """simple docstring"""



         __lowerCamelCase          =  MaxNewTokensCriteria(start_length=5  ,    max_new_tokens=5   )

         __lowerCamelCase     ,  __lowerCamelCase          =  self._get_tensors(5   )
         self.assertFalse(criteria(a  ,    a   )   )

         __lowerCamelCase     ,  __lowerCamelCase          =  self._get_tensors(9   )
         self.assertFalse(criteria(a  ,    a   )   )

         __lowerCamelCase     ,  __lowerCamelCase          =  self._get_tensors(10   )
         self.assertTrue(criteria(a  ,    a   )   )

         __lowerCamelCase          =  StoppingCriteriaList([criteria]   )
         self.assertEqual(criteria_list.max_length  ,    10   )






     def        SCREAMING_SNAKE_CASE__       (			self		:		int   ):





         """simple docstring"""



         __lowerCamelCase     ,  __lowerCamelCase          =  self._get_tensors(5   )

         __lowerCamelCase          =  MaxTimeCriteria(max_time=0.1   )
         self.assertFalse(criteria(a  ,    a   )   )

         __lowerCamelCase          =  MaxTimeCriteria(max_time=0.1  ,    initial_timestamp=time.time() - 0.2   )
         self.assertTrue(criteria(a  ,    a   )   )






     def        SCREAMING_SNAKE_CASE__       (			self		:		List[Any]   ):





         """simple docstring"""



         validate_stopping_criteria(StoppingCriteriaList([MaxLengthCriteria(10   )]   )  ,    10   )

         with self.assertWarns(a   ):
             validate_stopping_criteria(StoppingCriteriaList([MaxLengthCriteria(10   )]   )  ,    11   )

         __lowerCamelCase          =  validate_stopping_criteria(StoppingCriteriaList()  ,    11   )

         self.assertEqual(len(a   )  ,    1   )
