





# 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						(   UpperCAmelCase					)    ->    str:







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



def 	_a						(   UpperCAmelCase					)    ->    str:







   """simple docstring"""
   lowerCamelCase__       :						Any        =							create_tensor(UpperCAmelCase					)
   lowerCamelCase__       :						List[str]        =							gather(UpperCAmelCase					)
   assert gathered_tensor.tolist() == list(range(1      ,  state.num_processes**2 + 1					)					)



def 	_a						(   UpperCAmelCase					)    ->    Optional[Any]:







   """simple docstring"""
   lowerCamelCase__       :						Dict        =							[state.process_index]
   lowerCamelCase__       :						Union[str, Any]        =							gather_object(UpperCAmelCase					)
   assert len(UpperCAmelCase					) == state.num_processes, f"{gathered_obj}, {len(UpperCAmelCase					)} != {state.num_processes}"
   assert gathered_obj == list(range(state.num_processes					)					), f"{gathered_obj} != {list(range(state.num_processes					)					)}"



def 	_a						(   UpperCAmelCase					)    ->    int:







   """simple docstring"""
   lowerCamelCase__       :						Optional[Any]        =							create_tensor(UpperCAmelCase					)
   lowerCamelCase__       :						int        =							broadcast(UpperCAmelCase					)
   assert broadcasted_tensor.shape == torch.Size([state.num_processes]					)
   assert broadcasted_tensor.tolist() == list(range(1      ,  state.num_processes + 1					)					)



def 	_a						(   UpperCAmelCase					)    ->    Optional[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:
      lowerCamelCase__       :						Union[str, Any]        =							torch.arange(state.num_processes + 1					).to(state.device					)
   else:
      lowerCamelCase__       :						List[Any]        =							torch.arange(state.num_processes					).to(state.device					)
   lowerCamelCase__       :						str        =							pad_across_processes(UpperCAmelCase					)
   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						(   UpperCAmelCase					)    ->    Optional[Any]:







   """simple docstring"""
   # For now runs on only two processes
   if state.num_processes != 2:
      return
   lowerCamelCase__       :						Dict        =							create_tensor(UpperCAmelCase					)
   lowerCamelCase__       :						Any        =							reduce(UpperCAmelCase      ,  '''sum'''					)
   lowerCamelCase__       :						List[str]        =							torch.tensor([4.0, 6]					).to(state.device					)
   assert torch.allclose(UpperCAmelCase      ,  UpperCAmelCase					), f"{reduced_tensor} != {truth_tensor}"



def 	_a						(   UpperCAmelCase					)    ->    Optional[Any]:







   """simple docstring"""
   # For now runs on only two processes
   if state.num_processes != 2:
      return
   lowerCamelCase__       :						Any        =							create_tensor(UpperCAmelCase					)
   lowerCamelCase__       :						int        =							reduce(UpperCAmelCase      ,  '''mean'''					)
   lowerCamelCase__       :						Optional[Any]        =							torch.tensor([2.0, 3]					).to(state.device					)
   assert torch.allclose(UpperCAmelCase      ,  UpperCAmelCase					), f"{reduced_tensor} != {truth_tensor}"



def 	_a						(   UpperCAmelCase					)    ->    List[str]:







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



def 	_a						(   )    ->    int:







   """simple docstring"""
   lowerCamelCase__       :						List[str]        =							PartialState()
   state.print(f"State: {state}"					)
   state.print('''testing gather'''					)
   test_gather(UpperCAmelCase					)
   state.print('''testing gather_object'''					)
   test_gather_object(UpperCAmelCase					)
   state.print('''testing broadcast'''					)
   test_broadcast(UpperCAmelCase					)
   state.print('''testing pad_across_processes'''					)
   test_pad_across_processes(UpperCAmelCase					)
   state.print('''testing reduce_sum'''					)
   test_reduce_sum(UpperCAmelCase					)
   state.print('''testing reduce_mean'''					)
   test_reduce_mean(UpperCAmelCase					)


if __name__ == "__main__":
       main()










from ...configuration_utils import PretrainedConfig
from ...utils import logging


_A :   Any  			=  logging.get_logger(__name__)

_A :   int  			=  {
    'facebook/timesformer': 'https://huggingface.co/facebook/timesformer/resolve/main/config.json',
}


class        __SCREAMING_SNAKE_CASE      (			lowerCAmelCase_ ):
       _UpperCAmelCase				:  Dict			   =   "timesformer"







       def __init__(				self					:     List[str]		, A					:     int=2_2_4		, A					:     Optional[Any]=1_6		, A					:     str=3		, A					:     str=8		, A					:     Any=7_6_8		, A					:     Dict=1_2		, A					:     Optional[Any]=1_2		, A					:     Any=3_0_7_2		, A					:     str="gelu"		, A					:     Optional[int]=0.0		, A					:     Union[str, Any]=0.0		, A					:     List[Any]=0.02		, A					:     int=1e-6		, A					:     Tuple=True		, A					:     Any="divided_space_time"		, A					:     Optional[Any]=0		, **A					:     Tuple		, )       ->str:
          super().__init__(**A      )

          lowerCamelCase__       :						Optional[Any]        =							image_size
          lowerCamelCase__       :						int        =							patch_size
          lowerCamelCase__       :						Any        =							num_channels
          lowerCamelCase__       :						Optional[int]        =							num_frames

          lowerCamelCase__       :						Optional[Any]        =							hidden_size
          lowerCamelCase__       :						Optional[int]        =							num_hidden_layers
          lowerCamelCase__       :						Optional[int]        =							num_attention_heads
          lowerCamelCase__       :						Dict        =							intermediate_size
          lowerCamelCase__       :						Optional[Any]        =							hidden_act
          lowerCamelCase__       :						Union[str, Any]        =							hidden_dropout_prob
          lowerCamelCase__       :						Dict        =							attention_probs_dropout_prob
          lowerCamelCase__       :						List[Any]        =							initializer_range
          lowerCamelCase__       :						str        =							layer_norm_eps
          lowerCamelCase__       :						Union[str, Any]        =							qkv_bias

          lowerCamelCase__       :						str        =							attention_type
          lowerCamelCase__       :						List[str]        =							drop_path_rate



