
import os
import shutil
import unittest
from unittest.mock import patch

from transformers.testing_utils import CaptureStd, is_pt_tf_cross_test, require_torch




class 	UpperCAmelCase_      (			unittest.TestCase					):


  '''simple docstring'''







  @patch("""sys.argv"""      ,							["""fakeprogrampath""", """env"""]   )
  def 							_lowercase							(			self      :			Dict   )      ->     Tuple:







    """simple docstring"""






    import transformers.commands.transformers_cli

    with CaptureStd() as cs:
      transformers.commands.transformers_cli.main()
    self.assertIn("""Python version"""      ,							cs.out   )
    self.assertIn("""Platform"""      ,							cs.out   )
    self.assertIn("""Using distributed or parallel set-up in script?"""      ,							cs.out   )
  @is_pt_tf_cross_test
  @patch(
      """sys.argv"""      ,							["""fakeprogrampath""", """pt-to-tf""", """--model-name""", """hf-internal-testing/tiny-random-gptj""", """--no-pr"""]   )
  def 							_lowercase							(			self      :			List[str]   )      ->     Optional[Any]:







    """simple docstring"""






    import transformers.commands.transformers_cli

    shutil.rmtree("""/tmp/hf-internal-testing/tiny-random-gptj"""      ,							ignore_errors=UpperCamelCase__   )  # cleans potential past runs
    transformers.commands.transformers_cli.main()

    # The original repo has no TF weights -- if they exist, they were created by the CLI
    self.assertTrue(os.path.exists("""/tmp/hf-internal-testing/tiny-random-gptj/tf_model.h5"""   )   )
  @require_torch
  @patch("""sys.argv"""      ,							["""fakeprogrampath""", """download""", """hf-internal-testing/tiny-random-gptj""", """--cache-dir""", """/tmp"""]   )
  def 							_lowercase							(			self      :			List[Any]   )      ->     Dict:







    """simple docstring"""






    import transformers.commands.transformers_cli

    # # remove any previously downloaded model to start clean
    shutil.rmtree("""/tmp/models--hf-internal-testing--tiny-random-gptj"""      ,							ignore_errors=UpperCamelCase__   )

    # run the command
    transformers.commands.transformers_cli.main()

    # check if the model files are downloaded correctly on /tmp/models--hf-internal-testing--tiny-random-gptj
    self.assertTrue(os.path.exists("""/tmp/models--hf-internal-testing--tiny-random-gptj/blobs"""   )   )
    self.assertTrue(os.path.exists("""/tmp/models--hf-internal-testing--tiny-random-gptj/refs"""   )   )
    self.assertTrue(os.path.exists("""/tmp/models--hf-internal-testing--tiny-random-gptj/snapshots"""   )   )





  @require_torch
  @patch(
      """sys.argv"""      ,							[
          """fakeprogrampath""",
          """download""",
          """hf-internal-testing/test_dynamic_model_with_tokenizer""",
          """--trust-remote-code""",
          """--cache-dir""",
          """/tmp""",
      ]      ,							)
  def 							_lowercase							(			self      :			Dict   )      ->     Tuple:







    """simple docstring"""






    import transformers.commands.transformers_cli

    # # remove any previously downloaded model to start clean
    shutil.rmtree("""/tmp/models--hf-internal-testing--test_dynamic_model_with_tokenizer"""      ,							ignore_errors=UpperCamelCase__   )

    # run the command
    transformers.commands.transformers_cli.main()

    # check if the model files are downloaded correctly on /tmp/models--hf-internal-testing--test_dynamic_model_with_tokenizer
    self.assertTrue(os.path.exists("""/tmp/models--hf-internal-testing--test_dynamic_model_with_tokenizer/blobs"""   )   )
    self.assertTrue(os.path.exists("""/tmp/models--hf-internal-testing--test_dynamic_model_with_tokenizer/refs"""   )   )
    self.assertTrue(
        os.path.exists("""/tmp/models--hf-internal-testing--test_dynamic_model_with_tokenizer/snapshots"""   )   )



import torch.nn as nn

from .imports import is_fpa_available


if is_fpa_available():
    import transformer_engine.pytorch as te

def a__   (	A_,					A_=True,					A_=True,					A_=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(A_,					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
      __magic_name__    					=	module.bias is not None
      __magic_name__    					=	te.Linear(
          module.in_features,					module.out_features,					bias=A_,					params_dtype=module.weight.dtype )
      __magic_name__    					=	module.weight.data.clone()
      if has_bias:
        __magic_name__    					=	module.bias.data.clone()

      setattr(A_,					A_,					A_ )
    elif isinstance(A_,					nn.LayerNorm ) and to_transformer_engine and _convert_ln:
      __magic_name__    					=	te.LayerNorm(module.normalized_shape[0],					eps=module.eps,					params_dtype=module.weight.dtype )
      __magic_name__    					=	module.weight.data.clone()
      __magic_name__    					=	module.bias.data.clone()

      setattr(A_,					A_,					A_ )
    elif isinstance(A_,					te.Linear ) and not to_transformer_engine and _convert_linear:
      __magic_name__    					=	module.bias is not None
      __magic_name__    					=	nn.Linear(
          module.in_features,					module.out_features,					bias=A_,					params_dtype=module.weight.dtype )
      __magic_name__    					=	module.weight.data.clone()
      if has_bias:
        __magic_name__    					=	module.bias.data.clone()

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

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





def a__   (	A_ ):






  '''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(A_,					(te.LayerNorm, te.Linear) ):
      return True
  return False

