






# 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      a_ (      _A  )			->			Optional[Any]:







				"""simple docstring"""
				return (torch.arange(state.num_processes  ) + 1.0 + (state.num_processes * state.process_index)).to(state.device  )
def      a_ (      _A  )			->			List[str]:







				"""simple docstring"""
				snake_case__									=     create_tensor(_lowerCamelCase  )
				snake_case__									=     gather(_lowerCamelCase  )
				assert gathered_tensor.tolist() == list(range(1      ,   state.num_processes**2 + 1  )  )
def      a_ (      _A  )			->			Optional[Any]:







				"""simple docstring"""
				snake_case__									=     [state.process_index]
				snake_case__									=     gather_object(_lowerCamelCase  )
				assert len(_lowerCamelCase  ) == state.num_processes, f'''{gathered_obj}, {len(_lowerCamelCase  )} != {state.num_processes}'''
				assert gathered_obj == list(range(state.num_processes  )  ), f'''{gathered_obj} != {list(range(state.num_processes  )  )}'''
def      a_ (      _A  )			->			List[str]:







				"""simple docstring"""
				snake_case__									=     create_tensor(_lowerCamelCase  )
				snake_case__									=     broadcast(_lowerCamelCase  )
				assert broadcasted_tensor.shape == torch.Size([state.num_processes]  )
				assert broadcasted_tensor.tolist() == list(range(1      ,   state.num_processes + 1  )  )
def      a_ (      _A  )			->			Any:







				"""simple docstring"""
				# 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:
								snake_case__									=     torch.arange(state.num_processes + 1  ).to(state.device  )
				else:
								snake_case__									=     torch.arange(state.num_processes  ).to(state.device  )
				snake_case__									=     pad_across_processes(_lowerCamelCase  )
				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      a_ (      _A  )			->			int:







				"""simple docstring"""
				# For now runs on only two processes
				if state.num_processes != 2:
								return
				snake_case__									=     create_tensor(_lowerCamelCase  )
				snake_case__									=     reduce(_lowerCamelCase      ,   'sum'  )
				snake_case__									=     torch.tensor([4.0, 6]  ).to(state.device  )
				assert torch.allclose(_lowerCamelCase      ,   _lowerCamelCase  ), f'''{reduced_tensor} != {truth_tensor}'''
def      a_ (      _A  )			->			Dict:







				"""simple docstring"""
				# For now runs on only two processes
				if state.num_processes != 2:
								return
				snake_case__									=     create_tensor(_lowerCamelCase  )
				snake_case__									=     reduce(_lowerCamelCase      ,   'mean'  )
				snake_case__									=     torch.tensor([2.0, 3]  ).to(state.device  )
				assert torch.allclose(_lowerCamelCase      ,   _lowerCamelCase  ), f'''{reduced_tensor} != {truth_tensor}'''
def      a_ (      _A  )			->			str:







				"""simple docstring"""
				# For xla_spawn (TPUs)
				main()
def      a_ (      )			->			Dict:







				"""simple docstring"""
				snake_case__									=     PartialState()
				state.print(f'''State: {state}'''  )
				state.print('testing gather'  )
				test_gather(_lowerCamelCase  )
				state.print('testing gather_object'  )
				test_gather_object(_lowerCamelCase  )
				state.print('testing broadcast'  )
				test_broadcast(_lowerCamelCase  )
				state.print('testing pad_across_processes'  )
				test_pad_across_processes(_lowerCamelCase  )
				state.print('testing reduce_sum'  )
				test_reduce_sum(_lowerCamelCase  )
				state.print('testing reduce_mean'  )
				test_reduce_mean(_lowerCamelCase  )


if __name__ == "__main__":
							main()












from collections.abc import Sequence
def      a_ (      _A      ,   _A  )			->			float:







				"""simple docstring"""
				return sum(c * (x**i) for i, c in enumerate(_A  )  )
def      a_ (      _A      ,   _A  )			->			float:







				"""simple docstring"""
				snake_case__									=     0.0
				for coeff in reversed(_A  ):
								snake_case__									=     result * x + coeff
				return result


if __name__ == "__main__":
							__UpperCamelCase					:		str	  =						(0.0, 0.0, 5.0, 9.3, 7.0)
							__UpperCamelCase					:		Dict	  =						1_0.0
							print(evaluate_poly(poly, x))
							print(horner(poly, x))




