

# 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        UpperCAmelCase_     (		_A       ):





     '''simple docstring'''




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





     '''simple docstring'''




     SCREAMING_SNAKE_CASE__					=					create_tensor(_A       )
     SCREAMING_SNAKE_CASE__					=					gather(_A       )
     assert gathered_tensor.tolist() == list(range(1   ,	state.num_processes**2 + 1       )       )
def        UpperCAmelCase_     (		_A       ):





     '''simple docstring'''




     SCREAMING_SNAKE_CASE__					=					[state.process_index]
     SCREAMING_SNAKE_CASE__					=					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        UpperCAmelCase_     (		_A       ):





     '''simple docstring'''




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





     '''simple docstring'''




     if state.is_main_process:
          SCREAMING_SNAKE_CASE__					=					torch.arange(state.num_processes + 1       ).to(state.device       )
     else:
          SCREAMING_SNAKE_CASE__					=					torch.arange(state.num_processes       ).to(state.device       )
     SCREAMING_SNAKE_CASE__					=					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        UpperCAmelCase_     (		_A       ):





     '''simple docstring'''




     if state.num_processes != 2:
          return
     SCREAMING_SNAKE_CASE__					=					create_tensor(_A       )
     SCREAMING_SNAKE_CASE__					=					reduce(_A   ,	'''sum'''       )
     SCREAMING_SNAKE_CASE__					=					torch.tensor([4.0, 6]       ).to(state.device       )
     assert torch.allclose(_A   ,	_A       ), F'''{reduced_tensor} != {truth_tensor}'''
def        UpperCAmelCase_     (		_A       ):





     '''simple docstring'''




     if state.num_processes != 2:
          return
     SCREAMING_SNAKE_CASE__					=					create_tensor(_A       )
     SCREAMING_SNAKE_CASE__					=					reduce(_A   ,	'''mean'''       )
     SCREAMING_SNAKE_CASE__					=					torch.tensor([2.0, 3]       ).to(state.device       )
     assert torch.allclose(_A   ,	_A       ), F'''{reduced_tensor} != {truth_tensor}'''
def        UpperCAmelCase_     (		_A       ):





     '''simple docstring'''




     main()
def        UpperCAmelCase_     (		):





     '''simple docstring'''




     SCREAMING_SNAKE_CASE__					=					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()





import doctest
from collections import deque

import numpy as np






class  UpperCAmelCase__	:






  """simple docstring"""


  def __init__(							self		:	Optional[Any]      )	->							None:
       SCREAMING_SNAKE_CASE__					=					[2, 1, 2, -1]
       SCREAMING_SNAKE_CASE__					=					[1, 2, 3, 4]







  def    lowercase_	(							self		:	List[str]      )	->							list[float]:
       SCREAMING_SNAKE_CASE__					=					len(self.first_signal      )
       SCREAMING_SNAKE_CASE__					=					len(self.second_signal      )

       SCREAMING_SNAKE_CASE__					=					max(__lowerCamelCase			,     __lowerCamelCase      )

       # create a zero matrix of max_length x max_length
       SCREAMING_SNAKE_CASE__					=					[[0] * max_length for i in range(__lowerCamelCase      )]

       # fills the smaller signal with zeros to make both signals of same length
       if length_first_signal < length_second_signal:
            self.first_signal += [0] * (max_length - length_first_signal)
       elif length_first_signal > length_second_signal:
            self.second_signal += [0] * (max_length - length_second_signal)
       for i in range(__lowerCamelCase      ):
            SCREAMING_SNAKE_CASE__					=					deque(self.second_signal      )
            rotated_signal.rotate(__lowerCamelCase      )
            for j, item in enumerate(__lowerCamelCase      ):
                 matrix[i][j] += item

        # multiply the matrix with the first signal
       SCREAMING_SNAKE_CASE__					=					np.matmul(np.transpose(__lowerCamelCase      )			,     np.transpose(self.first_signal      )      )

       # rounding-off to two decimal places
       return [round(__lowerCamelCase			,     2      ) for i in final_signal]


if __name__ == "__main__":
       doctest.testmod()


