






"""simple docstring"""





import torch.nn as nn

from .imports import is_fpa_available


if is_fpa_available():
  import transformer_engine.pytorch as te






def 							A(    snake_case_		,  snake_case_=True		,  snake_case_=True		,  snake_case_=True						):
       """simple docstring"""





       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
                     lowercase__:							List[str]         =     module.bias is not None
                     lowercase__:							int         =     te.Linear(
                         module.in_features		,  module.out_features		,  bias=__lowerCamelCase		,  params_dtype=module.weight.dtype						)
                     lowercase__:							Dict         =     module.weight.data.clone()
                     if has_bias:
                            lowercase__:							Dict         =     module.bias.data.clone()

                     setattr(__lowerCamelCase		,  __lowerCamelCase		,  __lowerCamelCase						)
              elif isinstance(__lowerCamelCase		,  nn.LayerNorm						) and to_transformer_engine and _convert_ln:
                     lowercase__:							Any         =     te.LayerNorm(module.normalized_shape[0]		,  eps=module.eps		,  params_dtype=module.weight.dtype						)
                     lowercase__:							int         =     module.weight.data.clone()
                     lowercase__:							List[str]         =     module.bias.data.clone()

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

                     setattr(__lowerCamelCase		,  __lowerCamelCase		,  __lowerCamelCase						)
              elif isinstance(__lowerCamelCase		,  te.LayerNorm						) and not to_transformer_engine and _convert_ln:
                     lowercase__:							Tuple         =     nn.LayerNorm(module.normalized_shape[0]		,  eps=module.eps		,  params_dtype=module.weight.dtype						)
                     lowercase__:							Optional[Any]         =     module.weight.data.clone()
                     lowercase__:							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 							A(    snake_case_						):
       """simple docstring"""





       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








"""simple docstring"""





import math
from timeit import timeit







def       __a	(						__lowerCamelCase    ):
   UpperCAmelCase_   :   Tuple     	=       0
   UpperCAmelCase_   :   Any     	=       abs(__lowerCamelCase    )
   while True:
      UpperCAmelCase_   :   Optional[int]     	=       n // 10
      digits += 1
      if n == 0:
         break
   return digits







def       __a	(						__lowerCamelCase    ):
   return 1 if n == 0 else math.floor(math.log(abs(__lowerCamelCase    ),						10    ) + 1    )







def       __a	(						__lowerCamelCase    ):
   return len(str(abs(__lowerCamelCase    )    )    )







def       __a	(						):
   from collections.abc import Callable

   def benchmark_a_function(__lowerCamelCase,						__lowerCamelCase    ) -> None:
      UpperCAmelCase_   :   Optional[Any]     	=       f"""{func.__name__}({value})"""
      UpperCAmelCase_   :   Optional[int]     	=       timeit(f"""__main__.{call}""",						setup="import __main__"    )
      print(f"""{call}: {func(__lowerCamelCase    )} -- {timing} seconds"""    )

   for value in (26_2144, 1125_8999_0684_2624, 126_7650_6002_2822_9401_4967_0320_5376):
      for func in (num_digits, num_digits_fast, num_digits_faster):
         benchmark_a_function(__lowerCamelCase,						__lowerCamelCase    )
      print()


if __name__ == "__main__":
    import doctest

    doctest.testmod()
    benchmark()


