






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					)   -> Optional[int]:

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

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

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

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

     '''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














from __future__ import annotations







class 							UpperCAmelCase_     :




     def __init__(							self  ,	_lowerCAmelCase		):
          UpperCAmelCase__			:     List[Any]										= value
          UpperCAmelCase__			:     Node | None										= None
          UpperCAmelCase__			:     Node | None										= None







class 							UpperCAmelCase_     :
     __lowerCamelCase										=     42




     def __init__(							self		):
          UpperCAmelCase__			:     Any										= 0




     def      __UpperCAmelCase     (							self  ,	_lowerCAmelCase  ,	_lowerCAmelCase		):
          if node is None:
               return

          if path_sum == self.target:
               self.paths += 1

          if node.left:
               self.depth_first_search(node.left  ,	path_sum + node.left.value		)
          if node.right:
               self.depth_first_search(node.right  ,	path_sum + node.right.value		)




     def      __UpperCAmelCase     (							self  ,	_lowerCAmelCase  ,	_lowerCAmelCase = None		):
          if node is None:
               return 0
          if target is not None:
               UpperCAmelCase__			:     List[Any]										= target

          self.depth_first_search(_lowerCAmelCase  ,	node.value		)
          self.path_sum(node.left		)
          self.path_sum(node.right		)

          return self.paths


if __name__ == "__main__":
      import doctest

      doctest.testmod()






