


'''simple docstring'''



import torch

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


def 				lowerCamelCase__				(	A       :      List[str]					):




      '''simple docstring'''






      return (torch.arange(state.num_processes					) + 1.0 + (state.num_processes * state.process_index)).to(state.device					)


def 				lowerCamelCase__				(	A       :      str					):




      '''simple docstring'''






      UpperCAmelCase					    =     create_tensor(A					)
      UpperCAmelCase					    =     gather(A					)
      assert gathered_tensor.tolist() == list(range(1				,       state.num_processes**2 + 1					)					)


def 				lowerCamelCase__				(	A       :      int					):




      '''simple docstring'''






      UpperCAmelCase					    =     [state.process_index]
      UpperCAmelCase					    =     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 				lowerCamelCase__				(	A       :      List[str]					):




      '''simple docstring'''






      UpperCAmelCase					    =     create_tensor(A					)
      UpperCAmelCase					    =     broadcast(A					)
      assert broadcasted_tensor.shape == torch.Size([state.num_processes]					)
      assert broadcasted_tensor.tolist() == list(range(1				,       state.num_processes + 1					)					)


def 				lowerCamelCase__				(	A       :      Any					):




      '''simple docstring'''






      if state.is_main_process:
            UpperCAmelCase					    =     torch.arange(state.num_processes + 1					).to(state.device					)
      else:
            UpperCAmelCase					    =     torch.arange(state.num_processes					).to(state.device					)
      UpperCAmelCase					    =     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 				lowerCamelCase__				(	A       :      Dict					):




      '''simple docstring'''






      if state.num_processes != 2:
            return
      UpperCAmelCase					    =     create_tensor(A					)
      UpperCAmelCase					    =     reduce(A				,       '''sum'''					)
      UpperCAmelCase					    =     torch.tensor([4.0, 6]					).to(state.device					)
      assert torch.allclose(A				,       A					), f"""{reduced_tensor} != {truth_tensor}"""


def 				lowerCamelCase__				(	A       :      Optional[int]					):




      '''simple docstring'''






      if state.num_processes != 2:
            return
      UpperCAmelCase					    =     create_tensor(A					)
      UpperCAmelCase					    =     reduce(A				,       '''mean'''					)
      UpperCAmelCase					    =     torch.tensor([2.0, 3]					).to(state.device					)
      assert torch.allclose(A				,       A					), f"""{reduced_tensor} != {truth_tensor}"""


def 				lowerCamelCase__				(	A       :      Optional[int]					):




      '''simple docstring'''






      main()


def 				lowerCamelCase__				(	):




      '''simple docstring'''






      UpperCAmelCase					    =     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'''



from .data_collator import (
    DataCollatorForLanguageModeling,
    DataCollatorForPermutationLanguageModeling,
    DataCollatorForSeqaSeq,
    DataCollatorForSOP,
    DataCollatorForTokenClassification,
    DataCollatorForWholeWordMask,
    DataCollatorWithPadding,
    DefaultDataCollator,
    default_data_collator,
)
from .metrics import glue_compute_metrics, xnli_compute_metrics
from .processors import (
    DataProcessor,
    InputExample,
    InputFeatures,
    SingleSentenceClassificationProcessor,
    SquadExample,
    SquadFeatures,
    SquadVaProcessor,
    SquadVaProcessor,
    glue_convert_examples_to_features,
    glue_output_modes,
    glue_processors,
    glue_tasks_num_labels,
    squad_convert_examples_to_features,
    xnli_output_modes,
    xnli_processors,
    xnli_tasks_num_labels,
)





