paddle-cppt
This 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 ...
Features
cppt 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 ...
Intro
In 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 command
cppt 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 use
cppt 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 the
cppt 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 command
cppt summarythe generate the meta info between paddle weight file.
So, it's cool right ? these codes help me converting work 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 post issue to discuss with us. We look forward to discussing it with you.
Quick Start
Installation
pip install paddle-cppt
or install from the source code:
git clone https://github.com/wj-Mcat/paddle-cppt
cd paddle-cpp
python setup.py install
save paddle state dict
If you complete the code, you can init the paddle weight file with the following code:
from paddlenlp.transformers.{your-model}.modeling import {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_diff
this command will generate the name mapping
cppt gen_diff \
--torch_file=/path/to/pytorch_model.bin \
--paddle_file=/path/to/model_state.pdparams \
--output_file=/path/to/diff.xlsx --auto_match
with this command, you will get the diff.xlsx file which contains the layer name of paddle/torch weight file, eg:
| torch-name | torch-shape | torch-dtype | torch-type | paddle-name | paddle-shape | paddle-dtype | paddle-type |
|---|---|---|---|---|---|---|---|
| embeddings.position_ids | [1, 512] | torch.int64 | embedding | embeddings.word_embeddings.weight | [30522, 768] | paddle.float32 | embedding |
| embeddings.word_embeddings.weight | [30522, 768] | torch.float32 | embedding | embeddings.position_embeddings.weight | [512, 768] | paddle.float32 | embedding |
| embeddings.position_embeddings.weight | [512, 768] | torch.float32 | embedding | embeddings.token_type_embeddings.weight | [2, 768] | paddle.float32 | embedding |
| embeddings.token_type_embeddings.weight | [2, 768] | torch.float32 | embedding | embeddings.layer_norm.weight | [768] | paddle.float32 | embedding |
| embeddings.LayerNorm.weight | [768] | torch.float32 | embedding | embeddings.layer_norm.bias | [768] | paddle.float32 | embedding |
| embeddings.LayerNorm.bias | [768] | torch.float32 | embedding | encoder.layers.0.self_attn.q_proj.weight | [768, 768] | paddle.float32 | linear-weight |
| encoder.layer.0.attention.self.query.weight | [768, 768] | torch.float32 | linear-weight | encoder.layers.0.self_attn.q_proj.bias | [768] | paddle.float32 | linear-bias |
| encoder.layer.0.attention.self.query.bias | [768] | torch.float32 | linear-bias | encoder.layers.0.self_attn.k_proj.weight | [768, 768] | paddle.float32 | linear-weight |
| encoder.layer.0.attention.self.key.weight | [768, 768] | torch.float32 | linear-weight | encoder.layers.0.self_attn.k_proj.bias | [768] | paddle.float32 | linear-bias |
| encoder.layer.0.attention.self.key.bias | [768] | torch.float32 | linear-bias | encoder.layers.0.self_attn.v_proj.weight | [768, 768] | paddle.float32 | linear-weight |
| encoder.layer.0.attention.self.value.weight | [768, 768] | torch.float32 | linear-weight | encoder.layers.0.self_attn.v_proj.bias | [768] | paddle.float32 | linear-bias |
| encoder.layer.0.attention.self.value.bias | [768] | torch.float32 | linear-bias | encoder.layers.0.self_attn.out_proj.weight | [768, 768] | paddle.float32 | linear-weight |
| encoder.layer.0.attention.output.dense.weight | [768, 768] | torch.float32 | linear-weight | encoder.layers.0.self_attn.out_proj.bias | [768] | paddle.float32 | linear-bias |
| encoder.layer.0.attention.output.dense.bias | [768] | torch.float32 | linear-bias | encoder.layers.0.linear1.weight | [768, 3072] | paddle.float32 | linear-weight |
| encoder.layer.0.attention.output.LayerNorm.weight | [768] | torch.float32 | norm | encoder.layers.0.linear1.bias | [3072] | paddle.float32 | linear-bias |
cppt auto_match
this command will generate the final name mapping into the excel file.
cppt auto_match \
--diff_file=/path/to/diff.xlsx \
--output_file=/path/to/diff-result.xlsx
cppt convert
convert torch model to paddle weight according the final diff file.
cppt convert \
--torch_file=/path/to/pytorch_model.bin \
--output_file=/path/to/model_state.pdparams \
--diff_file=/path/to/diff-result.xlsx
cppt summary
print the summary metadata info between torch and paddle model
cppt summary \
--torch_file=/path/to/pytorch_model.bin \
--output_file=/path/to/model_state.pdparams \
--diff_file=/path/to/diff.xlsx
Creators
License
Released under Apache License 2.0 by @wj-Mcat.
Copyright & License
- Code & Docs © 2022 wj-Mcat wjmcater@gmail.com
- Code released under the Apache-2.0 License
- Docs released under Creative Commons
Metadata
Release files for paddle-cppt 0.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| paddle-cppt-0.0.1.tar.gz | 17.5 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| paddle_cppt-0.0.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 32.5 kB
Release files / paddle-cppt-0.0.1.tar.gz
| Download URL | paddle-cppt-0.0.1.tar.gz |
|---|---|
| Size | 17.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d479aaad4b860b18414306fc16ec6da7b35ad77bbd0b65d83fc8ddd5d83570b4
|
|
BLAKE2b-256 checksum How to use checksums |
9f710d221907b12c37bccc49eaa4f677cbb83e1eef58abb1b76ca3e697b46609
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.8.13
|
Release files / paddle_cppt-0.0.1-py3-none-any.whl
| Download URL | paddle_cppt-0.0.1-py3-none-any.whl |
|---|---|
| Size | 15.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f685f756fb3963220944b31fdfbe6288ddde3eb98f2e72b79045cd19d0aa9bba
|
|
BLAKE2b-256 checksum How to use checksums |
76d3e87399b7a38a84b61e2688302220d0f7f0bfe511c11e657621ed1227e921
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.8.13
|
Security for paddle-cppt 0.0.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.