



'''simple docstring'''




from heapq import heappop, heappush

import numpy as np







def _lowerCAmelCase     (      _lowerCAmelCase       ,    _lowerCAmelCase       ,    _lowerCAmelCase       ,    _lowerCAmelCase       ,    )->     tuple[float | int, list[tuple[int, int]]]:
 __UpperCAmelCase					,    __UpperCAmelCase					=   grid.shape
 __UpperCAmelCase					=   [-1, 1, 0, 0]
 __UpperCAmelCase					=   [0, 0, -1, 1]
 if allow_diagonal:
  dx += [-1, -1, 1, 1]
  dy += [-1, 1, -1, 1]

 __UpperCAmelCase					,    __UpperCAmelCase					=   [(0, source)], set()
 __UpperCAmelCase					=   np.full((rows, cols)       ,    np.inf    )
 __UpperCAmelCase					=   0
 __UpperCAmelCase					=   np.empty((rows, cols)       ,    dtype=_lowerCAmelCase    )
 __UpperCAmelCase					=   None

 while queue:
  ((__UpperCAmelCase)					,    (__UpperCAmelCase))					=   heappop(_lowerCAmelCase    )
  if (x, y) in visited:
   continue
  visited.add((x, y)    )

  if (x, y) == destination:
   __UpperCAmelCase					=   []
   while (x, y) != source:
    path.append((x, y)    )
    __UpperCAmelCase					,    __UpperCAmelCase					=   predecessors[x, y]
   path.append(_lowerCAmelCase    )  # add the source manually
   path.reverse()
   return matrix[destination], path

  for i in range(len(_lowerCAmelCase    )    ):
   __UpperCAmelCase					,    __UpperCAmelCase					=   x + dx[i], y + dy[i]
   if 0 <= nx < rows and 0 <= ny < cols:
    __UpperCAmelCase					=   grid[nx][ny]
    if next_node == 1 and matrix[nx, ny] > dist + 1:
     heappush(_lowerCAmelCase       ,    (dist + 1, (nx, ny))    )
     __UpperCAmelCase					=   dist + 1
     __UpperCAmelCase					=   (x, y)

 return np.inf, []


if __name__ == "__main__":
    import doctest

    doctest.testmod()












'''simple docstring'''




# Copyright 2021 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 pathlib import Path

import torch

from ...utils import is_npu_available, is_xpu_available
from .config_args import ClusterConfig, default_json_config_file
from .config_utils import SubcommandHelpFormatter


_A:					str							=				"""Create a default config file for Accelerate with only a few flags set."""







def _lowerCAmelCase     (      _lowerCAmelCase="no"       ,    _lowerCAmelCase = default_json_config_file       ,    _lowerCAmelCase = False    )->     List[Any]:
 __UpperCAmelCase					=   Path(_lowerCAmelCase    )
 path.parent.mkdir(parents=_lowerCAmelCase       ,    exist_ok=_lowerCAmelCase    )
 if path.exists():
  print(
      F'Configuration already exists at {save_location}, will not override. Run `accelerate config` manually or pass a different `save_location`.'    )
  return False
 __UpperCAmelCase					=   mixed_precision.lower()
 if mixed_precision not in ["no", "fp16", "bf16", "fp8"]:
  raise ValueError(
      F'`mixed_precision` should be one of \'no\', \'fp16\', \'bf16\', or \'fp8\'. Received {mixed_precision}'    )
 __UpperCAmelCase					=   {
     'compute_environment': 'LOCAL_MACHINE',
     'mixed_precision': mixed_precision,
 }
 if torch.cuda.is_available():
  __UpperCAmelCase					=   torch.cuda.device_count()
  __UpperCAmelCase					=   num_gpus
  __UpperCAmelCase					=   False
  if num_gpus > 1:
   __UpperCAmelCase					=   'MULTI_GPU'
  else:
   __UpperCAmelCase					=   'NO'
 elif is_xpu_available() and use_xpu:
  __UpperCAmelCase					=   torch.xpu.device_count()
  __UpperCAmelCase					=   num_xpus
  __UpperCAmelCase					=   False
  if num_xpus > 1:
   __UpperCAmelCase					=   'MULTI_XPU'
  else:
   __UpperCAmelCase					=   'NO'
 elif is_npu_available():
  __UpperCAmelCase					=   torch.npu.device_count()
  __UpperCAmelCase					=   num_npus
  __UpperCAmelCase					=   False
  if num_npus > 1:
   __UpperCAmelCase					=   'MULTI_NPU'
  else:
   __UpperCAmelCase					=   'NO'
 else:
  __UpperCAmelCase					=   0
  __UpperCAmelCase					=   True
  __UpperCAmelCase					=   1
  __UpperCAmelCase					=   'NO'
 __UpperCAmelCase					=   ClusterConfig(**_lowerCAmelCase    )
 config.to_json_file(_lowerCAmelCase    )
 return path







def _lowerCAmelCase     (      _lowerCAmelCase       ,    _lowerCAmelCase    )->     List[str]:
 __UpperCAmelCase					=   parser.add_parser('default'       ,    parents=_lowerCAmelCase       ,    help=_lowerCAmelCase       ,    formatter_class=_lowerCAmelCase    )
 parser.add_argument(
     '--config_file'       ,    default=_lowerCAmelCase       ,    help=(
         'The path to use to store the config file. Will default to a file named default_config.yaml in the cache '
         'location, which is the content of the environment `HF_HOME` suffixed with \'accelerate\', or if you don\'t have '
         'such an environment variable, your cache directory (\'~/.cache\' or the content of `XDG_CACHE_HOME`) suffixed '
         'with \'huggingface\'.'
     )       ,    dest='save_location'       ,    )

 parser.add_argument(
     '--mixed_precision'       ,    choices=['no', 'fp16', 'bf16']       ,    type=_lowerCAmelCase       ,    help='Whether or not to use mixed precision training. '
     'Choose between FP16 and BF16 (bfloat16) training. '
     'BF16 training is only supported on Nvidia Ampere GPUs and PyTorch 1.10 or later.'       ,    default='no'       ,    )
 parser.set_defaults(func=_lowerCAmelCase    )
 return parser







def _lowerCAmelCase     (      _lowerCAmelCase    )->     Union[str, Any]:
 __UpperCAmelCase					=   write_basic_config(args.mixed_precision       ,    args.save_location    )
 if config_file:
  print(F'accelerate configuration saved at {config_file}'    )





