Upgrade python project from paddle-1.x to paddle-2.0
Project description
Upgrade your python model from paddle-1.x to paddle-2.
Attention
paddle_upgrade_tool aims to convert python files from paddle-1.x to paddle-2 one by one, it won't handle indirect imports. e.g.
# filename "a.py"
import paddle.fluid as fluid
pass
# filename "b.py"
from a import *
class MyLayer(fluid.layers.Layer):
pass
fluid.layers.Layer in "b.py" won't get converted.
So you have to make sure you have import all used paddle modules, classes, objects directly for every python file before running paddle_upgrade_tool.
Install
paddle_upgrade_tool support Linux, Mac OS, Windows(Git Bash is recommended), but it requires Python 3.5.4 or higher to run. Multi-Process is supported for Linux and Mac OS, Single-Process is support for Windows, this will lead to performance difference.
- install with pip
pip install -U paddle_upgrade_tool
paddle_upgrade_tool --help # show help
paddle_upgrade_tool --inpath /path/to/model.py # upgrade your model from paddle-1.x to paddle-2.0
ATTENTION: If your device contains multiple versions of python, you may need to run the following commands instead:
python3 -m pip install -U paddle_upgrade_tool
python3 -m paddle_upgrade_tool -h
python3 -m paddle_upgrade_tool --inpath /path/to/model.py
- install from source
git clone https://github.com/T8T9/paddle_upgrade_tool.git
cd paddle_upgrade_tool
python setup.py sdist bdist_wheel
pip install -U ./dist/paddle_upgrade_tool-*.whl
paddle_upgrade_tool --help # show help
paddle_upgrade_tool --inpath /path/to/model.py # upgrade your model from paddle-1.x to paddle-2.0
Develop
If you are a develop, and you want to test your code quickly, you can run the following command in project directory:
python -m paddle_upgrade_tool --inpath /path/to/model.py
#or
python paddle_upgrade_tool/main.py --inpath /path/to/model.py
Moreover, if you want to run a specific refactor, you can use the following command:
python -m paddle_upgrade_tool --inpath /path/to/model.py --refactor <refactor_name>
use python -m paddle_upgrade_tool -h to see full list of all refactors.
if you want to run all unittest, use command:
python -m unittest discover paddle_upgrade_tool/tests/
# or
python setup.py test
or use command:
python -m unittest paddle_upgrade_tool/tests/test_refactor.py
to run specific test file.
Change Spec
change_spec is a python dict defined in spec.py, it defines the rules to refactor your code.
change_spec = {
"path.to.old_api": {
"alias": [
"path.to.old_api_alias1",
"path.to1.to2.old_api_alias2",
],
"update_to": "path.to.new_api",
"warning": "this api is deprecated.",
"args_list": ["arg1", "arg2"],
"args_change": [
["arg2", "arg2_rename"],
["arg3", ""],
["", "new_arg", "default_value"],
],
"args_warning": {"arg1":"warning message"},
"args_transformer": "_default_transformer",
},
}
alias: a list of alias of main aliaspath.to.old_api, all alias will be replaced with main alias.update_to:path.to.old_apiwill be replaced with this new api if specified.warning: print specified warning message whenpath.to.old_apiis found. This field will be ignored ifupdate_tois specified.args_list: is argument list ofpath.to.old_api.args_change: a list of list. It contains following format:["arg", "new_arg"]: rename a argument, e.g.func(arg=value)->func(new_arg=value)["arg", ""]: remove a argument, e.g.func(arg=value)->func()["", "new_arg", "default_value"]: add a new argument, e.g.func(arg=value)->func(arg=value, new_arg=default_value)
args_warning: print specified warning message for specified argument after applyargs_change.args_transformer: execute customized transformer on an AST node, it will be called after applyingargs_changeto do further refactor.
Other Tools
- find pattern of specific code snippet, usage:
find_pattern 'import paddle'
find_pattern command will traverse all nodes in AST, if you see code snippet you want, type in 'y' to get pattern.
- find match node in specific code for specific pattern, usage:
find_match_node -ss 'import paddle' -ps 'any'
you can also specify "--print-results" option to got representation of matched node, specify "--print-lineno" to got line number of matched code.
Acknowledgements
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file paddle_upgrade_tool-0.0.27.tar.gz.
File metadata
- Download URL: paddle_upgrade_tool-0.0.27.tar.gz
- Upload date:
- Size: 94.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
993bdbabc813ac4abb7b72e8cf1b18f9dfa110d7ac15b5cd8d518b707a6fef45
|
|
| MD5 |
4fd366633cb8a14e159c761b97386568
|
|
| BLAKE2b-256 |
0b05c708c4940304463ade05ad181a268021eb6dc31319576bc855a85ab43df3
|
File details
Details for the file paddle_upgrade_tool-0.0.27-py3-none-any.whl.
File metadata
- Download URL: paddle_upgrade_tool-0.0.27-py3-none-any.whl
- Upload date:
- Size: 111.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0d9a878c0680d6123905beee35b8ab00414d5cb705e76b9b87c72cd5481b7427
|
|
| MD5 |
71f8e417a3ba7a3ee14fb837355f3637
|
|
| BLAKE2b-256 |
972457a48429c70f4d1894894f45849f8a721208c81334e3782cec6dbafb7a51
|
File details
Details for the file paddle_upgrade_tool-0.0.27-py2-none-any.whl.
File metadata
- Download URL: paddle_upgrade_tool-0.0.27-py2-none-any.whl
- Upload date:
- Size: 111.1 kB
- Tags: Python 2
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7c2ff0de223c792375fb140afcc357cc2ddc88d7b0278f7c43aa7575780e8ae4
|
|
| MD5 |
798f78ecaea1014c5ff13e3a64727527
|
|
| BLAKE2b-256 |
9f756640863baebb1fb84dae92431eb1ce1a3d746caa038912d76cbbf92bae8d
|