
"""simple docstring"""

import os
import sys
import unittest


SCREAMING_SNAKE_CASE_    					=						os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
sys.path.append(os.path.join(git_repo_path, '''utils'''))

import check_dummies  # noqa: E402
from check_dummies import create_dummy_files, create_dummy_object, find_backend, read_init  # noqa: E402


# Align TRANSFORMERS_PATH in check_dummies with the current path
SCREAMING_SNAKE_CASE_    					=						os.path.join(git_repo_path, '''src''', '''diffusers''')
class      _UpperCAmelCase							(		unittest.TestCase							):




      def     a_      (							self							)						->					str:
          UpperCAmelCase									=					find_backend('    if not is_torch_available():'							)
          self.assertEqual(lowercase_   ,       'torch'							)

          # backend_with_underscore = find_backend("    if not is_tensorflow_text_available():")
          # self.assertEqual(backend_with_underscore, "tensorflow_text")

          UpperCAmelCase									=					find_backend('    if not (is_torch_available() and is_transformers_available()):'							)
          self.assertEqual(lowercase_   ,       'torch_and_transformers'							)

          # double_backend_with_underscore = find_backend(
          #    "    if not (is_sentencepiece_available() and is_tensorflow_text_available()):"
          # )
          # self.assertEqual(double_backend_with_underscore, "sentencepiece_and_tensorflow_text")

          UpperCAmelCase									=					find_backend(
              '    if not (is_torch_available() and is_transformers_available() and is_onnx_available()):'							)
          self.assertEqual(lowercase_   ,       'torch_and_transformers_and_onnx'							)




      def     a_      (							self							)						->					Any:
          UpperCAmelCase									=					read_init()
          # We don't assert on the exact list of keys to allow for smooth grow of backend-specific objects
          self.assertIn('torch'   ,       lowercase_							)
          self.assertIn('torch_and_transformers'   ,       lowercase_							)
          self.assertIn('flax_and_transformers'   ,       lowercase_							)
          self.assertIn('torch_and_transformers_and_onnx'   ,       lowercase_							)

          # Likewise, we can't assert on the exact content of a key
          self.assertIn('UNet2DModel'   ,       objects['torch']							)
          self.assertIn('FlaxUNet2DConditionModel'   ,       objects['flax']							)
          self.assertIn('StableDiffusionPipeline'   ,       objects['torch_and_transformers']							)
          self.assertIn('FlaxStableDiffusionPipeline'   ,       objects['flax_and_transformers']							)
          self.assertIn('LMSDiscreteScheduler'   ,       objects['torch_and_scipy']							)
          self.assertIn('OnnxStableDiffusionPipeline'   ,       objects['torch_and_transformers_and_onnx']							)




      def     a_      (							self							)						->					Optional[int]:
          UpperCAmelCase									=					create_dummy_object('CONSTANT'   ,       '\'torch\''							)
          self.assertEqual(lowercase_   ,       '\nCONSTANT = None\n'							)

          UpperCAmelCase									=					create_dummy_object('function'   ,       '\'torch\''							)
          self.assertEqual(
              lowercase_   ,       '\ndef function(*args, **kwargs):\n    requires_backends(function, \'torch\')\n'							)

          UpperCAmelCase									=					'\nclass FakeClass(metaclass=DummyObject):\n    _backends = \'torch\'\n\n    def __init__(self, *args, **kwargs):\n        requires_backends(self, \'torch\')\n\n    @classmethod\n    def from_config(cls, *args, **kwargs):\n        requires_backends(cls, \'torch\')\n\n    @classmethod\n    def from_pretrained(cls, *args, **kwargs):\n        requires_backends(cls, \'torch\')\n'
          UpperCAmelCase									=					create_dummy_object('FakeClass'   ,       '\'torch\''							)
          self.assertEqual(lowercase_   ,       lowercase_							)




      def     a_      (							self							)						->					Tuple:
          UpperCAmelCase									=					'# This file is autogenerated by the command `make fix-copies`, do not edit.\nfrom ..utils import DummyObject, requires_backends\n\n\nCONSTANT = None\n\n\ndef function(*args, **kwargs):\n    requires_backends(function, ["torch"])\n\n\nclass FakeClass(metaclass=DummyObject):\n    _backends = ["torch"]\n\n    def __init__(self, *args, **kwargs):\n        requires_backends(self, ["torch"])\n\n    @classmethod\n    def from_config(cls, *args, **kwargs):\n        requires_backends(cls, ["torch"])\n\n    @classmethod\n    def from_pretrained(cls, *args, **kwargs):\n        requires_backends(cls, ["torch"])\n'
          UpperCAmelCase									=					create_dummy_files({'torch': ['CONSTANT', 'function', 'FakeClass']}							)
          self.assertEqual(dummy_files['torch']   ,       lowercase_							)





"""simple docstring"""

from multiprocessing import Lock, Pipe, Process

# lock used to ensure that two processes do not access a pipe at the same time
SCREAMING_SNAKE_CASE_    					=						Lock()




def     lowercase__						(					lowerCAmelCase				:					Dict ,				lowerCAmelCase				:					Dict ,				lowerCAmelCase				:					Union[str, Any] ,				lowerCAmelCase				:					Optional[Any] ,				lowerCAmelCase				:					str ,				lowerCAmelCase				:					Optional[int] ,				lowerCAmelCase				:					Optional[Any] ) ->      Optional[int]:






    """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(lowerCAmelCase )
            process_lock.release()

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

            # take the lower value since you are on the left
            UpperCAmelCase									=					min(lowerCAmelCase ,				lowerCAmelCase )
        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(lowerCAmelCase )
            process_lock.release()

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

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




def     lowercase__						(					lowerCAmelCase				:					Union[str, Any] ) ->      List[str]:






    """simple docstring"""




    UpperCAmelCase									=					[]
    UpperCAmelCase									=					[]
    # 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
    UpperCAmelCase									=					Pipe()
    UpperCAmelCase									=					Pipe()
    process_array_.append(
        Process(
            target=lowerCAmelCase ,				args=(0, arr[0], None, temp_rs, None, temp_rr, result_pipe[0]) ,				) )
    UpperCAmelCase									=					temp_rs
    UpperCAmelCase									=					temp_rr

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

    process_array_.append(
        Process(
            target=lowerCAmelCase ,				args=(
                len(lowerCAmelCase ) - 1,
                arr[len(lowerCAmelCase ) - 1],
                temp_ls,
                None,
                temp_lr,
                None,
                result_pipe[len(lowerCAmelCase ) - 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(lowerCAmelCase ) ):
        UpperCAmelCase									=					result_pipe[p][0].recv()
        process_array_[p].join()
    return arr




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






    """simple docstring"""




    UpperCAmelCase									=					list(range(10 ,				0 ,				-1 ) )
    print('Initial List' )
    print(*lowerCAmelCase )
    UpperCAmelCase									=					odd_even_transposition(lowerCAmelCase )
    print('Sorted List\n' )
    print(*lowerCAmelCase )


if __name__ == "__main__":
   main()



