Package: paddle-cppt
paddle-cpptConvert Pytorch to Paddle ToolkitThis Repo contains the toolkit that help you transform the pytorch model to paddle model. eg: Weight file Comparer, Weight Converter, Weight Summary, and so on ...Featurescppt gen_diff: generate the diff between paddle weight and torch weight file, for more details you check out ...cppt auto_match: auto match the names of paddle and torch occording to the name semantic, eg: torch.model.embeddings.embed_token.weight -> paddle.opt.model.word_embeddings.weight ...cppt convert: convert the torch weight file to paddle weight file according to the diff file ...cppt summary: summary the tensor meta info according to the weight files and diff file ...IntroIn order to convert pytorch weight file to paddle weight file and make sure that the logits of paddle model is absolute align with pytorch, there are some steps you should follow.first, you should get the layer names of you paddle model. In this abastract, you can init the paddle model with the same configuration as pytorch model, and save the state_dict.second, in order to convert pytorch weight file to paddle weight file, you should find the name mapping between weight files, so you can load the state dict of paddle weight and pytorch weight file and find the diffs. In this abstract, you can use the commandcppt gen_diffto find the diffs between layer names.third, mapping the names between pytorch and paddle models is a boring work, so let's make it more intelligent. You can usecppt auto_matchcommand to auto match the names with similarity algo. you can edit the final diff file, and make it correct.fourth, you get the correct name mapping with third step, you can use thecppt convertcommand to convet the pytorch weight file to paddle weight file. In this abstract, the script will automaticly transpose the linear-weight tensor.finaly, in order to checking the tensor data of paddle weight file, you can use the commandcppt summarythe generate the meta info between paddle weight file.So, it's cool right ? these codes help meconvertingwork more soft.But, there are also some great works can be done:compare the computing gragh between pytorch and paddle code.compare the outputs of different layers, eg: embedding layer, transformer layer, lm_head layer and so on.convert the pytorch code to paddle code using the ast. Objecoive： We can't convert it and make it run at onece, but you can convert it and edit it with simple works to make it run.If you have more ideas about it, you can postissueto discuss with us. We look forward to discussing it with you.Quick StartInstallationpipinstallpaddle-cpptor install from the source code:gitclonehttps://github.com/wj-Mcat/paddle-cpptcdpaddle-cpp
pythonsetup.pyinstallsave paddle state dictIf you complete the code, you can init the paddle weight file with the following code:frompaddlenlp.transformers.{your-model}.modelingimport{YourModel}# this code will be different according to different model, but anyway the final result is to save the state dict of model which contains the layer names of your model code.model={YourModel}(model={YourModel}(**{YourModel}.pretrained_init_configuration['{name-of-configuration}']))model.save_pretrained('/path/to/local/dir')cppt gen_diffthis command will generate the name mappingcpptgen_diff\--torch_file=/path/to/pytorch_model.bin\--paddle_file=/path/to/model_state.pdparams\--output_file=/path/to/diff.xlsx--auto_matchwith this command, you will get thediff.xlsxfile which contains the layer name of paddle/torch weight file, eg:torch-nametorch-shapetorch-dtypetorch-typepaddle-namepaddle-shapepaddle-dtypepaddle-typeembeddings.position_ids[1, 512]torch.int64embeddingembeddings.word_embeddings.weight[30522, 768]paddle.float32embeddingembeddings.word_embeddings.weight[30522, 768]torch.float32embeddingembeddings.position_embeddings.weight[512, 768]paddle.float32embeddingembeddings.position_embeddings.weight[512, 768]torch.float32embeddingembeddings.token_type_embeddings.weight[2, 768]paddle.float32embeddingembeddings.token_type_embeddings.weight[2, 768]torch.float32embeddingembeddings.layer_norm.weight[768]paddle.float32embeddingembeddings.LayerNorm.weight[768]torch.float32embeddingembeddings.layer_norm.bias[768]paddle.float32embeddingembeddings.LayerNorm.bias[768]torch.float32embeddingencoder.layers.0.self_attn.q_proj.weight[768, 768]paddle.float32linear-weightencoder.layer.0.attention.self.query.weight[768, 768]torch.float32linear-weightencoder.layers.0.self_attn.q_proj.bias[768]paddle.float32linear-biasencoder.layer.0.attention.self.query.bias[768]torch.float32linear-biasencoder.layers.0.self_attn.k_proj.weight[768, 768]paddle.float32linear-weightencoder.layer.0.attention.self.key.weight[768, 768]torch.float32linear-weightencoder.layers.0.self_attn.k_proj.bias[768]paddle.float32linear-biasencoder.layer.0.attention.self.key.bias[768]torch.float32linear-biasencoder.layers.0.self_attn.v_proj.weight[768, 768]paddle.float32linear-weightencoder.layer.0.attention.self.value.weight[768, 768]torch.float32linear-weightencoder.layers.0.self_attn.v_proj.bias[768]paddle.float32linear-biasencoder.layer.0.attention.self.value.bias[768]torch.float32linear-biasencoder.layers.0.self_attn.out_proj.weight[768, 768]paddle.float32linear-weightencoder.layer.0.attention.output.dense.weight[768, 768]torch.float32linear-weightencoder.layers.0.self_attn.out_proj.bias[768]paddle.float32linear-biasencoder.layer.0.attention.output.dense.bias[768]torch.float32linear-biasencoder.layers.0.linear1.weight[768, 3072]paddle.float32linear-weightencoder.layer.0.attention.output.LayerNorm.weight[768]torch.float32normencoder.layers.0.linear1.bias[3072]paddle.float32linear-biascppt auto_matchthis command will generate the final name mapping into the excel file.cpptauto_match\--diff_file=/path/to/diff.xlsx\--output_file=/path/to/diff-result.xlsxcppt convertconvert torch model to paddle weight according the final diff file.cpptconvert\--torch_file=/path/to/pytorch_model.bin\--output_file=/path/to/model_state.pdparams\--diff_file=/path/to/diff-result.xlsxcppt summaryprint the summary metadata info between torch and paddle modelcpptsummary\--torch_file=/path/to/pytorch_model.bin\--output_file=/path/to/model_state.pdparams\--diff_file=/path/to/diff.xlsxCreatorsLicenseReleased underApache License 2.0by@wj-Mcat.Copyright & LicenseCode & Docs © 2022 wj-Mcatwjmcater@gmail.comCode released under the Apache-2.0 LicenseDocs released under Creative Commons