# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # 不适用于这里

# +

import os
import torch
from pytorch_pretrained_bert import WEIGHTS_NAME, CONFIG_NAME
from pytorch_pretrained_bert import BertTokenizer, BertModel, BertForMaskedLM,BertForNextSentencePrediction,BertForPreTraining,BertForQuestionAnswering



# model=torch.load('./finetuned_lm/pytorch_model.bin')
# model

#     parser.add_argument("--bert_model", default=None, type=str, required=True,
#                         help="Bert pre-trained model selected in the list: bert-base-uncased, "
#                         "bert-large-uncased, bert-base-cased, bert-large-cased, bert-base-multilingual-uncased, "
#                         "bert-base-multilingual-cased, bert-base-chinese.")

# tokenizer = BertTokenizer.from_pretrained('bert-base-cased')
# #model = BertForPreTraining.from_pretrained('bert-base-chinese')
# # model = BertForQuestionAnswering.from_pretrained('bert-base-chinese')
# model = BertForPreTraining.from_pretrained('bert-base-cased')

output_dir = "./data/model/"

# Step 1: Save a model, configuration and vocabulary that you have fine-tuned

# If we have a distributed model, save only the encapsulated model
# (it was wrapped in PyTorch DistributedDataParallel or DataParallel)
model_to_save = model.module if hasattr(model, 'module') else model

# If we save using the predefined names, we can load using `from_pretrained`
output_model_file = os.path.join(output_dir, WEIGHTS_NAME)
output_config_file = os.path.join(output_dir, CONFIG_NAME)

torch.save(model_to_save.state_dict(), output_model_file)
model_to_save.config.to_json_file(output_config_file)
tokenizer.save_vocabulary(output_dir)

# Step 2: Re-load the saved model and vocabulary

# # Example for a Bert model
# model = BertForQuestionAnswering.from_pretrained(output_dir)
# tokenizer = BertTokenizer.from_pretrained(output_dir, do_lower_case=args.do_lower_case)  # Add specific options if needed
# # Example for a GPT model
# model = OpenAIGPTDoubleHeadsModel.from_pretrained(output_dir)
# tokenizer = OpenAIGPTTokenizer.from_pretrained(output_dir)
# -

# # 第二种导出方式

# +

import os
import torch
from pytorch_pretrained_bert import WEIGHTS_NAME, CONFIG_NAME
from pytorch_pretrained_bert import BertTokenizer, BertModel, BertForMaskedLM,BertForNextSentencePrediction,BertForPreTraining,BertForQuestionAnswering

tokenizer = BertTokenizer.from_pretrained('bert-base-cased')
#model = BertForPreTraining.from_pretrained('bert-base-chinese')
# model = BertForQuestionAnswering.from_pretrained('bert-base-chinese')
model = BertForQuestionAnswering.from_pretrained('bert-base-cased')

# +
output_model_file = "./data/model/pytorch_model.bin"
output_config_file = "./data/model/config.json"
output_vocab_file = "./data/model/"

# Step 1: Save a model, configuration and vocabulary that you have fine-tuned

# If we have a distributed model, save only the encapsulated model
# (it was wrapped in PyTorch DistributedDataParallel or DataParallel)
model_to_save = model.module if hasattr(model, 'module') else model

torch.save(model_to_save.state_dict(), output_model_file)
model_to_save.config.to_json_file(output_config_file)
tokenizer.save_vocabulary(output_vocab_file)
# tokenizer.save_vocabulary(output_dir)
# -





StarcoderdataJupyterScriptsDedupFiltered