def convert_checkpoint(bert_config, output_path, v1_checkpoint): """Converts a V1 checkpoint into an OO V2 checkpoint.""" output_dir, _ = os.path.split(output_path) # Create a temporary V1 name-converted checkpoint in the output directory. temporary_checkpoint_dir = os.path.join(output_dir, "temp_v1") temporary_checkpoint = os.path.join(temporary_checkpoint_dir, "ckpt") tf1_checkpoint_converter_lib.convert( checkpoint_from_path=v1_checkpoint, checkpoint_to_path=temporary_checkpoint, num_heads=bert_config.num_attention_heads, name_replacements=tf1_checkpoint_converter_lib. BERT_V2_NAME_REPLACEMENTS, permutations=tf1_checkpoint_converter_lib.BERT_V2_PERMUTATIONS, exclude_patterns=["adam", "Adam"]) # Create a V2 checkpoint from the temporary checkpoint. model = _create_bert_model(bert_config) tf1_checkpoint_converter_lib.create_v2_checkpoint(model, temporary_checkpoint, output_path) # Clean up the temporary checkpoint, if it exists. try: tf.io.gfile.rmtree(temporary_checkpoint_dir) except tf.errors.OpError: # If it doesn't exist, we don't need to clean it up; continue. pass
>>>>>>> a811a3b7e640722318ad868c99feddf3f3063e36 # Create a temporary V1 name-converted checkpoint in the output directory. temporary_checkpoint_dir = os.path.join(output_dir, "temp_v1") temporary_checkpoint = os.path.join(temporary_checkpoint_dir, "ckpt") tf1_checkpoint_converter_lib.convert( checkpoint_from_path=v1_checkpoint, checkpoint_to_path=temporary_checkpoint, num_heads=bert_config.num_attention_heads, name_replacements=tf1_checkpoint_converter_lib.BERT_V2_NAME_REPLACEMENTS, permutations=tf1_checkpoint_converter_lib.BERT_V2_PERMUTATIONS, exclude_patterns=["adam", "Adam"]) # Create a V2 checkpoint from the temporary checkpoint. model = _create_bert_model(bert_config) tf1_checkpoint_converter_lib.create_v2_checkpoint(model, temporary_checkpoint, output_path) # Clean up the temporary checkpoint, if it exists. try: tf.io.gfile.rmtree(temporary_checkpoint_dir) except tf.errors.OpError: # If it doesn't exist, we don't need to clean it up; continue. pass def main(_): output_path = FLAGS.converted_checkpoint_path v1_checkpoint = FLAGS.checkpoint_to_convert bert_config = configs.BertConfig.from_json_file(FLAGS.bert_config_file) convert_checkpoint(bert_config, output_path, v1_checkpoint)