
"""simple docstring"""


import random
from typing import List, Optional, Union

import numpy as np
import torch

from ..state import AcceleratorState
from .constants import CUDA_DISTRIBUTED_TYPES
from .dataclasses import DistributedType, RNGType
from .imports import is_npu_available, is_tpu_available, is_xpu_available


if is_tpu_available(check_device=False):
			import torch_xla.core.xla_model as xm



def 					_lowerCamelCase(__UpperCamelCase					,				__UpperCamelCase = False )       ->				Union[str, Any]:
	if device_specific:
		seed += AcceleratorState().process_index
	random.seed(__UpperCamelCase )
	np.random.seed(__UpperCamelCase )
	torch.manual_seed(__UpperCamelCase )
	if is_xpu_available():
		torch.xpu.manual_seed_all(__UpperCamelCase )
	elif is_npu_available():
		torch.npu.manual_seed_all(__UpperCamelCase )
	else:
		torch.cuda.manual_seed_all(__UpperCamelCase )
	# ^^ safe to call this function even if cuda is not available
	if is_tpu_available():
		xm.set_rng_state(__UpperCamelCase )



def 					_lowerCamelCase(__UpperCamelCase = None					,				__UpperCamelCase = None )       ->				Optional[Any]:
	# Get the proper rng state
	if rng_type == RNGType.TORCH:
		_lowerCAmelCase			=torch.get_rng_state()
	elif rng_type == RNGType.CUDA:
		_lowerCAmelCase			=torch.cuda.get_rng_state()
	elif rng_type == RNGType.XLA:
		assert is_tpu_available(), "Can't synchronize XLA seeds on an environment without TPUs."
		_lowerCAmelCase			=torch.tensor(xm.get_rng_state() )
	elif rng_type == RNGType.NPU:
		assert is_npu_available(), "Can't synchronize NPU seeds on an environment without NPUs."
		_lowerCAmelCase			=torch.npu.get_rng_state()
	elif rng_type == RNGType.XPU:
		assert is_xpu_available(), "Can't synchronize XPU seeds on an environment without XPUs."
		_lowerCAmelCase			=torch.xpu.get_rng_state()
	elif rng_type == RNGType.GENERATOR:
		assert generator is not None, "Need a generator to synchronize its seed."
		_lowerCAmelCase			=generator.get_state()

	# Broadcast the rng state from device 0 to other devices
	_lowerCAmelCase			=AcceleratorState()
	if state.distributed_type == DistributedType.TPU:
		_lowerCAmelCase			=rng_state.to(xm.xla_device() )
		xm.collective_broadcast([rng_state] )
		xm.mark_step()
		_lowerCAmelCase			=rng_state.cpu()
	elif (
	    state.distributed_type in CUDA_DISTRIBUTED_TYPES
	    or state.distributed_type == DistributedType.MULTI_NPU
	    or state.distributed_type == DistributedType.MULTI_XPU
	):
		_lowerCAmelCase			=rng_state.to(state.device )
		torch.distributed.broadcast(__UpperCamelCase					,				0 )
		_lowerCAmelCase			=rng_state.cpu()
	elif state.distributed_type == DistributedType.MULTI_CPU:
		torch.distributed.broadcast(__UpperCamelCase					,				0 )

	# Set the broadcast rng state
	if rng_type == RNGType.TORCH:
		torch.set_rng_state(__UpperCamelCase )
	elif rng_type == RNGType.CUDA:
		torch.cuda.set_rng_state(__UpperCamelCase )
	elif rng_type == RNGType.NPU:
		torch.npu.set_rng_state(__UpperCamelCase )
	elif rng_type == RNGType.XPU:
		torch.xpu.set_rng_state(__UpperCamelCase )
	elif rng_type == RNGType.XLA:
		xm.set_rng_state(rng_state.item() )
	elif rng_type == RNGType.GENERATOR:
		generator.set_state(__UpperCamelCase )



def 					_lowerCamelCase(__UpperCamelCase					,				__UpperCamelCase = None )       ->				int:
	for rng_type in rng_types:
		synchronize_rng_state(RNGType(__UpperCamelCase )					,				generator=__UpperCamelCase )





"""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.
from typing import TYPE_CHECKING

# rely on isort to merge the imports
from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_torch_available,
)


__A 		= {
    'configuration_convnextv2': [
        'CONVNEXTV2_PRETRAINED_CONFIG_ARCHIVE_MAP',
        'ConvNextV2Config',
    ]
}

try:
			if not is_torch_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			pass
else:
			__A 		= [
			    'CONVNEXTV2_PRETRAINED_MODEL_ARCHIVE_LIST',
			    'ConvNextV2ForImageClassification',
			    'ConvNextV2Model',
			    'ConvNextV2PreTrainedModel',
			    'ConvNextV2Backbone',
			]


if TYPE_CHECKING:
			from .configuration_convnextva import (
			    CONVNEXTV2_PRETRAINED_CONFIG_ARCHIVE_MAP,
			    ConvNextVaConfig,
			)

			try:
						if not is_torch_available():
									raise OptionalDependencyNotAvailable()
			except OptionalDependencyNotAvailable:
						pass
			else:
						from .modeling_convnextva import (
						    CONVNEXTV2_PRETRAINED_MODEL_ARCHIVE_LIST,
						    ConvNextVaBackbone,
						    ConvNextVaForImageClassification,
						    ConvNextVaModel,
						    ConvNextVaPreTrainedModel,
						)

else:
			import sys

			__A 		= _LazyModule(__name__, globals()['__file__'], _import_structure)



