forked from mindspore-Ecosystem/mindspore
fix bert export bug
This commit is contained in:
parent
3eb4c14d86
commit
0d1f209d69
|
@ -21,23 +21,26 @@ import mindspore.common.dtype as mstype
|
||||||
from mindspore.train.serialization import load_checkpoint, export
|
from mindspore.train.serialization import load_checkpoint, export
|
||||||
|
|
||||||
from src.finetune_eval_model import BertCLSModel, BertSquadModel, BertNERModel
|
from src.finetune_eval_model import BertCLSModel, BertSquadModel, BertNERModel
|
||||||
from src.finetune_eval_config import optimizer_cfg, bert_net_cfg
|
from src.finetune_eval_config import bert_net_cfg
|
||||||
from src.bert_for_finetune import BertNER
|
from src.bert_for_finetune import BertNER
|
||||||
from src.utils import convert_labels_to_index
|
from src.utils import convert_labels_to_index
|
||||||
|
|
||||||
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
|
|
||||||
|
|
||||||
parser = argparse.ArgumentParser(description='Bert export')
|
parser = argparse.ArgumentParser(description='Bert export')
|
||||||
|
parser.add_argument("--device_id", type=int, default=0, help="Device id")
|
||||||
parser.add_argument('--use_crf', type=str, default="false", help='Use cfg, default is false.')
|
parser.add_argument('--use_crf', type=str, default="false", help='Use cfg, default is false.')
|
||||||
parser.add_argument('--downstream_task', type=str, choices=["NER", "CLS", "SQUAD"], default="NER",
|
parser.add_argument('--downstream_task', type=str, choices=["NER", "CLS", "SQUAD"], default="NER",
|
||||||
help='at present,support NER only')
|
help='at present,support NER only')
|
||||||
parser.add_argument('--num_class', type=int, default=41, help='The number of class, default is 41.')
|
parser.add_argument('--num_class', type=int, default=41, help='The number of class, default is 41.')
|
||||||
|
parser.add_argument("--batch_size", type=int, default=16, help="batch size")
|
||||||
parser.add_argument('--label_file_path', type=str, default="", help='label file path, used in clue benchmark.')
|
parser.add_argument('--label_file_path', type=str, default="", help='label file path, used in clue benchmark.')
|
||||||
parser.add_argument('--ckpt_file', type=str, required=True, help='Bert ckpt file.')
|
parser.add_argument('--ckpt_file', type=str, required=True, help='Bert ckpt file.')
|
||||||
parser.add_argument('--output_file', type=str, default='Bert.air', help='bert output air name.')
|
parser.add_argument('--output_file', type=str, default='Bert.air', help='bert output air name.')
|
||||||
parser.add_argument('--file_format', type=str, choices=["AIR", "ONNX", "MINDIR"], default='AIR', help='file format')
|
parser.add_argument('--file_format', type=str, choices=["AIR", "ONNX", "MINDIR"], default='AIR', help='file format')
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend", device_id=args.device_id)
|
||||||
|
|
||||||
label_list = []
|
label_list = []
|
||||||
with open(args.label_file_path) as f:
|
with open(args.label_file_path) as f:
|
||||||
for label in f:
|
for label in f:
|
||||||
|
@ -57,7 +60,7 @@ else:
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
if args.downstream_task == "NER":
|
if args.downstream_task == "NER":
|
||||||
if args.use_crf.lower() == "true":
|
if args.use_crf.lower() == "true":
|
||||||
net = BertNER(bert_net_cfg, optimizer_cfg.batch_size, False, num_labels=number_labels,
|
net = BertNER(bert_net_cfg, args.batch_size, False, num_labels=number_labels,
|
||||||
use_crf=True, tag_to_index=tag_to_index)
|
use_crf=True, tag_to_index=tag_to_index)
|
||||||
else:
|
else:
|
||||||
net = BertNERModel(bert_net_cfg, False, number_labels, use_crf=(args.use_crf.lower() == "true"))
|
net = BertNERModel(bert_net_cfg, False, number_labels, use_crf=(args.use_crf.lower() == "true"))
|
||||||
|
@ -71,10 +74,10 @@ if __name__ == '__main__':
|
||||||
load_checkpoint(args.ckpt_file, net=net)
|
load_checkpoint(args.ckpt_file, net=net)
|
||||||
net.set_train(False)
|
net.set_train(False)
|
||||||
|
|
||||||
input_ids = Tensor(np.zeros([optimizer_cfg.batch_size, bert_net_cfg.seq_length]), mstype.int32)
|
input_ids = Tensor(np.zeros([args.batch_size, bert_net_cfg.seq_length]), mstype.int32)
|
||||||
input_mask = Tensor(np.zeros([optimizer_cfg.batch_size, bert_net_cfg.seq_length]), mstype.int32)
|
input_mask = Tensor(np.zeros([args.batch_size, bert_net_cfg.seq_length]), mstype.int32)
|
||||||
token_type_id = Tensor(np.zeros([optimizer_cfg.batch_size, bert_net_cfg.seq_length]), mstype.int32)
|
token_type_id = Tensor(np.zeros([args.batch_size, bert_net_cfg.seq_length]), mstype.int32)
|
||||||
label_ids = Tensor(np.zeros([optimizer_cfg.batch_size, bert_net_cfg.seq_length]), mstype.int32)
|
label_ids = Tensor(np.zeros([args.batch_size, bert_net_cfg.seq_length]), mstype.int32)
|
||||||
|
|
||||||
if args.downstream_task == "NER" and args.use_crf.lower() == "true":
|
if args.downstream_task == "NER" and args.use_crf.lower() == "true":
|
||||||
input_data = [input_ids, input_mask, token_type_id, label_ids]
|
input_data = [input_ids, input_mask, token_type_id, label_ids]
|
||||||
|
|
Loading…
Reference in New Issue