Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update 2.0 public api in all left files #33313

Merged
merged 2 commits into from
Jun 11, 2021
Merged

update 2.0 public api in all left files #33313

merged 2 commits into from
Jun 11, 2021

Conversation

zhiboniu
Copy link
Contributor

@zhiboniu zhiboniu commented Jun 3, 2021

PR types

Others

PR changes

APIs

Describe

update 2.0 public api in all left files

背景:
对于用户调用api的层级,由于之前没有明确的设计,api开放层级比较随意。影响用户使用中的逻辑和正规感受。
目的:
1)重新整理api开放层级,提升用户使用感受
2)API保留自有特色,同时更好地符合行业规范,降低用户学习成本
3)提高框架api逻辑性,便于后续维护和优化
主要实现方法:
1)__all__内容清晰化,内容逐个列出,一行一个。不再使用 all 的向上传递机制,仅在公开API层级添加 all,其余的 all 列表统一置空
2)不再使用 #DEFINE_ALIAS
3)导入内容清晰化,原则上不再使用 import *, 按需引入需要的API, 每行一个
4)原则上不再使用 from . import module, from .module import submodule时会引入module,避免重复引用
5)添加新的Public API时,需向上寻找到最近的节点添加 import 并加入 all 列表
6)Public API列表由多个公开API层级的 all 列表共同组成,文档抽取以此为准,这些指定层级是:
paddle,
paddle.amp,
paddle.nn,
paddle.nn.functional,
paddle.nn.initializer,
paddle.nn.utils,
paddle.static,
paddle.static.nn,
paddle.io,
paddle.jit,
paddle.metric,
paddle.distribution,
paddle.optimizer,
paddle.optimizer.lr,
paddle.regularizer,
paddle.text,
paddle.utils,
paddle.utils.download,
paddle.utils.profiler,
paddle.utils.cpp_extension,
paddle.sysconfig,
paddle.vision,
paddle.vision.datasets,
paddle.vision.models,
paddle.vision.transforms,
paddle.vision.ops,
paddle.distributed,
paddle.distributed.fleet,
paddle.distributed.fleet.utils,
paddle.distributed.parallel,
paddle.distributed.utils,
paddle.callbacks,
paddle.hub,
paddle.autograd,
paddle.incubate,
paddle.inference,
paddle.onnx,
paddle.Tensor

修改示例展示:
原来写法:
image
新的写法:
image

对开发者使用API的影响:
1)在指定层级推荐方式:(不受影响)
from paddle.nn import CTCLoss
from paddle.nn import Conv2D
2)非指定层级支持但不推荐:(不受影响)
from paddle.nn.layer.loss import CTCLoss
from paddle.nn.layer.conv import Conv2D
3)对于import *使用方式不推荐。(非公开层级受影响)
不再支持:(非公开层级)
from paddle.nn.layer import *
from paddle.nn.layer.conv import *
from paddle.nn.initializer.norm import *
from paddle.tensor.math import *
from paddle.optimizer.adam import *
可以支持:(公开层级)
from paddle import *
from paddle.nn import *
from paddle.utils import *
from paddle.optimizer import *

本pr涉及的改动:
1) 本pr对剩余一些零散目录进行规范化整理,这些目录包括:amp/autograd/incubate/inference/jit/metric/onnx
其中比较重要的整理是:去掉compat中公开接口;新增paddle.inference公开层级;去掉device中重复api接口。
其他是常规规范操作,例如清理底层py文件中all接口等。
2)对近期发现的一些问题进行修复
paddle.tensor.tensor_method_func 中去掉无实现接口 ’mul’
paddle.init 中去掉无实现接口 ‘ComplexTensor’
去掉重复公开路径:paddle.distributed.parallel.init_parallel_env
新增公开接口:paddle.nn.functional.temporal_shift
static中新增部分公开api

__all__ += optimizer.__all__
__all__ += checkpoint.__all__
__all__ = [ # noqa
'LookAhead', 'ModelAverage', 'auto_checkpoint'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

和其它一样分行写?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里相对还好只有3个,目前这样也还比较清楚。
我看看有没有其他修改一起顺便改了,如果没有其他要改的就下次再改也行吧

@@ -244,9 +244,8 @@
from .framework import load # noqa: F401
from .framework import DataParallel # noqa: F401

from .framework import set_default_dtype #DEFINE_ALIAS
from .framework import get_default_dtype #DEFINE_ALIAS
from .framework import set_grad_enabled #DEFINE_ALIAS
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里是否要改成 from .framework import set_grad_enabled # noqa: F401
或者说set_grad_enabled 如果不公开了,那下面的__all__列表中也要删除set_grad_enabled

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里的set_grad_enabled是重复导入,跟242行重复了。

XiaoguangHu01
XiaoguangHu01 previously approved these changes Jun 4, 2021
Copy link
Contributor

@XiaoguangHu01 XiaoguangHu01 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

'set_device',
'get_device',
'XPUPlace',
'is_compiled_with_xpu',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

注意后面Examples: .. code-block:: python里的写法要和最终推荐路径都保持一致,现在比较杂乱,比如有:support_xpu = paddle.device.is_compiled_with_xpu()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done,已经进行修改

@@ -68,7 +53,7 @@ def is_compiled_with_xpu():
.. code-block:: python

import paddle
support_xpu = paddle.device.is_compiled_with_xpu()
support_xpu = paddle.is_compiled_with_xpu()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这几个API,考虑到接下来还会增加device相关的api,我建议保留paddle.device层级更好些。
如果全都移到根目录下的话,当前还好,未来这类api会比较分散。
兼容性考虑,根目录下的api需要保留别名,但不用加到all列表。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Contributor

@jzhang533 jzhang533 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

python/paddle/init.py:25:9: F821 undefined name 'batch'
python/paddle/init.py:138:1: F811 redefinition of unused 'transpose' from line 93
python/paddle/init.py:194:1: F811 redefinition of unused 'atan' from line 151
python/paddle/init.py:283:17: E262 inline comment should start with '# '
python/paddle/batch.py:63:22: E712 comparison to False should be 'if cond is False:' or 'if not cond:'
python/paddle/compat.py:22:17: F821 undefined name 'long'
python/paddle/device.py:117:52: E502 the backslash is redundant between brackets
python/paddle/device.py:123:52: E502 the backslash is redundant between brackets
python/paddle/device.py:138:75: E502 the backslash is redundant between brackets
python/paddle/device.py:147:75: E502 the backslash is redundant between brackets
python/paddle/distributed/parallel.py:153:9: F841 local variable 'ep_rank' is assigned to but never used
python/paddle/incubate/optimizer/lookahead.py:285:9: F841 local variable 'parameter_list' is assigned to but never used
python/paddle/incubate/optimizer/modelaverage.py:418:17: F841 local variable 'num_updates' is assigned to but never used
python/paddle/incubate/optimizer/modelaverage.py:509:9: F841 local variable 'num_updates' is assigned to but never used
python/paddle/incubate/optimizer/modelaverage.py:517:51: E711 comparison to None should be 'if cond is None:'
python/paddle/incubate/optimizer/modelaverage.py:519:51: E711 comparison to None should be 'if cond is None:'
python/paddle/tensor/init.py:28:1: F811 redefinition of unused 'eye' from line 21
python/paddle/tensor/init.py:85:1: F811 redefinition of unused 'transpose' from line 39
python/paddle/tensor/init.py:146:1: F811 redefinition of unused 'atan' from line 97

fix some flake8 errors
@zhiboniu
Copy link
Contributor Author

zhiboniu commented Jun 11, 2021

python/paddle/init.py:25:9: F821 undefined name 'batch'
python/paddle/init.py:138:1: F811 redefinition of unused 'transpose' from line 93
python/paddle/init.py:194:1: F811 redefinition of unused 'atan' from line 151
python/paddle/init.py:283:17: E262 inline comment should start with '# '
python/paddle/batch.py:63:22: E712 comparison to False should be 'if cond is False:' or 'if not cond:'
python/paddle/compat.py:22:17: F821 undefined name 'long'
python/paddle/device.py:117:52: E502 the backslash is redundant between brackets
python/paddle/device.py:123:52: E502 the backslash is redundant between brackets
python/paddle/device.py:138:75: E502 the backslash is redundant between brackets
python/paddle/device.py:147:75: E502 the backslash is redundant between brackets
python/paddle/distributed/parallel.py:153:9: F841 local variable 'ep_rank' is assigned to but never used
python/paddle/incubate/optimizer/lookahead.py:285:9: F841 local variable 'parameter_list' is assigned to but never used
python/paddle/incubate/optimizer/modelaverage.py:418:17: F841 local variable 'num_updates' is assigned to but never used
python/paddle/incubate/optimizer/modelaverage.py:509:9: F841 local variable 'num_updates' is assigned to but never used
python/paddle/incubate/optimizer/modelaverage.py:517:51: E711 comparison to None should be 'if cond is None:'
python/paddle/incubate/optimizer/modelaverage.py:519:51: E711 comparison to None should be 'if cond is None:'
python/paddle/tensor/init.py:28:1: F811 redefinition of unused 'eye' from line 21
python/paddle/tensor/init.py:85:1: F811 redefinition of unused 'transpose' from line 39
python/paddle/tensor/init.py:146:1: F811 redefinition of unused 'atan' from line 97

done

@zhiboniu zhiboniu closed this Jun 11, 2021
@zhiboniu zhiboniu reopened this Jun 11, 2021
Copy link
Collaborator

@raindrops2sea raindrops2sea left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Contributor

@jzhang533 jzhang533 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Copy link
Contributor

@XiaoguangHu01 XiaoguangHu01 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@XiaoguangHu01 XiaoguangHu01 merged commit 022198c into PaddlePaddle:develop Jun 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants