shibing624 / pycorrector

pycorrector is a toolkit for text error correction. 文本纠错,实现了Kenlm,T5,MacBERT,ChatGLM3,Qwen2.5等模型应用在纠错场景,开箱即用。
https://www.mulanai.com/product/corrector/
Apache License 2.0
5.61k stars 1.1k forks source link

macbert训练后如何转化为onnx模型呢 #374

Closed lazywangyuan closed 11 months ago

lazywangyuan commented 1 year ago
if ckpt_path and os.path.exists(ckpt_path):
    model.load_state_dict(torch.load(ckpt_path)['state_dict'])
    # 先保存原始transformer bert model
    tokenizer.save_pretrained(cfg.OUTPUT_DIR)
    bert = BertForMaskedLM.from_pretrained(cfg.MODEL.BERT_CKPT)
    bert.save_pretrained(cfg.OUTPUT_DIR)
    state_dict = torch.load(ckpt_path)['state_dict']
    new_state_dict = OrderedDict()
    if cfg.MODEL.NAME in ['macbert4csc']:
        for k, v in state_dict.items():
            if k.startswith('bert.'):
                new_state_dict[k[5:]] = v
    else:
        new_state_dict = state_dict
    # 再保存finetune训练后的模型文件,替换原始的pytorch_model.bin
    torch.save(new_state_dict, os.path.join(cfg.OUTPUT_DIR, 'pytorch_model.bin'))
shibing624 commented 1 year ago

还没做,稍后我补充下。

Yazooliu commented 1 year ago

还没做,稍后我补充下。

Could I update code and PR to support this exchange model to onnx format ? thanks.

BestRegards Yazhou

shibing624 commented 1 year ago

可以

Yazooliu commented 1 year ago

可以

Thanks. I will update it as soon as possible

ChenchenJT commented 11 months ago

你好,想问一下这个issue实现了吗,在项目没找到相应的代码或者PR

shibing624 commented 11 months ago

后续我不添加该功能,官方教程就可以转换为onnx。