


import unittest

import numpy as np

from transformers import is_flax_available
from transformers.testing_utils import require_flax

from ..test_modeling_flax_common import ids_tensor


if is_flax_available():
		import jax
		import jax.numpy as jnp

		from transformers.generation import (
		    FlaxForcedBOSTokenLogitsProcessor,
		    FlaxForcedEOSTokenLogitsProcessor,
		    FlaxLogitsProcessorList,
		    FlaxMinLengthLogitsProcessor,
		    FlaxTemperatureLogitsWarper,
		    FlaxTopKLogitsWarper,
		    FlaxTopPLogitsWarper,
		)

@require_flax
class 						__a (							unittest.TestCase     ):
		def       UpperCAmelCase__							(     self     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE		)					->			Union[str, Any]:




									"""simple docstring"""





									_UpperCAmelCase											=     jnp.ones((batch_size, length)		) / length
									return scores
		def       UpperCAmelCase__							(     self		)					->			Tuple:




									"""simple docstring"""





									_UpperCAmelCase											=     None
									_UpperCAmelCase											=     20

									_UpperCAmelCase											=     self._get_uniform_logits(batch_size=2     ,       length=__A		)

									# tweak scores to not be uniform anymore
									_UpperCAmelCase											=     scores.at[1, 5].set((1 / length) + 0.1		)  # peak, 1st batch
									_UpperCAmelCase											=     scores.at[1, 10].set((1 / length) - 0.4		)  # valley, 1st batch

									# compute softmax
									_UpperCAmelCase											=     jax.nn.softmax(__A     ,       axis=-1		)

									_UpperCAmelCase											=     FlaxTemperatureLogitsWarper(temperature=0.5		)
									_UpperCAmelCase											=     FlaxTemperatureLogitsWarper(temperature=1.3		)

									_UpperCAmelCase											=     jax.nn.softmax(temp_dist_warper_sharper(__A     ,       scores.copy()     ,       cur_len=__A		)     ,       axis=-1		)
									_UpperCAmelCase											=     jax.nn.softmax(temp_dist_warper_smoother(__A     ,       scores.copy()     ,       cur_len=__A		)     ,       axis=-1		)

									# uniform distribution stays uniform
									self.assertTrue(jnp.allclose(probs[0, :]     ,       warped_prob_sharp[0, :]     ,       atol=1e-3		)		)
									self.assertTrue(jnp.allclose(probs[0, :]     ,       warped_prob_smooth[0, :]     ,       atol=1e-3		)		)

									# sharp peaks get higher, valleys get lower
									self.assertLess(probs[1, :].max()     ,       warped_prob_sharp[1, :].max()		)
									self.assertGreater(probs[1, :].min()     ,       warped_prob_sharp[1, :].min()		)

									# smooth peaks get lower, valleys get higher
									self.assertGreater(probs[1, :].max()     ,       warped_prob_smooth[1, :].max()		)
									self.assertLess(probs[1, :].min()     ,       warped_prob_smooth[1, :].min()		)
		def       UpperCAmelCase__							(     self		)					->			Union[str, Any]:




									"""simple docstring"""





									_UpperCAmelCase											=     None
									_UpperCAmelCase											=     10
									_UpperCAmelCase											=     2

									# create ramp distribution
									_UpperCAmelCase											=     np.broadcast_to(np.arange(__A		)[None, :]     ,       (batch_size, vocab_size)		).copy()
									_UpperCAmelCase											=     ramp_logits[1:, : vocab_size // 2] + vocab_size

									_UpperCAmelCase											=     FlaxTopKLogitsWarper(3		)

									_UpperCAmelCase											=     top_k_warp(__A     ,       __A     ,       cur_len=__A		)

									# check that correct tokens are filtered
									self.assertListEqual(jnp.isinf(scores[0]		).tolist()     ,       7 * [True] + 3 * [False]		)
									self.assertListEqual(jnp.isinf(scores[1]		).tolist()     ,       2 * [True] + 3 * [False] + 5 * [True]		)

									# check special case
									_UpperCAmelCase											=     5
									_UpperCAmelCase											=     FlaxTopKLogitsWarper(top_k=1     ,       filter_value=0.0     ,       min_tokens_to_keep=3		)

									_UpperCAmelCase											=     np.broadcast_to(np.arange(__A		)[None, :]     ,       (batch_size, length)		).copy()
									_UpperCAmelCase											=     top_k_warp_safety_check(__A     ,       __A     ,       cur_len=__A		)

									# min_tokens overwrites k: 3 tokens are kept => 2 tokens are nullified
									self.assertListEqual((scores == 0.0).sum(axis=-1		).tolist()     ,       [2, 2]		)
		def       UpperCAmelCase__							(     self		)					->			int:




									"""simple docstring"""





									_UpperCAmelCase											=     None
									_UpperCAmelCase											=     10
									_UpperCAmelCase											=     2

									# create distribution and take log (inverse to Softmax as taken in TopPLogitsWarper)
									_UpperCAmelCase											=     np.log(np.array([[0.3, 0.1, 0.1, 0.5], [0.15, 0.3, 0.3, 0.25]]		)		)

									_UpperCAmelCase											=     FlaxTopPLogitsWarper(0.8		)
									_UpperCAmelCase											=     np.exp(top_p_warp(__A     ,       __A     ,       cur_len=__A		)		)

									# dist should be filtered to keep min num values so that sum is >= top_p
									# exp (-inf) => 0
									_UpperCAmelCase											=     np.array([[0.3, 0.0, 0.0, 0.5], [0.0, 0.3, 0.3, 0.25]]		)
									self.assertTrue(np.allclose(__A     ,       __A     ,       atol=1e-3		)		)

									# check edge cases with negative and extreme logits
									_UpperCAmelCase											=     np.broadcast_to(np.arange(__A		)[None, :]     ,       (batch_size, vocab_size)		).copy() - (
									    vocab_size // 2
									)

									# make ramp_logits more extreme
									_UpperCAmelCase											=     ramp_logits[1] * 100.0

									# make sure at least 2 tokens are kept
									_UpperCAmelCase											=     FlaxTopPLogitsWarper(0.9     ,       min_tokens_to_keep=2     ,       filter_value=0.0		)
									_UpperCAmelCase											=     top_p_warp(__A     ,       __A     ,       cur_len=__A		)

									# first batch should keep three tokens, second batch would keep only 1, but due to `min_tokens_to_keep=2` keeps 2.
									self.assertListEqual((filtered_dist != 0.0).sum(axis=-1		).tolist()     ,       [3, 2]		)
		def       UpperCAmelCase__							(     self		)					->			Optional[int]:




									"""simple docstring"""





									_UpperCAmelCase											=     20
									_UpperCAmelCase											=     4
									_UpperCAmelCase											=     0

									_UpperCAmelCase											=     FlaxMinLengthLogitsProcessor(min_length=10     ,       eos_token_id=__A		)

									# check that min length is applied at length 5
									_UpperCAmelCase											=     ids_tensor((batch_size, 20)     ,       vocab_size=20		)
									_UpperCAmelCase											=     5
									_UpperCAmelCase											=     self._get_uniform_logits(__A     ,       __A		)
									_UpperCAmelCase											=     min_dist_processor(__A     ,       __A     ,       cur_len=__A		)
									self.assertListEqual(scores_before_min_length[:, eos_token_id].tolist()     ,       4 * [-float('inf'		)]		)

									# check that min length is not applied anymore at length 15
									_UpperCAmelCase											=     self._get_uniform_logits(__A     ,       __A		)
									_UpperCAmelCase											=     15
									_UpperCAmelCase											=     min_dist_processor(__A     ,       __A     ,       cur_len=__A		)
									self.assertFalse(jnp.isinf(__A		).any()		)
		def       UpperCAmelCase__							(     self		)					->			Optional[Any]:




									"""simple docstring"""





									_UpperCAmelCase											=     20
									_UpperCAmelCase											=     4
									_UpperCAmelCase											=     0

									_UpperCAmelCase											=     FlaxForcedBOSTokenLogitsProcessor(bos_token_id=__A		)

									# check that all scores are -inf except the bos_token_id score
									_UpperCAmelCase											=     ids_tensor((batch_size, 1)     ,       vocab_size=20		)
									_UpperCAmelCase											=     1
									_UpperCAmelCase											=     self._get_uniform_logits(__A     ,       __A		)
									_UpperCAmelCase											=     logits_processor(__A     ,       __A     ,       cur_len=__A		)
									self.assertTrue(jnp.isneginf(scores[:, bos_token_id + 1 :]		).all()		)
									self.assertListEqual(scores[:, bos_token_id].tolist()     ,       4 * [0]		)  # score for bos_token_id shold be zero

									# check that bos_token_id is not forced if current length is greater than 1
									_UpperCAmelCase											=     3
									_UpperCAmelCase											=     self._get_uniform_logits(__A     ,       __A		)
									_UpperCAmelCase											=     logits_processor(__A     ,       __A     ,       cur_len=__A		)
									self.assertFalse(jnp.isinf(__A		).any()		)
		def       UpperCAmelCase__							(     self		)					->			int:




									"""simple docstring"""





									_UpperCAmelCase											=     20
									_UpperCAmelCase											=     4
									_UpperCAmelCase											=     0
									_UpperCAmelCase											=     5

									_UpperCAmelCase											=     FlaxForcedEOSTokenLogitsProcessor(max_length=__A     ,       eos_token_id=__A		)

									# check that all scores are -inf except the eos_token_id when max_length is reached
									_UpperCAmelCase											=     ids_tensor((batch_size, 4)     ,       vocab_size=20		)
									_UpperCAmelCase											=     4
									_UpperCAmelCase											=     self._get_uniform_logits(__A     ,       __A		)
									_UpperCAmelCase											=     logits_processor(__A     ,       __A     ,       cur_len=__A		)
									self.assertTrue(jnp.isneginf(scores[:, eos_token_id + 1 :]		).all()		)
									self.assertListEqual(scores[:, eos_token_id].tolist()     ,       4 * [0]		)  # score for eos_token_id should be zero

									# check that eos_token_id is not forced if max_length is not reached
									_UpperCAmelCase											=     3
									_UpperCAmelCase											=     self._get_uniform_logits(__A     ,       __A		)
									_UpperCAmelCase											=     logits_processor(__A     ,       __A     ,       cur_len=__A		)
									self.assertFalse(jnp.isinf(__A		).any()		)
		def       UpperCAmelCase__							(     self		)					->			Optional[Any]:




									"""simple docstring"""





									_UpperCAmelCase											=     4
									_UpperCAmelCase											=     10
									_UpperCAmelCase											=     15
									_UpperCAmelCase											=     2
									_UpperCAmelCase											=     1
									_UpperCAmelCase											=     15

									# dummy input_ids and scores
									_UpperCAmelCase											=     ids_tensor((batch_size, sequence_length)     ,       __A		)
									_UpperCAmelCase											=     input_ids.copy()

									_UpperCAmelCase											=     self._get_uniform_logits(__A     ,       __A		)
									_UpperCAmelCase											=     scores.copy()

									# instantiate all dist processors
									_UpperCAmelCase											=     FlaxTemperatureLogitsWarper(temperature=0.5		)
									_UpperCAmelCase											=     FlaxTopKLogitsWarper(3		)
									_UpperCAmelCase											=     FlaxTopPLogitsWarper(0.8		)

									# instantiate all logits processors
									_UpperCAmelCase											=     FlaxMinLengthLogitsProcessor(min_length=10     ,       eos_token_id=__A		)
									_UpperCAmelCase											=     FlaxForcedBOSTokenLogitsProcessor(bos_token_id=__A		)
									_UpperCAmelCase											=     FlaxForcedEOSTokenLogitsProcessor(max_length=__A     ,       eos_token_id=__A		)

									_UpperCAmelCase											=     10

									# no processor list
									_UpperCAmelCase											=     temp_dist_warp(__A     ,       __A     ,       cur_len=__A		)
									_UpperCAmelCase											=     top_k_warp(__A     ,       __A     ,       cur_len=__A		)
									_UpperCAmelCase											=     top_p_warp(__A     ,       __A     ,       cur_len=__A		)
									_UpperCAmelCase											=     min_dist_proc(__A     ,       __A     ,       cur_len=__A		)
									_UpperCAmelCase											=     bos_dist_proc(__A     ,       __A     ,       cur_len=__A		)
									_UpperCAmelCase											=     eos_dist_proc(__A     ,       __A     ,       cur_len=__A		)

									# with processor list
									_UpperCAmelCase											=     FlaxLogitsProcessorList(
									    [temp_dist_warp, top_k_warp, top_p_warp, min_dist_proc, bos_dist_proc, eos_dist_proc]		)
									_UpperCAmelCase											=     processor(__A     ,       __A     ,       cur_len=__A		)

									# scores should be equal
									self.assertTrue(jnp.allclose(__A     ,       __A     ,       atol=1e-3		)		)

									# input_ids should never be changed
									self.assertListEqual(input_ids.tolist()     ,       input_ids_comp.tolist()		)






		def       UpperCAmelCase__							(     self		)					->			Optional[int]:




									"""simple docstring"""





									_UpperCAmelCase											=     4
									_UpperCAmelCase											=     10
									_UpperCAmelCase											=     15
									_UpperCAmelCase											=     2
									_UpperCAmelCase											=     1
									_UpperCAmelCase											=     15

									# dummy input_ids and scores
									_UpperCAmelCase											=     ids_tensor((batch_size, sequence_length)     ,       __A		)
									_UpperCAmelCase											=     input_ids.copy()

									_UpperCAmelCase											=     self._get_uniform_logits(__A     ,       __A		)
									_UpperCAmelCase											=     scores.copy()

									# instantiate all dist processors
									_UpperCAmelCase											=     FlaxTemperatureLogitsWarper(temperature=0.5		)
									_UpperCAmelCase											=     FlaxTopKLogitsWarper(3		)
									_UpperCAmelCase											=     FlaxTopPLogitsWarper(0.8		)

									# instantiate all logits processors
									_UpperCAmelCase											=     FlaxMinLengthLogitsProcessor(min_length=10     ,       eos_token_id=__A		)
									_UpperCAmelCase											=     FlaxForcedBOSTokenLogitsProcessor(bos_token_id=__A		)
									_UpperCAmelCase											=     FlaxForcedEOSTokenLogitsProcessor(max_length=__A     ,       eos_token_id=__A		)

									_UpperCAmelCase											=     10

									# no processor list
									def run_no_processor_list(_SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE		):
																_UpperCAmelCase											=     temp_dist_warp(__A     ,       __A     ,       cur_len=__A		)
																_UpperCAmelCase											=     top_k_warp(__A     ,       __A     ,       cur_len=__A		)
																_UpperCAmelCase											=     top_p_warp(__A     ,       __A     ,       cur_len=__A		)
																_UpperCAmelCase											=     min_dist_proc(__A     ,       __A     ,       cur_len=__A		)
																_UpperCAmelCase											=     bos_dist_proc(__A     ,       __A     ,       cur_len=__A		)
																_UpperCAmelCase											=     eos_dist_proc(__A     ,       __A     ,       cur_len=__A		)
																return scores

									# with processor list
									def run_processor_list(_SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE		):
																_UpperCAmelCase											=     FlaxLogitsProcessorList(
																    [temp_dist_warp, top_k_warp, top_p_warp, min_dist_proc, bos_dist_proc, eos_dist_proc]		)
																_UpperCAmelCase											=     processor(__A     ,       __A     ,       cur_len=__A		)
																return scores

									_UpperCAmelCase											=     jax.jit(__A		)
									_UpperCAmelCase											=     jax.jit(__A		)

									_UpperCAmelCase											=     jitted_run_no_processor_list(__A     ,       __A     ,       __A		)
									_UpperCAmelCase											=     jitted_run_processor_list(__A     ,       __A     ,       __A		)

									# scores should be equal
									self.assertTrue(jnp.allclose(__A     ,       __A     ,       atol=1e-3		)		)

									# input_ids should never be changed
									self.assertListEqual(input_ids.tolist()     ,       input_ids_comp.tolist()		)




def  a   (lowerCAmelCase__   ):
   __a					=  False
   while is_sorted is False:  # Until all the indices are traversed keep looping
      __a					=  True
      for i in range(0		,				len(lowerCAmelCase__   ) - 1		,				2   ):  # iterating over all even indices
         if input_list[i] > input_list[i + 1]:
            __a	,    __a					=  input_list[i + 1], input_list[i]
            # swapping if elements not in order
            __a					=  False

      for i in range(1		,				len(lowerCAmelCase__   ) - 1		,				2   ):  # iterating over all odd indices
         if input_list[i] > input_list[i + 1]:
            __a	,    __a					=  input_list[i + 1], input_list[i]
            # swapping if elements not in order
            __a					=  False
   return input_list


if __name__ == "__main__":
  print('Enter list to be sorted')
  SCREAMING_SNAKE_CASE							    =							[int(x) for x in input().split()]
  # inputing elements of the list in one line
  SCREAMING_SNAKE_CASE							    =							odd_even_sort(input_list)
  print('The sorted list is')
  print(sorted_list)



