


# 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     lowercase	(    a ):



		'''simple docstring'''




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


def     lowercase	(    a ):



		'''simple docstring'''




		SCREAMING_SNAKE_CASE_				:List[Any]				     =						create_tensor(a )
		SCREAMING_SNAKE_CASE_				:int				     =						gather(a )
		assert gathered_tensor.tolist() == list(range(1     , state.num_processes**2 + 1 ) )


def     lowercase	(    a ):



		'''simple docstring'''




		SCREAMING_SNAKE_CASE_				:Tuple				     =						[state.process_index]
		SCREAMING_SNAKE_CASE_				:Any				     =						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     lowercase	(    a ):



		'''simple docstring'''




		SCREAMING_SNAKE_CASE_				:Union[str, Any]				     =						create_tensor(a )
		SCREAMING_SNAKE_CASE_				:List[str]				     =						broadcast(a )
		assert broadcasted_tensor.shape == torch.Size([state.num_processes] )
		assert broadcasted_tensor.tolist() == list(range(1     , state.num_processes + 1 ) )


def     lowercase	(    a ):



		'''simple docstring'''




		if state.is_main_process:
				SCREAMING_SNAKE_CASE_				:str				     =						torch.arange(state.num_processes + 1 ).to(state.device )
		else:
				SCREAMING_SNAKE_CASE_				:str				     =						torch.arange(state.num_processes ).to(state.device )
		SCREAMING_SNAKE_CASE_				:Optional[Any]				     =						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     lowercase	(    a ):



		'''simple docstring'''




		if state.num_processes != 2:
				return
		SCREAMING_SNAKE_CASE_				:str				     =						create_tensor(a )
		SCREAMING_SNAKE_CASE_				:List[str]				     =						reduce(a     , "sum" )
		SCREAMING_SNAKE_CASE_				:Tuple				     =						torch.tensor([4.0, 6] ).to(state.device )
		assert torch.allclose(a     , a ), F"{reduced_tensor} != {truth_tensor}"


def     lowercase	(    a ):



		'''simple docstring'''




		if state.num_processes != 2:
				return
		SCREAMING_SNAKE_CASE_				:Dict				     =						create_tensor(a )
		SCREAMING_SNAKE_CASE_				:Dict				     =						reduce(a     , "mean" )
		SCREAMING_SNAKE_CASE_				:List[str]				     =						torch.tensor([2.0, 3] ).to(state.device )
		assert torch.allclose(a     , a ), F"{reduced_tensor} != {truth_tensor}"


def     lowercase	(    a ):



		'''simple docstring'''




		main()


def     lowercase	(    ):



		'''simple docstring'''




		SCREAMING_SNAKE_CASE_				:str				     =						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_flax_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
    is_vision_available,
)


SCREAMING_SNAKE_CASE__     					=			{
    "configuration_owlvit": [
        "OWLVIT_PRETRAINED_CONFIG_ARCHIVE_MAP",
        "OwlViTConfig",
        "OwlViTOnnxConfig",
        "OwlViTTextConfig",
        "OwlViTVisionConfig",
    ],
    "processing_owlvit": ["OwlViTProcessor"],
}


try:
		if not is_vision_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		SCREAMING_SNAKE_CASE__     					=			["OwlViTFeatureExtractor"]
		SCREAMING_SNAKE_CASE__     					=			["OwlViTImageProcessor"]

try:
		if not is_torch_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		SCREAMING_SNAKE_CASE__     					=			[
		    "OWLVIT_PRETRAINED_MODEL_ARCHIVE_LIST",
		    "OwlViTModel",
		    "OwlViTPreTrainedModel",
		    "OwlViTTextModel",
		    "OwlViTVisionModel",
		    "OwlViTForObjectDetection",
		]

if TYPE_CHECKING:
		from .configuration_owlvit import (
		    OWLVIT_PRETRAINED_CONFIG_ARCHIVE_MAP,
		    OwlViTConfig,
		    OwlViTOnnxConfig,
		    OwlViTTextConfig,
		    OwlViTVisionConfig,
		)
		from .processing_owlvit import OwlViTProcessor

		try:
				if not is_vision_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .feature_extraction_owlvit import OwlViTFeatureExtractor
				from .image_processing_owlvit import OwlViTImageProcessor

		try:
				if not is_torch_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_owlvit import (
				    OWLVIT_PRETRAINED_MODEL_ARCHIVE_LIST,
				    OwlViTForObjectDetection,
				    OwlViTModel,
				    OwlViTPreTrainedModel,
				    OwlViTTextModel,
				    OwlViTVisionModel,
				)

else:
		import sys

		SCREAMING_SNAKE_CASE__     					=			_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)






