






"""simple docstring"""



import math
def 		_SCREAMING_SNAKE_CASE					(       __snake_case     :    int       ):





  '''simple docstring'''





  if n <= 0:
    raise ValueError('Only positive integers have prime factors'       )
  lowercase				       =			[]
  while n % 2 == 0:
    pf.append(2       )
    lowercase				       =			int(n / 2       )
  for i in range(3						,   int(math.sqrt(__snake_case       )       ) + 1						,   2       ):
    while n % i == 0:
      pf.append(__snake_case       )
      lowercase				       =			int(n / i       )
  if n > 2:
    pf.append(__snake_case       )
  return pf
def 		_SCREAMING_SNAKE_CASE					(       __snake_case     :    int       ):





  '''simple docstring'''





  if n <= 0:
    raise ValueError('Only positive numbers are accepted'       )
  lowercase				       =			1
  lowercase				       =			1
  while n % 2 == 0:
    temp += 1
    lowercase				       =			int(n / 2       )
  div *= temp
  for i in range(3						,   int(math.sqrt(__snake_case       )       ) + 1						,   2       ):
    lowercase				       =			1
    while n % i == 0:
      temp += 1
      lowercase				       =			int(n / i       )
    div *= temp
  if n > 1:
    div *= 2
  return div
def 		_SCREAMING_SNAKE_CASE					(       __snake_case     :    int       ):





  '''simple docstring'''





  if n <= 0:
    raise ValueError('Only positive numbers are accepted'       )
  lowercase				       =			1
  lowercase				       =			1
  while n % 2 == 0:
    temp += 1
    lowercase				       =			int(n / 2       )
  if temp > 1:
    s *= (2**temp - 1) / (2 - 1)
  for i in range(3						,   int(math.sqrt(__snake_case       )       ) + 1						,   2       ):
    lowercase				       =			1
    while n % i == 0:
      temp += 1
      lowercase				       =			int(n / i       )
    if temp > 1:
      s *= (i**temp - 1) / (i - 1)
  return int(__snake_case       )
def 		_SCREAMING_SNAKE_CASE					(       __snake_case     :    int       ):





  '''simple docstring'''





  lowercase				       =			n
  for x in set(prime_factors(__snake_case       )       ):
    s *= (x - 1) / x
  return int(__snake_case       )


if __name__ == "__main__":
    import doctest

    doctest.testmod()










"""simple docstring"""
import torch.nn as nn

from .imports import is_fpa_available


if is_fpa_available():
      import transformer_engine.pytorch as te






def 			lowerCamelCase_    (		_lowerCamelCase     ,  _lowerCamelCase=True     ,  _lowerCamelCase=True     ,  _lowerCamelCase=True							):
 if not is_fpa_available():
  raise ImportError('Using `convert_model` requires transformer_engine to be installed.'							)
 for name, module in model.named_children():
  if isinstance(_lowerCamelCase     ,  nn.Linear							) and to_transformer_engine and _convert_linear:
   # Return early if the linear layer weights are not multiples of 16
   if any(p % 16 != 0 for p in module.weight.shape							):
    return
   lowerCamelCase__	:     List[Any]								=		module.bias is not None
   lowerCamelCase__	:     Tuple								=		te.Linear(
       module.in_features     ,  module.out_features     ,  bias=_lowerCamelCase     ,  params_dtype=module.weight.dtype							)
   lowerCamelCase__	:     Optional[Any]								=		module.weight.data.clone()
   if has_bias:
    lowerCamelCase__	:     List[str]								=		module.bias.data.clone()

   setattr(_lowerCamelCase     ,  _lowerCamelCase     ,  _lowerCamelCase							)
  elif isinstance(_lowerCamelCase     ,  nn.LayerNorm							) and to_transformer_engine and _convert_ln:
   lowerCamelCase__	:     Tuple								=		te.LayerNorm(module.normalized_shape[0]     ,  eps=module.eps     ,  params_dtype=module.weight.dtype							)
   lowerCamelCase__	:     Tuple								=		module.weight.data.clone()
   lowerCamelCase__	:     Optional[Any]								=		module.bias.data.clone()

   setattr(_lowerCamelCase     ,  _lowerCamelCase     ,  _lowerCamelCase							)
  elif isinstance(_lowerCamelCase     ,  te.Linear							) and not to_transformer_engine and _convert_linear:
   lowerCamelCase__	:     Optional[Any]								=		module.bias is not None
   lowerCamelCase__	:     Union[str, Any]								=		nn.Linear(
       module.in_features     ,  module.out_features     ,  bias=_lowerCamelCase     ,  params_dtype=module.weight.dtype							)
   lowerCamelCase__	:     str								=		module.weight.data.clone()
   if has_bias:
    lowerCamelCase__	:     Optional[int]								=		module.bias.data.clone()

   setattr(_lowerCamelCase     ,  _lowerCamelCase     ,  _lowerCamelCase							)
  elif isinstance(_lowerCamelCase     ,  te.LayerNorm							) and not to_transformer_engine and _convert_ln:
   lowerCamelCase__	:     str								=		nn.LayerNorm(module.normalized_shape[0]     ,  eps=module.eps     ,  params_dtype=module.weight.dtype							)
   lowerCamelCase__	:     Dict								=		module.weight.data.clone()
   lowerCamelCase__	:     Dict								=		module.bias.data.clone()

   setattr(_lowerCamelCase     ,  _lowerCamelCase     ,  _lowerCamelCase							)
  else:
   convert_model(
       _lowerCamelCase     ,  to_transformer_engine=_lowerCamelCase     ,  _convert_linear=_convert_linear     ,  _convert_ln=_convert_ln     ,  )






def 			lowerCamelCase_    (		_lowerCamelCase							):
 if not is_fpa_available():
  raise ImportError('Using `has_transformer_engine_layers` requires transformer_engine to be installed.'							)
 for m in model.modules():
  if isinstance(_lowerCamelCase     ,  (te.LayerNorm, te.Linear)							):
   return True
 return False

