



'''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()




from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


snake_case_			  = {
    '''configuration_instructblip''': [
        '''INSTRUCTBLIP_PRETRAINED_CONFIG_ARCHIVE_MAP''',
        '''InstructBlipConfig''',
        '''InstructBlipQFormerConfig''',
        '''InstructBlipVisionConfig''',
    ],
    '''processing_instructblip''': ['''InstructBlipProcessor'''],
}

try:
       if not is_torch_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       snake_case_			  = [
           '''INSTRUCTBLIP_PRETRAINED_MODEL_ARCHIVE_LIST''',
           '''InstructBlipQFormerModel''',
           '''InstructBlipPreTrainedModel''',
           '''InstructBlipForConditionalGeneration''',
           '''InstructBlipVisionModel''',
       ]

if TYPE_CHECKING:
       from .configuration_instructblip import (
           INSTRUCTBLIP_PRETRAINED_CONFIG_ARCHIVE_MAP,
           InstructBlipConfig,
           InstructBlipQFormerConfig,
           InstructBlipVisionConfig,
       )
       from .processing_instructblip import InstructBlipProcessor

       try:
              if not is_torch_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .modeling_instructblip import (
                  INSTRUCTBLIP_PRETRAINED_MODEL_ARCHIVE_LIST,
                  InstructBlipForConditionalGeneration,
                  InstructBlipPreTrainedModel,
                  InstructBlipQFormerModel,
                  InstructBlipVisionModel,
              )

else:
       import sys

       snake_case_			  = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)

