original_docstring: Load tf checkpoints in a pytorch model
docstring: Load tf checkpoints in a pytorch model
docstring_tokens: Load tf checkpoints in a pytorch model
code: def load_tf_weights_in_gpt2(model, config, gpt2_checkpoint_path):
      """Load tf checkpoints in a pytorch model"""
      try:
          import re
  
          import tensorflow as tf
      except ImportError:
          logger.error(
              "Loading a TensorFlow model in PyTorch, requires TensorFlow to be installed. Please see "
              "https://www.tensorflow.org/install/ for installation instructions."
          )
          raise
      tf_path = os.path.abspath(gpt2_checkpoint_path)
      logger.info("Converting TensorFlow checkpoint from {}".format(tf_path))
      # Load weights from TF model
      init_vars = tf.train.list_variables(tf_path)
      names = []
      arrays = []
      for name, shape in init_vars:
          logger.info("Loading TF weight {} with shape {}".format(name, shape))
          array = tf.train.load_variable(tf_path, name)
          names.append(name)
          arrays.append(array.squeeze())
  
      for name, array in zip(names, arrays):
          name = name[6:]  # skip "model/"
          name = name.split("/")
          pointer = model
          for m_name in name:
              if re.fullmatch(r"[A-Za-z]+\d+", m_name):
                  scope_names = re.split(r"(\d+)", m_name)
              else:
                  scope_names = [m_name]
              if scope_names[0] == "w" or scope_names[0] == "g":
                  pointer = getattr(pointer, "weight")
              elif scope_names[0] == "b":
                  pointer = getattr(pointer, "bias")
              elif scope_names[0] == "wpe" or scope_names[0] == "wte":
                  pointer = getattr(pointer, scope_names[0])
                  pointer = getattr(pointer, "weight")
              else:
                  pointer = getattr(pointer, scope_names[0])
              if len(scope_names) >= 2:
                  num = int(scope_names[1])
                  pointer = pointer[num]
          try:
              assert (
                  pointer.shape == array.shape
              ), f"Pointer shape {pointer.shape} and array shape {array.shape} mismatched"
          except AssertionError as e:
              e.args += (pointer.shape, array.shape)
              raise
          logger.info("Initialize PyTorch weight {}".format(name))
          pointer.data = torch.from_numpy(array)
      return model
code_tokens: def load_tf_weights_in_gpt2 ( model , config , gpt2_checkpoint_path ) : try : import re import tensorflow as tf except ImportError : logger . error ( "Loading a TensorFlow model in PyTorch, requires TensorFlow to be installed. Please see " "https://www.tensorflow.org/install/ for installation instructions." ) raise tf_path = os . path . abspath ( gpt2_checkpoint_path ) logger . info ( "Converting TensorFlow checkpoint from {}" . format ( tf_path ) ) init_vars = tf . train . list_variables ( tf_path ) names = [ ] arrays = [ ] for name , shape in init_vars : logger . info ( "Loading TF weight {} with shape {}" . format ( name , shape ) ) array = tf . train . load_variable ( tf_path , name ) names . append ( name ) arrays . append ( array . squeeze ( ) ) for name , array in zip ( names , arrays ) : name = name [ 6 : ] name = name . split ( "/" ) pointer = model for m_name in name : if re . fullmatch ( r"[A-Za-z]+\d+" , m_name ) : scope_names = re . split ( r"(\d+)" , m_name ) else : scope_names = [ m_name ] if scope_names [ 0 ] == "w" or scope_names [ 0 ] == "g" : pointer = getattr ( pointer , "weight" ) elif scope_names [ 0 ] == "b" : pointer = getattr ( pointer , "bias" ) elif scope_names [ 0 ] == "wpe" or scope_names [ 0 ] == "wte" : pointer = getattr ( pointer , scope_names [ 0 ] ) pointer = getattr ( pointer , "weight" ) else : pointer = getattr ( pointer , scope_names [ 0 ] ) if len ( scope_names ) >= 2 : num = int ( scope_names [ 1 ] ) pointer = pointer [ num ] try : assert ( pointer . shape == array . shape ) , f"Pointer shape {pointer.shape} and array shape {array.shape} mismatched" except AssertionError as e : e . args += ( pointer . shape , array . shape ) raise logger . info ( "Initialize PyTorch weight {}" . format ( name ) ) pointer . data = torch . from_numpy ( array ) return model
short_docstring: Load tf checkpoints in a pytorch model
short_docstring_tokens: Load tf checkpoints in a pytorch model
comment: """Load tf checkpoints in a pytorch model"""$SEP$# Load weights from TF model$SEP$# skip "model/"