From 10b0b0b57cd454792a5011d53f98b528038e6dec Mon Sep 17 00:00:00 2001 From: Zhenghai Zhang <65210872+ccsuzzh@users.noreply.github.com> Date: Thu, 21 Sep 2023 11:07:18 +0800 Subject: [PATCH] [CodeStyle][task 39] enable isort in `python/paddle/base` (part1) (#57413) * enable isort rule in python/paddle/base * fix bug * fix bug * fix bug --- pyproject.toml | 3 +- python/paddle/base/backward.py | 19 ++++---- python/paddle/base/compiler.py | 19 ++++---- python/paddle/base/data_feed_desc.py | 3 +- python/paddle/base/data_feeder.py | 13 +++--- python/paddle/base/dataset.py | 6 ++- python/paddle/base/default_scope_funcs.py | 3 +- python/paddle/base/dygraph/base.py | 16 ++++--- python/paddle/base/dygraph/math_op_patch.py | 10 ++--- .../base/dygraph/tensor_patch_methods.py | 34 +++++++------- python/paddle/base/dygraph/tracer.py | 3 +- python/paddle/base/dygraph_utils.py | 3 +- python/paddle/base/executor.py | 33 ++++++-------- .../incubate/checkpoint/auto_checkpoint.py | 11 ++--- python/paddle/base/initializer.py | 3 +- python/paddle/base/io.py | 1 + python/paddle/base/layer_helper.py | 11 ++--- python/paddle/base/layer_helper_base.py | 9 ++-- python/paddle/base/layers/io.py | 6 +-- .../base/layers/layer_function_generator.py | 13 +++--- python/paddle/base/layers/math_op_patch.py | 11 +++-- python/paddle/base/lod_tensor.py | 3 +- python/paddle/base/multiprocess_utils.py | 7 ++- python/paddle/base/param_attr.py | 2 +- python/paddle/base/reader.py | 44 +++++++++---------- python/paddle/base/trainer_desc.py | 2 +- python/paddle/base/trainer_factory.py | 26 ++++++----- python/paddle/base/unique_name.py | 3 +- python/paddle/base/variable_index.py | 24 +++++----- python/paddle/base/wrapped_decorator.py | 3 +- 30 files changed, 172 insertions(+), 172 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index e11ab2108c2be..8dd98b65873aa 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -16,8 +16,9 @@ skip = ["build", "third_party", "__init__.py"] extend_skip_glob = [ # These files do not need to be formatted, # see .flake8 for more details - "python/paddle/base/**", "python/paddle/utils/gast/**", + "python/paddle/base/core.py", + "python/paddle/base/framework.py", ] [tool.ruff] diff --git a/python/paddle/base/backward.py b/python/paddle/base/backward.py index 563e423e0c7ea..1f3f67a98b640 100755 --- a/python/paddle/base/backward.py +++ b/python/paddle/base/backward.py @@ -12,23 +12,20 @@ # See the License for the specific language governing permissions and # limitations under the License. -from .proto import framework_pb2 - -from paddle.base import framework as framework -from paddle.base import program_guard -from . import core import collections import copy import logging -from . import unique_name -from . import log_helper -import paddle.base -from .data_feeder import check_type +import re import warnings - from collections.abc import Sequence -import re +import paddle.base +from paddle.base import framework as framework +from paddle.base import program_guard + +from . import core, log_helper, unique_name +from .data_feeder import check_type +from .proto import framework_pb2 __all__ = [ 'append_backward', diff --git a/python/paddle/base/compiler.py b/python/paddle/base/compiler.py index 69ae6f1d31344..3ee939920dc2b 100644 --- a/python/paddle/base/compiler.py +++ b/python/paddle/base/compiler.py @@ -14,9 +14,9 @@ import sys import warnings -from . import framework -from .framework import cuda_places, cpu_places, xpu_places -from . import core + +from . import core, framework +from .framework import cpu_places, cuda_places, xpu_places __all__ = [ 'CompiledProgram', @@ -399,10 +399,11 @@ def convert_concrete_program( """ Convert the ConcreteProgram to IPUConcreteProgram. """ - from ..base.dygraph.base import switch_to_static_graph + import paddle + from ..base import backward + from ..base.dygraph.base import switch_to_static_graph from ..base.framework import device_guard - import paddle inputs = concrete_program.inputs outputs = concrete_program.outputs @@ -508,14 +509,12 @@ def patch_program_cache(ipu_strategy): Returns: None """ + from paddle.jit.dy2static import logging_utils + from paddle.jit.dy2static.partial_program import partial_program_from from paddle.jit.dy2static.program_translator import ( + MAX_TRACED_PROGRAM_COUNT, CacheKey, ProgramCache, - MAX_TRACED_PROGRAM_COUNT, - ) - from paddle.jit.dy2static import logging_utils - from paddle.jit.dy2static.partial_program import ( - partial_program_from, ) old_getter = ProgramCache.__getitem__ diff --git a/python/paddle/base/data_feed_desc.py b/python/paddle/base/data_feed_desc.py index 8aa69890f1933..de1b00d090bb1 100644 --- a/python/paddle/base/data_feed_desc.py +++ b/python/paddle/base/data_feed_desc.py @@ -12,9 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -from paddle.base.proto import data_feed_pb2 from google.protobuf import text_format +from paddle.base.proto import data_feed_pb2 + __all__ = ['DataFeedDesc'] diff --git a/python/paddle/base/data_feeder.py b/python/paddle/base/data_feeder.py index 78781a6856af1..52ed983ffa729 100644 --- a/python/paddle/base/data_feeder.py +++ b/python/paddle/base/data_feeder.py @@ -12,20 +12,21 @@ # See the License for the specific language governing permissions and # limitations under the License. -from . import core -import numpy as np -import warnings import struct +import warnings + +import numpy as np +from ..ir import OpResult +from . import core from .framework import ( Variable, + _cpu_num, + _cuda_ids, default_main_program, in_dygraph_mode, in_pir_mode, ) -from .framework import _cpu_num, _cuda_ids - -from ..ir import OpResult __all__ = ['DataFeeder'] diff --git a/python/paddle/base/dataset.py b/python/paddle/base/dataset.py index 533fb69a6621b..961a392349707 100644 --- a/python/paddle/base/dataset.py +++ b/python/paddle/base/dataset.py @@ -13,10 +13,12 @@ # limitations under the License. """This is definition of dataset class, which is high performance IO.""" -from paddle.base.proto import data_feed_pb2 from google.protobuf import text_format -from . import core + +from paddle.base.proto import data_feed_pb2 + from ..utils import deprecated +from . import core __all__ = ['DatasetFactory', 'InMemoryDataset', 'QueueDataset'] diff --git a/python/paddle/base/default_scope_funcs.py b/python/paddle/base/default_scope_funcs.py index 80cfe40db57ad..992714e6cd409 100644 --- a/python/paddle/base/default_scope_funcs.py +++ b/python/paddle/base/default_scope_funcs.py @@ -26,9 +26,10 @@ invoked in a new local scope. """ -import paddle.base.core import threading +import paddle.base.core + __tl_scope__ = threading.local() __all__ = [ diff --git a/python/paddle/base/dygraph/base.py b/python/paddle/base/dygraph/base.py index 7edb748026d84..d85fc8ca25bf7 100644 --- a/python/paddle/base/dygraph/base.py +++ b/python/paddle/base/dygraph/base.py @@ -11,20 +11,22 @@ # 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 ..wrapped_decorator import signature_safe_contextmanager, wrap_decorator -import decorator import inspect import sys +import warnings + +import decorator import numpy as np -from paddle.base import core -from paddle.base import framework + +import paddle +from paddle.base import core, framework from paddle.base.framework import global_var from paddle.base.multiprocess_utils import CleanupFuncRegistrar -from .tracer import Tracer + from ..data_feeder import convert_dtype -import warnings from ..framework import _get_paddle_place -import paddle +from ..wrapped_decorator import signature_safe_contextmanager, wrap_decorator +from .tracer import Tracer __all__ = [ 'no_grad', diff --git a/python/paddle/base/dygraph/math_op_patch.py b/python/paddle/base/dygraph/math_op_patch.py index 9448d7d9de9dd..5972b545f93e2 100644 --- a/python/paddle/base/dygraph/math_op_patch.py +++ b/python/paddle/base/dygraph/math_op_patch.py @@ -12,15 +12,13 @@ # See the License for the specific language governing permissions and # limitations under the License. -from .. import core -from ..framework import ( - convert_np_dtype_to_dtype_, -) -from .. import framework - import numpy as np + from paddle import _C_ops, _legacy_C_ops +from .. import core, framework +from ..framework import convert_np_dtype_to_dtype_ + _supported_int_dtype_ = [ core.VarDesc.VarType.UINT8, core.VarDesc.VarType.INT8, diff --git a/python/paddle/base/dygraph/tensor_patch_methods.py b/python/paddle/base/dygraph/tensor_patch_methods.py index 8026884c34fc8..4f1b138abaae4 100644 --- a/python/paddle/base/dygraph/tensor_patch_methods.py +++ b/python/paddle/base/dygraph/tensor_patch_methods.py @@ -13,33 +13,33 @@ # limitations under the License. import inspect -import numpy as np -import warnings import sys +import warnings + +import numpy as np import paddle -from .. import framework -from ..framework import convert_np_dtype_to_dtype_ -from .. import core -from .. import unique_name +import paddle.profiler as profiler +import paddle.utils.deprecated as deprecated +from paddle import _C_ops +from paddle.base.data_feeder import ( + _PADDLE_DTYPE_2_NUMPY_DTYPE, + convert_uint16_to_float, +) +from paddle.profiler.utils import in_profiler_mode + +from .. import core, framework, unique_name from ..framework import ( - Variable, + EagerParamBase, Parameter, + Variable, _getitem_static, - _setitem_static, _setitem_impl_, - EagerParamBase, + _setitem_static, + convert_np_dtype_to_dtype_, ) from .base import switch_to_static_graph from .math_op_patch import monkey_patch_math_tensor -from paddle.base.data_feeder import ( - convert_uint16_to_float, - _PADDLE_DTYPE_2_NUMPY_DTYPE, -) -import paddle.utils.deprecated as deprecated -import paddle.profiler as profiler -from paddle.profiler.utils import in_profiler_mode -from paddle import _C_ops _grad_scalar = None diff --git a/python/paddle/base/dygraph/tracer.py b/python/paddle/base/dygraph/tracer.py index 35cbe88f91f87..4df9517073c66 100644 --- a/python/paddle/base/dygraph/tracer.py +++ b/python/paddle/base/dygraph/tracer.py @@ -13,9 +13,8 @@ # limitations under the License. -from paddle.base import core -from paddle.base import framework from paddle import _C_ops, _legacy_C_ops +from paddle.base import core, framework name_mapping = { "graph_send_recv": { diff --git a/python/paddle/base/dygraph_utils.py b/python/paddle/base/dygraph_utils.py index 655a5f4f8b773..926c4680017ce 100644 --- a/python/paddle/base/dygraph_utils.py +++ b/python/paddle/base/dygraph_utils.py @@ -12,9 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -from .framework import dygraph_only from paddle import _legacy_C_ops +from .framework import dygraph_only + @dygraph_only def _append_activation_in_dygraph( diff --git a/python/paddle/base/executor.py b/python/paddle/base/executor.py index e5fddd15329e3..0921d7b79d14b 100755 --- a/python/paddle/base/executor.py +++ b/python/paddle/base/executor.py @@ -12,36 +12,31 @@ # See the License for the specific language governing permissions and # limitations under the License. +import copy import logging import os import sys import warnings -import numpy as np +from functools import lru_cache -from . import set_flags, get_flags -from .framework import Program, default_main_program +import numpy as np from ..ir import OpResult -from .wrapped_decorator import signature_safe_contextmanager +from . import compiler, core, framework, get_flags, set_flags, unique_name from .data_feeder import convert_dtype -from .framework import Variable, Operator, in_pir_mode - from .framework import ( - convert_np_dtype_to_dtype_, + Operator, + Program, + Variable, _apply_pass, + convert_np_dtype_to_dtype_, + default_main_program, + in_pir_mode, paddle_type_to_proto_type, ) - -from . import core -from . import unique_name -from . import compiler -from .trainer_factory import TrainerFactory -from .trainer_factory import FetchHandlerMonitor -import copy -from . import framework from .incubate.checkpoint import auto_checkpoint as acp - -from functools import lru_cache +from .trainer_factory import FetchHandlerMonitor, TrainerFactory +from .wrapped_decorator import signature_safe_contextmanager __all__ = ['Executor', 'global_scope', 'scope_guard'] @@ -614,8 +609,8 @@ def _to_str(var): def _prepare_fleet_executor(): - from ..distributed.fleet.proto import fleet_executor_desc_pb2 from ..distributed.backup_env import getenv_or_backup + from ..distributed.fleet.proto import fleet_executor_desc_pb2 trainer_endpoints_str = getenv_or_backup("PADDLE_TRAINER_ENDPOINTS", "") trainer_endpoints = trainer_endpoints_str.split(',') @@ -945,7 +940,7 @@ def _get_program_and_executor(self, cached_data): # print(f"Program after convert:\n {inner_program}", flush=True) else: build_strategy = None - from paddle.incubate.autograd import prim_enabled, prim2orig + from paddle.incubate.autograd import prim2orig, prim_enabled if prim_enabled() and program == default_main_program(): prim2orig() diff --git a/python/paddle/base/incubate/checkpoint/auto_checkpoint.py b/python/paddle/base/incubate/checkpoint/auto_checkpoint.py index 23239b692c975..e8f75f3a4ed55 100644 --- a/python/paddle/base/incubate/checkpoint/auto_checkpoint.py +++ b/python/paddle/base/incubate/checkpoint/auto_checkpoint.py @@ -12,16 +12,17 @@ # See the License for the specific language governing permissions and # limitations under the License. -import sys -import logging import json +import logging import os +import sys import time from threading import current_thread -from paddle.base import unique_name, compiler -from .checkpoint_saver import SerializableBase, CheckpointSaver, PaddleModel -from paddle.base.framework import in_dygraph_mode, Program +from paddle.base import compiler, unique_name +from paddle.base.framework import Program, in_dygraph_mode + +from .checkpoint_saver import CheckpointSaver, PaddleModel, SerializableBase g_train_epoch_range = None g_checker = None diff --git a/python/paddle/base/initializer.py b/python/paddle/base/initializer.py index 3902281721eac..7443e63b13e52 100644 --- a/python/paddle/base/initializer.py +++ b/python/paddle/base/initializer.py @@ -12,9 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -from .data_feeder import check_type import paddle +from .data_feeder import check_type + __all__ = ['set_global_initializer'] _global_weight_initializer_ = None diff --git a/python/paddle/base/io.py b/python/paddle/base/io.py index 89468f88648e8..a2c7d02ede349 100644 --- a/python/paddle/base/io.py +++ b/python/paddle/base/io.py @@ -15,6 +15,7 @@ import logging from paddle.base.log_helper import get_logger + from . import reader from .reader import * diff --git a/python/paddle/base/layer_helper.py b/python/paddle/base/layer_helper.py index e6be93e777b75..312eaf67a3320 100644 --- a/python/paddle/base/layer_helper.py +++ b/python/paddle/base/layer_helper.py @@ -13,18 +13,19 @@ # limitations under the License. import copy + import paddle + +from . import unique_name +from .dygraph_utils import _append_activation_in_dygraph from .framework import ( Parameter, + _global_flags, dtype_is_floating, in_dygraph_mode, - _global_flags, ) -from . import unique_name -from .param_attr import ParamAttr - from .layer_helper_base import LayerHelperBase -from .dygraph_utils import _append_activation_in_dygraph +from .param_attr import ParamAttr class LayerHelper(LayerHelperBase): diff --git a/python/paddle/base/layer_helper_base.py b/python/paddle/base/layer_helper_base.py index b7bc6c6b8585e..6c047c08766fe 100644 --- a/python/paddle/base/layer_helper_base.py +++ b/python/paddle/base/layer_helper_base.py @@ -13,21 +13,22 @@ # limitations under the License. import copy + import numpy as np + import paddle +from . import core, unique_name from .framework import ( Variable, + _current_expected_place, default_main_program, default_startup_program, in_dygraph_mode, in_pir_mode, - _current_expected_place, ) -from . import unique_name +from .initializer import _global_bias_initializer, _global_weight_initializer from .param_attr import ParamAttr, WeightNormParamAttr -from . import core -from .initializer import _global_weight_initializer, _global_bias_initializer __all__ = ['LayerHelperBase'] diff --git a/python/paddle/base/layers/io.py b/python/paddle/base/layers/io.py index d4aa7734aee6f..51f5b10fe0618 100644 --- a/python/paddle/base/layers/io.py +++ b/python/paddle/base/layers/io.py @@ -14,13 +14,9 @@ from .. import core from ..executor import global_scope -from ..framework import ( - default_main_program, - default_startup_program, -) +from ..framework import default_main_program, default_startup_program from ..unique_name import generate as unique_name - __all__ = [] diff --git a/python/paddle/base/layers/layer_function_generator.py b/python/paddle/base/layers/layer_function_generator.py index 1b1b85d00ea42..bd11a412ffc5b 100644 --- a/python/paddle/base/layers/layer_function_generator.py +++ b/python/paddle/base/layers/layer_function_generator.py @@ -13,21 +13,22 @@ # limitations under the License. import re -import warnings import string - +import warnings from io import StringIO -from ..proto import framework_pb2 + +from paddle import _C_ops, _legacy_C_ops + +from ..data_feeder import check_variable_and_dtype from ..framework import ( OpProtoHolder, Variable, - core, convert_np_dtype_to_dtype_, + core, in_dygraph_mode, ) from ..layer_helper import LayerHelper -from ..data_feeder import check_variable_and_dtype -from paddle import _C_ops, _legacy_C_ops +from ..proto import framework_pb2 __all__ = [ 'generate_layer_fn', diff --git a/python/paddle/base/layers/math_op_patch.py b/python/paddle/base/layers/math_op_patch.py index 06f384eae23d1..53f35939b1f3a 100644 --- a/python/paddle/base/layers/math_op_patch.py +++ b/python/paddle/base/layers/math_op_patch.py @@ -12,13 +12,14 @@ # See the License for the specific language governing permissions and # limitations under the License. -import warnings import inspect +import warnings + +from paddle.base.dygraph.base import in_to_static_mode from .. import core -from ..framework import Variable, unique_name, static_only +from ..framework import Variable, static_only, unique_name from .layer_function_generator import OpProtoHolder -from paddle.base.dygraph.base import in_to_static_mode _supported_int_dtype_ = [ core.VarDesc.VarType.BOOL, @@ -354,9 +355,7 @@ def pop(self, *args): Returns: Variable: self[index] """ - from paddle.jit.dy2static.convert_operators import ( - _run_paddle_pop, - ) + from paddle.jit.dy2static.convert_operators import _run_paddle_pop if self.type != core.VarDesc.VarType.LOD_TENSOR_ARRAY: raise TypeError( diff --git a/python/paddle/base/lod_tensor.py b/python/paddle/base/lod_tensor.py index 96e18ec8f3bde..4be41d5cc6adc 100644 --- a/python/paddle/base/lod_tensor.py +++ b/python/paddle/base/lod_tensor.py @@ -12,9 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. +import numpy as np + from . import core from .data_feeder import DataToLoDTensorConverter -import numpy as np __all__ = ['create_lod_tensor', 'create_random_int_lodtensor'] diff --git a/python/paddle/base/multiprocess_utils.py b/python/paddle/base/multiprocess_utils.py index b763446930fdb..8d18db0bb3ea8 100644 --- a/python/paddle/base/multiprocess_utils.py +++ b/python/paddle/base/multiprocess_utils.py @@ -12,14 +12,13 @@ # See the License for the specific language governing permissions and # limitations under the License. -import sys -import signal import atexit +import queue +import signal +import sys from . import core -import queue - # multi-process worker check indices queue interval, avoid # hanging in subprocess data loading MP_STATUS_CHECK_INTERVAL = 5.0 diff --git a/python/paddle/base/param_attr.py b/python/paddle/base/param_attr.py index a17432fcc3df2..674c4ad4328c5 100644 --- a/python/paddle/base/param_attr.py +++ b/python/paddle/base/param_attr.py @@ -13,8 +13,8 @@ # limitations under the License. import paddle -from paddle.regularizer import WeightDecayRegularizer from paddle.base.data_feeder import check_type +from paddle.regularizer import WeightDecayRegularizer __all__ = [ 'ParamAttr', diff --git a/python/paddle/base/reader.py b/python/paddle/base/reader.py index 63b97ee2bd495..c3a65721db275 100644 --- a/python/paddle/base/reader.py +++ b/python/paddle/base/reader.py @@ -12,44 +12,44 @@ # See the License for the specific language governing permissions and # limitations under the License. -from . import core +import logging +import multiprocessing +import queue import sys -import numpy as np import threading +import warnings + +import numpy as np + import paddle +from paddle.base.framework import _set_expected_place +from . import core +from .data_feeder import BatchedTensorProvider, DataFeeder +from .executor import global_scope from .framework import ( Program, - program_guard, + _current_expected_place, + _get_paddle_place, + _get_paddle_place_list, default_main_program, default_startup_program, in_dygraph_mode, - _current_expected_place, + program_guard, ) -from .executor import global_scope -from .data_feeder import DataFeeder, BatchedTensorProvider +from .layers.io import ( + __create_unshared_decorated_reader__, + _copy_reader_var_, + monkey_patch_reader_methods, +) +from .multiprocess_utils import _cleanup # noqa: F401 +from .multiprocess_utils import multiprocess_queue_set # noqa: F401 from .multiprocess_utils import ( - multiprocess_queue_set, # noqa: F401 CleanupFuncRegistrar, _cleanup_mmap, - _cleanup, # noqa: F401 _set_SIGCHLD_handler, ) -from .layers.io import ( - monkey_patch_reader_methods, - _copy_reader_var_, - __create_unshared_decorated_reader__, -) from .unique_name import UniqueNameGenerator -from .framework import _get_paddle_place, _get_paddle_place_list -from paddle.base.framework import _set_expected_place -import logging -import warnings - -### Dygraph DataLoader configs ### -import multiprocessing - -import queue # NOTE: [ avoid hanging & failed quickly ] These value is used in getting data from another process QUEUE_GET_TIMEOUT = 60 diff --git a/python/paddle/base/trainer_desc.py b/python/paddle/base/trainer_desc.py index 48cc427ac8e7e..f64530ec02353 100644 --- a/python/paddle/base/trainer_desc.py +++ b/python/paddle/base/trainer_desc.py @@ -13,8 +13,8 @@ # limitations under the License. """Definition of trainers.""" -import sys import os +import sys __all__ = [ 'TrainerDesc', diff --git a/python/paddle/base/trainer_factory.py b/python/paddle/base/trainer_factory.py index cf197fab524e0..75351872d73d6 100644 --- a/python/paddle/base/trainer_factory.py +++ b/python/paddle/base/trainer_factory.py @@ -13,33 +13,35 @@ # limitations under the License. """Definition of TrainerFactory.""" +import logging import threading import time -import logging + import numpy as np + from paddle.base.log_helper import get_logger local_logger = get_logger( __name__, logging.INFO, fmt='%(asctime)s-%(levelname)s: %(message)s' ) -from .trainer_desc import ( # noqa: F401 - MultiTrainer, - DistMultiTrainer, - PipelineTrainer, - HeterXpuTrainer, - PSGPUTrainer, - HeterPipelineTrainer, -) from .device_worker import ( # noqa: F401 - Hogwild, - DownpourSGD, DownpourLite, - Section, + DownpourSGD, DownpourSGDOPT, HeterSection, + Hogwild, + Section, ) from .framework import Variable +from .trainer_desc import ( # noqa: F401 + DistMultiTrainer, + HeterPipelineTrainer, + HeterXpuTrainer, + MultiTrainer, + PipelineTrainer, + PSGPUTrainer, +) __all__ = ["TrainerFactory", "FetchHandlerMonitor"] diff --git a/python/paddle/base/unique_name.py b/python/paddle/base/unique_name.py index 745675767f150..c240273da890d 100644 --- a/python/paddle/base/unique_name.py +++ b/python/paddle/base/unique_name.py @@ -13,6 +13,7 @@ # limitations under the License. import collections + from .wrapped_decorator import signature_safe_contextmanager __all__ = ['generate', 'switch', 'guard'] @@ -121,7 +122,7 @@ def generate(key): # NOTE(zhiqiu): use c++ unique_name_generator in dygraph mode, # in order to keep name consistency. def generate_with_ignorable_key(key): - from .framework import in_dygraph_mode, _dygraph_tracer + from .framework import _dygraph_tracer, in_dygraph_mode if in_dygraph_mode(): return _dygraph_tracer()._generate_unique_name() diff --git a/python/paddle/base/variable_index.py b/python/paddle/base/variable_index.py index 1b3039c5a8cbe..dcc87b74ea658 100644 --- a/python/paddle/base/variable_index.py +++ b/python/paddle/base/variable_index.py @@ -12,13 +12,14 @@ # See the License for the specific language governing permissions and # limitations under the License. +import itertools +import warnings + import numpy as np -from . import unique_name -from . import core + import paddle -import warnings -import itertools +from . import core, unique_name MAX_INTEGER = 2**31 - 1 @@ -370,9 +371,7 @@ def _setitem_for_tensor_array(var, item, value): not paddle.in_dynamic_mode() ), "setitem for tensor_array must be called in static graph mode." if isinstance(item, (Variable, int)): - from paddle.jit.dy2static.variable_trans_func import ( - to_static_variable, - ) + from paddle.jit.dy2static.variable_trans_func import to_static_variable from paddle.tensor import array_write item = paddle.cast(to_static_variable(item), dtype='int64') @@ -388,7 +387,8 @@ def _setitem_for_tensor_array(var, item, value): def _setitem_impl_(var, item, value): from paddle.base import core - from .framework import default_main_program, Variable + + from .framework import Variable, default_main_program if var.type == core.VarDesc.VarType.LOD_TENSOR_ARRAY: return _setitem_for_tensor_array(var, item, value) @@ -572,9 +572,7 @@ def _setitem_impl_(var, item, value): if not paddle.in_dynamic_mode(): # map var to the new output - from paddle.jit.dy2static.program_translator import ( - ProgramTranslator, - ) + from paddle.jit.dy2static.program_translator import ProgramTranslator ProgramTranslator.get_instance()._inplace_map.add( cur_block.program, var.desc.id(), output @@ -601,8 +599,8 @@ def set_value_for_bool_tensor(var, item, value): ) def idx_not_empty(var, item, value): - from .framework import Variable from ..tensor import gather_nd, scatter_nd_add + from .framework import Variable if not isinstance(value, Variable): value = paddle.assign(value).cast(var.dtype) @@ -826,7 +824,7 @@ def _setitem_static(x, indices, values): indices(int|slice|None|Tensor|List|Tuple...): Indices, used to indicate the position of the element to be fetched. values(Tensor|Number|Ndarray): values to be assigned to the x. """ - from .framework import default_main_program, Variable + from .framework import Variable, default_main_program if x.type == paddle.base.core.VarDesc.VarType.LOD_TENSOR_ARRAY: return _setitem_for_tensor_array(x, indices, values) diff --git a/python/paddle/base/wrapped_decorator.py b/python/paddle/base/wrapped_decorator.py index 7e7dbff65611e..1567bb0d4c55c 100644 --- a/python/paddle/base/wrapped_decorator.py +++ b/python/paddle/base/wrapped_decorator.py @@ -12,9 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -import decorator import contextlib +import decorator + __all__ = ['wrap_decorator', 'signature_safe_contextmanager']