



'''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 				__lowerCamelCase				(   A__    )       ->	Optional[int]:






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







def 				__lowerCamelCase				(   A__    )       ->	int:






   """simple docstring"""
   UpperCamelCase					=  create_tensor(A__    )
   UpperCamelCase					=  gather(A__    )
   assert gathered_tensor.tolist() == list(range(1    ,						state.num_processes**2 + 1    )    )







def 				__lowerCamelCase				(   A__    )       ->	Tuple:






   """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__    )       ->	Dict:






   """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__    )       ->	str:






   """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:
      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__    )       ->	str:






   """simple docstring"""
   # For now runs on only two processes
   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__    )       ->	Dict:






   """simple docstring"""
   # For now runs on only two processes
   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__    )       ->	Dict:






   """simple docstring"""
   # For xla_spawn (TPUs)
   main()







def 				__lowerCamelCase				(   )       ->	Optional[int]:






   """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 . import (
    albert,
    align,
    altclip,
    audio_spectrogram_transformer,
    auto,
    autoformer,
    bark,
    bart,
    barthez,
    bartpho,
    beit,
    bert,
    bert_generation,
    bert_japanese,
    bertweet,
    big_bird,
    bigbird_pegasus,
    biogpt,
    bit,
    blenderbot,
    blenderbot_small,
    blip,
    blip_a,
    bloom,
    bridgetower,
    byta,
    camembert,
    canine,
    chinese_clip,
    clap,
    clip,
    clipseg,
    codegen,
    conditional_detr,
    convbert,
    convnext,
    convnextva,
    cpm,
    cpmant,
    ctrl,
    cvt,
    dataavec,
    deberta,
    deberta_va,
    decision_transformer,
    deformable_detr,
    deit,
    deprecated,
    deta,
    detr,
    dialogpt,
    dinat,
    distilbert,
    dit,
    donut,
    dpr,
    dpt,
    efficientformer,
    efficientnet,
    electra,
    encodec,
    encoder_decoder,
    ernie,
    ernie_m,
    esm,
    falcon,
    flaubert,
    flava,
    fnet,
    focalnet,
    fsmt,
    funnel,
    git,
    glpn,
    gpta,
    gpt_bigcode,
    gpt_neo,
    gpt_neox,
    gpt_neox_japanese,
    gpt_swa,
    gptj,
    gptsan_japanese,
    graphormer,
    groupvit,
    herbert,
    hubert,
    ibert,
    imagegpt,
    informer,
    instructblip,
    jukebox,
    layoutlm,
    layoutlmva,
    layoutlmva,
    layoutxlm,
    led,
    levit,
    lilt,
    llama,
    longformer,
    longta,
    luke,
    lxmert,
    mam_aaa,
    marian,
    markuplm,
    maskaformer,
    maskformer,
    mbart,
    mbartaa,
    mega,
    megatron_bert,
    megatron_gpta,
    mgp_str,
    mluke,
    mobilebert,
    mobilenet_va,
    mobilenet_va,
    mobilevit,
    mobilevitva,
    mpnet,
    mra,
    mta,
    musicgen,
    mvp,
    nat,
    nezha,
    nllb,
    nllb_moe,
    nystromformer,
    oneformer,
    open_llama,
    openai,
    opt,
    owlvit,
    pegasus,
    pegasus_x,
    perceiver,
    phobert,
    pixastruct,
    plbart,
    poolformer,
    prophetnet,
    qdqbert,
    rag,
    realm,
    reformer,
    regnet,
    rembert,
    resnet,
    roberta,
    roberta_prelayernorm,
    roc_bert,
    roformer,
    rwkv,
    sam,
    segformer,
    sew,
    sew_d,
    speech_encoder_decoder,
    speech_to_text,
    speech_to_text_a,
    speechta,
    splinter,
    squeezebert,
    swiftformer,
    swin,
    swinasr,
    swinva,
    switch_transformers,
    ta,
    table_transformer,
    tapas,
    time_series_transformer,
    timesformer,
    timm_backbone,
    transfo_xl,
    trocr,
    tvlt,
    umta,
    unispeech,
    unispeech_sat,
    upernet,
    videomae,
    vilt,
    vision_encoder_decoder,
    vision_text_dual_encoder,
    visual_bert,
    vit,
    vit_hybrid,
    vit_mae,
    vit_msn,
    vivit,
    wavaveca,
    wavaveca_conformer,
    wavaveca_phoneme,
    wavaveca_with_lm,
    wavlm,
    whisper,
    x_clip,
    xglm,
    xlm,
    xlm_prophetnet,
    xlm_roberta,
    xlm_roberta_xl,
    xlnet,
    xmod,
    yolos,
    yoso,
)

