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:
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:
Upgrade your python model from paddle-1.x to paddle-2.
Attention
paddle_upgrade_toolaims to convert python files from paddle-1.x to paddle-2 one by one, it won’t handle indirect imports. e.g.fluid.layers.Layerin “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 runningpaddle_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.
ATTENTION: If your device contains multiple versions of python, you may need to run the following commands instead:
Develop
If you are a develop, and you want to test your code quickly, you can run the following command in project directory:
Moreover, if you want to run a specific refactor, you can use the following command:
use
python -m paddle_upgrade_tool -hto see full list of all refactors.if you want to run all unittest, use command:
or use command:
to run specific test file.
Change Spec
change_specis a python dict defined in spec.py, it defines the rules to refactor your code.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_patterncommand will traverse all nodes in AST, if you see code snippet you want, type in ‘y’ to get pattern.you can also specify “–print-results” option to got representation of matched node, specify “–print-lineno” to got line number of matched code.
Acknowledgements