Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 21 additions & 20 deletions python/paddle/common_ops_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,32 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from paddle.fluid.layer_helper import LayerHelper # noqa: F401
from paddle.fluid.param_attr import ParamAttr # noqa: F401
import paddle.fluid as fluid # noqa: F401
from paddle.fluid import core, dygraph_utils # noqa: F401
from paddle.fluid.core import VarDesc # noqa: F401
from paddle.fluid.data_feeder import ( # noqa: F401
check_dtype,
check_type,
check_variable_and_dtype,
convert_dtype,
)
from paddle.fluid.framework import ( # noqa: F401
convert_np_dtype_to_dtype_,
OpProtoHolder,
Variable,
_dygraph_tracer,
_in_legacy_dygraph,
_non_static_mode,
_varbase_creator,
in_dygraph_mode,
_in_legacy_dygraph,
)
from paddle.fluid.framework import ( # noqa: F401
device_guard,
convert_np_dtype_to_dtype_,
default_main_program,
device_guard,
dygraph_only,
_dygraph_tracer,
in_dygraph_mode,
)
from paddle.fluid.framework import OpProtoHolder, Variable # noqa: F401
from paddle.fluid.initializer import Constant # noqa: F401
from paddle.fluid.core import VarDesc # noqa: F401
from paddle.fluid import core, dygraph_utils # noqa: F401
from paddle.fluid.data_feeder import ( # noqa: F401
check_type,
check_dtype,
check_variable_and_dtype,
convert_dtype,
)
from paddle.fluid.layer_helper import LayerHelper # noqa: F401
from paddle.fluid.layers import fill_constant, utils # noqa: F401
from paddle.tensor.layer_function_generator import templatedoc # noqa: F401
import paddle.fluid as fluid # noqa: F401
from paddle.fluid.layers.layer_function_generator import ( # noqa: F401
Copy link
Member Author

@SigureMo SigureMo Nov 25, 2022

Choose a reason for hiding this comment

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

因为格式化后变化有点多,这里的实际 diff 如下:

# python/paddle/common_ops_import.py
- from paddle.tensor.layer_function_generator import templatedoc  # noqa: F401
+ from paddle.fluid.layers.layer_function_generator import (  # noqa: F401
+     templatedoc,
+ )

# python/paddle/tensor/math.py
- from paddle.common_ops_import import templatedoc
+ from .layer_function_generator import templatedoc

因为按照默认排序在 import paddle 时就会报如下错误

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/aistudio/Projects/Paddle/build/python/paddle/__init__.py", line 30, in <module>
    monkey_patch_variable()
  File "/home/aistudio/Projects/Paddle/build/python/paddle/fluid/layers/math_op_patch.py", line 600, in monkey_patch_variable
    import paddle.tensor
  File "/home/aistudio/Projects/Paddle/build/python/paddle/tensor/__init__.py", line 15, in <module>
    from .attribute import is_complex  # noqa: F401
  File "/home/aistudio/Projects/Paddle/build/python/paddle/tensor/attribute.py", line 24, in <module>
    from ..static import Variable
  File "/home/aistudio/Projects/Paddle/build/python/paddle/static/__init__.py", line 18, in <module>
    from . import nn  # noqa: F401
  File "/home/aistudio/Projects/Paddle/build/python/paddle/static/nn/__init__.py", line 15, in <module>
    from .common import fc  # noqa: F401
  File "/home/aistudio/Projects/Paddle/build/python/paddle/static/nn/common.py", line 20, in <module>
    from paddle.common_ops_import import (
  File "/home/aistudio/Projects/Paddle/build/python/paddle/common_ops_import.py", line 40, in <module>
    from paddle.tensor.layer_function_generator import templatedoc  # noqa: F401
  File "/home/aistudio/Projects/Paddle/build/python/paddle/tensor/layer_function_generator.py", line 32, in <module>
    from ..static import Variable

貌似是因为有循环依赖 tensor -> static -> common_ops_import -> tensor,因为这里 common_ops_import 包含一个从 paddle.tensor import 的函数 templatedoclayer_function_generator 里的函数)

这就导致任何 import common_ops_import 的模块都会去 import tensor,很容易产生循环依赖,因此这里将这个 templatedoc 改为从 fluid.layers 里直接 import 了(tensor 下的 templatedoc 是从 fluid.layers copy 过来的,内容完全一致)

修改后 common_ops_import 里的全是 fluid 下的 API,可以保证非 fluid 模块引用时不会产生循环依赖

这里用图说明下改动:

Before After
circular-import-before drawio circular-import-after drawio

之后 common_ops_import 里的 templatedoc 应该会在清理 fluid 的时候就被清理掉了

templatedoc,
)
from paddle.fluid.param_attr import ParamAttr # noqa: F401
5 changes: 2 additions & 3 deletions python/paddle/tensor/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@

# Define functions about array.

from ..fluid.data_feeder import check_type, check_variable_and_dtype
from ..framework import LayerHelper, _non_static_mode, core
from ..static import Variable
from ..framework import LayerHelper, core, _non_static_mode
from ..fluid.data_feeder import check_type
from ..fluid.data_feeder import check_variable_and_dtype

__all__ = []

Expand Down
17 changes: 7 additions & 10 deletions python/paddle/tensor/attribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,18 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from ..framework import core
from ..framework import LayerHelper
from ..fluid.data_feeder import check_variable_and_dtype
from ..fluid.data_feeder import check_type
# TODO: define functions to get tensor attributes

from .creation import assign
from .creation import _complex_to_real_dtype
import numpy as np

# TODO: define functions to get tensor attributes
import paddle
from paddle import _C_ops, _legacy_C_ops
from ..static import Variable
from ..fluid.framework import _in_legacy_dygraph, in_dygraph_mode

import numpy as np
from ..fluid.data_feeder import check_type, check_variable_and_dtype
from ..fluid.framework import _in_legacy_dygraph, in_dygraph_mode
from ..framework import LayerHelper, core
from ..static import Variable
from .creation import _complex_to_real_dtype, assign

__all__ = []

Expand Down
39 changes: 23 additions & 16 deletions python/paddle/tensor/creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,39 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import numpy as np
# TODO: define functions to get create a tensor

import math
import re
import warnings

import numpy as np

import paddle
from paddle import _C_ops, _legacy_C_ops
from paddle.common_ops_import import fill_constant
from ..fluid.layers import utils
from ..static import Variable, device_guard
from ..framework import _current_expected_place, _get_paddle_place
from ..framework import core
from ..framework import in_dygraph_mode, _non_static_mode
from ..framework import LayerHelper

from ..fluid.data_feeder import (
check_variable_and_dtype,
check_type,
check_dtype,
check_type,
check_variable_and_dtype,
convert_dtype,
)
from ..framework import convert_np_dtype_to_dtype_

# TODO: define functions to get create a tensor
import paddle
from paddle import _C_ops, _legacy_C_ops
from ..fluid.framework import (
_in_legacy_dygraph,
_in_eager_without_dygraph_check,
_in_legacy_dygraph,
)
import warnings
from ..fluid.layers import utils
from ..framework import (
LayerHelper,
_current_expected_place,
_get_paddle_place,
_non_static_mode,
convert_np_dtype_to_dtype_,
core,
in_dygraph_mode,
)
from ..static import Variable, device_guard

__all__ = []

Expand Down
21 changes: 11 additions & 10 deletions python/paddle/tensor/einsum.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,23 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import collections
import itertools
import numpy as np
import re
import string

import numpy as np
import opt_einsum

from .linalg import matmul, transpose
from .manipulation import squeeze, unsqueeze, reshape
from .math import multiply
from .math import sum as paddle_sum
from ..fluid.framework import _in_legacy_dygraph
from paddle import _C_ops, _legacy_C_ops

from ..fluid.data_feeder import check_type, check_variable_and_dtype
from ..fluid.layer_helper import LayerHelper
from ..fluid.framework import _in_legacy_dygraph, in_dygraph_mode
import collections
import string
import opt_einsum
from ..fluid.layer_helper import LayerHelper
from .linalg import matmul, transpose
from .manipulation import reshape, squeeze, unsqueeze
from .math import multiply
from .math import sum as paddle_sum

__all__ = []

Expand Down
13 changes: 7 additions & 6 deletions python/paddle/tensor/layer_function_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,23 @@
# limitations under the License.

import re
import warnings
import string

import warnings
from io import StringIO
from ..static import Variable

from paddle import _C_ops, _legacy_C_ops

from ..fluid.data_feeder import check_variable_and_dtype
from ..fluid.proto import framework_pb2
from ..framework import (
LayerHelper,
OpProtoHolder,
_non_static_mode,
convert_np_dtype_to_dtype_,
core,
in_dygraph_mode,
)
from ..framework import LayerHelper
from ..fluid.data_feeder import check_variable_and_dtype
from paddle import _C_ops, _legacy_C_ops
from ..static import Variable

__all__ = []

Expand Down
24 changes: 12 additions & 12 deletions python/paddle/tensor/linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,23 @@
# limitations under the License.

import numpy as np
from ..framework import LayerHelper
from ..framework import _non_static_mode, in_dygraph_mode

import paddle
from paddle import _C_ops, _legacy_C_ops
from paddle.common_ops_import import VarDesc

from ..fluid.data_feeder import (
check_variable_and_dtype,
check_type,
check_dtype,
check_type,
check_variable_and_dtype,
)
from ..static import Variable
from ..fluid.framework import _in_legacy_dygraph
from .manipulation import cast
from .math import multiply, add
from .logic import logical_not
from ..framework import LayerHelper, _non_static_mode, in_dygraph_mode
from ..static import Variable
from .creation import full

import paddle
from paddle.common_ops_import import VarDesc
from paddle import _C_ops, _legacy_C_ops
from .logic import logical_not
from .manipulation import cast
from .math import add, multiply

__all__ = []

Expand Down
17 changes: 8 additions & 9 deletions python/paddle/tensor/logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,26 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# TODO: define logic functions of a tensor

import paddle
from ..fluid.data_feeder import check_type, check_variable_and_dtype
from .layer_function_generator import templatedoc
from ..static import Variable

# TODO: define logic functions of a tensor
from ..fluid.data_feeder import check_type, check_variable_and_dtype
from ..fluid.framework import _in_eager_mode_
from ..static import Variable
from .layer_function_generator import templatedoc

if _in_eager_mode_:
Tensor = paddle.fluid.framework.core.eager.Tensor
else:
from ..framework import VarBase as Tensor

from ..framework import in_dygraph_mode
from ..framework import LayerHelper
from ..fluid.framework import _in_legacy_dygraph

# TODO: define logic functions of a tensor
from paddle import _C_ops, _legacy_C_ops
from paddle.tensor.creation import full

from ..fluid.framework import _in_legacy_dygraph
from ..framework import LayerHelper, in_dygraph_mode

__all__ = []


Expand Down
42 changes: 23 additions & 19 deletions python/paddle/tensor/manipulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,35 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# TODO: define functions to manipulate a tensor

import warnings
from collections import Counter

from ..static import Variable
from ..framework import core, in_dygraph_mode
from ..fluid.framework import _in_legacy_dygraph, _non_static_mode
from ..framework import LayerHelper
from ..framework import convert_np_dtype_to_dtype_, dygraph_only
from ..fluid.data_feeder import (
convert_dtype,
check_variable_and_dtype,
check_type,
check_dtype,
)
from ..fluid.layers import utils
import numpy as np

# TODO: define functions to manipulate a tensor
from ..fluid.dygraph.inplace_utils import inplace_apis_in_dygraph_only
import paddle
from paddle import _C_ops, _legacy_C_ops
from ..common_ops_import import dygraph_utils, fill_constant, _varbase_creator
import warnings
from .creation import zeros
from .creation import _complex_to_real_dtype
from .creation import _real_to_complex_dtype

from ..common_ops_import import _varbase_creator, dygraph_utils, fill_constant
from ..fluid.data_feeder import (
check_dtype,
check_type,
check_variable_and_dtype,
convert_dtype,
)
from ..fluid.dygraph.inplace_utils import inplace_apis_in_dygraph_only
from ..fluid.framework import _in_legacy_dygraph, _non_static_mode
from ..fluid.layers import utils
from ..framework import (
LayerHelper,
convert_np_dtype_to_dtype_,
core,
dygraph_only,
in_dygraph_mode,
)
from ..static import Variable
from .creation import _complex_to_real_dtype, _real_to_complex_dtype, zeros

__all__ = []

Expand Down
Loading