



"""simple docstring"""






import os
import socket
from contextlib import contextmanager

import torch

from ..commands.config.default import write_basic_config  # noqa: F401
from ..state import PartialState
from .dataclasses import DistributedType
from .imports import is_deepspeed_available, is_tpu_available
from .transformer_engine import convert_model
from .versions import is_torch_version


if is_deepspeed_available():
     from deepspeed import DeepSpeedEngine

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




def 			snake_case		(		lowerCAmelCase_	)						->				Optional[Any]:
  if is_torch_version('''<'''  ,						'''2.0.0'''	) or not hasattr(lowerCAmelCase_  ,						'''_dynamo'''	):
    return False
  return isinstance(lowerCAmelCase_  ,						torch._dynamo.eval_frame.OptimizedModule	)




def 			snake_case		(		lowerCAmelCase_  ,						lowerCAmelCase_ = True	)						->				Dict:
  _snake_case   	=      (torch.nn.parallel.DistributedDataParallel, torch.nn.DataParallel)

  _snake_case   	=      is_compiled_module(lowerCAmelCase_	)
  if is_compiled:
    _snake_case   	=      model
    _snake_case   	=      model._orig_mod

  if is_deepspeed_available():
    options += (DeepSpeedEngine,)

  while isinstance(lowerCAmelCase_  ,						lowerCAmelCase_	):
    _snake_case   	=      model.module

  if not keep_fpaa_wrapper:
    _snake_case   	=      getattr(lowerCAmelCase_  ,						'''forward'''	)
    _snake_case   	=      model.__dict__.pop('''_original_forward'''  ,						lowerCAmelCase_	)
    if original_forward is not None:
      while hasattr(lowerCAmelCase_  ,						'''__wrapped__'''	):
        _snake_case   	=      forward.__wrapped__
        if forward == original_forward:
          break
      _snake_case   	=      forward
    if getattr(lowerCAmelCase_  ,						'''_converted_to_transformer_engine'''  ,						lowerCAmelCase_	):
      convert_model(lowerCAmelCase_  ,						to_transformer_engine=lowerCAmelCase_	)

  if is_compiled:
    _snake_case   	=      model
    _snake_case   	=      compiled_model

  return model




def 			snake_case		(		)						->				str:
  PartialState().wait_for_everyone()




def 			snake_case		(		lowerCAmelCase_  ,						lowerCAmelCase_	)						->				Dict:
  if PartialState().distributed_type == DistributedType.TPU:
    xm.save(lowerCAmelCase_  ,						lowerCAmelCase_	)
  elif PartialState().local_process_index == 0:
    torch.save(lowerCAmelCase_  ,						lowerCAmelCase_	)




@contextmanager
def 			snake_case		(		**lowerCAmelCase_	)						->				str:
  for key, value in kwargs.items():
    _snake_case   	=      str(lowerCAmelCase_	)

  yield

  for key in kwargs:
    if key.upper() in os.environ:
      del os.environ[key.upper()]




def 			snake_case		(		lowerCAmelCase_	)						->				Tuple:
  if not hasattr(lowerCAmelCase_  ,						'''__qualname__'''	) and not hasattr(lowerCAmelCase_  ,						'''__name__'''	):
    _snake_case   	=      getattr(lowerCAmelCase_  ,						'''__class__'''  ,						lowerCAmelCase_	)
  if hasattr(lowerCAmelCase_  ,						'''__qualname__'''	):
    return obj.__qualname__
  if hasattr(lowerCAmelCase_  ,						'''__name__'''	):
    return obj.__name__
  return str(lowerCAmelCase_	)




def 			snake_case		(		lowerCAmelCase_  ,						lowerCAmelCase_	)						->				Tuple:
  for key, value in source.items():
    if isinstance(lowerCAmelCase_  ,						lowerCAmelCase_	):
      _snake_case   	=      destination.setdefault(lowerCAmelCase_  ,						{}	)
      merge_dicts(lowerCAmelCase_  ,						lowerCAmelCase_	)
    else:
      _snake_case   	=      value

  return destination







def 			snake_case		(		lowerCAmelCase_ = None	)						->				bool:
  if port is None:
    _snake_case   	=      29500
  with socket.socket(socket.AF_INET  ,						socket.SOCK_STREAM	) as s:
    return s.connect_ex(('''localhost''', port)	) == 0










"""simple docstring"""






from math import sqrt




def 			snake_case		(		lowerCAmelCase_ = 1000000	)						->				int:
  _snake_case   	=      0
  _snake_case   	=      0
  _snake_case   	=      42

  while num_cuboids <= limit:
    max_cuboid_size += 1
    for sum_shortest_sides in range(2  ,						2 * max_cuboid_size + 1	):
      if sqrt(sum_shortest_sides**2 + max_cuboid_size**2	).is_integer():
        num_cuboids += (
            min(lowerCAmelCase_  ,						sum_shortest_sides // 2	)
            - max(1  ,						sum_shortest_sides - max_cuboid_size	)
            + 1
        )

  return max_cuboid_size


if __name__ == "__main__":
     print(F"{solution() = }")





