




import unittest

import numpy as np

from transformers import is_tf_available
from transformers.testing_utils import require_tf


if is_tf_available():
      import tensorflow as tf

      from transformers.activations_tf import get_tf_activation






@require_tf
class       A      (		unittest.TestCase		):
     """simple docstring"""

     def 			__snake_case (     self	: List[str]      )					->							str:
           lowerCamelCase__   	=					tf.constant([-1_0_0, -1.0, -0.1, 0, 0.1, 1.0, 100.0]      )
           lowerCamelCase__   	=					get_tf_activation('gelu'      )
           lowerCamelCase__   	=					get_tf_activation('gelu_10'      )

           lowerCamelCase__   	=					gelu(A__      )
           lowerCamelCase__   	=					geluaa(A__      )

           lowerCamelCase__   	=					tf.where(y_gelu_aa < 10.0						,	1.0						,	0.0      )

           self.assertEqual(tf.math.reduce_max(A__      ).numpy().item()						,	10.0      )
           self.assertTrue(np.allclose(y_gelu * clipped_mask						,	y_gelu_aa * clipped_mask      )      )




     def 			__snake_case (     self	: int      )					->							Optional[Any]:
           get_tf_activation('gelu'      )
           get_tf_activation('gelu_10'      )
           get_tf_activation('gelu_fast'      )
           get_tf_activation('gelu_new'      )
           get_tf_activation('glu'      )
           get_tf_activation('mish'      )
           get_tf_activation('quick_gelu'      )
           get_tf_activation('relu'      )
           get_tf_activation('sigmoid'      )
           get_tf_activation('silu'      )
           get_tf_activation('swish'      )
           get_tf_activation('tanh'      )
           with self.assertRaises(A__      ):
                 get_tf_activation('bogus'      )
           with self.assertRaises(A__      ):
                 get_tf_activation(A__      )
import os
from pathlib import Path
from typing import Dict, List

import fire
import torch

from transformers import AutoModelForSeqaSeqLM, AutoTokenizer
from transformers.utils.logging import get_logger


_SCREAMING_SNAKE_CASE     =     get_logger(__name__)
def 					__lowercase						(    lowerCamelCase__			,					lowerCamelCase__       )					->Optional[int]:

  '''simple docstring'''




  if text.startswith(lowerCamelCase__       ):
    return text[len(lowerCamelCase__       ) :]
  return text  # or whatever
def 					__lowercase						(    lowerCamelCase__       )					->int:

  '''simple docstring'''




  return {remove_prefix(lowerCamelCase__			,					"model."       ): v for k, v in sd.items()}
def 					__lowercase						(    lowerCamelCase__       )					->Any:

  '''simple docstring'''




  a__  		=  {}
  for k in state_dicts[0].keys():
    a__  		=  [sd[k] for sd in state_dicts]
    a__  		=  sum(lowerCamelCase__       ) / len(lowerCamelCase__       )
    assert isinstance(lowerCamelCase__			,					torch.Tensor       )
    a__  		=  new_t
  return new_sd
def 					__lowercase						(    lowerCamelCase__			,					lowerCamelCase__			,					lowerCamelCase__       )					->None:

  '''simple docstring'''




  a__  		=  AutoModelForSeqaSeqLM.from_pretrained(lowerCamelCase__       )
  if os.path.isfile(lowerCamelCase__       ):
    a__  		=  [pl_ckpt_path]
  else:
    assert os.path.isdir(lowerCamelCase__       )
    a__  		=  list(Path(lowerCamelCase__       ).glob("*.ckpt"       )       )
    assert ckpt_files, f"could not find any ckpt files inside the {pl_ckpt_path} directory"

  if len(lowerCamelCase__       ) > 1:
    logger.info(f"averaging the weights of {ckpt_files}"       )

  a__  		=  [sanitize(torch.load(lowerCamelCase__			,					map_location="cpu"       )["state_dict"]       ) for x in ckpt_files]
  a__  		=  average_state_dicts(lowerCamelCase__       )

  a__		,a__  		=  hf_model.load_state_dict(lowerCamelCase__			,					strict=lowerCamelCase__       )
  assert not missing, f"missing keys: {missing}"
  hf_model.save_pretrained(lowerCamelCase__       )
  try:
    a__  		=  AutoTokenizer.from_pretrained(lowerCamelCase__       )
    tok.save_pretrained(lowerCamelCase__       )
  except Exception:
    pass
    # dont copy tokenizer if cant


if __name__ == "__main__":
     fire.Fire(convert_pl_to_hf)

