




"""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					(					__lowerCAmelCase			:		Optional[Any]  ,	__lowerCAmelCase			:		str = False						)    ->       int:
 if device_specific:
  seed += AcceleratorState().process_index
 random.seed(__lowerCamelCase						)
 np.random.seed(__lowerCamelCase						)
 torch.manual_seed(__lowerCamelCase						)
 if is_xpu_available():
  torch.xpu.manual_seed_all(__lowerCamelCase						)
 elif is_npu_available():
  torch.npu.manual_seed_all(__lowerCamelCase						)
 else:
  torch.cuda.manual_seed_all(__lowerCamelCase						)
 # ^^ safe to call this function even if cuda is not available
 if is_tpu_available():
  xm.set_rng_state(__lowerCamelCase						)






def 				__lowerCAmelCase					(					__lowerCAmelCase			:		Optional[Any] = None  ,	__lowerCAmelCase			:		Any = None						)    ->       Optional[int]:
 # Get the proper rng state
 if rng_type == RNGType.TORCH:
  _UpperCamelCase      :	List[str]					      =  torch.get_rng_state()
 elif rng_type == RNGType.CUDA:
  _UpperCamelCase      :	Any					      =  torch.cuda.get_rng_state()
 elif rng_type == RNGType.XLA:
  assert is_tpu_available(), "Can't synchronize XLA seeds on an environment without TPUs."
  _UpperCamelCase      :	Union[str, Any]					      =  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."
  _UpperCamelCase      :	Optional[Any]					      =  torch.npu.get_rng_state()
 elif rng_type == RNGType.XPU:
  assert is_xpu_available(), "Can't synchronize XPU seeds on an environment without XPUs."
  _UpperCamelCase      :	Optional[Any]					      =  torch.xpu.get_rng_state()
 elif rng_type == RNGType.GENERATOR:
  assert generator is not None, "Need a generator to synchronize its seed."
  _UpperCamelCase      :	List[Any]					      =  generator.get_state()

 # Broadcast the rng state from device 0 to other devices
 _UpperCamelCase      :	Dict					      =  AcceleratorState()
 if state.distributed_type == DistributedType.TPU:
  _UpperCamelCase      :	int					      =  rng_state.to(xm.xla_device()						)
  xm.collective_broadcast([rng_state]						)
  xm.mark_step()
  _UpperCamelCase      :	List[Any]					      =  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
 ):
  _UpperCamelCase      :	Dict					      =  rng_state.to(state.device						)
  torch.distributed.broadcast(__lowerCamelCase  ,	0						)
  _UpperCamelCase      :	int					      =  rng_state.cpu()
 elif state.distributed_type == DistributedType.MULTI_CPU:
  torch.distributed.broadcast(__lowerCamelCase  ,	0						)

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






def 				__lowerCAmelCase					(					__lowerCAmelCase			:		List[Any]  ,	__lowerCAmelCase			:		List[str] = None						)    ->       Tuple:
 for rng_type in rng_types:
  synchronize_rng_state(RNGType(__lowerCamelCase						)  ,	generator=__lowerCamelCase						)





import torch

from transformers import PreTrainedModel

from .custom_configuration import CustomConfig, NoSuperInitConfig





class 				UpperCamelCase__		(	__SCREAMING_SNAKE_CASE	):






       """simple docstring"""


       UpperCAmelCase_				       =CustomConfig



       def __init__(							self ,     _A	)				->				int:
          super().__init__(_A	)
          SCREAMING_SNAKE_CASE_	=      torch.nn.Linear(config.hidden_size ,     config.hidden_size	)



       def     _UpperCamelCase	(							self ,     _A	)				->				Optional[int]:
          return self.linear(_A	)




       def     _UpperCamelCase	(							self ,     _A	)				->				int:
          pass







class 				UpperCamelCase__		(	__SCREAMING_SNAKE_CASE	):






       """simple docstring"""


       UpperCAmelCase_				       =NoSuperInitConfig



       def __init__(							self ,     _A	)				->				Optional[int]:
          super().__init__(_A	)
          SCREAMING_SNAKE_CASE_	=      torch.nn.Linear(config.attribute ,     config.attribute	)



       def     _UpperCamelCase	(							self ,     _A	)				->				Any:
          return self.linear(_A	)




       def     _UpperCamelCase	(							self ,     _A	)				->				Tuple:
          pass


