






"""simple docstring"""
# Copyright 2023 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import torch

from accelerate import PartialState
from accelerate.utils.operations import broadcast, gather, gather_object, pad_across_processes, reduce

def 				snake_case      (   A__						):
			return (torch.arange(state.num_processes						) + 1.0 + (state.num_processes * state.process_index)).to(state.device						)

def 				snake_case      (   A__						):
			UpperCAmelCase_     : List[Any]			     =					create_tensor(A__						)
			UpperCAmelCase_     : str			     =					gather(A__						)
			assert gathered_tensor.tolist() == list(range(1	,state.num_processes**2 + 1						)						)

def 				snake_case      (   A__						):
			UpperCAmelCase_     : List[Any]			     =					[state.process_index]
			UpperCAmelCase_     : List[Any]			     =					gather_object(A__						)
			assert len(A__						) == state.num_processes, F"""{gathered_obj}, {len(A__						)} != {state.num_processes}"""
			assert gathered_obj == list(range(state.num_processes						)						), F"""{gathered_obj} != {list(range(state.num_processes						)						)}"""

def 				snake_case      (   A__						):
			UpperCAmelCase_     : Optional[Any]			     =					create_tensor(A__						)
			UpperCAmelCase_     : Optional[Any]			     =					broadcast(A__						)
			assert broadcasted_tensor.shape == torch.Size([state.num_processes]						)
			assert broadcasted_tensor.tolist() == list(range(1	,state.num_processes + 1						)						)

def 				snake_case      (   A__						):
			# We need to pad the tensor with one more element if we are the main process
			# to ensure that we can pad
			if state.is_main_process:
						UpperCAmelCase_     : Any			     =					torch.arange(state.num_processes + 1						).to(state.device						)
			else:
						UpperCAmelCase_     : List[str]			     =					torch.arange(state.num_processes						).to(state.device						)
			UpperCAmelCase_     : Optional[Any]			     =					pad_across_processes(A__						)
			assert padded_tensor.shape == torch.Size([state.num_processes + 1]						)
			if not state.is_main_process:
						assert padded_tensor.tolist() == list(range(0	,state.num_processes						)						) + [0]

def 				snake_case      (   A__						):
			# For now runs on only two processes
			if state.num_processes != 2:
						return
			UpperCAmelCase_     : List[str]			     =					create_tensor(A__						)
			UpperCAmelCase_     : Tuple			     =					reduce(A__	,"sum"						)
			UpperCAmelCase_     : Optional[int]			     =					torch.tensor([4.0, 6]						).to(state.device						)
			assert torch.allclose(A__	,A__						), F"""{reduced_tensor} != {truth_tensor}"""

def 				snake_case      (   A__						):
			# For now runs on only two processes
			if state.num_processes != 2:
						return
			UpperCAmelCase_     : str			     =					create_tensor(A__						)
			UpperCAmelCase_     : Optional[int]			     =					reduce(A__	,"mean"						)
			UpperCAmelCase_     : List[str]			     =					torch.tensor([2.0, 3]						).to(state.device						)
			assert torch.allclose(A__	,A__						), F"""{reduced_tensor} != {truth_tensor}"""

def 				snake_case      (   A__						):
			# For xla_spawn (TPUs)
			main()

def 				snake_case      (   ):
			UpperCAmelCase_     : Dict			     =					PartialState()
			state.print(F"""State: {state}"""						)
			state.print("testing gather"						)
			test_gather(A__						)
			state.print("testing gather_object"						)
			test_gather_object(A__						)
			state.print("testing broadcast"						)
			test_broadcast(A__						)
			state.print("testing pad_across_processes"						)
			test_pad_across_processes(A__						)
			state.print("testing reduce_sum"						)
			test_reduce_sum(A__						)
			state.print("testing reduce_mean"						)
			test_reduce_mean(A__						)


if __name__ == "__main__":
			main()











"""simple docstring"""
lowerCamelCase_					=      [
    (1000, '''M'''),
    (900, '''CM'''),
    (500, '''D'''),
    (400, '''CD'''),
    (100, '''C'''),
    (90, '''XC'''),
    (50, '''L'''),
    (40, '''XL'''),
    (10, '''X'''),
    (9, '''IX'''),
    (5, '''V'''),
    (4, '''IV'''),
    (1, '''I'''),
]

def 				snake_case      (   A__						):
			UpperCAmelCase_     : List[str]			     =					{"I": 1, "V": 5, "X": 10, "L": 50, "C": 1_00, "D": 5_00, "M": 10_00}
			UpperCAmelCase_     : Optional[Any]			     =					0
			UpperCAmelCase_     : Tuple			     =					0
			while place < len(A__						):
						if (place + 1 < len(A__						)) and (vals[roman[place]] < vals[roman[place + 1]]):
									total += vals[roman[place + 1]] - vals[roman[place]]
									place += 2
						else:
									total += vals[roman[place]]
									place += 1
			return total

def 				snake_case      (   A__						):
			UpperCAmelCase_     : Union[str, Any]			     =					[]
			for arabic, roman in ROMAN:
						((UpperCAmelCase_)     ,	(UpperCAmelCase_))     : str			     =					divmod(A__	,A__						)
						result.append(roman * factor						)
						if number == 0:
									break
			return "".join(A__						)


if __name__ == "__main__":
			import doctest

			doctest.testmod()



