
'''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   A     (      UpperCamelCase_					:     int						,	UpperCamelCase_					:     bool = False      )					->       Optional[Any]:







			'''simple docstring'''



			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   A     (      UpperCamelCase_					:     Optional[RNGType] = None						,	UpperCamelCase_					:     Optional[torch.Generator] = None      )					->       Union[str, Any]:







			'''simple docstring'''



			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   A     (      UpperCamelCase_					:     List[Union[str, RNGType]]						,	UpperCamelCase_					:     Optional[torch.Generator] = None      )					->       Optional[int]:







			'''simple docstring'''



			for rng_type in rng_types:
						synchronize_rng_state(RNGType(UpperCamelCase_      )						,	generator=UpperCamelCase_      )







'''simple docstring'''

def   A     (      UpperCamelCase_					:     int						,	UpperCamelCase_					:     int						,	UpperCamelCase_					:     int						,	UpperCamelCase_					:     int      )					->       int:







			'''simple docstring'''



			return len_board * len_board_column * row + column







def   A     (      UpperCamelCase_					:     list[list[str]]						,	UpperCamelCase_					:     str						,	UpperCamelCase_					:     int						,	UpperCamelCase_					:     int						,	UpperCamelCase_					:     int						,	UpperCamelCase_					:     set[int]						,	)					->       bool:







			'''simple docstring'''



			if board[row][column] != word[word_index]:
						return False

			if word_index == len(UpperCamelCase_      ) - 1:
						return True

			lowerCAmelCase__								=							[(0, 1), (0, -1), (-1, 0), (1, 0)]
			lowerCAmelCase__								=							len(UpperCamelCase_      )
			lowerCAmelCase__								=							len(board[0]      )
			for direction in traverts_directions:
						lowerCAmelCase__								=							row + direction[0]
						lowerCAmelCase__								=							column + direction[1]
						if not (0 <= next_i < len_board and 0 <= next_j < len_board_column):
									continue

						lowerCAmelCase__								=							get_point_key(UpperCamelCase_						,	UpperCamelCase_						,	UpperCamelCase_						,	UpperCamelCase_      )
						if key in visited_points_set:
									continue

						visited_points_set.add(UpperCamelCase_      )
						if exits_word(UpperCamelCase_						,	UpperCamelCase_						,	UpperCamelCase_						,	UpperCamelCase_						,	word_index + 1						,	UpperCamelCase_      ):
									return True

						visited_points_set.remove(UpperCamelCase_      )

			return False







def   A     (      UpperCamelCase_					:     list[list[str]]						,	UpperCamelCase_					:     str      )					->       bool:







			'''simple docstring'''



			lowerCAmelCase__								=							(
			    "The board should be a non empty matrix of single chars strings."
			)

			lowerCAmelCase__								=							len(UpperCamelCase_      )
			if not isinstance(UpperCamelCase_						,	UpperCamelCase_      ) or len(UpperCamelCase_      ) == 0:
						raise ValueError(UpperCamelCase_      )

			for row in board:
						if not isinstance(UpperCamelCase_						,	UpperCamelCase_      ) or len(UpperCamelCase_      ) == 0:
									raise ValueError(UpperCamelCase_      )

						for item in row:
									if not isinstance(UpperCamelCase_						,	UpperCamelCase_      ) or len(UpperCamelCase_      ) != 1:
												raise ValueError(UpperCamelCase_      )

    # Validate word
			if not isinstance(UpperCamelCase_						,	UpperCamelCase_      ) or len(UpperCamelCase_      ) == 0:
						raise ValueError(
						    "The word parameter should be a string of length greater than 0."      )

			lowerCAmelCase__								=							len(board[0]      )
			for i in range(UpperCamelCase_      ):
						for j in range(UpperCamelCase_      ):
									if exits_word(
									    UpperCamelCase_						,	UpperCamelCase_						,	UpperCamelCase_						,	UpperCamelCase_						,	0						,	{get_point_key(UpperCamelCase_						,	UpperCamelCase_						,	UpperCamelCase_						,	UpperCamelCase_      )}      ):
												return True

			return False


if __name__ == "__main__":
		import doctest

		doctest.testmod()





