




from __future__ import annotations


def 						lowerCamelCase__   (		_A					,     _A							):







	'''simple docstring'''





	snake_case_		   =					0
	snake_case_		   =					len(_A							) - 1
	while left <= right:
		snake_case_		   =					(left + right) // 2
		if left == right:
			if sorted_list[middle] < item:
				snake_case_		   =					middle + 1
			break
		elif sorted_list[middle] < item:
			snake_case_		   =					middle + 1
		else:
			snake_case_		   =					middle - 1
	sorted_list.insert(_A					,     _A							)
	return sorted_list


def 						lowerCamelCase__   (		_A					,     _A							):







	'''simple docstring'''





	snake_case_		   =					[]
	while left and right:
		if left[0][0] < right[0][0]:
			result.append(left.pop(0							)							)
		else:
			result.append(right.pop(0							)							)
	return result + left + right


def 						lowerCamelCase__   (		_A							):







	'''simple docstring'''





	snake_case_		   =					len(_A							)
	if length <= 1:
		return list_ad
	snake_case_		   =					length // 2
	return merge(sortlist_ad(list_ad[:middle]							)					,     sortlist_ad(list_ad[middle:]							)							)


def 						lowerCamelCase__   (		_A							):







	'''simple docstring'''





	if len(_A							) <= 1:
		return collection
	snake_case_		   =					[]
	snake_case_		   =					False
	for i in range(0					,     len(_A							)					,     2							):
		if i == len(_A							) - 1:
			snake_case_		   =					True
		else:
			if collection[i] < collection[i + 1]:
				two_paired_list.append([collection[i], collection[i + 1]]							)
			else:
				two_paired_list.append([collection[i + 1], collection[i]]							)
	snake_case_		   =					sortlist_ad(_A							)
	snake_case_		   =					[i[0] for i in sorted_list_ad]
	result.append(sorted_list_ad[-1][1]							)
	if has_last_odd_item:
		snake_case_		   =					collection[-1]
		snake_case_		   =					binary_search_insertion(_A					,     _A							)
	snake_case_		   =					False
	for i in range(len(_A							) - 1							):
		if result[i] == collection[-1] and has_last_odd_item:
			snake_case_		   =					True
		snake_case_		   =					sorted_list_ad[i][1]
		# If last_odd_item is inserted before the item's index,
		# you should forward index one more.
		if is_last_odd_item_inserted_before_this_index:
			snake_case_		   =					result[: i + 2] + binary_search_insertion(result[i + 2 :]					,     _A							)
		else:
			snake_case_		   =					result[: i + 1] + binary_search_insertion(result[i + 1 :]					,     _A							)

	return result


if __name__ == "__main__":
		lowercase__    :					List[str]			 =						input("Enter numbers separated by a comma:\n").strip()
		lowercase__    :					Optional[Any]			 =						[int(item) for item in user_input.split(",")]
		print(merge_insertion_sort(unsorted))






import unittest
from functools import partial

import torch

from accelerate import Accelerator, debug_launcher
from accelerate.state import AcceleratorState, GradientState
from accelerate.test_utils import require_cpu, require_huggingface_suite
from accelerate.utils import GradientAccumulationPlugin


def 						lowerCamelCase__   (		_A=2					,     _A=True					,     _A=False							):







	'''simple docstring'''





	snake_case_		   =					Accelerator(step_scheduler_with_optimizer=_A					,     split_batches=_A							)
	snake_case_		   =					torch.nn.Linear(2					,     4							)
	snake_case_		   =					torch.optim.AdamW(model.parameters()					,     lr=1.0							)
	snake_case_		   =					torch.optim.lr_scheduler.OneCycleLR(_A					,     max_lr=0.01					,     steps_per_epoch=2					,     epochs=1							)
	snake_case_   ,				snake_case_   ,				snake_case_		   =					accelerator.prepare(_A					,     _A					,     _A							)

	# Optimizer has stepped
	scheduler.step()
	if step_scheduler_with_optimizer or (num_processes == 1):
		assert (
		    scheduler.scheduler.last_epoch == num_processes
		), f"Last Epoch ({scheduler.scheduler.last_epoch}) != Num Processes ({num_processes})"
	else:
		assert (
		    scheduler.scheduler.last_epoch != num_processes
		), f"Last Epoch ({scheduler.scheduler.last_epoch}) == Num Processes ({num_processes})"


def 						lowerCamelCase__   (		_A=2					,     _A=True					,     _A=False							):







	'''simple docstring'''





	snake_case_		   =					Accelerator(step_scheduler_with_optimizer=_A					,     split_batches=_A							)
	snake_case_		   =					torch.nn.Linear(2					,     4							)
	snake_case_		   =					torch.optim.AdamW(model.parameters()					,     lr=1.0							)
	snake_case_		   =					torch.optim.lr_scheduler.LambdaLR(_A					,     lr_lambda=lambda _A							: 1 - n / 10							)
	snake_case_   ,				snake_case_   ,				snake_case_		   =					accelerator.prepare(_A					,     _A					,     _A							)

	# Optimizer has stepped
	snake_case_		   =					False
	scheduler.step()
	snake_case_		   =					1 - (num_processes if (step_scheduler_with_optimizer and not split_batches) else 1) / 10
	assert (
	    scheduler.get_last_lr()[0] == expected_lr
	), f"Wrong lr found at first step, expected {expected_lr}, got {scheduler.get_last_lr()[0]}"

	# Optimizer has not stepped
	snake_case_		   =					True
	scheduler.step()
	if not step_scheduler_with_optimizer:
		snake_case_		   =					1 - 2 / 10
	assert (
	    scheduler.get_last_lr()[0] == expected_lr
	), f"Wrong lr found at second step, expected {expected_lr}, got {scheduler.get_last_lr()[0]}"


def 						lowerCamelCase__   (		_A = 2							):







	'''simple docstring'''





	from transformers import get_linear_schedule_with_warmup

	snake_case_		   =					[1, 2, 4]
	for num_steps in steps:
		snake_case_		   =					GradientAccumulationPlugin(num_steps=_A					,     adjust_scheduler=num_steps > 1							)
		snake_case_		   =					Accelerator(gradient_accumulation_plugin=_A							)
		snake_case_		   =					torch.nn.Linear(2					,     4							)
		snake_case_		   =					torch.optim.AdamW(model.parameters()					,     lr=10.0							)
		snake_case_		   =					get_linear_schedule_with_warmup(optimizer=_A					,     num_warmup_steps=0					,     num_training_steps=20							)

		snake_case_   ,				snake_case_   ,				snake_case_		   =					accelerator.prepare(_A					,     _A					,     _A							)

		for i in range(10 * num_steps							):
			with accelerator.accumulate(_A							):
				optimizer.step()
				scheduler.step()

			if i == (10 * num_steps - 2):
				assert (
				    scheduler.get_last_lr()[0] != 0
				), f"Wrong lr found at second-to-last step, expected non-zero, got {scheduler.get_last_lr()[0]}. num_steps: {num_steps}"
		assert (
		    scheduler.get_last_lr()[0] == 0
		), f"Wrong lr found at last step, expected 0, got {scheduler.get_last_lr()[0]}"
		GradientState._reset_state()






@require_cpu
class 		UpperCAmelCase       (    unittest.TestCase      ):







						'''simple docstring'''





						def 	snake_case__						(						self							:					Tuple						):






							"""simple docstring"""






							debug_launcher(partial(__lowercase	,     num_processes=1						)	,     num_processes=1						)
							debug_launcher(partial(__lowercase	,     num_processes=1	,     split_batches=__lowercase						)	,     num_processes=1						)






						def 	snake_case__						(						self							:					int						):






							"""simple docstring"""






							debug_launcher(partial(__lowercase	,     num_processes=1						)	,     num_processes=1						)
							debug_launcher(partial(__lowercase	,     num_processes=1	,     split_batches=__lowercase						)	,     num_processes=1						)






						def 	snake_case__						(						self							:					Dict						):






							"""simple docstring"""






							debug_launcher(partial(__lowercase	,     num_processes=1	,     step_scheduler_with_optimizer=__lowercase						)	,     num_processes=1						)






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






							"""simple docstring"""






							debug_launcher(partial(__lowercase	,     num_processes=1	,     step_scheduler_with_optimizer=__lowercase						)	,     num_processes=1						)






						def 	snake_case__						(						self							:					List[Any]						):






							"""simple docstring"""






							AcceleratorState._reset_state(__lowercase						)
							debug_launcher(__lowercase						)
							debug_launcher(partial(__lowercase	,     num_processes=1	,     split_batches=__lowercase						)	,     num_processes=1						)






						def 	snake_case__						(						self							:					Dict						):






							"""simple docstring"""






							AcceleratorState._reset_state(__lowercase						)
							debug_launcher(__lowercase						)
							debug_launcher(partial(__lowercase	,     num_processes=1	,     split_batches=__lowercase						)	,     num_processes=1						)






						def 	snake_case__						(						self							:					List[str]						):






							"""simple docstring"""






							AcceleratorState._reset_state(__lowercase						)
							debug_launcher(partial(__lowercase	,     step_scheduler_with_optimizer=__lowercase						)						)






						def 	snake_case__						(						self							:					Optional[Any]						):






							"""simple docstring"""






							AcceleratorState._reset_state(__lowercase						)
							debug_launcher(partial(__lowercase	,     step_scheduler_with_optimizer=__lowercase						)						)






						@require_huggingface_suite
						def 	snake_case__						(						self							:					Optional[Any]						):






							"""simple docstring"""






							AcceleratorState._reset_state(__lowercase						)
							debug_launcher(partial(__lowercase	,     num_processes=1						)						)
							debug_launcher(__lowercase						)
