
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  A__ (       __lowerCamelCase,  __lowerCamelCase = False						):
   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  A__ (       __lowerCamelCase = None,  __lowerCamelCase = None						):
   # Get the proper rng state
   if rng_type == RNGType.TORCH:
      SCREAMING_SNAKE_CASE_	=      torch.get_rng_state()
   elif rng_type == RNGType.CUDA:
      SCREAMING_SNAKE_CASE_	=      torch.cuda.get_rng_state()
   elif rng_type == RNGType.XLA:
      assert is_tpu_available(), "Can't synchronize XLA seeds on an environment without TPUs."
      SCREAMING_SNAKE_CASE_	=      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."
      SCREAMING_SNAKE_CASE_	=      torch.npu.get_rng_state()
   elif rng_type == RNGType.XPU:
      assert is_xpu_available(), "Can't synchronize XPU seeds on an environment without XPUs."
      SCREAMING_SNAKE_CASE_	=      torch.xpu.get_rng_state()
   elif rng_type == RNGType.GENERATOR:
      assert generator is not None, "Need a generator to synchronize its seed."
      SCREAMING_SNAKE_CASE_	=      generator.get_state()

   # Broadcast the rng state from device 0 to other devices
   SCREAMING_SNAKE_CASE_	=      AcceleratorState()
   if state.distributed_type == DistributedType.TPU:
      SCREAMING_SNAKE_CASE_	=      rng_state.to(xm.xla_device()						)
      xm.collective_broadcast([rng_state]						)
      xm.mark_step()
      SCREAMING_SNAKE_CASE_	=      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
   ):
      SCREAMING_SNAKE_CASE_	=      rng_state.to(state.device						)
      torch.distributed.broadcast(__lowerCamelCase,  0						)
      SCREAMING_SNAKE_CASE_	=      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  A__ (       __lowerCamelCase,  __lowerCamelCase = None						):
   for rng_type in rng_types:
      synchronize_rng_state(RNGType(__lowerCamelCase						),  generator=__lowerCamelCase						)




from math import sqrt


def  A__ (       __lowerCamelCase						):
   SCREAMING_SNAKE_CASE_	=      [1]
   for i in range(2,  int(sqrt(__lowerCamelCase						)						) + 1,  1						):
      if number % i == 0:
         values.append(__lowerCamelCase						)
         if int(number // i						) != i:
            values.append(int(number // i						)						)
   return sorted(__lowerCamelCase						)


def  A__ (       __lowerCamelCase						):
   return sum(factors(__lowerCamelCase						)						) > n


def  A__ (       __lowerCamelCase						):
   SCREAMING_SNAKE_CASE_	=      factors(__lowerCamelCase						)
   SCREAMING_SNAKE_CASE_	=      len(__lowerCamelCase						)
   SCREAMING_SNAKE_CASE_	=      [[0 for i in range(number + 1						)] for j in range(r + 1						)]
   for i in range(r + 1						):
      SCREAMING_SNAKE_CASE_	=      True

   for i in range(1,  number + 1						):
      SCREAMING_SNAKE_CASE_	=      False

   for i in range(1,  r + 1						):
      for j in range(1,  number + 1						):
         if j < values[i - 1]:
            SCREAMING_SNAKE_CASE_	=      subset[i - 1][j]
         else:
            SCREAMING_SNAKE_CASE_	=      subset[i - 1][j] or subset[i - 1][j - values[i - 1]]

   return subset[r][number] != 0


def  A__ (       __lowerCamelCase						):
   return abundant(__lowerCamelCase						) and not semi_perfect(__lowerCamelCase						)


if __name__ == "__main__":
   import doctest

   doctest.testmod(verbose=True)
   for number in (69, 70, 71):
      print(F"""{number} is {'' if weird(number) else 'not '}weird.""")


