



"""simple docstring"""
from multiprocessing import Lock, Pipe, Process

# lock used to ensure that two processes do not access a pipe at the same time
UpperCAmelCase    :		List[Any]      =			Lock()


def       _SCREAMING_SNAKE_CASE    (__lowerCAmelCase			,	__lowerCAmelCase			,	__lowerCAmelCase			,	__lowerCAmelCase			,	__lowerCAmelCase			,	__lowerCAmelCase			,	__lowerCAmelCase				)			->    str:







  '''simple docstring'''


  global process_lock

  # we perform n swaps since after n swaps we know we are sorted
  # we *could* stop early if we are sorted already, but it takes as long to
  # find out we are sorted as it does to sort the list with this algorithm
  for i in range(0			,	10				):
    if (i + position) % 2 == 0 and r_send is not None:
      # send your value to your right neighbor
      process_lock.acquire()
      r_send[1].send(UpperCamelCase__				)
      process_lock.release()

      # receive your right neighbor's value
      process_lock.acquire()
      lowercase_             =			rr_cv[0].recv()
      process_lock.release()

      # take the lower value since you are on the left
      lowercase_             =			min(UpperCamelCase__			,	UpperCamelCase__				)
    elif (i + position) % 2 != 0 and l_send is not None:
      # send your value to your left neighbor
      process_lock.acquire()
      l_send[1].send(UpperCamelCase__				)
      process_lock.release()

      # receive your left neighbor's value
      process_lock.acquire()
      lowercase_             =			lr_cv[0].recv()
      process_lock.release()

      # take the higher value since you are on the right
      lowercase_             =			max(UpperCamelCase__			,	UpperCamelCase__				)
    # after all swaps are performed, send the values back to main
  result_pipe[1].send(UpperCamelCase__				)


def       _SCREAMING_SNAKE_CASE    (__lowerCAmelCase				)			->    int:







  '''simple docstring'''


  lowercase_             =			[]
  lowercase_             =			[]
  # initialize the list of pipes where the values will be retrieved
  for _ in arr:
    result_pipe.append(Pipe()				)
  # creates the processes
  # the first and last process only have one neighbor so they are made outside
  # of the loop
  lowercase_             =			Pipe()
  lowercase_             =			Pipe()
  process_array_.append(
      Process(
          target=UpperCamelCase__			,	args=(0, arr[0], None, temp_rs, None, temp_rr, result_pipe[0])			,	)				)
  lowercase_             =			temp_rs
  lowercase_             =			temp_rr

  for i in range(1			,	len(UpperCamelCase__				) - 1				):
    lowercase_             =			Pipe()
    lowercase_             =			Pipe()
    process_array_.append(
        Process(
            target=UpperCamelCase__			,	args=(i, arr[i], temp_ls, temp_rs, temp_lr, temp_rr, result_pipe[i])			,	)				)
    lowercase_             =			temp_rs
    lowercase_             =			temp_rr

  process_array_.append(
      Process(
          target=UpperCamelCase__			,	args=(
              len(UpperCamelCase__				) - 1,
              arr[len(UpperCamelCase__				) - 1],
              temp_ls,
              None,
              temp_lr,
              None,
              result_pipe[len(UpperCamelCase__				) - 1],
          )			,	)				)

  # start the processes
  for p in process_array_:
    p.start()

  # wait for the processes to end and write their values to the list
  for p in range(0			,	len(UpperCamelCase__				)				):
    lowercase_             =			result_pipe[p][0].recv()
    process_array_[p].join()
  return arr


def       _SCREAMING_SNAKE_CASE    ()			->    Union[str, Any]:







  '''simple docstring'''


  lowercase_             =			list(range(10			,	0			,	-1				)				)
  print("""Initial List"""				)
  print(*UpperCamelCase__				)
  lowercase_             =			odd_even_transposition(UpperCamelCase__				)
  print("""Sorted List\n"""				)
  print(*UpperCamelCase__				)


if __name__ == "__main__":
       main()








import gc
import unittest

import numpy as np
import torch

from diffusers import StableDiffusionKDiffusionPipeline
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu


enable_full_determinism()




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


							def   _snake_case	(				self     :Union[str, Any]		) -> List[str]:



									"""simple docstring"""



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


							def   _snake_case	(				self     :Any		) -> Optional[int]:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								=   StableDiffusionKDiffusionPipeline.from_pretrained("""CompVis/stable-diffusion-v1-4"""		)
									SCREAMING_SNAKE_CASE__								=   sd_pipe.to(__A		)
									sd_pipe.set_progress_bar_config(disable=__A		)

									sd_pipe.set_scheduler("""sample_euler"""		)

									SCREAMING_SNAKE_CASE__								=   """A painting of a squirrel eating a burger"""
									SCREAMING_SNAKE_CASE__								=   torch.manual_seed(0		)
									SCREAMING_SNAKE_CASE__								=   sd_pipe([prompt]				,					generator=__A				,					guidance_scale=9.0				,					num_inference_steps=20				,					output_type="""np"""		)

									SCREAMING_SNAKE_CASE__								=   output.images

									SCREAMING_SNAKE_CASE__								=   image[0, -3:, -3:, -1]

									assert image.shape == (1, 512, 512, 3)
									SCREAMING_SNAKE_CASE__								=   np.array([0.0_4_4_7, 0.0_4_9_2, 0.0_4_6_8, 0.0_4_0_8, 0.0_3_8_3, 0.0_4_0_8, 0.0_3_5_4, 0.0_3_8_0, 0.0_3_3_9]		)

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


							def   _snake_case	(				self     :str		) -> Union[str, Any]:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								=   StableDiffusionKDiffusionPipeline.from_pretrained("""stabilityai/stable-diffusion-2-1-base"""		)
									SCREAMING_SNAKE_CASE__								=   sd_pipe.to(__A		)
									sd_pipe.set_progress_bar_config(disable=__A		)

									sd_pipe.set_scheduler("""sample_euler"""		)

									SCREAMING_SNAKE_CASE__								=   """A painting of a squirrel eating a burger"""
									SCREAMING_SNAKE_CASE__								=   torch.manual_seed(0		)
									SCREAMING_SNAKE_CASE__								=   sd_pipe([prompt]				,					generator=__A				,					guidance_scale=9.0				,					num_inference_steps=20				,					output_type="""np"""		)

									SCREAMING_SNAKE_CASE__								=   output.images

									SCREAMING_SNAKE_CASE__								=   image[0, -3:, -3:, -1]

									assert image.shape == (1, 512, 512, 3)
									SCREAMING_SNAKE_CASE__								=   np.array([0.1_2_3_7, 0.1_3_2_0, 0.1_4_3_8, 0.1_3_5_9, 0.1_3_9_0, 0.1_1_3_2, 0.1_2_7_7, 0.1_1_7_5, 0.1_1_1_2]		)

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


							def   _snake_case	(				self     :Tuple		) -> Optional[Any]:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								=   StableDiffusionKDiffusionPipeline.from_pretrained("""stabilityai/stable-diffusion-2-1-base"""		)
									SCREAMING_SNAKE_CASE__								=   sd_pipe.to(__A		)
									sd_pipe.set_progress_bar_config(disable=__A		)

									sd_pipe.set_scheduler("""sample_dpmpp_2m"""		)

									SCREAMING_SNAKE_CASE__								=   """A painting of a squirrel eating a burger"""
									SCREAMING_SNAKE_CASE__								=   torch.manual_seed(0		)
									SCREAMING_SNAKE_CASE__								=   sd_pipe(
									    [prompt]				,					generator=__A				,					guidance_scale=7.5				,					num_inference_steps=15				,					output_type="""np"""				,					use_karras_sigmas=__A				,					)

									SCREAMING_SNAKE_CASE__								=   output.images

									SCREAMING_SNAKE_CASE__								=   image[0, -3:, -3:, -1]

									assert image.shape == (1, 512, 512, 3)
									SCREAMING_SNAKE_CASE__								=   np.array(
									    [0.1_1_3_8_1_6_8_9, 0.1_2_1_1_2_9_2_1, 0.1_3_8_9_4_5_7, 0.1_2_5_4_9_6_0_6, 0.1_2_4_4_9_6_4, 0.1_0_8_3_1_5_1_7, 0.1_1_5_6_2_8_6_6, 0.1_0_8_6_7_8_1_6, 0.1_0_4_9_9_0_4_8]		)

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